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/DivModTests.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.BigDecimal;
25
import java.math.RoundingMode;
26
27
/**
28
* @test Test Math and StrictMath Floor Div / Modulo operations.
29
* @bug 6282196
30
* @summary Basic tests for Floor division and modulo methods for both Math
31
* and StrictMath for int and long datatypes.
32
*/
33
public class DivModTests {
34
35
/**
36
* The count of test errors.
37
*/
38
private static int errors = 0;
39
40
/**
41
* @param args the command line arguments are unused
42
*/
43
public static void main(String[] args) {
44
errors = 0;
45
testIntFloorDivMod();
46
testLongFloorDivMod();
47
48
if (errors > 0) {
49
throw new RuntimeException(errors + " errors found in DivMod methods.");
50
}
51
}
52
53
/**
54
* Report a test failure and increment the error count.
55
* @param message the formatting string
56
* @param args the variable number of arguments for the message.
57
*/
58
static void fail(String message, Object... args) {
59
errors++;
60
System.out.printf(message, args);
61
}
62
63
/**
64
* Test the integer floorDiv and floorMod methods.
65
* Math and StrictMath tested and the same results are expected for both.
66
*/
67
static void testIntFloorDivMod() {
68
testIntFloorDivMod(4, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
69
testIntFloorDivMod(4, 3, 1, 1);
70
testIntFloorDivMod(3, 3, 1, 0);
71
testIntFloorDivMod(2, 3, 0, 2);
72
testIntFloorDivMod(1, 3, 0, 1);
73
testIntFloorDivMod(0, 3, 0, 0);
74
testIntFloorDivMod(4, -3, -2, -2);
75
testIntFloorDivMod(3, -3, -1, 0);
76
testIntFloorDivMod(2, -3, -1, -1);
77
testIntFloorDivMod(1, -3, -1, -2);
78
testIntFloorDivMod(0, -3, 0, 0);
79
testIntFloorDivMod(-1, 3, -1, 2);
80
testIntFloorDivMod(-2, 3, -1, 1);
81
testIntFloorDivMod(-3, 3, -1, 0);
82
testIntFloorDivMod(-4, 3, -2, 2);
83
testIntFloorDivMod(-1, -3, 0, -1);
84
testIntFloorDivMod(-2, -3, 0, -2);
85
testIntFloorDivMod(-3, -3, 1, 0);
86
testIntFloorDivMod(-4, -3, 1, -1);
87
testIntFloorDivMod(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
88
testIntFloorDivMod(Integer.MAX_VALUE, -1, -Integer.MAX_VALUE, 0);
89
testIntFloorDivMod(Integer.MAX_VALUE, 3, 715827882, 1);
90
testIntFloorDivMod(Integer.MAX_VALUE - 1, 3, 715827882, 0);
91
testIntFloorDivMod(Integer.MIN_VALUE, 3, -715827883, 1);
92
testIntFloorDivMod(Integer.MIN_VALUE + 1, 3, -715827883, 2);
93
testIntFloorDivMod(Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE, 0);
94
// Special case of integer overflow
95
testIntFloorDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0);
96
}
97
98
/**
99
* Test FloorDiv and then FloorMod with int data.
100
*/
101
static void testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) {
102
testIntFloorDiv(x, y, divExpected);
103
testIntFloorMod(x, y, modExpected);
104
}
105
106
/**
107
* Test FloorDiv with int data.
108
*/
109
static void testIntFloorDiv(int x, int y, Object expected) {
110
Object result = doFloorDiv(x, y);
111
if (!resultEquals(result, expected)) {
112
fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
113
}
114
115
Object strict_result = doStrictFloorDiv(x, y);
116
if (!resultEquals(strict_result, expected)) {
117
fail("FAIL: StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
118
}
119
}
120
121
/**
122
* Test FloorMod with int data.
123
*/
124
static void testIntFloorMod(int x, int y, Object expected) {
125
Object result = doFloorMod(x, y);
126
if (!resultEquals(result, expected)) {
127
fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
128
}
129
130
Object strict_result = doStrictFloorMod(x, y);
131
if (!resultEquals(strict_result, expected)) {
132
fail("FAIL: StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
133
}
134
135
try {
136
// Verify result against double precision floor function
137
int tmp = x / y; // Force ArithmeticException for divide by zero
138
double ff = x - Math.floor((double)x / (double)y) * y;
139
int fr = (int)ff;
140
boolean t = (fr == ((Integer)result));
141
if (!result.equals(fr)) {
142
fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr);
143
}
144
} catch (ArithmeticException ae) {
145
if (y != 0) {
146
fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae);
147
}
148
}
149
}
150
151
/**
152
* Test the floorDiv and floorMod methods for primitive long.
153
*/
154
static void testLongFloorDivMod() {
155
testLongFloorDivMod(4L, 0L, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
156
testLongFloorDivMod(4L, 3L, 1L, 1L);
157
testLongFloorDivMod(3L, 3L, 1L, 0L);
158
testLongFloorDivMod(2L, 3L, 0L, 2L);
159
testLongFloorDivMod(1L, 3L, 0L, 1L);
160
testLongFloorDivMod(0L, 3L, 0L, 0L);
161
testLongFloorDivMod(4L, -3L, -2L, -2L);
162
testLongFloorDivMod(3L, -3L, -1L, 0l);
163
testLongFloorDivMod(2L, -3L, -1L, -1L);
164
testLongFloorDivMod(1L, -3L, -1L, -2L);
165
testLongFloorDivMod(0L, -3L, 0L, 0L);
166
testLongFloorDivMod(-1L, 3L, -1L, 2L);
167
testLongFloorDivMod(-2L, 3L, -1L, 1L);
168
testLongFloorDivMod(-3L, 3L, -1L, 0L);
169
testLongFloorDivMod(-4L, 3L, -2L, 2L);
170
testLongFloorDivMod(-1L, -3L, 0L, -1L);
171
testLongFloorDivMod(-2L, -3L, 0L, -2L);
172
testLongFloorDivMod(-3L, -3L, 1L, 0L);
173
testLongFloorDivMod(-4L, -3L, 1L, -1L);
174
175
testLongFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);
176
testLongFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);
177
testLongFloorDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L, 1L);
178
testLongFloorDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L);
179
testLongFloorDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L - 1L, 1L);
180
testLongFloorDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L - 1L, 2L);
181
testLongFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);
182
// Special case of integer overflow
183
testLongFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);
184
}
185
186
/**
187
* Test the integer floorDiv and floorMod methods.
188
* Math and StrictMath are tested and the same results are expected for both.
189
*/
190
static void testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) {
191
testLongFloorDiv(x, y, divExpected);
192
testLongFloorMod(x, y, modExpected);
193
}
194
195
/**
196
* Test FloorDiv with long arguments against expected value.
197
* The expected value is usually a Long but in some cases is
198
* an ArithmeticException.
199
*
200
* @param x dividend
201
* @param y modulus
202
* @param expected expected value,
203
*/
204
static void testLongFloorDiv(long x, long y, Object expected) {
205
Object result = doFloorDiv(x, y);
206
if (!resultEquals(result, expected)) {
207
fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
208
}
209
210
Object strict_result = doStrictFloorDiv(x, y);
211
if (!resultEquals(strict_result, expected)) {
212
fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
213
}
214
}
215
216
/**
217
* Test FloorMod of long arguments against expected value.
218
* The expected value is usually a Long but in some cases is
219
* an ArithmeticException.
220
*
221
* @param x dividend
222
* @param y modulus
223
* @param expected expected value
224
*/
225
static void testLongFloorMod(long x, long y, Object expected) {
226
Object result = doFloorMod(x, y);
227
if (!resultEquals(result, expected)) {
228
fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
229
}
230
231
Object strict_result = doStrictFloorMod(x, y);
232
if (!resultEquals(strict_result, expected)) {
233
fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
234
}
235
236
try {
237
// Verify the result against BigDecimal rounding mode.
238
BigDecimal xD = new BigDecimal(x);
239
BigDecimal yD = new BigDecimal(y);
240
BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);
241
resultD = resultD.multiply(yD);
242
resultD = xD.subtract(resultD);
243
long fr = resultD.longValue();
244
if (!result.equals(fr)) {
245
fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
246
247
}
248
} catch (ArithmeticException ae) {
249
if (y != 0) {
250
fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");
251
}
252
}
253
}
254
255
/**
256
* Invoke floorDiv and return the result or any exception.
257
* @param x the x value
258
* @param y the y value
259
* @return the result Integer or an exception.
260
*/
261
static Object doFloorDiv(int x, int y) {
262
try {
263
return Math.floorDiv(x, y);
264
} catch (ArithmeticException ae) {
265
return ae;
266
}
267
}
268
269
/**
270
* Invoke floorDiv and return the result or any exception.
271
* @param x the x value
272
* @param y the y value
273
* @return the result Integer or an exception.
274
*/
275
static Object doFloorDiv(long x, long y) {
276
try {
277
return Math.floorDiv(x, y);
278
} catch (ArithmeticException ae) {
279
return ae;
280
}
281
}
282
283
/**
284
* Invoke floorDiv and return the result or any exception.
285
* @param x the x value
286
* @param y the y value
287
* @return the result Integer or an exception.
288
*/
289
static Object doFloorMod(int x, int y) {
290
try {
291
return Math.floorMod(x, y);
292
} catch (ArithmeticException ae) {
293
return ae;
294
}
295
}
296
297
/**
298
* Invoke floorDiv and return the result or any exception.
299
* @param x the x value
300
* @param y the y value
301
* @return the result Integer or an exception.
302
*/
303
static Object doFloorMod(long x, long y) {
304
try {
305
return Math.floorMod(x, y);
306
} catch (ArithmeticException ae) {
307
return ae;
308
}
309
}
310
311
/**
312
* Invoke floorDiv and return the result or any exception.
313
* @param x the x value
314
* @param y the y value
315
* @return the result Integer or an exception.
316
*/
317
static Object doStrictFloorDiv(int x, int y) {
318
try {
319
return StrictMath.floorDiv(x, y);
320
} catch (ArithmeticException ae) {
321
return ae;
322
}
323
}
324
325
/**
326
* Invoke floorDiv and return the result or any exception.
327
* @param x the x value
328
* @param y the y value
329
* @return the result Integer or an exception.
330
*/
331
static Object doStrictFloorDiv(long x, long y) {
332
try {
333
return StrictMath.floorDiv(x, y);
334
} catch (ArithmeticException ae) {
335
return ae;
336
}
337
}
338
339
/**
340
* Invoke floorDiv and return the result or any exception.
341
* @param x the x value
342
* @param y the y value
343
* @return the result Integer or an exception.
344
*/
345
static Object doStrictFloorMod(int x, int y) {
346
try {
347
return StrictMath.floorMod(x, y);
348
} catch (ArithmeticException ae) {
349
return ae;
350
}
351
}
352
353
/**
354
* Invoke floorDiv and return the result or any exception.
355
* @param x the x value
356
* @param y the y value
357
* @return the result Integer or an exception.
358
*/
359
static Object doStrictFloorMod(long x, long y) {
360
try {
361
return StrictMath.floorMod(x, y);
362
} catch (ArithmeticException ae) {
363
return ae;
364
}
365
}
366
367
/**
368
* Returns a boolean by comparing the result and the expected value.
369
* The equals method is not defined for ArithmeticException but it is
370
* desirable to have equals return true if the expected and the result
371
* both threw the same exception (class and message.)
372
*
373
* @param result the result from testing the method
374
* @param expected the expected value
375
* @return true if the result is equal to the expected values; false otherwise.
376
*/
377
static boolean resultEquals(Object result, Object expected) {
378
if (result.getClass() != expected.getClass()) {
379
fail("FAIL: Result type mismatch, %s; expected: %s%n",
380
result.getClass().getName(), expected.getClass().getName());
381
return false;
382
}
383
384
if (result.equals(expected)) {
385
return true;
386
}
387
// Handle special case to compare ArithmeticExceptions
388
if (result instanceof ArithmeticException && expected instanceof ArithmeticException) {
389
return true;
390
}
391
return false;
392
}
393
394
}
395
396