summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter17aTest.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-08-10 11:11:18 -0700
committerCliff Click <cliffc@acm.org>2026-10-24 15:56:44 -0700
commit741d78b6e840c88b7b183010c812ad982b342da1 (patch)
tree84a75eb7ad351bffd20cd5fb152bab032b74271c /src/test/java/com/seaofnodes/simple/Chapter17aTest.java
parent8d90d2292bbdb23596849c3c3d1cda2f6cad783e (diff)
downloadSimple-linear-chapter28a.tar.gz
Simple-linear-chapter28a.zip
Chapter 28a: Modules and Lazy Class Initializationlinear-chapter28a
Co-authored-by: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: fuhsnn <66062782+fuhsnn@users.noreply.github.com> Co-authored-by: Symious <14933944+symious@users.noreply.github.com> Co-authored-by: Jun Kawasaki <jun784@gmail.com> Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Diffstat (limited to 'src/test/java/com/seaofnodes/simple/Chapter17aTest.java')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter17aTest.java45
1 files changed, 24 insertions, 21 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter17aTest.java b/src/test/java/com/seaofnodes/simple/Chapter17aTest.java
index 64ecb235..ea7afe56 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter17aTest.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter17aTest.java
@@ -24,8 +24,8 @@ return y;
@Test
public void testConstruct1() {
CodeGen code = new CodeGen("""
-struct X { int ~x; };
-X z = new X { x=3; };
+struct _X { int ~x; new _X = { int xx -> x=xx; }; };
+_X z = new _X(3);
return z.x;
""");
code.driver(CodeGen.Phase.FinalizeIR);
@@ -36,20 +36,20 @@ return z.x;
@Test
public void testStructFinal0() {
CodeGen code = new CodeGen("""
-struct Point { int ~x, ~y; };
-Point p = new Point { x=3; y=4; };
+struct _Point { int ~x, ~y; new _Point = { int xx, int yy -> x=xx; y=yy; }; };
+_Point p = new _Point(3,4);
return p;
""");
code.driver(CodeGen.Phase.FinalizeIR);
- assertEquals("return (const)Point;", code.print());
- assertEquals("Point{x=3,y=4}", Eval2.eval(code, 0));
+ assertEquals("return (const)Test._Point;", code.print());
+ assertEquals("Test._Point{x=3,y=4}", Eval2.eval(code, 0));
}
@Test
public void testStructFinal1() {
CodeGen code = new CodeGen("""
-struct Point { int x=3, y=4; };
-~Point p = new Point { x=5; y=6; };
+struct _Point { int x=3, y=4; new _Point = { int xx, int yy -> x=xx; y=yy; }; };
+~_Point p = new _Point(5,6);
p.x++;
return p;
""");
@@ -60,8 +60,8 @@ return p;
@Test
public void testStructFinal2() {
CodeGen code = new CodeGen("""
-struct Point { int x=3, y=4; };
-~Point p = new Point;
+struct _Point { int x=3, y=4; };
+~_Point p = new _Point;
p.x++;
return p;
""");
@@ -71,18 +71,21 @@ return p;
@Test
public void testStructFinal3() {
- for( String fields : new String[] { "var x; var y;", "int[] !x;", "int[] !x;" } ) {
- String src = "struct Point { "+fields+" }; Point p = new Point; p.x++; return p;";
- try { new CodeGen(src).driver(CodeGen.Phase.TypeCheck); fail(fields); }
- catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
- }
+ CodeGen code = new CodeGen("""
+struct _Point { var x; var y; };
+_Point p = new _Point;
+p.x++;
+return p;
+""");
+ try { code.driver(CodeGen.Phase.FinalizeIR); fail(); }
+ catch( Exception e ) { assertEquals("'Test._Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
}
@Test
public void testStructFinal4() {
CodeGen code = new CodeGen("""
-struct Point { val x=3; val y=4; };
-Point p = new Point;
+struct _Point { val x=2; val y=4; };
+_Point p = new _Point;
p.x++;
return p;
""");
@@ -93,14 +96,14 @@ return p;
@Test
public void testStructFinal5() {
CodeGen code = new CodeGen("""
-struct Point { var x=3; var y=4; };
-!Point !p = new Point;
+struct _Point { var x=3; var y=4; };
+!_Point !p = new _Point;
p.x++;
return p;
""");
code.driver(CodeGen.Phase.FinalizeIR);
- assertEquals("return Point;", code.print());
- assertEquals("Point{x=4,y=4}", Eval2.eval(code, 0));
+ assertEquals("return Test._Point;", code.print());
+ assertEquals("Test._Point{x=4,y=4}", Eval2.eval(code, 0));
}
}