1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.util;
23
24 import org.hipparchus.exception.MathIllegalArgumentException;
25 import org.hipparchus.exception.MathRuntimeException;
26 import org.hipparchus.random.RandomDataGenerator;
27 import org.junit.jupiter.api.Test;
28 import org.junit.jupiter.api.Timeout;
29
30 import java.math.BigInteger;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.concurrent.TimeUnit;
34
35 import static org.junit.jupiter.api.Assertions.assertEquals;
36 import static org.junit.jupiter.api.Assertions.assertThrows;
37 import static org.junit.jupiter.api.Assertions.assertTrue;
38 import static org.junit.jupiter.api.Assertions.fail;
39
40
41
42
43 class ArithmeticUtilsTest {
44
45 @Test
46 void testAddAndCheck() {
47 int big = Integer.MAX_VALUE;
48 int bigNeg = Integer.MIN_VALUE;
49 assertEquals(big, ArithmeticUtils.addAndCheck(big, 0));
50 try {
51 ArithmeticUtils.addAndCheck(big, 1);
52 fail("Expecting MathRuntimeException");
53 } catch (MathRuntimeException ex) {
54 }
55 try {
56 ArithmeticUtils.addAndCheck(bigNeg, -1);
57 fail("Expecting MathRuntimeException");
58 } catch (MathRuntimeException ex) {
59 }
60 }
61
62 @Test
63 void testAddAndCheckLong() {
64 long max = Long.MAX_VALUE;
65 long min = Long.MIN_VALUE;
66 assertEquals(max, ArithmeticUtils.addAndCheck(max, 0L));
67 assertEquals(min, ArithmeticUtils.addAndCheck(min, 0L));
68 assertEquals(max, ArithmeticUtils.addAndCheck(0L, max));
69 assertEquals(min, ArithmeticUtils.addAndCheck(0L, min));
70 assertEquals(1, ArithmeticUtils.addAndCheck(-1L, 2L));
71 assertEquals(1, ArithmeticUtils.addAndCheck(2L, -1L));
72 assertEquals(-3, ArithmeticUtils.addAndCheck(-2L, -1L));
73 assertEquals(min, ArithmeticUtils.addAndCheck(min + 1, -1L));
74 assertEquals(-1, ArithmeticUtils.addAndCheck(min, max));
75 testAddAndCheckLongFailure(max, 1L);
76 testAddAndCheckLongFailure(min, -1L);
77 testAddAndCheckLongFailure(1L, max);
78 testAddAndCheckLongFailure(-1L, min);
79 testAddAndCheckLongFailure(max, max);
80 testAddAndCheckLongFailure(min, min);
81 }
82
83 @Test
84 void testGcd() {
85 int a = 30;
86 int b = 50;
87 int c = 77;
88
89 assertEquals(0, ArithmeticUtils.gcd(0, 0));
90
91 assertEquals(b, ArithmeticUtils.gcd(0, b));
92 assertEquals(a, ArithmeticUtils.gcd(a, 0));
93 assertEquals(b, ArithmeticUtils.gcd(0, -b));
94 assertEquals(a, ArithmeticUtils.gcd(-a, 0));
95
96 assertEquals(10, ArithmeticUtils.gcd(a, b));
97 assertEquals(10, ArithmeticUtils.gcd(-a, b));
98 assertEquals(10, ArithmeticUtils.gcd(a, -b));
99 assertEquals(10, ArithmeticUtils.gcd(-a, -b));
100
101 assertEquals(1, ArithmeticUtils.gcd(a, c));
102 assertEquals(1, ArithmeticUtils.gcd(-a, c));
103 assertEquals(1, ArithmeticUtils.gcd(a, -c));
104 assertEquals(1, ArithmeticUtils.gcd(-a, -c));
105
106 assertEquals(3 * (1<<15), ArithmeticUtils.gcd(3 * (1<<20), 9 * (1<<15)));
107
108 assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(Integer.MAX_VALUE, 0));
109 assertEquals(Integer.MAX_VALUE, ArithmeticUtils.gcd(-Integer.MAX_VALUE, 0));
110 assertEquals(1<<30, ArithmeticUtils.gcd(1<<30, -Integer.MIN_VALUE));
111 try {
112
113 ArithmeticUtils.gcd(Integer.MIN_VALUE, 0);
114 fail("expecting MathRuntimeException");
115 } catch (MathRuntimeException expected) {
116
117 }
118 try {
119
120 ArithmeticUtils.gcd(0, Integer.MIN_VALUE);
121 fail("expecting MathRuntimeException");
122 } catch (MathRuntimeException expected) {
123
124 }
125 try {
126
127 ArithmeticUtils.gcd(Integer.MIN_VALUE, Integer.MIN_VALUE);
128 fail("expecting MathRuntimeException");
129 } catch (MathRuntimeException expected) {
130
131 }
132 }
133
134 @Test
135 void testGcdConsistency() {
136 int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
137 ArrayList<Integer> primes = new ArrayList<Integer>();
138 for (int i = 0; i < primeList.length; i++) {
139 primes.add(Integer.valueOf(primeList[i]));
140 }
141 RandomDataGenerator randomData = new RandomDataGenerator();
142 for (int i = 0; i < 20; i++) {
143 Object[] sample = randomData.nextSample(primes, 4);
144 int p1 = ((Integer) sample[0]).intValue();
145 int p2 = ((Integer) sample[1]).intValue();
146 int p3 = ((Integer) sample[2]).intValue();
147 int p4 = ((Integer) sample[3]).intValue();
148 int i1 = p1 * p2 * p3;
149 int i2 = p1 * p2 * p4;
150 int gcd = p1 * p2;
151 assertEquals(gcd, ArithmeticUtils.gcd(i1, i2));
152 long l1 = i1;
153 long l2 = i2;
154 assertEquals(gcd, ArithmeticUtils.gcd(l1, l2));
155 }
156 }
157
158 @Test
159 void testGcdLong(){
160 long a = 30;
161 long b = 50;
162 long c = 77;
163
164 assertEquals(0, ArithmeticUtils.gcd(0L, 0));
165
166 assertEquals(b, ArithmeticUtils.gcd(0, b));
167 assertEquals(a, ArithmeticUtils.gcd(a, 0));
168 assertEquals(b, ArithmeticUtils.gcd(0, -b));
169 assertEquals(a, ArithmeticUtils.gcd(-a, 0));
170
171 assertEquals(10, ArithmeticUtils.gcd(a, b));
172 assertEquals(10, ArithmeticUtils.gcd(-a, b));
173 assertEquals(10, ArithmeticUtils.gcd(a, -b));
174 assertEquals(10, ArithmeticUtils.gcd(-a, -b));
175
176 assertEquals(1, ArithmeticUtils.gcd(a, c));
177 assertEquals(1, ArithmeticUtils.gcd(-a, c));
178 assertEquals(1, ArithmeticUtils.gcd(a, -c));
179 assertEquals(1, ArithmeticUtils.gcd(-a, -c));
180
181 assertEquals(3L * (1L<<45), ArithmeticUtils.gcd(3L * (1L<<50), 9L * (1L<<45)));
182
183 assertEquals(1L<<45, ArithmeticUtils.gcd(1L<<45, Long.MIN_VALUE));
184
185 assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(Long.MAX_VALUE, 0L));
186 assertEquals(Long.MAX_VALUE, ArithmeticUtils.gcd(-Long.MAX_VALUE, 0L));
187 assertEquals(1, ArithmeticUtils.gcd(60247241209L, 153092023L));
188 try {
189
190 ArithmeticUtils.gcd(Long.MIN_VALUE, 0);
191 fail("expecting MathRuntimeException");
192 } catch (MathRuntimeException expected) {
193
194 }
195 try {
196
197 ArithmeticUtils.gcd(0, Long.MIN_VALUE);
198 fail("expecting MathRuntimeException");
199 } catch (MathRuntimeException expected) {
200
201 }
202 try {
203
204 ArithmeticUtils.gcd(Long.MIN_VALUE, Long.MIN_VALUE);
205 fail("expecting MathRuntimeException");
206 } catch (MathRuntimeException expected) {
207
208 }
209 }
210
211
212 @Test
213 void testLcm() {
214 int a = 30;
215 int b = 50;
216 int c = 77;
217
218 assertEquals(0, ArithmeticUtils.lcm(0, b));
219 assertEquals(0, ArithmeticUtils.lcm(a, 0));
220 assertEquals(b, ArithmeticUtils.lcm(1, b));
221 assertEquals(a, ArithmeticUtils.lcm(a, 1));
222 assertEquals(150, ArithmeticUtils.lcm(a, b));
223 assertEquals(150, ArithmeticUtils.lcm(-a, b));
224 assertEquals(150, ArithmeticUtils.lcm(a, -b));
225 assertEquals(150, ArithmeticUtils.lcm(-a, -b));
226 assertEquals(2310, ArithmeticUtils.lcm(a, c));
227
228
229
230 assertEquals((1<<20)*15, ArithmeticUtils.lcm((1<<20)*3, (1<<20)*5));
231
232
233 assertEquals(0, ArithmeticUtils.lcm(0, 0));
234
235 try {
236
237 ArithmeticUtils.lcm(Integer.MIN_VALUE, 1);
238 fail("Expecting MathRuntimeException");
239 } catch (MathRuntimeException expected) {
240
241 }
242
243 try {
244
245 ArithmeticUtils.lcm(Integer.MIN_VALUE, 1<<20);
246 fail("Expecting MathRuntimeException");
247 } catch (MathRuntimeException expected) {
248
249 }
250
251 try {
252 ArithmeticUtils.lcm(Integer.MAX_VALUE, Integer.MAX_VALUE - 1);
253 fail("Expecting MathRuntimeException");
254 } catch (MathRuntimeException expected) {
255
256 }
257 }
258
259 @Test
260 void testLcmLong() {
261 long a = 30;
262 long b = 50;
263 long c = 77;
264
265 assertEquals(0, ArithmeticUtils.lcm(0, b));
266 assertEquals(0, ArithmeticUtils.lcm(a, 0));
267 assertEquals(b, ArithmeticUtils.lcm(1, b));
268 assertEquals(a, ArithmeticUtils.lcm(a, 1));
269 assertEquals(150, ArithmeticUtils.lcm(a, b));
270 assertEquals(150, ArithmeticUtils.lcm(-a, b));
271 assertEquals(150, ArithmeticUtils.lcm(a, -b));
272 assertEquals(150, ArithmeticUtils.lcm(-a, -b));
273 assertEquals(2310, ArithmeticUtils.lcm(a, c));
274
275 assertEquals(Long.MAX_VALUE, ArithmeticUtils.lcm(60247241209L, 153092023L));
276
277
278
279 assertEquals((1L<<50)*15, ArithmeticUtils.lcm((1L<<45)*3, (1L<<50)*5));
280
281
282 assertEquals(0L, ArithmeticUtils.lcm(0L, 0L));
283
284 try {
285
286 ArithmeticUtils.lcm(Long.MIN_VALUE, 1);
287 fail("Expecting MathRuntimeException");
288 } catch (MathRuntimeException expected) {
289
290 }
291
292 try {
293
294 ArithmeticUtils.lcm(Long.MIN_VALUE, 1<<20);
295 fail("Expecting MathRuntimeException");
296 } catch (MathRuntimeException expected) {
297
298 }
299
300 assertEquals((long) Integer.MAX_VALUE * (Integer.MAX_VALUE - 1),
301 ArithmeticUtils.lcm((long)Integer.MAX_VALUE, Integer.MAX_VALUE - 1));
302 try {
303 ArithmeticUtils.lcm(Long.MAX_VALUE, Long.MAX_VALUE - 1);
304 fail("Expecting MathRuntimeException");
305 } catch (MathRuntimeException expected) {
306
307 }
308 }
309
310 @Test
311 void testMulAndCheck() {
312 int big = Integer.MAX_VALUE;
313 int bigNeg = Integer.MIN_VALUE;
314 assertEquals(big, ArithmeticUtils.mulAndCheck(big, 1));
315 try {
316 ArithmeticUtils.mulAndCheck(big, 2);
317 fail("Expecting MathRuntimeException");
318 } catch (MathRuntimeException ex) {
319 }
320 try {
321 ArithmeticUtils.mulAndCheck(bigNeg, 2);
322 fail("Expecting MathRuntimeException");
323 } catch (MathRuntimeException ex) {
324 }
325 }
326
327 @Test
328 void testMulAndCheckLong() {
329 long max = Long.MAX_VALUE;
330 long min = Long.MIN_VALUE;
331 assertEquals(max, ArithmeticUtils.mulAndCheck(max, 1L));
332 assertEquals(min, ArithmeticUtils.mulAndCheck(min, 1L));
333 assertEquals(0L, ArithmeticUtils.mulAndCheck(max, 0L));
334 assertEquals(0L, ArithmeticUtils.mulAndCheck(min, 0L));
335 assertEquals(max, ArithmeticUtils.mulAndCheck(1L, max));
336 assertEquals(min, ArithmeticUtils.mulAndCheck(1L, min));
337 assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, max));
338 assertEquals(0L, ArithmeticUtils.mulAndCheck(0L, min));
339 assertEquals(1L, ArithmeticUtils.mulAndCheck(-1L, -1L));
340 assertEquals(min, ArithmeticUtils.mulAndCheck(min / 2, 2));
341 testMulAndCheckLongFailure(max, 2L);
342 testMulAndCheckLongFailure(2L, max);
343 testMulAndCheckLongFailure(min, 2L);
344 testMulAndCheckLongFailure(2L, min);
345 testMulAndCheckLongFailure(min, -1L);
346 testMulAndCheckLongFailure(-1L, min);
347 }
348
349 @Test
350 void testSubAndCheck() {
351 int big = Integer.MAX_VALUE;
352 int bigNeg = Integer.MIN_VALUE;
353 assertEquals(big, ArithmeticUtils.subAndCheck(big, 0));
354 assertEquals(bigNeg + 1, ArithmeticUtils.subAndCheck(bigNeg, -1));
355 assertEquals(-1, ArithmeticUtils.subAndCheck(bigNeg, -big));
356 try {
357 ArithmeticUtils.subAndCheck(big, -1);
358 fail("Expecting MathRuntimeException");
359 } catch (MathRuntimeException ex) {
360 }
361 try {
362 ArithmeticUtils.subAndCheck(bigNeg, 1);
363 fail("Expecting MathRuntimeException");
364 } catch (MathRuntimeException ex) {
365 }
366 }
367
368 @Test
369 void testSubAndCheckErrorMessage() {
370 int big = Integer.MAX_VALUE;
371 try {
372 ArithmeticUtils.subAndCheck(big, -1);
373 fail("Expecting MathRuntimeException");
374 } catch (MathRuntimeException ex) {
375 assertTrue(ex.getMessage().length() > 1);
376 }
377 }
378
379 @Test
380 void testSubAndCheckLong() {
381 long max = Long.MAX_VALUE;
382 long min = Long.MIN_VALUE;
383 assertEquals(max, ArithmeticUtils.subAndCheck(max, 0));
384 assertEquals(min, ArithmeticUtils.subAndCheck(min, 0));
385 assertEquals(-max, ArithmeticUtils.subAndCheck(0, max));
386 assertEquals(min + 1, ArithmeticUtils.subAndCheck(min, -1));
387
388 assertEquals(-1, ArithmeticUtils.subAndCheck(-max - 1, -max));
389 assertEquals(max, ArithmeticUtils.subAndCheck(-1, -1 - max));
390 testSubAndCheckLongFailure(0L, min);
391 testSubAndCheckLongFailure(max, -1L);
392 testSubAndCheckLongFailure(min, 1L);
393 }
394
395 @Test
396 void testPow() {
397
398 assertEquals(1801088541, ArithmeticUtils.pow(21, 7));
399 assertEquals(1, ArithmeticUtils.pow(21, 0));
400 try {
401 ArithmeticUtils.pow(21, -7);
402 fail("Expecting MathIllegalArgumentException");
403 } catch (MathIllegalArgumentException e) {
404
405 }
406
407 assertEquals(1801088541, ArithmeticUtils.pow(21, 7));
408 assertEquals(1, ArithmeticUtils.pow(21, 0));
409 try {
410 ArithmeticUtils.pow(21, -7);
411 fail("Expecting MathIllegalArgumentException");
412 } catch (MathIllegalArgumentException e) {
413
414 }
415
416 assertEquals(1801088541L, ArithmeticUtils.pow(21L, 7));
417 assertEquals(1L, ArithmeticUtils.pow(21L, 0));
418 try {
419 ArithmeticUtils.pow(21L, -7);
420 fail("Expecting MathIllegalArgumentException");
421 } catch (MathIllegalArgumentException e) {
422
423 }
424
425 BigInteger twentyOne = BigInteger.valueOf(21L);
426 assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne, 7));
427 assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0));
428 try {
429 ArithmeticUtils.pow(twentyOne, -7);
430 fail("Expecting MathIllegalArgumentException");
431 } catch (MathIllegalArgumentException e) {
432
433 }
434
435 assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne,
436 7L));
437 assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, 0L));
438 try {
439 ArithmeticUtils.pow(twentyOne, -7L);
440 fail("Expecting MathIllegalArgumentException");
441 } catch (MathIllegalArgumentException e) {
442
443 }
444
445 assertEquals(BigInteger.valueOf(1801088541L), ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(
446 7L)));
447 assertEquals(BigInteger.ONE, ArithmeticUtils.pow(twentyOne, BigInteger.ZERO));
448 try {
449 ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(-7L));
450 fail("Expecting MathIllegalArgumentException");
451 } catch (MathIllegalArgumentException e) {
452
453 }
454
455 BigInteger bigOne =
456 new BigInteger("1543786922199448028351389769265814882661837148" +
457 "4763915343722775611762713982220306372888519211" +
458 "560905579993523402015636025177602059044911261");
459 assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103));
460 assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, 103L));
461 assertEquals(bigOne, ArithmeticUtils.pow(twentyOne, BigInteger.valueOf(
462 103L)));
463
464 }
465
466 @Test
467 void testPowIntOverflow() {
468 assertThrows(MathRuntimeException.class, () -> ArithmeticUtils.pow(21, 8));
469 }
470
471 @Test
472 void testPowInt() {
473 final int base = 21;
474
475 assertEquals(85766121L,
476 ArithmeticUtils.pow(base, 6));
477 assertEquals(1801088541L,
478 ArithmeticUtils.pow(base, 7));
479 }
480
481 @Test
482 void testPowNegativeIntOverflow() {
483 assertThrows(MathRuntimeException.class, () -> ArithmeticUtils.pow(-21, 8));
484 }
485
486 @Test
487 void testPowNegativeInt() {
488 final int base = -21;
489
490 assertEquals(85766121,
491 ArithmeticUtils.pow(base, 6));
492 assertEquals(-1801088541,
493 ArithmeticUtils.pow(base, 7));
494 }
495
496 @Test
497 void testPowMinusOneInt() {
498 final int base = -1;
499 for (int i = 0; i < 100; i++) {
500 final int pow = ArithmeticUtils.pow(base, i);
501 assertEquals(i % 2 == 0 ? 1 : -1, pow, "i: " + i);
502 }
503 }
504
505 @Test
506 void testPowOneInt() {
507 final int base = 1;
508 for (int i = 0; i < 100; i++) {
509 final int pow = ArithmeticUtils.pow(base, i);
510 assertEquals(1, pow, "i: " + i);
511 }
512 }
513
514 @Test
515 void testPowLongOverflow() {
516 assertThrows(MathRuntimeException.class, () -> ArithmeticUtils.pow(21, 15));
517 }
518
519 @Test
520 void testPowLong() {
521 final long base = 21;
522
523 assertEquals(154472377739119461L,
524 ArithmeticUtils.pow(base, 13));
525 assertEquals(3243919932521508681L,
526 ArithmeticUtils.pow(base, 14));
527 }
528
529 @Test
530 void testPowNegativeLongOverflow() {
531 assertThrows(MathRuntimeException.class, () -> ArithmeticUtils.pow(-21L, 15));
532 }
533
534 @Test
535 void testPowNegativeLong() {
536 final long base = -21;
537
538 assertEquals(-154472377739119461L,
539 ArithmeticUtils.pow(base, 13));
540 assertEquals(3243919932521508681L,
541 ArithmeticUtils.pow(base, 14));
542 }
543
544 @Test
545 void testPowMinusOneLong() {
546 final long base = -1;
547 for (int i = 0; i < 100; i++) {
548 final long pow = ArithmeticUtils.pow(base, i);
549 assertEquals(i % 2 == 0 ? 1 : -1, pow, "i: " + i);
550 }
551 }
552
553 @Test
554 void testPowOneLong() {
555 final long base = 1;
556 for (int i = 0; i < 100; i++) {
557 final long pow = ArithmeticUtils.pow(base, i);
558 assertEquals(1, pow, "i: " + i);
559 }
560 }
561
562 @Test
563 void testIsPowerOfTwo() {
564 final int n = 1025;
565 final boolean[] expected = new boolean[n];
566 Arrays.fill(expected, false);
567 for (int i = 1; i < expected.length; i *= 2) {
568 expected[i] = true;
569 }
570 for (int i = 0; i < expected.length; i++) {
571 final boolean actual = ArithmeticUtils.isPowerOfTwo(i);
572 assertEquals(actual, expected[i], Integer.toString(i));
573 }
574 }
575
576 private void testAddAndCheckLongFailure(long a, long b) {
577 try {
578 ArithmeticUtils.addAndCheck(a, b);
579 fail("Expecting MathRuntimeException");
580 } catch (MathRuntimeException ex) {
581
582 }
583 }
584
585 private void testMulAndCheckLongFailure(long a, long b) {
586 try {
587 ArithmeticUtils.mulAndCheck(a, b);
588 fail("Expecting MathRuntimeException");
589 } catch (MathRuntimeException ex) {
590
591 }
592 }
593
594 private void testSubAndCheckLongFailure(long a, long b) {
595 try {
596 ArithmeticUtils.subAndCheck(a, b);
597 fail("Expecting MathRuntimeException");
598 } catch (MathRuntimeException ex) {
599
600 }
601 }
602
603
604
605
606
607
608
609
610
611
612 private static int[] getIntSpecialCases() {
613 int[] ints = new int[100];
614 int i = 0;
615 ints[i++] = Integer.MAX_VALUE;
616 ints[i++] = Integer.MAX_VALUE - 1;
617 ints[i++] = 100;
618 ints[i++] = 101;
619 ints[i++] = 102;
620 ints[i++] = 300;
621 ints[i++] = 567;
622 for (int j = 0; j < 20; j++) {
623 ints[i++] = j;
624 }
625 for (int j = i - 1; j >= 0; j--) {
626 ints[i++] = ints[j] > 0 ? -ints[j] : Integer.MIN_VALUE;
627 }
628 java.util.Random r = new java.util.Random(System.nanoTime());
629 while (i < ints.length) {
630 ints[i++] = r.nextInt();
631 }
632 return ints;
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646 private static long[] getLongSpecialCases() {
647 long[] longs = new long[100];
648 int i = 0;
649 longs[i++] = Long.MAX_VALUE;
650 longs[i++] = Long.MAX_VALUE - 1L;
651 longs[i++] = (long) Integer.MAX_VALUE + 1L;
652 longs[i++] = Integer.MAX_VALUE;
653 longs[i++] = Integer.MAX_VALUE - 1;
654 longs[i++] = 100L;
655 longs[i++] = 101L;
656 longs[i++] = 102L;
657 longs[i++] = 300L;
658 longs[i++] = 567L;
659 for (int j = 0; j < 20; j++) {
660 longs[i++] = j;
661 }
662 for (int j = i - 1; j >= 0; j--) {
663 longs[i++] = longs[j] > 0L ? -longs[j] : Long.MIN_VALUE;
664 }
665 java.util.Random r = new java.util.Random(System.nanoTime());
666 while (i < longs.length) {
667 longs[i++] = r.nextLong();
668 }
669 return longs;
670 }
671
672 private static long toUnsignedLong(int number) {
673 return number < 0 ? 0x100000000L + (long)number : (long)number;
674 }
675
676 private static int remainderUnsignedExpected(int dividend, int divisor) {
677 return (int)remainderUnsignedExpected(toUnsignedLong(dividend), toUnsignedLong(divisor));
678 }
679
680 private static int divideUnsignedExpected(int dividend, int divisor) {
681 return (int)divideUnsignedExpected(toUnsignedLong(dividend), toUnsignedLong(divisor));
682 }
683
684 private static BigInteger toUnsignedBigInteger(long number) {
685 return number < 0L ? BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(number)) : BigInteger.valueOf(number);
686 }
687
688 private static long remainderUnsignedExpected(long dividend, long divisor) {
689 return toUnsignedBigInteger(dividend).remainder(toUnsignedBigInteger(divisor)).longValue();
690 }
691
692 private static long divideUnsignedExpected(long dividend, long divisor) {
693 return toUnsignedBigInteger(dividend).divide(toUnsignedBigInteger(divisor)).longValue();
694 }
695
696 @Test
697 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
698 void testRemainderUnsignedInt() {
699 assertEquals(36, ArithmeticUtils.remainderUnsigned(-2147479015, 63));
700 assertEquals(6, ArithmeticUtils.remainderUnsigned(-2147479015, 25));
701 }
702
703 @Test
704 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
705 void testRemainderUnsignedIntSpecialCases() {
706 int[] ints = getIntSpecialCases();
707 for (int dividend : ints) {
708 for (int divisor : ints) {
709 if (divisor == 0) {
710 try {
711 ArithmeticUtils.remainderUnsigned(dividend, divisor);
712 fail("Should have failed with ArithmeticException: division by zero");
713 } catch (ArithmeticException e) {
714
715 }
716 } else {
717 assertEquals(remainderUnsignedExpected(dividend, divisor), ArithmeticUtils.remainderUnsigned(dividend, divisor));
718 }
719 }
720 }
721 }
722
723 @Test
724 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
725 void testRemainderUnsignedLong() {
726 assertEquals(48L, ArithmeticUtils.remainderUnsigned(-2147479015L, 63L));
727 }
728
729
730 @Test
731 void testRemainderUnsignedLongSpecialCases() {
732 long[] longs = getLongSpecialCases();
733 for (long dividend : longs) {
734 for (long divisor : longs) {
735 if (divisor == 0L) {
736 try {
737 ArithmeticUtils.remainderUnsigned(dividend, divisor);
738 fail("Should have failed with ArithmeticException: division by zero");
739 } catch (ArithmeticException e) {
740
741 }
742 } else {
743 assertEquals(remainderUnsignedExpected(dividend, divisor), ArithmeticUtils.remainderUnsigned(dividend, divisor));
744 }
745 }
746 }
747 }
748
749 @Test
750 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
751 void testDivideUnsignedInt() {
752 assertEquals(34087115, ArithmeticUtils.divideUnsigned(-2147479015, 63));
753 assertEquals(85899531, ArithmeticUtils.divideUnsigned(-2147479015, 25));
754 assertEquals(2147483646, ArithmeticUtils.divideUnsigned(-3, 2));
755 assertEquals(330382098, ArithmeticUtils.divideUnsigned(-16, 13));
756 assertEquals(306783377, ArithmeticUtils.divideUnsigned(-16, 14));
757 assertEquals(2, ArithmeticUtils.divideUnsigned(-1, 2147483647));
758 assertEquals(2, ArithmeticUtils.divideUnsigned(-2, 2147483647));
759 assertEquals(1, ArithmeticUtils.divideUnsigned(-3, 2147483647));
760 assertEquals(1, ArithmeticUtils.divideUnsigned(-16, 2147483647));
761 assertEquals(1, ArithmeticUtils.divideUnsigned(-16, 2147483646));
762 }
763
764 @Test
765 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
766 void testDivideUnsignedIntSpecialCases() {
767 int[] ints = getIntSpecialCases();
768 for (int dividend : ints) {
769 for (int divisor : ints) {
770 if (divisor == 0) {
771 try {
772 ArithmeticUtils.divideUnsigned(dividend, divisor);
773 fail("Should have failed with ArithmeticException: division by zero");
774 } catch (ArithmeticException e) {
775
776 }
777 } else {
778 assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor));
779 }
780 }
781 }
782 }
783
784 @Test
785 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
786 void testDivideUnsignedLong() {
787 assertEquals(292805461453366231L, ArithmeticUtils.divideUnsigned(-2147479015L, 63L));
788 }
789
790 @Test
791 @Timeout(value = 5000L, unit = TimeUnit.MILLISECONDS)
792 void testDivideUnsignedLongSpecialCases() {
793 long[] longs = getLongSpecialCases();
794 for (long dividend : longs) {
795 for (long divisor : longs) {
796 if (divisor == 0L) {
797 try {
798 ArithmeticUtils.divideUnsigned(dividend, divisor);
799 fail("Should have failed with ArithmeticException: division by zero");
800 } catch (ArithmeticException e) {
801
802 }
803 } else {
804 assertEquals(divideUnsignedExpected(dividend, divisor), ArithmeticUtils.divideUnsigned(dividend, divisor));
805 }
806 }
807 }
808 }
809 }