Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/math/BigDecimal/LongValueExactTests.java
41149 views
1
/*
2
* Copyright (c) 2005, 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 6806261 8211936
27
* @summary Tests of BigDecimal.longValueExact
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 LongValueExactTests {
35
public static void main(String... args) {
36
int failures = 0;
37
38
failures += longValueExactSuccessful();
39
failures += longValueExactExceptional();
40
41
if (failures > 0) {
42
throw new RuntimeException("Incurred " + failures +
43
" failures while testing longValueExact.");
44
}
45
}
46
47
private static long simpleLongValueExact(BigDecimal bd) {
48
return bd.toBigIntegerExact().longValue();
49
}
50
51
private static int longValueExactSuccessful() {
52
int failures = 0;
53
54
// Strings used to create BigDecimal instances on which invoking
55
// longValueExact() will succeed.
56
Map<BigDecimal, Long> successCases =
57
Map.ofEntries(entry(new BigDecimal("9223372036854775807"), Long.MAX_VALUE), // 2^63 -1
58
entry(new BigDecimal("9223372036854775807.0"), Long.MAX_VALUE),
59
entry(new BigDecimal("9223372036854775807.00"), Long.MAX_VALUE),
60
61
entry(new BigDecimal("-9223372036854775808"), Long.MIN_VALUE), // -2^63
62
entry(new BigDecimal("-9223372036854775808.0"), Long.MIN_VALUE),
63
entry(new BigDecimal("-9223372036854775808.00"),Long.MIN_VALUE),
64
65
entry(new BigDecimal("1e0"), 1L),
66
entry(new BigDecimal(BigInteger.ONE, -18), 1_000_000_000_000_000_000L),
67
68
entry(new BigDecimal("0e13"), 0L), // Fast path zero
69
entry(new BigDecimal("0e64"), 0L),
70
entry(new BigDecimal("0e1024"), 0L),
71
72
entry(new BigDecimal("10.000000000000000000000000000000000"), 10L));
73
74
for (var testCase : successCases.entrySet()) {
75
BigDecimal bd = testCase.getKey();
76
long expected = testCase.getValue();
77
try {
78
long longValueExact = bd.longValueExact();
79
if (expected != longValueExact ||
80
longValueExact != simpleLongValueExact(bd)) {
81
failures++;
82
System.err.println("Unexpected longValueExact result " + longValueExact +
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 longValueExactExceptional() {
94
int failures = 0;
95
List<BigDecimal> exceptionalCases =
96
List.of(new BigDecimal("9223372036854775808"), // Long.MAX_VALUE + 1
97
new BigDecimal("9223372036854775808.0"),
98
new BigDecimal("9223372036854775808.00"),
99
new BigDecimal("-9223372036854775809"), // Long.MIN_VALUE - 1
100
new BigDecimal("-9223372036854775808.1"),
101
new BigDecimal("-9223372036854775808.01"),
102
103
new BigDecimal("9999999999999999999"),
104
new BigDecimal("10000000000000000000"),
105
106
new BigDecimal("0.99"),
107
new BigDecimal("0.999999999999999999999"));
108
109
for (BigDecimal bd : exceptionalCases) {
110
try {
111
long longValueExact = bd.longValueExact();
112
failures++;
113
System.err.println("Unexpected non-exceptional longValueExact on " + bd);
114
} catch (ArithmeticException e) {
115
// Success;
116
}
117
}
118
return failures;
119
}
120
}
121
122