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 4160406 4705734 4707389 4826774 4895911 4421494
27 * @summary Test for Double.parseDouble method and acceptance regex
28 */
29
30 import java.util.regex.*;
31 import java.math.BigDecimal;
32
33 public class ParseDouble {
34
35 private static void check(String val, double expected) {
36 double n = Double.parseDouble(val);
37 if (n != expected)
38 throw new RuntimeException("Double.parseDouble failed. String:" +
39 val + " Result:" + n);
40 }
41
42 private static void rudimentaryTest() {
43 check(new String(""+Double.MIN_VALUE), Double.MIN_VALUE);
44 check(new String(""+Double.MAX_VALUE), Double.MAX_VALUE);
45
46 check("10", (double) 10.0);
564 // An ulp is the same for all subnormal values
565 BigDecimal ulp_BD = new BigDecimal(Double.MIN_VALUE);
566
567 // Test subnormal powers of two
568 for(int i = -1074; i <= -1022; i++) {
569 double d = Math.scalb(1.0, i);
570
571 /*
572 * The region [d - ulp/2, d + ulp/2] should round to d.
573 */
574 BigDecimal d_BD = new BigDecimal(d);
575
576 BigDecimal lowerBound = d_BD.subtract(ulp_BD.divide(TWO));
577 BigDecimal upperBound = d_BD.add(ulp_BD.divide(TWO));
578
579 double convertedLowerBound = Double.parseDouble(lowerBound.toString());
580 double convertedUpperBound = Double.parseDouble(upperBound.toString());
581 }
582 }
583
584 public static void main(String[] args) throws Exception {
585 rudimentaryTest();
586
587 testParsing(goodStrings, false);
588 testParsing(paddedGoodStrings, false);
589 testParsing(badStrings, true);
590 testParsing(paddedBadStrings, true);
591
592 testRegex(goodStrings, false);
593 testRegex(paddedGoodStrings, false);
594 testRegex(badStrings, true);
595 testRegex(paddedBadStrings, true);
596
597 testSubnormalPowers();
598 }
599 }
|
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 4160406 4705734 4707389 4826774 4895911 4421494 7021568
27 * @summary Test for Double.parseDouble method and acceptance regex
28 */
29
30 import java.util.regex.*;
31 import java.math.BigDecimal;
32
33 public class ParseDouble {
34
35 private static void check(String val, double expected) {
36 double n = Double.parseDouble(val);
37 if (n != expected)
38 throw new RuntimeException("Double.parseDouble failed. String:" +
39 val + " Result:" + n);
40 }
41
42 private static void rudimentaryTest() {
43 check(new String(""+Double.MIN_VALUE), Double.MIN_VALUE);
44 check(new String(""+Double.MAX_VALUE), Double.MAX_VALUE);
45
46 check("10", (double) 10.0);
564 // An ulp is the same for all subnormal values
565 BigDecimal ulp_BD = new BigDecimal(Double.MIN_VALUE);
566
567 // Test subnormal powers of two
568 for(int i = -1074; i <= -1022; i++) {
569 double d = Math.scalb(1.0, i);
570
571 /*
572 * The region [d - ulp/2, d + ulp/2] should round to d.
573 */
574 BigDecimal d_BD = new BigDecimal(d);
575
576 BigDecimal lowerBound = d_BD.subtract(ulp_BD.divide(TWO));
577 BigDecimal upperBound = d_BD.add(ulp_BD.divide(TWO));
578
579 double convertedLowerBound = Double.parseDouble(lowerBound.toString());
580 double convertedUpperBound = Double.parseDouble(upperBound.toString());
581 }
582 }
583
584
585 private static void testStrictness() {
586 final double expected = 0x0.0000008000001p-1022;
587 boolean failed = false;
588 double conversion = 0.0;
589 double sum = 0.0; // Prevent conversion from being optimized away
590
591 //2^-1047 + 2^-1075
592 String decimal = "6.631236871469758276785396630275967243399099947355303144249971758736286630139265439618068200788048744105960420552601852889715006376325666595539603330361800519107591783233358492337208057849499360899425128640718856616503093444922854759159988160304439909868291973931426625698663157749836252274523485312442358651207051292453083278116143932569727918709786004497872322193856150225415211997283078496319412124640111777216148110752815101775295719811974338451936095907419622417538473679495148632480391435931767981122396703443803335529756003353209830071832230689201383015598792184172909927924176339315507402234836120730914783168400715462440053817592702766213559042115986763819482654128770595766806872783349146967171293949598850675682115696218943412532098591327667236328125E-316";
593
594 for(int i = 0; i <= 12_000; i++) {
595 conversion = Double.parseDouble(decimal);
596 sum += conversion;
597 if (conversion != expected) {
598 failed = true;
599 System.out.printf("Iteration %d converts as %a%n",
600 i, conversion);
601 }
602 }
603
604 System.out.println("Sum = " + sum);
605 if (failed)
606 throw new RuntimeException("Inconsistent conversion");
607 }
608
609 public static void main(String[] args) throws Exception {
610 rudimentaryTest();
611
612 testParsing(goodStrings, false);
613 testParsing(paddedGoodStrings, false);
614 testParsing(badStrings, true);
615 testParsing(paddedBadStrings, true);
616
617 testRegex(goodStrings, false);
618 testRegex(paddedGoodStrings, false);
619 testRegex(badStrings, true);
620 testRegex(paddedBadStrings, true);
621
622 testSubnormalPowers();
623 testStrictness();
624 }
625 }
|