Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Double/ParseHexFloatingPoint.java
38813 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 482677426* @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat27* @author Joseph D. Darcy28* @key randomness29*/303132import java.util.regex.*;33import sun.misc.DoubleConsts;3435public class ParseHexFloatingPoint {36private ParseHexFloatingPoint(){}3738public static final double infinityD = Double.POSITIVE_INFINITY;39public static final double NaND = Double.NaN;4041static int test(String testName, String input,42double result, double expected) {43int failures =0;4445if (Double.compare(result, expected) != 0 ) {46System.err.println("Failure for " + testName +47": For input " + input +48" expected " + expected +49" got " + result + ".");50}5152return failures;53}5455static int testCase(String input, double expected) {56int failures =0;575859// Try different combination of letter components60input = input.toLowerCase(java.util.Locale.US);6162String [] suffices = {"", "f", "F", "d", "D"};63String [] signs = {"", "-", "+"};6465for(int i = 0; i < 2; i++) {66String s1 = input;67if(i == 1)68s1 = s1.replace('x', 'X');6970for(int j = 0; j < 2; j++) {71String s2 = s1;72if(j == 1)73s2 = s2.replace('p', 'P');7475for(int k = 0; k < 2; k++) {76String s3 = s2;77if(k == 1)78s3 = upperCaseHex(s3);798081for(int m = 0; m < suffices.length; m++) {82String s4 = s3 + suffices[m];838485for(int n = 0; n < signs.length; n++) {86String s5 = signs[n] + s4;8788double result = Double.parseDouble(s5);89failures += test("Double.parseDouble",90s5, result, (signs[n].equals("-") ?91-expected:92expected));93}94}95}96}97}9899return failures;100}101102static String upperCaseHex(String s) {103return s.replace('a', 'A').replace('b', 'B').replace('c', 'C').104replace('d', 'D').replace('e','E').replace('f', 'F');105}106107/*108* Test easy and tricky double rounding cases.109*/110static int doubleTests() {111112/*113* A String, double pair114*/115class PairSD {116public String s;117public double d;118PairSD(String s, double d) {119this.s = s;120this.d = d;121}122}123int failures = 0;124125126127// Hex strings that convert to three; test basic functionality128// of significand and exponent shift adjusts along with the129// no-op of adding leading zeros. These cases don't exercise130// the rounding code.131String leadingZeros = "0x0000000000000000000";132String [] threeTests = {133"0x.003p12",134"0x.006p11",135"0x.00cp10",136"0x.018p9",137138"0x.3p4",139"0x.6p3",140"0x.cp2",141"0x1.8p1",142143"0x3p0",144"0x6.0p-1",145"0xc.0p-2",146"0x18.0p-3",147148"0x3000000p-24",149"0x3.0p0",150"0x3.000000p0",151};152for(int i=0; i < threeTests.length; i++) {153String input = threeTests[i];154failures += testCase(input, 3.0);155156input.replaceFirst("^0x", leadingZeros);157failures += testCase(input, 3.0);158}159160long bigExponents [] = {1612*DoubleConsts.MAX_EXPONENT,1622*DoubleConsts.MIN_EXPONENT,163164(long)Integer.MAX_VALUE-1,165(long)Integer.MAX_VALUE,166(long)Integer.MAX_VALUE+1,167168(long)Integer.MIN_VALUE-1,169(long)Integer.MIN_VALUE,170(long)Integer.MIN_VALUE+1,171172Long.MAX_VALUE-1,173Long.MAX_VALUE,174175Long.MIN_VALUE+1,176Long.MIN_VALUE,177};178179// Test zero significand with large exponents.180for(int i = 0; i < bigExponents.length; i++) {181failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);182}183184// Test nonzero significand with large exponents.185for(int i = 0; i < bigExponents.length; i++) {186long exponent = bigExponents[i];187failures += testCase("0x10000.0p"+Long.toString(exponent) ,188(exponent <0?0.0:infinityD));189}190191// Test significands with different lengths and bit patterns.192{193long signif = 0;194for(int i = 1; i <= 0xe; i++) {195signif = (signif <<4) | (long)i;196failures += testCase("0x"+Long.toHexString(signif)+"p0", signif);197}198}199200PairSD [] testCases = {201new PairSD("0x0.0p0", 0.0/16.0),202new PairSD("0x0.1p0", 1.0/16.0),203new PairSD("0x0.2p0", 2.0/16.0),204new PairSD("0x0.3p0", 3.0/16.0),205new PairSD("0x0.4p0", 4.0/16.0),206new PairSD("0x0.5p0", 5.0/16.0),207new PairSD("0x0.6p0", 6.0/16.0),208new PairSD("0x0.7p0", 7.0/16.0),209new PairSD("0x0.8p0", 8.0/16.0),210new PairSD("0x0.9p0", 9.0/16.0),211new PairSD("0x0.ap0", 10.0/16.0),212new PairSD("0x0.bp0", 11.0/16.0),213new PairSD("0x0.cp0", 12.0/16.0),214new PairSD("0x0.dp0", 13.0/16.0),215new PairSD("0x0.ep0", 14.0/16.0),216new PairSD("0x0.fp0", 15.0/16.0),217218// Half-way case between zero and MIN_VALUE rounds down to219// zero220new PairSD("0x1.0p-1075", 0.0),221222// Slighly more than half-way case between zero and223// MIN_VALUES rounds up to zero.224new PairSD("0x1.1p-1075", Double.MIN_VALUE),225new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE),226new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE),227228// More subnormal rounding tests229new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(DoubleConsts.MIN_NORMAL)),230new PairSD("0x0.fffffffffffff8p-1022", DoubleConsts.MIN_NORMAL),231new PairSD("0x0.fffffffffffff800000001p-1022",DoubleConsts.MIN_NORMAL),232new PairSD("0x0.fffffffffffff80000000000000001p-1022",DoubleConsts.MIN_NORMAL),233new PairSD("0x1.0p-1022", DoubleConsts.MIN_NORMAL),234235236// Large value and overflow rounding tests237new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE),238new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),239new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE),240new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE),241new PairSD("0x1.fffffffffffff8p1023", infinityD),242new PairSD("0x1.fffffffffffff8000001p1023", infinityD),243244new PairSD("0x1.ffffffffffffep1023", Math.nextDown(Double.MAX_VALUE)),245new PairSD("0x1.ffffffffffffe0000p1023", Math.nextDown(Double.MAX_VALUE)),246new PairSD("0x1.ffffffffffffe8p1023", Math.nextDown(Double.MAX_VALUE)),247new PairSD("0x1.ffffffffffffe7p1023", Math.nextDown(Double.MAX_VALUE)),248new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE),249new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),250};251252for (int i = 0; i < testCases.length; i++) {253failures += testCase(testCases[i].s,testCases[i].d);254}255256failures += significandAlignmentTests();257258{259java.util.Random rand = new java.util.Random();260// Consistency check; double => hexadecimal => double261// preserves the original value.262for(int i = 0; i < 1000; i++) {263double d = rand.nextDouble();264failures += testCase(Double.toHexString(d), d);265}266}267268return failures;269}270271/*272* Verify rounding works the same regardless of how the273* significand is aligned on input. A useful extension could be274* to have this sort of test for strings near the overflow275* threshold.276*/277static int significandAlignmentTests() {278int failures = 0;279// baseSignif * 2^baseExp = nextDown(2.0)280long [] baseSignifs = {2810x1ffffffffffffe00L,2820x1fffffffffffff00L283};284285double [] answers = {286Math.nextDown(Math.nextDown(2.0)),287Math.nextDown(2.0),2882.0289};290291int baseExp = -60;292int count = 0;293for(int i = 0; i < 2; i++) {294for(long j = 0; j <= 0xfL; j++) {295for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8}296long base = baseSignifs[i];297long testValue = base | (j<<4) | k;298299int offset = 0;300// Calculate when significand should be incremented301// see table 4.7 in Koren book302303if ((base & 0x100L) == 0L ) { // lsb is 0304if ( (j >= 8L) && // round is 1305((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1306offset = 1;307}308else { // lsb is 1309if (j >= 8L) // round is 1310offset = 1;311}312313double expected = answers[i+offset];314315for(int m = -2; m <= 3; m++) {316count ++;317318// Form equal value string and evaluate it319String s = "0x" +320Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) +321"p" + (baseExp - m);322323failures += testCase(s, expected);324}325}326}327}328329return failures;330}331332333/*334* Test tricky float rounding cases. The code which335* reads in a hex string converts the string to a double value.336* If a float value is needed, the double value is cast to float.337* However, the cast be itself not always guaranteed to return the338* right result since:339*340* 1. hex string => double can discard a sticky bit which would341* influence a direct hex string => float conversion.342*343* 2. hex string => double => float can have a rounding to double344* precision which results in a larger float value while a direct345* hex string => float conversion would not round up.346*347* This method includes tests of the latter two possibilities.348*/349static int floatTests(){350int failures = 0;351352/*353* A String, float pair354*/355class PairSD {356public String s;357public float f;358PairSD(String s, float f) {359this.s = s;360this.f = f;361}362}363364String [][] roundingTestCases = {365// Target float value hard rouding version366367{"0x1.000000p0", "0x1.0000000000001p0"},368369// Try some values that should round up to nextUp(1.0f)370{"0x1.000002p0", "0x1.0000010000001p0"},371{"0x1.000002p0", "0x1.00000100000008p0"},372{"0x1.000002p0", "0x1.0000010000000fp0"},373{"0x1.000002p0", "0x1.00000100000001p0"},374{"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"},375{"0x1.000002p0", "0x1.0000010000000fp0"},376377// Potential double rounding cases378{"0x1.000002p0", "0x1.000002fffffffp0"},379{"0x1.000002p0", "0x1.000002fffffff8p0"},380{"0x1.000002p0", "0x1.000002ffffffffp0"},381382{"0x1.000002p0", "0x1.000002ffff0ffp0"},383{"0x1.000002p0", "0x1.000002ffff0ff8p0"},384{"0x1.000002p0", "0x1.000002ffff0fffp0"},385386387{"0x1.000000p0", "0x1.000000fffffffp0"},388{"0x1.000000p0", "0x1.000000fffffff8p0"},389{"0x1.000000p0", "0x1.000000ffffffffp0"},390391{"0x1.000000p0", "0x1.000000ffffffep0"},392{"0x1.000000p0", "0x1.000000ffffffe8p0"},393{"0x1.000000p0", "0x1.000000ffffffefp0"},394395// Float subnormal cases396{"0x0.000002p-126", "0x0.0000010000001p-126"},397{"0x0.000002p-126", "0x0.00000100000000000001p-126"},398399{"0x0.000006p-126", "0x0.0000050000001p-126"},400{"0x0.000006p-126", "0x0.00000500000000000001p-126"},401402{"0x0.0p-149", "0x0.7ffffffffffffffp-149"},403{"0x1.0p-148", "0x1.3ffffffffffffffp-148"},404{"0x1.cp-147", "0x1.bffffffffffffffp-147"},405406{"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"},407};408409String [] signs = {"", "-"};410411for(int i = 0; i < roundingTestCases.length; i++) {412for(int j = 0; j < signs.length; j++) {413String expectedIn = signs[j]+roundingTestCases[i][0];414String resultIn = signs[j]+roundingTestCases[i][1];415416float expected = Float.parseFloat(expectedIn);417float result = Float.parseFloat(resultIn);418419if( Float.compare(expected, result) != 0) {420failures += 1;421System.err.println("" + (i+1));422System.err.println("Expected = " + Float.toHexString(expected));423System.err.println("Rounded = " + Float.toHexString(result));424System.err.println("Double = " + Double.toHexString(Double.parseDouble(resultIn)));425System.err.println("Input = " + resultIn);426System.err.println("");427}428}429}430431return failures;432}433434public static void main(String argv[]) {435int failures = 0;436437failures += doubleTests();438failures += floatTests();439440if (failures != 0) {441throw new RuntimeException("" + failures + " failures while " +442"testing hexadecimal floating-point " +443"parsing.");444}445}446447}448449450