Skip to content

Commit faaa178

Browse files
committed
Extra lattice branch testing
1 parent b9abf9e commit faaa178

File tree

6 files changed

+70
-20
lines changed

6 files changed

+70
-20
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ public int getBranchCount() {
436436
for (int i=0; i<rc; i++) {
437437
Ref<?> r=c.getRef(i);
438438
if (r.isEmbedded()) {
439-
ACell child=r.getValue();
440-
if (child!=null) result+=child.getBranchCount();
439+
// need to recursively count branches in embedded Ref
440+
result+=r.branchCount();
441441
} else {
442442
// we found a branch!
443443
result+=1;
@@ -460,12 +460,12 @@ public <T extends ACell> Ref<T> getBranchRef(int index) {
460460
Ref<?> r=c.getRef(i);
461461
if (r.isEmbedded()) {
462462
ACell child=r.getValue();
463-
if (child==null) continue; // no branch here
463+
if (child==null) continue; // no branch here!
464464
int cbc=child.getBranchCount();
465465
if (cbc>index) return child.getBranchRef(index);
466466
index-=cbc;
467467
} else {
468-
if (index==0) return (Ref<T>) r;
468+
if (index==0) return (Ref<T>) r; // we are pointing to exactly this branch
469469
index-=1;
470470
}
471471
}
@@ -573,7 +573,7 @@ public void attachRef(Ref<?> ref) {
573573
}
574574

575575
/**
576-
* Tests if this Cell is completely encoded, i.e. has no external Refs. This implies that the
576+
* Tests if this Cell is completely encoded, i.e. has no external branch Refs. This implies that the
577577
* complete Cell can be represented in a single encoding.
578578
* @return true if completely encoded, false otherwise
579579
*/

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ public class Cells {
1919

2020
/**
2121
* The maximum number of branches possible from a single Cell encoding
22+
* TODO: Verify this is 68 (vector with 16 embedded children with 4 branches each + embedded tail with 4 branches)
2223
*/
23-
public static final int MAX_BRANCH_COUNT = 72;
24+
public static final int MAX_BRANCH_COUNT = 68;
2425

2526
/**
2627
* Equality method allowing for nulls
@@ -69,19 +70,9 @@ public static int refCount(ACell a) {
6970
* @param a Cell to check (may be null)
7071
* @return Number of Refs in the object.
7172
*/
72-
public static int branchCount(ACell a) {
73-
if (a==null) return 0;
74-
int n=a.getRefCount();
75-
int bc=0;
76-
for (int i=0; i<n; i++) {
77-
Ref<?> ref=a.getRef(i);
78-
if (ref.isEmbedded()) {
79-
bc+=branchCount(ref.getValue());
80-
} else {
81-
bc++;
82-
}
83-
}
84-
return bc;
73+
public static int branchCount(ACell v) {
74+
if (v==null) return 0;
75+
return v.getBranchCount();
8576
}
8677

8778
/**
@@ -93,7 +84,7 @@ public static int branchCount(ACell a) {
9384
* @return Ref for cell
9485
*/
9586
public static <R extends ACell> Ref<R> getRef(ACell cell, int index) {
96-
if (cell ==null) throw new IndexOutOfBoundsException("Bad ref index called on null");
87+
if (cell==null) throw new IndexOutOfBoundsException("getRef called on null");
9788
return cell.getRef(index);
9889
}
9990

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ public Ref<T> withMinimumStatus(int newStatus) {
236236
* @return The value contained in this Ref
237237
*/
238238
public abstract T getValue();
239+
240+
/**
241+
* Get the number of child branches from this Ref
242+
* @return Number of branches
243+
*/
244+
public int branchCount() {
245+
ACell v=getValue();
246+
if (v==null) return 0;
247+
return v.getBranchCount();
248+
}
239249

240250
@Override
241251
public int hashCode() {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ public static long uniqueRefCount(ACell a) {
147147
Set<Ref<?>> rs=accumulateRefSet(a);
148148
return rs.size();
149149
}
150+
151+
152+
150153

151154
/**
152155
* Utility function to locate missing data

convex-core/src/test/java/convex/core/data/ObjectsTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,31 @@ private static void doBranchTests(ACell a) {
8080
assertTrue(bc>=0);
8181
assertTrue(bc<=Cells.MAX_BRANCH_COUNT);
8282

83+
// out of range branches should be null
84+
assertNull(a.getBranchRef(-1));
85+
assertNull(a.getBranchRef(bc));
86+
87+
// bc of zero equivalent to completely encoded
88+
assertEquals(a.isCompletelyEncoded(),bc==0);
89+
90+
int rc=a.getRefCount();
91+
if (rc==0) {
92+
// if no Refs, clearly none of them can be branches!
93+
assertEquals(0,bc);
94+
} else if (rc==1) {
95+
Ref<?> cr=a.getRef(0);
96+
if (cr.isEmbedded()) {
97+
assertEquals(bc,cr.branchCount());
98+
} else {
99+
assertEquals(1,bc);
100+
}
101+
}
102+
83103
if (bc==0) {
84104
assertNull(a.getBranchRef(0));
85105
Cells.visitBranches(a, v->fail("Shouldn't visit any branch!"));
86106
} else {
107+
// branch refs within range should be non-null
87108
assertNotNull(a.getBranchRef(0));
88109
assertNotNull(a.getBranchRef(bc-1));
89110

convex-core/src/test/java/convex/core/data/VectorsTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ public void testChunks() {
8080
AVector<CVMLong> v = Samples.INT_VECTOR_300.getChunk(0);
8181
assertEquals(VectorTree.class, v.getChunk(0).concat(v).getClass());
8282
}
83+
84+
@Test
85+
public void testMaxBranches() {
86+
// non-embedded vector with 16 elements
87+
AVector<ACell> nonEmbed=Vectors.repeat(Samples.INT_VECTOR_10, 16);
88+
assertFalse(nonEmbed.isEmbedded());
89+
90+
// embedded tail with 4 branches, count 64
91+
AVector<ACell> tail=nonEmbed.concat(nonEmbed).concat(nonEmbed).concat(nonEmbed);
92+
assertEquals(4,tail.getBranchCount());
93+
assertEquals(64,tail.count());
94+
assertTrue(tail.isEmbedded());
95+
96+
97+
AVector<ACell> bigEmbed=Vectors.repeat(tail,16);
98+
assertEquals(16,bigEmbed.getRefCount());
99+
assertEquals(64,bigEmbed.getBranchCount()); // maximum for vector?
100+
101+
AVector<ACell> v=tail.concat(bigEmbed);
102+
assertFalse(v.isCompletelyEncoded());
103+
104+
doVectorTests(nonEmbed);
105+
doVectorTests(bigEmbed);
106+
doVectorTests(v);
107+
}
83108

84109
@Test
85110
public void testChunkConcat() {

0 commit comments

Comments
 (0)