Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/PolymorphicTest.java @ 16756:5148aab962af
Truffle-DSL: updated tests for the new generation layout.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 11 Aug 2014 15:53:05 +0200 |
parents | f0bb82ebe30c |
children | 9f38d222fa6c |
comparison
equal
deleted
inserted
replaced
16755:bd28da642eea | 16756:5148aab962af |
---|---|
23 package com.oracle.truffle.api.dsl.test; | 23 package com.oracle.truffle.api.dsl.test; |
24 | 24 |
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*; | 25 import static com.oracle.truffle.api.dsl.test.TestHelper.*; |
26 import static org.junit.Assert.*; | 26 import static org.junit.Assert.*; |
27 | 27 |
28 import java.util.*; | |
29 | |
28 import org.junit.*; | 30 import org.junit.*; |
29 | 31 |
30 import com.oracle.truffle.api.dsl.*; | 32 import com.oracle.truffle.api.dsl.*; |
31 import com.oracle.truffle.api.dsl.test.BinaryNodeTest.BinaryNode; | 33 import com.oracle.truffle.api.dsl.test.PolymorphicTestFactory.Polymorphic1Factory; |
32 import com.oracle.truffle.api.dsl.test.PolymorphicTestFactory.Node1Factory; | 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; | |
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | 37 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; |
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | 38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; |
35 import com.oracle.truffle.api.nodes.*; | 39 import com.oracle.truffle.api.nodes.*; |
36 | 40 |
37 public class PolymorphicTest { | 41 public class PolymorphicTest { |
45 if (parent != expectedParent) { | 49 if (parent != expectedParent) { |
46 assertEquals(expectedParent, parent); | 50 assertEquals(expectedParent, parent); |
47 } | 51 } |
48 } | 52 } |
49 | 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 | |
50 @Test | 74 @Test |
51 public void testJustSpecialize() { | 75 public void testPolymorphic1() { |
52 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 76 assertRuns(Polymorphic1Factory.getInstance(), // |
53 assertEquals("(int,int)", executeWith(node, 42, 42)); | 77 array(42, 43, true, false, "a", "b"), // |
54 assertEquals("(boolean,boolean)", executeWith(node, false, false)); | 78 array(42, 43, true, false, "a", "b"),// |
55 assertEquals("(int,boolean)", executeWith(node, 42, false)); | 79 new ExecutionListener() { |
56 assertEquals("(boolean,int)", executeWith(node, false, 42)); | 80 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { |
57 assertEquals(NodeCost.MONOMORPHIC, node.getNode().getCost()); | 81 Polymorphic1 polymorphic = ((Polymorphic1) node.getNode()); |
58 assertParent(node.getNode(), node.getNode().getLeft()); | 82 assertParent(node.getNode(), polymorphic.getA()); |
59 assertParent(node.getNode(), node.getNode().getRight()); | 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 @Generic | |
112 String add(Object left) { | |
113 throw new AssertionError(left.toString()); | |
114 } | |
115 | |
60 } | 116 } |
61 | 117 |
62 @Test | 118 @Test |
63 public void testPolymorphic2() { | 119 public void testPolymorphic2() { |
64 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 120 assertRuns(Polymorphic2Factory.getInstance(), // |
65 assertEquals("(int,boolean)", executeWith(node, 42, false)); | 121 array(0, 1, 1, "1", "2", 2, 3), // |
66 assertEquals("(int,int)", executeWith(node, 42, 42)); | 122 array(0, 1, 1, "1", "2", 2, 3),// |
67 assertEquals(NodeCost.POLYMORPHIC, node.getNode().getCost()); | 123 new ExecutionListener() { |
68 assertParent(node.getNode(), node.getNode().getLeft()); | 124 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { |
69 assertParent(node.getNode(), node.getNode().getRight()); | 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 | |
70 } | 158 } |
71 | 159 |
72 @Test | 160 @Test |
73 public void testPolymorphic3() { | 161 public void testPolymorphic3() { |
74 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 162 assertRuns(Polymorphic3Factory.getInstance(), // |
75 assertEquals("(int,boolean)", executeWith(node, 42, false)); | 163 array("0", "1", 1, 1, 2, 2, 3, 3), // |
76 assertEquals("(boolean,boolean)", executeWith(node, true, false)); | 164 array("0", "1", 1, 1, 2, 2, 3, 3),// |
77 assertEquals("(int,int)", executeWith(node, 42, 42)); | 165 new ExecutionListener() { |
78 assertEquals(NodeCost.POLYMORPHIC, node.getNode().getCost()); | 166 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) { |
79 assertParent(node.getNode(), node.getNode().getLeft()); | 167 Polymorphic3 polymorphic = ((Polymorphic3) node.getNode()); |
80 assertParent(node.getNode(), node.getNode().getRight()); | 168 assertParent(node.getNode(), polymorphic.getA()); |
81 } | 169 assertNoDuplicates(polymorphic, polymorphic.getA()); |
82 | 170 } |
83 @Test | 171 }); |
84 public void testGenericLimitReached() { | 172 } |
85 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 173 |
86 assertEquals("(boolean,int)", executeWith(node, false, 42)); | 174 @NodeChild("a") |
87 assertEquals("(int,boolean)", executeWith(node, 42, false)); | 175 abstract static class Polymorphic3 extends ValueNode { |
88 assertEquals("(boolean,boolean)", executeWith(node, true, false)); | 176 |
89 assertEquals("(int,int)", executeWith(node, 42, 42)); | 177 public abstract ValueNode getA(); |
90 assertEquals(NodeCost.MEGAMORPHIC, node.getNode().getCost()); | 178 |
91 assertParent(node.getNode(), node.getNode().getLeft()); | 179 @Specialization |
92 assertParent(node.getNode(), node.getNode().getRight()); | 180 String s2(String a) { |
93 } | 181 return a; |
94 | 182 } |
95 @Test | 183 |
96 public void testGenericInitial() { | 184 @Specialization(rewriteOn = RuntimeException.class) |
97 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 185 int s0(int a) { |
98 assertEquals("(generic,generic)", executeWith(node, "1", "1")); | 186 if (a == 1) { |
99 assertEquals(NodeCost.MEGAMORPHIC, node.getNode().getCost()); | 187 throw new RuntimeException(); |
100 assertParent(node.getNode(), node.getNode().getLeft()); | 188 } |
101 assertParent(node.getNode(), node.getNode().getRight()); | 189 return a; |
102 } | 190 } |
103 | 191 |
104 @Test | 192 @Specialization(rewriteOn = RuntimeException.class) |
105 public void testGenericPolymorphic1() { | 193 int s1(int a) { |
106 TestRootNode<Node1> node = TestHelper.createRoot(Node1Factory.getInstance()); | 194 if (a == 1) { |
107 assertEquals("(boolean,int)", executeWith(node, false, 42)); | 195 throw new RuntimeException(); |
108 assertEquals("(boolean,boolean)", executeWith(node, false, false)); | 196 } |
109 assertEquals("(generic,generic)", executeWith(node, "", "")); | 197 return a; |
110 assertEquals(NodeCost.MEGAMORPHIC, node.getNode().getCost()); | 198 } |
111 /* Assertions for bug GRAAL-425 */ | 199 |
112 assertParent(node.getNode(), node.getNode().getLeft()); | 200 @Specialization |
113 assertParent(node.getNode(), node.getNode().getRight()); | 201 int s2(int a) { |
114 } | 202 return a; |
115 | |
116 @SuppressWarnings("unused") | |
117 @PolymorphicLimit(3) | |
118 abstract static class Node1 extends BinaryNode { | |
119 | |
120 public abstract ValueNode getLeft(); | |
121 | |
122 public abstract ValueNode getRight(); | |
123 | |
124 @Specialization(order = 1) | |
125 String add(int left, int right) { | |
126 return "(int,int)"; | |
127 } | |
128 | |
129 @Specialization(order = 2) | |
130 String add(boolean left, boolean right) { | |
131 return "(boolean,boolean)"; | |
132 } | |
133 | |
134 @Specialization(order = 3) | |
135 String add(int left, boolean right) { | |
136 return "(int,boolean)"; | |
137 } | |
138 | |
139 @Specialization(order = 4) | |
140 String add(boolean left, int right) { | |
141 return "(boolean,int)"; | |
142 } | |
143 | |
144 @Generic | |
145 String add(Object left, Object right) { | |
146 return "(generic,generic)"; | |
147 } | 203 } |
148 | 204 |
149 } | 205 } |
150 | 206 |
151 } | 207 } |