summaryrefslogtreecommitdiff
path: root/chapter01/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorDibyendu Majumdar <dibyendumajumdar@users.noreply.github.com>2023-09-19 23:48:34 +0100
committerGitHub <noreply@github.com>2023-09-19 23:48:34 +0100
commit62bfd0d619b28892152a7d89639f3437bc9df573 (patch)
tree0e628f6a331a0c7e3b56424021243535019f7784 /chapter01/src/test/java/com/seaofnodes
parentee3a2f571897b23caecfe9cfe4f5e4338007f885 (diff)
parentcecb62d847da7dc937b52857ae2fd4b41af98a7c (diff)
downloadSimple-62bfd0d619b28892152a7d89639f3437bc9df573.tar.gz
Simple-62bfd0d619b28892152a7d89639f3437bc9df573.zip
Merge pull request #3 from dibyendumajumdar/main
Chapter 1 implementation
Diffstat (limited to 'chapter01/src/test/java/com/seaofnodes')
-rw-r--r--chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java72
1 files changed, 72 insertions, 0 deletions
diff --git a/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
new file mode 100644
index 00000000..887f63b8
--- /dev/null
+++ b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
@@ -0,0 +1,72 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class ParserTest {
+
+ @Test
+ public void testSimpleProgram() {
+ Parser parser = new Parser();
+ StartNode startNode = parser.parse("return 1;");
+ assertNotNull(startNode);
+ assertEquals(2, startNode.nOuts());
+ for (int i = 0; i < startNode.nOuts(); i++) {
+ switch( startNode.out(i) ) {
+ case ReturnNode ret:
+ assertEquals(2, ret.nIns());
+ assertEquals(startNode, ret.in(0));
+ assertTrue(ret.in(1) instanceof ConstantNode);
+ break;
+ case ConstantNode con:
+ assertEquals(1, con._value);
+ assertEquals(1, con.nIns());
+ assertEquals(startNode, con.in(0));
+ break;
+ default:
+ throw new IllegalStateException();
+ }
+ }
+ }
+
+ @Test
+ public void testZero() {
+ Parser parser = new Parser();
+ StartNode start = parser.parse("return 0;");
+ for( Node use : start._outputs )
+ if( use instanceof ConstantNode con )
+ assertEquals(0,con._value);
+ }
+
+ @Test
+ public void testBad1() {
+ try { new Parser().parse("ret"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected return: ret",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad2() {
+ try { new Parser().parse("return 0123;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error: integer values cannot start with '0'",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad3() {
+ try { new Parser().parse("return --12;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected integer literal: --",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad4() {
+ try { new Parser().parse("return 100"); }
+ catch( RuntimeException e ) { assertTrue(e.getMessage().contains("syntax error, expected ; got :")); }
+ }
+
+ // Negative numbers require unary operator support that is not in scope
+ @Test
+ public void testBad5() {
+ try { new Parser().parse("return -100;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected integer literal: -", e.getMessage()); }
+ }
+}