comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/PolymorphicTest.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/PolymorphicTest.java@c88ab4f1f04a
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 com.oracle.truffle.api.dsl.test.TestHelper.*;
26 import static org.junit.Assert.*;
27
28 import java.util.*;
29
30 import org.junit.*;
31
32 import com.oracle.truffle.api.dsl.*;
33 import com.oracle.truffle.api.dsl.test.PolymorphicTestFactory.Polymorphic1Factory;
34 import com.oracle.truffle.api.dsl.test.PolymorphicTestFactory.Polymorphic2Factory;
35 import com.oracle.truffle.api.dsl.test.PolymorphicTestFactory.Polymorphic3Factory;
36 import com.oracle.truffle.api.dsl.test.TestHelper.ExecutionListener;
37 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
39 import com.oracle.truffle.api.nodes.*;
40
41 public class PolymorphicTest {
42
43 private static void assertParent(Node expectedParent, Node child) {
44 Node parent = child.getParent();
45 while (parent != null && parent != expectedParent) {
46 parent = parent.getParent();
47 }
48
49 if (parent != expectedParent) {
50 assertEquals(expectedParent, parent);
51 }
52 }
53
54 public static void assertNoDuplicates(Node node, Node... ignored) {
55 assertNoDuplicatesRec(new HashSet<>(Arrays.asList(ignored)), new HashSet<Class<?>>(), node);
56 }
57
58 private static void assertNoDuplicatesRec(Set<Node> ignored, Set<Class<?>> seenClasses, Node current) {
59 if (!ignored.contains(current)) {
60 if (seenClasses.contains(current.getClass())) {
61 Assert.fail(String.format("Multiple occurences of the same class %s. %nTree: %s", current.getClass().getSimpleName(), NodeUtil.printCompactTreeToString(current.getRootNode())));
62 } else {
63 seenClasses.add(current.getClass());
64 }
65 }
66
67 for (Node child : current.getChildren()) {
68 if (child != null) {
69 assertNoDuplicatesRec(ignored, seenClasses, child);
70 }
71 }
72 }
73
74 @Test
75 public void testPolymorphic1() {
76 assertRuns(Polymorphic1Factory.getInstance(), //
77 array(42, 43, true, false, "a", "b"), //
78 array(42, 43, true, false, "a", "b"), //
79 new ExecutionListener() {
80 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) {
81 Polymorphic1 polymorphic = ((Polymorphic1) node.getNode());
82 assertParent(node.getNode(), polymorphic.getA());
83 assertNoDuplicates(polymorphic, polymorphic.getA());
84 if (index == 0) {
85 assertEquals(NodeCost.MONOMORPHIC, node.getNode().getCost());
86 }
87 }
88 });
89 }
90
91 @NodeChild("a")
92 abstract static class Polymorphic1 extends ValueNode {
93
94 public abstract ValueNode getA();
95
96 @Specialization
97 int add(int a) {
98 return a;
99 }
100
101 @Specialization
102 boolean add(boolean a) {
103 return a;
104 }
105
106 @Specialization
107 String add(String a) {
108 return a;
109 }
110
111 @Fallback
112 String add(Object left) {
113 throw new AssertionError(left.toString());
114 }
115
116 }
117
118 @Test
119 public void testPolymorphic2() {
120 assertRuns(Polymorphic2Factory.getInstance(), //
121 array(0, 1, 1, "1", "2", 2, 3), //
122 array(0, 1, 1, "1", "2", 2, 3), //
123 new ExecutionListener() {
124 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) {
125 Polymorphic2 polymorphic = ((Polymorphic2) node.getNode());
126 assertParent(node.getNode(), polymorphic.getA());
127 assertNoDuplicates(polymorphic, polymorphic.getA());
128 if (index == 0) {
129 assertEquals(NodeCost.MONOMORPHIC, node.getNode().getCost());
130 }
131 }
132 });
133 }
134
135 @NodeChild("a")
136 abstract static class Polymorphic2 extends ValueNode {
137
138 public abstract ValueNode getA();
139
140 @Specialization
141 String s2(String a) {
142 return a;
143 }
144
145 @Specialization(rewriteOn = RuntimeException.class)
146 int s0(int a) {
147 if (a == 1) {
148 throw new RuntimeException();
149 }
150 return a;
151 }
152
153 @Specialization
154 int s1(int a) {
155 return a;
156 }
157
158 }
159
160 @Test
161 public void testPolymorphic3() {
162 assertRuns(Polymorphic3Factory.getInstance(), //
163 array("0", "1", 1, 1, 2, 2, 3, 3), //
164 array("0", "1", 1, 1, 2, 2, 3, 3), //
165 new ExecutionListener() {
166 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) {
167 Polymorphic3 polymorphic = ((Polymorphic3) node.getNode());
168 assertParent(node.getNode(), polymorphic.getA());
169 assertNoDuplicates(polymorphic, polymorphic.getA());
170 }
171 });
172 }
173
174 @NodeChild("a")
175 abstract static class Polymorphic3 extends ValueNode {
176
177 public abstract ValueNode getA();
178
179 @Specialization
180 String s2(String a) {
181 return a;
182 }
183
184 @Specialization(rewriteOn = RuntimeException.class)
185 int s0(int a) {
186 if (a == 1) {
187 throw new RuntimeException();
188 }
189 return a;
190 }
191
192 @Specialization(rewriteOn = RuntimeException.class)
193 int s1(int a) {
194 if (a == 1) {
195 throw new RuntimeException();
196 }
197 return a;
198 }
199
200 @Specialization
201 int s2(int a) {
202 return a;
203 }
204
205 }
206
207 }