summaryrefslogtreecommitdiff
path: root/chapter33/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'chapter33/src/test/java/com')
-rw-r--r--chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java44
1 files changed, 44 insertions, 0 deletions
diff --git a/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
new file mode 100644
index 00000000..99dc4452
--- /dev/null
+++ b/chapter33/src/test/java/com/seaofnodes/simple/Chapter33Test.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.util.WorkList;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/** Regressions for the independent compiler fixes introduced in this chapter. */
+public class Chapter33Test {
+ @Test public void testWorkListRequeueAndTraversal() {
+ CodeGen code = new CodeGen("return arg;").parse();
+ Node arg = code.expr();
+ WorkList<Node> work = new WorkList<>();
+ work.push(null);
+ work.push(arg);
+ work.push(arg);
+ assertSame(arg,work.pop());
+ assertNull(work.pop());
+ work.push(arg);
+ assertSame(arg,work.popOnce());
+ work.push(arg);
+ assertNull("A traversal must not revisit a cycle",work.popOnce());
+ work.clear();
+ work.push(arg);
+ assertSame(arg,work.popOnce());
+ }
+
+ @Test public void testPeepholeMayReturnDeadSelf() {
+ CodeGen code = new CodeGen("return arg;").parse();
+ Node replacement = code.expr();
+ // A rewrite may replace its own uses and thereby kill itself before
+ // returning self to report progress to the iterative peephole driver.
+ Node rewritten = new AddNode(replacement,code.con(1)) {
+ @Override public Node idealize() {
+ subsume(in(1));
+ return this;
+ }
+ }.init();
+ code.driver(CodeGen.Phase.TypeCheck);
+ assertTrue(rewritten.isDead());
+ assertEquals("42",Eval2.eval(code,42));
+ }
+}