Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MethodGuardsTest.java @ 16756:5148aab962af
Truffle-DSL: updated tests for the new generation layout.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Aug 2014 15:53:05 +0200 |
parents | |
children | 9f38d222fa6c |
comparison
equal
deleted
inserted
replaced
16755:bd28da642eea | 16756:5148aab962af |
---|---|
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.dsl.*; | |
31 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GlobalFlagGuardFactory; | |
32 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardWithBaseClassFactory; | |
33 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardWithBoxedPrimitiveFactory; | |
34 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.GuardWithObjectFactory; | |
35 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.InvocationGuardFactory; | |
36 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.TestAbstractGuard1Factory; | |
37 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.TestGuardResolve1Factory; | |
38 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.TestGuardResolve2Factory; | |
39 import com.oracle.truffle.api.dsl.test.MethodGuardsTestFactory.TestGuardResolve3Factory; | |
40 import com.oracle.truffle.api.dsl.test.TypeSystemTest.Abstract; | |
41 import com.oracle.truffle.api.dsl.test.TypeSystemTest.BExtendsAbstract; | |
42 import com.oracle.truffle.api.dsl.test.TypeSystemTest.CExtendsAbstract; | |
43 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | |
44 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | |
45 | |
46 @SuppressWarnings("unused") | |
47 public class MethodGuardsTest { | |
48 | |
49 private static final Object NULL = new Object(); | |
50 | |
51 @Test | |
52 public void testGuardInvocations() { | |
53 TestRootNode<InvocationGuard> root = createRoot(InvocationGuardFactory.getInstance()); | |
54 | |
55 assertEquals(Integer.MAX_VALUE, executeWith(root, Integer.MAX_VALUE - 1, 1)); | |
56 assertEquals(1, InvocationGuard.specializedInvocations); | |
57 assertEquals(0, InvocationGuard.genericInvocations); | |
58 | |
59 assertEquals(42, executeWith(root, Integer.MAX_VALUE, 1)); | |
60 assertEquals(1, InvocationGuard.specializedInvocations); | |
61 assertEquals(1, InvocationGuard.genericInvocations); | |
62 } | |
63 | |
64 @NodeChildren({@NodeChild("value0"), @NodeChild("value1")}) | |
65 public abstract static class InvocationGuard extends ValueNode { | |
66 | |
67 static int specializedInvocations = 0; | |
68 static int genericInvocations = 0; | |
69 | |
70 boolean guard(int value0, int value1) { | |
71 return value0 != Integer.MAX_VALUE; | |
72 } | |
73 | |
74 @Specialization(guards = "guard") | |
75 int doSpecialized(int value0, int value1) { | |
76 specializedInvocations++; | |
77 return value0 + value1; | |
78 } | |
79 | |
80 @Generic | |
81 int doGeneric(Object value0, Object value1) { | |
82 genericInvocations++; | |
83 return 42; // the generic answer to all questions | |
84 } | |
85 } | |
86 | |
87 @Test | |
88 public void testGuardGlobal() { | |
89 TestRootNode<GlobalFlagGuard> root = createRoot(GlobalFlagGuardFactory.getInstance()); | |
90 | |
91 assertEquals(42, executeWith(root, NULL)); | |
92 | |
93 GlobalFlagGuard.globalFlag = true; | |
94 assertEquals(41, executeWith(root, NULL)); | |
95 | |
96 GlobalFlagGuard.globalFlag = false; | |
97 assertEquals(42, executeWith(root, NULL)); | |
98 } | |
99 | |
100 @NodeChild("expression") | |
101 public abstract static class GlobalFlagGuard extends ValueNode { | |
102 | |
103 static boolean globalFlag = false; | |
104 | |
105 static boolean globalFlagGuard() { | |
106 return globalFlag; | |
107 } | |
108 | |
109 @Specialization(guards = "globalFlagGuard") | |
110 int doSpecialized(Object value0) { | |
111 return 41; | |
112 } | |
113 | |
114 @Generic | |
115 int doGeneric(Object value0) { | |
116 return 42; // the generic answer to all questions | |
117 } | |
118 } | |
119 | |
120 @Test | |
121 public void testGuardWithBaseClass() { | |
122 TestRootNode<?> root = createRoot(GuardWithBaseClassFactory.getInstance()); | |
123 | |
124 assertEquals(42, executeWith(root, new BExtendsAbstract())); | |
125 } | |
126 | |
127 @NodeChild("expression") | |
128 public abstract static class GuardWithBaseClass extends ValueNode { | |
129 | |
130 boolean baseGuard(Abstract base) { | |
131 return true; | |
132 } | |
133 | |
134 @Specialization(guards = "baseGuard") | |
135 int doSpecialized(BExtendsAbstract value0) { | |
136 return 42; | |
137 } | |
138 } | |
139 | |
140 @NodeChild("expression") | |
141 public abstract static class GuardWithBaseInterface extends ValueNode { | |
142 | |
143 boolean baseGuard(CharSequence base) { | |
144 return true; | |
145 } | |
146 | |
147 @Specialization(guards = "baseGuard") | |
148 @ExpectError("No compatible guard with method name 'baseGuard' found. Please note that all signature types of the method guard must be declared in the type system.") | |
149 int doSpecialized(String value0) { | |
150 return 42; | |
151 } | |
152 } | |
153 | |
154 @Test | |
155 public void testGuardWithPrimitive() { | |
156 TestRootNode<?> root = createRoot(GuardWithBoxedPrimitiveFactory.getInstance()); | |
157 | |
158 assertEquals(42, executeWith(root, 42)); | |
159 } | |
160 | |
161 @NodeChild("expression") | |
162 public abstract static class GuardWithBoxedPrimitive extends ValueNode { | |
163 | |
164 boolean baseGuard(Integer primitive) { | |
165 return true; | |
166 } | |
167 | |
168 @Specialization(guards = "baseGuard") | |
169 int doSpecialized(int value0) { | |
170 return value0; | |
171 } | |
172 } | |
173 | |
174 @Test | |
175 public void testGuardWithObject() { | |
176 TestRootNode<?> root = createRoot(GuardWithObjectFactory.getInstance()); | |
177 | |
178 assertEquals(42, executeWith(root, 42)); | |
179 } | |
180 | |
181 @NodeChild("expression") | |
182 public abstract static class GuardWithObject extends ValueNode { | |
183 | |
184 boolean baseGuard(Object primitive) { | |
185 return true; | |
186 } | |
187 | |
188 @Specialization(guards = "baseGuard") | |
189 int doSpecialized(int value0) { | |
190 return value0; | |
191 } | |
192 } | |
193 | |
194 @Test | |
195 public void testGuardResolve1() { | |
196 TestRootNode<?> root = createRoot(TestGuardResolve1Factory.getInstance()); | |
197 | |
198 assertEquals(42, executeWith(root, 42)); | |
199 } | |
200 | |
201 @NodeChild("expression") | |
202 public abstract static class TestGuardResolve1 extends ValueNode { | |
203 | |
204 boolean guard(Object primitive) { | |
205 return false; | |
206 } | |
207 | |
208 boolean guard(int primitive) { | |
209 return true; | |
210 } | |
211 | |
212 @Specialization(guards = "guard") | |
213 int doSpecialized(int value0) { | |
214 return value0; | |
215 } | |
216 } | |
217 | |
218 @Test | |
219 public void testGuardResolve2() { | |
220 TestRootNode<?> root = createRoot(TestGuardResolve2Factory.getInstance()); | |
221 assertEquals(42, executeWith(root, new BExtendsAbstract())); | |
222 } | |
223 | |
224 @NodeChild("expression") | |
225 public abstract static class TestGuardResolve2 extends ValueNode { | |
226 | |
227 boolean guard(Object primitive) { | |
228 return false; | |
229 } | |
230 | |
231 boolean guard(Abstract primitive) { | |
232 return true; | |
233 } | |
234 | |
235 @Specialization(guards = "guard") | |
236 int doSpecialized(BExtendsAbstract value0) { | |
237 return 42; | |
238 } | |
239 } | |
240 | |
241 @Test | |
242 public void testGuardResolve3() { | |
243 TestRootNode<?> root = createRoot(TestGuardResolve3Factory.getInstance()); | |
244 | |
245 assertEquals(42, executeWith(root, new BExtendsAbstract())); | |
246 } | |
247 | |
248 @NodeChild("expression") | |
249 public abstract static class TestGuardResolve3 extends ValueNode { | |
250 | |
251 boolean guard(Object primitive) { | |
252 return false; | |
253 } | |
254 | |
255 boolean guard(Abstract primitive) { | |
256 return false; | |
257 } | |
258 | |
259 boolean guard(BExtendsAbstract primitive) { | |
260 return true; | |
261 } | |
262 | |
263 @Specialization(guards = "guard") | |
264 int doSpecialized(BExtendsAbstract value0) { | |
265 return 42; | |
266 } | |
267 } | |
268 | |
269 @NodeChild("expression") | |
270 public abstract static class TestGuardResolve4 extends ValueNode { | |
271 | |
272 boolean guard(Abstract primitive) { | |
273 return false; | |
274 } | |
275 | |
276 @Specialization(guards = "guard") | |
277 int doSpecialized(BExtendsAbstract value0) { | |
278 return 42; | |
279 } | |
280 } | |
281 | |
282 @Test | |
283 public void testAbstractGuard1() { | |
284 TestRootNode<?> root = createRoot(TestAbstractGuard1Factory.getInstance()); | |
285 | |
286 assertEquals(BExtendsAbstract.INSTANCE, executeWith(root, BExtendsAbstract.INSTANCE)); | |
287 assertEquals(CExtendsAbstract.INSTANCE, executeWith(root, CExtendsAbstract.INSTANCE)); | |
288 } | |
289 | |
290 @NodeChild("expression") | |
291 public abstract static class TestAbstractGuard1 extends ValueNode { | |
292 | |
293 boolean guard(Abstract value0) { | |
294 return true; | |
295 } | |
296 | |
297 @Specialization(guards = "guard") | |
298 BExtendsAbstract do1(BExtendsAbstract value0) { | |
299 return value0; | |
300 } | |
301 | |
302 @Specialization(guards = "guard") | |
303 CExtendsAbstract do2(CExtendsAbstract value0) { | |
304 return value0; | |
305 } | |
306 } | |
307 | |
308 } |