Path: blob/master/test/jdk/java/lang/Math/PowTests.java
41149 views
/*1* Copyright (c) 2004, 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* @test25* @bug 4984407 5033578 813479526* @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-0x1.0p65,91-0x1.0000000000001p64,92-0x1.0p64,93/**/ (double) -((1L<<53)),94/**/ (double) -((1L<<53)-1L),95/**/ -((double)Integer.MAX_VALUE + 4.0),96/**/ (double)Integer.MIN_VALUE - 1.0,97/**/ (double)Integer.MIN_VALUE,98/**/ (double)Integer.MIN_VALUE + 1.0,99-0x1.0p31 + 2.0,100-0x1.0p31 + 1.0,101-0x1.0000000000001p31,102-0x1.0p31,103/**/ -Math.PI,104/**/ -3.0,105/**/ -Math.E,106/**/ -2.0,107/**/ -1.0000000000000004,108/* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo)109-1.0,110/* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo)111/* > -1.0 */ -0.9999999999999998,112-0x1.fffffp-1,113-0x1.ffffeffffffffp-1,114/**/ -0.5,115/**/ -1.0/3.0,116/* < 0.0 */ -Double.MIN_VALUE,117-0.0,118+0.0,119/* > 0.0 */ +Double.MIN_VALUE,120/**/ +1.0/3.0,121/**/ +0.5,122+0x1.ffffeffffffffp-1,123+0x1.fffffp-1,124/**/ +0.9999999999999998,125/* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo)126+1.0,127/* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo)128/**/ +1.0000000000000004,129/**/ +2.0,130/**/ +Math.E,131/**/ +3.0,132/**/ +Math.PI,1330x1.0p31,1340x1.0000000000001p31,1350x1.0p31 + 1.0,1360x1.0p31 + 2.0,137/**/ -(double)Integer.MIN_VALUE - 1.0,138/**/ -(double)Integer.MIN_VALUE,139/**/ -(double)Integer.MIN_VALUE + 1.0,140/**/ (double)Integer.MAX_VALUE + 4.0,141/**/ (double) ((1L<<53)-1L),142/**/ (double) ((1L<<53)),143/**/ (double) ((1L<<53)+2L),1440x1.0p64,1450x1.0000000000001p64,1460x1.0p65,147/**/ -(double)Long.MIN_VALUE,148/* < oo */ Double.MAX_VALUE,149Double.POSITIVE_INFINITY,150Double.NaN151};152153double NaN = Double.NaN;154for(double x: testData) {155for(double y: testData) {156boolean testPass = false;157double expected=NaN;158double actual;159160// First, switch on y161if( Double.isNaN(y)) {162expected = NaN;163} else if (y == 0.0) {164expected = 1.0;165} else if (Double.isInfinite(y) ) {166if(y > 0) { // x ^ (+oo)167if (Math.abs(x) > 1.0) {168expected = Double.POSITIVE_INFINITY;169} else if (Math.abs(x) == 1.0) {170expected = NaN;171} else if (Math.abs(x) < 1.0) {172expected = +0.0;173} else { // x is NaN174assert Double.isNaN(x);175expected = NaN;176}177} else { // x ^ (-oo)178if (Math.abs(x) > 1.0) {179expected = +0.0;180} else if (Math.abs(x) == 1.0) {181expected = NaN;182} else if (Math.abs(x) < 1.0) {183expected = Double.POSITIVE_INFINITY;184} else { // x is NaN185assert Double.isNaN(x);186expected = NaN;187}188} /* end Double.isInfinite(y) */189} else if (y == 1.0) {190expected = x;191} else if (Double.isNaN(x)) { // Now start switching on x192assert y != 0.0;193expected = NaN;194} else if (x == Double.NEGATIVE_INFINITY) {195expected = (y < 0.0) ? f2(y) :f1(y);196} else if (x == Double.POSITIVE_INFINITY) {197expected = (y < 0.0) ? +0.0 : Double.POSITIVE_INFINITY;198} else if (equivalent(x, +0.0)) {199assert y != 0.0;200expected = (y < 0.0) ? Double.POSITIVE_INFINITY: +0.0;201} else if (equivalent(x, -0.0)) {202assert y != 0.0;203expected = (y < 0.0) ? f1(y): f2(y);204} else if( x < 0.0) {205assert y != 0.0;206failures += testStrictPowCase(x, y, f3(x, y));207failures += testNonstrictPowCase(x, y, f3ns(x, y));208continue;209} else {210// go to next iteration211expected = NaN;212continue;213}214215failures += testPowCase(x, y, expected);216} // y217} // x218return failures;219}220221static boolean equivalent(double a, double b) {222return Double.compare(a, b) == 0;223}224225static double f1(double y) {226return (intClassify(y) == 1)?227Double.NEGATIVE_INFINITY:228Double.POSITIVE_INFINITY;229}230231232static double f2(double y) {233return (intClassify(y) == 1)?-0.0:0.0;234}235236static double f3(double x, double y) {237switch( intClassify(y) ) {238case 0:239return StrictMath.pow(Math.abs(x), y);240// break;241242case 1:243return -StrictMath.pow(Math.abs(x), y);244// break;245246case -1:247return Double.NaN;248// break;249250default:251throw new AssertionError("Bad classification.");252// break;253}254}255256static double f3ns(double x, double y) {257switch( intClassify(y) ) {258case 0:259return Math.pow(Math.abs(x), y);260// break;261262case 1:263return -Math.pow(Math.abs(x), y);264// break;265266case -1:267return Double.NaN;268// break;269270default:271throw new AssertionError("Bad classification.");272// break;273}274}275276static boolean isFinite(double a) {277return (0.0 * a == 0);278}279280/**281* Return classification of argument: -1 for non-integers, 0 for282* even integers, 1 for odd integers.283*/284static int intClassify(double a) {285if(!isFinite(a) || // NaNs and infinities286(a != Math.floor(a) )) { // only integers are fixed-points of floor287return -1;288}289else {290// Determine if argument is an odd or even integer.291292a = StrictMath.abs(a); // absolute value doesn't affect odd/even293294if(a+1.0 == a) { // a > maximum odd floating-point integer295return 0; // Large integers are all even296}297else { // Convert double -> long and look at low-order bit298long ell = (long) a;299return ((ell & 0x1L) == (long)1)?1:0;300}301}302}303304public static void main(String [] argv) {305int failures = 0;306307failures += testPow();308failures += testCrossProduct();309310if (failures > 0) {311System.err.println("Testing pow incurred "312+ failures + " failures.");313throw new RuntimeException();314}315}316}317318319