summaryrefslogtreecommitdiff
path: root/chapter23/src/test/java/com/seaofnodes/simple/Chapter08Test.java
blob: 09669cc7d81de67421bf1c31c03a848845d771d4 (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.codegen.CodeGen;
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() {
        CodeGen code = new CodeGen(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        break;
    if (arg == 6)
        break;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Region,(Phi(Loop,arg,Add)+1),Add,Phi_arg);", code.print());
        assertTrue(code.ctrl() instanceof RegionNode);
        Assert.assertEquals( "5", Eval2.eval(code, 1));
        Assert.assertEquals("10", Eval2.eval(code, 6));
    }


    @Test
    public void testEx5() {
        CodeGen code = new CodeGen(
                """
int a = 1;
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 7)
        continue;
    a = a + 1;
}
return a;
                """);
        code.parse().opto();
        assertEquals("return Phi(Loop,1,Phi(Region,Phi_a,(Phi_a+1),Phi_a));", code.print());
        assertTrue(code.ctrl() instanceof CProjNode);
    }


    @Test
    public void testEx4() {
        CodeGen code = new CodeGen(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 6)
        break;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", code.print());
        assertTrue(code.ctrl() instanceof RegionNode);
    }

    @Test
    public void testEx3() {
        CodeGen code = new CodeGen(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 6)
        break;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", code.print());
        assertTrue(code.ctrl() instanceof RegionNode);
    }


    @Test
    public void testEx2() {
        CodeGen code = new CodeGen(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
    if (arg == 6)
        continue;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code.print());
        assertTrue(code.ctrl() instanceof CProjNode);
    }


    @Test
    public void testEx1() {
        CodeGen code = new CodeGen(
                """
while(arg < 10) {
    arg = arg + 1;
    if (arg == 5)
        continue;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code.print());
        assertTrue(code.ctrl() instanceof CProjNode);
    }

    @Test
    public void testRegress1() {
        CodeGen code = new CodeGen(
                """
while( arg < 10 ) {
    int a = arg+2;
    if( a > 4 )
        break;
}
return arg;
                """);
        code.parse().opto();
        assertEquals("return arg;", code.print());
    }

    @Test
    public void testRegress2() {
        CodeGen code = new CodeGen("if(1) return 0;  else while(arg>- -arg) arg=arg+1; return 0;");
        code.parse().opto();
        assertEquals("return 0;", code.print());
    }

    @Test
    public void testBreakOutsideLoop() {
        try {
            new CodeGen("""
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() {
        CodeGen code = new CodeGen("""
while(arg < 10) {
    break;
}
return arg;
""");
        code.parse().opto();
        assertEquals("return arg;", code.print());
    }

    @Test
    public void testRegress4() {
        CodeGen code = new CodeGen("""
int a = 1;
while(arg < 10) {
    a = a + 1;
    if (arg > 2) {
        int a = 17;
        break;
    }
}
return a;
""");
        code.parse().opto();
        assertEquals("return Phi(Region,Phi(Loop,1,(Phi_a+1)),Add);", code.print());
        assertTrue(code.ctrl() instanceof RegionNode);
    }

    @Test
    public void testRegress5() {
        CodeGen code = new CodeGen("""
int a = 1;
while(1) {
    a = a + 1;
    if (a<10) continue;
    break;
}
return a;
""");
        code.parse().opto();
        assertEquals("return (Phi(Loop,1,Add)+1);", code.print());
        assertTrue(code.ctrl() instanceof CProjNode prj && prj._idx==1 );
    }

}