|
12 | 12 | import static org.junit.Assert.assertTrue;
|
13 | 13 | import java.math.BigInteger;
|
14 | 14 | import java.util.Arrays;
|
| 15 | +import java.util.Random; |
15 | 16 | import org.junit.Assert;
|
16 | 17 | import org.junit.Test;
|
17 | 18 |
|
@@ -83,6 +84,17 @@ public final class LibraryTest {
|
83 | 84 | }
|
84 | 85 |
|
85 | 86 |
|
| 87 | + @Test public void testSqrtIntRandomly() { |
| 88 | + final int trials = 1000000; |
| 89 | + for (int i = 0; i < trials; i++) { |
| 90 | + int x = rand.nextInt() >>> 1; // uint31 |
| 91 | + int y = Library.sqrt(x); |
| 92 | + assertTrue(0 <= y && y <= x); |
| 93 | + assertTrue((long)y * y <= x && x < (y + 1L) * (y + 1L)); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + |
86 | 98 | @Test(expected=IllegalArgumentException.class)
|
87 | 99 | public void testSqrtIntInvalid0() {
|
88 | 100 | Library.sqrt(-1);
|
@@ -120,6 +132,18 @@ public void testSqrtIntInvalid2() {
|
120 | 132 | }
|
121 | 133 |
|
122 | 134 |
|
| 135 | + @Test public void testSqrtLongRandomly() { |
| 136 | + final int trials = 1000000; |
| 137 | + for (int i = 0; i < trials; i++) { |
| 138 | + long x = rand.nextLong() >>> 1; // uint63 |
| 139 | + long y = Library.sqrt(x); |
| 140 | + assertTrue(0 <= y && y <= x); |
| 141 | + if (x > 0) |
| 142 | + assertTrue(y <= x / y && x / (y + 1) < y + 1); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + |
123 | 147 | @Test(expected=IllegalArgumentException.class)
|
124 | 148 | public void testSqrtLongInvalid0() {
|
125 | 149 | Library.sqrt(-1);
|
@@ -249,6 +273,28 @@ public void testSqrtLongInvalid2() {
|
249 | 273 | }
|
250 | 274 |
|
251 | 275 |
|
| 276 | + @Test public void testReciprocalModRandomly() { |
| 277 | + final int trials = 100000; |
| 278 | + for (int i = 0; i < trials; i++) { |
| 279 | + int mod = rand.nextInt() >>> 1; // uint31 |
| 280 | + if (mod < 2) |
| 281 | + continue; |
| 282 | + int x = rand.nextInt(mod); |
| 283 | + if (Library.gcd(x, mod) == 1) { |
| 284 | + int y = Library.reciprocalMod(x, mod); |
| 285 | + assertTrue(1 <= y && y < mod); |
| 286 | + assertEquals(1, (long)x * y % mod); |
| 287 | + assertEquals(x, Library.reciprocalMod(y, mod)); |
| 288 | + } else { |
| 289 | + try { |
| 290 | + Library.reciprocalMod(x, mod); |
| 291 | + Assert.fail(); |
| 292 | + } catch (IllegalArgumentException e) {} // Pass |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + |
| 297 | + |
252 | 298 | @Test public void testFactorial() {
|
253 | 299 | assertEquals(new BigInteger("1"), Library.factorial(0));
|
254 | 300 | assertEquals(new BigInteger("1"), Library.factorial(1));
|
@@ -317,6 +363,22 @@ public void testFactorialInvalid2() {
|
317 | 363 | }
|
318 | 364 |
|
319 | 365 |
|
| 366 | + @Test public void testGcdRandomly() { |
| 367 | + final int trials = 1000000; |
| 368 | + for (int i = 0; i < trials; i++) { |
| 369 | + int x = rand.nextInt() >>> 1; // uint31 |
| 370 | + int y = rand.nextInt() >>> 1; // uint31 |
| 371 | + int z = Library.gcd(x, y); |
| 372 | + if (x == 0) |
| 373 | + assertEquals(y, z); |
| 374 | + else if (y == 0) |
| 375 | + assertEquals(x, z); |
| 376 | + else // x, y > 0 |
| 377 | + assertTrue(0 < z && z <= x && z <= y && x % z == 0 && y % z == 0); |
| 378 | + } |
| 379 | + } |
| 380 | + |
| 381 | + |
320 | 382 | @Test public void testIsPrime() {
|
321 | 383 | assertFalse(Library.isPrime(0));
|
322 | 384 | assertFalse(Library.isPrime(1));
|
@@ -416,6 +478,20 @@ public void testListPrimesInvalid1() {
|
416 | 478 | }
|
417 | 479 |
|
418 | 480 |
|
| 481 | + @Test public void testTotientRandomly() { |
| 482 | + final int trials = 100; |
| 483 | + for (int i = 0; i < trials; i++) { |
| 484 | + int n = rand.nextInt(100000) + 1; |
| 485 | + int tot = 0; |
| 486 | + for (int j = 1; j <= n; j++) { |
| 487 | + if (Library.gcd(j, n) == 1) |
| 488 | + tot++; |
| 489 | + } |
| 490 | + assertEquals(tot, Library.totient(n)); |
| 491 | + } |
| 492 | + } |
| 493 | + |
| 494 | + |
419 | 495 | @Test(expected=IllegalArgumentException.class)
|
420 | 496 | public void testTotientInvalid0() {
|
421 | 497 | Library.totient(-1);
|
@@ -470,4 +546,8 @@ public void testListTotientsInvalid1() {
|
470 | 546 | assertFalse(Library.nextPermutation(arr)); assertArrayEquals(new int[]{9, 5, 3, 2, 1}, arr);
|
471 | 547 | }
|
472 | 548 |
|
| 549 | + |
| 550 | + |
| 551 | + private static Random rand = new Random(); |
| 552 | + |
473 | 553 | }
|
0 commit comments