809N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
809N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
809N/A *
809N/A * This code is free software; you can redistribute it and/or modify it
809N/A * under the terms of the GNU General Public License version 2 only, as
809N/A * published by the Free Software Foundation.
809N/A *
809N/A * This code is distributed in the hope that it will be useful, but WITHOUT
809N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
809N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
809N/A * version 2 for more details (a copy is included in the LICENSE file that
809N/A * accompanied this code).
809N/A *
809N/A * You should have received a copy of the GNU General Public License version
809N/A * 2 along with this work; if not, write to the Free Software Foundation,
809N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
809N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
809N/A */
809N/A
809N/A/*
809N/A * @test
809N/A * @bug 4826774 4926547
809N/A * @summary Tests for {Float, Double}.toHexString methods
809N/A * @author Joseph D. Darcy
809N/A */
809N/A
809N/Aimport java.util.regex.*;
809N/Aimport sun.misc.FpUtils;
809N/Aimport sun.misc.DoubleConsts;
809N/A
809N/Apublic class ToHexString {
809N/A private ToHexString() {}
809N/A
809N/A /*
809N/A * Given a double value, create a hexadecimal floating-point
809N/A * string via an intermediate long hex string.
809N/A */
809N/A static String doubleToHexString(double d) {
809N/A return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));
809N/A }
809N/A
809N/A /*
809N/A * Transform the hexadecimal long output into the equivalent
809N/A * hexadecimal double value.
809N/A */
809N/A static String hexLongStringtoHexDoubleString(String transString) {
809N/A transString = transString.toLowerCase();
809N/A
809N/A String zeros = "";
809N/A StringBuffer result = new StringBuffer(24);
809N/A
809N/A for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");
809N/A transString = zeros + transString;
809N/A
809N/A // assert transString.length == 16;
809N/A
809N/A char topChar;
809N/A // Extract sign
809N/A if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...
809N/A result.append("-");
809N/A // clear sign bit
809N/A transString =
809N/A Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +
809N/A transString.substring(1,16);
809N/A }
809N/A
809N/A // check for NaN and infinity
809N/A String signifString = transString.substring(3,16);
809N/A
809N/A if( transString.substring(0,3).equals("7ff") ) {
809N/A if(signifString.equals("0000000000000")) {
809N/A result.append("Infinity");
809N/A }
809N/A else
809N/A result.append("NaN");
809N/A }
809N/A else { // finite value
809N/A // Extract exponent
809N/A int exponent = Integer.parseInt(transString.substring(0,3), 16) -
809N/A DoubleConsts.EXP_BIAS;
809N/A result.append("0x");
809N/A
809N/A if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal
809N/A if(signifString.equals("0000000000000")) {
809N/A result.append("0.0p0");
809N/A }
809N/A else {
809N/A result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
809N/A "p-1022");
809N/A }
809N/A }
809N/A else { // normal value
809N/A result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +
809N/A "p" + exponent);
809N/A }
809N/A }
809N/A return result.toString();
809N/A }
809N/A
809N/A public static int toHexStringTests() {
809N/A int failures = 0;
809N/A String [][] testCases1 = {
809N/A {"Infinity", "Infinity"},
809N/A {"-Infinity", "-Infinity"},
809N/A {"NaN", "NaN"},
809N/A {"-NaN", "NaN"},
809N/A {"0.0", "0x0.0p0"},
809N/A {"-0.0", "-0x0.0p0"},
809N/A {"1.0", "0x1.0p0"},
809N/A {"-1.0", "-0x1.0p0"},
809N/A {"2.0", "0x1.0p1"},
809N/A {"3.0", "0x1.8p1"},
809N/A {"0.5", "0x1.0p-1"},
809N/A {"0.25", "0x1.0p-2"},
809N/A {"1.7976931348623157e+308", "0x1.fffffffffffffp1023"}, // MAX_VALUE
809N/A {"2.2250738585072014E-308", "0x1.0p-1022"}, // MIN_NORMAL
809N/A {"2.225073858507201E-308", "0x0.fffffffffffffp-1022"}, // MAX_SUBNORMAL
809N/A {"4.9e-324", "0x0.0000000000001p-1022"} // MIN_VALUE
809N/A };
809N/A
809N/A // Compare decimal string -> double -> hex string to hex string
809N/A for (int i = 0; i < testCases1.length; i++) {
809N/A String result;
809N/A if(! (result=Double.toHexString(Double.parseDouble(testCases1[i][0]))).
809N/A equals(testCases1[i][1])) {
809N/A failures ++;
809N/A System.err.println("For floating-point string " + testCases1[i][0] +
809N/A ", expected hex output " + testCases1[i][1] + ", got " + result +".");
809N/A }
809N/A }
809N/A
809N/A
809N/A // Except for float subnormals, the output for numerically
809N/A // equal float and double values should be the same.
809N/A // Therefore, we will explicitly test float subnormal values.
809N/A String [][] floatTestCases = {
809N/A {"Infinity", "Infinity"},
809N/A {"-Infinity", "-Infinity"},
809N/A {"NaN", "NaN"},
809N/A {"-NaN", "NaN"},
809N/A {"0.0", "0x0.0p0"},
809N/A {"-0.0", "-0x0.0p0"},
809N/A {"1.0", "0x1.0p0"},
809N/A {"-1.0", "-0x1.0p0"},
809N/A {"2.0", "0x1.0p1"},
809N/A {"3.0", "0x1.8p1"},
809N/A {"0.5", "0x1.0p-1"},
809N/A {"0.25", "0x1.0p-2"},
809N/A {"3.4028235e+38f", "0x1.fffffep127"}, // MAX_VALUE
809N/A {"1.17549435E-38f", "0x1.0p-126"}, // MIN_NORMAL
809N/A {"1.1754942E-38", "0x0.fffffep-126"}, // MAX_SUBNORMAL
809N/A {"1.4e-45f", "0x0.000002p-126"} // MIN_VALUE
809N/A };
809N/A // Compare decimal string -> double -> hex string to hex string
809N/A for (int i = 0; i < floatTestCases.length; i++) {
809N/A String result;
809N/A if(! (result=Float.toHexString(Float.parseFloat(floatTestCases[i][0]))).
809N/A equals(floatTestCases[i][1])) {
809N/A failures++;
809N/A System.err.println("For floating-point string " + floatTestCases[i][0] +
809N/A ", expected hex output\n" + floatTestCases[i][1] + ", got\n" + result +".");
809N/A }
809N/A }
809N/A
809N/A // Particular floating-point values and hex equivalents, mostly
809N/A // taken from fdlibm source.
809N/A String [][] testCases2 = {
809N/A {"+0.0", "0000000000000000"},
809N/A {"-0.0", "8000000000000000"},
809N/A {"+4.9e-324", "0000000000000001"},
809N/A {"-4.9e-324", "8000000000000001"},
809N/A
809N/A // fdlibm k_sin.c
809N/A {"+5.00000000000000000000e-01", "3FE0000000000000"},
809N/A {"-1.66666666666666324348e-01", "BFC5555555555549"},
809N/A {"+8.33333333332248946124e-03", "3F8111111110F8A6"},
809N/A {"-1.98412698298579493134e-04", "BF2A01A019C161D5"},
809N/A {"+2.75573137070700676789e-06", "3EC71DE357B1FE7D"},
809N/A {"-2.50507602534068634195e-08", "BE5AE5E68A2B9CEB"},
809N/A {"+1.58969099521155010221e-10", "3DE5D93A5ACFD57C"},
809N/A
809N/A // fdlibm k_cos.c
809N/A {"+4.16666666666666019037e-02", "3FA555555555554C"},
809N/A {"-1.38888888888741095749e-03", "BF56C16C16C15177"},
809N/A {"+2.48015872894767294178e-05", "3EFA01A019CB1590"},
809N/A {"-2.75573143513906633035e-07", "BE927E4F809C52AD"},
809N/A {"+2.08757232129817482790e-09", "3E21EE9EBDB4B1C4"},
809N/A {"-1.13596475577881948265e-11", "BDA8FAE9BE8838D4"},
809N/A
809N/A // fdlibm e_rempio.c
809N/A {"1.67772160000000000000e+07", "4170000000000000"},
809N/A {"6.36619772367581382433e-01", "3FE45F306DC9C883"},
809N/A {"1.57079632673412561417e+00", "3FF921FB54400000"},
809N/A {"6.07710050650619224932e-11", "3DD0B4611A626331"},
809N/A {"6.07710050630396597660e-11", "3DD0B4611A600000"},
809N/A {"2.02226624879595063154e-21", "3BA3198A2E037073"},
809N/A {"2.02226624871116645580e-21", "3BA3198A2E000000"},
809N/A {"8.47842766036889956997e-32", "397B839A252049C1"},
809N/A
809N/A
809N/A // fdlibm s_cbrt.c
809N/A {"+5.42857142857142815906e-01", "3FE15F15F15F15F1"},
809N/A {"-7.05306122448979611050e-01", "BFE691DE2532C834"},
809N/A {"+1.41428571428571436819e+00", "3FF6A0EA0EA0EA0F"},
809N/A {"+1.60714285714285720630e+00", "3FF9B6DB6DB6DB6E"},
809N/A {"+3.57142857142857150787e-01", "3FD6DB6DB6DB6DB7"},
809N/A };
809N/A
809N/A // Compare decimal string -> double -> hex string to
809N/A // long hex string -> double hex string
809N/A for (int i = 0; i < testCases2.length; i++) {
809N/A String result;
809N/A String expected;
809N/A if(! (result=Double.toHexString(Double.parseDouble(testCases2[i][0]))).
809N/A equals( expected=hexLongStringtoHexDoubleString(testCases2[i][1]) )) {
809N/A failures ++;
809N/A System.err.println("For floating-point string " + testCases2[i][0] +
809N/A ", expected hex output " + expected + ", got " + result +".");
809N/A }
809N/A }
809N/A
809N/A // Test random double values;
809N/A // compare double -> Double.toHexString with local doubleToHexString
809N/A java.util.Random rand = new java.util.Random(0);
809N/A for (int i = 0; i < 1000; i++) {
809N/A String result;
809N/A String expected;
809N/A double d = rand.nextDouble();
809N/A if(! (expected=doubleToHexString(d)).equals(result=Double.toHexString(d)) ) {
809N/A failures ++;
809N/A System.err.println("For floating-point value " + d +
809N/A ", expected hex output " + expected + ", got " + result +".");
809N/A }
809N/A }
809N/A
809N/A return failures;
809N/A }
809N/A
809N/A public static void main(String argv[]) {
809N/A int failures = 0;
809N/A
809N/A failures = toHexStringTests();
809N/A
809N/A if (failures != 0) {
809N/A throw new RuntimeException("" + failures + " failures while testing Double.toHexString");
809N/A }
809N/A }
809N/A}