test/java/lang/Double/ParseHexFloatingPoint.java

Print this page




  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 import sun.misc.DoubleConsts;
  34 
  35 public class ParseHexFloatingPoint {
  36     private ParseHexFloatingPoint(){}
  37 
  38     public static final double infinityD = Double.POSITIVE_INFINITY;
  39     public static final double NaND = Double.NaN;
  40 
  41     static int test(String testName, String input,
  42                     double result, double expected) {
  43         int failures =0;
  44 
  45         if (Double.compare(result, expected) != 0 ) {
  46             System.err.println("Failure for " + testName +
  47                                ": For input " + input +
  48                                " expected " + expected +
  49                                " got " + result + ".");
  50         }
  51 
  52         return failures;
  53     }


 141             "0x1.8p1",
 142 
 143             "0x3p0",
 144             "0x6.0p-1",
 145             "0xc.0p-2",
 146             "0x18.0p-3",
 147 
 148             "0x3000000p-24",
 149             "0x3.0p0",
 150             "0x3.000000p0",
 151         };
 152         for(int i=0; i < threeTests.length; i++) {
 153             String input = threeTests[i];
 154             failures += testCase(input, 3.0);
 155 
 156             input.replaceFirst("^0x", leadingZeros);
 157             failures += testCase(input, 3.0);
 158         }
 159 
 160         long bigExponents [] = {
 161             2*DoubleConsts.MAX_EXPONENT,
 162             2*DoubleConsts.MIN_EXPONENT,
 163 
 164             (long)Integer.MAX_VALUE-1,
 165             (long)Integer.MAX_VALUE,
 166             (long)Integer.MAX_VALUE+1,
 167 
 168             (long)Integer.MIN_VALUE-1,
 169             (long)Integer.MIN_VALUE,
 170             (long)Integer.MIN_VALUE+1,
 171 
 172             Long.MAX_VALUE-1,
 173             Long.MAX_VALUE,
 174 
 175             Long.MIN_VALUE+1,
 176             Long.MIN_VALUE,
 177         };
 178 
 179         // Test zero significand with large exponents.
 180         for(int i = 0; i < bigExponents.length; i++) {
 181             failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);
 182         }


 209             new PairSD("0x0.8p0",               8.0/16.0),
 210             new PairSD("0x0.9p0",               9.0/16.0),
 211             new PairSD("0x0.ap0",               10.0/16.0),
 212             new PairSD("0x0.bp0",               11.0/16.0),
 213             new PairSD("0x0.cp0",               12.0/16.0),
 214             new PairSD("0x0.dp0",               13.0/16.0),
 215             new PairSD("0x0.ep0",               14.0/16.0),
 216             new PairSD("0x0.fp0",               15.0/16.0),
 217 
 218             // Half-way case between zero and MIN_VALUE rounds down to
 219             // zero
 220             new PairSD("0x1.0p-1075",           0.0),
 221 
 222             // Slighly more than half-way case between zero and
 223             // MIN_VALUES rounds up to zero.
 224             new PairSD("0x1.1p-1075",                   Double.MIN_VALUE),
 225             new PairSD("0x1.000000000001p-1075",        Double.MIN_VALUE),
 226             new PairSD("0x1.000000000000001p-1075",     Double.MIN_VALUE),
 227 
 228             // More subnormal rounding tests
 229             new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(DoubleConsts.MIN_NORMAL)),
 230             new PairSD("0x0.fffffffffffff8p-1022",      DoubleConsts.MIN_NORMAL),
 231             new PairSD("0x0.fffffffffffff800000001p-1022",DoubleConsts.MIN_NORMAL),
 232             new PairSD("0x0.fffffffffffff80000000000000001p-1022",DoubleConsts.MIN_NORMAL),
 233             new PairSD("0x1.0p-1022",                   DoubleConsts.MIN_NORMAL),
 234 
 235 
 236             // Large value and overflow rounding tests
 237             new PairSD("0x1.fffffffffffffp1023",        Double.MAX_VALUE),
 238             new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),
 239             new PairSD("0x1.fffffffffffff4p1023",       Double.MAX_VALUE),
 240             new PairSD("0x1.fffffffffffff7fffffp1023",  Double.MAX_VALUE),
 241             new PairSD("0x1.fffffffffffff8p1023",       infinityD),
 242             new PairSD("0x1.fffffffffffff8000001p1023", infinityD),
 243 
 244             new PairSD("0x1.ffffffffffffep1023",        Math.nextDown(Double.MAX_VALUE)),
 245             new PairSD("0x1.ffffffffffffe0000p1023",    Math.nextDown(Double.MAX_VALUE)),
 246             new PairSD("0x1.ffffffffffffe8p1023",       Math.nextDown(Double.MAX_VALUE)),
 247             new PairSD("0x1.ffffffffffffe7p1023",       Math.nextDown(Double.MAX_VALUE)),
 248             new PairSD("0x1.ffffffffffffeffffffp1023",  Double.MAX_VALUE),
 249             new PairSD("0x1.ffffffffffffe8000001p1023", Double.MAX_VALUE),
 250         };
 251 
 252         for (int i = 0; i < testCases.length; i++) {
 253             failures += testCase(testCases[i].s,testCases[i].d);




  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     }


 140             "0x1.8p1",
 141 
 142             "0x3p0",
 143             "0x6.0p-1",
 144             "0xc.0p-2",
 145             "0x18.0p-3",
 146 
 147             "0x3000000p-24",
 148             "0x3.0p0",
 149             "0x3.000000p0",
 150         };
 151         for(int i=0; i < threeTests.length; i++) {
 152             String input = threeTests[i];
 153             failures += testCase(input, 3.0);
 154 
 155             input.replaceFirst("^0x", leadingZeros);
 156             failures += testCase(input, 3.0);
 157         }
 158 
 159         long bigExponents [] = {
 160             2*Double.MAX_EXPONENT,
 161             2*Double.MIN_EXPONENT,
 162 
 163             (long)Integer.MAX_VALUE-1,
 164             (long)Integer.MAX_VALUE,
 165             (long)Integer.MAX_VALUE+1,
 166 
 167             (long)Integer.MIN_VALUE-1,
 168             (long)Integer.MIN_VALUE,
 169             (long)Integer.MIN_VALUE+1,
 170 
 171             Long.MAX_VALUE-1,
 172             Long.MAX_VALUE,
 173 
 174             Long.MIN_VALUE+1,
 175             Long.MIN_VALUE,
 176         };
 177 
 178         // Test zero significand with large exponents.
 179         for(int i = 0; i < bigExponents.length; i++) {
 180             failures += testCase("0x0.0p"+Long.toString(bigExponents[i]) , 0.0);
 181         }


 208             new PairSD("0x0.8p0",               8.0/16.0),
 209             new PairSD("0x0.9p0",               9.0/16.0),
 210             new PairSD("0x0.ap0",               10.0/16.0),
 211             new PairSD("0x0.bp0",               11.0/16.0),
 212             new PairSD("0x0.cp0",               12.0/16.0),
 213             new PairSD("0x0.dp0",               13.0/16.0),
 214             new PairSD("0x0.ep0",               14.0/16.0),
 215             new PairSD("0x0.fp0",               15.0/16.0),
 216 
 217             // Half-way case between zero and MIN_VALUE rounds down to
 218             // zero
 219             new PairSD("0x1.0p-1075",           0.0),
 220 
 221             // Slighly more than half-way case between zero and
 222             // MIN_VALUES rounds up to zero.
 223             new PairSD("0x1.1p-1075",                   Double.MIN_VALUE),
 224             new PairSD("0x1.000000000001p-1075",        Double.MIN_VALUE),
 225             new PairSD("0x1.000000000000001p-1075",     Double.MIN_VALUE),
 226 
 227             // More subnormal rounding tests
 228             new PairSD("0x0.fffffffffffff7fffffp-1022", Math.nextDown(Double.MIN_NORMAL)),
 229             new PairSD("0x0.fffffffffffff8p-1022",      Double.MIN_NORMAL),
 230             new PairSD("0x0.fffffffffffff800000001p-1022",Double.MIN_NORMAL),
 231             new PairSD("0x0.fffffffffffff80000000000000001p-1022",Double.MIN_NORMAL),
 232             new PairSD("0x1.0p-1022",                   Double.MIN_NORMAL),
 233 
 234 
 235             // Large value and overflow rounding tests
 236             new PairSD("0x1.fffffffffffffp1023",        Double.MAX_VALUE),
 237             new PairSD("0x1.fffffffffffff0000000p1023", Double.MAX_VALUE),
 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);