From 650f541f976c03e22a4bc93bb9b09d81a7f810ba Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Mon, 16 Nov 2026 15:45:47 -0800 Subject: Another round of test speedup More TestType lattice test speedups. Collapse together some expensive tests. Trim some tests from `sanity` target --- .../com/seaofnodes/simple/AssemblyDumpTest.java | 28 +++-- .../test/java/com/seaofnodes/simple/ModTest.java | 27 +++-- .../com/seaofnodes/simple/StringFormatTest.java | 113 +++++++++++++++------ .../java/com/seaofnodes/simple/TestWorkspace.java | 2 +- .../test/java/com/seaofnodes/simple/TypeTest.java | 81 ++++++++------- 5 files changed, 164 insertions(+), 87 deletions(-) (limited to 'chapter32/src/test') diff --git a/chapter32/src/test/java/com/seaofnodes/simple/AssemblyDumpTest.java b/chapter32/src/test/java/com/seaofnodes/simple/AssemblyDumpTest.java index 0e4c230a..5399aecf 100644 --- a/chapter32/src/test/java/com/seaofnodes/simple/AssemblyDumpTest.java +++ b/chapter32/src/test/java/com/seaofnodes/simple/AssemblyDumpTest.java @@ -13,25 +13,39 @@ public class AssemblyDumpTest { @Test public void libraryUnitsAndGlyphs() throws Exception { Path root=Path.of("build/objs/assembly-dump"); Files.createDirectories(root); - Path build=Files.createTempDirectory(root,"sys-"); - CodeGen code=new CodeGen("src/main/smp",build.toString(),null,"sys",null,123,true,TypeInteger.BOT) + Path fixture=Files.createTempDirectory(root,"font-"); + Path source=Files.createDirectories(fixture.resolve("src")); + Path build=Files.createDirectories(fixture.resolve("obj")); + Files.createDirectories(source.resolve("Atlas")); + // Use the real font, but only two tiny modules: drawing integration is + // covered by DrawTest. This test needs multiple units and constant data. + String canvasSource=Files.readString(Path.of("src/main/smp/sys/draw/Canvas.smp")); + int fontStart=canvasSource.indexOf("val _glyphs ="); + assertTrue("Font declaration",fontStart>=0); + int fontEnd=canvasSource.indexOf("};",fontStart); + assertTrue("Font initializer",fontEnd>=0); + Files.writeString(source.resolve("Atlas/Font.smp"),canvasSource.substring(fontStart,fontEnd+2)+ + "\nval glyph={int i -> _glyphs[i];};\n"); + Files.writeString(source.resolve("Atlas.smp"),"val glyph={int i -> Font.glyph(i);};\n"); + CodeGen code=new CodeGen(source.toString(),build.toString(),null,"Atlas",null,123,true,TypeInteger.BOT) .driver(CodeGen.Phase.Serialize,TestC.CPU_PORT,TestC.CALL_CONVENTION) .driver(CodeGen.Phase.Encoding,TestC.CPU_PORT,TestC.CALL_CONVENTION); var entry=code._encoding; String before=code.asm(); assertSame("Dumping another unit must not change the active encoding",entry,code._encoding); + assertEquals(2,code._compunits.size()); for( var unit : code._compunits.values() ) if( unit._src!=null ) { String heading="=== Compilation unit: "+unit._cname+" ==="; assertEquals("Exactly one heading for "+unit._cname,2,before.split(heading,-1).length); } - int start=before.indexOf("=== Compilation unit: sys.draw.Canvas ==="); + int start=before.indexOf("=== Compilation unit: Atlas.Font ==="); assertTrue(start>=0); int end=before.indexOf("=== Compilation unit:",start+1); String canvas=before.substring(start,end<0 ? before.length() : end); assertTrue(canvas.contains("Constant Pool")); - assertTrue(canvas.contains("sys.draw.Canvas._glyphs: []u16")); - TypeMemPtr ptr=(TypeMemPtr)code._compunits.get("sys/draw/Canvas")._clz.field("_glyphs")._t; + assertTrue(canvas.contains("Atlas.Font._glyphs: []u16")); + TypeMemPtr ptr=(TypeMemPtr)code._compunits.get("Atlas/Font")._clz.field("_glyphs")._t; TypeConAry glyphs=(TypeConAry)ptr._obj.field("[]")._t; assertEquals(96,glyphs.len()); BAOS bytes=new BAOS(); @@ -40,10 +54,10 @@ public class AssemblyDumpTest { canvas.contains("60000000"+HexFormat.of().formatHex(bytes.toByteArray()))); code.exportELF(false,false); com.seaofnodes.simple.codegen.PrintRegTestSupport.checkDataAliases( - build.resolve("sys/draw/Canvas.o"),196,"sys.draw.Canvas._glyphs"); + build.resolve("Atlas/Font.o"),196,"Atlas.Font._glyphs"); assertSame(entry,code._encoding); assertEquals("Export must not truncate the dump to its entry unit",before,code.asm()); - Files.writeString(build.resolve("sys.asm"),before); + Files.writeString(build.resolve("Atlas.asm"),before); } @Test public void combinedEncodingAndEarlierPhase() { diff --git a/chapter32/src/test/java/com/seaofnodes/simple/ModTest.java b/chapter32/src/test/java/com/seaofnodes/simple/ModTest.java index 7f79599d..c06e99f1 100644 --- a/chapter32/src/test/java/com/seaofnodes/simple/ModTest.java +++ b/chapter32/src/test/java/com/seaofnodes/simple/ModTest.java @@ -166,20 +166,31 @@ public class ModTest { return integerEdges(new long[]{Long.MIN_VALUE,Long.MIN_VALUE+1,-17,-5,-1,0,1,5,17,Long.MAX_VALUE}); } private static String integerEdges(long[] values) { + // Keep the full value matrix in data, rather than compiling a long + // chain of constant calls and conditional returns for every pair. StringBuilder source=new StringBuilder(""" val rem_noInline={ i64 x,i64 y -> x%y; }; val div_noInline={ i64 x,i64 y -> x/y; }; val main={ -> + val values=new i64[~]{ """); - int failure=1; - for( long x : values ) - for( long y : values ) { - source.append("if(rem_noInline(").append(literal(x)).append(',').append(literal(y)) - .append(")!=").append(literal(y==0 ? 0 : x%y)).append(") return ").append(failure++).append(';'); - source.append("if(div_noInline(").append(literal(x)).append(',').append(literal(y)) - .append(")!=").append(literal(y==0 ? 0 : x/y)).append(") return ").append(failure++).append(';'); + for(int i=0;i "+body+" return 0; };", "stringFormat",expected,-1); + // Share a few native programs, retaining a separately reported assertion + // for each case. Blocks isolate locals; the delimiter preserves embedded + // NULs and newlines in the expected output. + private enum Format { + DefaultsAndNestedStrings("sys.io.p(\"CPU plays %(2+3). %(\"nested %(7)\") %(true) %(1.5)\\n\");", + "CPU plays 5. nested 7 1 1.5\n"), + Formats("sys.io.p(\"%+06d(17)|%#08x(42)|%-5.3s(\"abcdef\")|%.2f(1.25)|%c(65)|%b(false)\\n\");", + "+00017|0x00002a|abc |1.25|A|false\n"), + RemainderInInterpolation("int mod=17; sys.io.p(\"%(mod%5)|%02d(mod%5)|%%|\\%|%(mod %= 5)|%(mod)\");", + "2|02|%|%|2|2"), + IntegerLimits("i64 lo=-9223372036854775807-1; sys.io.p(\"%(lo)|%(9223372036854775807)|%u(-1)|%X(-1)\");", + "-9223372036854775808|9223372036854775807|18446744073709551615|FFFFFFFFFFFFFFFF"), + IntegerPaddingRules("sys.io.p(\"%#.0o(0)|%#.3o(1)|%#x(0)|%-08d(17)|%08.4d(-17)|%+d(0)|% d(0)\");", + "0|001|0|17 | -0017|+0| 0"), + BuilderNumberUsesFormatter("val b=new sys.aryu8(); b.num(-9223372036854775807-1).add('|').num(9223372036854775807); sys.io.p(sys.text.end(b));", + "-9223372036854775808|9223372036854775807"), + EvaluationOrder("int x=0; val s=\"%(x++)%(x++)%(x++)\"; sys.io.p(\"%(s):%(s#):%(x)|%%|\\%\");", + "012:3:3|%|%"), + EvaluationMutatesBytes("val a=new u8[1]; a[0]='A'; sys.io.p(\"%(a)%(a[0]='B')%(a)\");", "A66B"), + Converters("sys.io.p(sys.text.int(17)); sys.io.p(sys.text.char(65)); sys.io.p(sys.text.bool(true)); sys.io.p(sys.text.flt(1.5));", + "17Atrue1.5"), + FieldNamedInt("struct S { int int=7; }; sys.io.p(\"%((new S).int)\");", "7"), + FloatAndAlternateFormats("f32 x=1.25f; sys.io.p(\"%(x)|%f(x)|%.2e(x)|%.2G(x)|%#o(9)|% d(7)|%.0d(0)|%a(1.5)\");", + "1.25|1.250000|1.25e+00|1.2|011| 7||0x1.8p+0"), + BufferGrowth("sys.io.p(\"%0100d(17)|%.100f(1.0)\");", "0".repeat(98)+"17|1."+"0".repeat(100)), + BytesAndEmptyStrings("val empty=\"%(\"\")\"; val bytes=\"a\\0b\"; sys.io.p(\"%(empty#):%s(bytes):%.2s(bytes):%c(0)\");", + "0:a\0b:a\0:\0"), + Utf8String("val s=\"π\"; sys.io.p(\"%(s):%(s#):%(s[0]):π\");", "π:2:207:π"), + DeferredParsing(""" + struct S { u8[~] s; new S={ -> s="%("}) %(3)")"; }; }; + val s=new S; + sys.io.p(s.s); + for(int i=0; i<2; sys.io.p("%(i++) %("(")")) { } + """, "}) 30 (1 ("); + + final String body, expected; + Format(String body, String expected) { this.body=body; this.expected=expected; } + } + private static final String END="\n--end-format-case--\n"; + private static final int BATCH_SIZE=5; + private static final String[] OUTPUTS=new String[Format.values().length]; + + @org.junit.BeforeClass public static void resetNativeBatches() { + java.util.Arrays.fill(OUTPUTS,null); + } + + private static void prints(Format test) throws Exception { + int index=test.ordinal(); + if( OUTPUTS[index]==null ) { + Format[] cases=Format.values(); + int first=index/BATCH_SIZE*BATCH_SIZE, limit=Math.min(first+BATCH_SIZE,cases.length); + StringBuilder source=new StringBuilder("val main={ -> "); + for( int i=first; i(new String[]{RELEASE_SYS_DIR}),CALL_CONVENTION,null,null,-1); + String[] outputs=exec(TEST_TIMEOUT_SECONDS,exe).split(java.util.regex.Pattern.quote(END),-1); + assertEquals("Each format case must complete",limit-first+1,outputs.length); + assertEquals("No output after final case", "",outputs[outputs.length-1]); + for( int i=first; i \"%(x)|%f(x)\"; }; val main={ -> sys.io.p(format_noInline(1.25f)); return 0; };", "stringFormatFloat","1.25|1.250000",-1); } @Test public void testBufferGrowth() throws Exception { - prints("sys.io.p(\"%0100d(17)|%.100f(1.0)\");", "0".repeat(98)+"17|1."+"0".repeat(100)); + prints(Format.BufferGrowth); } @Test public void testLiteralRemainsConstant() { @@ -107,18 +162,12 @@ public class StringFormatTest extends TestC { } @Test public void testBytesAndEmptyStrings() throws Exception { - prints("val empty=\"%(\"\")\"; val bytes=\"a\\0b\"; sys.io.p(\"%(empty#):%s(bytes):%.2s(bytes):%c(0)\");", - "0:a\0b:a\0:\0"); - prints("val s=\"π\"; sys.io.p(\"%(s):%(s#):%(s[0]):π\");", "π:2:207:π"); + prints(Format.BytesAndEmptyStrings); + prints(Format.Utf8String); } @Test public void testDeferredParsing() throws Exception { - prints(""" - struct S { u8[~] s; new S={ -> s="%("}) %(3)")"; }; }; - val s=new S; - sys.io.p(s.s); - for(int i=0; i<2; sys.io.p("%(i++) %("(")")) { } - """, "}) 30 (1 ("); + prints(Format.DeferredParsing); } @Test public void testImportedFormatter() throws Exception { diff --git a/chapter32/src/test/java/com/seaofnodes/simple/TestWorkspace.java b/chapter32/src/test/java/com/seaofnodes/simple/TestWorkspace.java index 30cff89f..d875c47a 100644 --- a/chapter32/src/test/java/com/seaofnodes/simple/TestWorkspace.java +++ b/chapter32/src/test/java/com/seaofnodes/simple/TestWorkspace.java @@ -7,7 +7,7 @@ import java.nio.file.attribute.BasicFileAttributes; /** Prepare a Make test group's working directory without sharing native outputs. */ public class TestWorkspace { public static void main(String[] args) throws IOException { - if( args.length!=2 || !args[0].matches("tests_group[0-7]") || !args[1].matches("[A-Za-z0-9_]+") ) + if( args.length!=2 || !args[0].matches("tests_group[0-7]|sanity_group[0-2]") || !args[1].matches("[A-Za-z0-9_]+") ) throw new IllegalArgumentException("Expected test group and CPU_ABI"); Path root = Path.of("").toAbsolutePath().normalize(); Path work = root.resolve("build/tests").resolve(args[0]).normalize(); diff --git a/chapter32/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter32/src/test/java/com/seaofnodes/simple/TypeTest.java index 8f212bda..adcc1b3a 100644 --- a/chapter32/src/test/java/com/seaofnodes/simple/TypeTest.java +++ b/chapter32/src/test/java/com/seaofnodes/simple/TypeTest.java @@ -276,74 +276,77 @@ public class TypeTest { public void testLatticeTheory() { Type[] ts = Type.gather(); // Make partitions the outer loop between independent JVMs. A direct - // JUnit/IDE invocation retains the full Cartesian-product coverage. + // JUnit/IDE invocation retains the full coverage. int parts = Integer.parseInt(System.getProperty("simple.lattice.parts","1")); int part = Integer.parseInt(System.getProperty("simple.lattice.part" ,"0")); Assert.assertTrue("Invalid lattice partition",parts > 0 && parts <= ts.length && part >= 0 && part < parts); - // Confirm commutative & complete - for( int i=part; i