Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/GuardsTest.java @ 11439:efe58aa92f86
Truffle-DSL: guards can now be declared using any base type or interface of the target value type.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 27 Aug 2013 22:08:26 +0200 |
parents | 79041ab43660 |
children | d6a5ab791b0d |
comparison
equal
deleted
inserted
replaced
11438:b77721210bd6 | 11439:efe58aa92f86 |
---|---|
27 | 27 |
28 import org.junit.*; | 28 import org.junit.*; |
29 | 29 |
30 import com.oracle.truffle.api.dsl.*; | 30 import com.oracle.truffle.api.dsl.*; |
31 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GlobalFlagGuardFactory; | 31 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GlobalFlagGuardFactory; |
32 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GuardWithBaseClassFactory; | |
33 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GuardWithBaseInterfaceFactory; | |
34 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GuardWithBoxedPrimitiveFactory; | |
35 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.GuardWithObjectFactory; | |
32 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.InvocationGuardFactory; | 36 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.InvocationGuardFactory; |
37 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.TestGuardResolve1Factory; | |
38 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.TestGuardResolve2Factory; | |
39 import com.oracle.truffle.api.dsl.test.GuardsTestFactory.TestGuardResolve3Factory; | |
40 import com.oracle.truffle.api.dsl.test.NodeContainerTest.Str; | |
41 import com.oracle.truffle.api.dsl.test.NodeContainerTest.StrBase; | |
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | 42 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; |
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | 43 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; |
35 | 44 |
36 @SuppressWarnings("unused") | 45 @SuppressWarnings("unused") |
37 public class GuardsTest { | 46 public class GuardsTest { |
105 int doGeneric(Object value0) { | 114 int doGeneric(Object value0) { |
106 return 42; // the generic answer to all questions | 115 return 42; // the generic answer to all questions |
107 } | 116 } |
108 } | 117 } |
109 | 118 |
119 @Test | |
120 public void testGuardWithBaseClass() { | |
121 TestRootNode<?> root = createRoot(GuardWithBaseClassFactory.getInstance()); | |
122 | |
123 assertEquals(42, executeWith(root, new Str("42"))); | |
124 } | |
125 | |
126 @NodeChild("expression") | |
127 public abstract static class GuardWithBaseClass extends ValueNode { | |
128 | |
129 boolean baseGuard(StrBase base) { | |
130 return true; | |
131 } | |
132 | |
133 @Specialization(guards = "baseGuard") | |
134 int doSpecialized(Str value0) { | |
135 return 42; | |
136 } | |
137 } | |
138 | |
139 @Test | |
140 public void testGuardWithBaseInterface() { | |
141 TestRootNode<?> root = createRoot(GuardWithBaseInterfaceFactory.getInstance()); | |
142 | |
143 assertEquals(42, executeWith(root, "anything")); | |
144 } | |
145 | |
146 @NodeChild("expression") | |
147 public abstract static class GuardWithBaseInterface extends ValueNode { | |
148 | |
149 boolean baseGuard(CharSequence base) { | |
150 return true; | |
151 } | |
152 | |
153 @Specialization(guards = "baseGuard") | |
154 int doSpecialized(String value0) { | |
155 return 42; | |
156 } | |
157 } | |
158 | |
159 @Test | |
160 public void testGuardWithPrimitive() { | |
161 TestRootNode<?> root = createRoot(GuardWithBoxedPrimitiveFactory.getInstance()); | |
162 | |
163 assertEquals(42, executeWith(root, 42)); | |
164 } | |
165 | |
166 @NodeChild("expression") | |
167 public abstract static class GuardWithBoxedPrimitive extends ValueNode { | |
168 | |
169 boolean baseGuard(Integer primitive) { | |
170 return true; | |
171 } | |
172 | |
173 @Specialization(guards = "baseGuard") | |
174 int doSpecialized(int value0) { | |
175 return value0; | |
176 } | |
177 } | |
178 | |
179 @Test | |
180 public void testGuardWithObject() { | |
181 TestRootNode<?> root = createRoot(GuardWithObjectFactory.getInstance()); | |
182 | |
183 assertEquals(42, executeWith(root, 42)); | |
184 } | |
185 | |
186 @NodeChild("expression") | |
187 public abstract static class GuardWithObject extends ValueNode { | |
188 | |
189 boolean baseGuard(Object primitive) { | |
190 return true; | |
191 } | |
192 | |
193 @Specialization(guards = "baseGuard") | |
194 int doSpecialized(int value0) { | |
195 return value0; | |
196 } | |
197 } | |
198 | |
199 @Test | |
200 public void testGuardResolve1() { | |
201 TestRootNode<?> root = createRoot(TestGuardResolve1Factory.getInstance()); | |
202 | |
203 assertEquals(42, executeWith(root, 42)); | |
204 } | |
205 | |
206 @NodeChild("expression") | |
207 public abstract static class TestGuardResolve1 extends ValueNode { | |
208 | |
209 boolean guard(Object primitive) { | |
210 return false; | |
211 } | |
212 | |
213 boolean guard(int primitive) { | |
214 return true; | |
215 } | |
216 | |
217 @Specialization(guards = "guard") | |
218 int doSpecialized(int value0) { | |
219 return value0; | |
220 } | |
221 } | |
222 | |
223 @Test | |
224 public void testGuardResolve2() { | |
225 TestRootNode<?> root = createRoot(TestGuardResolve2Factory.getInstance()); | |
226 assertEquals(42, executeWith(root, new Str(""))); | |
227 } | |
228 | |
229 @NodeChild("expression") | |
230 public abstract static class TestGuardResolve2 extends ValueNode { | |
231 | |
232 boolean guard(Object primitive) { | |
233 return false; | |
234 } | |
235 | |
236 boolean guard(StrBase primitive) { | |
237 return true; | |
238 } | |
239 | |
240 @Specialization(guards = "guard") | |
241 int doSpecialized(Str value0) { | |
242 return 42; | |
243 } | |
244 } | |
245 | |
246 @Test | |
247 public void testGuardResolve3() { | |
248 TestRootNode<?> root = createRoot(TestGuardResolve3Factory.getInstance()); | |
249 | |
250 assertEquals(42, executeWith(root, new Str(""))); | |
251 } | |
252 | |
253 @NodeChild("expression") | |
254 public abstract static class TestGuardResolve3 extends ValueNode { | |
255 | |
256 boolean guard(Object primitive) { | |
257 return false; | |
258 } | |
259 | |
260 boolean guard(StrBase primitive) { | |
261 return false; | |
262 } | |
263 | |
264 boolean guard(Str primitive) { | |
265 return true; | |
266 } | |
267 | |
268 @Specialization(guards = "guard") | |
269 int doSpecialized(Str value0) { | |
270 return 42; | |
271 } | |
272 } | |
273 | |
110 } | 274 } |