Skip to content

Commit

Permalink
Testing for CAD3 Extension Values
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 12, 2024
1 parent 89490c6 commit ecf4a94
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
9 changes: 5 additions & 4 deletions convex-core/src/main/java/convex/core/data/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,13 @@ private static ACell readCode(byte tag, Blob b, int pos) throws BadFormatExcepti
throw new BadFormatException("Can't read Op with tag byte: " + Utils.toHexString(tag));
}


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

return ExtensionValue.create(tag, readVLQCount(blob,offset+1));

if (tag == Tag.CORE_DEF) return Core.fromCode(code);

return ExtensionValue.create(tag, code);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions convex-core/src/main/java/convex/core/lang/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -3011,8 +3011,7 @@ private static Context applyDocumentation(Context ctx) throws IOException {
* @return Singleton cell representing the Core value
* @throws BadFormatException In case of encoding error
*/
public static ACell read(Blob b, int pos) throws BadFormatException {
long code=Format.readVLQCount(b, pos+1);
public static ACell fromCode(long code) throws BadFormatException {
if (code <0 || code>=CODE_MAP.length) throw new BadFormatException("Core code out of range: "+code);

ACell o = CODE_MAP[(int)code];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public void exitCad3(Cad3Context ctx) {
String s=ctx.getStop().getText();
Blob enc=Blob.fromHex(s.substring(2, s.length()-1));
try {
ACell cell=convex.core.data.Format.read(enc);
ACell cell=convex.core.data.Format.decodeMultiCell(enc);
push (cell);
} catch (BadFormatException e) {
throw new ParseException("Invalid CAD3 encoding: "+e.getMessage(),e);
Expand Down
18 changes: 18 additions & 0 deletions convex-core/src/test/java/convex/core/data/CAD3Test.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package convex.core.data;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

import org.junit.jupiter.api.Test;

import convex.core.data.prim.CVMLong;
import convex.core.lang.Core;
import convex.core.lang.Reader;
import convex.core.util.Utils;

public class CAD3Test {

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

ObjectsTest.doAnyValueTests(ev);
}

@Test public void testExtensionCoreDefs() {
assertSame(Core.VECTOR,Reader.read("#["+Utils.toHexString(Tag.CORE_DEF)+"01]"));
}

@Test public void testReadEncodings() {
assertSame(Address.ZERO,Reader.read("#[2100]"));
assertSame(CVMLong.ZERO,Reader.read("#[10]"));
assertNull(Reader.read("#[00]"));
assertEquals(ExtensionValue.create((byte) 0xe5, 0),Reader.read("#[e500]"));
}

}

0 comments on commit ecf4a94

Please sign in to comment.