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