/* * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 4160406 4705734 4707389 4826774 4895911 4421494 6358355 7021568 7039369 4396272 * @summary Test for Double.parseDouble method and acceptance regex
*/
// Non-ASCII digits are not recognized "\u0661e\u0661", // 1e1 in Arabic-Indic digits "\u06F1e\u06F1", // 1e1 in Extended Arabic-Indic digits "\u0967e\u0967", // 1e1 in Devanagari digits
// JCK test lex03592m3 ".",
// JCK test lex03592m4 "e42",
// JCK test lex03592m5 ".e42",
// JCK test lex03592m6 "d",
// JCK test lex03592m7 ".d",
// JCK test lex03592m8 "e42d",
// JCK test lex03592m9 ".e42d",
// JCK test lex03593m10 "1A01.01125e-10d",
// JCK test lex03593m11 "2;3.01125e-10d",
// JCK test lex03593m12 "1_34.01125e-10d",
// JCK test lex03593m14 "202..01125e-10d",
// JCK test lex03593m15 "202,01125e-10d",
// JCK test lex03593m16 "202.03b4e-10d",
// JCK test lex03593m18 "202.06_3e-10d",
// JCK test lex03593m20 "202.01125e-f0d",
// JCK test lex03593m21 "202.01125e_3d",
// JCK test lex03593m22 "202.01125e -5d",
// JCK test lex03593m24 "202.01125e-10r",
// JCK test lex03593m25 "202.01125e-10ff",
// JCK test lex03593m26 "1234L.01",
// JCK test lex03593m27 "12ee-2",
// JCK test lex03593m28 "12e-2.2.2",
// JCK test lex03593m29 "12.01e+",
// JCK test lex03593m30 "12.01E",
// Bad hexadecimal-style strings
// Two leading zeros "00x1.0p1",
// Must have hex specifier "1.0p1", "00010p1", "deadbeefp1",
// Need an explicit fully-formed exponent "0x1.0p", "0x1.0",
// Exponent must be in decimal "0x1.0pa", "0x1.0pf",
// Exponent separated by "p" "0x1.0e22", "0x1.0e22",
static String paddedBadStrings[]; static String paddedGoodStrings[]; static {
String pad = " \t\n\r\f\u0001\u000b\u001f";
paddedBadStrings = new String[badStrings.length]; for(int i = 0 ; i < badStrings.length; i++)
paddedBadStrings[i] = pad + badStrings[i] + pad;
paddedGoodStrings = new String[goodStrings.length]; for(int i = 0 ; i < goodStrings.length; i++)
paddedGoodStrings[i] = pad + goodStrings[i] + pad;
}
/* * Throws an exception if <code>Input</code> is * <code>exceptionalInput</code> and {@link Double.parseDouble * parseDouble} does <em>not</em> throw an exception or if * <code>Input</code> is not <code>exceptionalInput</code> and * <code>parseDouble</code> throws an exception. This method does * not attempt to test whether the string is converted to the * proper value; just whether the input is accepted appropriately * or not.
*/ privatestaticvoid testParsing(String [] input, boolean exceptionalInput) { for(int i = 0; i < input.length; i++) { double d;
/* * Throws an exception if <code>Input</code> is * <code>exceptionalInput</code> and the regular expression * matches one of the strings or if <code>Input</code> is not * <code>exceptionalInput</code> and the regular expression fails * to match an input string.
*/ privatestaticvoid testRegex(String [] input, boolean exceptionalInput) { /* * The regex below is taken from the JavaDoc for * Double.valueOf.
*/
final String Digits = "(\\p{Digit}+)"; final String HexDigits = "(\\p{XDigit}+)"; // an exponent is 'e' or 'E' followed by an optionally // signed decimal integer. final String Exp = "[eE][+-]?"+Digits; final String fpRegex =
("[\\x00-\\x20]*"+ // Optional leading "whitespace" "[+-]?(" + // Optional sign character "NaN|" + // "NaN" string "Infinity|" + // "Infinity" string
// A floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2.
// A decimal floating-point string representing a finite positive // number without a leading sign has at most five basic pieces: // Digits . Digits ExponentPart FloatTypeSuffix // // Since this method allows integer-only strings as input // in addition to strings of floating-point literals, the // two sub-patterns below are simplifications of the grammar // productions from the Java Language Specification, 2nd // edition, section 3.10.2.
for(int i = 0; i < input.length; i++) {
Matcher m = fpPattern.matcher(input[i]); if (m.matches() != ! exceptionalInput) { thrownew RuntimeException("Regular expression " +
(exceptionalInput? "accepted bad": "rejected good") + " string `" +
input[i] + "'.");
}
}
}
/** * For each subnormal power of two, test at boundaries of * region that should convert to that value.
*/ privatestaticvoid testSubnormalPowers() { boolean failed = false;
BigDecimal TWO = BigDecimal.valueOf(2); // An ulp is the same for all subnormal values
BigDecimal ulp_BD = new BigDecimal(Double.MIN_VALUE);
// Test subnormal powers of two (except Double.MIN_VALUE) for(int i = -1073; i <= -1022; i++) { double d = Math.scalb(1.0, i);
/* * The region [d - ulp/2, d + ulp/2] should round to d.
*/
BigDecimal d_BD = new BigDecimal(d);
double convertedLowerBound = Double.parseDouble(lowerBound.toString()); double convertedUpperBound = Double.parseDouble(upperBound.toString()); if (convertedLowerBound != d) {
failed = true;
System.out.printf("2^%d lowerBound converts as %a %s%n",
i, convertedLowerBound, lowerBound);
} if (convertedUpperBound != d) {
failed = true;
System.out.printf("2^%d upperBound converts as %a %s%n",
i, convertedUpperBound, upperBound);
}
} /* * Double.MIN_VALUE * The region ]0.5*Double.MIN_VALUE, 1.5*Double.MIN_VALUE[ should round to Double.MIN_VALUE .
*/
BigDecimal minValue = new BigDecimal(Double.MIN_VALUE); if (Double.parseDouble(minValue.multiply(new BigDecimal(0.5)).toString()) != 0.0) {
failed = true;
System.out.printf("0.5*MIN_VALUE doesn't convert 0%n");
} if (Double.parseDouble(minValue.multiply(new BigDecimal(0.50000000001)).toString()) != Double.MIN_VALUE) {
failed = true;
System.out.printf("0.50000000001*MIN_VALUE doesn't convert to MIN_VALUE%n");
} if (Double.parseDouble(minValue.multiply(new BigDecimal(1.49999999999)).toString()) != Double.MIN_VALUE) {
failed = true;
System.out.printf("1.49999999999*MIN_VALUE doesn't convert to MIN_VALUE%n");
} if (Double.parseDouble(minValue.multiply(new BigDecimal(1.5)).toString()) != 2*Double.MIN_VALUE) {
failed = true;
System.out.printf("1.5*MIN_VALUE doesn't convert to 2*MIN_VALUE%n");
}
if (failed) thrownew RuntimeException("Inconsistent conversion");
}
/** * For each power of two, test at boundaries of * region that should convert to that value.
*/ privatestaticvoid testPowers() { for(int i = -1074; i <= +1023; i++) { double d = Math.scalb(1.0, i);
BigDecimal d_BD = new BigDecimal(d);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.