diff options
Diffstat (limited to 'chapter15/src/test/java/com')
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java | 38 | ||||
| -rw-r--r-- | chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java | 4 |
2 files changed, 36 insertions, 6 deletions
diff --git a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java index 6ba6eed4..62e865aa 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/Chapter15Test.java @@ -61,6 +61,36 @@ return a[0]; } @Test + public void testBasic4() { + Parser parser = new Parser( +""" +struct A { int i; } +A[] a = new A[2]; +return a; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return A[];", stop.toString()); + assertEquals("Obj<A[]> {\n #=2\n []=null\n}", Evaluator.evaluate(stop, 0).toString()); + } + + @Test + public void testTree() { + Parser parser = new Parser( +""" +// Can we define a forward-reference array? +struct Tree { Tree[] _kids; } +Tree root = new Tree; +root._kids = new Tree[2]; +root._kids[0] = new Tree; +return root; +"""); + StopNode stop = parser.parse(false).iterate(false); + assertEquals("return Tree;", stop.toString()); + assertEquals("Obj<Tree> {\n _kids=Obj<Tree[]> {\n #=2\n []=Obj<Tree> {\n _kids=null\n}\n}\n}", Evaluator.evaluate(stop, 0).toString()); + } + + + @Test public void testRollingSum() { Parser parser = new Parser( """ @@ -123,9 +153,9 @@ while( j < nprimes ) { return rez; """); StopNode stop = parser.parse(false).iterate(true); - assertEquals("return []int;", stop.toString()); + assertEquals("return int[];", stop.toString()); Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20); - assertEquals("[]int {\n # :int;\n [] :int;\n}",obj.struct().toString()); + assertEquals("int[] {\n # :int;\n [] :int;\n}",obj.struct().toString()); long nprimes = (Long)obj.fields()[0]; long[] primes = new long[]{2,3,5,7,11,13,17,19}; for( int i=0; i<nprimes; i++ ) @@ -140,7 +170,7 @@ return rez; return new flt; """); try { parser.parse(false).iterate(false); fail(); } - catch( Exception e ) { assertEquals("Cannot allocate a FltBot",e.getMessage()); } + catch( Exception e ) { assertEquals("Cannot allocate a flt",e.getMessage()); } } @Test @@ -150,7 +180,7 @@ return new flt; int is = new int[2]; """); try { parser.parse(false).iterate(false); fail(); } - catch( Exception e ) { assertEquals("Type *[]int is not of declared type int",e.getMessage()); } + catch( Exception e ) { assertEquals("Type *int[] is not of declared type int",e.getMessage()); } } @Test diff --git a/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java b/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java index 324dd42d..7cd854bf 100644 --- a/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java +++ b/chapter15/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java @@ -109,8 +109,8 @@ public class ScriptGenerator { static { new Parser("", TypeInteger.BOT); - for(var e : Parser.TYPES.entrySet() ) { - if (e.getValue() instanceof TypeInteger) { + for( var e : Parser.TYPES.entrySet() ) { + if( e.getValue() instanceof TypeInteger ) { INTTYPES.add(new TypeInt(e.getKey())); } } |
