summaryrefslogtreecommitdiff
path: root/chapter15/src/test/java/com/seaofnodes/simple/Chapter08Test.java
blob: 7760ae091e15b713194cf2a3ac3444372ee47a85 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.seaofnodes.simple;

import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.*;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class Chapter08Test {

    @Test
    public void testEx6() {
        Parser parser = new Parser(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        break;
    if (arg == 6)
        break;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Region37,Phi(Region26,Phi(Loop8,arg,(Phi_arg+1)),Add),Add);", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof RegionNode);
        Assert.assertEquals(5L, Evaluator.evaluate(stop, 1));
        Assert.assertEquals(10L, Evaluator.evaluate(stop, 6));
    }


    @Test
    public void testEx5() {
        Parser parser = new Parser(
                """
int a = 1;
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 7)
        continue;
    a = a + 1;
}
return a;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Loop9,1,Phi(Region43,Phi_a,(Phi_a+1)));", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof CProjNode);
    }


    @Test
    public void testEx4() {
        Parser parser = new Parser(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 6)
        break;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Region35,Phi(Loop8,arg,(Phi_arg+1)),Add);", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof RegionNode);
    }

    @Test
    public void testEx3() {
        Parser parser = new Parser(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 6)
        break;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Region26,Phi(Loop8,arg,(Phi_arg+1)),Add);", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof RegionNode);
    }


    @Test
    public void testEx2() {
        Parser parser = new Parser(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 6)
        continue;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Loop8,arg,(Phi_arg+1));", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof CProjNode);
    }


    @Test
    public void testEx1() {
        Parser parser = new Parser(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Loop8,arg,(Phi_arg+1));", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof CProjNode);
    }

    @Test
    public void testRegress1() {
        Parser parser = new Parser(
                """
while( arg < 10 ) {
    int a = arg+2;
    if( a > 4 )
        break;
}
return arg;
                """);
        StopNode stop = parser.parse().iterate();
        assertEquals("return arg;", stop.toString());
    }

    @Test
    public void testRegress2() {
        Parser parser = new Parser("if(1) return 0;  else while(arg>--arg) arg=arg+1; return 0;");
        StopNode stop = parser.parse().iterate();
        assertEquals("return 0;", stop.toString());
    }

    @Test
    public void testBreakOutsideLoop() {
        try {
            new Parser("""
if(arg <= 10) {
    break;
    arg = arg + 1;
}
return arg;
""").parse();
            fail();
        } catch( RuntimeException e ) {
            assertEquals("No active loop for a break or continue",e.getMessage());
        }
    }

    @Test
    public void testRegress3() {
        Parser parser = new Parser("""
while(arg < 10) {
    break;
}
return arg;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return arg;", stop.toString());
    }

    @Test
    public void testRegress4() {
        Parser parser = new Parser("""
int a = 1;
while(arg < 10) {
    a = a + 1;
    if (arg > 2) {
        int a = 17;
        break;
    }
}
return a;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return Phi(Region29,Phi(Loop9,1,(Phi_a+1)),Add);", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof RegionNode);
    }

    @Test
    public void testRegress5() {
        Parser parser = new Parser("""
int a = 1;
while(1) {
    a = a + 1;
    if (a<10) continue;
    break;
}
return a;
""");
        StopNode stop = parser.parse().iterate();
        assertEquals("return (Phi(Loop9,1,Add)+1);", stop.toString());
        assertTrue(stop.ret().ctrl() instanceof CProjNode prj && prj._idx==1 );
    }

}