From eacc88cf682e9c3c283488283c7582d6a085e470 Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 21 Dec 2024 20:21:17 +0000 Subject: [PATCH] More generative tests for ops and transactions --- .../main/java/convex/core/cvm/ops/Local.java | 2 +- .../java/convex/core/cvm/AddressTest.java | 23 +++++++++++++ .../test/java/convex/core/cvm/GenTestOps.java | 33 +++++++++++++++++++ .../java/convex/test/generators/OpsGen.java | 2 +- .../test/generators/TransactionGen.java | 9 +++-- 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 convex-core/src/test/java/convex/core/cvm/GenTestOps.java diff --git a/convex-core/src/main/java/convex/core/cvm/ops/Local.java b/convex-core/src/main/java/convex/core/cvm/ops/Local.java index 27b87f90b..5f57cacab 100644 --- a/convex-core/src/main/java/convex/core/cvm/ops/Local.java +++ b/convex-core/src/main/java/convex/core/cvm/ops/Local.java @@ -49,7 +49,7 @@ public Context execute(Context ctx) { AVector env=ctx.getLocalBindings(); long ec=env.count(); if ((position<0)||(position>=ec)) { - return ctx.withError(ErrorCodes.BOUNDS,"Bad position for Local: "+position); + return ctx.withError(ErrorCodes.BOUNDS,"Bad position for Local: "+position).consumeJuice(Juice.LOOKUP); } T result = (T)env.get(position); return ctx.withResult(Juice.LOOKUP,result); diff --git a/convex-core/src/test/java/convex/core/cvm/AddressTest.java b/convex-core/src/test/java/convex/core/cvm/AddressTest.java index 35d7cad6a..4602588e7 100644 --- a/convex-core/src/test/java/convex/core/cvm/AddressTest.java +++ b/convex-core/src/test/java/convex/core/cvm/AddressTest.java @@ -2,7 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +import java.io.IOException; import org.junit.jupiter.api.Test; @@ -10,8 +14,11 @@ import convex.core.data.Blob; import convex.core.data.Blobs; import convex.core.data.BlobsTest; +import convex.core.data.Cells; import convex.core.data.Format; +import convex.core.data.Hash; import convex.core.data.ObjectsTest; +import convex.core.data.Ref; import convex.core.exceptions.BadFormatException; import convex.core.util.Utils; @@ -51,6 +58,22 @@ public void testParse() { assertEquals("#16",Address.parse("0x0000000000000010").toString()); } + @Test + public void doHashTest() { + Address a=Address.ZERO; + Hash h=Hash.get(a); + + try { + a=Cells.persist(a); + } catch (IOException e) { + fail(e); + } + Ref r = Ref.get(a); + assertSame(a, r.getValue()); // shouldn't get GC'd because we have a strong reference + assertEquals(h,r.getHash()); + + } + @Test public void testCanonical() { Address a=Address.create(17); diff --git a/convex-core/src/test/java/convex/core/cvm/GenTestOps.java b/convex-core/src/test/java/convex/core/cvm/GenTestOps.java new file mode 100644 index 000000000..c03247221 --- /dev/null +++ b/convex-core/src/test/java/convex/core/cvm/GenTestOps.java @@ -0,0 +1,33 @@ +package convex.core.cvm; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.runner.RunWith; + +import com.pholser.junit.quickcheck.From; +import com.pholser.junit.quickcheck.Property; +import com.pholser.junit.quickcheck.runner.JUnitQuickcheck; + +import convex.core.init.InitTest; +import convex.test.generators.OpsGen; + +@RunWith(JUnitQuickcheck.class) +public class GenTestOps { + + @SuppressWarnings("rawtypes") + @Property + public void testOpExecution(@From(OpsGen.class) AOp op) { + // A context should be able to execute any valid Op without throwing + Context c=Context.create(InitTest.STATE); + long initialJuice=c.getJuiceUsed(); + assertEquals(0,initialJuice); + c=c.execute(op); + assertEquals(0,c.getDepth()); + + assertTrue(c.getJuiceUsed()>initialJuice); + + } + + +} diff --git a/convex-core/src/test/java/convex/test/generators/OpsGen.java b/convex-core/src/test/java/convex/test/generators/OpsGen.java index 01cdb4bdf..1416194f6 100644 --- a/convex-core/src/test/java/convex/test/generators/OpsGen.java +++ b/convex-core/src/test/java/convex/test/generators/OpsGen.java @@ -29,7 +29,7 @@ public AOp generate(SourceOfRandomness r, GenerationStatus status) { switch (r.nextInt(8)) { case 0: { // flat multi-ops supporting any ops as children - int n=(int) Math.sqrt(r.nextInt(size)); + int n=(int) (Math.sqrt(r.nextInt(1+size))); AOp[] ops=new AOp[n]; for (int i=0; i op=Gen.OP.generate(r, status); + return Invoke.create(origin,seq,op); default: - AVector args=Gen.VECTOR.generate(r, status); - return Invoke.create(origin,seq, args); + ACell form=Gen.FORM.generate(r, status); + return Invoke.create(origin,seq, form); } } }