Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/lang/Math/PowTests.java
66644 views
1
/*
2
* Copyright (c) 2004, 2021, 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 4984407 5033578 8134795
27
* @summary Tests for {Math, StrictMath}.pow
28
* @author Joseph D. Darcy
29
*/
30
31
public class PowTests {
32
private PowTests(){}
33
34
static final double infinityD = Double.POSITIVE_INFINITY;
35
36
static int testPowCase(double input1, double input2, double expected) {
37
int failures = 0;
38
failures += Tests.test("StrictMath.pow(double, double)", input1, input2,
39
StrictMath.pow(input1, input2), expected);
40
failures += Tests.test("Math.pow(double, double)", input1, input2,
41
Math.pow(input1, input2), expected);
42
return failures;
43
}
44
45
46
static int testStrictPowCase(double input1, double input2, double expected) {
47
int failures = 0;
48
failures += Tests.test("StrictMath.pow(double, double)", input1, input2,
49
StrictMath.pow(input1, input2), expected);
50
return failures;
51
}
52
53
static int testNonstrictPowCase(double input1, double input2, double expected) {
54
int failures = 0;
55
failures += Tests.test("Math.pow(double, double)", input1, input2,
56
Math.pow(input1, input2), expected);
57
return failures;
58
}
59
60
static int testStrictVsNonstrictPowCase(double input1, double input2) {
61
double smResult = StrictMath.pow(input1, input2);
62
double mResult = Math.pow(input1, input2);
63
return Tests.testUlpDiff(
64
"StrictMath.pow(double, double) vs Math.pow(double, double)",
65
input1, input2, mResult, smResult, 2.0
66
);
67
}
68
69
/*
70
* Test for bad negation implementation.
71
*/
72
static int testPow() {
73
int failures = 0;
74
75
double [][] testCases = {
76
{-0.0, 3.0, -0.0},
77
{-0.0, 4.0, 0.0},
78
{-infinityD, -3.0, -0.0},
79
{-infinityD, -4.0, 0.0},
80
};
81
82
for (double[] testCase : testCases) {
83
failures+=testPowCase(testCase[0], testCase[1], testCase[2]);
84
}
85
86
return failures;
87
}
88
89
/*
90
* Test cross-product of different kinds of arguments.
91
*/
92
static int testCrossProduct() {
93
int failures = 0;
94
95
double testData[] = {
96
Double.NEGATIVE_INFINITY,
97
/* > -oo */ -Double.MAX_VALUE,
98
/**/ (double)Long.MIN_VALUE,
99
/**/ (double) -((1L<<53)+2L),
100
-0x1.0p65,
101
-0x1.0000000000001p64,
102
-0x1.0p64,
103
/**/ (double) -((1L<<53)),
104
/**/ (double) -((1L<<53)-1L),
105
/**/ -((double)Integer.MAX_VALUE + 4.0),
106
/**/ (double)Integer.MIN_VALUE - 1.0,
107
/**/ (double)Integer.MIN_VALUE,
108
/**/ (double)Integer.MIN_VALUE + 1.0,
109
-0x1.0p31 + 2.0,
110
-0x1.0p31 + 1.0,
111
-0x1.0000000000001p31,
112
-0x1.0p31,
113
/**/ -Math.PI,
114
/**/ -3.0,
115
/**/ -Math.E,
116
/**/ -2.0,
117
/**/ -1.0000000000000004,
118
/* < -1.0 */ -1.0000000000000002, // nextAfter(-1.0, -oo)
119
-1.0,
120
/* > -1.0 */ -0.9999999999999999, // nextAfter(-1.0, +oo)
121
/* > -1.0 */ -0.9999999999999998,
122
-0x1.fffffp-1,
123
-0x1.ffffeffffffffp-1,
124
/**/ -0.5,
125
/**/ -1.0/3.0,
126
/* < 0.0 */ -Double.MIN_VALUE,
127
-0.0,
128
+0.0,
129
/* > 0.0 */ +Double.MIN_VALUE,
130
/**/ +1.0/3.0,
131
/**/ +0.5,
132
+0x1.ffffeffffffffp-1,
133
+0x1.fffffp-1,
134
/**/ +0.9999999999999998,
135
/* < +1.0 */ +0.9999999999999999, // nextAfter(-1.0, +oo)
136
+1.0,
137
/* > 1.0 */ +1.0000000000000002, // nextAfter(+1.0, +oo)
138
/**/ +1.0000000000000004,
139
/**/ +2.0,
140
/**/ +Math.E,
141
/**/ +3.0,
142
/**/ +Math.PI,
143
0x1.0p31,
144
0x1.0000000000001p31,
145
0x1.0p31 + 1.0,
146
0x1.0p31 + 2.0,
147
/**/ -(double)Integer.MIN_VALUE - 1.0,
148
/**/ -(double)Integer.MIN_VALUE,
149
/**/ -(double)Integer.MIN_VALUE + 1.0,
150
/**/ (double)Integer.MAX_VALUE + 4.0,
151
/**/ (double) ((1L<<53)-1L),
152
/**/ (double) ((1L<<53)),
153
/**/ (double) ((1L<<53)+2L),
154
0x1.0p64,
155
0x1.0000000000001p64,
156
0x1.0p65,
157
/**/ -(double)Long.MIN_VALUE,
158
/* < oo */ Double.MAX_VALUE,
159
Double.POSITIVE_INFINITY,
160
Double.NaN
161
};
162
163
double NaN = Double.NaN;
164
for(double x: testData) {
165
for(double y: testData) {
166
boolean testPass = false;
167
double expected=NaN;
168
double actual;
169
170
// First, switch on y
171
if( Double.isNaN(y)) {
172
expected = NaN;
173
} else if (y == 0.0) {
174
expected = 1.0;
175
} else if (Double.isInfinite(y) ) {
176
if(y > 0) { // x ^ (+oo)
177
if (Math.abs(x) > 1.0) {
178
expected = Double.POSITIVE_INFINITY;
179
} else if (Math.abs(x) == 1.0) {
180
expected = NaN;
181
} else if (Math.abs(x) < 1.0) {
182
expected = +0.0;
183
} else { // x is NaN
184
assert Double.isNaN(x);
185
expected = NaN;
186
}
187
} else { // x ^ (-oo)
188
if (Math.abs(x) > 1.0) {
189
expected = +0.0;
190
} else if (Math.abs(x) == 1.0) {
191
expected = NaN;
192
} else if (Math.abs(x) < 1.0) {
193
expected = Double.POSITIVE_INFINITY;
194
} else { // x is NaN
195
assert Double.isNaN(x);
196
expected = NaN;
197
}
198
} /* end Double.isInfinite(y) */
199
} else if (y == 1.0) {
200
expected = x;
201
} else if (Double.isNaN(x)) { // Now start switching on x
202
assert y != 0.0;
203
expected = NaN;
204
} else if (x == Double.NEGATIVE_INFINITY) {
205
expected = (y < 0.0) ? f2(y) :f1(y);
206
} else if (x == Double.POSITIVE_INFINITY) {
207
expected = (y < 0.0) ? +0.0 : Double.POSITIVE_INFINITY;
208
} else if (equivalent(x, +0.0)) {
209
assert y != 0.0;
210
expected = (y < 0.0) ? Double.POSITIVE_INFINITY: +0.0;
211
} else if (equivalent(x, -0.0)) {
212
assert y != 0.0;
213
expected = (y < 0.0) ? f1(y): f2(y);
214
} else if( x < 0.0) {
215
assert y != 0.0;
216
failures += testStrictPowCase(x, y, f3(x, y));
217
failures += testNonstrictPowCase(x, y, f3ns(x, y));
218
failures += testStrictVsNonstrictPowCase(x, y);
219
continue;
220
} else {
221
failures += testStrictVsNonstrictPowCase(x, y);
222
// go to next iteration
223
expected = NaN;
224
continue;
225
}
226
227
failures += testPowCase(x, y, expected);
228
} // y
229
} // x
230
return failures;
231
}
232
233
static boolean equivalent(double a, double b) {
234
return Double.compare(a, b) == 0;
235
}
236
237
static double f1(double y) {
238
return (intClassify(y) == 1)?
239
Double.NEGATIVE_INFINITY:
240
Double.POSITIVE_INFINITY;
241
}
242
243
244
static double f2(double y) {
245
return (intClassify(y) == 1)?-0.0:0.0;
246
}
247
248
static double f3(double x, double y) {
249
switch( intClassify(y) ) {
250
case 0:
251
return StrictMath.pow(Math.abs(x), y);
252
// break;
253
254
case 1:
255
return -StrictMath.pow(Math.abs(x), y);
256
// break;
257
258
case -1:
259
return Double.NaN;
260
// break;
261
262
default:
263
throw new AssertionError("Bad classification.");
264
// break;
265
}
266
}
267
268
static double f3ns(double x, double y) {
269
switch( intClassify(y) ) {
270
case 0:
271
return Math.pow(Math.abs(x), y);
272
// break;
273
274
case 1:
275
return -Math.pow(Math.abs(x), y);
276
// break;
277
278
case -1:
279
return Double.NaN;
280
// break;
281
282
default:
283
throw new AssertionError("Bad classification.");
284
// break;
285
}
286
}
287
288
static boolean isFinite(double a) {
289
return (0.0 * a == 0);
290
}
291
292
/**
293
* Return classification of argument: -1 for non-integers, 0 for
294
* even integers, 1 for odd integers.
295
*/
296
static int intClassify(double a) {
297
if(!isFinite(a) || // NaNs and infinities
298
(a != Math.floor(a) )) { // only integers are fixed-points of floor
299
return -1;
300
}
301
else {
302
// Determine if argument is an odd or even integer.
303
304
a = StrictMath.abs(a); // absolute value doesn't affect odd/even
305
306
if(a+1.0 == a) { // a > maximum odd floating-point integer
307
return 0; // Large integers are all even
308
}
309
else { // Convert double -> long and look at low-order bit
310
long ell = (long) a;
311
return ((ell & 0x1L) == (long)1)?1:0;
312
}
313
}
314
}
315
316
public static void main(String [] argv) {
317
int failures = 0;
318
319
failures += testPow();
320
failures += testCrossProduct();
321
322
if (failures > 0) {
323
System.err.println("Testing pow incurred "
324
+ failures + " failures.");
325
throw new RuntimeException();
326
}
327
}
328
}
329
330