summaryrefslogtreecommitdiff
path: root/chapter26/src/test
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-25 10:24:58 -0700
committerCliff Click <cliffc@acm.org>2026-10-25 10:24:58 -0700
commit69e4a21f71aca4954469063387063434af35e679 (patch)
tree672e81600681625a22ed11f591b53d851f51c7d6 /chapter26/src/test
parent4051ce7cd26314f01dc8c0237899a742e0ab2084 (diff)
downloadSimple-69e4a21f71aca4954469063387063434af35e679.tar.gz
Simple-69e4a21f71aca4954469063387063434af35e679.zip
Backport a few missing guard checksmain
Diffstat (limited to 'chapter26/src/test')
-rw-r--r--chapter26/src/test/java/com/seaofnodes/simple/ConstructionTest.java17
-rw-r--r--chapter26/src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java13
2 files changed, 30 insertions, 0 deletions
diff --git a/chapter26/src/test/java/com/seaofnodes/simple/ConstructionTest.java b/chapter26/src/test/java/com/seaofnodes/simple/ConstructionTest.java
index dd20b746..a3317d09 100644
--- a/chapter26/src/test/java/com/seaofnodes/simple/ConstructionTest.java
+++ b/chapter26/src/test/java/com/seaofnodes/simple/ConstructionTest.java
@@ -7,6 +7,23 @@ import org.junit.Test;
import static org.junit.Assert.*;
public class ConstructionTest {
+ @Test public void testConditionalConstructorLoop() {
+ String[] bodies = {
+ "while(n--) if(n) x=1;",
+ "for(int i=0;i<n;i++) { if(i==1) continue; if(i==3) break; x+=i+1; }"
+ };
+ int[][] expected = {{0,0,1,1,1,1}, {0,1,1,4,4,4}};
+ for( int i=0; i<bodies.length; i++ )
+ for( long seed : new long[]{123,456,789} ) {
+ String src = "struct C { int x; new C={int n -> "+bodies[i]+"}; }; return new C(arg).x;";
+ var code = new CodeGen(src,com.seaofnodes.simple.type.TypeInteger.BOT,seed,true)
+ .driver(CodeGen.Phase.TypeCheck);
+ for( int arg=0; arg<expected[i].length; arg++ )
+ assertEquals("case="+i+" seed="+seed+" arg="+arg,
+ Integer.toString(expected[i][arg]),Eval2.eval(code,arg));
+ }
+ }
+
private static final String CONSTRUCT = """
struct Box {
int x=7;
diff --git a/chapter26/src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java b/chapter26/src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java
index f2f7d882..419cac51 100644
--- a/chapter26/src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java
+++ b/chapter26/src/test/java/com/seaofnodes/simple/IncompleteTypesTest.java
@@ -6,6 +6,19 @@ import com.seaofnodes.simple.type.TypeInteger;
import static org.junit.Assert.assertEquals;
public class IncompleteTypesTest {
+ @Test public void testLoadFromUnsettledLoopPhi() {
+ String src = """
+ struct C { int x; };
+ val f={int n -> return new C { while(n--) if(n) x=1; }; };
+ return f(arg).x;
+ """;
+ for( long seed : new long[]{123,456,789} ) {
+ var code = new CodeGen(src,TypeInteger.BOT,seed,true).driver(CodeGen.Phase.TypeCheck);
+ for( int arg=0; arg<6; arg++ )
+ assertEquals("seed="+seed+" arg="+arg,arg<2 ? "0" : "1",Eval2.eval(code,arg));
+ }
+ }
+
@Test public void testForwardFieldAndNumericMode() {
// At the addition, neither the field's type nor its offset is known.
String src = """