Skip to content

Commit

Permalink
More generative tests and validation checks
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Dec 21, 2024
1 parent 96516a6 commit a500ef4
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 33 deletions.
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/cvm/ops/Special.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class Special<T extends ACell> extends AOp<T> {
private final byte specialCode;

public static final int NUM_SPECIALS=25;
private static final int BASE=0;
public static final int BASE=0;
private static final int LIMIT=BASE+NUM_SPECIALS;
public static final Symbol[] SYMBOLS=new Symbol[NUM_SPECIALS];
private static final Special<?>[] specials=new Special[NUM_SPECIALS];
Expand Down
22 changes: 5 additions & 17 deletions convex-core/src/main/java/convex/core/data/ARecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ protected ARecord<K,V> toCanonical() {
* @return Vector of Values
*/
@Override
public AVector<V> values() {
int n=size();
ACell[] os=new ACell[n];
RecordFormat format=getFormat();
for (int i=0; i<n; i++) {
os[i]=get(format.getKey(i));
}
return Vectors.wrap(os);
}
public abstract AVector<V> values();

/**
* Gets the record field content for a given key, or null if not found.
Expand All @@ -103,18 +95,14 @@ public AVector<V> values() {
* @return Array of Values in this record
*/
public ACell[] getValuesArray() {
int n=size();
ACell[] result=new ACell[n];
AVector<Keyword> keys=getFormat().getKeys();
for (int i=0; i<n; i++) {
result[i]=get(keys.get(i));
}
return result;
return values().toCellArray();
}

@Override
public boolean containsKey(ACell key) {
return getFormat().containsKey(key);
RecordFormat format=getFormat();
if (format==null) return false;
return format.containsKey(key);
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions convex-core/src/main/java/convex/core/data/SetLeaf.java
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ protected void validateWithPrefix(Hash prefix, int digit, int position) throws I
}
}
e.validate();

T value=e.getValue();
if(!Cells.isCVM(value)) throw new InvalidDataException("Non-CVM value in Set",this);

}
}

Expand Down
6 changes: 6 additions & 0 deletions convex-core/src/main/java/convex/core/data/SignedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,10 @@ public static <T extends ACell> SignedData<T> fromData(AHashMap<Keyword, ACell>
ASignature sig=ASignature.fromBlob(RT.ensureBlob(value.get(Keywords.SIGNATURE)));
return create(key,sig,ref);
}

@Override
public AVector<ACell> values() {
// TODO Auto-generated method stub
return Vectors.create(pubKey,signature,valueRef.getValue());
}
}
10 changes: 4 additions & 6 deletions convex-core/src/main/java/convex/core/data/type/CAD3Type.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package convex.core.data.type;

import convex.core.cvm.Address;
import convex.core.data.ACell;
import convex.core.data.prim.ByteFlag;

Expand All @@ -19,12 +18,12 @@ private CAD3Type() {

@Override
public boolean check(ACell value) {
return value instanceof Address;
return true;
}

@Override
public String toString () {
return "Address";
return "CAD3";
}

@Override
Expand All @@ -33,9 +32,8 @@ public ACell defaultValue() {
}

@Override
public Address implicitCast(ACell a) {
if (a instanceof Address) return (Address)a;
return null;
public ACell implicitCast(ACell a) {
return a;
}

@Override
Expand Down
2 changes: 0 additions & 2 deletions convex-core/src/test/java/convex/comms/GenTestFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import convex.core.data.Ref;
import convex.core.data.Strings;
import convex.core.exceptions.BadFormatException;
import convex.core.lang.RT;
import convex.test.generators.PrimitiveGen;
import convex.test.generators.ValueGen;

Expand Down Expand Up @@ -56,7 +55,6 @@ public void dataRoundTrip(@From(ValueGen.class) ACell value) throws BadFormatExc
Blob b = Cells.encode(value);
ACell o = Format.read(b);

assertEquals(RT.getType(value), RT.getType(o));
assertEquals(value, o);
assertEquals(b, Cells.encode(o));
assertEquals(pref.getValue(), o);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public class AdversarialDataTest {
assertEquals(1,b.shift);
}

@Test public void testBadSetLeafs() {
@Test public void testBadSetLeafs() throws InvalidDataException {
CVMLong a=CVMLong.ZERO;
CVMLong b=CVMLong.ONE;
if (a.getHash().compareTo(b.getHash())>0) {
Expand All @@ -138,8 +138,7 @@ public class AdversarialDataTest {
invalidTest(Sets.of(NON_VALID));

// Inserting non-CVM values into existing valid sets
invalidTest(Sets.of(1,2,3,4).include(NON_CVM));
invalidTest(Samples.LONG_SET_100.conj(NON_CVM));
Cells.validate(Sets.of(1,2,3,4).include(NON_CVM));
}

@Test public void testBadKeywords() {
Expand Down
2 changes: 2 additions & 0 deletions convex-core/src/test/java/convex/test/generators/Gen.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ public class Gen {
public static final ByteFlagGen BYTE_FLAG = new ByteFlagGen();

public static final DenseRecordGen DENSE_RECORD = new DenseRecordGen();

public static final OpsGen OP = new OpsGen();
}
55 changes: 55 additions & 0 deletions convex-core/src/test/java/convex/test/generators/OpsGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package convex.test.generators;

import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;

import convex.core.cvm.AOp;
import convex.core.cvm.ops.Cond;
import convex.core.cvm.ops.Constant;
import convex.core.cvm.ops.Do;
import convex.core.cvm.ops.Invoke;
import convex.core.cvm.ops.Local;
import convex.core.cvm.ops.Query;
import convex.core.cvm.ops.Special;

/**
* Generator for plausible forms
*/
@SuppressWarnings("rawtypes")
public class OpsGen extends Generator<AOp> {
public OpsGen() {
super(AOp.class);
}

@Override
public AOp generate(SourceOfRandomness r, GenerationStatus status) {
int size=status.size();

switch (r.nextInt(8)) {
case 0: {
// flat multi-ops supporting any ops as children
int n=(int) Math.sqrt(r.nextInt(size));
AOp[] ops=new AOp[n];
for (int i=0; i<n; i++) {
ops[i]=Gen.OP.generate(r,status);
}
switch(r.nextInt(4)) {
case 0: return Do.create(ops);
case 1: return Cond.create(ops);
case 2: return Invoke.create(ops);
case 3: return Query.create(ops);
}
}

case 1:
return Special.create(r.nextInt(Special.BASE, Special.BASE+Special.NUM_SPECIALS-1));

case 2:
return Local.create(r.nextInt(1+size));

default:
return Constant.of(Gen.VALUE.generate(r, status));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ATransaction generate(SourceOfRandomness r, GenerationStatus status) {

Address origin = Address.create(r.nextInt(1+size));
Address dst = Address.create(r.nextInt(1+size));
long seq = r.nextInt(size);
long seq = r.nextInt(1+size);

switch (r.nextInt(10)) {
case 0: {
Expand Down

0 comments on commit a500ef4

Please sign in to comment.