Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ContainsTest.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/ContainsTest.java@f4792a544170 |
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 import static org.hamcrest.CoreMatchers.*; | |
27 import static org.junit.Assert.*; | |
28 | |
29 import org.junit.*; | |
30 | |
31 import com.oracle.truffle.api.dsl.*; | |
32 import com.oracle.truffle.api.dsl.internal.*; | |
33 import com.oracle.truffle.api.dsl.test.ContainsTestFactory.Contains1Factory; | |
34 import com.oracle.truffle.api.dsl.test.ContainsTestFactory.Contains2Factory; | |
35 import com.oracle.truffle.api.dsl.test.ContainsTestFactory.Contains3Factory; | |
36 import com.oracle.truffle.api.dsl.test.ContainsTestFactory.Contains4Factory; | |
37 import com.oracle.truffle.api.dsl.test.ContainsTestFactory.PolymorphicToMonomorphic0Factory; | |
38 import com.oracle.truffle.api.dsl.test.TestHelper.ExecutionListener; | |
39 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
40 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
41 import com.oracle.truffle.api.nodes.*; | |
42 | |
43 @SuppressWarnings("unused") | |
44 public class ContainsTest { | |
45 | |
46 /* | |
47 * Tests a simple monomorphic inclusion. | |
48 */ | |
49 @Test | |
50 public void testContains1() { | |
51 assertRuns(Contains1Factory.getInstance(), // | |
52 array(1, "a", 2, "b"), // | |
53 array(2, "aa", 3, "ba"), // | |
54 new ExecutionListener() { | |
55 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
56 if (value instanceof String) { | |
57 if (node.getNode() instanceof DSLNode) { | |
58 // assert that the final specialization is always Object | |
59 Assert.assertEquals(Object.class, ((DSLNode) node.getNode()).getMetadata0().getSpecializedTypes()[0]); | |
60 } else { | |
61 Assert.assertTrue(((SpecializedNode) node.getNode()).getSpecializationNode().toString().startsWith("F2Node_")); | |
62 } | |
63 } | |
64 } | |
65 }); | |
66 } | |
67 | |
68 @NodeChild("a") | |
69 abstract static class Contains1 extends ValueNode { | |
70 | |
71 @Specialization | |
72 int f1(int a) { | |
73 return a + 1; | |
74 } | |
75 | |
76 @Specialization(contains = "f1") | |
77 Object f2(Object a) { | |
78 if (a instanceof Integer) { | |
79 return ((Integer) a) + 1; | |
80 } | |
81 return a + "a"; | |
82 } | |
83 } | |
84 | |
85 /* | |
86 * Tests an inclusion in within a polymorphic chain. | |
87 */ | |
88 @Test | |
89 public void testContains2() { | |
90 assertRuns(Contains2Factory.getInstance(), // | |
91 array(true, 1, 0, false), // | |
92 array(false, -1, 1, true) // | |
93 ); | |
94 } | |
95 | |
96 @NodeChild("a") | |
97 abstract static class Contains2 extends ValueNode { | |
98 | |
99 static boolean isZero(int a) { | |
100 return a == 0; | |
101 } | |
102 | |
103 @Specialization(guards = "isZero(a)") | |
104 int f1(int a) { | |
105 return a + 1; | |
106 } | |
107 | |
108 @Specialization(contains = "f1") | |
109 int f2(int a) { | |
110 if (a == 0) { | |
111 return a + 1; | |
112 } | |
113 return -a; | |
114 } | |
115 | |
116 @Specialization | |
117 boolean f3(boolean a) { | |
118 return !a; | |
119 } | |
120 } | |
121 | |
122 /* | |
123 * Tests transitive monomorphic inclusion. | |
124 */ | |
125 @Test | |
126 public void testContains3() { | |
127 assertRuns(Contains3Factory.getInstance(), // | |
128 array(2, 1, 2, -3, -4), // | |
129 array(-2, 2, -2, -3, -4), // | |
130 new ExecutionListener() { | |
131 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
132 // assert that we are always monomorphic | |
133 Assert.assertEquals(NodeCost.MONOMORPHIC, node.getNode().getCost()); | |
134 } | |
135 }); | |
136 } | |
137 | |
138 @NodeChild("a") | |
139 abstract static class Contains3 extends ValueNode { | |
140 | |
141 static boolean isGreaterZero(int a) { | |
142 return a > 0; | |
143 } | |
144 | |
145 static boolean isOne(int a) { | |
146 return a == 1; | |
147 } | |
148 | |
149 @Specialization(guards = {"isOne(a)"}) | |
150 int f1(int a) { | |
151 return a + 1; | |
152 } | |
153 | |
154 @Specialization(contains = "f1", guards = {"isGreaterZero(a)"}) | |
155 int f2(int a) { | |
156 if (a == 1) { | |
157 return 2; | |
158 } | |
159 return -a; | |
160 } | |
161 | |
162 @Specialization(contains = "f2") | |
163 int f3(int a) { | |
164 if (a > 0) { | |
165 return a == 1 ? 2 : -a; | |
166 } else { | |
167 return a; | |
168 } | |
169 } | |
170 | |
171 } | |
172 | |
173 /* | |
174 * Tests that if it can be derived that two specializations actually a as powerful as the latter | |
175 * we can combine them. Therefore operation should always become monomorphic in the end. | |
176 */ | |
177 @Test | |
178 public void testContains4() { | |
179 assertRuns(Contains4Factory.getInstance(), // | |
180 array(-1, 0, 1, 2), // | |
181 array(1, 0, 1, 2), // | |
182 new ExecutionListener() { | |
183 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { | |
184 Assert.assertEquals(NodeCost.MONOMORPHIC, node.getNode().getCost()); | |
185 } | |
186 }); | |
187 } | |
188 | |
189 @NodeChild("a") | |
190 abstract static class Contains4 extends ValueNode { | |
191 | |
192 static boolean isOne(int a) { | |
193 return a == 1; | |
194 } | |
195 | |
196 @Specialization(guards = "isOne(a)") | |
197 int f0(int a) { | |
198 return 1; | |
199 } | |
200 | |
201 @Specialization(contains = "f0", guards = "a >= 0") | |
202 int f1(int a) { | |
203 return a; | |
204 } | |
205 | |
206 @Specialization(contains = {"f1"}) | |
207 int f2(int a) { | |
208 return Math.abs(a); | |
209 } | |
210 | |
211 } | |
212 | |
213 @NodeChild("a") | |
214 abstract static class ContainsError1 extends ValueNode { | |
215 @ExpectError("The contained specialization 'f1' must be declared before the containing specialization.") | |
216 @Specialization(contains = "f1") | |
217 int f0(int a) { | |
218 return a; | |
219 } | |
220 | |
221 @Specialization | |
222 Object f1(String a) { | |
223 return a; | |
224 } | |
225 } | |
226 | |
227 @NodeChild("a") | |
228 abstract static class ContainsError2 extends ValueNode { | |
229 | |
230 @ExpectError("The referenced specialization 'does not exist' could not be found.") | |
231 @Specialization(contains = "does not exist") | |
232 int f0(int a) { | |
233 return a; | |
234 } | |
235 } | |
236 | |
237 @NodeChild("a") | |
238 abstract static class ContainsError3 extends ValueNode { | |
239 | |
240 @Specialization | |
241 int f0(int a) { | |
242 return a; | |
243 } | |
244 | |
245 @ExpectError("Duplicate contains declaration 'f0'.") | |
246 @Specialization(contains = {"f0", "f0"}) | |
247 Object f1(double a) { | |
248 return a; | |
249 } | |
250 } | |
251 | |
252 @NodeChild("a") | |
253 abstract static class ContainsError4 extends ValueNode { | |
254 | |
255 @ExpectError("Circular contained specialization 'f1(double)' found.") | |
256 @Specialization(contains = {"f1"}) | |
257 Object f1(double a) { | |
258 return a; | |
259 } | |
260 } | |
261 | |
262 @NodeChild("a") | |
263 abstract static class ContainsError5 extends ValueNode { | |
264 | |
265 @ExpectError({"Circular contained specialization 'f0(int)' found.", "Circular contained specialization 'f1(double)' found.", | |
266 "The contained specialization 'f1' must be declared before the containing specialization."}) | |
267 @Specialization(contains = "f1") | |
268 int f0(int a) { | |
269 return a; | |
270 } | |
271 | |
272 @ExpectError("Circular contained specialization 'f1(double)' found.") | |
273 @Specialization(contains = {"f0"}) | |
274 Object f1(double a) { | |
275 return a; | |
276 } | |
277 } | |
278 | |
279 @NodeChild("a") | |
280 abstract static class ContainsType1 extends ValueNode { | |
281 @Specialization | |
282 int f0(int a) { | |
283 return a; | |
284 } | |
285 | |
286 @ExpectError("Specialization is not reachable. It is shadowed by f0(int).") | |
287 @Specialization(contains = "f0") | |
288 Object f1(int a) { | |
289 return a; | |
290 } | |
291 } | |
292 | |
293 @NodeChild("a") | |
294 abstract static class ContainsType2 extends ValueNode { | |
295 @Specialization | |
296 int f0(int a) { | |
297 return a; | |
298 } | |
299 | |
300 @Specialization(contains = "f0") | |
301 Object f1(Object a) { | |
302 return a; | |
303 } | |
304 } | |
305 | |
306 @NodeChild("a") | |
307 abstract static class ContainsType3 extends ValueNode { | |
308 @Specialization | |
309 int f0(int a) { | |
310 return a; | |
311 } | |
312 | |
313 @Specialization(contains = "f0") | |
314 Object f1(double a) { // implicit type | |
315 return a; | |
316 } | |
317 } | |
318 | |
319 @NodeChild("a") | |
320 abstract static class ContainsType4 extends ValueNode { | |
321 @Specialization | |
322 double f0(double a) { | |
323 return a; | |
324 } | |
325 | |
326 @ExpectError({"Specialization is not reachable. It is shadowed by f0(double)."}) | |
327 @Specialization(contains = "f0") | |
328 int f1(int a) { // implicit type | |
329 return a; | |
330 } | |
331 } | |
332 | |
333 @NodeChildren({@NodeChild("a"), @NodeChild("b")}) | |
334 abstract static class ContainsType5 extends ValueNode { | |
335 @Specialization | |
336 Object f0(Object a, int b) { | |
337 return a; | |
338 } | |
339 | |
340 @Specialization(contains = "f0") | |
341 Object f1(int a, Object b) { | |
342 return a; | |
343 } | |
344 } | |
345 | |
346 @NodeChildren({@NodeChild("a"), @NodeChild("b")}) | |
347 abstract static class ContainsType6 extends ValueNode { | |
348 @Specialization | |
349 Object f0(double a, int b) { | |
350 return a; | |
351 } | |
352 | |
353 @Specialization(contains = "f0") | |
354 Object f1(int a, double b) { // implicit type | |
355 return a; | |
356 } | |
357 } | |
358 | |
359 abstract static class ContainsGuard1 extends ValueNode { | |
360 | |
361 boolean g1() { | |
362 return true; | |
363 } | |
364 | |
365 @Specialization(guards = "g1()") | |
366 Object f0() { | |
367 return null; | |
368 } | |
369 | |
370 @Specialization(contains = "f0") | |
371 Object f1() { | |
372 return null; | |
373 } | |
374 } | |
375 | |
376 abstract static class ContainsGuard2 extends ValueNode { | |
377 | |
378 boolean g1() { | |
379 return true; | |
380 } | |
381 | |
382 @Specialization | |
383 Object f0() { | |
384 return null; | |
385 } | |
386 | |
387 @ExpectError({"Specialization is not reachable. It is shadowed by f0()."}) | |
388 @Specialization(guards = "g1()", contains = "f0") | |
389 Object f1() { | |
390 return null; | |
391 } | |
392 } | |
393 | |
394 abstract static class ContainsGuard3 extends ValueNode { | |
395 | |
396 boolean g1() { | |
397 return true; | |
398 } | |
399 | |
400 @Specialization(guards = "g1()") | |
401 Object f0() { | |
402 return null; | |
403 } | |
404 | |
405 @Specialization(guards = "!g1()", contains = "f0") | |
406 Object f1() { | |
407 return null; | |
408 } | |
409 } | |
410 | |
411 abstract static class ContainsGuard4 extends ValueNode { | |
412 | |
413 boolean g1() { | |
414 return true; | |
415 } | |
416 | |
417 boolean g2() { | |
418 return true; | |
419 } | |
420 | |
421 @Specialization(guards = "g1()") | |
422 Object f0() { | |
423 return null; | |
424 } | |
425 | |
426 @Specialization(guards = "g2()", contains = "f0") | |
427 Object f1() { | |
428 return null; | |
429 } | |
430 } | |
431 | |
432 abstract static class ContainsGuard5 extends ValueNode { | |
433 | |
434 boolean g1() { | |
435 return true; | |
436 } | |
437 | |
438 boolean g2() { | |
439 return true; | |
440 } | |
441 | |
442 @Specialization(guards = "g1()") | |
443 Object f0() { | |
444 return null; | |
445 } | |
446 | |
447 @Specialization(guards = "g2()", contains = "f0") | |
448 Object f1() { | |
449 return null; | |
450 } | |
451 } | |
452 | |
453 abstract static class ContainsGuard6 extends ValueNode { | |
454 | |
455 boolean g1() { | |
456 return true; | |
457 } | |
458 | |
459 boolean g2() { | |
460 return true; | |
461 } | |
462 | |
463 @Specialization(guards = "g1()") | |
464 Object f0() { | |
465 return null; | |
466 } | |
467 | |
468 @Specialization(guards = "!g2()", contains = "f0") | |
469 Object f1() { | |
470 return null; | |
471 } | |
472 } | |
473 | |
474 abstract static class ContainsGuard7 extends ValueNode { | |
475 | |
476 boolean g1() { | |
477 return true; | |
478 } | |
479 | |
480 boolean g2() { | |
481 return true; | |
482 } | |
483 | |
484 @Specialization(guards = {"g1()", "g2()"}) | |
485 Object f0() { | |
486 return null; | |
487 } | |
488 | |
489 @Specialization(guards = "g2()", contains = "f0") | |
490 Object f1() { | |
491 return null; | |
492 } | |
493 } | |
494 | |
495 abstract static class ContainsThrowable1 extends ValueNode { | |
496 | |
497 @Specialization(rewriteOn = RuntimeException.class) | |
498 Object f0() throws RuntimeException { | |
499 throw new RuntimeException(); | |
500 } | |
501 | |
502 @Specialization(contains = "f0") | |
503 Object f1() { | |
504 return null; | |
505 } | |
506 } | |
507 | |
508 abstract static class ContainsThrowable2 extends ValueNode { | |
509 | |
510 @Specialization(rewriteOn = RuntimeException.class) | |
511 Object f0() throws RuntimeException { | |
512 throw new RuntimeException(); | |
513 } | |
514 | |
515 @Specialization(contains = "f0", rewriteOn = RuntimeException.class) | |
516 Object f1() throws RuntimeException { | |
517 throw new RuntimeException(); | |
518 } | |
519 | |
520 @Specialization(contains = "f1") | |
521 Object f2() { | |
522 return null; | |
523 } | |
524 } | |
525 | |
526 @Test | |
527 public void testPolymorphicToMonomorphic0() { | |
528 TestRootNode<PolymorphicToMonomorphic0> root = createRoot(PolymorphicToMonomorphic0Factory.getInstance()); | |
529 assertThat((int) executeWith(root, 1), is(1)); | |
530 assertThat((int) executeWith(root, 2), is(2)); | |
531 assertThat((int) executeWith(root, 3), is(3)); | |
532 assertThat(root.getNode().getCost(), is(NodeCost.MONOMORPHIC)); | |
533 } | |
534 | |
535 @NodeChild("a") | |
536 static class PolymorphicToMonomorphic0 extends ValueNode { | |
537 | |
538 @Specialization(guards = "a == 1") | |
539 int do1(int a) { | |
540 return a; | |
541 } | |
542 | |
543 @Specialization(guards = "a == 2") | |
544 int do2(int a) { | |
545 return a; | |
546 } | |
547 | |
548 @Specialization(contains = {"do1", "do2"}) | |
549 int do3(int a) { | |
550 return a; | |
551 } | |
552 | |
553 } | |
554 | |
555 } |