1 /*
   2  * Copyright (c) 2011, 2019, 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.jtt.lang;
  26 
  27 import org.graalvm.compiler.jtt.JTTTest;
  28 import org.graalvm.compiler.options.OptionValues;
  29 import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
  30 
  31 import jdk.vm.ci.meta.ResolvedJavaMethod;
  32 
  33 public abstract class UnaryMath extends JTTTest {
  34 
  35     private static final long STEP = Long.MAX_VALUE / 100_000;
  36 
  37     /**
  38      * Tests a unary {@link Math} method on a wide range of values.
  39      */
  40     void testManyValues(OptionValues options, ResolvedJavaMethod method) throws AssertionError {
  41         if (JavaVersionUtil.JAVA_SPEC > 8) {
  42             /*
  43              * GR-8276: Allow for variance on JVMCI > 8 until a JVMCI version that includes
  44              * https://github.com/graalvm/graal-jvmci-8/commit/
  45              * c86fb66f86b8d52a08dd2495d34879d3730f9987 or Graal has stubs that a monotonic with
  46              * other HotSpot implementations of these Math routines.
  47              */
  48             ulpDelta = 2D;
  49         } else {
  50             /*
  51              * Forces the assertion message shows the ulps by which a computed result is wrong.
  52              */
  53             ulpDelta = 0D;
  54         }
  55         Object receiver = null;
  56         long testIteration = 0;
  57         for (long l = Long.MIN_VALUE;; l += STEP) {
  58             double d = Double.longBitsToDouble(l);
  59             Result expect = executeExpected(method, receiver, d);
  60             try {
  61                 testAgainstExpected(options, method, expect, EMPTY, receiver, d);
  62                 testIteration++;
  63             } catch (AssertionError e) {
  64                 throw new AssertionError(String.format("%d: While testing %g [long: %d, hex: %x]", testIteration, d, l, l), e);
  65             }
  66             if (Long.MAX_VALUE - STEP < l) {
  67                 break;
  68             }
  69         }
  70     }
  71 }