Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/ExactArithTests.java
38812 views
1
/*
2
* Copyright (c) 2012, 2013, 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
import java.math.BigInteger;
25
26
/**
27
* @test Test for Math.*Exact integer and long methods.
28
* @bug 6708398
29
* @summary Basic tests for Math exact arithmetic operations.
30
*
31
* @author Roger Riggs
32
*/
33
public class ExactArithTests {
34
35
/**
36
* The count of test errors.
37
*/
38
private static int errors = 0;
39
40
/**
41
* @param args the command line arguments
42
*/
43
public static void main(String[] args) {
44
testIntegerExact();
45
testLongExact();
46
47
if (errors > 0) {
48
throw new RuntimeException(errors + " errors found in ExactArithTests.");
49
}
50
}
51
52
static void fail(String message) {
53
errors++;
54
System.err.println(message);
55
}
56
57
/**
58
* Test Math.addExact, multiplyExact, subtractExact, toIntValue methods
59
* with {@code int} arguments.
60
*/
61
static void testIntegerExact() {
62
testIntegerExact(0, 0);
63
testIntegerExact(1, 1);
64
testIntegerExact(1, -1);
65
testIntegerExact(-1, 1);
66
testIntegerExact(1000, 2000);
67
68
testIntegerExact(Integer.MIN_VALUE, Integer.MIN_VALUE);
69
testIntegerExact(Integer.MAX_VALUE, Integer.MAX_VALUE);
70
testIntegerExact(Integer.MIN_VALUE, 1);
71
testIntegerExact(Integer.MAX_VALUE, 1);
72
testIntegerExact(Integer.MIN_VALUE, 2);
73
testIntegerExact(Integer.MAX_VALUE, 2);
74
testIntegerExact(Integer.MIN_VALUE, -1);
75
testIntegerExact(Integer.MAX_VALUE, -1);
76
testIntegerExact(Integer.MIN_VALUE, -2);
77
testIntegerExact(Integer.MAX_VALUE, -2);
78
79
}
80
81
/**
82
* Test exact arithmetic by comparing with the same operations using long
83
* and checking that the result is the same as the integer truncation.
84
* Errors are reported with {@link fail}.
85
*
86
* @param x first parameter
87
* @param y second parameter
88
*/
89
static void testIntegerExact(int x, int y) {
90
try {
91
// Test addExact
92
int sum = Math.addExact(x, y);
93
long sum2 = (long) x + (long) y;
94
if ((int) sum2 != sum2) {
95
fail("FAIL: int Math.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");
96
} else if (sum != sum2) {
97
fail("FAIL: long Math.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);
98
}
99
} catch (ArithmeticException ex) {
100
long sum2 = (long) x + (long) y;
101
if ((int) sum2 == sum2) {
102
fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);
103
104
}
105
}
106
107
try {
108
// Test subtractExact
109
int diff = Math.subtractExact(x, y);
110
long diff2 = (long) x - (long) y;
111
if ((int) diff2 != diff2) {
112
fail("FAIL: int Math.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);
113
}
114
115
} catch (ArithmeticException ex) {
116
long diff2 = (long) x - (long) y;
117
if ((int) diff2 == diff2) {
118
fail("FAIL: int Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);
119
}
120
}
121
122
try {
123
// Test multiplyExact
124
int product = Math.multiplyExact(x, y);
125
long m2 = (long) x * (long) y;
126
if ((int) m2 != m2) {
127
fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ") = " + product + "; expected: " + m2);
128
}
129
} catch (ArithmeticException ex) {
130
long m2 = (long) x * (long) y;
131
if ((int) m2 == m2) {
132
fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);
133
}
134
}
135
try {
136
// Test incrementExact
137
int inc = Math.incrementExact(x);
138
long inc2 = (long) x + 1L;
139
if ((int) inc2 != inc2) {
140
fail("FAIL: int Math.incrementExact(" + x + ") = " + inc + "; expected Arithmetic exception");
141
} else if (inc != inc2) {
142
fail("FAIL: long Math.incrementExact(" + x + ") = " + inc + "; expected: " + inc2);
143
}
144
} catch (ArithmeticException ex) {
145
long inc2 = (long) x + 1L;
146
if ((int) inc2 == inc2) {
147
fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex);
148
149
}
150
}
151
152
try {
153
// Test decrementExact
154
int dec = Math.decrementExact(x);
155
long dec2 = (long) x - 1L;
156
if ((int) dec2 != dec2) {
157
fail("FAIL: int Math.decrementExact(" + x + ") = " + dec + "; expected Arithmetic exception");
158
} else if (dec != dec2) {
159
fail("FAIL: long Math.decrementExact(" + x + ") = " + dec + "; expected: " + dec2);
160
}
161
} catch (ArithmeticException ex) {
162
long dec2 = (long) x - 1L;
163
if ((int) dec2 == dec2) {
164
fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex);
165
166
}
167
}
168
169
try {
170
// Test negateExact
171
int neg = Math.negateExact(x);
172
long neg2 = -((long)x) ;
173
if ((int) neg2 != neg2) {
174
fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception");
175
} else if (neg != neg2) {
176
fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2);
177
}
178
} catch (ArithmeticException ex) {
179
long neg2 = (long) x - 1L;
180
if ((int) neg2 == neg2) {
181
fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex);
182
183
}
184
}
185
}
186
187
/**
188
* Test Math.addExact, multiplyExact, subtractExact, toIntExact methods
189
* with {@code long} arguments.
190
*/
191
static void testLongExact() {
192
testLongExactTwice(0, 0);
193
testLongExactTwice(1, 1);
194
testLongExactTwice(1, -1);
195
testLongExactTwice(1000, 2000);
196
197
testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);
198
testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);
199
testLongExactTwice(Long.MIN_VALUE, 1);
200
testLongExactTwice(Long.MAX_VALUE, 1);
201
testLongExactTwice(Long.MIN_VALUE, 2);
202
testLongExactTwice(Long.MAX_VALUE, 2);
203
testLongExactTwice(Long.MIN_VALUE, -1);
204
testLongExactTwice(Long.MAX_VALUE, -1);
205
testLongExactTwice(Long.MIN_VALUE, -2);
206
testLongExactTwice(Long.MAX_VALUE, -2);
207
testLongExactTwice(Long.MIN_VALUE/2, 2);
208
testLongExactTwice(Long.MAX_VALUE, 2);
209
testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);
210
testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);
211
testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);
212
testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);
213
testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);
214
testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);
215
testLongExactTwice(Integer.MIN_VALUE/2, 2);
216
217
}
218
219
/**
220
* Test each of the exact operations with the arguments and
221
* with the arguments reversed.
222
* @param x
223
* @param y
224
*/
225
static void testLongExactTwice(long x, long y) {
226
testLongExact(x, y);
227
testLongExact(y, x);
228
}
229
230
231
/**
232
* Test long exact arithmetic by comparing with the same operations using BigInteger
233
* and checking that the result is the same as the long truncation.
234
* Errors are reported with {@link fail}.
235
*
236
* @param x first parameter
237
* @param y second parameter
238
*/
239
static void testLongExact(long x, long y) {
240
BigInteger resultBig = null;
241
final BigInteger xBig = BigInteger.valueOf(x);
242
final BigInteger yBig = BigInteger.valueOf(y);
243
try {
244
// Test addExact
245
resultBig = xBig.add(yBig);
246
long sum = Math.addExact(x, y);
247
checkResult("long Math.addExact", x, y, sum, resultBig);
248
} catch (ArithmeticException ex) {
249
if (inLongRange(resultBig)) {
250
fail("FAIL: long Math.addExact(" + x + " + " + y + "); Unexpected exception: " + ex);
251
}
252
}
253
254
try {
255
// Test subtractExact
256
resultBig = xBig.subtract(yBig);
257
long diff = Math.subtractExact(x, y);
258
checkResult("long Math.subtractExact", x, y, diff, resultBig);
259
} catch (ArithmeticException ex) {
260
if (inLongRange(resultBig)) {
261
fail("FAIL: long Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);
262
}
263
}
264
265
try {
266
// Test multiplyExact
267
resultBig = xBig.multiply(yBig);
268
long product = Math.multiplyExact(x, y);
269
checkResult("long Math.multiplyExact", x, y, product, resultBig);
270
} catch (ArithmeticException ex) {
271
if (inLongRange(resultBig)) {
272
fail("FAIL: long Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);
273
}
274
}
275
276
try {
277
// Test incrementExact
278
resultBig = xBig.add(BigInteger.ONE);
279
long inc = Math.incrementExact(x);
280
checkResult("long Math.incrementExact", x, 1L, inc, resultBig);
281
} catch (ArithmeticException ex) {
282
if (inLongRange(resultBig)) {
283
fail("FAIL: long Math.incrementExact(" + x + "); Unexpected exception: " + ex);
284
}
285
}
286
287
try {
288
// Test decrementExact
289
resultBig = xBig.subtract(BigInteger.ONE);
290
long dec = Math.decrementExact(x);
291
checkResult("long Math.decrementExact", x, 1L, dec, resultBig);
292
} catch (ArithmeticException ex) {
293
if (inLongRange(resultBig)) {
294
fail("FAIL: long Math.decrementExact(" + x + "); Unexpected exception: " + ex);
295
}
296
}
297
298
try {
299
// Test negateExact
300
resultBig = xBig.negate();
301
long dec = Math.negateExact(x);
302
checkResult("long Math.negateExact", x, 0L, dec, resultBig);
303
} catch (ArithmeticException ex) {
304
if (inLongRange(resultBig)) {
305
fail("FAIL: long Math.negateExact(" + x + "); Unexpected exception: " + ex);
306
}
307
}
308
309
try {
310
// Test toIntExact
311
int value = Math.toIntExact(x);
312
if ((long)value != x) {
313
fail("FAIL: " + "long Math.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");
314
}
315
} catch (ArithmeticException ex) {
316
if (resultBig.bitLength() <= 32) {
317
fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);
318
}
319
}
320
321
}
322
323
/**
324
* Compare the expected and actual results.
325
* @param message message for the error
326
* @param x first argument
327
* @param y second argument
328
* @param result actual result value
329
* @param expected expected result value
330
*/
331
static void checkResult(String message, long x, long y, long result, BigInteger expected) {
332
BigInteger resultBig = BigInteger.valueOf(result);
333
if (!inLongRange(expected)) {
334
fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");
335
} else if (!resultBig.equals(expected)) {
336
fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);
337
}
338
}
339
340
/**
341
* Check if the value fits in 64 bits (a long).
342
* @param value
343
* @return true if the value fits in 64 bits (including the sign).
344
*/
345
static boolean inLongRange(BigInteger value) {
346
return value.bitLength() <= 63;
347
}
348
}
349
350