43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2013, 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 org.graalvm.compiler.test;
|
|
24 |
|
46640
|
25 |
import static org.graalvm.compiler.debug.DebugContext.DEFAULT_LOG_STREAM;
|
|
26 |
import static org.graalvm.compiler.debug.DebugContext.NO_DESCRIPTION;
|
|
27 |
|
43972
|
28 |
import java.io.PrintStream;
|
|
29 |
import java.io.PrintWriter;
|
|
30 |
import java.lang.reflect.Field;
|
|
31 |
import java.lang.reflect.Method;
|
46640
|
32 |
import java.util.ArrayList;
|
43972
|
33 |
import java.util.Arrays;
|
46640
|
34 |
import java.util.Collection;
|
|
35 |
import java.util.Collections;
|
|
36 |
import java.util.List;
|
43972
|
37 |
|
46640
|
38 |
import org.graalvm.compiler.debug.DebugContext;
|
|
39 |
import org.graalvm.compiler.debug.DebugDumpHandler;
|
48861
|
40 |
import org.graalvm.compiler.debug.DebugHandlersFactory;
|
|
41 |
import org.graalvm.compiler.debug.GlobalMetrics;
|
46640
|
42 |
import org.graalvm.compiler.options.OptionValues;
|
49873
|
43 |
import org.graalvm.compiler.serviceprovider.GraalServices;
|
46640
|
44 |
import org.junit.After;
|
43972
|
45 |
import org.junit.Assert;
|
|
46 |
import org.junit.internal.ComparisonCriteria;
|
|
47 |
import org.junit.internal.ExactComparisonCriteria;
|
|
48 |
|
48861
|
49 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
43972
|
50 |
import sun.misc.Unsafe;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Base class that contains common utility methods and classes useful in unit tests.
|
|
54 |
*/
|
|
55 |
public class GraalTest {
|
|
56 |
|
|
57 |
public static final Unsafe UNSAFE;
|
|
58 |
static {
|
|
59 |
try {
|
|
60 |
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
|
|
61 |
theUnsafe.setAccessible(true);
|
|
62 |
UNSAFE = (Unsafe) theUnsafe.get(Unsafe.class);
|
|
63 |
} catch (Exception e) {
|
|
64 |
throw new RuntimeException("exception while trying to get Unsafe", e);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
49873
|
68 |
public static final boolean Java8OrEarlier = GraalServices.Java8OrEarlier;
|
43972
|
69 |
|
|
70 |
protected Method getMethod(String methodName) {
|
|
71 |
return getMethod(getClass(), methodName);
|
|
72 |
}
|
|
73 |
|
|
74 |
protected Method getMethod(Class<?> clazz, String methodName) {
|
|
75 |
Method found = null;
|
|
76 |
for (Method m : clazz.getMethods()) {
|
|
77 |
if (m.getName().equals(methodName)) {
|
|
78 |
Assert.assertNull(found);
|
|
79 |
found = m;
|
|
80 |
}
|
|
81 |
}
|
|
82 |
if (found == null) {
|
|
83 |
/* Now look for non-public methods (but this does not look in superclasses). */
|
|
84 |
for (Method m : clazz.getDeclaredMethods()) {
|
|
85 |
if (m.getName().equals(methodName)) {
|
|
86 |
Assert.assertNull(found);
|
|
87 |
found = m;
|
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
if (found != null) {
|
|
92 |
return found;
|
|
93 |
} else {
|
|
94 |
throw new RuntimeException("method not found: " + methodName);
|
|
95 |
}
|
|
96 |
}
|
|
97 |
|
46459
|
98 |
protected Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes) {
|
43972
|
99 |
try {
|
|
100 |
return clazz.getMethod(methodName, parameterTypes);
|
|
101 |
} catch (NoSuchMethodException | SecurityException e) {
|
|
102 |
throw new RuntimeException("method not found: " + methodName + "" + Arrays.toString(parameterTypes));
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
/**
|
|
107 |
* Compares two given objects for {@linkplain Assert#assertEquals(Object, Object) equality}.
|
|
108 |
* Does a deep copy equality comparison if {@code expected} is an array.
|
|
109 |
*/
|
|
110 |
protected void assertDeepEquals(Object expected, Object actual) {
|
|
111 |
assertDeepEquals(null, expected, actual);
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Compares two given objects for {@linkplain Assert#assertEquals(Object, Object) equality}.
|
|
116 |
* Does a deep copy equality comparison if {@code expected} is an array.
|
|
117 |
*
|
|
118 |
* @param message the identifying message for the {@link AssertionError}
|
|
119 |
*/
|
|
120 |
protected void assertDeepEquals(String message, Object expected, Object actual) {
|
|
121 |
if (ulpsDelta() > 0) {
|
|
122 |
assertDeepEquals(message, expected, actual, ulpsDelta());
|
|
123 |
} else {
|
|
124 |
assertDeepEquals(message, expected, actual, equalFloatsOrDoublesDelta());
|
|
125 |
}
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Compares two given values for equality, doing a recursive test if both values are arrays of
|
|
130 |
* the same type.
|
|
131 |
*
|
|
132 |
* @param message the identifying message for the {@link AssertionError}
|
|
133 |
* @param delta the maximum delta between two doubles or floats for which both numbers are still
|
|
134 |
* considered equal.
|
|
135 |
*/
|
|
136 |
protected void assertDeepEquals(String message, Object expected, Object actual, double delta) {
|
|
137 |
if (expected != null && actual != null) {
|
|
138 |
Class<?> expectedClass = expected.getClass();
|
|
139 |
Class<?> actualClass = actual.getClass();
|
|
140 |
if (expectedClass.isArray()) {
|
|
141 |
Assert.assertEquals(message, expectedClass, actual.getClass());
|
|
142 |
if (expected instanceof int[]) {
|
|
143 |
Assert.assertArrayEquals(message, (int[]) expected, (int[]) actual);
|
|
144 |
} else if (expected instanceof byte[]) {
|
|
145 |
Assert.assertArrayEquals(message, (byte[]) expected, (byte[]) actual);
|
|
146 |
} else if (expected instanceof char[]) {
|
|
147 |
Assert.assertArrayEquals(message, (char[]) expected, (char[]) actual);
|
|
148 |
} else if (expected instanceof short[]) {
|
|
149 |
Assert.assertArrayEquals(message, (short[]) expected, (short[]) actual);
|
|
150 |
} else if (expected instanceof float[]) {
|
|
151 |
Assert.assertArrayEquals(message, (float[]) expected, (float[]) actual, (float) delta);
|
|
152 |
} else if (expected instanceof long[]) {
|
|
153 |
Assert.assertArrayEquals(message, (long[]) expected, (long[]) actual);
|
|
154 |
} else if (expected instanceof double[]) {
|
|
155 |
Assert.assertArrayEquals(message, (double[]) expected, (double[]) actual, delta);
|
|
156 |
} else if (expected instanceof boolean[]) {
|
|
157 |
new ExactComparisonCriteria().arrayEquals(message, expected, actual);
|
|
158 |
} else if (expected instanceof Object[]) {
|
|
159 |
new ComparisonCriteria() {
|
|
160 |
@Override
|
|
161 |
protected void assertElementsEqual(Object e, Object a) {
|
|
162 |
assertDeepEquals(message, e, a, delta);
|
|
163 |
}
|
|
164 |
}.arrayEquals(message, expected, actual);
|
|
165 |
} else {
|
|
166 |
Assert.fail((message == null ? "" : message) + "non-array value encountered: " + expected);
|
|
167 |
}
|
|
168 |
} else if (expectedClass.equals(double.class) && actualClass.equals(double.class)) {
|
|
169 |
Assert.assertEquals((double) expected, (double) actual, delta);
|
|
170 |
} else if (expectedClass.equals(float.class) && actualClass.equals(float.class)) {
|
|
171 |
Assert.assertEquals((float) expected, (float) actual, delta);
|
|
172 |
} else {
|
|
173 |
Assert.assertEquals(message, expected, actual);
|
|
174 |
}
|
|
175 |
} else {
|
|
176 |
Assert.assertEquals(message, expected, actual);
|
|
177 |
}
|
|
178 |
}
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Compares two given values for equality, doing a recursive test if both values are arrays of
|
|
182 |
* the same type. Uses {@linkplain StrictMath#ulp(float) ULP}s for comparison of floats.
|
|
183 |
*
|
|
184 |
* @param message the identifying message for the {@link AssertionError}
|
|
185 |
* @param ulpsDelta the maximum allowed ulps difference between two doubles or floats for which
|
|
186 |
* both numbers are still considered equal.
|
|
187 |
*/
|
|
188 |
protected void assertDeepEquals(String message, Object expected, Object actual, int ulpsDelta) {
|
|
189 |
ComparisonCriteria doubleUlpsDeltaCriteria = new ComparisonCriteria() {
|
|
190 |
@Override
|
|
191 |
protected void assertElementsEqual(Object e, Object a) {
|
|
192 |
assertTrue(message, e instanceof Double && a instanceof Double);
|
|
193 |
// determine acceptable error based on whether it is a normal number or a NaN/Inf
|
|
194 |
double de = (Double) e;
|
|
195 |
double epsilon = (!Double.isNaN(de) && Double.isFinite(de) ? ulpsDelta * Math.ulp(de) : 0);
|
|
196 |
Assert.assertEquals(message, (Double) e, (Double) a, epsilon);
|
|
197 |
}
|
|
198 |
};
|
|
199 |
|
|
200 |
ComparisonCriteria floatUlpsDeltaCriteria = new ComparisonCriteria() {
|
|
201 |
@Override
|
|
202 |
protected void assertElementsEqual(Object e, Object a) {
|
|
203 |
assertTrue(message, e instanceof Float && a instanceof Float);
|
|
204 |
// determine acceptable error based on whether it is a normal number or a NaN/Inf
|
|
205 |
float fe = (Float) e;
|
|
206 |
float epsilon = (!Float.isNaN(fe) && Float.isFinite(fe) ? ulpsDelta * Math.ulp(fe) : 0);
|
|
207 |
Assert.assertEquals(message, (Float) e, (Float) a, epsilon);
|
|
208 |
}
|
|
209 |
};
|
|
210 |
|
|
211 |
if (expected != null && actual != null) {
|
|
212 |
Class<?> expectedClass = expected.getClass();
|
|
213 |
Class<?> actualClass = actual.getClass();
|
|
214 |
if (expectedClass.isArray()) {
|
|
215 |
Assert.assertEquals(message, expectedClass, actualClass);
|
|
216 |
if (expected instanceof double[] || expected instanceof Object[]) {
|
|
217 |
doubleUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
218 |
return;
|
|
219 |
} else if (expected instanceof float[] || expected instanceof Object[]) {
|
|
220 |
floatUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
221 |
return;
|
|
222 |
}
|
|
223 |
} else if (expectedClass.equals(double.class) && actualClass.equals(double.class)) {
|
|
224 |
doubleUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
225 |
return;
|
|
226 |
} else if (expectedClass.equals(float.class) && actualClass.equals(float.class)) {
|
|
227 |
floatUlpsDeltaCriteria.arrayEquals(message, expected, actual);
|
|
228 |
return;
|
|
229 |
}
|
|
230 |
}
|
|
231 |
// anything else just use the non-ulps version
|
|
232 |
assertDeepEquals(message, expected, actual, equalFloatsOrDoublesDelta());
|
|
233 |
}
|
|
234 |
|
|
235 |
/**
|
|
236 |
* Gets the value used by {@link #assertDeepEquals(Object, Object)} and
|
|
237 |
* {@link #assertDeepEquals(String, Object, Object)} for the maximum delta between two doubles
|
|
238 |
* or floats for which both numbers are still considered equal.
|
|
239 |
*/
|
|
240 |
protected double equalFloatsOrDoublesDelta() {
|
|
241 |
return 0.0D;
|
|
242 |
}
|
|
243 |
|
|
244 |
// unless overridden ulpsDelta is not used
|
|
245 |
protected int ulpsDelta() {
|
|
246 |
return 0;
|
|
247 |
}
|
|
248 |
|
|
249 |
@SuppressWarnings("serial")
|
|
250 |
public static class MultiCauseAssertionError extends AssertionError {
|
|
251 |
|
|
252 |
private Throwable[] causes;
|
|
253 |
|
|
254 |
public MultiCauseAssertionError(String message, Throwable... causes) {
|
|
255 |
super(message);
|
|
256 |
this.causes = causes;
|
|
257 |
}
|
|
258 |
|
|
259 |
@Override
|
|
260 |
public void printStackTrace(PrintStream out) {
|
|
261 |
super.printStackTrace(out);
|
|
262 |
int num = 0;
|
|
263 |
for (Throwable cause : causes) {
|
|
264 |
if (cause != null) {
|
|
265 |
out.print("cause " + (num++));
|
|
266 |
cause.printStackTrace(out);
|
|
267 |
}
|
|
268 |
}
|
|
269 |
}
|
|
270 |
|
|
271 |
@Override
|
|
272 |
public void printStackTrace(PrintWriter out) {
|
|
273 |
super.printStackTrace(out);
|
|
274 |
int num = 0;
|
|
275 |
for (Throwable cause : causes) {
|
|
276 |
if (cause != null) {
|
|
277 |
out.print("cause " + (num++) + ": ");
|
|
278 |
cause.printStackTrace(out);
|
|
279 |
}
|
|
280 |
}
|
|
281 |
}
|
|
282 |
}
|
|
283 |
|
|
284 |
/*
|
|
285 |
* Overrides to the normal JUnit {@link Assert} routines that provide varargs style formatting
|
|
286 |
* and produce an exception stack trace with the assertion frames trimmed out.
|
|
287 |
*/
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Fails a test with the given message.
|
|
291 |
*
|
|
292 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
293 |
* okay)
|
|
294 |
* @see AssertionError
|
|
295 |
*/
|
|
296 |
public static void fail(String message, Object... objects) {
|
|
297 |
AssertionError e;
|
|
298 |
if (message == null) {
|
|
299 |
e = new AssertionError();
|
|
300 |
} else {
|
|
301 |
e = new AssertionError(String.format(message, objects));
|
|
302 |
}
|
|
303 |
// Trim the assert frames from the stack trace
|
|
304 |
StackTraceElement[] trace = e.getStackTrace();
|
|
305 |
int start = 1; // Skip this frame
|
|
306 |
String thisClassName = GraalTest.class.getName();
|
|
307 |
while (start < trace.length && trace[start].getClassName().equals(thisClassName) && (trace[start].getMethodName().equals("assertTrue") || trace[start].getMethodName().equals("assertFalse"))) {
|
|
308 |
start++;
|
|
309 |
}
|
|
310 |
e.setStackTrace(Arrays.copyOfRange(trace, start, trace.length));
|
|
311 |
throw e;
|
|
312 |
}
|
|
313 |
|
|
314 |
/**
|
|
315 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} with the
|
|
316 |
* given message.
|
|
317 |
*
|
|
318 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
319 |
* okay)
|
|
320 |
* @param condition condition to be checked
|
|
321 |
*/
|
|
322 |
public static void assertTrue(String message, boolean condition) {
|
|
323 |
assertTrue(condition, message);
|
|
324 |
}
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} without a
|
|
328 |
* message.
|
|
329 |
*
|
|
330 |
* @param condition condition to be checked
|
|
331 |
*/
|
|
332 |
public static void assertTrue(boolean condition) {
|
|
333 |
assertTrue(condition, null);
|
|
334 |
}
|
|
335 |
|
|
336 |
/**
|
|
337 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
|
|
338 |
* given message.
|
|
339 |
*
|
|
340 |
* @param message the identifying message for the {@link AssertionError} (<code>null</code>
|
|
341 |
* okay)
|
|
342 |
* @param condition condition to be checked
|
|
343 |
*/
|
|
344 |
public static void assertFalse(String message, boolean condition) {
|
|
345 |
assertTrue(!condition, message);
|
|
346 |
}
|
|
347 |
|
|
348 |
/**
|
|
349 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} without a
|
|
350 |
* message.
|
|
351 |
*
|
|
352 |
* @param condition condition to be checked
|
|
353 |
*/
|
|
354 |
public static void assertFalse(boolean condition) {
|
|
355 |
assertTrue(!condition, null);
|
|
356 |
}
|
|
357 |
|
|
358 |
/**
|
|
359 |
* Asserts that a condition is true. If it isn't it throws an {@link AssertionError} with the
|
|
360 |
* given message.
|
|
361 |
*
|
|
362 |
* @param condition condition to be checked
|
|
363 |
* @param message the identifying message for the {@link AssertionError}
|
|
364 |
* @param objects arguments to the format string
|
|
365 |
*/
|
|
366 |
public static void assertTrue(boolean condition, String message, Object... objects) {
|
|
367 |
if (!condition) {
|
|
368 |
fail(message, objects);
|
|
369 |
}
|
|
370 |
}
|
|
371 |
|
|
372 |
/**
|
|
373 |
* Asserts that a condition is false. If it isn't it throws an {@link AssertionError} with the
|
|
374 |
* given message produced by {@link String#format}.
|
|
375 |
*
|
|
376 |
* @param condition condition to be checked
|
|
377 |
* @param message the identifying message for the {@link AssertionError}
|
|
378 |
* @param objects arguments to the format string
|
|
379 |
*/
|
|
380 |
public static void assertFalse(boolean condition, String message, Object... objects) {
|
|
381 |
assertTrue(!condition, message, objects);
|
|
382 |
}
|
46640
|
383 |
|
|
384 |
/**
|
|
385 |
* Gets the {@link DebugHandlersFactory}s available for a {@link DebugContext}.
|
|
386 |
*/
|
|
387 |
protected Collection<DebugHandlersFactory> getDebugHandlersFactories() {
|
|
388 |
return Collections.emptyList();
|
|
389 |
}
|
|
390 |
|
|
391 |
/**
|
|
392 |
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
|
|
393 |
* none currently exists. Debug contexts created by this method will have their
|
|
394 |
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
|
|
395 |
*/
|
|
396 |
protected DebugContext getDebugContext(OptionValues options) {
|
47798
|
397 |
return getDebugContext(options, null, null);
|
|
398 |
}
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Gets a {@link DebugContext} object corresponding to {@code options}, creating a new one if
|
48861
|
402 |
* none currently exists. Debug contexts created by this method will have their
|
47798
|
403 |
* {@link DebugDumpHandler}s closed in {@link #afterTest()}.
|
|
404 |
*
|
|
405 |
* @param options currently active options
|
|
406 |
* @param id identification of the compilation or {@code null}
|
|
407 |
* @param method method to use for a proper description of the context or {@code null}
|
|
408 |
* @return configured context for compilation
|
|
409 |
*/
|
|
410 |
protected DebugContext getDebugContext(OptionValues options, String id, ResolvedJavaMethod method) {
|
46640
|
411 |
List<DebugContext> cached = cachedDebugs.get();
|
|
412 |
if (cached == null) {
|
|
413 |
cached = new ArrayList<>();
|
|
414 |
cachedDebugs.set(cached);
|
|
415 |
}
|
|
416 |
for (DebugContext debug : cached) {
|
|
417 |
if (debug.getOptions() == options) {
|
|
418 |
return debug;
|
|
419 |
}
|
|
420 |
}
|
47798
|
421 |
final DebugContext.Description descr;
|
|
422 |
if (method == null) {
|
|
423 |
descr = NO_DESCRIPTION;
|
|
424 |
} else {
|
|
425 |
descr = new DebugContext.Description(method, id == null ? method.getName() : id);
|
|
426 |
}
|
48861
|
427 |
DebugContext debug = DebugContext.create(options, descr, globalMetrics, DEFAULT_LOG_STREAM, getDebugHandlersFactories());
|
46640
|
428 |
cached.add(debug);
|
|
429 |
return debug;
|
|
430 |
}
|
|
431 |
|
48861
|
432 |
private static final GlobalMetrics globalMetrics = new GlobalMetrics();
|
|
433 |
|
|
434 |
static {
|
|
435 |
Runtime.getRuntime().addShutdownHook(new Thread("GlobalMetricsPrinter") {
|
|
436 |
@Override
|
|
437 |
public void run() {
|
|
438 |
globalMetrics.print(new OptionValues(OptionValues.newOptionMap()));
|
|
439 |
}
|
|
440 |
});
|
|
441 |
}
|
46640
|
442 |
private final ThreadLocal<List<DebugContext>> cachedDebugs = new ThreadLocal<>();
|
|
443 |
|
|
444 |
@After
|
|
445 |
public void afterTest() {
|
|
446 |
List<DebugContext> cached = cachedDebugs.get();
|
|
447 |
if (cached != null) {
|
|
448 |
for (DebugContext debug : cached) {
|
48861
|
449 |
debug.close();
|
46640
|
450 |
debug.closeDumpHandlers(true);
|
|
451 |
}
|
|
452 |
}
|
|
453 |
}
|
43972
|
454 |
}
|