Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/Tests.java
38812 views
/*1* Copyright (c) 2003, 2012, 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* Shared static test methods for numerical tests. Sharing these25* helper test methods avoids repeated functions in the various test26* programs. The test methods return 1 for a test failure and 0 for27* success. The order of arguments to the test methods is generally28* the test name, followed by the test arguments, the computed result,29* and finally the expected result.30*/3132import sun.misc.FpUtils;3334public class Tests {35private Tests(){}; // do not instantiate3637public static String toHexString(float f) {38if (!Float.isNaN(f))39return Float.toHexString(f);40else41return "NaN(0x" + Integer.toHexString(Float.floatToRawIntBits(f)) + ")";42}4344public static String toHexString(double d) {45if (!Double.isNaN(d))46return Double.toHexString(d);47else48return "NaN(0x" + Long.toHexString(Double.doubleToRawLongBits(d)) + ")";49}5051/**52* Return the floating-point value next larger in magnitude.53*/54public static double nextOut(double d) {55if (d > 0.0)56return Math.nextUp(d);57else58return -Math.nextUp(-d);59}6061public static int test(String testName, float input,62boolean result, boolean expected) {63if (expected != result) {64System.err.println("Failure for " + testName + ":\n" +65"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +66"\texpected " + expected + "\n" +67"\tgot " + result + ").");68return 1;69}70else71return 0;72}7374public static int test(String testName, double input,75boolean result, boolean expected) {76if (expected != result) {77System.err.println("Failure for " + testName + ":\n" +78"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +79"\texpected " + expected + "\n" +80"\tgot " + result + ").");81return 1;82}83else84return 0;85}8687public static int test(String testName, float input1, float input2,88boolean result, boolean expected) {89if (expected != result) {90System.err.println("Failure for " + testName + ":\n" +91"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "92+ input2 + "\t(" + toHexString(input2) + ")\n" +93"\texpected " + expected + "\n" +94"\tgot " + result + ").");95return 1;96}97return 0;98}99100public static int test(String testName, double input1, double input2,101boolean result, boolean expected) {102if (expected != result) {103System.err.println("Failure for " + testName + ":\n" +104"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "105+ input2 + "\t(" + toHexString(input2) + ")\n" +106"\texpected " + expected + "\n" +107"\tgot " + result + ").");108return 1;109}110return 0;111}112113public static int test(String testName, float input,114int result, int expected) {115if (expected != result) {116System.err.println("Failure for " + testName + ":\n" +117"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +118"\texpected " + expected + "\n" +119"\tgot " + result + ").");120return 1;121}122return 0;123}124125public static int test(String testName, double input,126int result, int expected) {127if (expected != result) {128System.err.println("Failure for " + testName + ":\n" +129"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +130"\texpected " + expected + "\n" +131"\tgot " + result + ").");132return 1;133}134else135return 0;136}137138public static int test(String testName, float input,139float result, float expected) {140if (Float.compare(expected, result) != 0 ) {141System.err.println("Failure for " + testName + ":\n" +142"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +143"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +144"\tgot " + result + "\t(" + toHexString(result) + ").");145return 1;146}147else148return 0;149}150151152public static int test(String testName, double input,153double result, double expected) {154if (Double.compare(expected, result ) != 0) {155System.err.println("Failure for " + testName + ":\n" +156"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +157"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +158"\tgot " + result + "\t(" + toHexString(result) + ").");159return 1;160}161else162return 0;163}164165public static int test(String testName,166float input1, double input2,167float result, float expected) {168if (Float.compare(expected, result ) != 0) {169System.err.println("Failure for " + testName + ":\n" +170"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "171+ input2 + "\t(" + toHexString(input2) + ")\n" +172"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +173"\tgot " + result + "\t(" + toHexString(result) + ").");174return 1;175}176else177return 0;178}179180public static int test(String testName,181double input1, double input2,182double result, double expected) {183if (Double.compare(expected, result ) != 0) {184System.err.println("Failure for " + testName + ":\n" +185"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "186+ input2 + "\t(" + toHexString(input2) + ")\n" +187"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +188"\tgot " + result + "\t(" + toHexString(result) + ").");189return 1;190}191else192return 0;193}194195public static int test(String testName,196float input1, int input2,197float result, float expected) {198if (Float.compare(expected, result ) != 0) {199System.err.println("Failure for " + testName + ":\n" +200"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "201+ input2 + "\n" +202"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +203"\tgot " + result + "\t(" + toHexString(result) + ").");204return 1;205}206else207return 0;208}209210public static int test(String testName,211double input1, int input2,212double result, double expected) {213if (Double.compare(expected, result ) != 0) {214System.err.println("Failure for " + testName + ":\n" +215"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "216+ input2 + "\n" +217"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +218"\tgot " + result + "\t(" + toHexString(result) + ").");219return 1;220}221else222return 0;223}224225static int testUlpCore(double result, double expected, double ulps) {226// We assume we won't be unlucky and have an inexact expected227// be nextDown(2^i) when 2^i would be the correctly rounded228// answer. This would cause the ulp size to be half as large229// as it should be, doubling the measured error).230231if (Double.compare(expected, result) == 0) {232return 0; // result and expected are equivalent233} else {234if( ulps == 0.0) {235// Equivalent results required but not found236return 1;237} else {238double difference = expected - result;239if (FpUtils.isUnordered(expected, result) ||240Double.isNaN(difference) ||241// fail if greater than or unordered242!(Math.abs( difference/Math.ulp(expected) ) <= Math.abs(ulps)) ) {243return 1;244}245else246return 0;247}248}249}250251// One input argument.252public static int testUlpDiff(String testName, double input,253double result, double expected, double ulps) {254int code = testUlpCore(result, expected, ulps);255if (code == 1) {256System.err.println("Failure for " + testName + ":\n" +257"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +258"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +259"\tgot " + result + "\t(" + toHexString(result) + ");\n" +260"\tdifference greater than ulp tolerance " + ulps);261}262return code;263}264265// Two input arguments.266public static int testUlpDiff(String testName, double input1, double input2,267double result, double expected, double ulps) {268int code = testUlpCore(result, expected, ulps);269if (code == 1) {270System.err.println("Failure for " + testName + ":\n" +271"\tFor inputs " + input1 + "\t(" + toHexString(input1) + ") and "272+ input2 + "\t(" + toHexString(input2) + ")\n" +273"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +274"\tgot " + result + "\t(" + toHexString(result) + ");\n" +275"\tdifference greater than ulp tolerance " + ulps);276}277return code;278}279280// For a successful test, the result must be within the ulp bound of281// expected AND the result must have absolute value less than or282// equal to absBound.283public static int testUlpDiffWithAbsBound(String testName, double input,284double result, double expected,285double ulps, double absBound) {286int code = 0; // return code value287288if (!(StrictMath.abs(result) <= StrictMath.abs(absBound)) &&289!Double.isNaN(expected)) {290code = 1;291} else292code = testUlpCore(result, expected, ulps);293294if (code == 1) {295System.err.println("Failure for " + testName + ":\n" +296"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +297"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +298"\tgot " + result + "\t(" + toHexString(result) + ");\n" +299"\tdifference greater than ulp tolerance " + ulps +300" or the result has larger magnitude than " + absBound);301}302return code;303}304305// For a successful test, the result must be within the ulp bound of306// expected AND the result must have absolute value greater than307// or equal to the lowerBound.308public static int testUlpDiffWithLowerBound(String testName, double input,309double result, double expected,310double ulps, double lowerBound) {311int code = 0; // return code value312313if (!(result >= lowerBound) && !Double.isNaN(expected)) {314code = 1;315} else316code = testUlpCore(result, expected, ulps);317318if (code == 1) {319System.err.println("Failure for " + testName +320":\n" +321"\tFor input " + input + "\t(" + toHexString(input) + ")" +322"\n\texpected " + expected + "\t(" + toHexString(expected) + ")" +323"\n\tgot " + result + "\t(" + toHexString(result) + ");" +324"\ndifference greater than ulp tolerance " + ulps +325" or result not greater than or equal to the bound " + lowerBound);326}327return code;328}329330public static int testTolerance(String testName, double input,331double result, double expected, double tolerance) {332if (Double.compare(expected, result ) != 0) {333double difference = expected - result;334if (FpUtils.isUnordered(expected, result) ||335Double.isNaN(difference) ||336// fail if greater than or unordered337!(Math.abs((difference)/expected) <= StrictMath.pow(10, -tolerance)) ) {338System.err.println("Failure for " + testName + ":\n" +339"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +340"\texpected " + expected + "\t(" + toHexString(expected) + ")\n" +341"\tgot " + result + "\t(" + toHexString(result) + ");\n" +342"\tdifference greater than tolerance 10^-" + tolerance);343return 1;344}345return 0;346}347else348return 0;349}350351// For a successful test, the result must be within the upper and352// lower bounds.353public static int testBounds(String testName, double input, double result,354double bound1, double bound2) {355if ((result >= bound1 && result <= bound2) ||356(result <= bound1 && result >= bound2))357return 0;358else {359double lowerBound = Math.min(bound1, bound2);360double upperBound = Math.max(bound1, bound2);361System.err.println("Failure for " + testName + ":\n" +362"\tFor input " + input + "\t(" + toHexString(input) + ")\n" +363"\tgot " + result + "\t(" + toHexString(result) + ");\n" +364"\toutside of range\n" +365"\t[" + lowerBound + "\t(" + toHexString(lowerBound) + "), " +366upperBound + "\t(" + toHexString(upperBound) + ")]");367return 1;368}369}370}371372373