From 00f1cf17b72c76d8dfbc3a10d1770eaea813a0c6 Mon Sep 17 00:00:00 2001 From: maxonfjvipon Date: Wed, 10 Apr 2024 12:49:37 +0300 Subject: [PATCH] fix(#3057): tests + qulice --- eo-runtime/src/main/eo/org/eolang/cage.eo | 3 - eo-runtime/src/main/eo/org/eolang/memory.eo | 4 - .../src/main/java/EOorg/EOeolang/Cages.java | 1 - .../EOeolang/EOcage$EOencaged$EOencage.java | 1 + .../EOeolang/EOcage$EOencaged$EOfree.java | 1 + .../EOeolang/EOcage$EOencaged$EOfreed.java | 63 ------ .../EOcage$EOencaged$EO\317\206.java" | 4 +- .../java/EOorg/EOeolang/EOcage$EOnew.java | 175 --------------- .../EOmemory$EOallocated$EOfreed.java | 64 ------ .../src/test/eo/org/eolang/cage-tests.eo | 207 ++++++++---------- .../src/test/eo/org/eolang/runtime-tests.eo | 22 +- .../test/java/EOorg/EOeolang/CagesTest.java | 2 +- .../test/java/EOorg/EOeolang/EOcageTest.java | 2 +- 13 files changed, 101 insertions(+), 448 deletions(-) delete mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfreed.java delete mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java delete mode 100644 eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOallocated$EOfreed.java diff --git a/eo-runtime/src/main/eo/org/eolang/cage.eo b/eo-runtime/src/main/eo/org/eolang/cage.eo index 68466832ae..91c4f36f23 100644 --- a/eo-runtime/src/main/eo/org/eolang/cage.eo +++ b/eo-runtime/src/main/eo/org/eolang/cage.eo @@ -47,6 +47,3 @@ # Just remove object from the cage. [] > free /true - - # Remove object from the cage and return it. - [] > freed /? diff --git a/eo-runtime/src/main/eo/org/eolang/memory.eo b/eo-runtime/src/main/eo/org/eolang/memory.eo index 91f4c78553..7846063a8c 100644 --- a/eo-runtime/src/main/eo/org/eolang/memory.eo +++ b/eo-runtime/src/main/eo/org/eolang/memory.eo @@ -38,7 +38,6 @@ # m # dataization leads to reading from memory # m.@ # taking @ attribute also leads to reading from memory # m.free # clear memory -# m.freed # clear memory and return the data # ``` # Clearing the memory is optional and is up to programmer. [data] > memory @@ -68,6 +67,3 @@ # Free memory. [] > free ^.pointer.free > @ - - # Free memory and return the data. - [] > freed /bytes diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/Cages.java b/eo-runtime/src/main/java/EOorg/EOeolang/Cages.java index e3dc68c2c2..ce27585f21 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/Cages.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/Cages.java @@ -48,7 +48,6 @@ final class Cages { */ private final ConcurrentHashMap objects = new ConcurrentHashMap<>(0); - /** * Ctor. */ diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOencage.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOencage.java index 6e61966153..53032e2f9a 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOencage.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOencage.java @@ -40,6 +40,7 @@ /** * Cage.encaged.encage object. * @since 0.36.0 + * @checkstyle TypeNameCheck (5 lines) */ @Versionized @XmirObject(oname = "cage.encaged.encage") diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfree.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfree.java index 2d70056179..6eccdcdc51 100644 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfree.java +++ b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfree.java @@ -40,6 +40,7 @@ /** * Cage.encaged.free object. * @since 0.36.0 + * @checkstyle TypeNameCheck (5 lines) */ @Versionized @XmirObject(oname = "cage.encaged.free") diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfreed.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfreed.java deleted file mode 100644 index d6997bbbad..0000000000 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EOfreed.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* - * @checkstyle PackageNameCheck (4 lines) - */ -package EOorg.EOeolang; - -import org.eolang.Atom; -import org.eolang.Attr; -import org.eolang.Param; -import org.eolang.PhDefault; -import org.eolang.PhTracedLocator; -import org.eolang.Phi; -import org.eolang.Versionized; -import org.eolang.XmirObject; - -/** - * Cage.encaged.freed object. - * @since 0.36.0 - */ -@Versionized -@XmirObject(oname = "cage.encaged.freed") -final class EOcage$EOencaged$EOfreed extends PhDefault implements Atom { - /** - * Ctor. - * @param sigma Sigma - */ - EOcage$EOencaged$EOfreed(final Phi sigma) { - super(sigma); - } - - @Override - public Phi lambda() throws Exception { - final int locator = Math.toIntExact( - new Param(this.take(Attr.RHO), "locator").strong(Long.class) - ); - final Phi object = Cages.INSTANCE.get(locator); - Cages.INSTANCE.remove(locator); - return object; - } -} diff --git "a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EO\317\206.java" "b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EO\317\206.java" index a7afa31437..589bbdcc88 100644 --- "a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EO\317\206.java" +++ "b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOencaged$EO\317\206.java" @@ -39,6 +39,7 @@ /** * Cage.encaged.φ object. * @since 0.36.0 + * @checkstyle TypeNameCheck (5 lines) */ @Versionized @XmirObject(oname = "cage.encaged.@") @@ -56,9 +57,8 @@ public Phi lambda() throws Exception { final int locator = Math.toIntExact( new Param(this.take(Attr.RHO), "locator").strong(Long.class) ); - final Phi obj = Cages.INSTANCE.get(locator); return new PhTracedLocator( - obj, + Cages.INSTANCE.get(locator), locator ); } diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java deleted file mode 100644 index 7f4abccbbd..0000000000 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOcage$EOnew.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* - * @checkstyle PackageNameCheck (4 lines) - */ - -package EOorg.EOeolang; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; -import org.eolang.AtSimple; -import org.eolang.Atom; -import org.eolang.Attr; -import org.eolang.Data; -import org.eolang.ExFailure; -import org.eolang.PhDefault; -import org.eolang.PhTracedLocator; -import org.eolang.PhWrite; -import org.eolang.Phi; -import org.eolang.Term; -import org.eolang.XmirObject; - -/** - * Cage.alloc object. - * @since 0.36.0 - * @checkstyle TypeNameCheck (5 lines) - */ -@XmirObject(oname = "cage.new") -public final class EOcage$EOnew extends PhDefault implements Atom { - /** - * Locator calculator. - */ - private static final AtomicInteger LOCATOR = new AtomicInteger(0); - - /** - * Cage for objects. - */ - private static final ConcurrentHashMap CAGES = new ConcurrentHashMap<>(0); - - /** - * Ctor. - * @param sigma Sigma - */ - EOcage$EOnew(final Phi sigma) { - super(sigma); - this.add("it", new EOcage$EOnew.AtEncaged()); - this.add( - "encage", - new AtSimple( - new PhWrite( - this, - "it", - rho -> new Data.ToPhi(true) - ) - ) - ); - } - - @Override - public Phi lambda() throws Exception { - return this.take("it"); - } - - /** - * Attribute that stores object. - * @since 0.36.0 - */ - private static class AtEncaged implements Attr { - /** - * Locator of encaged object. - */ - private Integer locator; - - /** - * Form of the stored object. - */ - private String forma; - - /** - * Ctor. - */ - AtEncaged() { - this(null, null); - } - - /** - * Ctor for copying. - * @param locator Locator of object in memory - * @param form The form of the object - */ - AtEncaged(final Integer locator, final String form) { - this.locator = locator; - this.forma = form; - } - - @Override - public Attr copy(final Phi self) { - return new EOcage$EOnew.AtEncaged(this.locator, this.forma); - } - - @Override - public Phi get() { - if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { - throw new ExFailure( - "There's no object in storage, can't read" - ); - } - return new PhTracedLocator(EOcage$EOnew.CAGES.get(this.locator), this.locator); - } - - @Override - public boolean put(final Phi phi) { - if (this.forma == null) { - this.forma = phi.forma(); - } else if (!this.forma.equals(phi.forma())) { - throw new ExFailure( - "Can't write an object formed by %s because object formed by %s was saved before", - phi.forma(), - this.forma - ); - } - if (this.locator == null) { - synchronized (EOcage$EOnew.LOCATOR) { - this.locator = EOcage$EOnew.LOCATOR.incrementAndGet(); - } - } - EOcage$EOnew.CAGES.put(this.locator, phi); - return true; - } - - @Override - public String φTerm() { - final String txt; - if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { - txt = Term.EMPTY; - } else { - txt = EOcage$EOnew.CAGES.get(this.locator).φTerm(); - } - return txt; - } - - @Override - public String toString() { - final String txt; - if (this.locator == null || !EOcage$EOnew.CAGES.containsKey(this.locator)) { - txt = Term.EMPTY; - } else { - txt = EOcage$EOnew.CAGES.get(this.locator).toString(); - } - return txt; - } - } -} diff --git a/eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOallocated$EOfreed.java b/eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOallocated$EOfreed.java deleted file mode 100644 index cdcf9162c7..0000000000 --- a/eo-runtime/src/main/java/EOorg/EOeolang/EOmemory$EOallocated$EOfreed.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2016-2024 Objectionary.com - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* - * @checkstyle PackageNameCheck (4 lines) - */ -package EOorg.EOeolang; - -import org.eolang.Atom; -import org.eolang.Attr; -import org.eolang.Data; -import org.eolang.Dataized; -import org.eolang.Param; -import org.eolang.PhDefault; -import org.eolang.Phi; -import org.eolang.Versionized; -import org.eolang.XmirObject; - -/** - * Memory.allocated.freed object. - * @since 0.36.0 - * @checkstyle TypeNameCheck (5 lines) - */ -@Versionized -@XmirObject(oname = "memory.allocated.freed") -final class EOmemory$EOallocated$EOfreed extends PhDefault implements Atom { - /** - * Ctor. - * @param sigma Sigma - */ - EOmemory$EOallocated$EOfreed(final Phi sigma) { - super(sigma); - } - - @Override - public Phi lambda() throws Exception { - final Phi data = new Data.ToPhi( - new Param(this).strong(byte[].class) - ); - new Dataized(this.take(Attr.RHO).take("free")).take(); - return data; - } -} diff --git a/eo-runtime/src/test/eo/org/eolang/cage-tests.eo b/eo-runtime/src/test/eo/org/eolang/cage-tests.eo index 556df0025d..5be3a16eff 100644 --- a/eo-runtime/src/test/eo/org/eolang/cage-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/cage-tests.eo @@ -27,102 +27,64 @@ +version 0.0.0 # Test. -# @todo #2931:30min Enable the tests: dataizes-encaged-object-lazily-third and -# dataizes-encaged-object-not-lazily-first. The tests were disabled because current cage -# implementation is wrong (see: https://github.com/objectionary/eo/issues/3057). -# We need to enable them when cage is great again. [] > encages-into-cage # Object with free attribute. [z] > a - cage.new a > x - eq. > @ - seq - * - x.encage - a 7 - x.z - 7 - -# Test. -[] > does-not-avoid-infinite-loop - cage.new > x - if - x' > copy - try > @ - seq - * - x.encage - if TRUE 1 0 - copy.it - x.encage - if TRUE copy 0 - x - [e] - QQ.io.stdout e > @ - TRUE + (cage a).new > x + seq > @ + * + x.encage + a 7 + and. + x.z.eq 7 + x.free # Test. -[] > dataizes-encaged-object-not-lazily-first - cage.new 0 > x - cage.new int.plus > sum - eq. > res - seq - * - x.encage 42 - sum.encage - x.as-int.plus 1 - x.encage 7 - sum - 8 - TRUE > @ +[] > checks-forma-and-fixes + (cage 0).new > x + (cage int.plus).new > sum + seq > @ + * + x.encage 42 + sum.encage + x.plus 1 + x.encage 7 + and. + and. + sum.eq 43 + sum.free + x.free # Test. [] > dataizes-encaged-object-lazily-second - cage.new 0 > x - cage.new int.plus > sum - eq. > @ - seq - * - x.encage 42 - sum.encage - 1.plus x - x.encage 7 - sum - 8 - -# Test. -[] > dataizes-encaged-object-lazily-third - cage.new 0 > x - cage.new 0 > y - cage.new int.plus > sum - eq. > res - seq - * - x.encage 42 - y.encage 7 - sum.encage - x.as-int.plus y - y.encage 13 - x.encage 4 - sum - 17 - TRUE > @ + (cage 0).new > x + (cage int.plus).new > sum + seq > @ + * + x.encage 42 + sum.encage + 1.plus x + x.encage 7 + and. + sum.eq 8 + and. + sum.free + x.free # Test. [] > stores-abstract-object-into-cage - eq. > @ - seq - * - cage.new > c - [] - 112 > @ - c.@ - 112 + new. > c + cage + [] + 112 > @ + and. > @ + c.eq 112 + c.free # Test. [] > multi-layer-volatility (memory 0).alloc > ma - cage.new a > ca + (cage a).new > ca # Layer A. [] > a # Layer A.phi. @@ -136,7 +98,7 @@ ma.write (ma.as-int.plus 1) ma.as-int (memory 0).alloc > mb - cage.new b > cb + (cage b).new > cb # Layer B. [] > b # Layer B.phi. @@ -150,15 +112,19 @@ mb.write (mb.as-int.plus 1) mb.as-int and. > @ - eq. - seq - * - ca - ca.plus 40 - seq - * - cb.z - cb.z.plus 40 + and. + eq. + seq + * + ca + ca.plus 40 + seq + * + cb.z + cb.z.plus 40 + and. + cb.free + ca.free and. ma.free mb.free @@ -170,24 +136,25 @@ # Add. [y] > add pyint (^.value.plus y.value) > @ - cage.new pyint > x - cage.new > tmp - x.add - eq. > @ - seq - * - x.encage - pyint 0 - tmp.encage - x.add (pyint 1) - QQ.io.stdout - tmp.value + (cage pyint).new > x + (cage x.add).new > tmp + seq > @ + * + x.encage + pyint 0 + tmp.encage + x.add (pyint 1) + QQ.io.stdout tmp.value - 1 + and. + tmp.value.eq 1 + and. + x.free + tmp.free # Test. [] > catches-encaging-objects-of-different-formas - cage.new 5 > cge + (cage int).new > cge seq > @ * try @@ -195,13 +162,14 @@ [e] cge.encage 10 > @ nop - cge.eq 10 + and. + cge.eq 10 + cge.free # Check that cage stack leads to error. Just prints the error # message if passes. [] > catches-stack-overflow - cage.new > cge - int.plus + (cage int.plus).new > cge try > @ [] seq > @ @@ -211,16 +179,21 @@ cge FALSE [stack-overflow-message] - QQ.io.stdout > @ - stack-overflow-message + and. > @ + QQ.io.stdout stack-overflow-message + cge.free FALSE # Test. [] > applies-after-retrieval - # Func. - [x] > func - 2.plus x > @ - cage.new func > c - eq. > @ - c.it 5 - 7 + new. > c + cage + [] + # Func. + [x] > func + 2.plus x > @ + and. > @ + eq. + c.func 5 + 7 + c.free diff --git a/eo-runtime/src/test/eo/org/eolang/runtime-tests.eo b/eo-runtime/src/test/eo/org/eolang/runtime-tests.eo index 3787db554f..7e94d93b3c 100644 --- a/eo-runtime/src/test/eo/org/eolang/runtime-tests.eo +++ b/eo-runtime/src/test/eo/org/eolang/runtime-tests.eo @@ -114,7 +114,7 @@ # Test. [] > writes-cage-to-memory (memory 0).alloc > f1 - cage.new int.plus > f2 + (cage int.plus).new > f2 (memory 0).alloc > f0 seq > @ * @@ -124,11 +124,11 @@ f1.write f2 and. and. - eq. - f1 - 3 + f1.eq 3 f1.free - f0.free + and. + f0.free + f2.free # Test. [] > recursion-without-arguments @@ -188,18 +188,6 @@ [] > a TRUE > @ -# Test. -[] > calling-caged-function - # Func. - [] > func - # Inner object. - [x] > @ - 2.plus x > @ - cage.new func > h - eq. > @ - h.@ 8 - 10 - # Test. [] > app-that-calls-func # App. diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java index a1d5f32e43..9492c59368 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/CagesTest.java @@ -42,7 +42,7 @@ */ class CagesTest { @Test - void initializeObjectForTheFirstTime() { + void initializesObjectForTheFirstTime() { final Phi phi = new PhFake(); final int locator = Cages.INSTANCE.init(phi); Assertions.assertDoesNotThrow( diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOcageTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOcageTest.java index ca65ed5c87..32b58d2e8a 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOcageTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOcageTest.java @@ -54,7 +54,7 @@ import org.junit.jupiter.api.parallel.ExecutionMode; /** - * Test cases for {@link EOcage} and {@link EOcage$EOφ}. + * Test cases for {@link EOcage}. * @since 0.19 */ final class EOcageTest {