Skip to content

Commit fd53626

Browse files
committed
Extra blob tests
1 parent 67450f7 commit fd53626

File tree

1 file changed

+73
-5
lines changed

1 file changed

+73
-5
lines changed

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

Lines changed: 73 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNotEquals;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
67
import static org.junit.jupiter.api.Assertions.assertNull;
78
import static org.junit.jupiter.api.Assertions.assertSame;
89
import static org.junit.jupiter.api.Assertions.assertThrows;
910
import static org.junit.jupiter.api.Assertions.assertTrue;
1011

1112
import java.io.IOException;
1213
import java.nio.ByteBuffer;
14+
import java.util.Arrays;
1315
import java.util.Random;
1416

1517
import org.junit.jupiter.api.Test;
@@ -106,12 +108,19 @@ public void testHexMatchLength() {
106108
assertEquals(0,Blob.fromHex("ffff0123").hexMatch(Blob.fromHex("ffff012f"), 3, 0));
107109
}
108110

109-
@Test public void testHexDigit() {
110-
111+
@Test
112+
public void testHexDigitComplete() {
113+
Blob b = Blob.fromHex("a1b2");
114+
assertEquals(10, b.getHexDigit(0)); // 'a'
115+
assertEquals(1, b.getHexDigit(1)); // '1'
116+
assertEquals(11, b.getHexDigit(2)); // 'b'
117+
assertEquals(2, b.getHexDigit(3)); // '2'
118+
119+
// Test boundary conditions
120+
assertThrows(IndexOutOfBoundsException.class, () -> b.getHexDigit(-1));
121+
assertThrows(IndexOutOfBoundsException.class, () -> b.getHexDigit(4));
122+
assertThrows(IndexOutOfBoundsException.class, () -> Blob.EMPTY.getHexDigit(0));
111123
}
112-
113-
114-
115124

116125
@Test
117126
public void testFromHex() {
@@ -391,6 +400,65 @@ public void testCreate() {
391400
assertSame(Blobs.empty(),Blob.create(bs, 10, 0));
392401
}
393402

403+
@Test
404+
public void testHexParsingEdgeCases() {
405+
// Case sensitivity
406+
assertEquals(Blob.fromHex("abcd"), Blob.fromHex("ABCD"));
407+
assertEquals(Blob.fromHex("abcd"), Blob.fromHex("AbCd"));
408+
409+
// Invalid characters
410+
assertNull(Blob.fromHex("12GH"));
411+
assertNull(Blob.fromHex("12 34"));
412+
assertNull(Blob.fromHex("12\n34"));
413+
414+
// Empty and whitespace
415+
assertEquals(Blob.EMPTY, Blob.fromHex(""));
416+
assertNull(Blob.fromHex(" ")); // Should this be EMPTY or null?
417+
}
418+
419+
420+
@Test
421+
public void testBlobTreeDepthLimits() {
422+
// Test extremely deep blob trees don't cause stack overflow
423+
BlobBuilder bb = new BlobBuilder();
424+
Blob small = Blob.fromHex("01");
425+
426+
// Build a very nested structure
427+
for (int i = 0; i < 1000; i++) {
428+
bb.append(small);
429+
}
430+
431+
ABlob result = bb.toBlob();
432+
assertNotNull(result);
433+
assertEquals(1000, result.count());
434+
}
435+
436+
@Test
437+
public void testCompareConsistency() {
438+
// Ensure compareTo is consistent with equals
439+
java.util.List<Blob> blobs = Arrays.asList(
440+
Blob.EMPTY,
441+
Blob.fromHex("00"),
442+
Blob.fromHex("01"),
443+
Blob.fromHex("FF"),
444+
Blob.fromHex("0000"),
445+
Blob.fromHex("0001"),
446+
Blob.fromHex("FFFF")
447+
);
448+
449+
for (Blob b1 : blobs) {
450+
for (Blob b2 : blobs) {
451+
// If equal, compareTo should return 0
452+
if (b1.equals(b2)) {
453+
assertEquals(0, b1.compareTo(b2));
454+
}
455+
456+
// compareTo should be antisymmetric
457+
assertEquals(-Integer.signum(b1.compareTo(b2)), Integer.signum(b2.compareTo(b1)));
458+
}
459+
}
460+
}
461+
394462
@Test
395463
public void testPacked() {
396464
Blob chunk=Samples.FULL_BLOB;

0 commit comments

Comments
 (0)