From ecf4a94c03d0872f55b797ce167a853153a00ded Mon Sep 17 00:00:00 2001 From: mikera Date: Sat, 12 Oct 2024 15:28:06 +0100 Subject: [PATCH] Testing for CAD3 Extension Values --- .../src/main/java/convex/core/data/Format.java | 9 +++++---- .../src/main/java/convex/core/lang/Core.java | 3 +-- .../convex/core/lang/reader/AntlrReader.java | 2 +- .../test/java/convex/core/data/CAD3Test.java | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/convex-core/src/main/java/convex/core/data/Format.java b/convex-core/src/main/java/convex/core/data/Format.java index 59a724aa7..b3c75077a 100644 --- a/convex-core/src/main/java/convex/core/data/Format.java +++ b/convex-core/src/main/java/convex/core/data/Format.java @@ -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); } /** diff --git a/convex-core/src/main/java/convex/core/lang/Core.java b/convex-core/src/main/java/convex/core/lang/Core.java index 43e0a0fbe..6858b6062 100644 --- a/convex-core/src/main/java/convex/core/lang/Core.java +++ b/convex-core/src/main/java/convex/core/lang/Core.java @@ -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]; diff --git a/convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java b/convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java index e1e8b535a..7574ff101 100644 --- a/convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java +++ b/convex-core/src/main/java/convex/core/lang/reader/AntlrReader.java @@ -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); diff --git a/convex-core/src/test/java/convex/core/data/CAD3Test.java b/convex-core/src/test/java/convex/core/data/CAD3Test.java index 4ec56bb0a..6104a5837 100644 --- a/convex-core/src/test/java/convex/core/data/CAD3Test.java +++ b/convex-core/src/test/java/convex/core/data/CAD3Test.java @@ -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() { @@ -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]")); + } }