From 51a029304d8105b201bdfb81a7692b8a1b16b028 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 16 Nov 2026 10:18:30 -0800 Subject: Packing Serialize tighter --- .../test/java/com/seaofnodes/simple/BAOSTest.java | 58 ++++++++++++++++++++++ .../java/com/seaofnodes/simple/MemMergeTest.java | 5 +- .../seaofnodes/simple/SeparateCompilationTest.java | 36 ++++++++++++++ .../simple/type/PackedTypeTestSupport.java | 20 ++++++++ 4 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 chapter32/src/test/java/com/seaofnodes/simple/BAOSTest.java create mode 100644 chapter32/src/test/java/com/seaofnodes/simple/type/PackedTypeTestSupport.java (limited to 'chapter32/src/test/java') 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 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()); + } +} -- cgit v1.2.3