Skip to content

Commit 803c2c2

Browse files
author
Amos Shi
committed
8302800: Augment NaN handling tests of FDLIBM methods
Reviewed-by: mbaesken Backport-of: dfce4e1943f2f95b74b5a9cdde9d738dcffd0b43
1 parent cd0bb4d commit 803c2c2

File tree

7 files changed

+286
-94
lines changed

7 files changed

+286
-94
lines changed

test/jdk/java/lang/Math/CubeRootTests.java

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -25,13 +25,15 @@
2525
* @test
2626
* @library /test/lib
2727
* @build jdk.test.lib.RandomFactory
28+
* @build Tests
2829
* @run main CubeRootTests
2930
* @bug 4347132 4939441 8078672
3031
* @summary Tests for {Math, StrictMath}.cbrt (use -Dseed=X to set PRNG seed)
3132
* @key randomness
3233
*/
3334

3435
import jdk.test.lib.RandomFactory;
36+
import static java.lang.Double.longBitsToDouble;
3537

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

45-
static int testCubeRootCase(double input, double expected) {
47+
private static int testCubeRootCase(double input, double expected) {
4648
int failures=0;
4749

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

56-
static int testCubeRoot() {
58+
private static int testCubeRoot() {
5759
int failures = 0;
60+
61+
for(double nan : Tests.NaNs) {
62+
failures += testCubeRootCase(nan, NaNd);
63+
}
64+
5865
double [][] testCases = {
59-
{NaNd, NaNd},
60-
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
61-
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
62-
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
63-
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
64-
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
65-
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
66-
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
67-
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
68-
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
69-
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
7066
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY},
7167
{Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY},
68+
7269
{+0.0, +0.0},
7370
{-0.0, -0.0},
7471
{+1.0, +1.0},

test/jdk/java/lang/Math/Expm1Tests.java

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,9 +24,14 @@
2424
/*
2525
* @test
2626
* @bug 4851638 4900189 4939441
27+
* @build Tests
28+
* @build Expm1Tests
29+
* @run main Expm1Tests
2730
* @summary Tests for {Math, StrictMath}.expm1
2831
*/
2932

33+
import static java.lang.Double.longBitsToDouble;
34+
3035
/*
3136
* The Taylor expansion of expxm1(x) = exp(x) -1 is
3237
*
@@ -48,21 +53,14 @@ private Expm1Tests(){}
4853
static final double infinityD = Double.POSITIVE_INFINITY;
4954
static final double NaNd = Double.NaN;
5055

51-
static int testExpm1() {
56+
private static int testExpm1() {
5257
int failures = 0;
5358

59+
for(double nan : Tests.NaNs) {
60+
failures += testExpm1Case(nan, NaNd);
61+
}
62+
5463
double [][] testCases = {
55-
{Double.NaN, NaNd},
56-
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
57-
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
58-
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
59-
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
60-
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
61-
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
62-
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
63-
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
64-
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
65-
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
6664
{infinityD, infinityD},
6765
{-infinityD, -1.0},
6866
{-0.0, -0.0},

test/jdk/java/lang/Math/HyperbolicTests.java

+18-34
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,9 +24,14 @@
2424
/*
2525
* @test
2626
* @bug 4851625 4900189 4939441
27+
* @build Tests
28+
* @build HyperbolicTests
29+
* @run main HyperbolicTests
2730
* @summary Tests for {Math, StrictMath}.{sinh, cosh, tanh}
2831
*/
2932

33+
import static java.lang.Double.longBitsToDouble;
34+
3035
public class HyperbolicTests {
3136
private HyperbolicTests(){}
3237

@@ -248,19 +253,12 @@ static int testSinh() {
248253
3.0);
249254
}
250255

256+
for(double nan : Tests.NaNs) {
257+
failures += testSinhCaseWithUlpDiff(nan, NaNd, 0);
258+
}
259+
251260
double [][] specialTestCases = {
252261
{0.0, 0.0},
253-
{NaNd, NaNd},
254-
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
255-
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
256-
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
257-
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
258-
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
259-
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
260-
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
261-
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
262-
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
263-
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
264262
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}
265263
};
266264

@@ -590,19 +588,12 @@ static int testCosh() {
590588
3.0);
591589
}
592590

591+
for(double nan : Tests.NaNs) {
592+
failures += testCoshCaseWithUlpDiff(nan, NaNd, 0);
593+
}
594+
593595
double [][] specialTestCases = {
594596
{0.0, 1.0},
595-
{NaNd, NaNd},
596-
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
597-
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
598-
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
599-
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
600-
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
601-
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
602-
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
603-
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
604-
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
605-
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
606597
{Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY}
607598
};
608599

@@ -938,19 +929,12 @@ static int testTanh() {
938929
3.0);
939930
}
940931

932+
for(double nan : Tests.NaNs) {
933+
failures += testTanhCaseWithUlpDiff(nan, NaNd, 0);
934+
}
935+
941936
double [][] specialTestCases = {
942937
{0.0, 0.0},
943-
{NaNd, NaNd},
944-
{Double.longBitsToDouble(0x7FF0000000000001L), NaNd},
945-
{Double.longBitsToDouble(0xFFF0000000000001L), NaNd},
946-
{Double.longBitsToDouble(0x7FF8555555555555L), NaNd},
947-
{Double.longBitsToDouble(0xFFF8555555555555L), NaNd},
948-
{Double.longBitsToDouble(0x7FFFFFFFFFFFFFFFL), NaNd},
949-
{Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL), NaNd},
950-
{Double.longBitsToDouble(0x7FFDeadBeef00000L), NaNd},
951-
{Double.longBitsToDouble(0xFFFDeadBeef00000L), NaNd},
952-
{Double.longBitsToDouble(0x7FFCafeBabe00000L), NaNd},
953-
{Double.longBitsToDouble(0xFFFCafeBabe00000L), NaNd},
954938
{Double.POSITIVE_INFINITY, 1.0}
955939
};
956940

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8302026
27+
* @build Tests
28+
* @build InverseTrigTests
29+
* @run main InverseTrigTests
30+
* @summary Tests for {Math, StrictMath}.{asin, acos, atan}
31+
*/
32+
33+
import static java.lang.Double.longBitsToDouble;
34+
35+
public class InverseTrigTests {
36+
private InverseTrigTests(){}
37+
38+
public static void main(String... args) {
39+
int failures = 0;
40+
41+
failures += testAsinSpecialCases();
42+
failures += testAcosSpecialCases();
43+
failures += testAtanSpecialCases();
44+
45+
if (failures > 0) {
46+
System.err.println("Testing inverse trig mthods incurred "
47+
+ failures + " failures.");
48+
throw new RuntimeException();
49+
}
50+
}
51+
52+
private static final double InfinityD = Double.POSITIVE_INFINITY;
53+
private static final double NaNd = Double.NaN;
54+
55+
/**
56+
* From the spec for Math.asin:
57+
*
58+
* "Special cases:
59+
*
60+
* If the argument is NaN or its absolute value is greater than 1,
61+
* then the result is NaN.
62+
*
63+
* If the argument is zero, then the result is a zero with the
64+
* same sign as the argument."
65+
*/
66+
private static int testAsinSpecialCases() {
67+
int failures = 0;
68+
69+
for(double nan : Tests.NaNs) {
70+
failures += testAsinCase(nan, NaNd);
71+
}
72+
73+
double [][] testCases = {
74+
{Math.nextUp(1.0), NaNd},
75+
{Math.nextDown(-1.0), NaNd},
76+
{ InfinityD, NaNd},
77+
{-InfinityD, NaNd},
78+
79+
{-0.0, -0.0},
80+
{+0.0, +0.0},
81+
};
82+
83+
for(int i = 0; i < testCases.length; i++) {
84+
failures += testAsinCase(testCases[i][0],
85+
testCases[i][1]);
86+
}
87+
88+
return failures;
89+
}
90+
91+
private static int testAsinCase(double input, double expected) {
92+
int failures=0;
93+
94+
failures += Tests.test("Math.asin", input, Math::asin, expected);
95+
failures += Tests.test("StrictMath.asin", input, StrictMath::asin, expected);
96+
97+
return failures;
98+
}
99+
100+
/**
101+
* From the spec for Math.acos:
102+
*
103+
* "Special case:
104+
*
105+
* If the argument is NaN or its absolute value is greater than 1,
106+
* then the result is NaN.
107+
*
108+
* If the argument is 1.0, the result is positive zero."
109+
*/
110+
private static int testAcosSpecialCases() {
111+
int failures = 0;
112+
113+
for(double nan : Tests.NaNs) {
114+
failures += testAcosCase(nan, NaNd);
115+
}
116+
117+
double [][] testCases = {
118+
{Math.nextUp(1.0), NaNd},
119+
{Math.nextDown(-1.0), NaNd},
120+
{InfinityD, NaNd},
121+
{-InfinityD, NaNd},
122+
123+
{1.0, +0.0},
124+
};
125+
126+
for(int i = 0; i < testCases.length; i++) {
127+
failures += testAcosCase(testCases[i][0],
128+
testCases[i][1]);
129+
}
130+
131+
return failures;
132+
}
133+
134+
private static int testAcosCase(double input, double expected) {
135+
int failures=0;
136+
137+
failures += Tests.test("Math.acos", input, Math::acos, expected);
138+
failures += Tests.test("StrictMath.acos", input, StrictMath::acos, expected);
139+
140+
return failures;
141+
}
142+
143+
/**
144+
* From the spec for Math.atan:
145+
*
146+
* "Special cases:
147+
*
148+
* If the argument is NaN, then the result is NaN.
149+
*
150+
* If the argument is zero, then the result is a zero with the
151+
* same sign as the argument.
152+
*
153+
* If the argument is infinite, then the result is the closest
154+
* value to pi/2 with the same sign as the input."
155+
*/
156+
private static int testAtanSpecialCases() {
157+
int failures = 0;
158+
159+
for(double nan : Tests.NaNs) {
160+
failures += testAtanCase(nan, NaNd);
161+
}
162+
163+
double [][] testCases = {
164+
{-0.0, -0.0},
165+
{+0.0, +0.0},
166+
167+
{ InfinityD, +Math.PI/2.0},
168+
{-InfinityD, -Math.PI/2.0},
169+
};
170+
171+
for(int i = 0; i < testCases.length; i++) {
172+
failures += testAtanCase(testCases[i][0],
173+
testCases[i][1]);
174+
}
175+
176+
return failures;
177+
}
178+
179+
private static int testAtanCase(double input, double expected) {
180+
int failures=0;
181+
182+
failures += Tests.test("Math.atan", input, Math::atan, expected);
183+
failures += Tests.test("StrictMath.atan", input, StrictMath::atan, expected);
184+
185+
return failures;
186+
}
187+
}

0 commit comments

Comments
 (0)