From fd8442075faa603b3956d51d81486424624c5355 Mon Sep 17 00:00:00 2001 From: Thalia Archibald Date: Fri, 21 Jun 2024 12:58:51 -0700 Subject: Backport tests (#87) * Fix unexpected number parse errors * Add chapter titles * Split test files by chapter * Backport rename Fuzzer08Test to FuzzerWrap * Replace graph build scripts with Makefile * Backport Chapter05Test.testRegress1 * Backport require space after 'return' * Backport check EOF after return statement --- .../java/com/seaofnodes/simple/Chapter01Test.java | 100 ++ .../java/com/seaofnodes/simple/Chapter02Test.java | 66 ++ .../java/com/seaofnodes/simple/Chapter03Test.java | 68 ++ .../java/com/seaofnodes/simple/Chapter04Test.java | 128 +++ .../java/com/seaofnodes/simple/Chapter05Test.java | 212 ++++ .../java/com/seaofnodes/simple/Chapter06Test.java | 294 ++++++ .../java/com/seaofnodes/simple/Chapter07Test.java | 240 +++++ .../java/com/seaofnodes/simple/Chapter08Test.java | 1050 +------------------- .../java/com/seaofnodes/simple/Fuzzer08Test.java | 26 - .../java/com/seaofnodes/simple/FuzzerWrap.java | 26 + 10 files changed, 1146 insertions(+), 1064 deletions(-) create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter01Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter04Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter05Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter06Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Chapter07Test.java delete mode 100644 chapter08/src/test/java/com/seaofnodes/simple/Fuzzer08Test.java create mode 100644 chapter08/src/test/java/com/seaofnodes/simple/FuzzerWrap.java (limited to 'chapter08/src/test/java/com/seaofnodes') diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter01Test.java new file mode 100644 index 00000000..13b42f96 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter01Test.java @@ -0,0 +1,100 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter01Test { + + @Test + public void testSimpleProgram() { + Parser parser = new Parser("return 1;"); + StopNode stop = parser.parse(); + StartNode start = Parser.START; + ReturnNode ret = (ReturnNode)stop.in(0); + + assertTrue(ret.ctrl() instanceof ProjNode); + Node expr = ret.expr(); + if( expr instanceof ConstantNode con ) { + assertEquals(start,con.in(0)); + assertEquals(TypeInteger.constant(1), con._type); + } else { + fail(); + } + } + + @Test + public void testZero() { + Parser parser = new Parser("return 0;"); + parser.parse(); + StartNode start = Parser.START; + for( Node use : start._outputs ) + if( use instanceof ConstantNode con && con._type instanceof TypeInteger ) + assertEquals(TypeInteger.constant(0),con._type); + } + + @Test + public void testBad1() { + try { + new Parser("ret").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected =: ",e.getMessage()); + } + } + + @Test + public void testBad2() { + try { + new Parser("return 0123;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage()); + } + } + + @Test + public void testNotBad3() { + // this test used to fail in chapter 1 + assertEquals("return 12;", new Parser("return --12;").parse().print()); + } + + @Test + public void testBad4() { + try { + new Parser("return 100").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected ;: ",e.getMessage()); + } + } + + @Test + public void testNotBad5() { + // this test used to fail in chapter 1 + assertEquals("return -100;", new Parser("return -100;").parse().print()); + } + + @Test + public void testBad6() { + try { + new Parser("return100").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected =: ",e.getMessage()); + } + } + + @Test + public void testBad7() { + try { + new Parser("return 1;}").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, unexpected }",e.getMessage()); + } + } + +} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java new file mode 100644 index 00000000..4085ed7e --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java @@ -0,0 +1,66 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter02Test { + + @Test + public void testParseGrammar() { + Parser parser = new Parser("return 1+2*3+-5;"); + Node._disablePeephole = true; // disable peephole so we can observe full graph + StopNode ret = parser.parse(); + assertEquals("return (1+((2*3)+(-5)));", ret.print()); + GraphVisualizer gv = new GraphVisualizer(); + System.out.println(gv.generateDotOutput(parser)); + Node._disablePeephole = false; + } + + @Test + public void testAddPeephole() { + Parser parser = new Parser("return 1+2;"); + StopNode ret = parser.parse(); + assertEquals("return 3;", ret.print()); + } + + @Test + public void testSubPeephole() { + Parser parser = new Parser("return 1-2;"); + StopNode ret = parser.parse(); + assertEquals("return -1;", ret.print()); + } + + @Test + public void testMulPeephole() { + Parser parser = new Parser("return 2*3;"); + StopNode ret = parser.parse(); + assertEquals("return 6;", ret.print()); + } + + @Test + public void testDivPeephole() { + Parser parser = new Parser("return 6/3;"); + StopNode ret = parser.parse(); + assertEquals("return 2;", ret.print()); + } + + @Test + public void testMinusPeephole() { + Parser parser = new Parser("return 6/-3;"); + StopNode ret = parser.parse(); + assertEquals("return -2;", ret.print()); + } + + @Test + public void testExample() { + Parser parser = new Parser("return 1+2*3+-5;"); + StopNode ret = parser.parse(); + assertEquals("return 2;", ret.print()); + GraphVisualizer gv = new GraphVisualizer(); + System.out.println(gv.generateDotOutput(parser)); + } + +} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java new file mode 100644 index 00000000..407fbe48 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java @@ -0,0 +1,68 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter03Test { + + @Test + public void testVarDecl() { + Parser parser = new Parser("int a=1; return a;"); + StopNode ret = parser.parse(); + assertEquals("return 1;", ret.print()); + } + + @Test + public void testVarAdd() { + Parser parser = new Parser("int a=1; int b=2; return a+b;"); + StopNode ret = parser.parse(); + assertEquals("return 3;", ret.print()); + } + + @Test + public void testVarScope() { + Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;"); + StopNode ret = parser.parse(); + assertEquals("return 4;", ret.print()); + } + + @Test + public void testVarScopeNoPeephole() { + Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; #showGraph; } return c; #showGraph;"); + Node._disablePeephole = true; + StopNode ret = parser.parse(); + Node._disablePeephole = false; + assertEquals("return (1+3);", ret.print()); + } + + @Test + public void testVarDist() { + Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return 8;", ret.print()); + } + + @Test + public void testSelfAssign() { + try { + new Parser("int a=a; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'a'",e.getMessage()); + } + } + + @Test + public void testBad1() { + try { + new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected }: ",e.getMessage()); + } + } + +} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter04Test.java new file mode 100644 index 00000000..cde23324 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter04Test.java @@ -0,0 +1,128 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter04Test { + + @Test + public void testPeephole() { + Parser parser = new Parser("return 1+arg+2; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return (arg+3);", ret.print()); + } + + @Test + public void testPeephole2() { + Parser parser = new Parser("return (1+arg)+2;"); + StopNode ret = parser.parse(); + assertEquals("return (arg+3);", ret.print()); + } + + @Test + public void testAdd0() { + Parser parser = new Parser("return 0+arg;"); + StopNode ret = parser.parse(); + assertEquals("return arg;", ret.print()); + } + + @Test + public void testAddAddMul() { + Parser parser = new Parser("return arg+0+arg;"); + StopNode ret = parser.parse(); + assertEquals("return (arg*2);", ret.print()); + } + + @Test + public void testPeephole3() { + Parser parser = new Parser("return 1+arg+2+arg+3; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return ((arg*2)+6);", ret.print()); + } + + @Test + public void testMul1() { + Parser parser = new Parser("return 1*arg;"); + StopNode ret = parser.parse(); + assertEquals("return arg;", ret.print()); + } + + @Test + public void testVarArg() { + Parser parser = new Parser("return arg; #showGraph;"); + StopNode stop = parser.parse(); + ReturnNode ret = stop.ret(); + assertTrue(ret.in(0) instanceof ProjNode); + assertTrue(ret.in(1) instanceof ProjNode); + } + + @Test + public void testConstantArg() { + Parser parser = new Parser("return arg; #showGraph;", TypeInteger.constant(2)); + StopNode ret = parser.parse(); + assertEquals("return 2;", ret.print()); + } + + @Test + public void testCompEq() { + Parser parser = new Parser("return 3==3; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return 1;", ret.print()); + } + + @Test + public void testCompEq2() { + Parser parser = new Parser("return 3==4; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return 0;", ret.print()); + } + + @Test + public void testCompNEq() { + Parser parser = new Parser("return 3!=3; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return 0;", ret.print()); + } + + @Test + public void testCompNEq2() { + Parser parser = new Parser("return 3!=4; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return 1;", ret.print()); + } + + @Test + public void testBug1() { + Parser parser = new Parser("int a=arg+1; int b=a; b=1; return a+2; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return (arg+3);", ret.print()); + } + + @Test + public void testBug2() { + Parser parser = new Parser("int a=arg+1; a=a; return a; #showGraph;"); + StopNode ret = parser.parse(); + assertEquals("return (arg+1);", ret.print()); + } + + @Test + public void testBug3() { + try { + new Parser("inta=1; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'inta'",e.getMessage()); + } + } + + @Test + public void testBug4() { + Parser parser = new Parser("return -arg;"); + StopNode ret = parser.parse(); + assertEquals("return (-arg);", ret.print()); + } + +} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter05Test.java new file mode 100644 index 00000000..ea348968 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter05Test.java @@ -0,0 +1,212 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter05Test { + + @Test + public void testIfStmt() { + Parser parser = new Parser( +""" +int a = 1; +if (arg == 1) + a = arg+2; +else { + a = arg-3; + #showGraph; +} +#showGraph; +return a;"""); + StopNode ret = parser.parse(true); + assertEquals("return Phi(Region17,(arg+2),(arg-3));", ret.toString()); + } + + @Test + public void testTest() { + Parser parser = new Parser( +""" +int c = 3; +int b = 2; +if (arg == 1) { + b = 3; + c = 4; +} +return c;""", TypeInteger.BOT); + StopNode ret = parser.parse(true); + assertEquals("return Phi(Region16,4,3);", ret.toString()); + } + + @Test + public void testReturn2() { + Parser parser = new Parser( +""" +if( arg==1 ) + return 3; +else + return 4; +#showGraph;""", TypeInteger.BOT); + StopNode stop = parser.parse(); + assertEquals("Stop[ return 3; return 4; ]", stop.toString()); + } + + @Test + public void testIfMergeB() { + Parser parser = new Parser( +""" +int a=arg+1; +int b=0; +if( arg==1 ) + b=a; +else + b=a+1; +return a+b;"""); + StopNode ret = parser.parse(true); + assertEquals("return ((arg*2)+Phi(Region20,2,3));", ret.toString()); + } + + @Test + public void testIfMerge2() { + Parser parser = new Parser( +""" +int a=arg+1; +int b=arg+2; +if( arg==1 ) + b=b+a; +else + a=b+1; +return a+b;"""); + StopNode ret = parser.parse(true); + assertEquals("return ((Phi(Region31,(arg*2),arg)+arg)+Phi(Region,4,5));", ret.toString()); + } + + @Test + public void testMerge3() { + Parser parser = new Parser( +""" +int a=1; +if( arg==1 ) + if( arg==2 ) + a=2; + else + a=3; +else if( arg==3 ) + a=4; +else + a=5; +return a; +#showGraph;""", TypeInteger.BOT); + StopNode stop = parser.parse(); + assertEquals("return Phi(Region33,Phi(Region21,2,3),Phi(Region31,4,5));", stop.toString()); + } + + @Test + public void testMerge4() { + Parser parser = new Parser( +""" +int a=0; +int b=0; +if( arg ) + a=1; +if( arg==0 ) + b=2; +return arg+a+b; +#showGraph;""", TypeInteger.BOT); + StopNode stop = parser.parse(); + assertEquals("return ((arg+Phi(Region13,1,0))+Phi(Region22,2,0));", stop.toString()); + } + + @Test + public void testMerge5() { + Parser parser = new Parser( +""" +int a=arg==2; +if( arg==1 ) +{ + a=arg==3; +} +return a;"""); + StopNode ret = parser.parse(true); + assertEquals("return (arg==Phi(Region16,3,2));", ret.toString()); + } + + @Test + public void testTrue() { + StopNode stop = new Parser("return true;").parse(); + assertEquals("return 1;",stop.toString()); + } + + @Test + public void testHalfDef() { + try { + new Parser("if( arg==1 ) int b=2; return b;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Cannot define a new name on one arm of an if",e.getMessage()); + } + } + + @Test + public void testHalfDef2() { + try { + new Parser("if( arg==1 ) { int b=2; } else { int b=3; } return b;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Undefined name 'b'",e.getMessage()); + } + } + + @Test + public void testRegress1() { + try { + new Parser("if(arg==2) int a=1; else int b=2; return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Cannot define a new name on one arm of an if",e.getMessage()); + } + } + + @Test + public void testBadNum() { + try { + new Parser("return 1-;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected an identifier or expression: ;",e.getMessage()); + } + } + + @Test + public void testKeyword1() { + try { + new Parser("int true=0; return true;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Expected an identifier, found 'true'",e.getMessage()); + } + } + + @Test + public void testKeyword2() { + try { + new Parser("int else=arg; if(else) else=2; else else=1; return else;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Expected an identifier, found 'else'",e.getMessage()); + } + } + + @Test + public void testKeyword3() { + try { + new Parser("int a=1; ififif(arg)inta=2;return a;").parse(); + fail(); + } catch( RuntimeException e ) { + assertEquals("Syntax error, expected =: (",e.getMessage()); + } + } + +} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter06Test.java new file mode 100644 index 00000000..618c8ca6 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter06Test.java @@ -0,0 +1,294 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.node.*; +import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class Chapter06Test { + + @Test + public void testPeepholeReturn() { + Parser parser = new Parser( +""" +if( true ) return 2; +return 1; +"""); + StopNode stop = parser.parse(true); + assertEquals("return 2;", stop.toString()); + assertTrue(stop.ret().ctrl() instanceof ProjNode); + } + + @Test + public void testPeepholeRotate() { + Parser parser = new Parser( +""" +int a = 1; +if (arg) + a = 2; +return (arg < a) < 3; +"""); + StopNode stop = parser.parse(false); + assertEquals("return ((arg--arg) arg=arg+1; return 0;"); StopNode stop = parser.parse(true); assertEquals("Stop[ return 0; return 0; ]", stop.toString()); } @Test - public void testChapter8BreakOutsideLoop() { + public void testBreakOutsideLoop() { try { new Parser(""" if(arg <= 10) { @@ -172,7 +172,7 @@ return arg; } @Test - public void testChapter8Regress3() { + public void testRegress3() { Parser parser = new Parser(""" while(arg < 10) { break; @@ -184,7 +184,7 @@ return arg; } @Test - public void testChapter8Regress4() { + public void testRegress4() { Parser parser = new Parser(""" int a = 1; while(arg < 10) { @@ -202,7 +202,7 @@ return a; } @Test - public void testChapter8Regress5() { + public void testRegress5() { Parser parser = new Parser(""" int a = 1; while(1) { @@ -217,1030 +217,4 @@ return a; assertTrue(stop.ret().ctrl() instanceof RegionNode); } - @Test - public void testChapter7Example() { - Parser parser = new Parser( - """ - while(arg < 10) { - arg = arg + 1; - #showGraph; - } - return arg; - """); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop6,arg,(Phi_arg+1));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - } - - @Test - public void testChapter7Regression() { - Parser parser = new Parser( - -""" -int a = 1; -if(arg){}else{ - while(a < 10) { - a = a + 1; - } -} -return a; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Region22,1,Phi(Loop11,1,(Phi_a+1)));", stop.toString()); - } - - @Test - public void testChapter7WhileNested() { - Parser parser = new Parser( - -""" -int sum = 0; -int i = 0; -while(i < arg) { - i = i + 1; - int j = 0; - while( j < arg ) { - sum = sum + j; - j = j + 1; - } -} -return sum; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop8,0,Phi(Loop20,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", stop.toString()); - System.out.println(IRPrinter.prettyPrint(stop,99)); - } - - @Test - public void testChapter7WhileScope() { - Parser parser = new Parser( -""" -int a = 1; -int b = 2; -while(a < 10) { - if (a == 2) a = 3; - else b = 4; -} -return b; -"""); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop8,2,Phi(Region26,Phi_b,4));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - System.out.println(IRPrinter.prettyPrint(stop,99)); - } - - @Test - public void testChapter7WhileNestedIfAndInc() { - Parser parser = new Parser( -""" -int a = 1; -int b = 2; -while(a < 10) { - if (a == 2) a = 3; - else b = 4; - b = b + 1; - a = a + 1; -} -return b; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop8,2,(Phi(Region26,Phi_b,4)+1));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - System.out.println(IRPrinter.prettyPrint(stop,99)); - } - - - @Test - public void testChapter7While() { - Parser parser = new Parser( -""" -int a = 1; -while(a < 10) { - a = a + 1; - a = a + 2; -} -return a; -"""); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - } - - @Test - public void testChapter7WhilePeep() { - Parser parser = new Parser( -""" -int a = 1; -while(a < 10) { - a = a + 1; - a = a + 2; -} -return a; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - } - - @Test - public void testChapter7While2() { - Parser parser = new Parser( -""" -int a = 1; -while(arg) a = 2; -return a; -"""); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop7,1,2);", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - System.out.println(IRPrinter.prettyPrint(stop,99)); - } - - @Test - public void testChapter7While2Peep() { - Parser parser = new Parser( -""" -int a = 1; -while(arg) a = 2; -return a; -"""); - StopNode stop = parser.parse(false); - assertEquals("return Phi(Loop7,1,2);", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - System.out.println(IRPrinter.prettyPrint(stop, 99)); - } - - @Test - public void testChapter7While3() { - Parser parser = new Parser( -""" -int a = 1; -while(a < 10) { - int b = a + 1; - a = b + 2; -} -return a; -"""); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - } - - @Test - public void testChapter7While3Peep() { - Parser parser = new Parser( -""" -int a = 1; -while(a < 10) { - int b = a + 1; - a = b + 2; -} -return a; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - } - - @Test - public void testChapter7While4() { - Parser parser = new Parser( -""" -int a = 1; -int b = 2; -while(a < 10) { - int b = a + 1; - a = b + 2; -} -return a; -"""); - Node._disablePeephole = true; - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop8,1,((Phi_a+1)+2));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; - } - - @Test - public void testChapter7While4Peep() { - Parser parser = new Parser( -""" -int a = 1; -int b = 2; -while(a < 10) { - int b = a + 1; - a = b + 2; -} -return a; -"""); - StopNode stop = parser.parse(true); - assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - } - - @Test - public void testChapter6PeepholeReturn() { - Parser parser = new Parser( -""" -if( true ) return 2; -return 1; -"""); - StopNode stop = parser.parse(true); - assertEquals("return 2;", stop.toString()); - assertTrue(stop.ret().ctrl() instanceof ProjNode); - } - - @Test - public void testChapter6PeepholeRotate() { - Parser parser = new Parser( -""" -int a = 1; -if (arg) - a = 2; -return (arg < a) < 3; -"""); - StopNode stop = parser.parse(false); - assertEquals("return ((argfuzzPeeps methods. - * This can be done from IntelliJ Gui or remove the `@Ignore` annotation and start the test case from the command line - * via mvn clean test -Dtest=com.seaofnodes.simple.Fuzzer08Test#fuzzPeeps - */ -public class Fuzzer08Test { - - @Test - @Ignore - public void fuzzPeeps() { - var fuzzer = new Fuzzer(); - for (int i=0; i<100000; i++) - fuzzer.fuzzPeeps(i); - assertTrue(fuzzer.noExceptions()); - } - -} diff --git a/chapter08/src/test/java/com/seaofnodes/simple/FuzzerWrap.java b/chapter08/src/test/java/com/seaofnodes/simple/FuzzerWrap.java new file mode 100644 index 00000000..82a6a472 --- /dev/null +++ b/chapter08/src/test/java/com/seaofnodes/simple/FuzzerWrap.java @@ -0,0 +1,26 @@ +package com.seaofnodes.simple; + +import com.seaofnodes.simple.fuzzer.Fuzzer; +import org.junit.Ignore; +import org.junit.Test; + + +import static org.junit.Assert.assertTrue; + +/** + * To use the fuzzer run the fuzzPeeps methods. + * This can be done from IntelliJ Gui or remove the `@Ignore` annotation and start the test case from the command line + * via mvn clean test -Dtest=com.seaofnodes.simple.FuzzerWrap#fuzzPeeps + */ +public class FuzzerWrap { + + @Test + @Ignore + public void fuzzPeeps() { + var fuzzer = new Fuzzer(); + for (int i=0; i<100000; i++) + fuzzer.fuzzPeeps(i); + assertTrue(fuzzer.noExceptions()); + } + +} -- cgit v1.2.3