comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MethodGuardsTest.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/MethodGuardsTest.java@b1530a6cce8c
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.junit.Assert.*;
27
28 import org.junit.*;
29
30 import com.oracle.truffle.api.*;
31 import com.oracle.truffle.api.dsl.*;
32 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardCompareWithFieldTestFactory;
33 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardComplexTestFactory;
34 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardEqualByteIntTestFactory;
35 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardEqualIntLongTestFactory;
36 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardEqualLongIntTestFactory;
37 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardEqualShortIntTestFactory;
38 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardEqualTestFactory;
39 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardFieldTestFactory;
40 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardGreaterEqualTestFactory;
41 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardGreaterTestFactory;
42 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardLessEqualTestFactory;
43 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardLessTestFactory;
44 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardMethodTestFactory;
45 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardMultipleAndMethodTestFactory;
46 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardMultipleOrMethodTestFactory;
47 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardNotTestFactory;
48 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardOrTestFactory;
49 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardStaticFieldTestFactory;
50 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardStaticFinalFieldCompareTestFactory;
51 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardUnboundMethodTestFactory;
52 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
53
54 @SuppressWarnings("unused")
55 public class MethodGuardsTest {
56
57 @Test
58 public void testGuardEqual() {
59 CallTarget root = createCallTarget(GuardEqualTestFactory.getInstance());
60 assertEquals("do1", root.call(1));
61 assertEquals("do2", root.call(2));
62 assertEquals("do1", root.call(1));
63 }
64
65 @NodeChild
66 static class GuardEqualTest extends ValueNode {
67 @Specialization(guards = "value == 1")
68 static String do1(int value) {
69 return "do1";
70 }
71
72 @Specialization
73 static String do2(int value) {
74 return "do2";
75 }
76 }
77
78 @Test
79 public void testGuardEqualIntLong() {
80 CallTarget root = createCallTarget(GuardEqualIntLongTestFactory.getInstance());
81 assertEquals("do1", root.call(1));
82 assertEquals("do2", root.call(2));
83 assertEquals("do1", root.call(1));
84 }
85
86 @NodeChild
87 static class GuardEqualIntLongTest extends ValueNode {
88 @Specialization(guards = "1 == value")
89 static String do1(long value) {
90 return "do1";
91 }
92
93 @Specialization
94 static String do2(long value) {
95 return "do2";
96 }
97 }
98
99 @Test
100 public void testGuardEqualByteInt() {
101 CallTarget root = createCallTarget(GuardEqualByteIntTestFactory.getInstance());
102 assertEquals("do1", root.call((byte) 1));
103 assertEquals("do2", root.call((byte) 2));
104 assertEquals("do1", root.call((byte) 1));
105 }
106
107 @NodeChild
108 static class GuardEqualByteIntTest extends ValueNode {
109 @Specialization(guards = "value == 1")
110 static String do1(byte value) {
111 return "do1";
112 }
113
114 @Specialization
115 static String do2(byte value) {
116 return "do2";
117 }
118 }
119
120 @Test
121 public void testGuardEqualShortInt() {
122 CallTarget root = createCallTarget(GuardEqualShortIntTestFactory.getInstance());
123 assertEquals("do1", root.call((short) 1));
124 assertEquals("do2", root.call((short) 2));
125 assertEquals("do1", root.call((short) 1));
126 }
127
128 @NodeChild
129 static class GuardEqualShortIntTest extends ValueNode {
130 @Specialization(guards = "value == 1")
131 static String do1(short value) {
132 return "do1";
133 }
134
135 @Specialization
136 static String do2(short value) {
137 return "do2";
138 }
139 }
140
141 @Test
142 public void testGuardEqualLongInt() {
143 CallTarget root = createCallTarget(GuardEqualLongIntTestFactory.getInstance());
144 assertEquals("do1", root.call(1));
145 assertEquals("do2", root.call(2));
146 assertEquals("do1", root.call(1));
147 }
148
149 @NodeChild
150 static class GuardEqualLongIntTest extends ValueNode {
151 @Specialization(guards = "value == 1")
152 static String do1(long value) {
153 return "do1";
154 }
155
156 @Specialization
157 static String do2(long value) {
158 return "do2";
159 }
160 }
161
162 @Test
163 public void testGuardLessEqual() {
164 CallTarget root = createCallTarget(GuardLessEqualTestFactory.getInstance());
165 assertEquals("do1", root.call(1));
166 assertEquals("do1", root.call(0));
167 assertEquals("do2", root.call(2));
168 assertEquals("do1", root.call(0));
169 }
170
171 @NodeChild
172 static class GuardLessEqualTest extends ValueNode {
173 @Specialization(guards = "value <= 1")
174 static String do1(int value) {
175 return "do1";
176 }
177
178 @Specialization
179 static String do2(int value) {
180 return "do2";
181 }
182 }
183
184 @Test
185 public void testGuardLess() {
186 CallTarget root = createCallTarget(GuardLessTestFactory.getInstance());
187 assertEquals("do1", root.call(0));
188 assertEquals("do2", root.call(1));
189 assertEquals("do2", root.call(2));
190 assertEquals("do1", root.call(-1));
191 }
192
193 @NodeChild
194 static class GuardLessTest extends ValueNode {
195 @Specialization(guards = "value < 1")
196 static String do1(int value) {
197 return "do1";
198 }
199
200 @Specialization
201 static String do2(int value) {
202 return "do2";
203 }
204 }
205
206 @Test
207 public void testGuardGreaterEqual() {
208 CallTarget root = createCallTarget(GuardGreaterEqualTestFactory.getInstance());
209 assertEquals("do1", root.call(1));
210 assertEquals("do2", root.call(0));
211 assertEquals("do1", root.call(2));
212 assertEquals("do2", root.call(0));
213 }
214
215 @NodeChild
216 static class GuardGreaterEqualTest extends ValueNode {
217 @Specialization(guards = "value >= 1")
218 static String do1(int value) {
219 return "do1";
220 }
221
222 @Specialization
223 static String do2(int value) {
224 return "do2";
225 }
226 }
227
228 @Test
229 public void testGuardGreater() {
230 CallTarget root = createCallTarget(GuardGreaterTestFactory.getInstance());
231 assertEquals("do1", root.call(2));
232 assertEquals("do2", root.call(0));
233 assertEquals("do2", root.call(1));
234 assertEquals("do2", root.call(0));
235 }
236
237 @NodeChild
238 static class GuardGreaterTest extends ValueNode {
239 @Specialization(guards = "value > 1")
240 static String do1(int value) {
241 return "do1";
242 }
243
244 @Specialization
245 static String do2(int value) {
246 return "do2";
247 }
248 }
249
250 @Test
251 public void testGuardOr() {
252 CallTarget root = createCallTarget(GuardOrTestFactory.getInstance());
253 assertEquals("do1", root.call(1));
254 assertEquals("do1", root.call(0));
255 assertEquals("do2", root.call(2));
256 assertEquals("do2", root.call(-1));
257 }
258
259 @NodeChild
260 static class GuardOrTest extends ValueNode {
261 @Specialization(guards = "value == 1 || value == 0")
262 static String do1(int value) {
263 return "do1";
264 }
265
266 @Specialization
267 static String do2(int value) {
268 return "do2";
269 }
270 }
271
272 @Test
273 public void testGuardNot() {
274 CallTarget root = createCallTarget(GuardNotTestFactory.getInstance());
275 assertEquals("do1", root.call(0));
276 assertEquals("do1", root.call(2));
277 assertEquals("do2", root.call(1));
278 assertEquals("do1", root.call(0));
279 }
280
281 @NodeChild
282 static class GuardNotTest extends ValueNode {
283 @Specialization(guards = "!(value == 1)")
284 static String do1(int value) {
285 return "do1";
286 }
287
288 @Specialization
289 static String do2(int value) {
290 return "do2";
291 }
292 }
293
294 @Test
295 public void testGuardField() {
296 CallTarget root = createCallTarget(GuardFieldTestFactory.getInstance());
297 GuardFieldTest node = getNode(root);
298 node.field = true;
299 assertEquals("do1", root.call(0));
300 assertEquals("do1", root.call(2));
301
302 node.field = false;
303 try {
304 root.call(2);
305 fail("expected Assertion failed");
306 } catch (AssertionError e) {
307 }
308 }
309
310 @NodeChild
311 static class GuardFieldTest extends ValueNode {
312
313 boolean field;
314
315 @Specialization(guards = "field")
316 static String do1(int value) {
317 return "do1";
318 }
319
320 @Specialization
321 static String do2(int value) {
322 return "do2";
323 }
324 }
325
326 @Test
327 public void testGuardCompareWithField() {
328 CallTarget root = createCallTarget(GuardCompareWithFieldTestFactory.getInstance());
329 GuardCompareWithFieldTest node = getNode(root);
330 node.field = 1;
331 assertEquals("do1", root.call(1));
332 assertEquals("do2", root.call(2));
333
334 node.field = 2;
335 assertEquals("do2", root.call(1));
336 assertEquals("do1", root.call(2));
337 }
338
339 @NodeChild
340 static class GuardCompareWithFieldTest extends ValueNode {
341
342 int field;
343
344 @Specialization(guards = "value == field")
345 static String do1(int value) {
346 return "do1";
347 }
348
349 @Specialization
350 static String do2(int value) {
351 return "do2";
352 }
353 }
354
355 @Test
356 public void testGuardStaticField() {
357 CallTarget root = createCallTarget(GuardStaticFieldTestFactory.getInstance());
358 GuardStaticFieldTest.field = true;
359 assertEquals("do1", root.call(1));
360 assertEquals("do1", root.call(2));
361 GuardStaticFieldTest.field = false;
362 try {
363 root.call(2);
364 fail("expected Assertion failed");
365 } catch (AssertionError e) {
366 }
367 }
368
369 @NodeChild
370 static class GuardStaticFieldTest extends ValueNode {
371
372 static boolean field;
373
374 @Specialization(guards = "field")
375 static String do1(int value) {
376 return "do1";
377 }
378
379 @Specialization
380 static String do2(int value) {
381 return "do2";
382 }
383 }
384
385 @Test
386 public void testGuardStaticFinalFieldCompare() {
387 CallTarget root = createCallTarget(GuardStaticFinalFieldCompareTestFactory.getInstance());
388 GuardStaticFieldTest.field = true;
389 assertEquals("do1", root.call(1));
390 assertEquals("do2", root.call(2));
391 }
392
393 @NodeChild
394 static class GuardStaticFinalFieldCompareTest extends ValueNode {
395
396 protected static final int FIELD = 1;
397
398 @Specialization(guards = "value == FIELD")
399 static String do1(int value) {
400 return "do1";
401 }
402
403 @Specialization(guards = "value != FIELD")
404 static String do2(int value) {
405 return "do2";
406 }
407 }
408
409 @Test
410 public void testGuardMethod() {
411 CallTarget root = createCallTarget(GuardMethodTestFactory.getInstance());
412 assertEquals("do1", root.call(1));
413 assertEquals("do2", root.call(2));
414 assertEquals("do1", root.call(1));
415 assertEquals("do2", root.call(0));
416 }
417
418 @NodeChild
419 static class GuardMethodTest extends ValueNode {
420
421 @Specialization(guards = "method(value)")
422 static String do1(int value) {
423 return "do1";
424 }
425
426 @Specialization
427 static String do2(int value) {
428 return "do2";
429 }
430
431 boolean method(int value) {
432 return value == 1;
433 }
434 }
435
436 @Test
437 public void testGuardUnboundMethodField() {
438 CallTarget root = createCallTarget(GuardUnboundMethodTestFactory.getInstance());
439 GuardUnboundMethodTest node = getNode(root);
440 node.hiddenValue = true;
441 assertEquals("do1", root.call(1));
442 assertEquals("do1", root.call(2));
443 node.hiddenValue = false;
444 try {
445 root.call(2);
446 fail("expected Assertion failed");
447 } catch (AssertionError e) {
448 }
449 }
450
451 @NodeChild
452 static class GuardUnboundMethodTest extends ValueNode {
453
454 private boolean hiddenValue;
455
456 @Specialization(guards = "method()")
457 static String do1(int value) {
458 return "do1";
459 }
460
461 boolean method() {
462 return hiddenValue;
463 }
464 }
465
466 @Test
467 public void testStaticGuardMethod() {
468 CallTarget root = createCallTarget(GuardMethodTestFactory.getInstance());
469 assertEquals("do1", root.call(1));
470 assertEquals("do2", root.call(2));
471 assertEquals("do1", root.call(1));
472 assertEquals("do2", root.call(0));
473 }
474
475 @NodeChild
476 static class StaticGuardMethodTest extends ValueNode {
477
478 @Specialization(guards = "method(value)")
479 static String do1(int value) {
480 return "do1";
481 }
482
483 @Specialization
484 static String do2(int value) {
485 return "do2";
486 }
487
488 static boolean method(int value) {
489 return value == 1;
490 }
491 }
492
493 @Test
494 public void testMultipleGuardAndMethod() {
495 CallTarget root = createCallTarget(GuardMultipleAndMethodTestFactory.getInstance());
496 assertEquals("do1", root.call(1));
497 assertEquals("do1", root.call(2));
498 assertEquals("do2", root.call(3));
499 assertEquals("do2", root.call(0));
500 }
501
502 @NodeChild
503 static class GuardMultipleAndMethodTest extends ValueNode {
504
505 @Specialization(guards = {"method1(value)", "method2(value)"})
506 static String do1(int value) {
507 return "do1";
508 }
509
510 @Specialization
511 static String do2(int value) {
512 return "do2";
513 }
514
515 boolean method1(int value) {
516 return value >= 1;
517 }
518
519 boolean method2(int value) {
520 return value <= 2;
521 }
522 }
523
524 @Test
525 public void testMultipleGuardOrMethod() {
526 CallTarget root = createCallTarget(GuardMultipleOrMethodTestFactory.getInstance());
527 assertEquals("do1", root.call(1));
528 assertEquals("do1", root.call(2));
529 assertEquals("do2", root.call(3));
530 assertEquals("do2", root.call(0));
531 }
532
533 @NodeChild
534 static class GuardMultipleOrMethodTest extends ValueNode {
535
536 @Specialization(guards = {"method1(value) || method2(value)"})
537 static String do1(int value) {
538 return "do1";
539 }
540
541 @Specialization
542 static String do2(int value) {
543 return "do2";
544 }
545
546 boolean method1(int value) {
547 return value == 1;
548 }
549
550 boolean method2(int value) {
551 return value == 2;
552 }
553 }
554
555 @Test
556 public void testComplexGuard() {
557 CallTarget root = createCallTarget(GuardComplexTestFactory.getInstance());
558 assertEquals("do1", root.call(1));
559 assertEquals("do1", root.call(2));
560 assertEquals("do2", root.call(3));
561 assertEquals("do1", root.call(0));
562 }
563
564 @NodeChild
565 static class GuardComplexTest extends ValueNode {
566
567 int field1 = 1;
568 static int field2 = 2;
569
570 @Specialization(guards = {"method2(method1(field1 == 1), value <= 2)", "field2 == 2"})
571 static String do1(int value) {
572 return "do1";
573 }
574
575 @Specialization
576 static String do2(int value) {
577 return "do2";
578 }
579
580 static boolean method1(boolean value) {
581 return value;
582 }
583
584 boolean method2(boolean value1, boolean value2) {
585 return value1 && value2;
586 }
587 }
588
589 @NodeChild
590 static class ErrorGuardNotTest extends ValueNode {
591 @ExpectError("Error parsing expression '!value == 1': The operator ! is undefined for the argument type int.")
592 @Specialization(guards = "!value == 1")
593 static String do1(int value) {
594 return "do1";
595 }
596 }
597
598 @NodeChild
599 static class ErrorIncompatibleReturnTypeTest extends ValueNode {
600
601 @ExpectError("Incompatible return type int. Guards must return boolean.")
602 @Specialization(guards = "1")
603 static String do1(int value) {
604 return "do1";
605 }
606
607 }
608
609 }