diff options
Diffstat (limited to 'chapter08/src/test/java/com/seaofnodes')
5 files changed, 16 insertions, 72 deletions
diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java index 47cee096..a6075b75 100644 --- a/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter02Test.java @@ -11,10 +11,8 @@ 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()); - Node._disablePeephole = false; + assertEquals("return 2;", ret.print()); } @Test diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java index 64ff153e..609dff76 100644 --- a/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter03Test.java @@ -30,15 +30,6 @@ public class Chapter03Test { } @Test - public void testVarScopeNoPeephole() { - Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c; "); - 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); "); StopNode ret = parser.parse(); diff --git a/chapter08/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter08/src/test/java/com/seaofnodes/simple/Chapter07Test.java index 40933f21..26b5c17d 100644 --- a/chapter08/src/test/java/com/seaofnodes/simple/Chapter07Test.java +++ b/chapter08/src/test/java/com/seaofnodes/simple/Chapter07Test.java @@ -17,11 +17,9 @@ public class Chapter07Test { } return arg; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); assertEquals("return Phi(Loop6,arg,(Phi_arg+1));", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test @@ -72,11 +70,9 @@ while(a < 10) { } return b; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); assertEquals("return Phi(Loop8,2,Phi(Region26,Phi_b,4));", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test @@ -110,11 +106,9 @@ while(a < 10) { } return a; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); - assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString()); + assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test @@ -141,11 +135,9 @@ int a = 1; while(arg) a = 2; return a; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); assertEquals("return Phi(Loop7,1,2);", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test @@ -172,11 +164,9 @@ while(a < 10) { } return a; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); - assertEquals("return Phi(Loop7,1,((Phi_a+1)+2));", stop.toString()); + assertEquals("return Phi(Loop7,1,(Phi_a+3));", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test @@ -207,11 +197,9 @@ while(a < 10) { } return a; """); - Node._disablePeephole = true; StopNode stop = parser.parse(); - assertEquals("return Phi(Loop8,1,((Phi_a+1)+2));", stop.toString()); + assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString()); assertTrue(stop.ret().ctrl() instanceof ProjNode); - Node._disablePeephole = false; } @Test diff --git a/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java b/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java index 861c8b3a..ef73fa4b 100644 --- a/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java +++ b/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java @@ -13,8 +13,8 @@ import java.util.function.Consumer; * This will generate a script similar to how the parser parses it but instead of parsing it will * randomly choose a variation the parser would parse and generate it. * These scripts are the parsed by the parser and all exceptions are caught and filtered - * to only show one occurrence of one problem. This includes script which generate different - * results when the compiled graph with and without peeps is executed. + * to only show one occurrence of one problem. Generated graphs are evaluated + * to detect compiler/evaluator failures; this chapter has no differential oracle. * To aid debugging scripts that cause errors are then reduced by applying rules and checking * that the same issue persists. */ @@ -49,49 +49,17 @@ public class Fuzzer { System.out.flush(); } - /** - * Check that two graphs result in the same output when supplied with a value - * @param stop1 Stop node of the first graph - * @param stop2 Stop node of the second graph - * @param in The input value to both graphs to test for an equal output - */ - private static void checkGraphs(StopNode stop1, StopNode stop2, long in) { - var e1 = GraphEvaluator.evaluateWithResult(stop1, in, EVAL_TIMEOUT); - var e2 = GraphEvaluator.evaluateWithResult(stop2, in, EVAL_TIMEOUT); - if (e1.type() == GraphEvaluator.ResultType.TIMEOUT || e2.type() == GraphEvaluator.ResultType.TIMEOUT) return; - if (e1.type() != e2.type()) - throw new RuntimeException("Different calculations types " + e1.type() + " vs " + e2.type()); - if (e1.value() != e2.value()) - throw new RuntimeException("Different calculations values " + e1.value() + " vs " + e2.value()); - } - - /** - * Run checks for script. Compile the script with peeps enabled and disabled. - * Check that exceptions raised in the parser by both methods are the same and only happens if the script may be invalid. - * If the script was successfully parsed check that both version behave the same. - * @param script The script to test - * @param valid If the script is definitely valid. If not some exceptions may be suppressed. - */ + /** Compile and evaluate generated programs with normal peepholes. */ private static void runCheck(String script, boolean valid) { - StopNode stop1; + StopNode stop; try { - stop1 = FuzzerUtils.parse(script, false); - } catch (RuntimeException e1) { - try { - FuzzerUtils.parse(script, true); - } catch (RuntimeException e2) { - if (FuzzerUtils.isExceptionFromSameCause(e1, e2)) { - if (!valid || e1.getClass() == RuntimeException.class) return; - } else { - e1.addSuppressed(e2); - } - } - throw e1; + stop = FuzzerUtils.parse(script); + } catch (RuntimeException e) { + if (!valid || e.getClass() == RuntimeException.class) return; + throw e; } - var stop2 = FuzzerUtils.parse(script, true); - checkGraphs(stop1, stop2, 0); - checkGraphs(stop1, stop2, 1); - checkGraphs(stop1, stop2, 10); + for (int input : new int[] {0, 1, 10}) + GraphEvaluator.evaluateWithResult(stop, input, EVAL_TIMEOUT); } /** diff --git a/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java b/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java index 37a57e11..0e83861d 100644 --- a/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java +++ b/chapter08/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java @@ -47,11 +47,10 @@ class FuzzerUtils { } /** - * Parse script with peepholes enabled or disabled + * Parse script with normal peephole optimization */ - public static StopNode parse(String script, boolean runPeeps) { + public static StopNode parse(String script) { var parser = new Parser(script); - Node._disablePeephole = !runPeeps; return parser.parse(); } |
