Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SpecializationFallthroughTest.java @ 21951:9c8c0937da41
Moving all sources into truffle subdirectory
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 17 Jun 2015 10:58:08 +0200 |
parents | graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SpecializationFallthroughTest.java@08aa0372dad4 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2012, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.dsl.test; | |
24 | |
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*; | |
26 | |
27 import org.junit.*; | |
28 | |
29 import com.oracle.truffle.api.dsl.*; | |
30 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest0Factory; | |
31 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest1Factory; | |
32 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest2Factory; | |
33 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest3Factory; | |
34 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest4Factory; | |
35 import com.oracle.truffle.api.dsl.test.SpecializationFallthroughTestFactory.FallthroughTest5Factory; | |
36 import com.oracle.truffle.api.dsl.test.TestHelper.ExecutionListener; | |
37 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
39 | |
40 public class SpecializationFallthroughTest { | |
41 | |
42 @Test | |
43 public void testFallthrough0() { | |
44 assertRuns(FallthroughTest0Factory.getInstance(), // | |
45 array(0, 0, 1, 2), // | |
46 array(0, 0, 1, 2), // | |
47 new ExecutionListener() { | |
48 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
49 if (!last) { | |
50 return; | |
51 } | |
52 if (FallthroughTest0.fallthroughCount > 1) { | |
53 Assert.fail("The fallthrough case must never be triggered twice. Therfore count must be <= 1, but is not."); | |
54 } | |
55 } | |
56 }); | |
57 } | |
58 | |
59 @NodeChildren({@NodeChild("a")}) | |
60 static class FallthroughTest0 extends ValueNode { | |
61 | |
62 static int fallthroughCount = 0; | |
63 | |
64 public FallthroughTest0() { | |
65 fallthroughCount = 0; | |
66 } | |
67 | |
68 @Specialization(rewriteOn = ArithmeticException.class) | |
69 int do1(int a) throws ArithmeticException { | |
70 if (a == 0) { | |
71 fallthroughCount++; | |
72 throw new ArithmeticException(); | |
73 } | |
74 return a; | |
75 } | |
76 | |
77 @Fallback | |
78 Object doFallback(Object a) { | |
79 return a; | |
80 } | |
81 } | |
82 | |
83 /* | |
84 * Tests that the fall through is never triggered twice for monomorphic cases. | |
85 */ | |
86 @Test | |
87 public void testFallthrough1() { | |
88 assertRuns(FallthroughTest1Factory.getInstance(), // | |
89 array(0, 0, 0, 1, 2), // | |
90 array(0, 0, 0, 1, 2), // | |
91 new ExecutionListener() { | |
92 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
93 if (!last) { | |
94 return; | |
95 } | |
96 if (FallthroughTest1.fallthroughCount > 1) { | |
97 Assert.fail("The fallthrough case must never be triggered twice. Therfore count must be <= 1, but is not."); | |
98 } | |
99 } | |
100 }); | |
101 } | |
102 | |
103 /* TODO assert falltrough do1 before do2 */ | |
104 @NodeChildren({@NodeChild("a")}) | |
105 static class FallthroughTest1 extends ValueNode { | |
106 | |
107 static int fallthroughCount; | |
108 | |
109 public FallthroughTest1() { | |
110 fallthroughCount = 0; | |
111 } | |
112 | |
113 @Specialization(rewriteOn = ArithmeticException.class) | |
114 int do1(int a) throws ArithmeticException { | |
115 if (a == 0) { | |
116 fallthroughCount++; | |
117 throw new ArithmeticException(); | |
118 } | |
119 return a; | |
120 } | |
121 | |
122 @Specialization | |
123 int do2(int a) { | |
124 return a; | |
125 } | |
126 | |
127 } | |
128 | |
129 /* | |
130 * Tests that the fall through is never triggered twice with two falltrhoughs in one operation. | |
131 */ | |
132 @Test | |
133 public void testFallthrough2() { | |
134 assertRuns(FallthroughTest2Factory.getInstance(), // | |
135 array(0, 0, 1, 1, 2, 2), // | |
136 array(0, 0, 1, 1, 2, 2), // | |
137 new ExecutionListener() { | |
138 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
139 if (!last) { | |
140 return; | |
141 } | |
142 if (FallthroughTest2.fallthrough1 > 1) { | |
143 Assert.fail(); | |
144 } | |
145 if (FallthroughTest2.fallthrough2 > 1) { | |
146 Assert.fail(); | |
147 } | |
148 FallthroughTest2.fallthrough1 = 0; | |
149 FallthroughTest2.fallthrough2 = 0; | |
150 } | |
151 }); | |
152 } | |
153 | |
154 @NodeChildren({@NodeChild("a")}) | |
155 static class FallthroughTest2 extends ValueNode { | |
156 | |
157 static int fallthrough1; | |
158 static int fallthrough2; | |
159 | |
160 @Specialization(order = 1, rewriteOn = ArithmeticException.class) | |
161 int do1(int a) throws ArithmeticException { | |
162 if (a == 0) { | |
163 fallthrough1++; | |
164 throw new ArithmeticException(); | |
165 } | |
166 return a; | |
167 } | |
168 | |
169 @Specialization(order = 2, rewriteOn = ArithmeticException.class) | |
170 int do2(int a) throws ArithmeticException { | |
171 if (a == 1) { | |
172 fallthrough2++; | |
173 throw new ArithmeticException(); | |
174 } | |
175 return a; | |
176 } | |
177 | |
178 @Specialization | |
179 int do3(int a) { | |
180 return a; | |
181 } | |
182 } | |
183 | |
184 /* | |
185 * Tests that the fall through is never triggered twice. In this case mixed fallthrough with | |
186 * normal specializations. | |
187 */ | |
188 @Test | |
189 public void testFallthrough3() { | |
190 assertRuns(FallthroughTest3Factory.getInstance(), // | |
191 array(0, 0, 1, 1, 2, 2), // | |
192 array(0, 0, 1, 1, 2, 2), // | |
193 new ExecutionListener() { | |
194 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
195 if (!last) { | |
196 return; | |
197 } | |
198 if (FallthroughTest3.fallthrough1 > 1) { | |
199 Assert.fail(String.valueOf(FallthroughTest3.fallthrough1)); | |
200 } | |
201 FallthroughTest3.fallthrough1 = 0; | |
202 } | |
203 }); | |
204 } | |
205 | |
206 @NodeChildren({@NodeChild("a")}) | |
207 static class FallthroughTest3 extends ValueNode { | |
208 | |
209 static int fallthrough1; | |
210 | |
211 boolean guard0(int a) { | |
212 return a == 1; | |
213 } | |
214 | |
215 @Specialization(guards = "guard0(a)") | |
216 int do2(int a) { | |
217 return a; | |
218 } | |
219 | |
220 @Specialization(rewriteOn = ArithmeticException.class) | |
221 int do1(int a) throws ArithmeticException { | |
222 if (a == 0) { | |
223 fallthrough1++; | |
224 throw new ArithmeticException(); | |
225 } | |
226 return a; | |
227 } | |
228 | |
229 @Specialization | |
230 int do3(int a) { | |
231 return a; | |
232 } | |
233 | |
234 } | |
235 | |
236 @Test | |
237 public void testFallthrough4() { | |
238 assertRuns(FallthroughTest4Factory.getInstance(), // | |
239 array(0, 0, 1, 1, 2, 2), // | |
240 array(0, 0, 1, 1, 2, 2), // | |
241 new ExecutionListener() { | |
242 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
243 if (!last) { | |
244 return; | |
245 } | |
246 if (FallthroughTest4.fallthrough1 > 1) { | |
247 Assert.fail(String.valueOf(FallthroughTest4.fallthrough1)); | |
248 } | |
249 if (FallthroughTest4.fallthrough2 > 1) { | |
250 Assert.fail(String.valueOf(FallthroughTest4.fallthrough1)); | |
251 } | |
252 FallthroughTest4.fallthrough1 = 0; | |
253 FallthroughTest4.fallthrough2 = 0; | |
254 } | |
255 }); | |
256 } | |
257 | |
258 @NodeChildren({@NodeChild("a")}) | |
259 static class FallthroughTest4 extends ValueNode { | |
260 | |
261 static int fallthrough1; | |
262 static int fallthrough2; | |
263 | |
264 @Specialization(order = 1, rewriteOn = ArithmeticException.class) | |
265 int do1(int a) throws ArithmeticException { | |
266 if (a == 0) { | |
267 fallthrough1++; | |
268 throw new ArithmeticException(); | |
269 } | |
270 return a; | |
271 } | |
272 | |
273 @Specialization(order = 2, rewriteOn = ArithmeticException.class) | |
274 int do2(int a) throws ArithmeticException { | |
275 if (a == 1) { | |
276 fallthrough2++; | |
277 throw new ArithmeticException(); | |
278 } | |
279 return a; | |
280 } | |
281 | |
282 @Specialization | |
283 int do3(int a) { | |
284 return a; | |
285 } | |
286 | |
287 } | |
288 | |
289 @Test | |
290 public void testFallthrough5() { | |
291 assertRuns(FallthroughTest5Factory.getInstance(), // | |
292 array(0, 0, 1, 1, 2, 2), // | |
293 array(0, 0, 1, 1, 2, 2), // | |
294 new ExecutionListener() { | |
295 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
296 if (!last) { | |
297 return; | |
298 } | |
299 if (FallthroughTest5.fallthrough1 > 1) { | |
300 Assert.fail(String.valueOf(FallthroughTest5.fallthrough1)); | |
301 } | |
302 FallthroughTest5.fallthrough1 = 0; | |
303 } | |
304 }); | |
305 } | |
306 | |
307 @NodeChildren({@NodeChild("a")}) | |
308 static class FallthroughTest5 extends ValueNode { | |
309 | |
310 static int fallthrough1; | |
311 | |
312 @Specialization(guards = "isDo1(a)", rewriteOn = ArithmeticException.class) | |
313 int do1(int a) throws ArithmeticException { | |
314 if (a == 0) { | |
315 fallthrough1++; | |
316 throw new ArithmeticException(); | |
317 } | |
318 return a; | |
319 } | |
320 | |
321 protected static boolean isDo1(int a) { | |
322 return a == 0 || a == 1; | |
323 } | |
324 | |
325 @Specialization(guards = "isDo1(a)") | |
326 int do2(int a) { | |
327 return a; | |
328 } | |
329 | |
330 @Specialization | |
331 int do3(int a) { | |
332 return a; | |
333 } | |
334 | |
335 } | |
336 | |
337 /* Throwing RuntimeExceptions without rewriteOn is allowed. */ | |
338 @NodeChildren({@NodeChild("a")}) | |
339 static class FallthroughExceptionType0 extends ValueNode { | |
340 | |
341 @Specialization | |
342 int do4(int a) throws RuntimeException { | |
343 return a; | |
344 } | |
345 | |
346 } | |
347 | |
348 /* Non runtime exceptions must be verified. */ | |
349 @NodeChildren({@NodeChild("a")}) | |
350 static class FallthroughExceptionType1 extends ValueNode { | |
351 | |
352 @ExpectError("A rewriteOn checked exception was specified but not thrown in the method's throws clause. The @Specialization method must specify a throws clause with the exception type 'java.lang.Throwable'.") | |
353 @Specialization(rewriteOn = Throwable.class) | |
354 int do4(int a) { | |
355 return a; | |
356 } | |
357 | |
358 } | |
359 | |
360 /* Checked exception must be verified. */ | |
361 @NodeChildren({@NodeChild("a")}) | |
362 static class FallthroughExceptionType2 extends ValueNode { | |
363 | |
364 @ExpectError("A checked exception 'java.lang.Throwable' is thrown but is not specified using the rewriteOn property. " | |
365 + "Checked exceptions that are not used for rewriting are not handled by the DSL. Use RuntimeExceptions for this purpose instead.") | |
366 @Specialization | |
367 int do4(int a) throws Throwable { | |
368 return a; | |
369 } | |
370 | |
371 } | |
372 | |
373 } |