author | ohair |
Tue, 25 May 2010 15:58:33 -0700 | |
changeset 5506 | 202f599c92aa |
parent 2923 | eca68ab5d025 |
permissions | -rw-r--r-- |
2923
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
1 |
/* |
5506 | 2 |
* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
2923
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
4 |
* |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
8 |
* |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
13 |
* accompanied this code). |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
14 |
* |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
2923
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
22 |
*/ |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
23 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
24 |
/* |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
25 |
* @test |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
26 |
* @bug 1234567 |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
27 |
* @summary Test that precision() is computed properly. |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
28 |
* @author Joseph D. Darcy |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
29 |
*/ |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
30 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
31 |
import java.math.*; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
32 |
import static java.math.BigDecimal.*; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
33 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
34 |
public class PrecisionTests { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
35 |
private static BigDecimal NINE = valueOf(9); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
36 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
37 |
public static void main(String argv[]) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
38 |
int failures = 0; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
39 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
40 |
// Smallest and largest values of a given length |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
41 |
BigDecimal[] testValues = { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
42 |
valueOf(1), valueOf(9), |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
43 |
}; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
44 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
45 |
failures += testPrecision(new BigDecimal(0), 1); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
46 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
47 |
for(int i = 1; i < 100; i++) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
48 |
for(BigDecimal bd : testValues) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
49 |
failures += testPrecision(bd, i); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
50 |
failures += testPrecision(bd.negate(), i); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
51 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
52 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
53 |
testValues[0] = testValues[0].multiply(TEN); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
54 |
testValues[1] = testValues[1].multiply(TEN).add(NINE); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
55 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
56 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
57 |
// The following test tries to cover testings for precision of long values |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
58 |
BigDecimal[] randomTestValues = { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
59 |
valueOf(2147483648L), // 2^31: 10 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
60 |
valueOf(-2147483648L), // -2^31: 10 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
61 |
valueOf(98893745455L), // random: 11 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
62 |
valueOf(3455436789887L), // random: 13 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
63 |
valueOf(140737488355328L), // 2^47: 15 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
64 |
valueOf(-140737488355328L), // -2^47: 15 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
65 |
valueOf(7564232235739573L), // random: 16 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
66 |
valueOf(25335434990002322L), // random: 17 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
67 |
valueOf(9223372036854775807L), // 2^63 - 1: 19 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
68 |
valueOf(-9223372036854775807L) // -2^63 + 1: 19 digits |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
69 |
}; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
70 |
// The array below contains the expected precision of the above numbers |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
71 |
int[] expectedPrecision = {10, 10, 11, 13, 15, 15, 16, 17, 19, 19}; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
72 |
for (int i = 0; i < randomTestValues.length; i++) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
73 |
failures += testPrecision(randomTestValues[i], expectedPrecision[i]); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
74 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
75 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
76 |
if (failures > 0) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
77 |
throw new RuntimeException("Incurred " + failures + |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
78 |
" failures while testing precision."); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
79 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
80 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
81 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
82 |
private static int testPrecision(BigDecimal bd, int expected) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
83 |
int precision = bd.precision(); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
84 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
85 |
// System.out.printf("Testing %s, expected %d%n", bd, expected); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
86 |
|
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
87 |
if (precision != expected) { |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
88 |
System.err.printf("For (%s).precision expected %d, got %d%n", |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
89 |
bd, expected, precision); |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
90 |
return 1; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
91 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
92 |
return 0; |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
93 |
} |
eca68ab5d025
6806261: BigDecimal.longValueExact() method throws NullPointerException
xlu
parents:
diff
changeset
|
94 |
} |