1 /*
2  * Copyright (c) 2012, 2021, 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 package test.java.lang.Math;
24 
25 import java.math.BigDecimal;
26 import java.math.RoundingMode;
27 
28 import org.testng.annotations.Test;
29 import org.testng.Assert;
30 
31 /**
32  * @test Test Math and StrictMath Floor and Ceil Div / Modulo operations.
33  * @bug 6282196 8271602
34  * @summary Basic tests for Floor and Ceil division and modulo methods for both Math
35  * and StrictMath for int and long datatypes.
36  */
37 public class DivModTests {
38 
39     /**
40      * The count of test errors.
41      */
42     private static int errors = 0;
43 
44     /**
45      * @param args the command line arguments are unused
46      *//*
47     public static void main(String[] args) {
48         errors = 0;
49         testIntFloorDivMod();
50         testLongIntFloorDivMod();
51         testLongFloorDivMod();
52         testIntCeilDivMod();
53         testLongIntCeilDivMod();
54         testLongCeilDivMod();
55 
56         if (errors > 0) {
57             throw new RuntimeException(errors + " errors found in DivMod methods.");
58         }
59     }
60     */
61 
62     /**
63      * Report a test failure and increment the error count.
64      * @param message the formatting string
65      * @param args the variable number of arguments for the message.
66      */
fail(String message, Object... args)67     static void fail(String message, Object... args) {
68         final String formattedMessage = String.format(message, args);
69         Assert.fail(formattedMessage);
70     }
71 
72     /**
73      * Test the integer floorDiv and floorMod methods.
74      * Math and StrictMath tested and the same results are expected for both.
75      */
76     @Test
testIntFloorDivMod()77     public void testIntFloorDivMod() {
78         testIntFloorDivMod(4, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
79         testIntFloorDivMod(4, 3, 1, 1);
80         testIntFloorDivMod(3, 3, 1, 0);
81         testIntFloorDivMod(2, 3, 0, 2);
82         testIntFloorDivMod(1, 3, 0, 1);
83         testIntFloorDivMod(0, 3, 0, 0);
84         testIntFloorDivMod(4, -3, -2, -2);
85         testIntFloorDivMod(3, -3, -1, 0);
86         testIntFloorDivMod(2, -3, -1, -1);
87         testIntFloorDivMod(1, -3, -1, -2);
88         testIntFloorDivMod(0, -3, 0, 0);
89         testIntFloorDivMod(-1, 3, -1, 2);
90         testIntFloorDivMod(-2, 3, -1, 1);
91         testIntFloorDivMod(-3, 3, -1, 0);
92         testIntFloorDivMod(-4, 3, -2, 2);
93         testIntFloorDivMod(-1, -3, 0, -1);
94         testIntFloorDivMod(-2, -3, 0, -2);
95         testIntFloorDivMod(-3, -3, 1, 0);
96         testIntFloorDivMod(-4, -3, 1, -1);
97         testIntFloorDivMod(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
98         testIntFloorDivMod(Integer.MAX_VALUE, -1, -Integer.MAX_VALUE, 0);
99         testIntFloorDivMod(Integer.MAX_VALUE, 3, 715827882, 1);
100         testIntFloorDivMod(Integer.MAX_VALUE - 1, 3, 715827882, 0);
101         testIntFloorDivMod(Integer.MIN_VALUE, 3, -715827883, 1);
102         testIntFloorDivMod(Integer.MIN_VALUE + 1, 3, -715827883, 2);
103         testIntFloorDivMod(Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE, 0);
104         testIntFloorDivMod(Integer.MAX_VALUE, Integer.MAX_VALUE, 1, 0);
105         testIntFloorDivMod(Integer.MAX_VALUE, Integer.MIN_VALUE, -1, -1);
106         testIntFloorDivMod(Integer.MIN_VALUE, Integer.MIN_VALUE, 1, 0);
107         testIntFloorDivMod(Integer.MIN_VALUE, Integer.MAX_VALUE, -2, 2147483646);
108         // Special case of integer overflow
109         testIntFloorDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0);
110     }
111 
112     /**
113      * Test FloorDiv and then FloorMod with int data.
114      */
testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected)115     static void testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) {
116         testIntFloorDiv(x, y, divExpected);
117         testIntFloorMod(x, y, modExpected);
118     }
119 
120     /**
121      * Test FloorDiv with int data.
122      */
testIntFloorDiv(int x, int y, Object expected)123     static void testIntFloorDiv(int x, int y, Object expected) {
124         Object result = doFloorDiv(x, y);
125         if (!resultEquals(result, expected)) {
126             fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
127         }
128 
129         Object strict_result = doStrictFloorDiv(x, y);
130         if (!resultEquals(result, expected)) {
131             fail("FAIL: StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
132         }
133     }
134 
135     /**
136      * Test FloorMod with int data.
137      */
testIntFloorMod(int x, int y, Object expected)138     static void testIntFloorMod(int x, int y, Object expected) {
139         Object result = doFloorMod(x, y);
140         if (!resultEquals(result, expected)) {
141             fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
142         }
143 
144         Object strict_result = doStrictFloorMod(x, y);
145         if (!resultEquals(strict_result, expected)) {
146             fail("FAIL: StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
147         }
148 
149         try {
150             // Verify result against double precision floor function
151             int tmp = x / y;     // Force ArithmeticException for divide by zero
152             double ff = x - Math.floor((double)x / (double)y) * y;
153             int fr = (int)ff;
154             boolean t = (fr == ((Integer)result));
155             if (!result.equals(fr)) {
156                 fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr);
157             }
158         } catch (ArithmeticException ae) {
159             if (y != 0) {
160                 fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae);
161             }
162         }
163     }
164 
165     /**
166      * Test the floorDiv and floorMod methods for primitive long.
167      */
168     @Test
testLongFloorDivMod()169     public void testLongFloorDivMod() {
170         testLongFloorDivMod(4L, 0L, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
171         testLongFloorDivMod(4L, 3L, 1L, 1L);
172         testLongFloorDivMod(3L, 3L, 1L, 0L);
173         testLongFloorDivMod(2L, 3L, 0L, 2L);
174         testLongFloorDivMod(1L, 3L, 0L, 1L);
175         testLongFloorDivMod(0L, 3L, 0L, 0L);
176         testLongFloorDivMod(4L, -3L, -2L, -2L);
177         testLongFloorDivMod(3L, -3L, -1L, 0l);
178         testLongFloorDivMod(2L, -3L, -1L, -1L);
179         testLongFloorDivMod(1L, -3L, -1L, -2L);
180         testLongFloorDivMod(0L, -3L, 0L, 0L);
181         testLongFloorDivMod(-1L, 3L, -1L, 2L);
182         testLongFloorDivMod(-2L, 3L, -1L, 1L);
183         testLongFloorDivMod(-3L, 3L, -1L, 0L);
184         testLongFloorDivMod(-4L, 3L, -2L, 2L);
185         testLongFloorDivMod(-1L, -3L, 0L, -1L);
186         testLongFloorDivMod(-2L, -3L, 0L, -2L);
187         testLongFloorDivMod(-3L, -3L, 1L, 0L);
188         testLongFloorDivMod(-4L, -3L, 1L, -1L);
189 
190         testLongFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);
191         testLongFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);
192         testLongFloorDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L, 1L);
193         testLongFloorDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L);
194         testLongFloorDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L - 1L, 1L);
195         testLongFloorDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L - 1L, 2L);
196         testLongFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);
197         testLongFloorDivMod(Long.MAX_VALUE, Long.MAX_VALUE, 1L, 0L);
198         testLongFloorDivMod(Long.MAX_VALUE, Long.MIN_VALUE, -1L, -1L);
199         testLongFloorDivMod(Long.MIN_VALUE, Long.MIN_VALUE, 1L, 0L);
200         testLongFloorDivMod(Long.MIN_VALUE, Long.MAX_VALUE, -2L, 9223372036854775806L);
201         // Special case of integer overflow
202         testLongFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);
203     }
204 
205     /**
206      * Test the long floorDiv and floorMod methods.
207      * Math and StrictMath are tested and the same results are expected for both.
208      */
testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected)209     static void testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) {
210         testLongFloorDiv(x, y, divExpected);
211         testLongFloorMod(x, y, modExpected);
212     }
213 
214     /**
215      * Test FloorDiv with long arguments against expected value.
216      * The expected value is usually a Long but in some cases  is
217      * an ArithmeticException.
218      *
219      * @param x dividend
220      * @param y modulus
221      * @param expected expected value,
222      */
testLongFloorDiv(long x, long y, Object expected)223     static void testLongFloorDiv(long x, long y, Object expected) {
224         Object result = doFloorDiv(x, y);
225         if (!resultEquals(result, expected)) {
226             fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
227         }
228 
229         Object strict_result = doStrictFloorDiv(x, y);
230         if (!resultEquals(strict_result, expected)) {
231             fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
232         }
233     }
234 
235     /**
236      * Test FloorMod of long arguments against expected value.
237      * The expected value is usually a Long but in some cases  is
238      * an ArithmeticException.
239      *
240      * @param x dividend
241      * @param y modulus
242      * @param expected expected value
243      */
testLongFloorMod(long x, long y, Object expected)244     static void testLongFloorMod(long x, long y, Object expected) {
245         Object result = doFloorMod(x, y);
246         if (!resultEquals(result, expected)) {
247             fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
248         }
249 
250         Object strict_result = doStrictFloorMod(x, y);
251         if (!resultEquals(strict_result, expected)) {
252             fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
253         }
254 
255         try {
256             // Verify the result against BigDecimal rounding mode.
257             BigDecimal xD = new BigDecimal(x);
258             BigDecimal yD = new BigDecimal(y);
259             BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);
260             resultD = resultD.multiply(yD);
261             resultD = xD.subtract(resultD);
262             long fr = resultD.longValue();
263             if (!result.equals(fr)) {
264                 fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
265 
266             }
267         } catch (ArithmeticException ae) {
268             if (y != 0) {
269                 fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");
270             }
271         }
272     }
273 
274     /**
275      * Test the floorDiv and floorMod methods for mixed long and int.
276      */
277     @Test
testLongIntFloorDivMod()278     public void testLongIntFloorDivMod() {
279         testLongIntFloorDivMod(4L, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
280         testLongIntFloorDivMod(4L, 3, 1L, 1);
281         testLongIntFloorDivMod(3L, 3, 1L, 0);
282         testLongIntFloorDivMod(2L, 3, 0L, 2);
283         testLongIntFloorDivMod(1L, 3, 0L, 1);
284         testLongIntFloorDivMod(0L, 3, 0L, 0);
285         testLongIntFloorDivMod(4L, -3, -2L, -2);
286         testLongIntFloorDivMod(3L, -3, -1L, 0);
287         testLongIntFloorDivMod(2L, -3, -1L, -1);
288         testLongIntFloorDivMod(1L, -3, -1L, -2);
289         testLongIntFloorDivMod(0L, -3, 0L, 0);
290         testLongIntFloorDivMod(-1L, 3, -1L, 2);
291         testLongIntFloorDivMod(-2L, 3, -1L, 1);
292         testLongIntFloorDivMod(-3L, 3, -1L, 0);
293         testLongIntFloorDivMod(-4L, 3, -2L, 2);
294         testLongIntFloorDivMod(-1L, -3, 0L, -1);
295         testLongIntFloorDivMod(-2L, -3, 0L, -2);
296         testLongIntFloorDivMod(-3L, -3, 1L, 0);
297         testLongIntFloorDivMod(-4L, -3, 1L, -1);
298 
299         testLongIntFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0);
300         testLongIntFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0);
301         testLongIntFloorDivMod(Long.MAX_VALUE, 3, Long.MAX_VALUE / 3L, 1);
302         testLongIntFloorDivMod(Long.MAX_VALUE - 1L, 3, (Long.MAX_VALUE - 1L) / 3L, 0);
303         testLongIntFloorDivMod(Long.MIN_VALUE, 3, Long.MIN_VALUE / 3L - 1L, 1);
304         testLongIntFloorDivMod(Long.MIN_VALUE + 1L, 3, Long.MIN_VALUE / 3L - 1L, 2);
305         testLongIntFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0);
306         testLongIntFloorDivMod(Long.MAX_VALUE, Integer.MAX_VALUE, 4294967298L, 1);
307         testLongIntFloorDivMod(Long.MAX_VALUE, Integer.MIN_VALUE, -4294967296L, -1);
308         testLongIntFloorDivMod(Long.MIN_VALUE, Integer.MIN_VALUE, 4294967296L, 0);
309         testLongIntFloorDivMod(Long.MIN_VALUE, Integer.MAX_VALUE, -4294967299L, 2147483645);
310         // Special case of integer overflow
311         testLongIntFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0);
312     }
313 
314     /**
315      * Test the integer floorDiv and floorMod methods.
316      * Math and StrictMath are tested and the same results are expected for both.
317      */
testLongIntFloorDivMod(long x, int y, Object divExpected, Object modExpected)318     static void testLongIntFloorDivMod(long x, int y, Object divExpected, Object modExpected) {
319         testLongIntFloorDiv(x, y, divExpected);
320         testLongIntFloorMod(x, y, modExpected);
321     }
322 
323     /**
324      * Test FloorDiv with long arguments against expected value.
325      * The expected value is usually a Long but in some cases  is
326      * an ArithmeticException.
327      *
328      * @param x dividend
329      * @param y modulus
330      * @param expected expected value,
331      */
testLongIntFloorDiv(long x, int y, Object expected)332     static void testLongIntFloorDiv(long x, int y, Object expected) {
333         Object result = doFloorDiv(x, y);
334         if (!resultEquals(result, expected)) {
335             fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
336         }
337 
338         Object strict_result = doStrictFloorDiv(x, y);
339         if (!resultEquals(strict_result, expected)) {
340             fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
341         }
342     }
343 
344     /**
345      * Test FloorMod of long arguments against expected value.
346      * The expected value is usually a Long but in some cases  is
347      * an ArithmeticException.
348      *
349      * @param x dividend
350      * @param y modulus
351      * @param expected expected value
352      */
testLongIntFloorMod(long x, int y, Object expected)353     static void testLongIntFloorMod(long x, int y, Object expected) {
354         Object result = doFloorMod(x, y);
355         if (!resultEquals(result, expected)) {
356             fail("FAIL: int Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
357         }
358 
359         Object strict_result = doStrictFloorMod(x, y);
360         if (!resultEquals(strict_result, expected)) {
361             fail("FAIL: int StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
362         }
363 
364         try {
365             // Verify the result against BigDecimal rounding mode.
366             BigDecimal xD = new BigDecimal(x);
367             BigDecimal yD = new BigDecimal(y);
368             BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);
369             resultD = resultD.multiply(yD);
370             resultD = xD.subtract(resultD);
371             int fr = resultD.intValue();
372             if (!result.equals(fr)) {
373                 fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
374             }
375         } catch (ArithmeticException ae) {
376             if (y != 0) {
377                 fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");
378             }
379         }
380     }
381 
382     /**
383      * Invoke floorDiv and return the result or any exception.
384      * @param x the x value
385      * @param y the y value
386      * @return the result Integer or an exception.
387      */
doFloorDiv(int x, int y)388     static Object doFloorDiv(int x, int y) {
389         try {
390             return Math.floorDiv(x, y);
391         } catch (ArithmeticException ae) {
392             return ae;
393         }
394     }
395 
396     /**
397      * Invoke floorDiv and return the result or any exception.
398      * @param x the x value
399      * @param y the y value
400      * @return the result Integer or an exception.
401      */
doFloorDiv(long x, int y)402     static Object doFloorDiv(long x, int y) {
403         try {
404             return Math.floorDiv(x, y);
405         } catch (ArithmeticException ae) {
406             return ae;
407         }
408     }
409 
410     /**
411      * Invoke floorDiv and return the result or any exception.
412      * @param x the x value
413      * @param y the y value
414      * @return the result Integer or an exception.
415      */
doFloorDiv(long x, long y)416     static Object doFloorDiv(long x, long y) {
417         try {
418             return Math.floorDiv(x, y);
419         } catch (ArithmeticException ae) {
420             return ae;
421         }
422     }
423 
424     /**
425      * Invoke floorMod and return the result or any exception.
426      * @param x the x value
427      * @param y the y value
428      * @return the result Integer or an exception.
429      */
doFloorMod(int x, int y)430     static Object doFloorMod(int x, int y) {
431         try {
432             return Math.floorMod(x, y);
433         } catch (ArithmeticException ae) {
434             return ae;
435         }
436     }
437 
438     /**
439      * Invoke floorMod and return the result or any exception.
440      * @param x the x value
441      * @param y the y value
442      * @return the result Integer or an exception.
443      */
doFloorMod(long x, int y)444     static Object doFloorMod(long x, int y) {
445         try {
446             return Math.floorMod(x, y);
447         } catch (ArithmeticException ae) {
448             return ae;
449         }
450     }
451 
452     /**
453      * Invoke floorMod and return the result or any exception.
454      * @param x the x value
455      * @param y the y value
456      * @return the result Integer or an exception.
457      */
doFloorMod(long x, long y)458     static Object doFloorMod(long x, long y) {
459         try {
460             return Math.floorMod(x, y);
461         } catch (ArithmeticException ae) {
462             return ae;
463         }
464     }
465 
466     /**
467      * Invoke floorDiv and return the result or any exception.
468      * @param x the x value
469      * @param y the y value
470      * @return the result Integer or an exception.
471      */
doStrictFloorDiv(int x, int y)472     static Object doStrictFloorDiv(int x, int y) {
473         try {
474             return StrictMath.floorDiv(x, y);
475         } catch (ArithmeticException ae) {
476             return ae;
477         }
478     }
479 
480     /**
481      * Invoke floorDiv and return the result or any exception.
482      * @param x the x value
483      * @param y the y value
484      * @return the result Integer or an exception.
485      */
doStrictFloorDiv(long x, int y)486     static Object doStrictFloorDiv(long x, int y) {
487         try {
488             return StrictMath.floorDiv(x, y);
489         } catch (ArithmeticException ae) {
490             return ae;
491         }
492     }
493 
494     /**
495      * Invoke floorDiv and return the result or any exception.
496      * @param x the x value
497      * @param y the y value
498      * @return the result Integer or an exception.
499      */
doStrictFloorDiv(long x, long y)500     static Object doStrictFloorDiv(long x, long y) {
501         try {
502             return StrictMath.floorDiv(x, y);
503         } catch (ArithmeticException ae) {
504             return ae;
505         }
506     }
507 
508     /**
509      * Invoke floorMod and return the result or any exception.
510      * @param x the x value
511      * @param y the y value
512      * @return the result Integer or an exception.
513      */
doStrictFloorMod(int x, int y)514     static Object doStrictFloorMod(int x, int y) {
515         try {
516             return StrictMath.floorMod(x, y);
517         } catch (ArithmeticException ae) {
518             return ae;
519         }
520     }
521 
522     /**
523      * Invoke floorMod and return the result or any exception.
524      * @param x the x value
525      * @param y the y value
526      * @return the result Integer or an exception.
527      */
doStrictFloorMod(long x, int y)528     static Object doStrictFloorMod(long x, int y) {
529         try {
530             return StrictMath.floorMod(x, y);
531         } catch (ArithmeticException ae) {
532             return ae;
533         }
534     }
535 
536     /**
537      * Invoke floorMod and return the result or any exception.
538      * @param x the x value
539      * @param y the y value
540      * @return the result Integer or an exception.
541      */
doStrictFloorMod(long x, long y)542     static Object doStrictFloorMod(long x, long y) {
543         try {
544             return StrictMath.floorMod(x, y);
545         } catch (ArithmeticException ae) {
546             return ae;
547         }
548     }
549 
550     /**
551      * Test the integer ceilDiv and ceilMod methods.
552      * Math and StrictMath tested and the same results are expected for both.
553      */
554     @Test
testIntCeilDivMod()555     public void testIntCeilDivMod() {
556         testIntCeilDivMod(4, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
557         testIntCeilDivMod(4, 3, 2, -2);
558         testIntCeilDivMod(3, 3, 1, 0);
559         testIntCeilDivMod(2, 3, 1, -1);
560         testIntCeilDivMod(1, 3, 1, -2);
561         testIntCeilDivMod(0, 3, 0, 0);
562         testIntCeilDivMod(4, -3, -1, 1);
563         testIntCeilDivMod(3, -3, -1, 0);
564         testIntCeilDivMod(2, -3, 0, 2);
565         testIntCeilDivMod(1, -3, 0, 1);
566         testIntCeilDivMod(0, -3, 0, 0);
567         testIntCeilDivMod(-1, 3, 0, -1);
568         testIntCeilDivMod(-2, 3, 0, -2);
569         testIntCeilDivMod(-3, 3, -1, 0);
570         testIntCeilDivMod(-4, 3, -1, -1);
571         testIntCeilDivMod(-1, -3, 1, 2);
572         testIntCeilDivMod(-2, -3, 1, 1);
573         testIntCeilDivMod(-3, -3, 1, 0);
574         testIntCeilDivMod(-4, -3, 2, 2);
575         testIntCeilDivMod(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);
576         testIntCeilDivMod(Integer.MAX_VALUE, -1, -Integer.MAX_VALUE, 0);
577         testIntCeilDivMod(Integer.MAX_VALUE, 3, 715_827_883, -2);
578         testIntCeilDivMod(Integer.MAX_VALUE - 1, 3, 715_827_882, 0);
579         testIntCeilDivMod(Integer.MIN_VALUE, 3, -715_827_882, -2);
580         testIntCeilDivMod(Integer.MIN_VALUE + 1, 3, -715_827_882, -1);
581         testIntCeilDivMod(Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE, 0);
582         testIntCeilDivMod(Integer.MAX_VALUE, Integer.MAX_VALUE, 1, 0);
583         testIntCeilDivMod(Integer.MAX_VALUE, Integer.MIN_VALUE, 0, Integer.MAX_VALUE);
584         testIntCeilDivMod(Integer.MIN_VALUE, Integer.MIN_VALUE, 1, 0);
585         testIntCeilDivMod(Integer.MIN_VALUE, Integer.MAX_VALUE, -1, -1);
586         // Special case of integer overflow
587         testIntCeilDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0);
588     }
589 
590     /**
591      * Test CeilDiv and then CeilMod with int data.
592      */
593     @Test
testIntCeilDivMod(int x, int y, Object divExpected, Object modExpected)594     public void testIntCeilDivMod(int x, int y, Object divExpected, Object modExpected) {
595         testIntCeilDiv(x, y, divExpected);
596         testIntCeilMod(x, y, modExpected);
597     }
598 
599     /**
600      * Test CeilDiv with int data.
601      */
testIntCeilDiv(int x, int y, Object expected)602     static void testIntCeilDiv(int x, int y, Object expected) {
603         Object result = doCeilDiv(x, y);
604         if (!resultEquals(result, expected)) {
605             fail("FAIL: Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
606         }
607 
608         Object strict_result = doStrictCeilDiv(x, y);
609         if (!resultEquals(strict_result, expected)) {
610             fail("FAIL: StrictMath.ceilDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
611         }
612     }
613 
614     /**
615      * Test CeilMod with int data.
616      */
testIntCeilMod(int x, int y, Object expected)617     static void testIntCeilMod(int x, int y, Object expected) {
618         Object result = doCeilMod(x, y);
619         if (!resultEquals(result, expected)) {
620             fail("FAIL: Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
621         }
622 
623         Object strict_result = doStrictCeilMod(x, y);
624         if (!resultEquals(strict_result, expected)) {
625             fail("FAIL: StrictMath.ceilMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
626         }
627 
628         try {
629             // Verify result against double precision ceil function
630             int tmp = x / y;     // Force ArithmeticException for divide by zero
631             double ff = x - Math.ceil((double)x / (double)y) * y;
632             int fr = (int)ff;
633             boolean t = (fr == ((Integer)result));
634             if (!result.equals(fr)) {
635                 fail("FAIL: Math.ceilMod(%d, %d) = %s differs from Math.ceil(x, y): %d%n", x, y, result, fr);
636             }
637         } catch (ArithmeticException ae) {
638             if (y != 0) {
639                 fail("FAIL: Math.ceilMod(%d, %d); unexpected %s%n", x, y, ae);
640             }
641         }
642     }
643 
644     /**
645      * Test the ceilDiv and ceilMod methods for primitive long.
646      */
647     @Test
testLongCeilDivMod()648     public void testLongCeilDivMod() {
649         testLongCeilDivMod(4L, 0L, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
650         testLongCeilDivMod(4L, 3L, 2L, -2L);
651         testLongCeilDivMod(3L, 3L, 1L, 0L);
652         testLongCeilDivMod(2L, 3L, 1L, -1L);
653         testLongCeilDivMod(1L, 3L, 1L, -2L);
654         testLongCeilDivMod(0L, 3L, 0L, 0L);
655         testLongCeilDivMod(4L, -3L, -1L, 1L);
656         testLongCeilDivMod(3L, -3L, -1L, 0L);
657         testLongCeilDivMod(2L, -3L, 0L, 2L);
658         testLongCeilDivMod(1L, -3L, 0L, 1L);
659         testLongCeilDivMod(0L, -3L, 0L, 0L);
660         testLongCeilDivMod(-1L, 3L, 0L, -1L);
661         testLongCeilDivMod(-2L, 3L, 0L, -2L);
662         testLongCeilDivMod(-3L, 3L, -1L, 0L);
663         testLongCeilDivMod(-4L, 3L, -1L, -1L);
664         testLongCeilDivMod(-1L, -3L, 1L, 2L);
665         testLongCeilDivMod(-2L, -3L, 1L, 1L);
666         testLongCeilDivMod(-3L, -3L, 1L, 0L);
667         testLongCeilDivMod(-4L, -3L, 2L, 2L);
668 
669         testLongCeilDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);
670         testLongCeilDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);
671         testLongCeilDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L + 1, -2L);
672         testLongCeilDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L);
673         testLongCeilDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L, -2L);
674         testLongCeilDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L, -1L);
675         testLongCeilDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);
676         testLongCeilDivMod(Long.MAX_VALUE, Long.MAX_VALUE, 1L, 0L);
677         testLongCeilDivMod(Long.MAX_VALUE, Long.MIN_VALUE, 0L, Long.MAX_VALUE);
678         testLongCeilDivMod(Long.MIN_VALUE, Long.MIN_VALUE, 1L, 0L);
679         testLongCeilDivMod(Long.MIN_VALUE, Long.MAX_VALUE, -1L, -1L);
680         // Special case of integer overflow
681         testLongCeilDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);
682     }
683 
684     /**
685      * Test the long ceilDiv and ceilMod methods.
686      * Math and StrictMath are tested and the same results are expected for both.
687      */
testLongCeilDivMod(long x, long y, Object divExpected, Object modExpected)688     static void testLongCeilDivMod(long x, long y, Object divExpected, Object modExpected) {
689         testLongCeilDiv(x, y, divExpected);
690         testLongCeilMod(x, y, modExpected);
691     }
692 
693     /**
694      * Test CeilDiv with long arguments against expected value.
695      * The expected value is usually a Long but in some cases  is
696      * an ArithmeticException.
697      *
698      * @param x dividend
699      * @param y modulus
700      * @param expected expected value,
701      */
testLongCeilDiv(long x, long y, Object expected)702     static void testLongCeilDiv(long x, long y, Object expected) {
703         Object result = doCeilDiv(x, y);
704         if (!resultEquals(result, expected)) {
705             fail("FAIL: long Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
706         }
707 
708         Object strict_result = doStrictCeilDiv(x, y);
709         if (!resultEquals(strict_result, expected)) {
710             fail("FAIL: long StrictMath.ceilDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
711         }
712     }
713 
714     /**
715      * Test CeilMod of long arguments against expected value.
716      * The expected value is usually a Long but in some cases  is
717      * an ArithmeticException.
718      *
719      * @param x dividend
720      * @param y modulus
721      * @param expected expected value
722      */
testLongCeilMod(long x, long y, Object expected)723     static void testLongCeilMod(long x, long y, Object expected) {
724         Object result = doCeilMod(x, y);
725         if (!resultEquals(result, expected)) {
726             fail("FAIL: long Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
727         }
728 
729         Object strict_result = doStrictCeilMod(x, y);
730         if (!resultEquals(strict_result, expected)) {
731             fail("FAIL: long StrictMath.ceilMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
732         }
733 
734         try {
735             // Verify the result against BigDecimal rounding mode.
736             BigDecimal xD = new BigDecimal(x);
737             BigDecimal yD = new BigDecimal(y);
738             BigDecimal resultD = xD.divide(yD, RoundingMode.CEILING);
739             resultD = resultD.multiply(yD);
740             resultD = xD.subtract(resultD);
741             long fr = resultD.longValue();
742             if (!result.equals(fr)) {
743                 fail("FAIL: Long.ceilMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
744 
745             }
746         } catch (ArithmeticException ae) {
747             if (y != 0) {
748                 fail("FAIL: long Math.ceilMod(%d, %d); unexpected ArithmeticException from bigdecimal");
749             }
750         }
751     }
752 
753     /**
754      * Test the ceilDiv and ceilMod methods for mixed long and int.
755      */
756     @Test
testLongIntCeilDivMod()757     public void testLongIntCeilDivMod() {
758         testLongIntCeilDivMod(4L, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException
759         testLongIntCeilDivMod(4L, 3, 2L, -2);
760         testLongIntCeilDivMod(3L, 3, 1L, 0);
761         testLongIntCeilDivMod(2L, 3, 1L, -1);
762         testLongIntCeilDivMod(1L, 3, 1L, -2);
763         testLongIntCeilDivMod(0L, 3, 0L, 0);
764         testLongIntCeilDivMod(4L, -3, -1L, 1);
765         testLongIntCeilDivMod(3L, -3, -1L, 0);
766         testLongIntCeilDivMod(2L, -3, 0L, 2);
767         testLongIntCeilDivMod(1L, -3, 0L, 1);
768         testLongIntCeilDivMod(0L, -3, 0L, 0);
769         testLongIntCeilDivMod(-1L, 3, 0L, -1);
770         testLongIntCeilDivMod(-2L, 3, 0L, -2);
771         testLongIntCeilDivMod(-3L, 3, -1L, 0);
772         testLongIntCeilDivMod(-4L, 3, -1L, -1);
773         testLongIntCeilDivMod(-1L, -3, 1L, 2);
774         testLongIntCeilDivMod(-2L, -3, 1L, 1);
775         testLongIntCeilDivMod(-3L, -3, 1L, 0);
776         testLongIntCeilDivMod(-4L, -3, 2L, 2);
777 
778         testLongIntCeilDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0);
779         testLongIntCeilDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0);
780         testLongIntCeilDivMod(Long.MAX_VALUE, 3, Long.MAX_VALUE / 3L + 1, -2);
781         testLongIntCeilDivMod(Long.MAX_VALUE - 1L, 3, (Long.MAX_VALUE - 1L) / 3L, 0);
782         testLongIntCeilDivMod(Long.MIN_VALUE, 3, Long.MIN_VALUE / 3L, -2);
783         testLongIntCeilDivMod(Long.MIN_VALUE + 1L, 3, Long.MIN_VALUE / 3L, -1);
784         testLongIntCeilDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0);
785         testLongIntCeilDivMod(Long.MAX_VALUE, Integer.MAX_VALUE, 4_294_967_299L, -2_147_483_646);
786         testLongIntCeilDivMod(Long.MAX_VALUE, Integer.MIN_VALUE, -4_294_967_295L, 2_147_483_647);
787         testLongIntCeilDivMod(Long.MIN_VALUE, Integer.MIN_VALUE, 4_294_967_296L, 0);
788         testLongIntCeilDivMod(Long.MIN_VALUE, Integer.MAX_VALUE, -4_294_967_298L, -2);
789         // Special case of integer overflow
790         testLongIntCeilDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0);
791     }
792 
793     /**
794      * Test the integer ceilDiv and ceilMod methods.
795      * Math and StrictMath are tested and the same results are expected for both.
796      */
testLongIntCeilDivMod(long x, int y, Object divExpected, Object modExpected)797     static void testLongIntCeilDivMod(long x, int y, Object divExpected, Object modExpected) {
798         testLongIntCeilDiv(x, y, divExpected);
799         testLongIntCeilMod(x, y, modExpected);
800     }
801 
802     /**
803      * Test CeilDiv with long arguments against expected value.
804      * The expected value is usually a Long but in some cases  is
805      * an ArithmeticException.
806      *
807      * @param x dividend
808      * @param y modulus
809      * @param expected expected value,
810      */
testLongIntCeilDiv(long x, int y, Object expected)811     static void testLongIntCeilDiv(long x, int y, Object expected) {
812         Object result = doCeilDiv(x, y);
813         if (!resultEquals(result, expected)) {
814             fail("FAIL: long Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
815         }
816 
817         Object strict_result = doStrictCeilDiv(x, y);
818         if (!resultEquals(strict_result, expected)) {
819             fail("FAIL: long StrictMath.ceilDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
820         }
821     }
822 
823     /**
824      * Test CeilMod of long arguments against expected value.
825      * The expected value is usually a Long but in some cases  is
826      * an ArithmeticException.
827      *
828      * @param x dividend
829      * @param y modulus
830      * @param expected expected value
831      */
testLongIntCeilMod(long x, int y, Object expected)832     static void testLongIntCeilMod(long x, int y, Object expected) {
833         Object result = doCeilMod(x, y);
834         if (!resultEquals(result, expected)) {
835             fail("FAIL: int Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
836         }
837 
838         Object strict_result = doStrictCeilMod(x, y);
839         if (!resultEquals(strict_result, expected)) {
840             fail("FAIL: int StrictMath.ceilMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
841         }
842 
843         try {
844             // Verify the result against BigDecimal rounding mode.
845             BigDecimal xD = new BigDecimal(x);
846             BigDecimal yD = new BigDecimal(y);
847             BigDecimal resultD = xD.divide(yD, RoundingMode.CEILING);
848             resultD = resultD.multiply(yD);
849             resultD = xD.subtract(resultD);
850             int fr = resultD.intValue();
851             if (!result.equals(fr)) {
852                 fail("FAIL: Long.ceilMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
853 
854             }
855         } catch (ArithmeticException ae) {
856             if (y != 0) {
857                 fail("FAIL: long Math.ceilMod(%d, %d); unexpected ArithmeticException from bigdecimal");
858             }
859         }
860     }
861 
862     /**
863      * Invoke ceilDiv and return the result or any exception.
864      * @param x the x value
865      * @param y the y value
866      * @return the result Integer or an exception.
867      */
doCeilDiv(int x, int y)868     static Object doCeilDiv(int x, int y) {
869         try {
870             return Math.ceilDiv(x, y);
871         } catch (ArithmeticException ae) {
872             return ae;
873         }
874     }
875 
876     /**
877      * Invoke ceilDiv and return the result or any exception.
878      * @param x the x value
879      * @param y the y value
880      * @return the result Integer or an exception.
881      */
doCeilDiv(long x, int y)882     static Object doCeilDiv(long x, int y) {
883         try {
884             return Math.ceilDiv(x, y);
885         } catch (ArithmeticException ae) {
886             return ae;
887         }
888     }
889 
890     /**
891      * Invoke ceilDiv and return the result or any exception.
892      * @param x the x value
893      * @param y the y value
894      * @return the result Integer or an exception.
895      */
doCeilDiv(long x, long y)896     static Object doCeilDiv(long x, long y) {
897         try {
898             return Math.ceilDiv(x, y);
899         } catch (ArithmeticException ae) {
900             return ae;
901         }
902     }
903 
904     /**
905      * Invoke ceilMod and return the result or any exception.
906      * @param x the x value
907      * @param y the y value
908      * @return the result Integer or an exception.
909      */
doCeilMod(int x, int y)910     static Object doCeilMod(int x, int y) {
911         try {
912             return Math.ceilMod(x, y);
913         } catch (ArithmeticException ae) {
914             return ae;
915         }
916     }
917 
918     /**
919      * Invoke ceilMod and return the result or any exception.
920      * @param x the x value
921      * @param y the y value
922      * @return the result Integer or an exception.
923      */
doCeilMod(long x, int y)924     static Object doCeilMod(long x, int y) {
925         try {
926             return Math.ceilMod(x, y);
927         } catch (ArithmeticException ae) {
928             return ae;
929         }
930     }
931 
932     /**
933      * Invoke ceilMod and return the result or any exception.
934      * @param x the x value
935      * @param y the y value
936      * @return the result Integer or an exception.
937      */
doCeilMod(long x, long y)938     static Object doCeilMod(long x, long y) {
939         try {
940             return Math.ceilMod(x, y);
941         } catch (ArithmeticException ae) {
942             return ae;
943         }
944     }
945 
946     /**
947      * Invoke ceilDiv and return the result or any exception.
948      * @param x the x value
949      * @param y the y value
950      * @return the result Integer or an exception.
951      */
doStrictCeilDiv(int x, int y)952     static Object doStrictCeilDiv(int x, int y) {
953         try {
954             return StrictMath.ceilDiv(x, y);
955         } catch (ArithmeticException ae) {
956             return ae;
957         }
958     }
959 
960     /**
961      * Invoke ceilDiv and return the result or any exception.
962      * @param x the x value
963      * @param y the y value
964      * @return the result Integer or an exception.
965      */
doStrictCeilDiv(long x, int y)966     static Object doStrictCeilDiv(long x, int y) {
967         try {
968             return StrictMath.ceilDiv(x, y);
969         } catch (ArithmeticException ae) {
970             return ae;
971         }
972     }
973 
974     /**
975      * Invoke ceilDiv and return the result or any exception.
976      * @param x the x value
977      * @param y the y value
978      * @return the result Integer or an exception.
979      */
doStrictCeilDiv(long x, long y)980     static Object doStrictCeilDiv(long x, long y) {
981         try {
982             return StrictMath.ceilDiv(x, y);
983         } catch (ArithmeticException ae) {
984             return ae;
985         }
986     }
987 
988     /**
989      * Invoke ceilMod and return the result or any exception.
990      * @param x the x value
991      * @param y the y value
992      * @return the result Integer or an exception.
993      */
doStrictCeilMod(int x, int y)994     static Object doStrictCeilMod(int x, int y) {
995         try {
996             return StrictMath.ceilMod(x, y);
997         } catch (ArithmeticException ae) {
998             return ae;
999         }
1000     }
1001 
1002     /**
1003      * Invoke ceilMod and return the result or any exception.
1004      * @param x the x value
1005      * @param y the y value
1006      * @return the result Integer or an exception.
1007      */
doStrictCeilMod(long x, int y)1008     static Object doStrictCeilMod(long x, int y) {
1009         try {
1010             return StrictMath.ceilMod(x, y);
1011         } catch (ArithmeticException ae) {
1012             return ae;
1013         }
1014     }
1015 
1016     /**
1017      * Invoke ceilMod and return the result or any exception.
1018      * @param x the x value
1019      * @param y the y value
1020      * @return the result Integer or an exception.
1021      */
doStrictCeilMod(long x, long y)1022     static Object doStrictCeilMod(long x, long y) {
1023         try {
1024             return StrictMath.ceilMod(x, y);
1025         } catch (ArithmeticException ae) {
1026             return ae;
1027         }
1028     }
1029 
1030     /**
1031      * Returns a boolean by comparing the result and the expected value.
1032      * The equals method is not defined for ArithmeticException but it is
1033      * desirable to have equals return true if the expected and the result
1034      * both threw the same exception (class and message.)
1035      *
1036      * @param result the result from testing the method
1037      * @param expected the expected value
1038      * @return true if the result is equal to the expected values; false otherwise.
1039      */
resultEquals(Object result, Object expected)1040     static boolean resultEquals(Object result, Object expected) {
1041         if (result.getClass() != expected.getClass()) {
1042             fail("FAIL: Result type mismatch, %s; expected: %s%n",
1043                     result.getClass().getName(), expected.getClass().getName());
1044             return false;
1045         }
1046 
1047         if (result.equals(expected)) {
1048             return true;
1049         }
1050         // Handle special case to compare ArithmeticExceptions
1051         if (result instanceof ArithmeticException && expected instanceof ArithmeticException) {
1052             return true;
1053         }
1054         return false;
1055     }
1056 
1057 }
1058