Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/misc/FloatingDecimal/TestFloatingDecimal.java
38839 views
1
/*
2
* Copyright (c) 2013, 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
import java.util.Random;
25
import sun.misc.FloatingDecimal;
26
27
/*
28
OldFloatingDecimalForTest
29
30
public class OldFloatingDecimalForTest {
31
public boolean digitsRoundedUp();
32
public OldFloatingDecimalForTest(double);
33
public OldFloatingDecimalForTest(float);
34
public boolean decimalDigitsExact();
35
public java.lang.String toString();
36
public java.lang.String toJavaFormatString();
37
public void appendTo(java.lang.Appendable);
38
public static OldFloatingDecimalForTest readJavaFormatString(java.lang.String) throws java.lang.NumberFormatException;
39
public strictfp double doubleValue();
40
public strictfp float floatValue();
41
}
42
43
sun.misc.FloatingDecimal
44
45
public class sun.misc.FloatingDecimal {
46
public sun.misc.FloatingDecimal();
47
public static java.lang.String toJavaFormatString(double);
48
public static java.lang.String toJavaFormatString(float);
49
public static void appendTo(double, java.lang.Appendable);
50
public static void appendTo(float, java.lang.Appendable);
51
public static double parseDouble(java.lang.String) throws java.lang.NumberFormatException;
52
public static float parseFloat(java.lang.String) throws java.lang.NumberFormatException;
53
public static sun.misc.FloatingDecimal$AbstractD2ABuffer getD2ABuffer(double);
54
}
55
*/
56
57
/**
58
* @test
59
* @bug 7032154
60
* @summary unit tests of sun.misc.FloatingDecimal
61
* @author Brian Burkhalter
62
* @key randomness
63
*/
64
public class TestFloatingDecimal {
65
private static enum ResultType {
66
RESULT_EXCEPTION,
67
RESULT_PRINT
68
}
69
70
private static final ResultType RESULT_TYPE = ResultType.RESULT_PRINT;
71
private static final int NUM_RANDOM_TESTS = 100000;
72
73
private static final Random RANDOM = new Random();
74
75
private static void result(String message) {
76
switch (RESULT_TYPE) {
77
case RESULT_EXCEPTION:
78
throw new RuntimeException(message);
79
case RESULT_PRINT:
80
System.err.println(message);
81
break;
82
default:
83
assert false;
84
}
85
}
86
87
private static int check(String test, Object expected, Object actual) {
88
int failures = 0;
89
if(!actual.equals(expected)) {
90
failures++;
91
result("Test "+test+" expected "+expected+" but obtained "+actual);
92
}
93
return failures;
94
}
95
96
private static int testAppendToDouble() {
97
System.out.println(" testAppendToDouble");
98
int failures = 0;
99
100
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
101
double[] d = new double[] {
102
RANDOM.nextLong(),
103
RANDOM.nextGaussian(),
104
RANDOM.nextDouble()*Double.MAX_VALUE
105
};
106
for(int j = 0; j < d.length; j++) {
107
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);
108
StringBuilder sb = new StringBuilder();
109
ofd.appendTo(sb);
110
String oldString = sb.toString();
111
sb = new StringBuilder();
112
FloatingDecimal.appendTo(d[j], sb);
113
String newString = sb.toString();
114
failures += check("testAppendToDouble", oldString, newString);
115
}
116
}
117
118
return failures;
119
}
120
121
private static int testAppendToFloat() {
122
System.out.println(" testAppendToFloat");
123
int failures = 0;
124
125
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
126
float[] f = new float[] {
127
RANDOM.nextLong(),
128
(float)RANDOM.nextGaussian(),
129
RANDOM.nextFloat()*Float.MAX_VALUE
130
};
131
for(int j = 0; j < f.length; j++) {
132
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);
133
StringBuilder sb = new StringBuilder();
134
ofd.appendTo(sb);
135
String oldString = sb.toString();
136
sb = new StringBuilder();
137
FloatingDecimal.appendTo(f[j], sb);
138
String newString = sb.toString();
139
failures += check("testAppendToFloat", oldString, newString);
140
}
141
}
142
143
return failures;
144
}
145
146
private static int testAppendTo() {
147
System.out.println("testAppendTo");
148
int failures = 0;
149
150
failures += testAppendToDouble();
151
failures += testAppendToFloat();
152
153
return failures;
154
}
155
156
private static int testParseDouble() {
157
System.out.println(" testParseDouble");
158
int failures = 0;
159
160
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
161
double[] d = new double[] {
162
RANDOM.nextLong(),
163
RANDOM.nextGaussian(),
164
RANDOM.nextDouble()*Double.MAX_VALUE
165
};
166
for(int j = 0; j < d.length; j++) {
167
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);
168
String javaFormatString = ofd.toJavaFormatString();
169
ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString);
170
double oldDouble = ofd.doubleValue();
171
double newDouble = FloatingDecimal.parseDouble(javaFormatString);
172
failures += check("testParseDouble", oldDouble, newDouble);
173
}
174
}
175
176
return failures;
177
}
178
179
private static int testParseFloat() {
180
System.out.println(" testParseFloat");
181
int failures = 0;
182
183
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
184
float[] f = new float[] {
185
RANDOM.nextInt(),
186
(float)RANDOM.nextGaussian(),
187
RANDOM.nextFloat()*Float.MAX_VALUE
188
};
189
for(int j = 0; j < f.length; j++) {
190
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);
191
String javaFormatString = ofd.toJavaFormatString();
192
ofd = OldFloatingDecimalForTest.readJavaFormatString(javaFormatString);
193
float oldFloat = ofd.floatValue();
194
float newFloat = FloatingDecimal.parseFloat(javaFormatString);
195
failures += check("testParseFloat", oldFloat, newFloat);
196
}
197
}
198
199
return failures;
200
}
201
202
private static int testParse() {
203
System.out.println("testParse");
204
int failures = 0;
205
206
failures += testParseDouble();
207
failures += testParseFloat();
208
209
return failures;
210
}
211
212
private static int testToJavaFormatStringDoubleFixed() {
213
System.out.println(" testToJavaFormatStringDoubleFixed");
214
int failures = 0;
215
216
double[] d = new double [] {
217
-5.9522650387500933e18, // dtoa() fast path
218
0.872989018674569, // dtoa() fast iterative - long
219
1.1317400099603851e308 // dtoa() slow iterative
220
};
221
222
for(int i = 0; i < d.length; i++) {
223
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[i]);
224
failures += check("testToJavaFormatStringDoubleFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[i]));
225
}
226
227
return failures;
228
}
229
230
private static int testToJavaFormatStringDoubleRandom() {
231
System.out.println(" testToJavaFormatStringDoubleRandom");
232
int failures = 0;
233
234
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
235
double[] d = new double[] {
236
RANDOM.nextLong(),
237
RANDOM.nextGaussian(),
238
RANDOM.nextDouble()*Double.MAX_VALUE
239
};
240
for(int j = 0; j < d.length; j++) {
241
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(d[j]);
242
failures += check("testToJavaFormatStringDoubleRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(d[j]));
243
}
244
}
245
246
return failures;
247
}
248
249
private static int testToJavaFormatStringDouble() {
250
System.out.println(" testToJavaFormatStringDouble");
251
int failures = 0;
252
failures += testToJavaFormatStringDoubleFixed();
253
failures += testToJavaFormatStringDoubleRandom();
254
return failures;
255
}
256
257
private static int testToJavaFormatStringFloatFixed() {
258
System.out.println(" testToJavaFormatStringFloatFixed");
259
int failures = 0;
260
261
float[] f = new float[] {
262
-9.8784166e8f, // dtoa() fast path
263
0.70443946f, // dtoa() fast iterative - int
264
1.8254228e37f // dtoa() slow iterative
265
};
266
267
for(int i = 0; i < f.length; i++) {
268
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[i]);
269
failures += check("testToJavaFormatStringFloatFixed", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[i]));
270
}
271
272
return failures;
273
}
274
275
private static int testToJavaFormatStringFloatRandom() {
276
System.out.println(" testToJavaFormatStringFloatRandom");
277
int failures = 0;
278
279
for(int i = 0; i < NUM_RANDOM_TESTS; i++) {
280
float[] f = new float[] {
281
RANDOM.nextInt(),
282
(float)RANDOM.nextGaussian(),
283
RANDOM.nextFloat()*Float.MAX_VALUE
284
};
285
for(int j = 0; j < f.length; j++) {
286
OldFloatingDecimalForTest ofd = new OldFloatingDecimalForTest(f[j]);
287
failures += check("testToJavaFormatStringFloatRandom", ofd.toJavaFormatString(), FloatingDecimal.toJavaFormatString(f[j]));
288
}
289
}
290
291
return failures;
292
}
293
294
private static int testToJavaFormatStringFloat() {
295
System.out.println(" testToJavaFormatStringFloat");
296
int failures = 0;
297
298
failures += testToJavaFormatStringFloatFixed();
299
failures += testToJavaFormatStringFloatRandom();
300
301
return failures;
302
}
303
304
private static int testToJavaFormatString() {
305
System.out.println("testToJavaFormatString");
306
int failures = 0;
307
308
failures += testToJavaFormatStringDouble();
309
failures += testToJavaFormatStringFloat();
310
311
return failures;
312
}
313
314
public static void main(String[] args) {
315
int failures = 0;
316
317
failures += testAppendTo();
318
failures += testParse();
319
failures += testToJavaFormatString();
320
321
if (failures != 0) {
322
throw new RuntimeException("" + failures + " failures while testing FloatingDecimal");
323
}
324
}
325
}
326
327