comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedTest.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/CachedTest.java@b1530a6cce8c
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.CachedTestFactory.BoundCacheFactory;
33 import com.oracle.truffle.api.dsl.test.CachedTestFactory.BoundCacheOverflowFactory;
34 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestBoundCacheOverflowContainsFactory;
35 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheFieldFactory;
36 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheMethodFactory;
37 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheNodeFieldFactory;
38 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithCachedAndDynamicParameterFactory;
39 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithJustCachedParameterFactory;
40 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestMultipleCachesFactory;
41 import com.oracle.truffle.api.dsl.test.CachedTestFactory.UnboundCacheFactory;
42 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
43
44 @SuppressWarnings("unused")
45 public class CachedTest {
46
47 @Test
48 public void testUnboundCache() {
49 CallTarget root = createCallTarget(UnboundCacheFactory.getInstance());
50 assertEquals(42, root.call(42));
51 assertEquals(42, root.call(43));
52 assertEquals(42, root.call(44));
53 }
54
55 @NodeChild
56 static class UnboundCache extends ValueNode {
57 @Specialization
58 static int do1(int value, @Cached("value") int cachedValue) {
59 return cachedValue;
60 }
61 }
62
63 @Test
64 public void testBoundCache() {
65 CallTarget root = createCallTarget(BoundCacheFactory.getInstance());
66 assertEquals(42, root.call(42));
67 assertEquals(43, root.call(43));
68 assertEquals(44, root.call(44));
69 try {
70 root.call(45);
71 fail();
72 } catch (UnsupportedSpecializationException e) {
73 }
74 }
75
76 @NodeChild
77 static class BoundCache extends ValueNode {
78
79 @Specialization(guards = "value == cachedValue", limit = "3")
80 static int do1(int value, @Cached("value") int cachedValue) {
81 return cachedValue;
82 }
83
84 }
85
86 @Test
87 public void testBoundCacheOverflow() {
88 CallTarget root = createCallTarget(BoundCacheOverflowFactory.getInstance());
89 assertEquals(42, root.call(42));
90 assertEquals(43, root.call(43));
91 assertEquals(-1, root.call(44));
92 assertEquals(42, root.call(42));
93 assertEquals(43, root.call(43));
94 assertEquals(-1, root.call(44));
95 }
96
97 @NodeChild
98 static class BoundCacheOverflow extends ValueNode {
99
100 @Specialization(guards = "value == cachedValue", limit = "2")
101 static int do1(int value, @Cached("value") int cachedValue) {
102 return cachedValue;
103 }
104
105 @Specialization
106 static int do2(int value) {
107 return -1;
108 }
109
110 }
111
112 @Test
113 public void testBoundCacheOverflowContains() {
114 CallTarget root = createCallTarget(TestBoundCacheOverflowContainsFactory.getInstance());
115 assertEquals(42, root.call(42));
116 assertEquals(43, root.call(43));
117 assertEquals(-1, root.call(44));
118 assertEquals(-1, root.call(42));
119 assertEquals(-1, root.call(43));
120 assertEquals(-1, root.call(44));
121 }
122
123 @NodeChild
124 static class TestBoundCacheOverflowContains extends ValueNode {
125
126 @Specialization(guards = "value == cachedValue", limit = "2")
127 static int do1(int value, @Cached("value") int cachedValue) {
128 return cachedValue;
129 }
130
131 @Specialization(contains = "do1")
132 static int do2(int value) {
133 return -1;
134 }
135
136 }
137
138 @Test
139 public void testCacheField() {
140 CallTarget root = createCallTarget(TestCacheFieldFactory.getInstance());
141 assertEquals(3, root.call(42));
142 assertEquals(3, root.call(43));
143 }
144
145 @NodeChild
146 static class TestCacheField extends ValueNode {
147
148 protected int field = 3;
149
150 @Specialization()
151 static int do1(int value, @Cached("field") int cachedValue) {
152 return cachedValue;
153 }
154
155 }
156
157 @Test
158 public void testCacheNodeField() {
159 CallTarget root = createCallTarget(TestCacheNodeFieldFactory.getInstance(), 21);
160 assertEquals(21, root.call(42));
161 assertEquals(21, root.call(43));
162 }
163
164 @NodeChild
165 @NodeField(name = "field", type = int.class)
166 static class TestCacheNodeField extends ValueNode {
167
168 @Specialization
169 static int do1(int value, @Cached("field") int cachedValue) {
170 return cachedValue;
171 }
172
173 }
174
175 @Test
176 public void testCacheMethod() {
177 TestCacheMethod.invocations = 0;
178 CallTarget root = createCallTarget(TestCacheMethodFactory.getInstance());
179 assertEquals(42, root.call(42));
180 assertEquals(42, root.call(43));
181 assertEquals(42, root.call(44));
182 assertEquals(1, TestCacheMethod.invocations);
183 }
184
185 @NodeChild
186 static class TestCacheMethod extends ValueNode {
187
188 static int invocations = 0;
189
190 @Specialization
191 static int do1(int value, @Cached("someMethod(value)") int cachedValue) {
192 return cachedValue;
193 }
194
195 static int someMethod(int value) {
196 invocations++;
197 return value;
198 }
199
200 }
201
202 @Test
203 public void testGuardWithJustCachedParameter() {
204 TestGuardWithJustCachedParameter.invocations = 0;
205 CallTarget root = createCallTarget(TestGuardWithJustCachedParameterFactory.getInstance());
206 assertEquals(42, root.call(42));
207 assertEquals(42, root.call(43));
208 assertEquals(42, root.call(44));
209 // guards with just cached parameters are just invoked on the slow path
210 assertEquals(assertionsEnabled() ? 4 : 1, TestGuardWithJustCachedParameter.invocations);
211 }
212
213 @NodeChild
214 static class TestGuardWithJustCachedParameter extends ValueNode {
215
216 static int invocations = 0;
217
218 @Specialization(guards = "someMethod(cachedValue)")
219 static int do1(int value, @Cached("value") int cachedValue) {
220 return cachedValue;
221 }
222
223 static boolean someMethod(int value) {
224 invocations++;
225 return true;
226 }
227
228 }
229
230 @Test
231 public void testGuardWithCachedAndDynamicParameter() {
232 TestGuardWithCachedAndDynamicParameter.cachedMethodInvocations = 0;
233 TestGuardWithCachedAndDynamicParameter.dynamicMethodInvocations = 0;
234 CallTarget root = createCallTarget(TestGuardWithCachedAndDynamicParameterFactory.getInstance());
235 assertEquals(42, root.call(42));
236 assertEquals(42, root.call(43));
237 assertEquals(42, root.call(44));
238 // guards with just cached parameters are just invoked on the slow path
239 assertEquals(assertionsEnabled() ? 4 : 1, TestGuardWithCachedAndDynamicParameter.cachedMethodInvocations);
240 assertEquals(4, TestGuardWithCachedAndDynamicParameter.dynamicMethodInvocations);
241 }
242
243 @NodeChild
244 static class TestGuardWithCachedAndDynamicParameter extends ValueNode {
245
246 static int cachedMethodInvocations = 0;
247 static int dynamicMethodInvocations = 0;
248
249 @Specialization(guards = {"dynamicMethod(value)", "cachedMethod(cachedValue)"})
250 static int do1(int value, @Cached("value") int cachedValue) {
251 return cachedValue;
252 }
253
254 static boolean cachedMethod(int value) {
255 cachedMethodInvocations++;
256 return true;
257 }
258
259 static boolean dynamicMethod(int value) {
260 dynamicMethodInvocations++;
261 return true;
262 }
263
264 }
265
266 /*
267 * Node should not produce any warnings in isIdentical of the generated code. Unnecessary casts
268 * were generated for isIdentical on the fast path.
269 */
270 @NodeChildren({@NodeChild, @NodeChild})
271 static class RegressionTestWarningInIsIdentical extends ValueNode {
272
273 @Specialization(guards = {"cachedName == name"})
274 protected Object directAccess(String receiver, String name, //
275 @Cached("name") String cachedName, //
276 @Cached("create(receiver, name)") Object callHandle) {
277 return receiver;
278 }
279
280 protected static Object create(String receiver, String name) {
281 return receiver;
282 }
283
284 }
285
286 @NodeChild
287 static class TestMultipleCaches extends ValueNode {
288
289 @Specialization
290 static int do1(int value, @Cached("value") int cachedValue1, @Cached("value") int cachedValue2) {
291 return cachedValue1 + cachedValue2;
292 }
293
294 }
295
296 @Test
297 public void testMultipleCaches() {
298 CallTarget root = createCallTarget(TestMultipleCachesFactory.getInstance());
299 assertEquals(42, root.call(21));
300 assertEquals(42, root.call(22));
301 assertEquals(42, root.call(23));
302 }
303
304 @NodeChild
305 static class CachedError1 extends ValueNode {
306 @Specialization
307 static int do1(int value, @ExpectError("Incompatible return type int. The expression type must be equal to the parameter type double.")//
308 @Cached("value") double cachedValue) {
309 return value;
310 }
311 }
312
313 @NodeChild
314 static class CachedError2 extends ValueNode {
315
316 // caches are not allowed to make backward references
317
318 @Specialization
319 static int do1(int value,
320 @ExpectError("The initializer expression of parameter 'cachedValue1' binds unitialized parameter 'cachedValue2. Reorder the parameters to resolve the problem.") @Cached("cachedValue2") int cachedValue1,
321 @Cached("value") int cachedValue2) {
322 return cachedValue1 + cachedValue2;
323 }
324
325 }
326
327 @NodeChild
328 static class CachedError3 extends ValueNode {
329
330 // cyclic dependency between cached expressions
331 @Specialization
332 static int do1(int value,
333 @ExpectError("The initializer expression of parameter 'cachedValue1' binds unitialized parameter 'cachedValue2. Reorder the parameters to resolve the problem.") @Cached("cachedValue2") int cachedValue1,
334 @Cached("cachedValue1") int cachedValue2) {
335 return cachedValue1 + cachedValue2;
336 }
337
338 }
339
340 }