Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Double/ToHexString.java
38812 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* @author Joseph D. Darcy28*/2930import java.util.regex.*;31import sun.misc.DoubleConsts;3233public class ToHexString {34private ToHexString() {}3536/*37* Given a double value, create a hexadecimal floating-point38* string via an intermediate long hex string.39*/40static String doubleToHexString(double d) {41return hexLongStringtoHexDoubleString(Long.toHexString(Double.doubleToLongBits(d)));42}4344/*45* Transform the hexadecimal long output into the equivalent46* hexadecimal double value.47*/48static String hexLongStringtoHexDoubleString(String transString) {49transString = transString.toLowerCase();5051String zeros = "";52StringBuffer result = new StringBuffer(24);5354for(int i = 0; i < (16 - transString.length()); i++, zeros += "0");55transString = zeros + transString;5657// assert transString.length == 16;5859char topChar;60// Extract sign61if((topChar=transString.charAt(0)) >= '8' ) {// 8, 9, a, A, b, B, ...62result.append("-");63// clear sign bit64transString =65Character.toString(Character.forDigit(Character.digit(topChar, 16) - 8, 16)) +66transString.substring(1,16);67}6869// check for NaN and infinity70String signifString = transString.substring(3,16);7172if( transString.substring(0,3).equals("7ff") ) {73if(signifString.equals("0000000000000")) {74result.append("Infinity");75}76else77result.append("NaN");78}79else { // finite value80// Extract exponent81int exponent = Integer.parseInt(transString.substring(0,3), 16) -82DoubleConsts.EXP_BIAS;83result.append("0x");8485if (exponent == DoubleConsts.MIN_EXPONENT - 1) { // zero or subnormal86if(signifString.equals("0000000000000")) {87result.append("0.0p0");88}89else {90result.append("0." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +91"p-1022");92}93}94else { // normal value95result.append("1." + signifString.replaceFirst("0+$", "").replaceFirst("^$", "0") +96"p" + exponent);97}98}99return result.toString();100}101102public static int toHexStringTests() {103int failures = 0;104String [][] testCases1 = {105{"Infinity", "Infinity"},106{"-Infinity", "-Infinity"},107{"NaN", "NaN"},108{"-NaN", "NaN"},109{"0.0", "0x0.0p0"},110{"-0.0", "-0x0.0p0"},111{"1.0", "0x1.0p0"},112{"-1.0", "-0x1.0p0"},113{"2.0", "0x1.0p1"},114{"3.0", "0x1.8p1"},115{"0.5", "0x1.0p-1"},116{"0.25", "0x1.0p-2"},117{"1.7976931348623157e+308", "0x1.fffffffffffffp1023"}, // MAX_VALUE118{"2.2250738585072014E-308", "0x1.0p-1022"}, // MIN_NORMAL119{"2.225073858507201E-308", "0x0.fffffffffffffp-1022"}, // MAX_SUBNORMAL120{"4.9e-324", "0x0.0000000000001p-1022"} // MIN_VALUE121};122123// Compare decimal string -> double -> hex string to hex string124for (int i = 0; i < testCases1.length; i++) {125String result;126if(! (result=Double.toHexString(Double.parseDouble(testCases1[i][0]))).127equals(testCases1[i][1])) {128failures ++;129System.err.println("For floating-point string " + testCases1[i][0] +130", expected hex output " + testCases1[i][1] + ", got " + result +".");131}132}133134135// Except for float subnormals, the output for numerically136// equal float and double values should be the same.137// Therefore, we will explicitly test float subnormal values.138String [][] floatTestCases = {139{"Infinity", "Infinity"},140{"-Infinity", "-Infinity"},141{"NaN", "NaN"},142{"-NaN", "NaN"},143{"0.0", "0x0.0p0"},144{"-0.0", "-0x0.0p0"},145{"1.0", "0x1.0p0"},146{"-1.0", "-0x1.0p0"},147{"2.0", "0x1.0p1"},148{"3.0", "0x1.8p1"},149{"0.5", "0x1.0p-1"},150{"0.25", "0x1.0p-2"},151{"3.4028235e+38f", "0x1.fffffep127"}, // MAX_VALUE152{"1.17549435E-38f", "0x1.0p-126"}, // MIN_NORMAL153{"1.1754942E-38", "0x0.fffffep-126"}, // MAX_SUBNORMAL154{"1.4e-45f", "0x0.000002p-126"} // MIN_VALUE155};156// Compare decimal string -> double -> hex string to hex string157for (int i = 0; i < floatTestCases.length; i++) {158String result;159if(! (result=Float.toHexString(Float.parseFloat(floatTestCases[i][0]))).160equals(floatTestCases[i][1])) {161failures++;162System.err.println("For floating-point string " + floatTestCases[i][0] +163", expected hex output\n" + floatTestCases[i][1] + ", got\n" + result +".");164}165}166167// Particular floating-point values and hex equivalents, mostly168// taken from fdlibm source.169String [][] testCases2 = {170{"+0.0", "0000000000000000"},171{"-0.0", "8000000000000000"},172{"+4.9e-324", "0000000000000001"},173{"-4.9e-324", "8000000000000001"},174175// fdlibm k_sin.c176{"+5.00000000000000000000e-01", "3FE0000000000000"},177{"-1.66666666666666324348e-01", "BFC5555555555549"},178{"+8.33333333332248946124e-03", "3F8111111110F8A6"},179{"-1.98412698298579493134e-04", "BF2A01A019C161D5"},180{"+2.75573137070700676789e-06", "3EC71DE357B1FE7D"},181{"-2.50507602534068634195e-08", "BE5AE5E68A2B9CEB"},182{"+1.58969099521155010221e-10", "3DE5D93A5ACFD57C"},183184// fdlibm k_cos.c185{"+4.16666666666666019037e-02", "3FA555555555554C"},186{"-1.38888888888741095749e-03", "BF56C16C16C15177"},187{"+2.48015872894767294178e-05", "3EFA01A019CB1590"},188{"-2.75573143513906633035e-07", "BE927E4F809C52AD"},189{"+2.08757232129817482790e-09", "3E21EE9EBDB4B1C4"},190{"-1.13596475577881948265e-11", "BDA8FAE9BE8838D4"},191192// fdlibm e_rempio.c193{"1.67772160000000000000e+07", "4170000000000000"},194{"6.36619772367581382433e-01", "3FE45F306DC9C883"},195{"1.57079632673412561417e+00", "3FF921FB54400000"},196{"6.07710050650619224932e-11", "3DD0B4611A626331"},197{"6.07710050630396597660e-11", "3DD0B4611A600000"},198{"2.02226624879595063154e-21", "3BA3198A2E037073"},199{"2.02226624871116645580e-21", "3BA3198A2E000000"},200{"8.47842766036889956997e-32", "397B839A252049C1"},201202203// fdlibm s_cbrt.c204{"+5.42857142857142815906e-01", "3FE15F15F15F15F1"},205{"-7.05306122448979611050e-01", "BFE691DE2532C834"},206{"+1.41428571428571436819e+00", "3FF6A0EA0EA0EA0F"},207{"+1.60714285714285720630e+00", "3FF9B6DB6DB6DB6E"},208{"+3.57142857142857150787e-01", "3FD6DB6DB6DB6DB7"},209};210211// Compare decimal string -> double -> hex string to212// long hex string -> double hex string213for (int i = 0; i < testCases2.length; i++) {214String result;215String expected;216if(! (result=Double.toHexString(Double.parseDouble(testCases2[i][0]))).217equals( expected=hexLongStringtoHexDoubleString(testCases2[i][1]) )) {218failures ++;219System.err.println("For floating-point string " + testCases2[i][0] +220", expected hex output " + expected + ", got " + result +".");221}222}223224// Test random double values;225// compare double -> Double.toHexString with local doubleToHexString226java.util.Random rand = new java.util.Random(0);227for (int i = 0; i < 1000; i++) {228String result;229String expected;230double d = rand.nextDouble();231if(! (expected=doubleToHexString(d)).equals(result=Double.toHexString(d)) ) {232failures ++;233System.err.println("For floating-point value " + d +234", expected hex output " + expected + ", got " + result +".");235}236}237238return failures;239}240241public static void main(String argv[]) {242int failures = 0;243244failures = toHexStringTests();245246if (failures != 0) {247throw new RuntimeException("" + failures + " failures while testing Double.toHexString");248}249}250}251252253