1 /* 2 * Copyright (c) 2003, 2017, 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 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run main ParseHexFloatingPoint 29 * @bug 4826774 8078672 30 * @summary Numerical tests for hexadecimal inputs to parse{Double, Float} (use -Dseed=X to set PRNG seed) 31 * @author Joseph D. Darcy 32 * @key randomness 33 */ 34 package test.java.lang.Double; 35 36 import java.util.Random; 37 38 import org.testng.annotations.Test; 39 import org.testng.Assert; 40 41 public class ParseHexFloatingPointTest { ParseHexFloatingPointTest()42 private ParseHexFloatingPointTest(){} 43 44 public static final double infinityD = Double.POSITIVE_INFINITY; 45 public static final double NaND = Double.NaN; 46 test(String testName, String input, double result, double expected)47 static void test(String testName, String input, 48 double result, double expected) { 49 Assert.assertEquals(Double.compare(result, expected), 0, 50 "Failure for " + testName + 51 ": For input " + input + 52 " expected " + expected + 53 " got " + result + "."); 54 55 } 56 testCase(String input, double expected)57 static void testCase(String input, double expected) { 58 // Try different combination of letter components 59 input = input.toLowerCase(java.util.Locale.US); 60 61 String [] suffices = {"", "f", "F", "d", "D"}; 62 String [] signs = {"", "-", "+"}; 63 64 for(int i = 0; i < 2; i++) { 65 String s1 = input; 66 if(i == 1) 67 s1 = s1.replace('x', 'X'); 68 69 for(int j = 0; j < 2; j++) { 70 String s2 = s1; 71 if(j == 1) 72 s2 = s2.replace('p', 'P'); 73 74 for(int k = 0; k < 2; k++) { 75 String s3 = s2; 76 if(k == 1) 77 s3 = upperCaseHex(s3); 78 79 80 for(int m = 0; m < suffices.length; m++) { 81 String s4 = s3 + suffices[m]; 82 83 for(int n = 0; n < signs.length; n++) { 84 String s5 = signs[n] + s4; 85 86 double result = Double.parseDouble(s5); 87 test("Double.parseDouble", 88 s5, result, (signs[n].equals("-") ? 89 -expected: 90 expected)); 91 } 92 } 93 } 94 } 95 } 96 } 97 upperCaseHex(String s)98 static String upperCaseHex(String s) { 99 return s.replace('a', 'A').replace('b', 'B').replace('c', 'C'). 100 replace('d', 'D').replace('e','E').replace('f', 'F'); 101 } 102 103 /* 104 * Test easy and tricky double rounding cases. 105 */ 106 @Test testDouble()107 public void testDouble() { 108 109 /* 110 * A String, double pair 111 */ 112 class PairSD { 113 public String s; 114 public double d; 115 PairSD(String s, double d) { 116 this.s = s; 117 this.d = d; 118 } 119 } 120 121 // Hex strings that convert to three; test basic functionality 122 // of significand and exponent shift adjusts along with the 123 // no-op of adding leading zeros. These cases don't exercise 124 // the rounding code. 125 String leadingZeros = "0x0000000000000000000"; 126 String [] threeTests = { 127 "0x.003p12", 128 "0x.006p11", 129 "0x.00cp10", 130 "0x.018p9", 131 132 "0x.3p4", 133 "0x.6p3", 134 "0x.cp2", 135 "0x1.8p1", 136 137 "0x3p0", 138 "0x6.0p-1", 139 "0xc.0p-2", 140 "0x18.0p-3", 141 142 "0x3000000p-24", 143 "0x3.0p0", 144 "0x3.000000p0", 145 }; 146 for(int i=0; i < threeTests.length; i++) { 147 String input = threeTests[i]; 148 testCase(input, 3.0); 149 150 input.replaceFirst("^0x", leadingZeros); 151 testCase(input, 3.0); 152 } 153 154 long bigExponents [] = { 155 2*Double.MAX_EXPONENT, 156 2*Double.MIN_EXPONENT, 157 158 (long)Integer.MAX_VALUE-1, 159 (long)Integer.MAX_VALUE, 160 (long)Integer.MAX_VALUE+1, 161 162 (long)Integer.MIN_VALUE-1, 163 (long)Integer.MIN_VALUE, 164 (long)Integer.MIN_VALUE+1, 165 166 Long.MAX_VALUE-1, 167 Long.MAX_VALUE, 168 169 Long.MIN_VALUE+1, 170 Long.MIN_VALUE, 171 }; 172 173 // Test zero significand with large exponents. 174 for(int i = 0; i < bigExponents.length; i++) { 175 testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0); 176 } 177 178 // Test nonzero significand with large exponents. 179 for(int i = 0; i < bigExponents.length; i++) { 180 long exponent = bigExponents[i]; 181 testCase("0x10000.0p"+Long.toString(exponent) , 182 (exponent <0?0.0:infinityD)); 183 } 184 185 // Test significands with different lengths and bit patterns. 186 { 187 long signif = 0; 188 for(int i = 1; i <= 0xe; i++) { 189 signif = (signif <<4) | (long)i; 190 testCase("0x"+Long.toHexString(signif)+"p0", signif); 191 } 192 } 193 194 PairSD [] testCases = { 195 new PairSD("0x0.0p0", 0.0/16.0), 196 new PairSD("0x0.1p0", 1.0/16.0), 197 new PairSD("0x0.2p0", 2.0/16.0), 198 new PairSD("0x0.3p0", 3.0/16.0), 199 new PairSD("0x0.4p0", 4.0/16.0), 200 new PairSD("0x0.5p0", 5.0/16.0), 201 new PairSD("0x0.6p0", 6.0/16.0), 202 new PairSD("0x0.7p0", 7.0/16.0), 203 new PairSD("0x0.8p0", 8.0/16.0), 204 new PairSD("0x0.9p0", 9.0/16.0), 205 new PairSD("0x0.ap0", 10.0/16.0), 206 new PairSD("0x0.bp0", 11.0/16.0), 207 new PairSD("0x0.cp0", 12.0/16.0), 208 new PairSD("0x0.dp0", 13.0/16.0), 209 new PairSD("0x0.ep0", 14.0/16.0), 210 new PairSD("0x0.fp0", 15.0/16.0), 211 212 // Half-way case between zero and MIN_VALUE rounds down to 213 // zero 214 new PairSD("0x1.0p-1075", 0.0), 215 216 // Slighly more than half-way case between zero and 217 // MIN_VALUES rounds up to zero. 218 new PairSD("0x1.1p-1075", Double.MIN_VALUE), 219 new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE), 220 new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE), 221 222 // More subnormal rounding tests 223 new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(Double.MIN_NORMAL)), 224 new PairSD("0x0.fffffffffffff8p-1022", Double.MIN_NORMAL), 225 new PairSD("0x0.fffffffffffff800000001p-1022",Double.MIN_NORMAL), 226 new PairSD("0x0.fffffffffffff80000000000000001p-1022",Double.MIN_NORMAL), 227 new PairSD("0x1.0p-1022", Double.MIN_NORMAL), 228 229 230 // Large value and overflow rounding tests 231 new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE), 232 new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE), 233 new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE), 234 new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE), 235 new PairSD("0x1.fffffffffffff8p1023", infinityD), 236 new PairSD("0x1.fffffffffffff8000001p1023", infinityD), 237 238 new PairSD("0x1.ffffffffffffep1023", Math.nextDown(Double.MAX_VALUE)), 239 new PairSD("0x1.ffffffffffffe0000p1023", Math.nextDown(Double.MAX_VALUE)), 240 new PairSD("0x1.ffffffffffffe8p1023", Math.nextDown(Double.MAX_VALUE)), 241 new PairSD("0x1.ffffffffffffe7p1023", Math.nextDown(Double.MAX_VALUE)), 242 new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE), 243 new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE), 244 }; 245 246 for (int i = 0; i < testCases.length; i++) { 247 testCase(testCases[i].s,testCases[i].d); 248 } 249 } 250 251 @Test testRandomDoubles()252 public void testRandomDoubles() { 253 java.util.Random rand = new Random(); 254 // Consistency check; double => hexadecimal => double 255 // preserves the original value. 256 for(int i = 0; i < 1000; i++) { 257 double d = rand.nextDouble(); 258 testCase(Double.toHexString(d), d); 259 } 260 } 261 262 /* 263 * Verify rounding works the same regardless of how the 264 * significand is aligned on input. A useful extension could be 265 * to have this sort of test for strings near the overflow 266 * threshold. 267 */ 268 @Test significandAlignmentTests()269 public static void significandAlignmentTests() { 270 // baseSignif * 2^baseExp = nextDown(2.0) 271 long [] baseSignifs = { 272 0x1ffffffffffffe00L, 273 0x1fffffffffffff00L 274 }; 275 276 double [] answers = { 277 Math.nextDown(Math.nextDown(2.0)), 278 Math.nextDown(2.0), 279 2.0 280 }; 281 282 int baseExp = -60; 283 int count = 0; 284 for(int i = 0; i < 2; i++) { 285 for(long j = 0; j <= 0xfL; j++) { 286 for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8} 287 long base = baseSignifs[i]; 288 long testValue = base | (j<<4) | k; 289 290 int offset = 0; 291 // Calculate when significand should be incremented 292 // see table 4.7 in Koren book 293 294 if ((base & 0x100L) == 0L ) { // lsb is 0 295 if ( (j >= 8L) && // round is 1 296 ((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1 297 offset = 1; 298 } 299 else { // lsb is 1 300 if (j >= 8L) // round is 1 301 offset = 1; 302 } 303 304 double expected = answers[i+offset]; 305 306 for(int m = -2; m <= 3; m++) { 307 count ++; 308 309 // Form equal value string and evaluate it 310 String s = "0x" + 311 Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) + 312 "p" + (baseExp - m); 313 314 testCase(s, expected); 315 } 316 } 317 } 318 } 319 } 320 321 322 /* 323 * Test tricky float rounding cases. The code which 324 * reads in a hex string converts the string to a double value. 325 * If a float value is needed, the double value is cast to float. 326 * However, the cast be itself not always guaranteed to return the 327 * right result since: 328 * 329 * 1. hex string => double can discard a sticky bit which would 330 * influence a direct hex string => float conversion. 331 * 332 * 2. hex string => double => float can have a rounding to double 333 * precision which results in a larger float value while a direct 334 * hex string => float conversion would not round up. 335 * 336 * This method includes tests of the latter two possibilities. 337 */ 338 @Test testFloat()339 public void testFloat() { 340 /* 341 * A String, float pair 342 */ 343 class PairSD { 344 public String s; 345 public float f; 346 PairSD(String s, float f) { 347 this.s = s; 348 this.f = f; 349 } 350 } 351 352 String [][] roundingTestCases = { 353 // Target float value hard rounding version 354 355 {"0x1.000000p0", "0x1.0000000000001p0"}, 356 357 // Try some values that should round up to nextUp(1.0f) 358 {"0x1.000002p0", "0x1.0000010000001p0"}, 359 {"0x1.000002p0", "0x1.00000100000008p0"}, 360 {"0x1.000002p0", "0x1.0000010000000fp0"}, 361 {"0x1.000002p0", "0x1.00000100000001p0"}, 362 {"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"}, 363 {"0x1.000002p0", "0x1.0000010000000fp0"}, 364 365 // Potential double rounding cases 366 {"0x1.000002p0", "0x1.000002fffffffp0"}, 367 {"0x1.000002p0", "0x1.000002fffffff8p0"}, 368 {"0x1.000002p0", "0x1.000002ffffffffp0"}, 369 370 {"0x1.000002p0", "0x1.000002ffff0ffp0"}, 371 {"0x1.000002p0", "0x1.000002ffff0ff8p0"}, 372 {"0x1.000002p0", "0x1.000002ffff0fffp0"}, 373 374 375 {"0x1.000000p0", "0x1.000000fffffffp0"}, 376 {"0x1.000000p0", "0x1.000000fffffff8p0"}, 377 {"0x1.000000p0", "0x1.000000ffffffffp0"}, 378 379 {"0x1.000000p0", "0x1.000000ffffffep0"}, 380 {"0x1.000000p0", "0x1.000000ffffffe8p0"}, 381 {"0x1.000000p0", "0x1.000000ffffffefp0"}, 382 383 // Float subnormal cases 384 {"0x0.000002p-126", "0x0.0000010000001p-126"}, 385 {"0x0.000002p-126", "0x0.00000100000000000001p-126"}, 386 387 {"0x0.000006p-126", "0x0.0000050000001p-126"}, 388 {"0x0.000006p-126", "0x0.00000500000000000001p-126"}, 389 390 {"0x0.0p-149", "0x0.7ffffffffffffffp-149"}, 391 {"0x1.0p-148", "0x1.3ffffffffffffffp-148"}, 392 {"0x1.cp-147", "0x1.bffffffffffffffp-147"}, 393 394 {"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"}, 395 }; 396 397 String [] signs = {"", "-"}; 398 399 for(int i = 0; i < roundingTestCases.length; i++) { 400 for(int j = 0; j < signs.length; j++) { 401 String expectedIn = signs[j]+roundingTestCases[i][0]; 402 String resultIn = signs[j]+roundingTestCases[i][1]; 403 404 float expected = Float.parseFloat(expectedIn); 405 float result = Float.parseFloat(resultIn); 406 407 Assert.assertEquals(Float.compare(expected, result), 0, 408 "Expected = " + Float.toHexString(expected) + 409 "Rounded = " + Float.toHexString(result) + 410 "Double = " + Double.toHexString(Double.parseDouble(resultIn)) + 411 "Input = " + resultIn); 412 } 413 } 414 } 415 } 416