Path: blob/master/test/jdk/java/lang/Double/ParseHexFloatingPoint.java
41152 views
/*1* Copyright (c) 2003, 2017, 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* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run main ParseHexFloatingPoint28* @bug 4826774 807867229* @summary Numerical tests for hexadecimal inputs to parse{Double, Float} (use -Dseed=X to set PRNG seed)30* @author Joseph D. Darcy31* @key randomness32*/3334import jdk.test.lib.RandomFactory;3536public class ParseHexFloatingPoint {37private ParseHexFloatingPoint(){}3839public static final double infinityD = Double.POSITIVE_INFINITY;40public static final double NaND = Double.NaN;4142static int test(String testName, String input,43double result, double expected) {44int failures =0;4546if (Double.compare(result, expected) != 0 ) {47System.err.println("Failure for " + testName +48": For input " + input +49" expected " + expected +50" got " + result + ".");51}5253return failures;54}5556static int testCase(String input, double expected) {57int failures =0;585960// Try different combination of letter components61input = input.toLowerCase(java.util.Locale.US);6263String [] suffices = {"", "f", "F", "d", "D"};64String [] signs = {"", "-", "+"};6566for(int i = 0; i < 2; i++) {67String s1 = input;68if(i == 1)69s1 = s1.replace('x', 'X');7071for(int j = 0; j < 2; j++) {72String s2 = s1;73if(j == 1)74s2 = s2.replace('p', 'P');7576for(int k = 0; k < 2; k++) {77String s3 = s2;78if(k == 1)79s3 = upperCaseHex(s3);808182for(int m = 0; m < suffices.length; m++) {83String s4 = s3 + suffices[m];848586for(int n = 0; n < signs.length; n++) {87String s5 = signs[n] + s4;8889double result = Double.parseDouble(s5);90failures += test("Double.parseDouble",91s5, result, (signs[n].equals("-") ?92-expected:93expected));94}95}96}97}98}99100return failures;101}102103static String upperCaseHex(String s) {104return s.replace('a', 'A').replace('b', 'B').replace('c', 'C').105replace('d', 'D').replace('e','E').replace('f', 'F');106}107108/*109* Test easy and tricky double rounding cases.110*/111static int doubleTests() {112113/*114* A String, double pair115*/116class PairSD {117public String s;118public double d;119PairSD(String s, double d) {120this.s = s;121this.d = d;122}123}124int failures = 0;125126127128// Hex strings that convert to three; test basic functionality129// of significand and exponent shift adjusts along with the130// no-op of adding leading zeros. These cases don't exercise131// the rounding code.132String leadingZeros = "0x0000000000000000000";133String [] threeTests = {134"0x.003p12",135"0x.006p11",136"0x.00cp10",137"0x.018p9",138139"0x.3p4",140"0x.6p3",141"0x.cp2",142"0x1.8p1",143144"0x3p0",145"0x6.0p-1",146"0xc.0p-2",147"0x18.0p-3",148149"0x3000000p-24",150"0x3.0p0",151"0x3.000000p0",152};153for(int i=0; i < threeTests.length; i++) {154String input = threeTests[i];155failures += testCase(input, 3.0);156157input.replaceFirst("^0x", leadingZeros);158failures += testCase(input, 3.0);159}160161long bigExponents [] = {1622*Double.MAX_EXPONENT,1632*Double.MIN_EXPONENT,164165(long)Integer.MAX_VALUE-1,166(long)Integer.MAX_VALUE,167(long)Integer.MAX_VALUE+1,168169(long)Integer.MIN_VALUE-1,170(long)Integer.MIN_VALUE,171(long)Integer.MIN_VALUE+1,172173Long.MAX_VALUE-1,174Long.MAX_VALUE,175176Long.MIN_VALUE+1,177Long.MIN_VALUE,178};179180// Test zero significand with large exponents.181for(int i = 0; i < bigExponents.length; i++) {182failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);183}184185// Test nonzero significand with large exponents.186for(int i = 0; i < bigExponents.length; i++) {187long exponent = bigExponents[i];188failures += testCase("0x10000.0p"+Long.toString(exponent) ,189(exponent <0?0.0:infinityD));190}191192// Test significands with different lengths and bit patterns.193{194long signif = 0;195for(int i = 1; i <= 0xe; i++) {196signif = (signif <<4) | (long)i;197failures += testCase("0x"+Long.toHexString(signif)+"p0", signif);198}199}200201PairSD [] testCases = {202new PairSD("0x0.0p0", 0.0/16.0),203new PairSD("0x0.1p0", 1.0/16.0),204new PairSD("0x0.2p0", 2.0/16.0),205new PairSD("0x0.3p0", 3.0/16.0),206new PairSD("0x0.4p0", 4.0/16.0),207new PairSD("0x0.5p0", 5.0/16.0),208new PairSD("0x0.6p0", 6.0/16.0),209new PairSD("0x0.7p0", 7.0/16.0),210new PairSD("0x0.8p0", 8.0/16.0),211new PairSD("0x0.9p0", 9.0/16.0),212new PairSD("0x0.ap0", 10.0/16.0),213new PairSD("0x0.bp0", 11.0/16.0),214new PairSD("0x0.cp0", 12.0/16.0),215new PairSD("0x0.dp0", 13.0/16.0),216new PairSD("0x0.ep0", 14.0/16.0),217new PairSD("0x0.fp0", 15.0/16.0),218219// Half-way case between zero and MIN_VALUE rounds down to220// zero221new PairSD("0x1.0p-1075", 0.0),222223// Slighly more than half-way case between zero and224// MIN_VALUES rounds up to zero.225new PairSD("0x1.1p-1075", Double.MIN_VALUE),226new PairSD("0x1.000000000001p-1075", Double.MIN_VALUE),227new PairSD("0x1.000000000000001p-1075", Double.MIN_VALUE),228229// More subnormal rounding tests230new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(Double.MIN_NORMAL)),231new PairSD("0x0.fffffffffffff8p-1022", Double.MIN_NORMAL),232new PairSD("0x0.fffffffffffff800000001p-1022",Double.MIN_NORMAL),233new PairSD("0x0.fffffffffffff80000000000000001p-1022",Double.MIN_NORMAL),234new PairSD("0x1.0p-1022", Double.MIN_NORMAL),235236237// Large value and overflow rounding tests238new PairSD("0x1.fffffffffffffp1023", Double.MAX_VALUE),239new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),240new PairSD("0x1.fffffffffffff4p1023", Double.MAX_VALUE),241new PairSD("0x1.fffffffffffff7fffffp1023", Double.MAX_VALUE),242new PairSD("0x1.fffffffffffff8p1023", infinityD),243new PairSD("0x1.fffffffffffff8000001p1023", infinityD),244245new PairSD("0x1.ffffffffffffep1023", Math.nextDown(Double.MAX_VALUE)),246new PairSD("0x1.ffffffffffffe0000p1023", Math.nextDown(Double.MAX_VALUE)),247new PairSD("0x1.ffffffffffffe8p1023", Math.nextDown(Double.MAX_VALUE)),248new PairSD("0x1.ffffffffffffe7p1023", Math.nextDown(Double.MAX_VALUE)),249new PairSD("0x1.ffffffffffffeffffffp1023", Double.MAX_VALUE),250new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),251};252253for (int i = 0; i < testCases.length; i++) {254failures += testCase(testCases[i].s,testCases[i].d);255}256257failures += significandAlignmentTests();258259{260java.util.Random rand = RandomFactory.getRandom();261// Consistency check; double => hexadecimal => double262// preserves the original value.263for(int i = 0; i < 1000; i++) {264double d = rand.nextDouble();265failures += testCase(Double.toHexString(d), d);266}267}268269return failures;270}271272/*273* Verify rounding works the same regardless of how the274* significand is aligned on input. A useful extension could be275* to have this sort of test for strings near the overflow276* threshold.277*/278static int significandAlignmentTests() {279int failures = 0;280// baseSignif * 2^baseExp = nextDown(2.0)281long [] baseSignifs = {2820x1ffffffffffffe00L,2830x1fffffffffffff00L284};285286double [] answers = {287Math.nextDown(Math.nextDown(2.0)),288Math.nextDown(2.0),2892.0290};291292int baseExp = -60;293int count = 0;294for(int i = 0; i < 2; i++) {295for(long j = 0; j <= 0xfL; j++) {296for(long k = 0; k <= 8; k+= 4) { // k = {0, 4, 8}297long base = baseSignifs[i];298long testValue = base | (j<<4) | k;299300int offset = 0;301// Calculate when significand should be incremented302// see table 4.7 in Koren book303304if ((base & 0x100L) == 0L ) { // lsb is 0305if ( (j >= 8L) && // round is 1306((j & 0x7L) != 0 || k != 0 ) ) // sticky is 1307offset = 1;308}309else { // lsb is 1310if (j >= 8L) // round is 1311offset = 1;312}313314double expected = answers[i+offset];315316for(int m = -2; m <= 3; m++) {317count ++;318319// Form equal value string and evaluate it320String s = "0x" +321Long.toHexString((m >=0) ?(testValue<<m):(testValue>>(-m))) +322"p" + (baseExp - m);323324failures += testCase(s, expected);325}326}327}328}329330return failures;331}332333334/*335* Test tricky float rounding cases. The code which336* reads in a hex string converts the string to a double value.337* If a float value is needed, the double value is cast to float.338* However, the cast be itself not always guaranteed to return the339* right result since:340*341* 1. hex string => double can discard a sticky bit which would342* influence a direct hex string => float conversion.343*344* 2. hex string => double => float can have a rounding to double345* precision which results in a larger float value while a direct346* hex string => float conversion would not round up.347*348* This method includes tests of the latter two possibilities.349*/350static int floatTests(){351int failures = 0;352353/*354* A String, float pair355*/356class PairSD {357public String s;358public float f;359PairSD(String s, float f) {360this.s = s;361this.f = f;362}363}364365String [][] roundingTestCases = {366// Target float value hard rouding version367368{"0x1.000000p0", "0x1.0000000000001p0"},369370// Try some values that should round up to nextUp(1.0f)371{"0x1.000002p0", "0x1.0000010000001p0"},372{"0x1.000002p0", "0x1.00000100000008p0"},373{"0x1.000002p0", "0x1.0000010000000fp0"},374{"0x1.000002p0", "0x1.00000100000001p0"},375{"0x1.000002p0", "0x1.00000100000000000000000000000000000000001p0"},376{"0x1.000002p0", "0x1.0000010000000fp0"},377378// Potential double rounding cases379{"0x1.000002p0", "0x1.000002fffffffp0"},380{"0x1.000002p0", "0x1.000002fffffff8p0"},381{"0x1.000002p0", "0x1.000002ffffffffp0"},382383{"0x1.000002p0", "0x1.000002ffff0ffp0"},384{"0x1.000002p0", "0x1.000002ffff0ff8p0"},385{"0x1.000002p0", "0x1.000002ffff0fffp0"},386387388{"0x1.000000p0", "0x1.000000fffffffp0"},389{"0x1.000000p0", "0x1.000000fffffff8p0"},390{"0x1.000000p0", "0x1.000000ffffffffp0"},391392{"0x1.000000p0", "0x1.000000ffffffep0"},393{"0x1.000000p0", "0x1.000000ffffffe8p0"},394{"0x1.000000p0", "0x1.000000ffffffefp0"},395396// Float subnormal cases397{"0x0.000002p-126", "0x0.0000010000001p-126"},398{"0x0.000002p-126", "0x0.00000100000000000001p-126"},399400{"0x0.000006p-126", "0x0.0000050000001p-126"},401{"0x0.000006p-126", "0x0.00000500000000000001p-126"},402403{"0x0.0p-149", "0x0.7ffffffffffffffp-149"},404{"0x1.0p-148", "0x1.3ffffffffffffffp-148"},405{"0x1.cp-147", "0x1.bffffffffffffffp-147"},406407{"0x1.fffffcp-127", "0x1.fffffdffffffffp-127"},408};409410String [] signs = {"", "-"};411412for(int i = 0; i < roundingTestCases.length; i++) {413for(int j = 0; j < signs.length; j++) {414String expectedIn = signs[j]+roundingTestCases[i][0];415String resultIn = signs[j]+roundingTestCases[i][1];416417float expected = Float.parseFloat(expectedIn);418float result = Float.parseFloat(resultIn);419420if( Float.compare(expected, result) != 0) {421failures += 1;422System.err.println("" + (i+1));423System.err.println("Expected = " + Float.toHexString(expected));424System.err.println("Rounded = " + Float.toHexString(result));425System.err.println("Double = " + Double.toHexString(Double.parseDouble(resultIn)));426System.err.println("Input = " + resultIn);427System.err.println("");428}429}430}431432return failures;433}434435public static void main(String argv[]) {436int failures = 0;437438failures += doubleTests();439failures += floatTests();440441if (failures != 0) {442throw new RuntimeException("" + failures + " failures while " +443"testing hexadecimal floating-point " +444"parsing.");445}446}447448}449450451