-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPrimeGensTest.java
72 lines (59 loc) · 2.46 KB
/
PrimeGensTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import org.junit.Test;
import java.util.Iterator;
import java.util.zip.CRC32;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class PrimeGensTest {
// Expected prefixes of the infinite sequences of twin primes, safe primes and strong primes.
private static final int[] TWIN_PRIMES_PREFIX = {
3, 5, 11, 17, 29, 41, 59, 71, 101, 107, 137, 149, 179, 191, 197, 227, 239, 269, 281, 311
};
private static final int[] SAFE_PRIMES_PREFIX = {
5, 7, 11, 23, 47, 59, 83, 107, 167, 179, 227, 263, 347, 359, 383, 467, 479, 503, 563, 587, 719, 839, 863
};
private static final int[] STRONG_PRIMES_PREFIX = {
11, 17, 29, 37, 41, 59, 67, 71, 79, 97, 101, 107, 127, 137, 149, 163, 179, 191, 197, 223, 227, 239, 251
};
@Test public void testTwinPrimes() {
Iterator<Integer> it = new PrimeGens.TwinPrimes();
int[] result = new int[TWIN_PRIMES_PREFIX.length];
for(int i = 0; i < TWIN_PRIMES_PREFIX.length; i++) {
result[i] = it.next();
}
assertArrayEquals(TWIN_PRIMES_PREFIX, result);
CRC32 check = new CRC32();
it = new PrimeGens.TwinPrimes();
for(int i = 0; i < 3500; i++) {
check.update(it.next());
}
assertEquals(2941193748L, check.getValue());
}
@Test public void testSafePrimes() {
Iterator<Integer> it = new PrimeGens.SafePrimes();
int[] result = new int[SAFE_PRIMES_PREFIX.length];
for(int i = 0; i < SAFE_PRIMES_PREFIX.length; i++) {
result[i] = it.next();
}
assertArrayEquals(SAFE_PRIMES_PREFIX, result);
CRC32 check = new CRC32();
it = new PrimeGens.SafePrimes();
for(int i = 0; i < 3000; i++) {
check.update(it.next());
}
assertEquals(3874618335L, check.getValue());
}
@Test public void testStrongPrimes() {
Iterator<Integer> it = new PrimeGens.StrongPrimes();
int[] result = new int[STRONG_PRIMES_PREFIX.length];
for(int i = 0; i < STRONG_PRIMES_PREFIX.length; i++) {
result[i] = it.next();
}
assertArrayEquals(STRONG_PRIMES_PREFIX, result);
CRC32 check = new CRC32();
it = new PrimeGens.StrongPrimes();
for(int i = 0; i < 15_000; i++) {
check.update(it.next());
}
assertEquals(494629196L, check.getValue());
}
}