Skip to content

Commit c00a4c0

Browse files
committed
LibraryTest: Added 5 test cases based on random value generation and oracle testing.
1 parent 5c092c4 commit c00a4c0

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

java/LibraryTest.java

+80
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import static org.junit.Assert.assertTrue;
1313
import java.math.BigInteger;
1414
import java.util.Arrays;
15+
import java.util.Random;
1516
import org.junit.Assert;
1617
import org.junit.Test;
1718

@@ -83,6 +84,17 @@ public final class LibraryTest {
8384
}
8485

8586

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+
8698
@Test(expected=IllegalArgumentException.class)
8799
public void testSqrtIntInvalid0() {
88100
Library.sqrt(-1);
@@ -120,6 +132,18 @@ public void testSqrtIntInvalid2() {
120132
}
121133

122134

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+
123147
@Test(expected=IllegalArgumentException.class)
124148
public void testSqrtLongInvalid0() {
125149
Library.sqrt(-1);
@@ -249,6 +273,28 @@ public void testSqrtLongInvalid2() {
249273
}
250274

251275

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+
252298
@Test public void testFactorial() {
253299
assertEquals(new BigInteger("1"), Library.factorial(0));
254300
assertEquals(new BigInteger("1"), Library.factorial(1));
@@ -317,6 +363,22 @@ public void testFactorialInvalid2() {
317363
}
318364

319365

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+
320382
@Test public void testIsPrime() {
321383
assertFalse(Library.isPrime(0));
322384
assertFalse(Library.isPrime(1));
@@ -416,6 +478,20 @@ public void testListPrimesInvalid1() {
416478
}
417479

418480

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+
419495
@Test(expected=IllegalArgumentException.class)
420496
public void testTotientInvalid0() {
421497
Library.totient(-1);
@@ -470,4 +546,8 @@ public void testListTotientsInvalid1() {
470546
assertFalse(Library.nextPermutation(arr)); assertArrayEquals(new int[]{9, 5, 3, 2, 1}, arr);
471547
}
472548

549+
550+
551+
private static Random rand = new Random();
552+
473553
}

0 commit comments

Comments
 (0)