test/java/lang/Double/ParseHexFloatingPoint.java

Print this page
rev 11813 : 8078586: java/lang/Double/ParseHexFloatingPoint.java fails intermittently
Summary: Add ability to initial the random number generator from the system property "seed" and print to STDOUT the seed value actually used.
Reviewed-by: XXX
   1 /*
   2  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 4826774

  27  * @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat
  28  * @author Joseph D. Darcy
  29  */
  30 
  31 
  32 import java.util.regex.*;
  33 
  34 public class ParseHexFloatingPoint {
  35     private ParseHexFloatingPoint(){}
  36 
  37     public static final double infinityD = Double.POSITIVE_INFINITY;
  38     public static final double NaND = Double.NaN;
  39 
  40     static int test(String testName, String input,
  41                     double result, double expected) {
  42         int failures =0;
  43 
  44         if (Double.compare(result, expected) != 0 ) {
  45             System.err.println("Failure for " + testName +
  46                                ": For input " + input +
  47                                " expected " + expected +
  48                                " got " + result + ".");
  49         }
  50 
  51         return failures;
  52     }
  53 


 238             new PairSD("0x1.fffffffffffff4p1023",       Double.MAX_VALUE),
 239             new PairSD("0x1.fffffffffffff7fffffp1023",  Double.MAX_VALUE),
 240             new PairSD("0x1.fffffffffffff8p1023",       infinityD),
 241             new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
 242 
 243             new PairSD("0x1.ffffffffffffep1023",        Math.nextDown(Double.MAX_VALUE)),
 244             new PairSD("0x1.ffffffffffffe0000p1023",    Math.nextDown(Double.MAX_VALUE)),
 245             new PairSD("0x1.ffffffffffffe8p1023",       Math.nextDown(Double.MAX_VALUE)),
 246             new PairSD("0x1.ffffffffffffe7p1023",       Math.nextDown(Double.MAX_VALUE)),
 247             new PairSD("0x1.ffffffffffffeffffffp1023",  Double.MAX_VALUE),
 248             new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
 249         };
 250 
 251         for (int i = 0; i < testCases.length; i++) {
 252             failures += testCase(testCases[i].s,testCases[i].d);
 253         }
 254 
 255         failures += significandAlignmentTests();
 256 
 257         {
 258             java.util.Random rand = new java.util.Random();

 259             // Consistency check; double => hexadecimal => double
 260             // preserves the original value.
 261             for(int i = 0; i < 1000; i++) {
 262                 double d = rand.nextDouble();
 263                 failures += testCase(Double.toHexString(d), d);
 264             }
 265         }
 266 
 267         return failures;
 268     }
 269 
 270     /*
 271      * Verify rounding works the same regardless of how the
 272      * significand is aligned on input.  A useful extension could be
 273      * to have this sort of test for strings near the overflow
 274      * threshold.
 275      */
 276     static int significandAlignmentTests() {
 277         int failures = 0;
 278                 // baseSignif * 2^baseExp = nextDown(2.0)


   1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @library ..
  27  * @bug 4826774 8078586
  28  * @summary Numerical tests for hexadecimal inputs to parseDouble, parseFloat
  29  * @author Joseph D. Darcy
  30  */
  31 



  32 public class ParseHexFloatingPoint {
  33     private ParseHexFloatingPoint(){}
  34 
  35     public static final double infinityD = Double.POSITIVE_INFINITY;
  36     public static final double NaND = Double.NaN;
  37 
  38     static int test(String testName, String input,
  39                     double result, double expected) {
  40         int failures =0;
  41 
  42         if (Double.compare(result, expected) != 0 ) {
  43             System.err.println("Failure for " + testName +
  44                                ": For input " + input +
  45                                " expected " + expected +
  46                                " got " + result + ".");
  47         }
  48 
  49         return failures;
  50     }
  51 


 236             new PairSD("0x1.fffffffffffff4p1023",       Double.MAX_VALUE),
 237             new PairSD("0x1.fffffffffffff7fffffp1023",  Double.MAX_VALUE),
 238             new PairSD("0x1.fffffffffffff8p1023",       infinityD),
 239             new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
 240 
 241             new PairSD("0x1.ffffffffffffep1023",        Math.nextDown(Double.MAX_VALUE)),
 242             new PairSD("0x1.ffffffffffffe0000p1023",    Math.nextDown(Double.MAX_VALUE)),
 243             new PairSD("0x1.ffffffffffffe8p1023",       Math.nextDown(Double.MAX_VALUE)),
 244             new PairSD("0x1.ffffffffffffe7p1023",       Math.nextDown(Double.MAX_VALUE)),
 245             new PairSD("0x1.ffffffffffffeffffffp1023",  Double.MAX_VALUE),
 246             new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
 247         };
 248 
 249         for (int i = 0; i < testCases.length; i++) {
 250             failures += testCase(testCases[i].s,testCases[i].d);
 251         }
 252 
 253         failures += significandAlignmentTests();
 254 
 255         {
 256             long seed = RandomSeedFactory.getSeed();
 257             java.util.Random rand = new java.util.Random(seed);
 258             // Consistency check; double => hexadecimal => double
 259             // preserves the original value.
 260             for(int i = 0; i < 1000; i++) {
 261                 double d = rand.nextDouble();
 262                 failures += testCase(Double.toHexString(d), d);
 263             }
 264         }
 265 
 266         return failures;
 267     }
 268 
 269     /*
 270      * Verify rounding works the same regardless of how the
 271      * significand is aligned on input.  A useful extension could be
 272      * to have this sort of test for strings near the overflow
 273      * threshold.
 274      */
 275     static int significandAlignmentTests() {
 276         int failures = 0;
 277                 // baseSignif * 2^baseExp = nextDown(2.0)