comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ExecuteGroupingTest.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/ExecuteGroupingTest.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 org.junit.Assert.*;
26
27 import org.junit.experimental.theories.*;
28 import org.junit.runner.*;
29
30 import com.oracle.truffle.api.dsl.*;
31 import com.oracle.truffle.api.dsl.test.ExecuteGroupingTestFactory.ExecuteGrouping1NodeGen;
32 import com.oracle.truffle.api.frame.*;
33 import com.oracle.truffle.api.nodes.*;
34
35 /*
36 * This test aims to test the reuse of execute methods with evaluated parameters as much as possible.
37 */
38 @RunWith(Theories.class)
39 public class ExecuteGroupingTest {
40
41 @DataPoints public static final Object[] parameters = new Object[]{1, 2};
42
43 static final class ExecuteGroupingChild extends Node {
44
45 int invocationCount = 0;
46
47 private final Object returnValue;
48
49 public ExecuteGroupingChild(Object returnValue) {
50 this.returnValue = returnValue;
51 }
52
53 Object execute() {
54 invocationCount++;
55 return returnValue;
56 }
57
58 }
59
60 @Theory
61 public void testExecuteGrouping1Node(Object a, Object b, Object c) throws UnexpectedResultException {
62 ExecuteGroupingChild child0 = new ExecuteGroupingChild(a);
63 ExecuteGroupingChild child1 = new ExecuteGroupingChild(b);
64 ExecuteGroupingChild child2 = new ExecuteGroupingChild(c);
65
66 int result = ((int) a) + ((int) b) + ((int) c);
67
68 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(child0, child1, child2)).execute());
69 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(child0, child1, child2)).execute((VirtualFrame) null));
70 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, child1, child2)).execute(a));
71 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, child1, child2)).executeInt(a));
72
73 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute(a, b));
74 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute((int) a, b));
75 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute(a, (int) b));
76 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).execute((int) a, (int) b));
77 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, child2)).executeInt((int) a, (int) b));
78
79 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute(a, b, c));
80 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute((int) a, (int) b, c));
81 assertEquals(result, TestHelper.createRoot(ExecuteGrouping1NodeGen.create(null, null, null)).execute((int) a, (int) b, (int) c));
82
83 }
84
85 @NodeChildren({@NodeChild(type = ExecuteGroupingChild.class), @NodeChild(type = ExecuteGroupingChild.class), @NodeChild(type = ExecuteGroupingChild.class)})
86 abstract static class ExecuteGrouping1Node extends Node {
87
88 abstract Object execute();
89
90 int executeInt() throws UnexpectedResultException {
91 Object value = execute();
92 if (value instanceof Integer) {
93 return (int) value;
94 }
95 throw new UnexpectedResultException(value);
96 }
97
98 abstract double executeDouble() throws UnexpectedResultException;
99
100 abstract Object execute(VirtualFrame frame);
101
102 abstract Object execute(Object o1);
103
104 abstract int executeInt(Object o1) throws UnexpectedResultException;
105
106 abstract Object execute(Object o1, Object o2);
107
108 abstract Object execute(int o1, int o2);
109
110 abstract Object execute(int o1, int o2, Object o3);
111
112 abstract int executeInt(int o1, int o2) throws UnexpectedResultException;
113
114 abstract Object execute(Object o1, int o2);
115
116 abstract Object execute(int o1, Object o2);
117
118 abstract Object execute(Object o1, Object o2, Object o3);
119
120 abstract Object execute(int o1, int o2, int o3);
121
122 @Specialization
123 int s1(int a, int b, int c) {
124 return a + b + c;
125 }
126
127 @Specialization
128 int s2(Object a, Object b, Object c) {
129 return ((int) a) + ((int) b) + ((int) c);
130 }
131
132 }
133
134 abstract static class StrangeReturnCase extends Node {
135
136 // we don't know how to implement executeDouble
137 public abstract double executeDouble();
138
139 public int executeInt() {
140 return 42;
141 }
142
143 @Specialization(rewriteOn = RuntimeException.class)
144 double s1() {
145 return 42;
146 }
147
148 @Specialization
149 double s2() {
150 return 42;
151 }
152
153 }
154
155 @ExpectError("Incompatible abstract execute methods found %")
156 abstract static class IncompatibleAbstract1 extends Node {
157
158 // we don't know how to implement executeDouble
159 abstract double executeDouble();
160
161 abstract int executeInt();
162
163 @Specialization
164 double s1() {
165 return 42;
166 }
167
168 }
169
170 abstract static class IncompatibleAbstract2 extends Node {
171
172 abstract double executeDouble();
173
174 // we can resolve duplicate path errors by making an execute method final
175 @SuppressWarnings("static-method")
176 public final int executeInt() {
177 return 42;
178 }
179
180 @ExpectError("The provided return type \"int\" does not match expected return type \"double\".%")
181 @Specialization(rewriteOn = RuntimeException.class)
182 int s1() {
183 return 42;
184 }
185
186 @Specialization
187 double s2() {
188 return 42;
189 }
190
191 }
192
193 }