Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SpecializationFallthroughTest.java @ 16756:5148aab962af
Truffle-DSL: updated tests for the new generation layout.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Aug 2014 15:53:05 +0200 |
parents | |
children | 9f38d222fa6c |
comparison
equal
deleted
inserted
replaced
16755:bd28da642eea | 16756:5148aab962af |
---|---|
1 package com.oracle.truffle.api.dsl.test; | |
2 | |
3 import static com.oracle.truffle.api.dsl.test.TestHelper.*; | |
4 | |
5 import org.junit.*; | |
6 | |
7 import com.oracle.truffle.api.dsl.*; | |
8 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest0Factory; | |
9 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest1Factory; | |
10 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest2Factory; | |
11 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest3Factory; | |
12 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest4Factory; | |
13 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest5Factory; | |
14 import com.oracle.truffle.api.dsl.test.TestHelper.ExecutionListener; | |
15 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
16 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
17 | |
18 public class SpecializationFallthroughTest { | |
19 | |
20 @Test | |
21 public void testFallthrough0() { | |
22 assertRuns(FallthroughTest0Factory.getInstance(), // | |
23 array(0, 0, 1, 2), // | |
24 array(0, 0, 1, 2),// | |
25 new ExecutionListener() { | |
26 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
27 if (!last) { | |
28 return; | |
29 } | |
30 if (FallthroughTest0.fallthroughCount > 1) { | |
31 Assert.fail("The fallthrough case must never be triggered twice. Therfore count must be <= 1, but is not."); | |
32 } | |
33 } | |
34 }); | |
35 } | |
36 | |
37 @NodeChildren({@NodeChild("a")}) | |
38 static class FallthroughTest0 extends ValueNode { | |
39 | |
40 static int fallthroughCount = 0; | |
41 | |
42 public FallthroughTest0() { | |
43 fallthroughCount = 0; | |
44 } | |
45 | |
46 @Specialization(rewriteOn = ArithmeticException.class) | |
47 int do1(int a) throws ArithmeticException { | |
48 if (a == 0) { | |
49 fallthroughCount++; | |
50 throw new ArithmeticException(); | |
51 } | |
52 return a; | |
53 } | |
54 | |
55 @Generic | |
56 Object doFallback(Object a) { | |
57 return a; | |
58 } | |
59 } | |
60 | |
61 /* | |
62 * Tests that the fall through is never triggered twice for monomorphic cases. | |
63 */ | |
64 @Test | |
65 public void testFallthrough1() { | |
66 assertRuns(FallthroughTest1Factory.getInstance(), // | |
67 array(0, 0, 0, 1, 2), // | |
68 array(0, 0, 0, 1, 2),// | |
69 new ExecutionListener() { | |
70 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
71 if (!last) { | |
72 return; | |
73 } | |
74 if (FallthroughTest1.fallthroughCount > 1) { | |
75 Assert.fail("The fallthrough case must never be triggered twice. Therfore count must be <= 1, but is not."); | |
76 } | |
77 } | |
78 }); | |
79 } | |
80 | |
81 /* TODO assert falltrough do1 before do2 */ | |
82 @NodeChildren({@NodeChild("a")}) | |
83 static class FallthroughTest1 extends ValueNode { | |
84 | |
85 static int fallthroughCount; | |
86 | |
87 public FallthroughTest1() { | |
88 fallthroughCount = 0; | |
89 } | |
90 | |
91 @Specialization(rewriteOn = ArithmeticException.class) | |
92 int do1(int a) throws ArithmeticException { | |
93 if (a == 0) { | |
94 fallthroughCount++; | |
95 throw new ArithmeticException(); | |
96 } | |
97 return a; | |
98 } | |
99 | |
100 @Specialization | |
101 int do2(int a) { | |
102 return a; | |
103 } | |
104 | |
105 } | |
106 | |
107 /* | |
108 * Tests that the fall through is never triggered twice with two falltrhoughs in one operation. | |
109 */ | |
110 @Test | |
111 public void testFallthrough2() { | |
112 assertRuns(FallthroughTest2Factory.getInstance(), // | |
113 array(0, 0, 1, 1, 2, 2), // | |
114 array(0, 0, 1, 1, 2, 2),// | |
115 new ExecutionListener() { | |
116 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
117 if (!last) { | |
118 return; | |
119 } | |
120 if (FallthroughTest2.fallthrough1 > 1) { | |
121 Assert.fail(); | |
122 } | |
123 if (FallthroughTest2.fallthrough2 > 1) { | |
124 Assert.fail(); | |
125 } | |
126 FallthroughTest2.fallthrough1 = 0; | |
127 FallthroughTest2.fallthrough2 = 0; | |
128 } | |
129 }); | |
130 } | |
131 | |
132 @NodeChildren({@NodeChild("a")}) | |
133 static class FallthroughTest2 extends ValueNode { | |
134 | |
135 static int fallthrough1; | |
136 static int fallthrough2; | |
137 | |
138 @Specialization(order = 1, rewriteOn = ArithmeticException.class) | |
139 int do1(int a) throws ArithmeticException { | |
140 if (a == 0) { | |
141 fallthrough1++; | |
142 throw new ArithmeticException(); | |
143 } | |
144 return a; | |
145 } | |
146 | |
147 @Specialization(order = 2, rewriteOn = ArithmeticException.class) | |
148 int do2(int a) throws ArithmeticException { | |
149 if (a == 1) { | |
150 fallthrough2++; | |
151 throw new ArithmeticException(); | |
152 } | |
153 return a; | |
154 } | |
155 | |
156 @Specialization | |
157 int do3(int a) { | |
158 return a; | |
159 } | |
160 } | |
161 | |
162 /* | |
163 * Tests that the fall through is never triggered twice. In this case mixed fallthrough with | |
164 * normal specializations. | |
165 */ | |
166 @Test | |
167 public void testFallthrough3() { | |
168 assertRuns(FallthroughTest3Factory.getInstance(), // | |
169 array(0, 0, 1, 1, 2, 2), // | |
170 array(0, 0, 1, 1, 2, 2),// | |
171 new ExecutionListener() { | |
172 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
173 if (!last) { | |
174 return; | |
175 } | |
176 if (FallthroughTest3.fallthrough1 > 1) { | |
177 Assert.fail(String.valueOf(FallthroughTest3.fallthrough1)); | |
178 } | |
179 FallthroughTest3.fallthrough1 = 0; | |
180 } | |
181 }); | |
182 } | |
183 | |
184 @NodeChildren({@NodeChild("a")}) | |
185 static class FallthroughTest3 extends ValueNode { | |
186 | |
187 static int fallthrough1; | |
188 | |
189 boolean guard0(int a) { | |
190 return a == 1; | |
191 } | |
192 | |
193 @Specialization(guards = "guard0") | |
194 int do2(int a) { | |
195 return a; | |
196 } | |
197 | |
198 @Specialization(rewriteOn = ArithmeticException.class) | |
199 int do1(int a) throws ArithmeticException { | |
200 if (a == 0) { | |
201 fallthrough1++; | |
202 throw new ArithmeticException(); | |
203 } | |
204 return a; | |
205 } | |
206 | |
207 @Specialization | |
208 int do3(int a) { | |
209 return a; | |
210 } | |
211 | |
212 } | |
213 | |
214 @Test | |
215 public void testFallthrough4() { | |
216 assertRuns(FallthroughTest4Factory.getInstance(), // | |
217 array(0, 0, 1, 1, 2, 2), // | |
218 array(0, 0, 1, 1, 2, 2),// | |
219 new ExecutionListener() { | |
220 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
221 if (!last) { | |
222 return; | |
223 } | |
224 if (FallthroughTest4.fallthrough1 > 1) { | |
225 Assert.fail(String.valueOf(FallthroughTest4.fallthrough1)); | |
226 } | |
227 if (FallthroughTest4.fallthrough2 > 1) { | |
228 Assert.fail(String.valueOf(FallthroughTest4.fallthrough1)); | |
229 } | |
230 FallthroughTest4.fallthrough1 = 0; | |
231 FallthroughTest4.fallthrough2 = 0; | |
232 } | |
233 }); | |
234 } | |
235 | |
236 @NodeChildren({@NodeChild("a")}) | |
237 static class FallthroughTest4 extends ValueNode { | |
238 | |
239 static int fallthrough1; | |
240 static int fallthrough2; | |
241 | |
242 @Specialization(order = 1, rewriteOn = ArithmeticException.class) | |
243 int do1(int a) throws ArithmeticException { | |
244 if (a == 0) { | |
245 fallthrough1++; | |
246 throw new ArithmeticException(); | |
247 } | |
248 return a; | |
249 } | |
250 | |
251 @Specialization(order = 2, rewriteOn = ArithmeticException.class) | |
252 int do2(int a) throws ArithmeticException { | |
253 if (a == 1) { | |
254 fallthrough2++; | |
255 throw new ArithmeticException(); | |
256 } | |
257 return a; | |
258 } | |
259 | |
260 @Specialization | |
261 int do3(int a) { | |
262 return a; | |
263 } | |
264 | |
265 } | |
266 | |
267 @Test | |
268 public void testFallthrough5() { | |
269 assertRuns(FallthroughTest5Factory.getInstance(), // | |
270 array(0, 0, 1, 1, 2, 2), // | |
271 array(0, 0, 1, 1, 2, 2),// | |
272 new ExecutionListener() { | |
273 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
274 if (!last) { | |
275 return; | |
276 } | |
277 if (FallthroughTest5.fallthrough1 > 1) { | |
278 Assert.fail(String.valueOf(FallthroughTest5.fallthrough1)); | |
279 } | |
280 FallthroughTest5.fallthrough1 = 0; | |
281 } | |
282 }); | |
283 } | |
284 | |
285 @NodeChildren({@NodeChild("a")}) | |
286 static class FallthroughTest5 extends ValueNode { | |
287 | |
288 static int fallthrough1; | |
289 | |
290 @Specialization(guards = "isDo1", rewriteOn = ArithmeticException.class) | |
291 int do1(int a) throws ArithmeticException { | |
292 if (a == 0) { | |
293 fallthrough1++; | |
294 throw new ArithmeticException(); | |
295 } | |
296 return a; | |
297 } | |
298 | |
299 protected static boolean isDo1(int a) { | |
300 return a == 0 || a == 1; | |
301 } | |
302 | |
303 @Specialization(guards = "isDo1") | |
304 int do2(int a) { | |
305 return a; | |
306 } | |
307 | |
308 @Specialization | |
309 int do3(int a) { | |
310 return a; | |
311 } | |
312 | |
313 } | |
314 | |
315 @NodeChildren({@NodeChild("a")}) | |
316 static class FallthroughTest6 extends ValueNode { | |
317 | |
318 static int fallthrough1; | |
319 static int fallthrough2; | |
320 static int fallthrough3; | |
321 static int fallthrough4; | |
322 | |
323 @Specialization(order = 1, rewriteOn = ArithmeticException.class) | |
324 int do4(int a) throws ArithmeticException { | |
325 return a; | |
326 } | |
327 | |
328 @Specialization(order = 2, rewriteOn = ArithmeticException.class) | |
329 int do2(int a) throws ArithmeticException { | |
330 return a; | |
331 } | |
332 | |
333 @Specialization(order = 3, rewriteOn = ArithmeticException.class) | |
334 int do3(int a) throws ArithmeticException { | |
335 return a; | |
336 } | |
337 | |
338 @Specialization(order = 4, rewriteOn = ArithmeticException.class) | |
339 int do1(int a) throws ArithmeticException { | |
340 return a; | |
341 } | |
342 | |
343 @Specialization | |
344 double do5(double a) { | |
345 return a; | |
346 } | |
347 | |
348 } | |
349 | |
350 } |