summaryrefslogtreecommitdiff
path: root/chapter32/src/test/java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-16 10:18:30 -0800
committerCliff Click <cliffc@acm.org>2026-11-16 10:18:30 -0800
commit51a029304d8105b201bdfb81a7692b8a1b16b028 (patch)
tree32f5312796aebf0b857eb9a7042249c9e08a334a /chapter32/src/test/java
parentc21026c5377429b11e285d251439407074fe3cd1 (diff)
downloadSimple-51a029304d8105b201bdfb81a7692b8a1b16b028.tar.gz
Simple-51a029304d8105b201bdfb81a7692b8a1b16b028.zip
Packing Serialize tighter
Diffstat (limited to 'chapter32/src/test/java')
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/BAOSTest.java58
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/MemMergeTest.java5
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java36
-rw-r--r--chapter32/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java20
4 files changed, 116 insertions, 3 deletions
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/BAOSTest.java b/chapter32/src/test/java/com/seaofnodes/simple/BAOSTest.java
new file mode 100644
index 00000000..03e53b6d
--- /dev/null
+++ b/chapter32/src/test/java/com/seaofnodes/simple/BAOSTest.java
@@ -0,0 +1,58 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.type.PackedTypeTestSupport;
+import com.seaofnodes.simple.util.BAOS;
+import java.util.Random;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class BAOSTest {
+ @Test public void packedIntBoundaries() {
+ int[] values={Integer.MIN_VALUE,-134217729,-134217728,-1048577,-1048576,
+ -8193,-8192,-65,-64,-1,0,1,63,64,8191,8192,1048575,1048576,
+ 134217727,134217728,Integer.MAX_VALUE};
+ int[] widths={5,5,4,4,3,3,2,2,1,1,1,1,1,2,2,3,3,4,4,5,5};
+ BAOS out=new BAOS();
+ for( int i=0; i<values.length; i++ ) {
+ int before=out.size();
+ out.packed4(values[i]);
+ assertEquals("width of "+values[i],widths[i],out.size()-before);
+ }
+ BAOS in=new BAOS(out.toByteArray());
+ for( int value : values ) assertEquals(value,in.packed4());
+ assertEquals(out.size(),in.size());
+ out.clear();
+ out.packed4(-65); out.packed4(64);
+ assertArrayEquals(new byte[]{(byte)0x81,1,(byte)0x80,1},out.toByteArray());
+ }
+
+ @Test public void packedIntsAndStrings() {
+ Random random=new Random(123);
+ int[] values=new int[10000];
+ BAOS out=new BAOS();
+ for( int i=0; i<values.length; i++ ) out.packed4(values[i]=random.nextInt());
+ String[] strings={"","ASCII stays compact", "\u0000", "caf\u00e9 \u65e5\u672c\u8a9e \uD83D\uDE00", "x".repeat(8192)};
+ for( String s : strings ) out.packedS(s);
+ out.packed8(Long.MIN_VALUE);
+ BAOS in=new BAOS(out.toByteArray());
+ for( int value : values ) assertEquals(value,in.packed4());
+ for( String s : strings ) assertEquals(s,in.packedS());
+ assertEquals(Long.MIN_VALUE,in.packed8());
+ assertEquals(out.size(),in.size());
+ }
+
+ @Test public void rejectPackedIntOverflow() {
+ for( byte last : new byte[]{0x10,(byte)0x80} ) {
+ BAOS in=new BAOS(new byte[]{(byte)0x80,(byte)0x80,(byte)0x80,(byte)0x80,last});
+ try { in.packed4(); fail("overflow accepted"); }
+ catch( IllegalArgumentException expected ) { assertTrue(expected.getMessage().contains("overflow")); }
+ }
+ }
+
+ @Test public void wideSingletonFunctionIds() {
+ new CodeGen("");
+ for( int id : new int[]{2,63,64,254,255,256,1024,65535} )
+ PackedTypeTestSupport.singleton(id);
+ }
+}
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/MemMergeTest.java b/chapter32/src/test/java/com/seaofnodes/simple/MemMergeTest.java
index cb17982e..253bce0b 100644
--- a/chapter32/src/test/java/com/seaofnodes/simple/MemMergeTest.java
+++ b/chapter32/src/test/java/com/seaofnodes/simple/MemMergeTest.java
@@ -20,9 +20,8 @@ public class MemMergeTest {
assertEquals(10000,mem.aliasAt(4));
assertSame(value,mem.alias(10000));
assertSame(defaults,mem.alias(512));
- assertEquals(10001,mem.packedSize());
- assertNull(mem.packedInput(9999));
- assertSame(value,mem.packedInput(10000));
+ assertNull(mem.explicitAlias(9999));
+ assertSame(value,mem.explicitAlias(10000));
mem.alias(513,null);
assertEquals(4,mem.nIns());
assertEquals(10000,mem.aliasAt(3));
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java b/chapter32/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
index 1c278791..404201e3 100644
--- a/chapter32/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
+++ b/chapter32/src/test/java/com/seaofnodes/simple/SeparateCompilationTest.java
@@ -10,6 +10,42 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class SeparateCompilationTest {
+ @Test public void testSparseWideMemoryImport() throws Exception {
+ Path build=Path.of("build/objs/separate");
+ Files.createDirectories(build);
+ Path lib=Files.createTempDirectory(build,"wide-memory-");
+ StringBuilder source=new StringBuilder("int f0=7;\n");
+ for( int i=1; i<300; i++ ) source.append("int f").append(i).append("=0;\n");
+ source.append("val touch={int n -> f299+=n; return f0+f299;};");
+ new CodeGen(null,lib.toString(),null,"Wide",source.toString(),123,true,TypeInteger.BOT)
+ .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
+ var paths=new Ary<>(new String[]{lib.toString()});
+ CodeGen code=new CodeGen(null,null,paths,"WideEval","return Wide.touch((arg<<32)>>32);",123,true,TypeInteger.BOT)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("12",Eval2.eval(code,5));
+ TestC.run("return Wide.touch(5)==12 ? 0 : 1;","wide_memory_client",paths,
+ TestC.CALL_CONVENTION,null,null,"",-1);
+ }
+
+ @Test public void testOldObjectFormatRequestsRebuild() throws Exception {
+ Path build=Path.of("build/objs/separate");
+ Files.createDirectories(build);
+ Path lib=Files.createTempDirectory(build,"old-format-");
+ new CodeGen(null,lib.toString(),null,"Old","val value=7;",123,true,TypeInteger.BOT)
+ .driver(TestC.CPU_PORT,TestC.CALL_CONVENTION,false,false);
+ Path file=lib.resolve("Old.o");
+ ElfReader elf=ElfReader.load(file.toFile(),null);
+ byte[] bytes=Files.readAllBytes(file);
+ bytes[elf._bais.size()+3]='E'; // Previous C0DE header, before format 2.
+ Files.write(file,bytes);
+ try {
+ ElfReader.load(file.toFile(),null).loadPublicTypes(new CodeGen(""));
+ fail("old format accepted");
+ } catch( IllegalArgumentException expected ) {
+ assertTrue(expected.getMessage().contains("rebuild the object file"));
+ }
+ }
+
@Test public void testImportedReadOnlyPrimitiveArray() throws Exception {
Path build=Path.of("build/objs/separate");
Files.createDirectories(build);
diff --git a/chapter32/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java b/chapter32/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java
new file mode 100644
index 00000000..c218e98b
--- /dev/null
+++ b/chapter32/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java
@@ -0,0 +1,20 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.util.BAOS;
+import java.util.HashMap;
+import static org.junit.Assert.*;
+
+public class PackedTypeTestSupport {
+ public static void singleton(int id) {
+ Type.TAGOFFS();
+ TypeFunPtr fun=TypeFunPtr.make((byte)2,true,new Type[0],TypeInteger.BOT,XInt.make(id));
+ BAOS out=new BAOS();
+ fun.packed(out,new HashMap<>());
+ assertEquals(id<=254 ? 2 : 4,out.size());
+ BAOS in=new BAOS(out.toByteArray());
+ TypeFunPtr copy=(TypeFunPtr)Type.packed(in,new String[]{""});
+ assertEquals(id,copy.fidx());
+ assertEquals(0,copy.nargs());
+ assertEquals(out.size(),in.size());
+ }
+}