Skip to content

Commit

Permalink
More generative tests for ops and transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 21, 2024
1 parent a500ef4 commit eacc88c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Local.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Context execute(Context ctx) {
AVector<ACell> 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);
Expand Down
23 changes: 23 additions & 0 deletions convex-core/src/test/java/convex/core/cvm/AddressTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@

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;

import convex.core.data.ACell;
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;

Expand Down Expand Up @@ -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<ACell> 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);
Expand Down
33 changes: 33 additions & 0 deletions convex-core/src/test/java/convex/core/cvm/GenTestOps.java
Original file line number Diff line number Diff line change
@@ -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);

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<n; i++) {
ops[i]=Gen.OP.generate(r,status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import convex.core.Constants;
import convex.core.cvm.AOp;
import convex.core.cvm.Address;
import convex.core.cvm.transactions.ATransaction;
import convex.core.cvm.transactions.Call;
Expand Down Expand Up @@ -49,10 +50,14 @@ public ATransaction generate(SourceOfRandomness r, GenerationStatus status) {
int mode=r.nextInt(Multi.MODE_ANY, Multi.MODE_UNTIL);
return Multi.create(origin, seq, mode, txs);
}

case 3:
AOp<?> 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);
}
}
}

0 comments on commit eacc88c

Please sign in to comment.