summaryrefslogtreecommitdiff
path: root/src/test/java/com
diff options
context:
space:
mode:
authorDibyendu Majumdar <mobile@majumdar.org.uk>2023-10-01 18:01:54 +0100
committerDibyendu Majumdar <mobile@majumdar.org.uk>2024-11-27 11:29:33 -0800
commita2a42257aaf2d78fd386f1bedd12f41bde3df119 (patch)
tree9fd9c9f1267bb48ceb6dc16fa8df609c40a93b46 /src/test/java/com
parent3b02b5bdffaf2e3191d55fbb468dc765eb439025 (diff)
downloadSimple-a2a42257aaf2d78fd386f1bedd12f41bde3df119.tar.gz
Simple-a2a42257aaf2d78fd386f1bedd12f41bde3df119.zip
Chapter 3: Variable Declarations
Co-authored-by: Cliff Click <cliffc@acm.org> Co-authored-by: Yichen Yan <oraluben@outlook.com> Co-authored-by: Thalia Archibald <thalia@archibald.dev> Co-authored-by: Tamas Hadhazy <helsgithub15@gmail.com> Co-authored-by: XmiliaH <45106915+XmiliaH@users.noreply.github.com>
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter01Test.java4
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter02Test.java2
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter03Test.java67
3 files changed, 70 insertions, 3 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index 49edfb5b..311ca23b 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -40,7 +40,7 @@ public class Chapter01Test {
new Parser("ret").parse();
fail();
} catch( RuntimeException e ) {
- assertEquals("Syntax error, expected a statement: ret",e.getMessage());
+ assertEquals("Syntax error, expected =: ",e.getMessage());
}
}
@@ -82,7 +82,7 @@ public class Chapter01Test {
new Parser("return100").parse();
fail();
} catch( RuntimeException e ) {
- assertEquals("Syntax error, expected a statement: return100",e.getMessage());
+ assertEquals("Syntax error, expected =: ",e.getMessage());
}
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/src/test/java/com/seaofnodes/simple/Chapter02Test.java
index 36e62f9b..60ee8874 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter02Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -10,8 +10,8 @@ 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;");
+ 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();
diff --git a/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/src/test/java/com/seaofnodes/simple/Chapter03Test.java
new file mode 100644
index 00000000..bdb1d9ea
--- /dev/null
+++ b/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -0,0 +1,67 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+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;");
+ ReturnNode 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;");
+ ReturnNode 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;");
+ ReturnNode 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;
+ ReturnNode 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;");
+ ReturnNode 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());
+ }
+ }
+
+}