Path: blob/master/test/jdk/java/lang/Double/ToHexString.java
41149 views
/*1* Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4826774 492654726* @summary Tests for {Float, Double}.toHexString methods27* @library ../Math28* @build DoubleConsts29* @run main ToHexString30* @author Joseph D. Darcy31*/3233import java.util.regex.*;3435public class ToHexString {36private ToHexString() {}3738/*39* Given a double value, create a hexadecimal floating-point40* string via an intermediate long hex string.41*/42static String doubleToHexString(double d) {43return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));44}4546/*47* Transform the hexadecimal long output into the equivalent48* hexadecimal double value.49*/50static String hexLongStringtoHexDoubleString(String transString) {51transString = transString.toLowerCase();5253String zeros = "";54StringBuffer result = new StringBuffer(24);5556for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");57transString = zeros + transString;5859// assert transString.length == 16;6061char topChar;62// Extract sign63if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...64result.append("-");65// clear sign bit66transString =67Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +68transString.substring(1,16);69}7071// check for NaN and infinity72String signifString = transString.substring(3,16);7374if( transString.substring(0,3).equals("7ff") ) {75if(signifString.equals("0000000000000")) {76result.append("Infinity");77}78else79result.append("NaN");80}81else { // finite value82// Extract exponent83int exponent = Integer.parseInt(transString.substring(0,3), 16) -84DoubleConsts.EXP_BIAS;85result.append("0x");8687if (exponent == Double.MIN_EXPONENT - 1) { // zero or subnormal88if(signifString.equals("0000000000000")) {89result.append("0.0p0");90}91else {92result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +93"p-1022");94}95}96else { // normal value97result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +98"p" + exponent);99}100}101return result.toString();102}103104public static int toHexStringTests() {105int failures = 0;106String [][] testCases1 = {107{"Infinity", "Infinity"},108{"-Infinity", "-Infinity"},109{"NaN", "NaN"},110{"-NaN", "NaN"},111{"0.0", "0x0.0p0"},112{"-0.0", "-0x0.0p0"},113{"1.0", "0x1.0p0"},114{"-1.0", "-0x1.0p0"},115{"2.0", "0x1.0p1"},116{"3.0", "0x1.8p1"},117{"0.5", "0x1.0p-1"},118{"0.25", "0x1.0p-2"},119{"1.7976931348623157e+308", "0x1.fffffffffffffp1023"}, // MAX_VALUE120{"2.2250738585072014E-308", "0x1.0p-1022"}, // MIN_NORMAL121{"2.225073858507201E-308", "0x0.fffffffffffffp-1022"}, // MAX_SUBNORMAL122{"4.9e-324", "0x0.0000000000001p-1022"} // MIN_VALUE123};124125// Compare decimal string -> double -> hex string to hex string126for (int i = 0; i < testCases1.length; i++) {127String result;128if(! (result=Double.toHexString(Double.parseDouble(testCases1[i][0]))).129equals(testCases1[i][1])) {130failures ++;131System.err.println("For floating-point string " + testCases1[i][0] +132", expected hex output " + testCases1[i][1] + ", got " + result +".");133}134}135136137// Except for float subnormals, the output for numerically138// equal float and double values should be the same.139// Therefore, we will explicitly test float subnormal values.140String [][] floatTestCases = {141{"Infinity", "Infinity"},142{"-Infinity", "-Infinity"},143{"NaN", "NaN"},144{"-NaN", "NaN"},145{"0.0", "0x0.0p0"},146{"-0.0", "-0x0.0p0"},147{"1.0", "0x1.0p0"},148{"-1.0", "-0x1.0p0"},149{"2.0", "0x1.0p1"},150{"3.0", "0x1.8p1"},151{"0.5", "0x1.0p-1"},152{"0.25", "0x1.0p-2"},153{"3.4028235e+38f", "0x1.fffffep127"}, // MAX_VALUE154{"1.17549435E-38f", "0x1.0p-126"}, // MIN_NORMAL155{"1.1754942E-38", "0x0.fffffep-126"}, // MAX_SUBNORMAL156{"1.4e-45f", "0x0.000002p-126"} // MIN_VALUE157};158// Compare decimal string -> double -> hex string to hex string159for (int i = 0; i < floatTestCases.length; i++) {160String result;161if(! (result=Float.toHexString(Float.parseFloat(floatTestCases[i][0]))).162equals(floatTestCases[i][1])) {163failures++;164System.err.println("For floating-point string " + floatTestCases[i][0] +165", expected hex output\n" + floatTestCases[i][1] + ", got\n" + result +".");166}167}168169// Particular floating-point values and hex equivalents, mostly170// taken from fdlibm source.171String [][] testCases2 = {172{"+0.0", "0000000000000000"},173{"-0.0", "8000000000000000"},174{"+4.9e-324", "0000000000000001"},175{"-4.9e-324", "8000000000000001"},176177// fdlibm k_sin.c178{"+5.00000000000000000000e-01", "3FE0000000000000"},179{"-1.66666666666666324348e-01", "BFC5555555555549"},180{"+8.33333333332248946124e-03", "3F8111111110F8A6"},181{"-1.98412698298579493134e-04", "BF2A01A019C161D5"},182{"+2.75573137070700676789e-06", "3EC71DE357B1FE7D"},183{"-2.50507602534068634195e-08", "BE5AE5E68A2B9CEB"},184{"+1.58969099521155010221e-10", "3DE5D93A5ACFD57C"},185186// fdlibm k_cos.c187{"+4.16666666666666019037e-02", "3FA555555555554C"},188{"-1.38888888888741095749e-03", "BF56C16C16C15177"},189{"+2.48015872894767294178e-05", "3EFA01A019CB1590"},190{"-2.75573143513906633035e-07", "BE927E4F809C52AD"},191{"+2.08757232129817482790e-09", "3E21EE9EBDB4B1C4"},192{"-1.13596475577881948265e-11", "BDA8FAE9BE8838D4"},193194// fdlibm e_rempio.c195{"1.67772160000000000000e+07", "4170000000000000"},196{"6.36619772367581382433e-01", "3FE45F306DC9C883"},197{"1.57079632673412561417e+00", "3FF921FB54400000"},198{"6.07710050650619224932e-11", "3DD0B4611A626331"},199{"6.07710050630396597660e-11", "3DD0B4611A600000"},200{"2.02226624879595063154e-21", "3BA3198A2E037073"},201{"2.02226624871116645580e-21", "3BA3198A2E000000"},202{"8.47842766036889956997e-32", "397B839A252049C1"},203204205// fdlibm s_cbrt.c206{"+5.42857142857142815906e-01", "3FE15F15F15F15F1"},207{"-7.05306122448979611050e-01", "BFE691DE2532C834"},208{"+1.41428571428571436819e+00", "3FF6A0EA0EA0EA0F"},209{"+1.60714285714285720630e+00", "3FF9B6DB6DB6DB6E"},210{"+3.57142857142857150787e-01", "3FD6DB6DB6DB6DB7"},211};212213// Compare decimal string -> double -> hex string to214// long hex string -> double hex string215for (int i = 0; i < testCases2.length; i++) {216String result;217String expected;218if(! (result=Double.toHexString(Double.parseDouble(testCases2[i][0]))).219equals( expected=hexLongStringtoHexDoubleString(testCases2[i][1]) )) {220failures ++;221System.err.println("For floating-point string " + testCases2[i][0] +222", expected hex output " + expected + ", got " + result +".");223}224}225226// Test random double values;227// compare double -> Double.toHexString with local doubleToHexString228java.util.Random rand = new java.util.Random(0);229for (int i = 0; i < 1000; i++) {230String result;231String expected;232double d = rand.nextDouble();233if(! (expected=doubleToHexString(d)).equals(result=Double.toHexString(d)) ) {234failures ++;235System.err.println("For floating-point value " + d +236", expected hex output " + expected + ", got " + result +".");237}238}239240return failures;241}242243public static void main(String argv[]) {244int failures = 0;245246failures = toHexStringTests();247248if (failures != 0) {249throw new RuntimeException("" + failures + " failures while testing Double.toHexString");250}251}252}253254255