Skip to content

Commit 5370a17

Browse files
committed
More generative testing enhancements
1 parent eacc88c commit 5370a17

25 files changed

+187
-31
lines changed

convex-core/src/main/java/convex/core/cpos/Belief.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,8 @@ public Index<AccountKey, SignedData<Order>> getOrders() {
179179

180180
@Override
181181
public boolean equals(ACell a) {
182-
if (!(a instanceof Belief)) return false;
183-
Belief as=(Belief)a;
184-
return equals(as);
182+
if (a instanceof Belief) return equals((Belief)a);
183+
return super.equals(a);
185184
}
186185

187186
/**

convex-core/src/main/java/convex/core/cvm/ops/Invoke.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected Invoke(AVector<AOp<ACell>> ops) {
3030
}
3131

3232
public static <T extends ACell> Invoke<T> create(ASequence<AOp<ACell>> ops) {
33+
if (ops.count()==0) return null;
3334
AVector<AOp<ACell>> vops = ops.toVector();
3435
return new Invoke<T>(vops);
3536
}

convex-core/src/main/java/convex/core/data/ACAD3Record.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void validateStructure() throws InvalidDataException {
4040
public boolean equals(ACell a) {
4141
if (a==null) return false;
4242
if (a.getTag()!=tag) return false;
43-
return encoding.equals(a.getEncoding());
43+
return getEncoding().equals(a.getEncoding());
4444
}
4545

4646
@Override

convex-core/src/main/java/convex/core/data/AVector.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ public AType getType() {
6161
*/
6262
@Override
6363
public abstract T get(long i);
64+
65+
/**
66+
* Remove an element at the specified position in a vector. WARNING: likely to be O(n)
67+
* @param i
68+
* @return Shortened Vector, or null if position was invalid
69+
*/
70+
public abstract AVector<T> dissocAt(long i);
6471

6572
/**
6673
* Appends a ListVector chunk to this vector. This vector must contain a whole

convex-core/src/main/java/convex/core/data/MapEntry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,11 @@ protected void visitAllChildren(Consumer<AVector<ACell>> visitor) {
290290
// nothing to visit
291291
}
292292

293+
@Override
294+
public AVector<ACell> dissocAt(long i) {
295+
if (i==0) return Vectors.create(get(1));
296+
if (i==1) return Vectors.create(get(0));
297+
return null;
298+
}
299+
293300
}

convex-core/src/main/java/convex/core/data/VectorArray.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package convex.core.data;
22

3+
import java.util.Arrays;
34
import java.util.ListIterator;
45
import java.util.NoSuchElementException;
56
import java.util.function.BiFunction;
@@ -295,6 +296,16 @@ public Ref<ACell> getRef(int i) {
295296
return getCanonical().getRef(i);
296297
}
297298

299+
@Override
300+
public AVector<T> dissocAt(long i) {
301+
int n=(int)count;
302+
if ((i<0)||(i>=n)) return null;
303+
ACell[] cells=Arrays.copyOf(data, n-1);
304+
System.arraycopy(data, (int)(i+1), cells, (int)i, (int)(n-i-1));
305+
306+
return wrap(cells);
307+
}
308+
298309

299310

300311

convex-core/src/main/java/convex/core/data/VectorLeaf.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package convex.core.data;
22

3+
import java.util.Arrays;
34
import java.util.ListIterator;
45
import java.util.NoSuchElementException;
56
import java.util.function.BiFunction;
@@ -437,6 +438,10 @@ public void add(T e) {
437438

438439
}
439440

441+
/**
442+
* Get the length of the prefix vector, will be 0 if no prefix
443+
* @return
444+
*/
440445
public long prefixLength() {
441446
return count - items.length;
442447
}
@@ -737,4 +742,24 @@ protected void visitAllChildren(Consumer<AVector<T>> visitor) {
737742
visitor.accept(child);
738743
}
739744
}
745+
746+
@Override
747+
public AVector<T> dissocAt(long i) {
748+
if ((i<0)||(i>=count)) return null;
749+
long pl=prefixLength();
750+
if (i >= pl) {
751+
int cn=items.length;
752+
if (cn==1) {
753+
// Just return prefix, or empty vector if no prefix
754+
return (pl==0)?Vectors.empty():prefix.getValue();
755+
}
756+
757+
int ci=(int)(i-pl);
758+
Ref<T>[] newItems=Arrays.copyOf(items, cn-1);
759+
System.arraycopy(items, ci+1, newItems, ci, cn-ci-1);
760+
761+
return new VectorLeaf<T>(newItems,prefix,count-1);
762+
}
763+
return slice(0,i).concat(slice(i+1,count));
764+
}
740765
}

convex-core/src/main/java/convex/core/data/VectorTree.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,5 +745,11 @@ protected void visitAllChildren(Consumer<AVector<T>> visitor) {
745745
visitor.accept(child);
746746
}
747747
}
748+
749+
@Override
750+
public AVector<T> dissocAt(long i) {
751+
if ((i<0)||(i>=count)) return null;
752+
return slice(0,i).concat(slice(i+1,count));
753+
}
748754

749755
}

convex-core/src/main/java/convex/core/data/Vectors.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convex.core.data;
22

33
import java.util.Collection;
4+
import java.util.Comparator;
45

56
import org.bouncycastle.util.Arrays;
67

@@ -23,6 +24,11 @@ public class Vectors {
2324

2425
public static final int MAX_ENCODING_LENGTH = Math.max(VectorLeaf.MAX_ENCODING_LENGTH,VectorTree.MAX_ENCODING_LENGTH);
2526

27+
@SuppressWarnings("rawtypes")
28+
public static final Comparator<AVector> lengthComparator = (a,b)->{
29+
return Utils.checkedInt(a.count()-b.count());
30+
};
31+
2632
/**
2733
* Creates a canonical AVector with the given elements
2834
*

convex-core/src/test/java/convex/comms/GenTestFormat.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,19 @@ public void primitiveRoundTrip(@From(PrimitiveGen.class) ACell prim) throws BadF
5353
public void dataRoundTrip(@From(ValueGen.class) ACell value) throws BadFormatException, IOException {
5454
Ref<ACell> pref = Ref.get(Cells.persist(value)); // ensure persisted
5555
Blob b = Cells.encode(value);
56-
ACell o = Format.read(b);
56+
try {
57+
ACell o = Format.read(b);
58+
59+
assertEquals(value, o);
60+
assertEquals(b, Cells.encode(o));
61+
assertEquals(pref.getValue(), o);
62+
63+
FuzzTestFormat.doMutationTest(b);
64+
} catch (BadFormatException e) {
65+
System.err.println("Bad format in GenTestFromat: "+b);
66+
throw e;
67+
}
5768

58-
assertEquals(value, o);
59-
assertEquals(b, Cells.encode(o));
60-
assertEquals(pref.getValue(), o);
6169

62-
FuzzTestFormat.doMutationTest(b);
6370
}
6471
}

0 commit comments

Comments
 (0)