summaryrefslogtreecommitdiff
path: root/chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-01 10:33:55 -0800
committerCliff Click <cliffc@acm.org>2026-11-01 10:33:55 -0800
commitf4bd723d929e53475f7990374dca7b7dfcbbdc42 (patch)
tree7bb080119e7b88ad4ac9b30af019fe6a5e4ba9dd /chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java
parent0ac0c9e10287972ac3af5a4ee43d26f0d5b318fe (diff)
downloadSimple-main.tar.gz
Simple-main.zip
Add Chapter 29 for tech debt cleanup with the parser splitmain
Keep Chapter 28b intact and carry the grammar-oriented parser package, Var move, IterPeeps move, and parser regression tests into Chapter 29. Defer privacy and exclude its experimental tests and fixture changes. Register the chapter in the indexes, IntelliJ modules and run configurations, Maven reactor, roadmap, and linear-history publisher. Windows and Linux each pass 573 tests, release builds, 199 allocation replays, and 29 native checks.
Diffstat (limited to 'chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java')
-rw-r--r--chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java222
1 files changed, 222 insertions, 0 deletions
diff --git a/chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java
new file mode 100644
index 00000000..7094c0d5
--- /dev/null
+++ b/chapter29/src/test/java/com/seaofnodes/simple/Chapter07Test.java
@@ -0,0 +1,222 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.node.*;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter07Test {
+
+ @Test
+ public void testExample() {
+ CodeGen code = new CodeGen(
+"""
+while(arg < 10) {
+ arg = arg + 1;
+}
+return arg;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testRegression() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+if(arg){}else{
+ while(a < 10) {
+ a = a + 1;
+ }
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Region,1,Phi(Loop,1,(Phi_a+1)));", code.print());
+ }
+
+ @Test
+ public void testWhileNested() {
+ CodeGen code = new CodeGen(
+"""
+int sum = 0;
+int i = 0;
+while(i < arg) {
+ i = i + 1;
+ int j = 0;
+ while( j < arg ) {
+ sum = sum + j;
+ j = j + 1;
+ }
+}
+return sum;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,0,Phi(Loop,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", code.print());
+ }
+
+ @Test
+ public void testWhileScope() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+int b = 2;
+while(a < 10) {
+ if (a == 2) a = 3;
+ else b = 4;
+}
+return b;
+""");
+ code.parse();
+ assertEquals("return Phi(Loop,2,Phi(Region,Phi_b,4));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhileNestedIfAndInc() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+int b = 2;
+while(a < 10) {
+ if (a == 2) a = 3;
+ else b = 4;
+ b = b + 1;
+ a = a + 1;
+}
+return b;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,2,(Phi(Region,Phi_b,4)+1));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+
+ @Test
+ public void testWhile() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(a < 10) {
+ a = a + 1;
+ a = a + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhilePeep() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(a < 10) {
+ a = a + 1;
+ a = a + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile2() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(arg) a = 2;
+return a;
+""");
+ code.parse();
+ assertEquals("return Phi(Loop,1,2);", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile2Peep() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(arg) a = 2;
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,2);", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile3() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(a < 10) {
+ int b = a + 1;
+ a = b + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile3Peep() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+while(a < 10) {
+ int b = a + 1;
+ a = b + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile4() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+int b = 2;
+while(a < 10) {
+ int b = a + 1;
+ a = b + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+ @Test
+ public void testWhile4Peep() {
+ CodeGen code = new CodeGen(
+"""
+int a = 1;
+int b = 2;
+while(a < 10) {
+ int b = a + 1;
+ a = b + 2;
+}
+return a;
+""");
+ code.driver(CodeGen.Phase.FinalizeIR);
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code.print());
+ assertTrue(code.ctrl() instanceof CProjNode);
+ }
+
+}