summaryrefslogtreecommitdiff
path: root/chapter19/src/test/java/com/seaofnodes/simple/Chapter17aTest.java
blob: ab92d4394866a13b95ae319ee2996e3276e7a36a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.seaofnodes.simple;

import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class Chapter17aTest {

    @Test
    public void testFinal1() {
        CodeGen code = new CodeGen(
"""
int x=2, y=3;
if( arg ) { int x = y; x = x*x; y=x; } // Shadow final x
return y;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,9,3);", code.print());
        assertEquals("3", Eval2.eval(code, 0));
        assertEquals("9", Eval2.eval(code, 1));
    }

    @Test
    public void testConstruct1() {
        CodeGen code = new CodeGen("""
struct X { int ~x; };
X z = new X { x=3; };
return z.x;
""");
        code.parse().opto();
        assertEquals("return 3;", code.print());
        assertEquals("3", Eval2.eval(code,  0));
    }

    @Test
    public void testStructFinal0() {
        CodeGen code = new CodeGen("""
struct Point { int ~x, ~y; };
Point p = new Point { x=3; y=4; };
return p;
""");
        code.parse().opto();
        assertEquals("return (const)Point;", code.print());
        assertEquals("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; };
p.x++;
return p;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @Test
    public void testStructFinal2() {
        CodeGen code = new CodeGen("""
struct Point { int x=3, y=4; };
Point p = new Point;
p.x++;
return p;
""");
        try { code.parse().opto().typeCheck(); fail(); }
        catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
    }

    @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).parse().opto(); fail(fields); }
            catch( Exception e ) { assertEquals("'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;
p.x++;
return p;
""");
        try { code.parse().opto(); fail(); }
        catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
    }

    @Test
    public void testStructFinal5() {
        CodeGen code = new CodeGen("""
struct Point { var x=3; var y=4; };
!Point !p = new Point;
p.x++;
return p;
""");
        code.parse().opto();
        assertEquals("return Point;", code.print());
        assertEquals("Point{x=4,y=4}", Eval2.eval(code,  0));
    }

}