summaryrefslogtreecommitdiff
path: root/chapter16/src/test/java/com/seaofnodes/simple/Chapter16Test.java
blob: 3d3708dc930a9a1e4f374ed2d5f2c08e55363e43 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package com.seaofnodes.simple;

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

public class Chapter16Test {

    @Test
    public void testRequiredReference() {
        for( String fields : new String[] { "int[] x;" } ) {
            String src = "struct Point { "+fields+" }; Point p = new Point; p.x=p.x+1; return p;";
            try { new Parser(src).parse().iterate(); fail(fields); }
            catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
        }
    }

    @Ignore
    @Test
    public void testJig() {
        Parser parser = new Parser(
"""
return 3.14;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 3.14;", stop.toString());
        assertEquals(3.14, Evaluator.evaluate(stop,  0));
    }


    @Test
    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 testConstruct0() {
        Parser parser = new Parser("""
struct X { int x=3; };
X z = new X;
return z.x;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 3;", stop.toString());
        assertEquals(3L, Evaluator.evaluate(stop,  0));
    }



    @Test
    public void testConstruct2() {
        Parser parser = new Parser("""
struct X { int x=3; };
X z = new X { x = 4; };
return z.x;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 4;", stop.toString());
        assertEquals(4L, Evaluator.evaluate(stop,  0));
    }




    // Same as the Chapter13 test with the same name, but using the new
    // constructor syntax
    @Test
    public void testLinkedList1() {
        Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
    head = new LLI { next=head; i=arg; };
    arg = arg-1;
}
if( !head ) return 0;
LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
        assertEquals(0L, Evaluator.evaluate(stop,  0));
        assertEquals(1L, Evaluator.evaluate(stop,  1));
        assertEquals(2L, Evaluator.evaluate(stop,  3));
    }

    @Test
    public void testLinkedList2() {
        Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
    head = new LLI {
        next=head;
        // Any old code in the constructor
        int tmp=arg;
        while( arg > 10 ) {
            tmp = tmp + arg;
            arg = arg - 1;
        }
        i=tmp;
    };
    arg = arg-1;
}
if( !head ) return 0;
LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
        assertEquals(0L, Evaluator.evaluate(stop,  0));
        assertEquals(1L, Evaluator.evaluate(stop,  1));
        assertEquals(2L, Evaluator.evaluate(stop, 11));
    }

    @Test
    public void testSquare() {
        Parser parser = new Parser(
"""
struct Square {
    flt side = arg;
    // Newtons approximation to the square root, computed in a constructor.
    // The actual allocation will copy in this result as the initial
    // value for 'diag'.
    flt diag = arg*arg/2;
    while( 1 ) {
        flt next = (side/diag + diag)/2;
        if( next == diag ) break;
        diag = next;
    }
};
return new Square;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return Square;", stop.toString());
        assertEquals("Obj<Square>{side=3.0,diag=1.7320508075688772}", Evaluator.evaluate(stop,  3).toString());
        assertEquals("Obj<Square>{side=4.0,diag=2.0}", Evaluator.evaluate(stop, 4).toString());
    }
    static final String CONSTRUCTOR_MEMORY = """
        struct S { int x; int y; };
        struct T { int z=arg+40; };
        T t = new T;
        S a = new S { x=11; y=7; };
        S b = new S { x=22; y=9; };
        S p=a;
        if (arg) p=b;
        int before=p.x;
        S c = new S {
            x=p.x+1;
            { int i=0; while (i<2) { p.y=p.y+1; i=i+1; } }
            y=p.y;
        };
        p.x=33;
        return before*10000+c.x*100+c.y+t.z;
        """;

    @Test public void testConstructorMemory() {
        StopNode stop = new Parser(CONSTRUCTOR_MEMORY).parse().iterate();
        assertEquals(111249L,Evaluator.evaluate(stop,0));
        assertEquals(222352L,Evaluator.evaluate(stop,1));
    }
}