summaryrefslogtreecommitdiff
path: root/chapter16/src/test/java/com/seaofnodes
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-10-14 10:31:40 -0700
committerCliff Click <cliffc@acm.org>2024-10-21 08:52:01 -0700
commit94e557c9de68e7b3f5595d75e7330b63df76c323 (patch)
treef16fca7e4b49f82f98f73c073b7495abedafd195 /chapter16/src/test/java/com/seaofnodes
parent475f66adc7c1c9f1cd479c4492999eae4c060c5b (diff)
downloadSimple-94e557c9de68e7b3f5595d75e7330b63df76c323.tar.gz
Simple-94e557c9de68e7b3f5595d75e7330b63df76c323.zip
final fields and multi defs
`int x,y;` works `int !x = 3;` works Structs defs are statements, require a trailing `;`
Diffstat (limited to 'chapter16/src/test/java/com/seaofnodes')
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java54
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java26
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java26
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java68
-rw-r--r--chapter16/src/test/java/com/seaofnodes/simple/TypeTest.java8
5 files changed, 121 insertions, 61 deletions
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index def9aaa6..ec6a0bb4 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -38,10 +38,10 @@ return p-r;
struct Bar {
int a;
int b;
-}
+};
struct Foo {
int x;
-}
+};
Foo? foo = null;
Bar bar = new Bar;
bar.a = 1;
@@ -55,7 +55,7 @@ return bar.a;
@Test
public void testExample() {
Parser parser = new Parser("""
-struct Vector2D { int x; int y; }
+struct Vector2D { int x; int y; };
Vector2D v = new Vector2D;
v.x = 1;
if (arg)
@@ -73,7 +73,7 @@ return v;
Parser parser = new Parser("""
struct s0 {
int v0;
-}
+};
s0? v1=null;
int v3=v1.zAicm;
""");
@@ -84,7 +84,7 @@ int v3=v1.zAicm;
@Test
public void testBug2() {
Parser parser = new Parser("""
-struct s0 { int v0; }
+struct s0 { int v0; };
arg=0+new s0.0;
""");
try { parser.parse(); fail(); }
@@ -94,7 +94,7 @@ arg=0+new s0.0;
@Test
public void testLoop() {
Parser parser = new Parser("""
-struct Bar { int a; }
+struct Bar { int a; };
Bar bar = new Bar;
while (arg) {
bar.a = bar.a + 2;
@@ -103,13 +103,13 @@ while (arg) {
return bar.a;
""");
StopNode stop = parser.parse(false).iterate();
- assertEquals("return Phi(Loop13,0,(Phi_a+2));", stop.toString());
+ assertEquals("return Phi(Loop14,0,(Phi_a+2));", stop.toString());
}
@Test
public void testIf() {
Parser parser = new Parser("""
-struct Bar { int a; }
+struct Bar { int a; };
Bar bar = new Bar;
if (arg) bar = null;
bar.a = 1;
@@ -122,7 +122,7 @@ return bar.a;
@Test
public void testIf2() {
Parser parser = new Parser("""
-struct Bar { int a; }
+struct Bar { int a; };
Bar? bar = null;
if (arg) bar = new Bar;
bar.a = 1;
@@ -135,7 +135,7 @@ return bar.a;
@Test
public void testIf3() {
Parser parser = new Parser("""
-struct Bar { int a; }
+struct Bar { int a; };
Bar bar = null;
if (arg) bar = null;
bar.a = 1;
@@ -148,21 +148,21 @@ return bar.a;
@Test
public void testIfOrNull() {
Parser parser = new Parser("""
-struct Bar { int a; }
+struct Bar { int a; };
Bar? bar = new Bar;
if (arg) bar = null;
if( bar ) bar.a = 1;
return bar;
""");
StopNode stop = parser.parse(false).iterate();
- assertEquals("return Phi(Region33,(*void)Phi(Region20,null,Bar),null);", stop.toString());
+ assertEquals("return Phi(Region34,(*void)Phi(Region21,null,Bar),null);", stop.toString());
}
@Test
public void testIfOrNull2() {
Parser parser = new Parser(
"""
-struct Bar { int a; }
+struct Bar { int a; };
Bar? bar = new Bar;
if (arg) bar = null;
int rez = 3;
@@ -171,13 +171,13 @@ else bar.a = 1;
return rez;
""");
StopNode stop = parser.parse(false).iterate();
- assertEquals("return Phi(Region39,4,3);", stop.toString());
+ assertEquals("return Phi(Region40,4,3);", stop.toString());
}
@Test
public void testWhileWithNullInside() {
Parser parser = new Parser("""
-struct s0 {int v0;}
+struct s0 {int v0;};
s0? v0 = new s0;
int ret = 0;
while(arg) {
@@ -197,7 +197,7 @@ return ret;
Parser parser = new Parser("""
struct s0 {
int v0;
-}
+};
s0? v1=new s0;
s0? v1;
v1=new s0;
@@ -214,7 +214,7 @@ v1=new s0;
struct Iter {
int x;
int len;
-}
+};
Iter i = new Iter;
i.len = arg;
int sum=0;
@@ -225,7 +225,7 @@ while( i.x < i.len ) {
return sum;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop18,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString());
+ assertEquals("return Phi(Loop20,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString());
}
@@ -234,7 +234,7 @@ return sum;
@Test
public void test1() {
Parser parser = new Parser("""
-struct s0 {int v0;}
+struct s0 {int v0;};
s0 ret = new s0;
while(arg) {
s0 v0 = new s0;
@@ -246,13 +246,13 @@ while(arg) {
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop13,s0,Phi(Region37,s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop14,s0,Phi(Region38,s0,Phi_ret));", stop.toString());
}
@Test
public void test2() {
Parser parser = new Parser("""
-struct s0 {int v0;}
+struct s0 {int v0;};
s0 ret = new s0;
s0 v0 = new s0;
while(arg) {
@@ -264,14 +264,14 @@ while(arg) {
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop17,s0,Phi(Region38,s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop18,s0,Phi(Region39,s0,Phi_ret));", stop.toString());
}
@Test
public void test3() {
Parser parser = new Parser("""
-struct s0 {int v0;}
+struct s0 {int v0;};
s0 ret = new s0;
while(arg < 10) {
s0 v0 = new s0;
@@ -281,7 +281,7 @@ while(arg < 10) {
return ret;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop13,s0,Phi(Region35,s0,Phi_ret));", stop.toString());
+ assertEquals("return Phi(Loop14,s0,Phi(Region36,s0,Phi_ret));", stop.toString());
}
@Test
@@ -289,7 +289,7 @@ return ret;
Parser parser = new Parser("""
struct s0 {
int f0;
-}
+};
if(0>=0) return new s0;
return new s0;
int v0=null.f0;
@@ -319,7 +319,7 @@ if(0) {
Parser parser = new Parser("""
struct s0 {
int f0;
-}
+};
if(0) return 0;
else return new s0;
if(new s0.f0) return 0;
@@ -346,7 +346,7 @@ return 0;
@Test
public void testBug7() {
Parser parser = new Parser("""
-struct s0 { int f0; }
+struct s0 { int f0; };
s0 v0 = new s0;
while(v0.f0) {}
s0 v1 = v0;
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index 5f9f9cac..4998d78f 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -337,7 +337,7 @@ return primeCount;
public void testAntiDeps1() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v=new S;
v.f = 2;
int i=new S.f;
@@ -355,7 +355,7 @@ return i;
public void testAntiDeps2() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
v.f = arg;
S t = new S;
@@ -369,14 +369,14 @@ if (arg) {
return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region40,.f,0);", stop.toString());
+ assertEquals("return Phi(Region41,.f,0);", stop.toString());
}
@Test
public void testAntiDeps3() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v0 = new S;
S? v1;
if (arg) v1 = new S;
@@ -396,7 +396,7 @@ return v0;
public void testAntiDeps4() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
v.f = arg;
S t = new S;
@@ -413,7 +413,7 @@ return i;
public void testAntiDeps5() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
while(1) {
while(arg+1) { arg=arg-1; }
@@ -430,7 +430,7 @@ return v;
public void testAntiDeps6() {
Parser parser = new Parser(
"""
-struct s { int v; }
+struct s { int v; };
s ptr=new s;
while( -arg )
ptr = new s;
@@ -445,7 +445,7 @@ while(1)
public void testAntiDeps7() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
S t = new S;
int i = v.f;
@@ -463,7 +463,7 @@ return i;
public void testAntiDeps8() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
S t = new S;
while(arg) {
@@ -476,14 +476,14 @@ while(arg) {
return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop17,arg,Phi(Region42,.f,0));", stop.toString());
+ assertEquals("return Phi(Loop18,arg,Phi(Region43,.f,0));", stop.toString());
}
@Test
public void testAntiDeps9() {
Parser parser = new Parser(
"""
-struct S { int f; }
+struct S { int f; };
S v = new S;
S t = new S;
if (arg) {
@@ -500,8 +500,8 @@ return v;
@Test
public void testExample2() {
Parser parser = new Parser(
- """
-struct S { int f; }
+"""
+struct S { int f; };
S v = new S;
int i = arg;
while (arg > 0) {
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index 416aba31..a95a6155 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -23,7 +23,7 @@ return 3.14;
public void testLinkedList0() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; }
+struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
LLI x = new LLI;
@@ -42,7 +42,7 @@ return head.next.i;
public void testLinkedList1() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; }
+struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
LLI x = new LLI;
@@ -65,8 +65,8 @@ return next.i;
public void testCoRecur() {
Parser parser = new Parser(
"""
-struct int0 { int i; flt0? f; }
-struct flt0 { flt f; int0? i; }
+struct int0 { int i; flt0? f; };
+struct flt0 { flt f; int0? i; };
int0 i0 = new int0;
i0.i = 17;
flt0 f0 = new flt0;
@@ -83,7 +83,7 @@ return f0.i.f.i.i;
public void testNullRef0() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
+struct N { N? next; int i; };
N n = new N;
return n.next;
""");
@@ -95,7 +95,7 @@ return n.next;
public void testNullRef1() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
+struct N { N next; int i; };
N n = new N;
n.next = new N;
return n.next;
@@ -108,7 +108,7 @@ return n.next;
public void testNullRef2() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
+struct N { N next; int i; };
N n = new N;
n.next = null;
return n.next;
@@ -121,7 +121,7 @@ return n.next;
public void testNullRef3() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
+struct N { N next; int i; };
N n = new N;
n.i = 3.14;
return n.i;
@@ -145,7 +145,7 @@ struct S{};
public void testForwardRef0() {
Parser parser = new Parser(
"""
-struct S1 { S2 s; }
+struct S1 { S2 s; };
return new S2;
""");
try { parser.parse().iterate(); fail(); }
@@ -156,8 +156,8 @@ return new S2;
public void testForwardRef1() {
Parser parser = new Parser(
"""
-struct S1 { S2? s; }
-struct S2 { int x; }
+struct S1 { S2? s; };
+struct S2 { int x; };
return new S1.s=new S2;
""");
StopNode stop = parser.parse().iterate();
@@ -168,8 +168,8 @@ return new S1.s=new S2;
public void testcheckNull() {
Parser parser = new Parser(
"""
-struct I {int i;}
-struct P {I pi;}
+struct I {int i;};
+struct P {I pi;};
P p1 = new P;
P p2 = new P;
p2.pi = new I;
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index d49d7ede..7adc5b10 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -10,12 +10,23 @@ import java.nio.charset.StandardCharsets;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+/**
+ struct S {
+ int x; // default init of 0
+ S? x; // default init of null
+ int !x; // final init required in constructor
+ S x; // init required in constructor
+ int !x=17; // final init done now, not in constructor
+ S x = ...;// init done now, not in constructor
+ };
+
+ */
+
public class Chapter16Test {
@Test
public void testJig() {
- Parser parser = new Parser(
-"""
+ Parser parser = new Parser("""
return 3.14;
""");
StopNode stop = parser.parse().iterate();
@@ -24,11 +35,60 @@ return 3.14;
}
@Test
- public void testBasicFinal() {
+ public void testMulti0() {
Parser parser = new Parser(
"""
+int x, y;
+return x+y;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 0;", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 0));
+ }
+ @Test
+ public void testMulti1() {
+ Parser parser = new Parser(
+"""
+int x=2, y=x+1;
+return x+y;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 5;", stop.toString());
+ assertEquals(5L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testFinal0() {
+ Parser parser = new Parser(
+"""
+int !x=2;
+x=3;
+return x;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
+ }
+
+ @Test
+ public void testFinal1() {
+ Parser parser = new Parser(
+"""
+int !x=2, y=3;
+if( arg ) { int x = y; x = x*x; y=x; } // Shadow final x
+return y;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region18,9,3);", stop.toString());
+ assertEquals(3L, Evaluator.evaluate(stop, 0));
+ assertEquals(9L, Evaluator.evaluate(stop, 1));
+ }
+
+
+ @Test
+ public void testStructFinal() {
+ Parser parser = new Parser("""
struct Point { int !x, !y; }
-Point p = new P { x=3; y=4; }
+Point p = new Point { x=3; y=4; }
return p;
""");
StopNode stop = parser.parse().iterate();
diff --git a/chapter16/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter16/src/test/java/com/seaofnodes/simple/TypeTest.java
index cb45a0d7..6f7409cd 100644
--- a/chapter16/src/test/java/com/seaofnodes/simple/TypeTest.java
+++ b/chapter16/src/test/java/com/seaofnodes/simple/TypeTest.java
@@ -13,11 +13,11 @@ public class TypeTest {
@Test
public void testTypeAdHoc() {
TypeStruct s1 = TypeStruct.make("s1", new Field[]{
- Field.make("a",-1, TypeInteger.BOT),
- Field.make("b",-2, TypeInteger.BOT) });
+ Field.make("a", TypeInteger.BOT,-1, false),
+ Field.make("b", TypeInteger.BOT,-2, false) });
TypeStruct s2 = TypeStruct.make("s2", new Field[]{
- Field.make("a",-3, TypeInteger.BOT),
- Field.make("b",-4, TypeInteger.BOT) });
+ Field.make("a", TypeInteger.BOT,-3, false),
+ Field.make("b", TypeInteger.BOT,-4, false) });
Assert.assertEquals(s1, s1.glb());
Assert.assertNotEquals(s1, s1.dual());
Assert.assertEquals(s1, s1.dual().glb());