Skip to content

Commit

Permalink
8302800: Augment NaN handling tests of FDLIBM methods
Browse files Browse the repository at this point in the history
Reviewed-by: mbaesken
Backport-of: dfce4e1943f2f95b74b5a9cdde9d738dcffd0b43
  • Loading branch information
Amos Shi committed Aug 26, 2024
1 parent fff35ed commit ba98d8b
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 94 deletions.
25 changes: 11 additions & 14 deletions test/jdk/java/lang/Math/CubeRootTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -25,13 +25,15 @@
* @test
* @library /test/lib
* @build jdk.test.lib.RandomFactory
* @build Tests
* @run main CubeRootTests
* @bug 4347132 4939441 8078672
* @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed)
* @key randomness
*/

import jdk.test.lib.RandomFactory;
import static java.lang.Double.longBitsToDouble;

public class CubeRootTests {
private CubeRootTests(){}
Expand All @@ -42,7 +44,7 @@ private CubeRootTests(){}
// Initialize shared random number generator
static java.util.Random rand = RandomFactory.getRandom();

static int testCubeRootCase(double input, double expected) {
private static int testCubeRootCase(double input, double expected) {
int failures=0;

failures+=Tests.test("Math.cbrt", input, Math::cbrt, expected);
Expand All @@ -53,22 +55,17 @@ static int testCubeRootCase(double input, double expected) {
return failures;
}

static int testCubeRoot() {
private static int testCubeRoot() {
int failures = 0;

for(double nan : Tests.NaNs) {
failures += testCubeRootCase(nan, NaNd);
}

double [][] testCases = {
{NaNd, NaNd},
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY},

{+0.0, +0.0},
{-0.0, -0.0},
{+1.0, +1.0},
Expand Down
24 changes: 11 additions & 13 deletions test/jdk/java/lang/Math/Expm1Tests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,9 +24,14 @@
/*
* @test
* @bug 4851638 4900189 4939441
* @build Tests
* @build Expm1Tests
* @run main Expm1Tests
* @summary Tests for {Math, StrictMath}.expm1
*/

import static java.lang.Double.longBitsToDouble;

/*
* The Taylor expansion of expxm1(x) = exp(x) -1 is
*
Expand All @@ -48,21 +53,14 @@ private Expm1Tests(){}
static final double infinityD = Double.POSITIVE_INFINITY;
static final double NaNd = Double.NaN;

static int testExpm1() {
private static int testExpm1() {
int failures = 0;

for(double nan : Tests.NaNs) {
failures += testExpm1Case(nan, NaNd);
}

double [][] testCases = {
{Double.NaN, NaNd},
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
{infinityD, infinityD},
{-infinityD, -1.0},
{-0.0, -0.0},
Expand Down
52 changes: 18 additions & 34 deletions test/jdk/java/lang/Math/HyperbolicTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,9 +24,14 @@
/*
* @test
* @bug 4851625 4900189 4939441
* @build Tests
* @build HyperbolicTests
* @run main HyperbolicTests
* @summary Tests for {Math, StrictMath}.{sinh, cosh, tanh}
*/

import static java.lang.Double.longBitsToDouble;

public class HyperbolicTests {
private HyperbolicTests(){}

Expand Down Expand Up @@ -248,19 +253,12 @@ static int testSinh() {
3.0);
}

for(double nan : Tests.NaNs) {
failures += testSinhCaseWithUlpDiff(nan, NaNd, 0);
}

double [][] specialTestCases = {
{0.0, 0.0},
{NaNd, NaNd},
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}
};

Expand Down Expand Up @@ -590,19 +588,12 @@ static int testCosh() {
3.0);
}

for(double nan : Tests.NaNs) {
failures += testCoshCaseWithUlpDiff(nan, NaNd, 0);
}

double [][] specialTestCases = {
{0.0, 1.0},
{NaNd, NaNd},
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}
};

Expand Down Expand Up @@ -938,19 +929,12 @@ static int testTanh() {
3.0);
}

for(double nan : Tests.NaNs) {
failures += testTanhCaseWithUlpDiff(nan, NaNd, 0);
}

double [][] specialTestCases = {
{0.0, 0.0},
{NaNd, NaNd},
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
{Double.POSITIVE_INFINITY, 1.0}
};

Expand Down
187 changes: 187 additions & 0 deletions test/jdk/java/lang/Math/InverseTrigTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
* @bug 8302026
* @build Tests
* @build InverseTrigTests
* @run main InverseTrigTests
* @summary Tests for {Math, StrictMath}.{asin, acos, atan}
*/

import static java.lang.Double.longBitsToDouble;

public class InverseTrigTests {
private InverseTrigTests(){}

public static void main(String... args) {
int failures = 0;

failures += testAsinSpecialCases();
failures += testAcosSpecialCases();
failures += testAtanSpecialCases();

if (failures > 0) {
System.err.println("Testing inverse trig mthods incurred "
+ failures + " failures.");
throw new RuntimeException();
}
}

private static final double InfinityD = Double.POSITIVE_INFINITY;
private static final double NaNd = Double.NaN;

/**
* From the spec for Math.asin:
*
* "Special cases:
*
* If the argument is NaN or its absolute value is greater than 1,
* then the result is NaN.
*
* If the argument is zero, then the result is a zero with the
* same sign as the argument."
*/
private static int testAsinSpecialCases() {
int failures = 0;

for(double nan : Tests.NaNs) {
failures += testAsinCase(nan, NaNd);
}

double [][] testCases = {
{Math.nextUp(1.0), NaNd},
{Math.nextDown(-1.0), NaNd},
{ InfinityD, NaNd},
{-InfinityD, NaNd},

{-0.0, -0.0},
{+0.0, +0.0},
};

for(int i = 0; i < testCases.length; i++) {
failures += testAsinCase(testCases[i][0],
testCases[i][1]);
}

return failures;
}

private static int testAsinCase(double input, double expected) {
int failures=0;

failures += Tests.test("Math.asin", input, Math::asin, expected);
failures += Tests.test("StrictMath.asin", input, StrictMath::asin, expected);

return failures;
}

/**
* From the spec for Math.acos:
*
* "Special case:
*
* If the argument is NaN or its absolute value is greater than 1,
* then the result is NaN.
*
* If the argument is 1.0, the result is positive zero."
*/
private static int testAcosSpecialCases() {
int failures = 0;

for(double nan : Tests.NaNs) {
failures += testAcosCase(nan, NaNd);
}

double [][] testCases = {
{Math.nextUp(1.0), NaNd},
{Math.nextDown(-1.0), NaNd},
{InfinityD, NaNd},
{-InfinityD, NaNd},

{1.0, +0.0},
};

for(int i = 0; i < testCases.length; i++) {
failures += testAcosCase(testCases[i][0],
testCases[i][1]);
}

return failures;
}

private static int testAcosCase(double input, double expected) {
int failures=0;

failures += Tests.test("Math.acos", input, Math::acos, expected);
failures += Tests.test("StrictMath.acos", input, StrictMath::acos, expected);

return failures;
}

/**
* From the spec for Math.atan:
*
* "Special cases:
*
* If the argument is NaN, then the result is NaN.
*
* If the argument is zero, then the result is a zero with the
* same sign as the argument.
*
* If the argument is infinite, then the result is the closest
* value to pi/2 with the same sign as the input."
*/
private static int testAtanSpecialCases() {
int failures = 0;

for(double nan : Tests.NaNs) {
failures += testAtanCase(nan, NaNd);
}

double [][] testCases = {
{-0.0, -0.0},
{+0.0, +0.0},

{ InfinityD, +Math.PI/2.0},
{-InfinityD, -Math.PI/2.0},
};

for(int i = 0; i < testCases.length; i++) {
failures += testAtanCase(testCases[i][0],
testCases[i][1]);
}

return failures;
}

private static int testAtanCase(double input, double expected) {
int failures=0;

failures += Tests.test("Math.atan", input, Math::atan, expected);
failures += Tests.test("StrictMath.atan", input, StrictMath::atan, expected);

return failures;
}
}
Loading

0 comments on commit ba98d8b

Please sign in to comment.