Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/PowTests.java
38812 views
/*1* Copyright (c) 2004, 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* @test25* @bug 4984407 503357826* @summary Tests for {Math, StrictMath}.pow27* @author Joseph D. Darcy28*/2930public class PowTests {31private PowTests(){}3233static final double infinityD = Double.POSITIVE_INFINITY;3435static int testPowCase(double input1, double input2, double expected) {36int failures = 0;37failures += Tests.test("StrictMath.pow(double, double)", input1, input2,38StrictMath.pow(input1, input2), expected);39failures += Tests.test("Math.pow(double, double)", input1, input2,40Math.pow(input1, input2), expected);41return failures;42}434445static int testStrictPowCase(double input1, double input2, double expected) {46int failures = 0;47failures += Tests.test("StrictMath.pow(double, double)", input1, input2,48StrictMath.pow(input1, input2), expected);49return failures;50}5152static int testNonstrictPowCase(double input1, double input2, double expected) {53int failures = 0;54failures += Tests.test("Math.pow(double, double)", input1, input2,55Math.pow(input1, input2), expected);56return failures;57}5859/*60* Test for bad negation implementation.61*/62static int testPow() {63int failures = 0;6465double [][] testCases = {66{-0.0, 3.0, -0.0},67{-0.0, 4.0, 0.0},68{-infinityD, -3.0, -0.0},69{-infinityD, -4.0, 0.0},70};7172for (double[] testCase : testCases) {73failures+=testPowCase(testCase[0], testCase[1], testCase[2]);74}7576return failures;77}7879/*80* Test cross-product of different kinds of arguments.81*/82static int testCrossProduct() {83int failures = 0;8485double testData[] = {86Double.NEGATIVE_INFINITY,87/* > -oo */ -Double.MAX_VALUE,88/**/ (double)Long.MIN_VALUE,89/**/ (double) -((1L<<53)+2L),90/**/ (double) -((1L<<53)),91/**/ (double) -((1L<<53)-1L),92/**/ -((double)Integer.MAX_VALUE + 4.0),93/**/ (double)Integer.MIN_VALUE - 1.0,94/**/ (double)Integer.MIN_VALUE,95/**/ (double)Integer.MIN_VALUE + 1.0,96/**/ -Math.PI,97/**/ -3.0,98/**/ -Math.E,99/**/ -2.0,100/**/ -1.0000000000000004,101/* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo)102-1.0,103/* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo)104/* > -1.0 */ -0.9999999999999998,105/**/ -0.5,106/**/ -1.0/3.0,107/* < 0.0 */ -Double.MIN_VALUE,108-0.0,109+0.0,110/* > 0.0 */ +Double.MIN_VALUE,111/**/ +1.0/3.0,112/**/ +0.5,113/**/ +0.9999999999999998,114/* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo)115+1.0,116/* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo)117/**/ +1.0000000000000004,118/**/ +2.0,119/**/ +Math.E,120/**/ +3.0,121/**/ +Math.PI,122/**/ -(double)Integer.MIN_VALUE - 1.0,123/**/ -(double)Integer.MIN_VALUE,124/**/ -(double)Integer.MIN_VALUE + 1.0,125/**/ (double)Integer.MAX_VALUE + 4.0,126/**/ (double) ((1L<<53)-1L),127/**/ (double) ((1L<<53)),128/**/ (double) ((1L<<53)+2L),129/**/ -(double)Long.MIN_VALUE,130/* < oo */ Double.MAX_VALUE,131Double.POSITIVE_INFINITY,132Double.NaN133};134135double NaN = Double.NaN;136for(double x: testData) {137for(double y: testData) {138boolean testPass = false;139double expected=NaN;140double actual;141142// First, switch on y143if( Double.isNaN(y)) {144expected = NaN;145} else if (y == 0.0) {146expected = 1.0;147} else if (Double.isInfinite(y) ) {148if(y > 0) { // x ^ (+oo)149if (Math.abs(x) > 1.0) {150expected = Double.POSITIVE_INFINITY;151} else if (Math.abs(x) == 1.0) {152expected = NaN;153} else if (Math.abs(x) < 1.0) {154expected = +0.0;155} else { // x is NaN156assert Double.isNaN(x);157expected = NaN;158}159} else { // x ^ (-oo)160if (Math.abs(x) > 1.0) {161expected = +0.0;162} else if (Math.abs(x) == 1.0) {163expected = NaN;164} else if (Math.abs(x) < 1.0) {165expected = Double.POSITIVE_INFINITY;166} else { // x is NaN167assert Double.isNaN(x);168expected = NaN;169}170} /* end Double.isInfinite(y) */171} else if (y == 1.0) {172expected = x;173} else if (Double.isNaN(x)) { // Now start switching on x174assert y != 0.0;175expected = NaN;176} else if (x == Double.NEGATIVE_INFINITY) {177expected = (y < 0.0) ? f2(y) :f1(y);178} else if (x == Double.POSITIVE_INFINITY) {179expected = (y < 0.0) ? +0.0 : Double.POSITIVE_INFINITY;180} else if (equivalent(x, +0.0)) {181assert y != 0.0;182expected = (y < 0.0) ? Double.POSITIVE_INFINITY: +0.0;183} else if (equivalent(x, -0.0)) {184assert y != 0.0;185expected = (y < 0.0) ? f1(y): f2(y);186} else if( x < 0.0) {187assert y != 0.0;188failures += testStrictPowCase(x, y, f3(x, y));189failures += testNonstrictPowCase(x, y, f3ns(x, y));190continue;191} else {192// go to next iteration193expected = NaN;194continue;195}196197failures += testPowCase(x, y, expected);198} // y199} // x200return failures;201}202203static boolean equivalent(double a, double b) {204return Double.compare(a, b) == 0;205}206207static double f1(double y) {208return (intClassify(y) == 1)?209Double.NEGATIVE_INFINITY:210Double.POSITIVE_INFINITY;211}212213214static double f2(double y) {215return (intClassify(y) == 1)?-0.0:0.0;216}217218static double f3(double x, double y) {219switch( intClassify(y) ) {220case 0:221return StrictMath.pow(Math.abs(x), y);222// break;223224case 1:225return -StrictMath.pow(Math.abs(x), y);226// break;227228case -1:229return Double.NaN;230// break;231232default:233throw new AssertionError("Bad classification.");234// break;235}236}237238static double f3ns(double x, double y) {239switch( intClassify(y) ) {240case 0:241return Math.pow(Math.abs(x), y);242// break;243244case 1:245return -Math.pow(Math.abs(x), y);246// break;247248case -1:249return Double.NaN;250// break;251252default:253throw new AssertionError("Bad classification.");254// break;255}256}257258static boolean isFinite(double a) {259return (0.0*a == 0);260}261262/**263* Return classification of argument: -1 for non-integers, 0 for264* even integers, 1 for odd integers.265*/266static int intClassify(double a) {267if(!isFinite(a) || // NaNs and infinities268(a != Math.floor(a) )) { // only integers are fixed-points of floor269return -1;270}271else {272// Determine if argument is an odd or even integer.273274a = StrictMath.abs(a); // absolute value doesn't affect odd/even275276if(a+1.0 == a) { // a > maximum odd floating-point integer277return 0; // Large integers are all even278}279else { // Convert double -> long and look at low-order bit280long ell = (long) a;281return ((ell & 0x1L) == (long)1)?1:0;282}283}284}285286public static void main(String [] argv) {287int failures = 0;288289failures += testPow();290failures += testCrossProduct();291292if (failures > 0) {293System.err.println("Testing pow incurred "294+ failures + " failures.");295throw new RuntimeException();296}297}298}299300301