Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ImplicitCastTest.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/ImplicitCastTest.java@93016f2f3f16 |
children | dc83cc1f94f2 |
comparison
equal
deleted
inserted
replaced
21950:2a5011c7e641 | 21951:9c8c0937da41 |
---|---|
1 /* | |
2 * Copyright (c) 2012, 2013, 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 org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.dsl.*; | |
28 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast0NodeFactory; | |
29 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast1NodeFactory; | |
30 import com.oracle.truffle.api.dsl.test.ImplicitCastTestFactory.ImplicitCast2NodeFactory; | |
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
32 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
33 import com.oracle.truffle.api.frame.*; | |
34 | |
35 public class ImplicitCastTest { | |
36 | |
37 @TypeSystem({int.class, String.class, boolean.class}) | |
38 static class ImplicitCast0Types { | |
39 | |
40 @ImplicitCast | |
41 static boolean castInt(int intvalue) { | |
42 return intvalue == 1 ? true : false; | |
43 } | |
44 | |
45 @ImplicitCast | |
46 static boolean castString(String strvalue) { | |
47 return strvalue.equals("1"); | |
48 } | |
49 | |
50 } | |
51 | |
52 @TypeSystemReference(ImplicitCast0Types.class) | |
53 @NodeChild(value = "operand", type = ImplicitCast0Node.class) | |
54 abstract static class ImplicitCast0Node extends ValueNode { | |
55 | |
56 public abstract Object executeEvaluated(VirtualFrame frame, Object value2); | |
57 | |
58 @Specialization | |
59 public String op1(String value) { | |
60 return value; | |
61 } | |
62 | |
63 @Specialization | |
64 public boolean op1(boolean value) { | |
65 return value; | |
66 } | |
67 | |
68 } | |
69 | |
70 @Test | |
71 public void testImplicitCast0() { | |
72 ImplicitCast0Node node = ImplicitCast0NodeFactory.create(null); | |
73 TestRootNode<ImplicitCast0Node> root = new TestRootNode<>(node); | |
74 root.adoptChildren(); | |
75 Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); | |
76 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
77 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | |
78 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
79 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | |
80 } | |
81 | |
82 @TypeSystemReference(ImplicitCast0Types.class) | |
83 @NodeChild(value = "operand", type = ImplicitCast1Node.class) | |
84 abstract static class ImplicitCast1Node extends ValueNode { | |
85 | |
86 public abstract Object executeEvaluated(VirtualFrame frame, Object operand); | |
87 | |
88 @Specialization | |
89 public String op0(String value) { | |
90 return value; | |
91 } | |
92 | |
93 @Specialization(rewriteOn = RuntimeException.class) | |
94 public boolean op1(@SuppressWarnings("unused") boolean value) throws RuntimeException { | |
95 throw new RuntimeException(); | |
96 } | |
97 | |
98 @Specialization(contains = "op1") | |
99 public boolean op2(boolean value) { | |
100 return value; | |
101 } | |
102 | |
103 } | |
104 | |
105 @Test | |
106 public void testImplicitCast1() { | |
107 ImplicitCast1Node node = ImplicitCast1NodeFactory.create(null); | |
108 TestRootNode<ImplicitCast1Node> root = new TestRootNode<>(node); | |
109 root.adoptChildren(); | |
110 Assert.assertEquals("2", root.getNode().executeEvaluated(null, "2")); | |
111 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
112 Assert.assertEquals("1", root.getNode().executeEvaluated(null, "1")); | |
113 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1)); | |
114 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true)); | |
115 } | |
116 | |
117 @TypeSystemReference(ImplicitCast0Types.class) | |
118 @NodeChildren({@NodeChild(value = "operand0", type = ImplicitCast2Node.class), @NodeChild(value = "operand1", type = ImplicitCast2Node.class, executeWith = "operand0")}) | |
119 // TODO temporary workaround | |
120 abstract static class ImplicitCast2Node extends ValueNode { | |
121 | |
122 @Specialization | |
123 public String op0(String v0, String v1) { | |
124 return v0 + v1; | |
125 } | |
126 | |
127 @SuppressWarnings("unused") | |
128 @Specialization(rewriteOn = RuntimeException.class) | |
129 public boolean op1(boolean v0, boolean v1) throws RuntimeException { | |
130 throw new RuntimeException(); | |
131 } | |
132 | |
133 @Specialization(contains = "op1") | |
134 public boolean op2(boolean v0, boolean v1) { | |
135 return v0 && v1; | |
136 } | |
137 | |
138 public abstract Object executeEvaluated(VirtualFrame frame, Object v1); | |
139 | |
140 public abstract Object executeEvaluated(VirtualFrame frame, Object v1, Object v2); | |
141 | |
142 public abstract Object executeEvaluated(VirtualFrame frame, boolean v1, boolean v2); | |
143 | |
144 } | |
145 | |
146 @Test | |
147 public void testImplicitCast2() { | |
148 ImplicitCast2Node node = ImplicitCast2NodeFactory.create(null, null); | |
149 TestRootNode<ImplicitCast2Node> root = new TestRootNode<>(node); | |
150 root.adoptChildren(); | |
151 Assert.assertEquals("42", root.getNode().executeEvaluated(null, "4", "2")); | |
152 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1, 1)); | |
153 Assert.assertEquals("42", root.getNode().executeEvaluated(null, "4", "2")); | |
154 Assert.assertEquals(true, root.getNode().executeEvaluated(null, 1, 1)); | |
155 Assert.assertEquals(true, root.getNode().executeEvaluated(null, true, true)); | |
156 } | |
157 | |
158 @TypeSystem({String.class, boolean.class}) | |
159 static class ImplicitCastError1 { | |
160 | |
161 @ImplicitCast | |
162 @ExpectError("Target type and source type of an @ImplicitCast must not be the same type.") | |
163 static String castInvalid(@SuppressWarnings("unused") String value) { | |
164 throw new AssertionError(); | |
165 } | |
166 | |
167 } | |
168 | |
169 @TypeSystem({String.class, boolean.class}) | |
170 static class ImplicitCastError2 { | |
171 | |
172 @ImplicitCast | |
173 @ExpectError("Target type and source type of an @ImplicitCast must not be the same type.") | |
174 static String castInvalid(@SuppressWarnings("unused") String value) { | |
175 throw new AssertionError(); | |
176 } | |
177 | |
178 } | |
179 | |
180 } |