comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/NullLiteralGuardsTest.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/NullLiteralGuardsTest.java@f83fd99b2962
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
1 /*
2 * Copyright (c) 2015, 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.NullLiteralGuardsTestFactory.CompareNotNullNodeFactory;
33 import com.oracle.truffle.api.dsl.test.NullLiteralGuardsTestFactory.CompareObjectsNullNodeFactory;
34 import com.oracle.truffle.api.dsl.test.NullLiteralGuardsTestFactory.CompareStringNullNodeFactory;
35 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ChildrenNode;
36
37 @SuppressWarnings("unused")
38 public class NullLiteralGuardsTest {
39
40 @Test
41 public void testCompareObjectsNull() {
42 CallTarget root = createCallTarget(CompareObjectsNullNodeFactory.getInstance());
43 assertEquals("do1", root.call((Object) null));
44 assertEquals("do2", root.call("42"));
45 }
46
47 abstract static class CompareObjectsNullNode extends ChildrenNode {
48 @Specialization(guards = "value == null")
49 String do1(Object value) {
50 return "do1";
51 }
52
53 @Specialization
54 String do2(Object value) {
55 return "do2";
56 }
57 }
58
59 @Test
60 public void testCompareStringNull() {
61 CallTarget root = createCallTarget(CompareStringNullNodeFactory.getInstance());
62 assertEquals("do1", root.call("42"));
63 assertEquals("do2", root.call((Object) null));
64 }
65
66 abstract static class CompareStringNullNode extends ChildrenNode {
67 @Specialization(guards = "value != null")
68 String do1(String value) {
69 return "do1";
70 }
71
72 @Specialization
73 String do2(Object value) {
74 return "do2";
75 }
76 }
77
78 @Test
79 public void testCompareNotNull() {
80 CallTarget root = createCallTarget(CompareNotNullNodeFactory.getInstance());
81 assertEquals("do1", root.call("42"));
82 assertEquals("do2", root.call((Object) null));
83 }
84
85 abstract static class CompareNotNullNode extends ChildrenNode {
86 @Specialization(guards = "value != null")
87 String do1(Object value) {
88 return "do1";
89 }
90
91 @Specialization
92 String do2(Object value) {
93 return "do2";
94 }
95 }
96
97 abstract static class ErrorNullIntComparison1 extends ChildrenNode {
98 @ExpectError("Error parsing expression 'value == null': Incompatible operand types int and null.")
99 @Specialization(guards = "value == null")
100 String do1(int value) {
101 return "do1";
102 }
103 }
104
105 abstract static class ErrorNullIntComparison2 extends ChildrenNode {
106 @ExpectError("Error parsing expression '1 == null': Incompatible operand types int and null.")
107 @Specialization(guards = "1 == null")
108 String do1(int value) {
109 return "do1";
110 }
111 }
112
113 abstract static class ErrorNullNullComparison extends ChildrenNode {
114 @ExpectError("Error parsing expression 'null == null': The operator == is undefined for the argument type(s) null null.")
115 @Specialization(guards = "null == null")
116 String do1(int value) {
117 return "do1";
118 }
119 }
120
121 abstract static class ErrorObjectVoidComparison extends ChildrenNode {
122 protected static void returnVoid() {
123 }
124
125 @ExpectError("Error parsing expression 'value == returnVoid()': Incompatible operand types Object and void.")
126 @Specialization(guards = "value == returnVoid()")
127 String do1(Object value) {
128 return "do1";
129 }
130 }
131
132 }