1 /*
2 * Copyright (c) 1998, 2013, 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 */
46 for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33,
47 34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511,
48 512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT,
49 Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) {
50 BigInteger x = ONE.shiftLeft(exponent);
51 for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) {
52 samples.add(y);
53 samples.add(y.negate());
54 }
55 }
56
57 Random rng = new Random(1234567);
58 for (int i = 0; i < 2000; i++) {
59 samples.add(new BigInteger(rng.nextInt(2000), rng));
60 }
61
62 ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples);
63 }
64
65 public static int testDoubleValue() {
66 int failures = 0;
67 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
68 double expected = Double.parseDouble(big.toString());
69 double actual = big.doubleValue();
70
71 // should be bitwise identical
72 if (Double.doubleToRawLongBits(expected) != Double
73 .doubleToRawLongBits(actual)) {
74 System.out.println(big);
75 failures++;
76 }
77 }
78 return failures;
79 }
80
81 public static int testFloatValue() {
82 int failures = 0;
83 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
84 float expected = Float.parseFloat(big.toString());
85 float actual = big.floatValue();
86
87 // should be bitwise identical
88 if (Float.floatToRawIntBits(expected) != Float
89 .floatToRawIntBits(actual)) {
90 System.out.println(big + " " + expected + " " + actual);
91 failures++;
92 }
93 }
94 return failures;
95 }
96
97 public static void main(String[] args) {
98 int failures = testDoubleValue();
99 failures += testFloatValue();
100 if (failures > 0) {
101 throw new RuntimeException("Incurred " + failures
102 + " failures while testing primitive conversions.");
103 }
104 }
105 }
|
1 /*
2 * Copyright (c) 1998, 2018, 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 */
46 for (int exponent : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 31, 32, 33,
47 34, 62, 63, 64, 65, 71, 72, 73, 79, 80, 81, 255, 256, 257, 511,
48 512, 513, Double.MAX_EXPONENT - 1, Double.MAX_EXPONENT,
49 Double.MAX_EXPONENT + 1, 2000, 2001, 2002)) {
50 BigInteger x = ONE.shiftLeft(exponent);
51 for (BigInteger y : Arrays.asList(x, x.add(ONE), x.subtract(ONE))) {
52 samples.add(y);
53 samples.add(y.negate());
54 }
55 }
56
57 Random rng = new Random(1234567);
58 for (int i = 0; i < 2000; i++) {
59 samples.add(new BigInteger(rng.nextInt(2000), rng));
60 }
61
62 ALL_BIGINTEGER_CANDIDATES = Collections.unmodifiableList(samples);
63 }
64
65 public static int testDoubleValue() {
66 System.out.println("--- testDoubleValue ---");
67 int failures = 0;
68 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
69 double expected = Double.parseDouble(big.toString());
70 double actual = big.doubleValue();
71
72 // should be bitwise identical
73 if (Double.doubleToRawLongBits(expected) != Double
74 .doubleToRawLongBits(actual)) {
75 System.out.format("big: %s, expected: %f, actual: %f%n",
76 big, expected, actual);
77 failures++;
78 }
79 }
80 return failures;
81 }
82
83 public static int testFloatValue() {
84 System.out.println("--- testFloatValue ---");
85 int failures = 0;
86 for (BigInteger big : ALL_BIGINTEGER_CANDIDATES) {
87 float expected = Float.parseFloat(big.toString());
88 float actual = big.floatValue();
89
90 // should be bitwise identical
91 if (Float.floatToRawIntBits(expected) != Float
92 .floatToRawIntBits(actual)) {
93 System.out.format("big: %s, expected: %f, actual: %f%n",
94 big, expected, actual);
95 failures++;
96 }
97 }
98 return failures;
99 }
100
101 public static void main(String[] args) {
102 int failures = testDoubleValue();
103 failures += testFloatValue();
104 if (failures > 0) {
105 throw new RuntimeException("Incurred " + failures
106 + " failures while testing primitive conversions.");
107 }
108 }
109 }
|