Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/math/BigInteger/TestValueExact.java
41149 views
1
/*
2
* Copyright (c) 2011, 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 6371401
27
* @summary Tests of fooValueExact methods
28
* @author Joseph D. Darcy
29
*/
30
import java.math.BigInteger;
31
32
public class TestValueExact {
33
public static void main(String... args) {
34
int errors = 0;
35
36
errors += testLongValueExact();
37
errors += testIntValueExact();
38
errors += testShortValueExact();
39
errors += testByteValueExact();
40
41
if (errors > 0)
42
throw new RuntimeException();
43
}
44
45
private static int testLongValueExact() {
46
int errors = 0;
47
BigInteger[] inRange = {
48
BigInteger.valueOf(Long.MIN_VALUE),
49
BigInteger.ZERO,
50
BigInteger.valueOf(Long.MAX_VALUE)
51
};
52
53
BigInteger[] outOfRange = {
54
BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE),
55
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)
56
};
57
58
for (BigInteger bi : inRange) {
59
if (bi.longValueExact() != bi.longValue()) {
60
System.err.println("Mismatching int conversion for " + bi);
61
errors++;
62
}
63
}
64
65
for (BigInteger bi : outOfRange) {
66
try {
67
long value = bi.longValueExact();
68
System.err.println("Failed to get expected exception on " +
69
bi + " got " + value);
70
errors++;
71
} catch(ArithmeticException ae) {
72
; // Expected
73
}
74
}
75
return errors;
76
}
77
78
private static int testIntValueExact() {
79
int errors = 0;
80
BigInteger[] inRange = {
81
BigInteger.valueOf(Integer.MIN_VALUE),
82
BigInteger.ZERO,
83
BigInteger.ONE,
84
BigInteger.TEN,
85
BigInteger.valueOf(Integer.MAX_VALUE)
86
};
87
88
BigInteger[] outOfRange = {
89
BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
90
BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
91
};
92
93
for (BigInteger bi : inRange) {
94
if (bi.intValueExact() != bi.intValue()) {
95
System.err.println("Mismatching int conversion for " + bi);
96
errors++;
97
}
98
}
99
100
for (BigInteger bi : outOfRange) {
101
try {
102
int value = bi.intValueExact();
103
System.err.println("Failed to get expected exception on " +
104
bi + " got " + value);
105
errors++;
106
} catch(ArithmeticException ae) {
107
; // Expected
108
}
109
}
110
return errors;
111
}
112
113
private static int testShortValueExact() {
114
int errors = 0;
115
BigInteger[] inRange = {
116
BigInteger.valueOf(Short.MIN_VALUE),
117
BigInteger.ZERO,
118
BigInteger.ONE,
119
BigInteger.TEN,
120
BigInteger.valueOf(Short.MAX_VALUE)
121
};
122
123
BigInteger[] outOfRange = {
124
BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
125
BigInteger.valueOf((long)Integer.MIN_VALUE),
126
BigInteger.valueOf( (int)Short.MIN_VALUE - 1),
127
BigInteger.valueOf( (int)Short.MAX_VALUE + 1),
128
BigInteger.valueOf((long)Integer.MAX_VALUE),
129
BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
130
};
131
132
for (BigInteger bi : inRange) {
133
if (bi.shortValueExact() != bi.shortValue()) {
134
System.err.println("Mismatching short conversion for " + bi);
135
errors++;
136
}
137
}
138
139
for (BigInteger bi : outOfRange) {
140
try {
141
int value = bi.shortValueExact();
142
System.err.println("Failed to get expected exception on " +
143
bi + " got " + value);
144
errors++;
145
} catch(ArithmeticException ae) {
146
; // Expected
147
}
148
}
149
return errors;
150
}
151
152
private static int testByteValueExact() {
153
int errors = 0;
154
BigInteger[] inRange = {
155
BigInteger.valueOf(Byte.MIN_VALUE),
156
BigInteger.valueOf(0),
157
BigInteger.ONE,
158
BigInteger.TEN,
159
BigInteger.valueOf(Byte.MAX_VALUE)
160
};
161
162
BigInteger[] outOfRange = {
163
BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
164
BigInteger.valueOf((long)Integer.MIN_VALUE),
165
BigInteger.valueOf( (int)Short.MIN_VALUE - 1),
166
BigInteger.valueOf( (int)Short.MIN_VALUE),
167
BigInteger.valueOf( (int)Byte.MIN_VALUE - 1),
168
BigInteger.valueOf( (int)Byte.MAX_VALUE + 1),
169
BigInteger.valueOf( (int)Short.MAX_VALUE + 1),
170
BigInteger.valueOf( (int)Short.MAX_VALUE),
171
BigInteger.valueOf((long)Integer.MAX_VALUE),
172
BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
173
};
174
175
for (BigInteger bi : inRange) {
176
if (bi.byteValueExact() != bi.byteValue()) {
177
System.err.println("Mismatching byte conversion for " + bi);
178
errors++;
179
}
180
}
181
182
for (BigInteger bi : outOfRange) {
183
try {
184
int value = bi.byteValueExact();
185
System.err.println("Failed to get expected exception on " +
186
bi + " got " + value);
187
errors++;
188
} catch(ArithmeticException ae) {
189
; // Expected
190
}
191
}
192
return errors;
193
}
194
}
195
196