Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Double/ToHexString.java
38812 views
1
/*
2
* Copyright (c) 2003, 2011, 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 4826774 4926547
27
* @summary Tests for {Float, Double}.toHexString methods
28
* @author Joseph D. Darcy
29
*/
30
31
import java.util.regex.*;
32
import sun.misc.DoubleConsts;
33
34
public class ToHexString {
35
private ToHexString() {}
36
37
/*
38
* Given a double value, create a hexadecimal floating-point
39
* string via an intermediate long hex string.
40
*/
41
static String doubleToHexString(double d) {
42
return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));
43
}
44
45
/*
46
* Transform the hexadecimal long output into the equivalent
47
* hexadecimal double value.
48
*/
49
static String hexLongStringtoHexDoubleString(String transString) {
50
transString = transString.toLowerCase();
51
52
String zeros = "";
53
StringBuffer result = new StringBuffer(24);
54
55
for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
56
transString = zeros + transString;
57
58
// assert transString.length == 16;
59
60
char topChar;
61
// Extract sign
62
if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
63
result.append("-");
64
// clear sign bit
65
transString =
66
Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
67
transString.substring(1,16);
68
}
69
70
// check for NaN and infinity
71
String signifString = transString.substring(3,16);
72
73
if( transString.substring(0,3).equals("7ff") ) {
74
if(signifString.equals("0000000000000")) {
75
result.append("Infinity");
76
}
77
else
78
result.append("NaN");
79
}
80
else { // finite value
81
// Extract exponent
82
int exponent = Integer.parseInt(transString.substring(0,3), 16) -
83
DoubleConsts.EXP_BIAS;
84
result.append("0x");
85
86
if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
87
if(signifString.equals("0000000000000")) {
88
result.append("0.0p0");
89
}
90
else {
91
result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
92
"p-1022");
93
}
94
}
95
else { // normal value
96
result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
97
"p" + exponent);
98
}
99
}
100
return result.toString();
101
}
102
103
public static int toHexStringTests() {
104
int failures = 0;
105
String [][] testCases1 = {
106
{"Infinity", "Infinity"},
107
{"-Infinity", "-Infinity"},
108
{"NaN", "NaN"},
109
{"-NaN", "NaN"},
110
{"0.0", "0x0.0p0"},
111
{"-0.0", "-0x0.0p0"},
112
{"1.0", "0x1.0p0"},
113
{"-1.0", "-0x1.0p0"},
114
{"2.0", "0x1.0p1"},
115
{"3.0", "0x1.8p1"},
116
{"0.5", "0x1.0p-1"},
117
{"0.25", "0x1.0p-2"},
118
{"1.7976931348623157e+308", "0x1.fffffffffffffp1023"}, // MAX_VALUE
119
{"2.2250738585072014E-308", "0x1.0p-1022"}, // MIN_NORMAL
120
{"2.225073858507201E-308", "0x0.fffffffffffffp-1022"}, // MAX_SUBNORMAL
121
{"4.9e-324", "0x0.0000000000001p-1022"} // MIN_VALUE
122
};
123
124
// Compare decimal string -> double -> hex string to hex string
125
for (int i = 0; i < testCases1.length; i++) {
126
String result;
127
if(! (result=Double.toHexString(Double.parseDouble(testCases1[i][0]))).
128
equals(testCases1[i][1])) {
129
failures ++;
130
System.err.println("For floating-point string " + testCases1[i][0] +
131
", expected hex output " + testCases1[i][1] + ", got " + result +".");
132
}
133
}
134
135
136
// Except for float subnormals, the output for numerically
137
// equal float and double values should be the same.
138
// Therefore, we will explicitly test float subnormal values.
139
String [][] floatTestCases = {
140
{"Infinity", "Infinity"},
141
{"-Infinity", "-Infinity"},
142
{"NaN", "NaN"},
143
{"-NaN", "NaN"},
144
{"0.0", "0x0.0p0"},
145
{"-0.0", "-0x0.0p0"},
146
{"1.0", "0x1.0p0"},
147
{"-1.0", "-0x1.0p0"},
148
{"2.0", "0x1.0p1"},
149
{"3.0", "0x1.8p1"},
150
{"0.5", "0x1.0p-1"},
151
{"0.25", "0x1.0p-2"},
152
{"3.4028235e+38f", "0x1.fffffep127"}, // MAX_VALUE
153
{"1.17549435E-38f", "0x1.0p-126"}, // MIN_NORMAL
154
{"1.1754942E-38", "0x0.fffffep-126"}, // MAX_SUBNORMAL
155
{"1.4e-45f", "0x0.000002p-126"} // MIN_VALUE
156
};
157
// Compare decimal string -> double -> hex string to hex string
158
for (int i = 0; i < floatTestCases.length; i++) {
159
String result;
160
if(! (result=Float.toHexString(Float.parseFloat(floatTestCases[i][0]))).
161
equals(floatTestCases[i][1])) {
162
failures++;
163
System.err.println("For floating-point string " + floatTestCases[i][0] +
164
", expected hex output\n" + floatTestCases[i][1] + ", got\n" + result +".");
165
}
166
}
167
168
// Particular floating-point values and hex equivalents, mostly
169
// taken from fdlibm source.
170
String [][] testCases2 = {
171
{"+0.0", "0000000000000000"},
172
{"-0.0", "8000000000000000"},
173
{"+4.9e-324", "0000000000000001"},
174
{"-4.9e-324", "8000000000000001"},
175
176
// fdlibm k_sin.c
177
{"+5.00000000000000000000e-01", "3FE0000000000000"},
178
{"-1.66666666666666324348e-01", "BFC5555555555549"},
179
{"+8.33333333332248946124e-03", "3F8111111110F8A6"},
180
{"-1.98412698298579493134e-04", "BF2A01A019C161D5"},
181
{"+2.75573137070700676789e-06", "3EC71DE357B1FE7D"},
182
{"-2.50507602534068634195e-08", "BE5AE5E68A2B9CEB"},
183
{"+1.58969099521155010221e-10", "3DE5D93A5ACFD57C"},
184
185
// fdlibm k_cos.c
186
{"+4.16666666666666019037e-02", "3FA555555555554C"},
187
{"-1.38888888888741095749e-03", "BF56C16C16C15177"},
188
{"+2.48015872894767294178e-05", "3EFA01A019CB1590"},
189
{"-2.75573143513906633035e-07", "BE927E4F809C52AD"},
190
{"+2.08757232129817482790e-09", "3E21EE9EBDB4B1C4"},
191
{"-1.13596475577881948265e-11", "BDA8FAE9BE8838D4"},
192
193
// fdlibm e_rempio.c
194
{"1.67772160000000000000e+07", "4170000000000000"},
195
{"6.36619772367581382433e-01", "3FE45F306DC9C883"},
196
{"1.57079632673412561417e+00", "3FF921FB54400000"},
197
{"6.07710050650619224932e-11", "3DD0B4611A626331"},
198
{"6.07710050630396597660e-11", "3DD0B4611A600000"},
199
{"2.02226624879595063154e-21", "3BA3198A2E037073"},
200
{"2.02226624871116645580e-21", "3BA3198A2E000000"},
201
{"8.47842766036889956997e-32", "397B839A252049C1"},
202
203
204
// fdlibm s_cbrt.c
205
{"+5.42857142857142815906e-01", "3FE15F15F15F15F1"},
206
{"-7.05306122448979611050e-01", "BFE691DE2532C834"},
207
{"+1.41428571428571436819e+00", "3FF6A0EA0EA0EA0F"},
208
{"+1.60714285714285720630e+00", "3FF9B6DB6DB6DB6E"},
209
{"+3.57142857142857150787e-01", "3FD6DB6DB6DB6DB7"},
210
};
211
212
// Compare decimal string -> double -> hex string to
213
// long hex string -> double hex string
214
for (int i = 0; i < testCases2.length; i++) {
215
String result;
216
String expected;
217
if(! (result=Double.toHexString(Double.parseDouble(testCases2[i][0]))).
218
equals( expected=hexLongStringtoHexDoubleString(testCases2[i][1]) )) {
219
failures ++;
220
System.err.println("For floating-point string " + testCases2[i][0] +
221
", expected hex output " + expected + ", got " + result +".");
222
}
223
}
224
225
// Test random double values;
226
// compare double -> Double.toHexString with local doubleToHexString
227
java.util.Random rand = new java.util.Random(0);
228
for (int i = 0; i < 1000; i++) {
229
String result;
230
String expected;
231
double d = rand.nextDouble();
232
if(! (expected=doubleToHexString(d)).equals(result=Double.toHexString(d)) ) {
233
failures ++;
234
System.err.println("For floating-point value " + d +
235
", expected hex output " + expected + ", got " + result +".");
236
}
237
}
238
239
return failures;
240
}
241
242
public static void main(String argv[]) {
243
int failures = 0;
244
245
failures = toHexStringTests();
246
247
if (failures != 0) {
248
throw new RuntimeException("" + failures + " failures while testing Double.toHexString");
249
}
250
}
251
}
252
253