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