summaryrefslogtreecommitdiff
path: root/chapter02/src/test/java
diff options
context:
space:
mode:
authorThalia Archibald <thalia@archibald.dev>2024-06-21 12:58:51 -0700
committerGitHub <noreply@github.com>2024-06-21 20:58:51 +0100
commitfd8442075faa603b3956d51d81486424624c5355 (patch)
tree029c1afb736ff0e350c2b032318e5fc376c7e1e4 /chapter02/src/test/java
parente5f4f7b85fbb91a293a7d89d7b85adfbaeeb51f4 (diff)
downloadSimple-fd8442075faa603b3956d51d81486424624c5355.tar.gz
Simple-fd8442075faa603b3956d51d81486424624c5355.zip
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
Diffstat (limited to 'chapter02/src/test/java')
-rw-r--r--chapter02/src/test/java/com/seaofnodes/simple/Chapter01Test.java99
-rw-r--r--chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java86
2 files changed, 106 insertions, 79 deletions
diff --git a/chapter02/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter02/src/test/java/com/seaofnodes/simple/Chapter01Test.java
new file mode 100644
index 00000000..49edfb5b
--- /dev/null
+++ b/chapter02/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 a statement: ret",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 a statement: return100",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/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java
index 35464955..36e62f9b 100644
--- a/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java
+++ b/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -2,19 +2,14 @@ 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 Chapter02Test {
- @Rule
- public ExpectedException exceptionRule = ExpectedException.none();
-
@Test
- public void testChapter2ParseGrammar() {
+ public void testParseGrammar() {
Node._disablePeephole = true; // disable peephole so we can observe full graph
Parser parser = new Parser("return 1+2*3+-5;");
ReturnNode ret = parser.parse();
@@ -25,42 +20,42 @@ public class Chapter02Test {
}
@Test
- public void testChapter2AddPeephole() {
+ public void testAddPeephole() {
Parser parser = new Parser("return 1+2;");
ReturnNode ret = parser.parse();
assertEquals("return 3;", ret.print());
}
@Test
- public void testChapter2SubPeephole() {
+ public void testSubPeephole() {
Parser parser = new Parser("return 1-2;");
ReturnNode ret = parser.parse();
assertEquals("return -1;", ret.print());
}
@Test
- public void testChapter2MulPeephole() {
+ public void testMulPeephole() {
Parser parser = new Parser("return 2*3;");
ReturnNode ret = parser.parse();
assertEquals("return 6;", ret.print());
}
@Test
- public void testChapter2DivPeephole() {
+ public void testDivPeephole() {
Parser parser = new Parser("return 6/3;");
ReturnNode ret = parser.parse();
assertEquals("return 2;", ret.print());
}
@Test
- public void testChapter2MinusPeephole() {
+ public void testMinusPeephole() {
Parser parser = new Parser("return 6/-3;");
ReturnNode ret = parser.parse();
assertEquals("return -2;", ret.print());
}
@Test
- public void testChapter2Example() {
+ public void testExample() {
Parser parser = new Parser("return 1+2*3+-5;");
ReturnNode ret = parser.parse();
assertEquals("return 2;", ret.print());
@@ -68,71 +63,4 @@ public class Chapter02Test {
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 return: ret",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());
- }
}