comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ExecuteEvaluatedTest.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/ExecuteEvaluatedTest.java@6361fa2e3321
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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 org.junit.*;
26
27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.dsl.*;
29 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.DoubleEvaluatedNodeFactory;
30 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.EvaluatedNodeFactory;
31 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedGenerationFactory;
32 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs0Factory;
33 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs1Factory;
34 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs2Factory;
35 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs3Factory;
36 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs4Factory;
37 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.TestEvaluatedVarArgs5Factory;
38 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.UseDoubleEvaluated1NodeFactory;
39 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.UseDoubleEvaluated2NodeFactory;
40 import com.oracle.truffle.api.dsl.test.ExecuteEvaluatedTestFactory.UseEvaluatedNodeFactory;
41 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode;
42 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ChildrenNode;
43 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
44 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
45 import com.oracle.truffle.api.frame.*;
46 import com.oracle.truffle.api.nodes.*;
47
48 public class ExecuteEvaluatedTest {
49
50 @Test
51 public void testSingleEvaluated() {
52 ArgumentNode arg0 = new ArgumentNode(0);
53 CallTarget callTarget = TestHelper.createCallTarget(UseEvaluatedNodeFactory.create(arg0, EvaluatedNodeFactory.create(null)));
54
55 Assert.assertEquals(43, callTarget.call(new Object[]{42}));
56 Assert.assertEquals(1, arg0.getInvocationCount());
57 }
58
59 @NodeChild("exp")
60 abstract static class EvaluatedNode extends ValueNode {
61
62 @Specialization
63 int doExecuteWith(int exp) {
64 return exp + 1;
65 }
66
67 public abstract Object executeEvaluated(VirtualFrame frame, Object targetValue);
68
69 public abstract int executeIntEvaluated(VirtualFrame frame, Object targetValue) throws UnexpectedResultException;
70 }
71
72 @NodeChildren({@NodeChild("exp0"), @NodeChild(value = "exp1", type = EvaluatedNode.class, executeWith = "exp0")})
73 abstract static class UseEvaluatedNode extends ValueNode {
74
75 @Specialization
76 int call(int exp0, int exp1) {
77 Assert.assertEquals(exp0 + 1, exp1);
78 return exp1;
79 }
80 }
81
82 @Test
83 public void testDoubleEvaluated1() {
84 ArgumentNode arg0 = new ArgumentNode(0);
85 ArgumentNode arg1 = new ArgumentNode(1);
86 CallTarget callTarget = TestHelper.createCallTarget(UseDoubleEvaluated1NodeFactory.create(arg0, arg1, DoubleEvaluatedNodeFactory.create(null, null)));
87
88 Assert.assertEquals(42, callTarget.call(new Object[]{43, 1}));
89 Assert.assertEquals(1, arg0.getInvocationCount());
90 Assert.assertEquals(1, arg1.getInvocationCount());
91 }
92
93 @NodeChildren({@NodeChild("exp0"), @NodeChild("exp1")})
94 abstract static class DoubleEvaluatedNode extends ValueNode {
95
96 @Specialization
97 int doExecuteWith(int exp0, int exp1) {
98 return exp0 - exp1;
99 }
100
101 public abstract Object executeEvaluated(VirtualFrame frame, Object exp0, Object exp1);
102
103 public abstract int executeIntEvaluated(VirtualFrame frame, Object exp0, Object exp1) throws UnexpectedResultException;
104 }
105
106 @NodeChildren({@NodeChild("exp0"), @NodeChild("exp1"), @NodeChild(value = "exp2", type = DoubleEvaluatedNode.class, executeWith = {"exp0", "exp1"})})
107 abstract static class UseDoubleEvaluated1Node extends ValueNode {
108
109 @Specialization
110 int call(int exp0, int exp1, int exp2) {
111 Assert.assertEquals(exp0 - exp1, exp2);
112 return exp2;
113 }
114 }
115
116 @Test
117 public void testDoubleEvaluated2() {
118 ArgumentNode arg0 = new ArgumentNode(0);
119 ArgumentNode arg1 = new ArgumentNode(1);
120 CallTarget callTarget = TestHelper.createCallTarget(UseDoubleEvaluated2NodeFactory.create(arg0, arg1, DoubleEvaluatedNodeFactory.create(null, null)));
121
122 Assert.assertEquals(42, callTarget.call(new Object[]{1, 43}));
123 Assert.assertEquals(1, arg0.getInvocationCount());
124 Assert.assertEquals(1, arg1.getInvocationCount());
125 }
126
127 @NodeChildren({@NodeChild("exp0"), @NodeChild("exp1"), @NodeChild(value = "exp2", type = DoubleEvaluatedNode.class, executeWith = {"exp1", "exp0"})})
128 abstract static class UseDoubleEvaluated2Node extends ValueNode {
129
130 @Specialization
131 int call(int exp0, int exp1, int exp2) {
132 Assert.assertEquals(exp1 - exp0, exp2);
133 return exp2;
134 }
135 }
136
137 @Test
138 public void testEvaluatedGeneration() throws UnexpectedResultException {
139 TestRootNode<TestEvaluatedGeneration> root = TestHelper.createRoot(TestEvaluatedGenerationFactory.getInstance());
140
141 Assert.assertEquals(42, root.getNode().executeEvaluated1(null, 42));
142 Assert.assertEquals(42, root.getNode().executeEvaluated2(null, 42));
143 Assert.assertEquals(42, root.getNode().executeEvaluated3(null, 42));
144 Assert.assertEquals(42, root.getNode().executeEvaluated4(null, 42));
145 }
146
147 @NodeChildren({@NodeChild("exp0")})
148 abstract static class TestEvaluatedGeneration extends ValueNode {
149
150 public abstract Object executeEvaluated1(VirtualFrame frame, Object value);
151
152 public abstract Object executeEvaluated2(VirtualFrame frame, int value);
153
154 public abstract int executeEvaluated3(VirtualFrame frame, Object value) throws UnexpectedResultException;
155
156 public abstract int executeEvaluated4(VirtualFrame frame, int value) throws UnexpectedResultException;
157
158 @Specialization
159 int call(int exp0) {
160 return exp0;
161 }
162 }
163
164 @Test
165 public void test0VarArgs1() {
166 TestRootNode<TestEvaluatedVarArgs0> root = TestHelper.createRoot(TestEvaluatedVarArgs0Factory.getInstance());
167 Assert.assertEquals(42, root.getNode().execute1(null));
168 }
169
170 abstract static class TestEvaluatedVarArgs0 extends ChildrenNode {
171
172 @Override
173 public final Object execute(VirtualFrame frame) {
174 return execute1(frame);
175 }
176
177 public abstract Object execute1(VirtualFrame frame, Object... value);
178
179 @Specialization
180 int call() {
181 return 42;
182 }
183 }
184
185 @Test
186 public void test1VarArgs1() {
187 TestRootNode<TestEvaluatedVarArgs1> root = TestHelper.createRoot(TestEvaluatedVarArgs1Factory.getInstance());
188 Assert.assertEquals(42, root.getNode().execute1(null, 42));
189 }
190
191 @Test(expected = Throwable.class)
192 public void test1VarArgs2() {
193 TestRootNode<TestEvaluatedVarArgs2> root = TestHelper.createRoot(TestEvaluatedVarArgs2Factory.getInstance());
194 root.getNode().execute1(null);
195 }
196
197 abstract static class TestEvaluatedVarArgs1 extends ChildrenNode {
198
199 public abstract Object execute1(VirtualFrame frame, Object... value);
200
201 @Specialization
202 int call(int exp0) {
203 return exp0;
204 }
205 }
206
207 @Test
208 public void test2VarArgs1() {
209 TestRootNode<TestEvaluatedVarArgs2> root = TestHelper.createRoot(TestEvaluatedVarArgs2Factory.getInstance());
210 Assert.assertEquals(42, root.getNode().execute1(null, 21, 21));
211 }
212
213 @Test(expected = Throwable.class)
214 public void test2VarArgs2() {
215 TestRootNode<TestEvaluatedVarArgs2> root = TestHelper.createRoot(TestEvaluatedVarArgs2Factory.getInstance());
216 root.getNode().execute1(null, 42);
217 }
218
219 @Test(expected = Throwable.class)
220 public void test2VarArgs3() {
221 TestRootNode<TestEvaluatedVarArgs2> root = TestHelper.createRoot(TestEvaluatedVarArgs2Factory.getInstance());
222 root.getNode().execute1(null);
223 }
224
225 abstract static class TestEvaluatedVarArgs2 extends ChildrenNode {
226
227 public abstract Object execute1(VirtualFrame frame, Object... value);
228
229 @Specialization
230 int call(int exp0, int exp1) {
231 return exp0 + exp1;
232 }
233 }
234
235 @Test
236 public void test3VarArgs1() {
237 TestRootNode<TestEvaluatedVarArgs3> root = TestHelper.createRoot(TestEvaluatedVarArgs3Factory.getInstance());
238 Assert.assertEquals(42, root.getNode().execute1(null, 42));
239 }
240
241 @NodeChild
242 abstract static class TestEvaluatedVarArgs3 extends ValueNode {
243
244 public abstract Object execute1(VirtualFrame frame, Object... value);
245
246 @Specialization
247 int call(int exp0) {
248 return exp0;
249 }
250 }
251
252 @Test
253 public void test4VarArgs1() {
254 TestRootNode<TestEvaluatedVarArgs4> root = TestHelper.createRoot(TestEvaluatedVarArgs4Factory.getInstance());
255 Assert.assertEquals(42, root.getNode().execute1(null, 21, 21));
256 }
257
258 @NodeChildren({@NodeChild, @NodeChild})
259 abstract static class TestEvaluatedVarArgs4 extends ValueNode {
260
261 public abstract Object execute1(VirtualFrame frame, Object... value);
262
263 @Specialization
264 int call(int exp0, int exp1) {
265 return exp0 + exp1;
266 }
267 }
268
269 @Test
270 public void test5VarArgs1() {
271 TestRootNode<TestEvaluatedVarArgs5> root = TestHelper.createRoot(TestEvaluatedVarArgs5Factory.getInstance());
272 Assert.assertEquals(42, root.getNode().execute1(null));
273 }
274
275 abstract static class TestEvaluatedVarArgs5 extends ValueNode {
276
277 @Override
278 public final Object execute(VirtualFrame frame) {
279 return execute1(frame);
280 }
281
282 public abstract Object execute1(VirtualFrame frame, Object... value);
283
284 @Specialization
285 int call() {
286 return 42;
287 }
288 }
289
290 @SuppressWarnings("unused")
291 @NodeChildren({@NodeChild(value = "a", type = ValueNode.class), @NodeChild(value = "b", type = ValueNode.class)})
292 abstract static class TestEvaluatedShortCircuit1 extends Node {
293
294 public abstract Object execute1(VirtualFrame frame, Object value);
295
296 public abstract Object execute2(VirtualFrame frame, Object value, boolean hasB);
297
298 public abstract Object execute3(VirtualFrame frame, Object value, boolean hasB, Object b);
299
300 @ShortCircuit("b")
301 public boolean needsB(Object a) {
302 return true;
303 }
304
305 @Specialization
306 int call(Object a, boolean hasB, Object b) {
307 return 42;
308 }
309 }
310
311 @SuppressWarnings("unused")
312 @NodeChildren({@NodeChild(value = "a", type = ValueNode.class), @NodeChild(value = "b", type = ValueNode.class)})
313 abstract static class TestEvaluatedShortCircuit2 extends Node {
314
315 public abstract Object execute1(VirtualFrame frame, Object value);
316
317 public abstract Object execute2(VirtualFrame frame, Object value, boolean hasB);
318
319 public abstract Object execute3(VirtualFrame frame, Object value, boolean hasB, Object b);
320
321 @ShortCircuit("b")
322 public boolean needsB(Object a) {
323 return true;
324 }
325
326 @Specialization
327 int call(int a, boolean hasB, int b) {
328 return 42;
329 }
330
331 @Specialization
332 int call(Object a, boolean hasB, Object b) {
333 return 42;
334 }
335 }
336
337 }