summaryrefslogtreecommitdiff
path: root/src/test/java/com/seaofnodes/simple/Chapter13Test.java
blob: 165d23e537d8c2cae0b38d99c2a4f330d2c7f6ab (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
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 Chapter13Test {
    @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 testLinkedList0() {
        Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
    LLI x = new LLI;
    x.next = head;
    x.i = arg;
    head = x;
    arg = arg-1;
}
return head.next.i;
""");
        try { parser.parse().iterate(); fail(); }
        catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
    }

    @Test
    public void testLinkedList1() {
        Parser parser = new Parser(
"""
struct LLI { LLI? next; int i; };
LLI? head = null;
while( arg ) {
    LLI x = new LLI;
    x.next = head;
    x.i = arg;
    head = x;
    arg = arg-1;
}
if( !head ) return 0;
LLI? next = head.next;
if( next==null ) return 1;
return next.i;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
        assertEquals(2L, Evaluator.evaluate(stop,  3));
    }

    @Test
    public void testCoRecur() {
        Parser parser = new Parser(
"""
struct int0 { int i; flt0? f; };
struct flt0 { flt f; int0? i; };
int0 i0 = new int0;
i0.i = 17;
flt0 f0 = new flt0;
f0.f = 3.14;
i0.f = f0;
f0.i = i0;
return f0.i.f.i.i;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 17;", stop.toString());
    }

    @Test
    public void testNullRef0() {
        Parser parser = new Parser(
"""
struct N { N? next; int i; };
N n = new N;
return n.next;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return null;", stop.toString());
    }

    @Test
    public void testNullRef1() {
        Parser parser = new Parser(
"""
struct M { int m; };
struct N { M next; int i; };
N n = new N { next = new M; };
return n.next;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return M;", stop.toString());
    }

    @Test
    public void testNullRef2() {
        Parser parser = new Parser(
"""
struct M { int m; };
struct N { M next; int i; };
N n = new N { next = null; }
return n.next;
""");
        try { parser.parse().iterate(); fail(); }
        catch( Exception e ) { assertEquals("Type null is not of declared type *M",e.getMessage()); }
    }

    @Test
    public void testNullRef3() {
        Parser parser = new Parser(
"""
struct N { N? next; int i; };
N n = new N;
n.i = 3.14;
return n.i;
""");
        try { parser.parse().iterate(); fail(); }
        catch( Exception e ) { assertEquals("Cannot store 3.14 into field int i",e.getMessage()); }
    }

    @Test
    public void testEmpty() {
        Parser parser = new Parser(
"""
struct S{};
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 0;", stop.toString());
        assertEquals(0L, Evaluator.evaluate(stop,  0));
    }

    @Test
    public void testForwardRef0() {
        Parser parser = new Parser(
"""
struct S1 { S2 s; };
return new S2;
""");
        try { parser.parse().iterate(); fail(); }
        catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
    }

    @Test
    public void testForwardRef1() {
        Parser parser = new Parser(
"""
struct S1 { S2? s; };
struct S2 { int x; };
return new S1.s=new S2;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return S2;", stop.toString());
    }

    @Test
    public void testcheckNull() {
        Parser parser = new Parser(
"""
struct I {int i;};
struct P { I? pi; };
P p1 = new P;
P p2 = new P;
p2.pi = new I;
p2.pi.i = 2;
if (arg) p1 = new P;
return p1.pi.i + 1;
""");
        try { parser.parse(false).iterate();  fail(); }
        catch( Exception e ) {  assertEquals("Might be null accessing 'i'",e.getMessage());  }
    }

}