Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/math/BigDecimal/PrecisionTests.java
41149 views
1
/*
2
* Copyright (c) 2009, 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
*/
23
24
/*
25
* @test
26
* @bug 1234567
27
* @summary Test that precision() is computed properly.
28
* @author Joseph D. Darcy
29
*/
30
31
import java.math.*;
32
import static java.math.BigDecimal.*;
33
34
public class PrecisionTests {
35
private static BigDecimal NINE = valueOf(9);
36
37
public static void main(String argv[]) {
38
int failures = 0;
39
40
// Smallest and largest values of a given length
41
BigDecimal[] testValues = {
42
valueOf(1), valueOf(9),
43
};
44
45
failures += testPrecision(new BigDecimal(0), 1);
46
47
for(int i = 1; i < 100; i++) {
48
for(BigDecimal bd : testValues) {
49
failures += testPrecision(bd, i);
50
failures += testPrecision(bd.negate(), i);
51
}
52
53
testValues[0] = testValues[0].multiply(TEN);
54
testValues[1] = testValues[1].multiply(TEN).add(NINE);
55
}
56
57
// The following test tries to cover testings for precision of long values
58
BigDecimal[] randomTestValues = {
59
valueOf(2147483648L), // 2^31: 10 digits
60
valueOf(-2147483648L), // -2^31: 10 digits
61
valueOf(98893745455L), // random: 11 digits
62
valueOf(3455436789887L), // random: 13 digits
63
valueOf(140737488355328L), // 2^47: 15 digits
64
valueOf(-140737488355328L), // -2^47: 15 digits
65
valueOf(7564232235739573L), // random: 16 digits
66
valueOf(25335434990002322L), // random: 17 digits
67
valueOf(9223372036854775807L), // 2^63 - 1: 19 digits
68
valueOf(-9223372036854775807L) // -2^63 + 1: 19 digits
69
};
70
// The array below contains the expected precision of the above numbers
71
int[] expectedPrecision = {10, 10, 11, 13, 15, 15, 16, 17, 19, 19};
72
for (int i = 0; i < randomTestValues.length; i++) {
73
failures += testPrecision(randomTestValues[i], expectedPrecision[i]);
74
}
75
76
if (failures > 0) {
77
throw new RuntimeException("Incurred " + failures +
78
" failures while testing precision.");
79
}
80
}
81
82
private static int testPrecision(BigDecimal bd, int expected) {
83
int precision = bd.precision();
84
85
// System.out.printf("Testing %s, expected %d%n", bd, expected);
86
87
if (precision != expected) {
88
System.err.printf("For (%s).precision expected %d, got %d%n",
89
bd, expected, precision);
90
return 1;
91
}
92
return 0;
93
}
94
}
95
96