Skip to content

Commit ecf4a94

Browse files
committed
Testing for CAD3 Extension Values
1 parent 89490c6 commit ecf4a94

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,12 +510,13 @@ private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatExcepti
510510
throw new BadFormatException("Can't read Op with tag byte: " + Utils.toHexString(tag));
511511
}
512512

513-
514513
private static ACell readExtension(byte tag, Blob blob, int offset) throws BadFormatException {
515-
if (tag == Tag.CORE_DEF) return Core.read(blob, offset);
514+
// We expect a VLQ Count following the tag
515+
long code=readVLQCount(blob,offset+1);
516516

517-
return ExtensionValue.create(tag, readVLQCount(blob,offset+1));
518-
517+
if (tag == Tag.CORE_DEF) return Core.fromCode(code);
518+
519+
return ExtensionValue.create(tag, code);
519520
}
520521

521522
/**

convex-core/src/main/java/convex/core/lang/Core.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,8 +3011,7 @@ private static Context applyDocumentation(Context ctx) throws IOException {
30113011
* @return Singleton cell representing the Core value
30123012
* @throws BadFormatException In case of encoding error
30133013
*/
3014-
public static ACell read(Blob b, int pos) throws BadFormatException {
3015-
long code=Format.readVLQCount(b, pos+1);
3014+
public static ACell fromCode(long code) throws BadFormatException {
30163015
if (code <0 || code>=CODE_MAP.length) throw new BadFormatException("Core code out of range: "+code);
30173016

30183017
ACell o = CODE_MAP[(int)code];

convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ public void exitCad3(Cad3Context ctx) {
406406
String s=ctx.getStop().getText();
407407
Blob enc=Blob.fromHex(s.substring(2, s.length()-1));
408408
try {
409-
ACell cell=convex.core.data.Format.read(enc);
409+
ACell cell=convex.core.data.Format.decodeMultiCell(enc);
410410
push (cell);
411411
} catch (BadFormatException e) {
412412
throw new ParseException("Invalid CAD3 encoding: "+e.getMessage(),e);

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

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

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertSame;
46

57
import org.junit.jupiter.api.Test;
68

9+
import convex.core.data.prim.CVMLong;
10+
import convex.core.lang.Core;
11+
import convex.core.lang.Reader;
12+
import convex.core.util.Utils;
13+
714
public class CAD3Test {
815

916
@Test public void testExtensionValues() {
@@ -14,5 +21,16 @@ public class CAD3Test {
1421

1522
ObjectsTest.doAnyValueTests(ev);
1623
}
24+
25+
@Test public void testExtensionCoreDefs() {
26+
assertSame(Core.VECTOR,Reader.read("#["+Utils.toHexString(Tag.CORE_DEF)+"01]"));
27+
}
28+
29+
@Test public void testReadEncodings() {
30+
assertSame(Address.ZERO,Reader.read("#[2100]"));
31+
assertSame(CVMLong.ZERO,Reader.read("#[10]"));
32+
assertNull(Reader.read("#[00]"));
33+
assertEquals(ExtensionValue.create((byte) 0xe5, 0),Reader.read("#[e500]"));
34+
}
1735

1836
}

0 commit comments

Comments
 (0)