comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ShortCircuitTest.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/ShortCircuitTest.java@2e850dbf82ae
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 static org.junit.Assert.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.*;
30 import com.oracle.truffle.api.dsl.*;
31 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.DoubleChildNodeFactory;
32 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.ShortCircuitWithImplicitCastNodeFactory;
33 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.SingleChildNodeFactory;
34 import com.oracle.truffle.api.dsl.test.ShortCircuitTestFactory.VarArgsNodeFactory;
35 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode;
36 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
37
38 public class ShortCircuitTest {
39
40 @Test
41 public void testSingleChild1() {
42 ArgumentNode arg0 = new ArgumentNode(0);
43 CallTarget callTarget = TestHelper.createCallTarget(SingleChildNodeFactory.create(arg0));
44 SingleChildNode.needsChild = true;
45 assertEquals(42, callTarget.call(new Object[]{42}));
46 assertEquals(1, arg0.getInvocationCount());
47 }
48
49 @Test
50 public void testSingleChild2() {
51 ArgumentNode arg0 = new ArgumentNode(0);
52 CallTarget callTarget = TestHelper.createCallTarget(SingleChildNodeFactory.create(arg0));
53 SingleChildNode.needsChild = false;
54 assertEquals(0, callTarget.call(new Object[]{42}));
55 assertEquals(0, arg0.getInvocationCount());
56 }
57
58 @NodeChild("child0")
59 abstract static class SingleChildNode extends ValueNode {
60
61 static boolean needsChild;
62
63 @ShortCircuit("child0")
64 boolean needsChild0() {
65 return needsChild;
66 }
67
68 @Specialization
69 int doIt(boolean hasChild0, int child0) {
70 assert hasChild0 == needsChild0();
71 return child0;
72 }
73
74 }
75
76 @Test
77 public void testDoubleChild1() {
78 ArgumentNode arg0 = new ArgumentNode(0);
79 ArgumentNode arg1 = new ArgumentNode(1);
80 CallTarget callTarget = TestHelper.createCallTarget(DoubleChildNodeFactory.create(arg0, arg1));
81 assertEquals(42, callTarget.call(new Object[]{41, 42}));
82 assertEquals(1, arg1.getInvocationCount());
83 }
84
85 @Test
86 public void testDoubleChild2() {
87 ArgumentNode arg0 = new ArgumentNode(0);
88 ArgumentNode arg1 = new ArgumentNode(1);
89 CallTarget callTarget = TestHelper.createCallTarget(DoubleChildNodeFactory.create(arg0, arg1));
90 assertEquals(0, callTarget.call(new Object[]{42, 42}));
91 assertEquals(0, arg1.getInvocationCount());
92 }
93
94 @NodeChildren({@NodeChild("child0"), @NodeChild("child1")})
95 @SuppressWarnings("unused")
96 abstract static class DoubleChildNode extends ValueNode {
97
98 @ShortCircuit("child1")
99 boolean needsChild1(Object leftValue) {
100 return leftValue.equals(41);
101 }
102
103 @Specialization
104 int doIt(int child0, boolean hasChild1, int child1) {
105 return child1;
106 }
107
108 }
109
110 @NodeChildren({@NodeChild("child0"), @NodeChild("child1")})
111 @SuppressWarnings("unused")
112 abstract static class GuardChildNode extends ValueNode {
113
114 @ShortCircuit("child1")
115 boolean needsChild1(Object a) {
116 return a.equals(new Integer(42));
117 }
118
119 static boolean guard(int a, boolean hasB, int b) {
120 return false;
121 }
122
123 @Specialization(guards = "guard(a, hasB, b)")
124 int doIt(int a, boolean hasB, int b) {
125 return a + b;
126 }
127
128 }
129
130 @Test
131 public void testVarArgs1() {
132 ArgumentNode arg0 = new ArgumentNode(0);
133 ArgumentNode arg1 = new ArgumentNode(1);
134 CallTarget callTarget = TestHelper.createCallTarget(VarArgsNodeFactory.create(new ValueNode[]{arg0, arg1}));
135 assertEquals(42, callTarget.call(new Object[]{41, 42}));
136 assertEquals(1, arg1.getInvocationCount());
137 }
138
139 @Test
140 public void testVarArgs2() {
141 ArgumentNode arg0 = new ArgumentNode(0);
142 ArgumentNode arg1 = new ArgumentNode(1);
143 CallTarget callTarget = TestHelper.createCallTarget(VarArgsNodeFactory.create(new ValueNode[]{arg0, arg1}));
144 assertEquals(0, callTarget.call(new Object[]{42, 42}));
145 assertEquals(0, arg1.getInvocationCount());
146 }
147
148 @NodeChild(value = "children", type = ValueNode[].class)
149 abstract static class VarArgsNode extends ValueNode {
150
151 @ShortCircuit("children[1]")
152 boolean needsChild1(Object leftValue) {
153 return leftValue.equals(41);
154 }
155
156 @Specialization
157 @SuppressWarnings("unused")
158 int doIt(int child0, boolean hasChild1, int child1) {
159 return child1;
160 }
161
162 }
163
164 @Test
165 public void testShortCircuitWithImplicitCastNode() {
166 ArgumentNode arg0 = new ArgumentNode(0);
167 ArgumentNode arg1 = new ArgumentNode(1);
168 CallTarget callTarget = TestHelper.createCallTarget(ShortCircuitWithImplicitCastNodeFactory.create(new ValueNode[]{arg0, arg1}));
169 assertEquals(42, callTarget.call(new Object[]{42, 41}));
170 }
171
172 @TypeSystem(int.class)
173 abstract static class ShortCircuitWithImplicitCastTypes {
174
175 @ImplicitCast
176 public static int doAnImplicitCast(String foo) {
177 return Integer.parseInt(foo);
178 }
179
180 }
181
182 @NodeChild(value = "children", type = ValueNode[].class)
183 @TypeSystemReference(ShortCircuitWithImplicitCastTypes.class)
184 abstract static class ShortCircuitWithImplicitCastNode extends ValueNode {
185
186 @ShortCircuit("children[1]")
187 public boolean needsRightNode(Object left) {
188 return (int) left == 41;
189 }
190
191 @Specialization
192 public int doInteger(int left, boolean needsRight, int right) {
193 return needsRight ? right : left;
194 }
195
196 }
197
198 }