summaryrefslogtreecommitdiff
path: root/chapter10
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 /chapter10
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 'chapter10')
-rw-r--r--chapter10/README.md2
-rw-r--r--chapter10/docs/Makefile12
-rw-r--r--chapter10/docs/build-graphs.sh5
-rw-r--r--chapter10/src/main/java/com/seaofnodes/simple/Parser.java10
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter01Test.java54
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter02Test.java33
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter03Test.java4
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter04Test.java2
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter05Test.java14
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter06Test.java2
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter07Test.java5
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java1
-rw-r--r--chapter10/src/test/java/com/seaofnodes/simple/FuzzerWrap.java4
13 files changed, 77 insertions, 71 deletions
diff --git a/chapter10/README.md b/chapter10/README.md
index bd46a70e..62c4844b 100644
--- a/chapter10/README.md
+++ b/chapter10/README.md
@@ -1,4 +1,4 @@
-# Chapter 10
+# Chapter 10: Structs and Memory
In this chapter:
diff --git a/chapter10/docs/Makefile b/chapter10/docs/Makefile
new file mode 100644
index 00000000..12659b8f
--- /dev/null
+++ b/chapter10/docs/Makefile
@@ -0,0 +1,12 @@
+GV := $(wildcard *.gv)
+SVG := $(patsubst %.gv,%.svg,$(GV))
+
+.PHONY: all
+all: $(SVG)
+
+%.svg: %.gv
+ dot -Tsvg $< > $@
+
+.PHONY: clean
+clean:
+ @rm -f $(SVG)
diff --git a/chapter10/docs/build-graphs.sh b/chapter10/docs/build-graphs.sh
deleted file mode 100644
index 67dd302f..00000000
--- a/chapter10/docs/build-graphs.sh
+++ /dev/null
@@ -1,5 +0,0 @@
-dot -Tsvg example1.gv > example1.svg
-dot -Tsvg example2a.gv > example2a.svg
-dot -Tsvg example2b.gv > example2b.svg
-dot -Tsvg example2c.gv > example2c.svg
-dot -Tsvg lattice.gv > lattice.svg
diff --git a/chapter10/src/main/java/com/seaofnodes/simple/Parser.java b/chapter10/src/main/java/com/seaofnodes/simple/Parser.java
index def37bae..9a1b4465 100644
--- a/chapter10/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter10/src/main/java/com/seaofnodes/simple/Parser.java
@@ -785,6 +785,7 @@ public class Parser {
String getAnyNextToken() {
if (isEOF()) return "";
if (isIdStart(peek())) return parseId();
+ if (isNumber(peek())) return parseNumberString();
if (isPunctuation(peek())) return parsePunctuation();
return String.valueOf(peek());
}
@@ -793,13 +794,16 @@ public class Parser {
boolean isNumber(char ch) {return Character.isDigit(ch);}
private Type parseNumber() {
- int start = _position;
- while (isNumber(nextChar())) ;
- String snum = new String(_input, start, --_position - start);
+ String snum = parseNumberString();
if (snum.length() > 1 && snum.charAt(0) == '0')
throw error("Syntax error: integer values cannot start with '0'");
return TypeInteger.constant(Long.parseLong(snum));
}
+ private String parseNumberString() {
+ int start = _position;
+ while (isNumber(nextChar())) ;
+ return new String(_input, start, --_position - start);
+ }
// First letter of an identifier
private boolean isIdStart(char ch) {
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index 5abfc4cf..89882b2f 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
@@ -37,6 +35,15 @@ public class Chapter01Test {
assertEquals(TypeInteger.constant(0),con._type);
}
+ @Test
+ public void testBad1() {
+ try {
+ new Parser("ret").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'ret'",e.getMessage());
+ }
+ }
@Test
public void testBad2() {
@@ -47,4 +54,47 @@ public class Chapter01Test {
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("Undefined name '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/chapter10/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter02Test.java
index 107225ae..2deba460 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter02Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
@@ -65,35 +63,4 @@ public class Chapter02Test {
System.out.println(gv.generateDotOutput(parser));
}
- @Test
- public void testBad1() {
- try {
- new Parser("ret").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Undefined name 'ret'",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());
- }
-
}
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter03Test.java
index 6bcfcc4b..407fbe48 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter03Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
@@ -58,7 +56,7 @@ public class Chapter03Test {
}
@Test
- public void testBad6() {
+ public void testBad1() {
try {
new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
fail();
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 5a024e77..cde23324 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter05Test.java
index e82aa7da..44fff0c0 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter05Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter05Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
@@ -203,7 +201,7 @@ return a;""");
}
@Test
- public void testKeyword3() {
+ public void testKeyword3() {
try {
new Parser("int a=1; ififif(arg)inta=2;return a;").parse();
fail();
@@ -212,14 +210,4 @@ return a;""");
}
}
- @Test
- public void testBad7() {
- try {
- new Parser("return 1;}").parse();
- fail();
- } catch( RuntimeException e ) {
- assertEquals("Syntax error, unexpected }",e.getMessage());
- }
- }
-
}
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter06Test.java
index 6c24992c..d30bd43d 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter06Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter06Test.java
@@ -2,9 +2,7 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter07Test.java
index 29d16f32..a5cddb68 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter07Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter07Test.java
@@ -2,15 +2,12 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.TypeInteger;
-import org.junit.Assert;
import org.junit.Test;
-import org.junit.Ignore;
import static org.junit.Assert.*;
public class Chapter07Test {
-
@Test
public void testExample() {
Parser parser = new Parser(
@@ -47,7 +44,6 @@ return a;
@Test
public void testWhileNested() {
Parser parser = new Parser(
-
"""
int sum = 0;
int i = 0;
@@ -240,4 +236,5 @@ return a;
assertEquals("return Phi(Loop8,1,(Phi_a+3));", stop.toString());
assertTrue(stop.ret().ctrl() instanceof ProjNode);
}
+
}
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index f7388ff9..2e1ae1aa 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -3,7 +3,6 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.node.StopNode;
import com.seaofnodes.simple.type.*;
-import org.junit.Assert;
import org.junit.Test;
import java.util.Arrays;
diff --git a/chapter10/src/test/java/com/seaofnodes/simple/FuzzerWrap.java b/chapter10/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
index 699711fc..bb3bec1c 100644
--- a/chapter10/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
+++ b/chapter10/src/test/java/com/seaofnodes/simple/FuzzerWrap.java
@@ -10,12 +10,12 @@ import static org.junit.Assert.assertTrue;
/**
* To use the fuzzer run the <code>fuzzPeeps</code> methods.
* This can be done from IntelliJ Gui or remove the `@Ignore` annotation and start the test case from the command line
- * via <code>mvn clean test -Dtest=com.seaofnodes.simple.Fuzzer08Test#fuzzPeeps</code>
+ * via <code>mvn clean test -Dtest=com.seaofnodes.simple.FuzzerWrap#fuzzPeeps</code>
*/
public class FuzzerWrap {
- @Ignore
@Test
+ @Ignore
public void fuzzPeeps() {
var fuzzer = new Fuzzer();
for (int i=0; i<1000000; i++)