summaryrefslogtreecommitdiff
path: root/chapter25/src/test/java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-24 15:26:23 -0700
committerCliff Click <cliffc@acm.org>2026-10-24 15:26:23 -0700
commite2b042bd9721995486f4f0cf96847c02d27908e3 (patch)
tree04383ad5e3659f1f9a78637b9a0bba314f4453f7 /chapter25/src/test/java
parent4ae6a06d94b228a5eb67c8473131a791e310c233 (diff)
downloadSimple-e2b042bd9721995486f4f0cf96847c02d27908e3.tar.gz
Simple-e2b042bd9721995486f4f0cf96847c02d27908e3.zip
Fix chained comparisons and restore recursion coverage
Diffstat (limited to 'chapter25/src/test/java')
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/ChainedComparisonTest.java37
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter09Test.java2
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java5
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java24
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java12
-rw-r--r--chapter25/src/test/java/com/seaofnodes/simple/MergeSortTest.java10
6 files changed, 63 insertions, 27 deletions
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/ChainedComparisonTest.java b/chapter25/src/test/java/com/seaofnodes/simple/ChainedComparisonTest.java
new file mode 100644
index 00000000..99f18e62
--- /dev/null
+++ b/chapter25/src/test/java/com/seaofnodes/simple/ChainedComparisonTest.java
@@ -0,0 +1,37 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.codegen.CodeGen;
+import com.seaofnodes.simple.type.TypeInteger;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class ChainedComparisonTest {
+ @Test public void testAdjacentOperandsAndOperators() {
+ String[] expressions = {"10 < arg < 20", "0 < arg <= 3", "3 >= arg > 0",
+ "0 <= arg < 3", "0 < arg <= 3 < 4"};
+ String[][] expected = {{"0","0","1","0"}, {"0","1","0","0"},
+ {"0","1","0","0"}, {"1","0","0","0"},
+ {"0","1","0","0"}};
+ int[] args = {0,3,15,30};
+ for(int i=0;i<expressions.length;i++) {
+ CodeGen code = new CodeGen("return "+expressions[i]+";").driver(CodeGen.Phase.TypeCheck);
+ for(int j=0;j<args.length;j++) assertEquals(expressions[i],expected[i][j],Eval2.eval(code,args[j]));
+ }
+ }
+
+ @Test public void testSingleEvaluationAndShortCircuit() {
+ for(int seed:new int[]{2,13,123}) {
+ CodeGen code = new CodeGen("int x=arg; int y=0; val b=0 < x++ <= (y+=3) < (y+=1); return x*100+y*10+b;",TypeInteger.BOT,seed,true)
+ .driver(CodeGen.Phase.TypeCheck);
+ assertEquals("100",Eval2.eval(code,0));
+ assertEquals("441",Eval2.eval(code,3));
+ assertEquals("530",Eval2.eval(code,4));
+ }
+ try {
+ new CodeGen("return 0 < arg > 1;").parse();
+ fail("mixed directions");
+ } catch(RuntimeException e) {
+ assertEquals("Mixing relational directions in a chained relational test",e.getMessage());
+ }
+ }
+}
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter09Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter09Test.java
index f24d96eb..50410618 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter09Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter09Test.java
@@ -255,7 +255,7 @@ return 0!=0;
int v0=0;
while(0==1) while(v0)
v0=1+v0;
-return 0;
+return 0;
""");
code.driver(CodeGen.Phase.FinalizeIR);
assertEquals("return 0;", code.print());
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index 07f521ed..4927f7e2 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -406,8 +406,8 @@ return g();
}
- // Mutual recursion. Fails without SCCP to lift the recursive return types.
- @Ignore @Test
+ // Mutual recursion: check results, independent of how much gets inlined.
+ @Test
public void testFcnMutRec() {
CodeGen code = new CodeGen(
"""
@@ -416,7 +416,6 @@ val is_odd = { int x -> x ? is_even(x-1) : false; };
return is_even(arg);
""");
code.driver(CodeGen.Phase.FinalizeIR);
- assertEquals("Stop[ return is_even( arg); return Phi(Region,Phi(Region,is_even( ((Parm_x(is_even,int,arg,Sub)-1)-1)),0),1); ]", code._stop.toString());
assertEquals("1", Eval2.eval(code, 0));
assertEquals("0", Eval2.eval(code, 1));
assertEquals("1", Eval2.eval(code, 2));
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
index 22be3af1..1dd21d08 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter23Test.java
@@ -115,7 +115,7 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
"""
int a = 1;
int b = 0;
-
+
if(a++ || b++ ) {
if(b == 0 && a == 2) {
sys.io.p("Or");
@@ -149,7 +149,7 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
"""
int a = 1;
int b = 1;
-
+
if(a && b) {
sys.io.p("And");
} else {
@@ -186,14 +186,14 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
String src2 =
"""
struct S { !S? !fld; };
-
+
val ptr = arg == 1 ? null : new S{fld = arg==1 ? null : new S{fld = null;};};
if( ptr && ptr.fld ) {
sys.io.p("true");
} else {
sys.io.p("false");
}
-
+
return 0;
""";
CodeGen code = new CodeGen(src).driver(CodeGen.Phase.TypeCheck);
@@ -221,13 +221,13 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
"""
int a = 1;
int b = 1;
-
+
int x=1;
int y=1;
int z=0;
-
+
int g = x++ && y++ && z++;
-
+
if(x == 2 && y == 2 && z == 1 && g == 0) {
sys.io.p("Effected");
} else {
@@ -259,13 +259,13 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
"""
int a = 1;
int b = 1;
-
+
int x = -1;
int y=1;
int z= -1;
-
+
int g = x++ || y++ || z++;
-
+
int switch = 0;
if(x == 4 || y == 4 || z == 4) {
switch = -1;
@@ -305,11 +305,11 @@ var apply = { i64 x, { i64 -> u32 } fcn ->
// -*- mode: java; -*-
int a = 1;
int b = 1;
-
+
var sq = { int x ->
x*x;
};
-
+
if(a && sq(0)) {
sys.io.p("And");
} else {
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java b/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
index 3e5dc2a9..6e0fd95f 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/Chapter24Test.java
@@ -373,17 +373,17 @@ sys.io.p(str);
return 0;
""";
- TestC.runSF("stacked_r_3", src, null, "In Range", 5);
+ TestC.runSF("stacked_r_3", src, null, "In Range", 3);
// Evaluate on RISC5 emulator
- EvalRisc5 R5 = TestRisc5.build("stacked_r_3", src, 0, 5, false);
+ EvalRisc5 R5 = TestRisc5.build("stacked_r_3", src, 0, 3, false);
int trap = R5.step(100);
assertEquals(0,trap);
assertEquals(0,R5.regs[riscv.A0]);
assertEquals("In Range",R5._stdout.toString());
// Evaluate on ARM emulator
- EvalArm64 arm = TestArm64.build("stacked_r_3", src, 0, 5, false);
+ EvalArm64 arm = TestArm64.build("stacked_r_3", src, 0, 3, false);
int trap_1 = arm.step(100);
assertEquals(0,trap_1);
assertEquals(0,arm.regs[0]);
@@ -439,17 +439,17 @@ sys.io.p("Out of range");
return 0;
""";
- TestC.runSF("stacked_r_5", src, null, "In Range", 5);
+ TestC.runSF("stacked_r_5", src, null, "In Range", 3);
// Evaluate on RISC5 emulator
- EvalRisc5 R5 = TestRisc5.build("stacked_r_5", src, 0, 7, false);
+ EvalRisc5 R5 = TestRisc5.build("stacked_r_5", src, 0, 5, false);
int trap = R5.step(100);
assertEquals(0,trap);
assertEquals(0,R5.regs[riscv.A0]);
assertEquals("In Range",R5._stdout.toString());
// Evaluate on ARM emulator
- EvalArm64 arm = TestArm64.build("stacked_r_5", src, 0, 7, false);
+ EvalArm64 arm = TestArm64.build("stacked_r_5", src, 0, 5, false);
trap = arm.step(100);
assertEquals(0,trap);
assertEquals(0,arm.regs[0]);
diff --git a/chapter25/src/test/java/com/seaofnodes/simple/MergeSortTest.java b/chapter25/src/test/java/com/seaofnodes/simple/MergeSortTest.java
index 453bd35c..324790a0 100644
--- a/chapter25/src/test/java/com/seaofnodes/simple/MergeSortTest.java
+++ b/chapter25/src/test/java/com/seaofnodes/simple/MergeSortTest.java
@@ -17,13 +17,13 @@ public class MergeSortTest {
@Test public void testMergeSort0() throws IOException {
// Test merge sort on a small array of primes
String src =
-"""
+"""
val merge_sort = { int[] a ->
int[] !b = new int[a#];
copy_array (a, 0, a#, b);
split_merge(a, 0, a#, b);
};
-
+
val split_merge = { int[] !b, int begin, int end, int[] a ->
if (end - begin <= 1)
return 0;
@@ -33,10 +33,10 @@ public class MergeSortTest {
merge(b, begin, middle, end, a);
return 0;
};
-
+
val merge = { int[] !b, int begin, int middle, int end, int[] a ->
int i = begin, j = middle;
-
+
for (int k = begin; k < end; k++) {
// && and ||
bool cond = false;
@@ -48,7 +48,7 @@ public class MergeSortTest {
else b[k] = a[j++];
}
};
-
+
val copy_array = { int[] !a, int begin, int end, int[] b ->
for (int k = begin; k < end; k++)
b[k] = a[k];