summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorDibyendu Majumdar <mobile@majumdar.org.uk>2023-09-26 17:43:36 +0100
committerDibyendu Majumdar <mobile@majumdar.org.uk>2025-06-13 15:42:08 -0700
commit0c1949a40dae7770045d64cb97036e6072b99dfd (patch)
tree19e04b16eef9798045d17389ce4e280f7483d0bc /src/test
parent8e8dcf86abfdd8d90b11ede7db2e94666f4b7356 (diff)
downloadSimple-0c1949a40dae7770045d64cb97036e6072b99dfd.tar.gz
Simple-0c1949a40dae7770045d64cb97036e6072b99dfd.zip
Chapter 2: Arithmetic
Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Yichen Yan <oraluben@outlook.com> Co-authored-by: Tamás Hadházy <85063808+Hels15@users.noreply.github.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com> Co-authored-by: hadha <helsgithub15@gmail.com> Co-authored-by: RobertObkircher <robert.obkircher@gmail.com>
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter01Test.java26
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter02Test.java66
2 files changed, 75 insertions, 17 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index a91aabb1..49edfb5b 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -1,6 +1,7 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -17,7 +18,7 @@ public class Chapter01Test {
Node expr = ret.expr();
if( expr instanceof ConstantNode con ) {
assertEquals(start,con.in(0));
- assertEquals(1, con._value);
+ assertEquals(TypeInteger.constant(1), con._type);
} else {
fail();
}
@@ -30,7 +31,7 @@ public class Chapter01Test {
StartNode start = Parser.START;
for( Node use : start._outputs )
if( use instanceof ConstantNode con )
- assertEquals(0,con._value);
+ assertEquals(TypeInteger.constant(0),con._type);
}
@Test
@@ -54,13 +55,9 @@ public class Chapter01Test {
}
@Test
- public void testBad3() {
- try {
- new Parser("return --12;").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, expected integer literal",e.getMessage());
- }
+ public void testNotBad3() {
+ // this test used to fail in chapter 1
+ assertEquals("return 12;", new Parser("return --12;").parse().print());
}
@Test
@@ -73,15 +70,10 @@ public class Chapter01Test {
}
}
- // Negative numbers require unary operator support that is not in scope
@Test
- public void testBad5() {
- try {
- new Parser("return -100;").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, expected integer literal",e.getMessage());
- }
+ public void testNotBad5() {
+ // this test used to fail in chapter 1
+ assertEquals("return -100;", new Parser("return -100;").parse().print());
}
@Test
diff --git a/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/src/test/java/com/seaofnodes/simple/Chapter02Test.java
new file mode 100644
index 00000000..36e62f9b
--- /dev/null
+++ b/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() {
+ Node._disablePeephole = true; // disable peephole so we can observe full graph
+ Parser parser = new Parser("return 1+2*3+-5;");
+ 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));
+ }
+
+}