Path: blob/master/test/jdk/java/lang/StrictMath/Tests.java
41149 views
/*1* Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*26* Shared static test method for StrictMath tests.27*/282930public class Tests {31private Tests(){}3233static int test(String testName,34double input,35double result,36double expected) {37if (Double.compare(expected, result ) != 0) {38System.err.println("Failure for " + testName + ":\n" +39"\tFor input " + input + "\t(" + Double.toHexString(input) + ")\n" +40"\texpected " + expected + "\t(" + Double.toHexString(expected) + ")\n" +41"\tgot " + result + "\t(" + Double.toHexString(result) + ").");42return 1;43}44else45return 0;46}4748static int test(String testName, double input1, double input2,49double result, double expected) {50if (Double.compare(expected, result ) != 0) {51System.err.println("Failure for " + testName + ":\n" +52"\tFor input " + input1 + "\t(" + Double.toHexString(input1) + "), " +53+ input2 + "\t(" + Double.toHexString(input2) + ")\n" +54"\texpected " + expected + "\t(" + Double.toHexString(expected) + ")\n" +55"\tgot " + result + "\t(" + Double.toHexString(result) + ").");56return 1;57}58else59return 0;60}6162/**63* Returns a double over the normalized range of floating-point values.64* @return a double over the normalized range of floating-point values65*/66static double createRandomDouble(java.util.Random random) {67final int EXPONENT_RANGE = Double.MAX_EXPONENT - Double.MIN_EXPONENT + 1;6869int targetExponent = Double.MIN_EXPONENT + random.nextInt(EXPONENT_RANGE + 1);70double tmp = random.nextDouble(); // Double in the range of [0.0, 1.0)71int tmpExponent = Math.getExponent(tmp);72return Math.scalb(tmp, targetExponent - tmpExponent);73}74}757677