PowTests.java revision 2362
486N/A/*
486N/A * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
486N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
486N/A *
486N/A * This code is free software; you can redistribute it and/or modify it
486N/A * under the terms of the GNU General Public License version 2 only, as
486N/A * published by the Free Software Foundation.
486N/A *
486N/A * This code is distributed in the hope that it will be useful, but WITHOUT
486N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
486N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
486N/A * version 2 for more details (a copy is included in the LICENSE file that
486N/A * accompanied this code).
486N/A *
486N/A * You should have received a copy of the GNU General Public License version
486N/A * 2 along with this work; if not, write to the Free Software Foundation,
486N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
486N/A *
486N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
873N/A * or visit www.oracle.com if you need additional information or have any
486N/A * questions.
486N/A */
486N/A
486N/A/*
486N/A * @test
4362N/A * @bug 4984407 5033578
486N/A * @summary Tests for {Math, StrictMath}.pow
486N/A * @author Joseph D. Darcy
619N/A */
619N/A
619N/Apublic class PowTests {
619N/A private PowTests(){}
619N/A
3488N/A static final double infinityD = Double.POSITIVE_INFINITY;
619N/A
619N/A static int testPowCase(double input1, double input2, double expected) {
619N/A int failures = 0;
3824N/A failures += Tests.test("StrictMath.pow(double, double)", input1, input2,
3824N/A StrictMath.pow(input1, input2), expected);
3824N/A failures += Tests.test("Math.pow(double, double)", input1, input2,
619N/A Math.pow(input1, input2), expected);
619N/A return failures;
486N/A }
536N/A
2976N/A
2976N/A static int testStrictPowCase(double input1, double input2, double expected) {
536N/A int failures = 0;
3972N/A failures += Tests.test("StrictMath.pow(double, double)", input1, input2,
3972N/A StrictMath.pow(input1, input2), expected);
3972N/A return failures;
3972N/A }
3972N/A
3972N/A static int testNonstrictPowCase(double input1, double input2, double expected) {
3972N/A int failures = 0;
3978N/A failures += Tests.test("Math.pow(double, double)", input1, input2,
3972N/A Math.pow(input1, input2), expected);
3972N/A return failures;
3978N/A }
3978N/A
3978N/A /*
3972N/A * Test for bad negation implementation.
3972N/A */
3972N/A static int testPow() {
3972N/A int failures = 0;
3972N/A
3972N/A double [][] testCases = {
3972N/A {-0.0, 3.0, -0.0},
2976N/A {-0.0, 4.0, 0.0},
2976N/A {-infinityD, -3.0, -0.0},
2976N/A {-infinityD, -4.0, 0.0},
3860N/A };
3860N/A
3824N/A for (double[] testCase : testCases) {
2976N/A failures+=testPowCase(testCase[0], testCase[1], testCase[2]);
2976N/A }
1181N/A
2976N/A return failures;
1181N/A }
536N/A
3860N/A /*
3860N/A * Test cross-product of different kinds of arguments.
3860N/A */
3860N/A static int testCrossProduct() {
3860N/A int failures = 0;
3860N/A
3860N/A double testData[] = {
2976N/A Double.NEGATIVE_INFINITY,
830N/A/* > -oo */ -Double.MAX_VALUE,
830N/A/**/ (double)Long.MIN_VALUE,
830N/A/**/ (double) -((1L<<53)+2L),
830N/A/**/ (double) -((1L<<53)),
830N/A/**/ (double) -((1L<<53)-1L),
830N/A/**/ -((double)Integer.MAX_VALUE + 4.0),
2650N/A/**/ (double)Integer.MIN_VALUE - 1.0,
830N/A/**/ (double)Integer.MIN_VALUE,
830N/A/**/ (double)Integer.MIN_VALUE + 1.0,
830N/A/**/ -Math.PI,
2296N/A/**/ -3.0,
830N/A/**/ -Math.E,
961N/A/**/ -2.0,
961N/A/**/ -1.0000000000000004,
961N/A/* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo)
486N/A -1.0,
2976N/A/* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo)
486N/A/* > -1.0 */ -0.9999999999999998,
486N/A/**/ -0.5,
486N/A/**/ -1.0/3.0,
486N/A/* < 0.0 */ -Double.MIN_VALUE,
2296N/A -0.0,
2296N/A +0.0,
2650N/A/* > 0.0 */ +Double.MIN_VALUE,
4362N/A/**/ +1.0/3.0,
2296N/A/**/ +0.5,
2650N/A/**/ +0.9999999999999998,
4362N/A/* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo)
2296N/A +1.0,
2402N/A/* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo)
2402N/A/**/ +1.0000000000000004,
2402N/A/**/ +2.0,
2402N/A/**/ +Math.E,
2650N/A/**/ +3.0,
2402N/A/**/ +Math.PI,
2422N/A/**/ -(double)Integer.MIN_VALUE - 1.0,
2402N/A/**/ -(double)Integer.MIN_VALUE,
2402N/A/**/ -(double)Integer.MIN_VALUE + 1.0,
2402N/A/**/ (double)Integer.MAX_VALUE + 4.0,
2402N/A/**/ (double) ((1L<<53)-1L),
2402N/A/**/ (double) ((1L<<53)),
2402N/A/**/ (double) ((1L<<53)+2L),
2402N/A/**/ -(double)Long.MIN_VALUE,
2402N/A/* < oo */ Double.MAX_VALUE,
2402N/A Double.POSITIVE_INFINITY,
4362N/A Double.NaN
961N/A };
961N/A
961N/A double NaN = Double.NaN;
961N/A for(double x: testData) {
961N/A for(double y: testData) {
961N/A boolean testPass = false;
961N/A double expected=NaN;
1024N/A double actual;
2976N/A
961N/A // First, switch on y
961N/A if( Double.isNaN(y)) {
961N/A expected = NaN;
3590N/A } else if (y == 0.0) {
3590N/A expected = 1.0;
3590N/A } else if (Double.isInfinite(y) ) {
3590N/A if(y > 0) { // x ^ (+oo)
3590N/A if (Math.abs(x) > 1.0) {
3590N/A expected = Double.POSITIVE_INFINITY;
3590N/A } else if (Math.abs(x) == 1.0) {
3590N/A expected = NaN;
3590N/A } else if (Math.abs(x) < 1.0) {
3590N/A expected = +0.0;
3590N/A } else { // x is NaN
961N/A assert Double.isNaN(x);
961N/A expected = NaN;
961N/A }
961N/A } else { // x ^ (-oo)
961N/A if (Math.abs(x) > 1.0) {
961N/A expected = +0.0;
961N/A } else if (Math.abs(x) == 1.0) {
486N/A expected = NaN;
} else if (Math.abs(x) < 1.0) {
expected = Double.POSITIVE_INFINITY;
} else { // x is NaN
assert Double.isNaN(x);
expected = NaN;
}
} /* end Double.isInfinite(y) */
} else if (y == 1.0) {
expected = x;
} else if (Double.isNaN(x)) { // Now start switching on x
assert y != 0.0;
expected = NaN;
} else if (x == Double.NEGATIVE_INFINITY) {
expected = (y < 0.0) ? f2(y) :f1(y);
} else if (x == Double.POSITIVE_INFINITY) {
expected = (y < 0.0) ? +0.0 : Double.POSITIVE_INFINITY;
} else if (equivalent(x, +0.0)) {
assert y != 0.0;
expected = (y < 0.0) ? Double.POSITIVE_INFINITY: +0.0;
} else if (equivalent(x, -0.0)) {
assert y != 0.0;
expected = (y < 0.0) ? f1(y): f2(y);
} else if( x < 0.0) {
assert y != 0.0;
failures += testStrictPowCase(x, y, f3(x, y));
failures += testNonstrictPowCase(x, y, f3ns(x, y));
continue;
} else {
// go to next iteration
expected = NaN;
continue;
}
failures += testPowCase(x, y, expected);
} // y
} // x
return failures;
}
static boolean equivalent(double a, double b) {
return Double.compare(a, b) == 0;
}
static double f1(double y) {
return (intClassify(y) == 1)?
Double.NEGATIVE_INFINITY:
Double.POSITIVE_INFINITY;
}
static double f2(double y) {
return (intClassify(y) == 1)?-0.0:0.0;
}
static double f3(double x, double y) {
switch( intClassify(y) ) {
case 0:
return StrictMath.pow(Math.abs(x), y);
// break;
case 1:
return -StrictMath.pow(Math.abs(x), y);
// break;
case -1:
return Double.NaN;
// break;
default:
throw new AssertionError("Bad classification.");
// break;
}
}
static double f3ns(double x, double y) {
switch( intClassify(y) ) {
case 0:
return Math.pow(Math.abs(x), y);
// break;
case 1:
return -Math.pow(Math.abs(x), y);
// break;
case -1:
return Double.NaN;
// break;
default:
throw new AssertionError("Bad classification.");
// break;
}
}
static boolean isFinite(double a) {
return (0.0*a == 0);
}
/**
* Return classification of argument: -1 for non-integers, 0 for
* even integers, 1 for odd integers.
*/
static int intClassify(double a) {
if(!isFinite(a) || // NaNs and infinities
(a != Math.floor(a) )) { // only integers are fixed-points of floor
return -1;
}
else {
// Determine if argument is an odd or even integer.
a = StrictMath.abs(a); // absolute value doesn't affect odd/even
if(a+1.0 == a) { // a > maximum odd floating-point integer
return 0; // Large integers are all even
}
else { // Convert double -> long and look at low-order bit
long ell = (long) a;
return ((ell & 0x1L) == (long)1)?1:0;
}
}
}
public static void main(String [] argv) {
int failures = 0;
failures += testPow();
failures += testCrossProduct();
if (failures > 0) {
System.err.println("Testing pow incurred "
+ failures + " failures.");
throw new RuntimeException();
}
}
}