summaryrefslogtreecommitdiff
path: root/chapter24/src/test/java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-19 22:31:42 -0700
committerCliff Click <cliffc@acm.org>2026-09-19 22:31:42 -0700
commit6e59fc1b71f2098c8baf90a6c54e5a33b7291e94 (patch)
treecb097fb6fdb0f7941ac7e784f56e11750ab14c53 /chapter24/src/test/java
parent45bd2e5fae4cbbee0ddd07094755c2f995178f28 (diff)
downloadSimple-6e59fc1b71f2098c8baf90a6c54e5a33b7291e94.tar.gz
Simple-6e59fc1b71f2098c8baf90a6c54e5a33b7291e94.zip
Unify mixed-return checking and results
Unify dominator caching.
Diffstat (limited to 'chapter24/src/test/java')
-rw-r--r--chapter24/src/test/java/com/seaofnodes/simple/Chapter10Test.java13
-rw-r--r--chapter24/src/test/java/com/seaofnodes/simple/Chapter12Test.java14
-rw-r--r--chapter24/src/test/java/com/seaofnodes/simple/Chapter18Test.java16
-rw-r--r--chapter24/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java11
4 files changed, 52 insertions, 2 deletions
diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index c1b5e3a8..d7a74f18 100644
--- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -6,6 +6,19 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class Chapter10Test {
+ @Test public void testDeadReferenceReturn() {
+ for( String body : new String[] {
+ "return new S; return 0;",
+ "if(1) return new S; else return 0;"
+ } ) {
+ String src = "struct S { int x; }; " + body;
+ var code = new CodeGen(src).parse().opto().typeCheck();
+ org.junit.Assert.assertTrue(body,
+ code.expr()._type instanceof com.seaofnodes.simple.type.TypeMemPtr);
+ }
+ }
+
+
// Issue #246: null-check guards start in Chapter 10; arrays arrive in Chapter 15.
private static final String NULLABLE_POINT_SOURCE = """
diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 8069ddaa..21759b78 100644
--- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -7,6 +7,20 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class Chapter12Test {
+ @Test public void testDeadNumericReturns() {
+ for( String[] test : new String[][] {
+ {"if(1) return 7; else return 2.5;", "7"},
+ {"if(0) return 7; else return 2.5;", "2.5"},
+ {"return 7; return 2.5;", "7"},
+ {"return 2.5; return 7;", "2.5"}
+ } ) {
+ String src = test[0];
+ var code = new CodeGen(src).parse().opto().typeCheck();
+ assertEquals(src,test[1],Eval2.eval(code,0));
+ }
+ }
+
+
@Test
public void testJig() {
CodeGen code = new CodeGen(
diff --git a/chapter24/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter24/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index 764356cf..5dbbf0f1 100644
--- a/chapter24/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter24/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -11,6 +11,22 @@ import static org.junit.Assert.*;
import static org.junit.Assert.fail;
public class Chapter18Test {
+ @Test public void testReachableMixedReturns() {
+ for( String[] test : new String[][] {
+ {"if(arg) return 7; else return 2.5;", "No common type amongst int and f64"},
+ {"struct S { int x; }; if(arg) return new S; else return 0;",
+ "No common type amongst int and reference"}
+ } ) {
+ try {
+ new CodeGen(test[0]).parse().opto().typeCheck();
+ fail("Expected incompatible return types: " + test[0]);
+ } catch( Parser.ParseException e ) {
+ assertEquals(test[1],e.getMessage());
+ }
+ }
+ }
+
+
@Test public void testPrintingForwardReferenceScope() throws Exception {
new CodeGen("return 0;").parse();
var declared = TypeMemPtr.make(TypeStruct.open("PrintForward"));
diff --git a/chapter24/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java b/chapter24/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java
index 01d471bf..ceea1de5 100644
--- a/chapter24/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java
+++ b/chapter24/src/test/java/com/seaofnodes/simple/node/PrintTestSupport.java
@@ -21,17 +21,24 @@ public class PrintTestSupport {
var ins = new IdentityHashMap<Node,Node[]>();
var outs = new IdentityHashMap<Node,Node[]>();
var hashes = new IdentityHashMap<Node,Integer>();
+ var versions = new IdentityHashMap<Node,Integer>();
var depths = new IdentityHashMap<Node,Integer>();
for( Node n : nodes ) {
ins.put(n,n._inputs.toArray(new Node[0]));
outs.put(n,n._outputs.toArray(new Node[0]));
hashes.put(n,hash.getInt(n));
- if( n instanceof CFGNode cfg ) depths.put(n,cfg._idepth);
+ if( n instanceof CFGNode cfg ) {
+ depths.put(n,(int)cfg._idepth);
+ versions.put(n,(int)cfg._idepthVersion);
+ }
}
print.run();
for( Node n : nodes ) {
assertEquals("hash of node "+n._nid,(int)hashes.get(n),hash.getInt(n));
- if( n instanceof CFGNode cfg ) assertEquals("depth of node "+n._nid,(int)depths.get(n),cfg._idepth);
+ if( n instanceof CFGNode cfg ) {
+ assertEquals("depth of node "+n._nid,(int)depths.get(n),cfg._idepth);
+ assertEquals("depth version of node "+n._nid,(int)versions.get(n),cfg._idepthVersion);
+ }
assertEquals(ins.get(n).length,n.nIns());
assertEquals(outs.get(n).length,n.nOuts());
for( int i=0; i<n.nIns(); i++ ) assertSame(ins.get(n)[i],n.in(i));