Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/math/BigDecimal/IntValueExactTests.java
41149 views
1
/*
2
* Copyright (c) 2019, 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 8211936
27
* @summary Tests of BigDecimal.intValueExact
28
*/
29
import java.math.*;
30
import java.util.List;
31
import java.util.Map;
32
import static java.util.Map.entry;
33
34
public class IntValueExactTests {
35
public static void main(String... args) {
36
int failures = 0;
37
38
failures += intValueExactSuccessful();
39
failures += intValueExactExceptional();
40
41
if (failures > 0) {
42
throw new RuntimeException("Incurred " + failures +
43
" failures while testing intValueExact.");
44
}
45
}
46
47
private static int simpleIntValueExact(BigDecimal bd) {
48
return bd.toBigIntegerExact().intValue();
49
}
50
51
private static int intValueExactSuccessful() {
52
int failures = 0;
53
54
// Strings used to create BigDecimal instances on which invoking
55
// intValueExact() will succeed.
56
Map<BigDecimal, Integer> successCases =
57
Map.ofEntries(entry(new BigDecimal("2147483647"), Integer.MAX_VALUE), // 2^31 -1
58
entry(new BigDecimal("2147483647.0"), Integer.MAX_VALUE),
59
entry(new BigDecimal("2147483647.00"), Integer.MAX_VALUE),
60
61
entry(new BigDecimal("-2147483648"), Integer.MIN_VALUE), // -2^31
62
entry(new BigDecimal("-2147483648.0"), Integer.MIN_VALUE),
63
entry(new BigDecimal("-2147483648.00"),Integer.MIN_VALUE),
64
65
entry(new BigDecimal("1e0"), 1),
66
entry(new BigDecimal(BigInteger.ONE, -9), 1_000_000_000),
67
68
entry(new BigDecimal("0e13"), 0), // Fast path zero
69
entry(new BigDecimal("0e32"), 0),
70
entry(new BigDecimal("0e512"), 0),
71
72
entry(new BigDecimal("10.000000000000000000000000000000000"), 10));
73
74
for (var testCase : successCases.entrySet()) {
75
BigDecimal bd = testCase.getKey();
76
int expected = testCase.getValue();
77
try {
78
int intValueExact = bd.intValueExact();
79
if (expected != intValueExact ||
80
intValueExact != simpleIntValueExact(bd)) {
81
failures++;
82
System.err.println("Unexpected intValueExact result " + intValueExact +
83
" on " + bd);
84
}
85
} catch (Exception e) {
86
failures++;
87
System.err.println("Error on " + bd + "\tException message:" + e.getMessage());
88
}
89
}
90
return failures;
91
}
92
93
private static int intValueExactExceptional() {
94
int failures = 0;
95
List<BigDecimal> exceptionalCases =
96
List.of(new BigDecimal("2147483648"), // Integer.MAX_VALUE + 1
97
new BigDecimal("2147483648.0"),
98
new BigDecimal("2147483648.00"),
99
new BigDecimal("-2147483649"), // Integer.MIN_VALUE - 1
100
new BigDecimal("-2147483649.1"),
101
new BigDecimal("-2147483649.01"),
102
103
new BigDecimal("9999999999999999999999999999999"),
104
new BigDecimal("10000000000000000000000000000000"),
105
106
new BigDecimal("0.99"),
107
new BigDecimal("0.999999999999999999999"));
108
109
for (BigDecimal bd : exceptionalCases) {
110
try {
111
int intValueExact = bd.intValueExact();
112
failures++;
113
System.err.println("Unexpected non-exceptional intValueExact on " + bd);
114
} catch (ArithmeticException e) {
115
// Success;
116
}
117
}
118
return failures;
119
}
120
}
121
122