summaryrefslogtreecommitdiff
path: root/chapter03/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'chapter03/src/test/java')
-rw-r--r--chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java99
-rw-r--r--chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java66
-rw-r--r--chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java138
3 files changed, 165 insertions, 138 deletions
diff --git a/chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java
new file mode 100644
index 00000000..311ca23b
--- /dev/null
+++ b/chapter03/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -0,0 +1,99 @@
+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;");
+ ReturnNode ret = parser.parse();
+ StartNode start = Parser.START;
+
+ assertEquals(start, ret.ctrl());
+ 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 )
+ 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/chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter02Test.java
new file mode 100644
index 00000000..60ee8874
--- /dev/null
+++ b/chapter03/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
+ ReturnNode 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;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testSubPeephole() {
+ Parser parser = new Parser("return 1-2;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return -1;", ret.print());
+ }
+
+ @Test
+ public void testMulPeephole() {
+ Parser parser = new Parser("return 2*3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 6;", ret.print());
+ }
+
+ @Test
+ public void testDivPeephole() {
+ Parser parser = new Parser("return 6/3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ }
+
+ @Test
+ public void testMinusPeephole() {
+ Parser parser = new Parser("return 6/-3;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return -2;", ret.print());
+ }
+
+ @Test
+ public void testExample() {
+ Parser parser = new Parser("return 1+2*3+-5;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 2;", ret.print());
+ GraphVisualizer gv = new GraphVisualizer();
+ System.out.println(gv.generateDotOutput(parser));
+ }
+
+}
diff --git a/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java
index 965a100a..950d87fe 100644
--- a/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java
+++ b/chapter03/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -2,17 +2,12 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Rule;
import org.junit.Test;
-import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
public class Chapter03Test {
- @Rule
- public ExpectedException exceptionRule = ExpectedException.none();
-
@Test
public void testVarDecl() {
Parser parser = new Parser("int a=1; return a;");
@@ -61,131 +56,8 @@ public class Chapter03Test {
}
@Test
- public void testChapter2ParseGrammar() {
- Parser parser = new Parser("return 1+2*3+-5;");
- Node._disablePeephole = true; // disable peephole so we can observe full graph
- ReturnNode 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 testChapter2AddPeephole() {
- Parser parser = new Parser("return 1+2;");
- ReturnNode ret = parser.parse();
- assertEquals("return 3;", ret.print());
- }
-
- @Test
- public void testChapter2SubPeephole() {
- Parser parser = new Parser("return 1-2;");
- ReturnNode ret = parser.parse();
- assertEquals("return -1;", ret.print());
- }
-
- @Test
- public void testChapter2MulPeephole() {
- Parser parser = new Parser("return 2*3;");
- ReturnNode ret = parser.parse();
- assertEquals("return 6;", ret.print());
- }
-
- @Test
- public void testChapter2DivPeephole() {
- Parser parser = new Parser("return 6/3;");
- ReturnNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
- }
-
- @Test
- public void testChapter2MinusPeephole() {
- Parser parser = new Parser("return 6/-3;");
- ReturnNode ret = parser.parse();
- assertEquals("return -2;", ret.print());
- }
-
- @Test
- public void testChapter2Example() {
- Parser parser = new Parser("return 1+2*3+-5;");
- ReturnNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
- GraphVisualizer gv = new GraphVisualizer();
- System.out.println(gv.generateDotOutput(parser));
- }
-
- @Test
- public void testSimpleProgram() {
- Parser parser = new Parser("return 1;");
- ReturnNode ret = parser.parse();
- StartNode start = Parser.START;
-
- assertEquals(start, ret.ctrl());
- 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 )
- 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("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
fail();
} catch( RuntimeException e ) {
@@ -193,14 +65,4 @@ public class Chapter03Test {
}
}
- @Test
- public void testBad7() {
- try {
- new Parser("return 1;}").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, unexpected }",e.getMessage());
- }
- }
-
}