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