comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/TestHelper.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 64dcb92ee75a
children c5db657d93c1
comparison
equal deleted inserted replaced
16755:bd28da642eea 16756:5148aab962af
96 96
97 static <E> Object executeWith(TestRootNode<? extends ValueNode> node, Object... values) { 97 static <E> Object executeWith(TestRootNode<? extends ValueNode> node, Object... values) {
98 return createCallTarget(node).call(values); 98 return createCallTarget(node).call(values);
99 } 99 }
100 100
101 static Object array(Object... val) { 101 static Object[] array(Object... val) {
102 return val; 102 return val;
103 } 103 }
104 104
105 static <E> List<List<E>> permutations(List<E> list) { 105 static <E> List<List<E>> permutations(List<E> list) {
106 return permutations(new ArrayList<E>(), list, new ArrayList<List<E>>()); 106 return permutations(new ArrayList<E>(), list, new ArrayList<List<E>>());
136 } 136 }
137 137
138 return output; 138 return output;
139 } 139 }
140 140
141 static void assertRuns(NodeFactory<? extends ValueNode> factory, Object[] testValues, Object[] result) {
142 assertRuns(factory, testValues, result, null);
143 }
144
141 /* Methods tests all test values in combinational order. */ 145 /* Methods tests all test values in combinational order. */
142 static void assertRuns(NodeFactory<? extends ValueNode> factory, Object result, Object... testValues) { 146 static void assertRuns(NodeFactory<? extends ValueNode> factory, Object[] testValues, Object[] result, ExecutionListener listener) {
143 // test each run by its own. 147 // test each run by its own.
144 for (int i = 0; i < testValues.length; i++) { 148 for (int i = 0; i < testValues.length; i++) {
145 assertValue(createRoot(factory), result, testValues); 149 assertValue(createRoot(factory), 0, testValues[i], result[i], listener, true);
146 } 150 }
147 151
148 // test all combinations of the test values 152 // test all combinations of the test values
149 List<List<Object>> permuts = permutations(Arrays.asList(testValues)); 153 List<Object> testValuesList = Arrays.asList(testValues);
154 List<List<Object>> permuts = permutations(testValuesList);
150 for (List<Object> list : permuts) { 155 for (List<Object> list : permuts) {
151 TestRootNode<?> root = createRoot(factory); 156 TestRootNode<?> root = createRoot(factory);
157 int index = 0;
152 for (Object object : list) { 158 for (Object object : list) {
153 assertValue(root, result, object); 159 assertValue(root, index, object, result[testValuesList.indexOf(object)], listener, index == list.size() - 1);
160 index++;
154 } 161 }
155 } 162 }
156 } 163 }
157 164
158 static void assertValue(TestRootNode<? extends ValueNode> root, Object result, Object testValues) { 165 static void assertValue(TestRootNode<? extends ValueNode> root, int index, Object value, Object result, ExecutionListener listener, boolean last) {
159 if (testValues instanceof Object[]) { 166 Object actualResult = null;
160 assertEquals(result, executeWith(root, (Object[]) testValues)); 167 if (result instanceof Class && Throwable.class.isAssignableFrom((Class<?>) result)) {
168 try {
169 if (value instanceof Object[]) {
170 actualResult = executeWith(root, (Object[]) value);
171 } else {
172 actualResult = executeWith(root, value);
173 }
174 fail(String.format("Exception %s expected but not occured.", result.getClass()));
175 } catch (Throwable e) {
176 actualResult = e;
177 if (!e.getClass().isAssignableFrom(((Class<?>) result))) {
178 e.printStackTrace();
179 fail(String.format("Incompatible exception class thrown. Expected %s but was %s.", result.toString(), e.getClass()));
180 }
181 }
182 } else if (value instanceof Object[]) {
183 actualResult = executeWith(root, (Object[]) value);
184 assertEquals(result, actualResult);
161 } else { 185 } else {
162 assertEquals(result, executeWith(root, testValues)); 186 actualResult = executeWith(root, value);
163 } 187 assertEquals(result, actualResult);
188 }
189 if (listener != null) {
190 listener.afterExecution(root, index, value, result, actualResult, last);
191 }
192 }
193
194 public static final class LogListener implements ExecutionListener {
195
196 public void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last) {
197 System.out.printf("Run %3d Node:%-20s Parameters: %10s Expected: %10s Result %10s%n", index, node.getNode().getClass().getSimpleName(), value, expectedResult, actualResult);
198 }
199
200 }
201
202 interface ExecutionListener {
203
204 void afterExecution(TestRootNode<? extends ValueNode> node, int index, Object value, Object expectedResult, Object actualResult, boolean last);
205
164 } 206 }
165 207
166 } 208 }