Skip to content

Commit e9cb170

Browse files
committed
small optimizations of prime tests
1 parent 61bf8f6 commit e9cb170

4 files changed

Lines changed: 62 additions & 58 deletions

File tree

src/main/java/de/tilman_neumann/jml/primes/probable/BPSWTest.java

Lines changed: 21 additions & 10 deletions
Large diffs are not rendered by default.

src/main/java/de/tilman_neumann/jml/primes/probable/PrPTest.java

Lines changed: 9 additions & 45 deletions
Large diffs are not rendered by default.

src/test/java/de/tilman_neumann/jml/primes/probable/NextProbablePrimePerformanceTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public class NextProbablePrimePerformanceTest {
3434
private static final Random RNG = new Random();
3535

3636
/** Number of test numbers for the performance test. */
37-
private static final int NCOUNT = 1000;
37+
private static final int NCOUNT = 10000;
3838

3939
private static final BPSWTest bpsw = new BPSWTest();
40+
private static final PrPTest prp = new PrPTest();
4041

4142
/**
4243
* Performance test.
@@ -64,6 +65,14 @@ private static void testPerformance() {
6465
duration = System.currentTimeMillis() - startMillis;
6566
addToMap(duration_2_algLists, duration, "BPSW");
6667

68+
// test PrP
69+
startMillis = System.currentTimeMillis();
70+
for (BigInteger n : testSet) {
71+
prp.nextProbablePrime(n);
72+
}
73+
duration = System.currentTimeMillis() - startMillis;
74+
addToMap(duration_2_algLists, duration, "PrP");
75+
6776
// test built-in method
6877
startMillis = System.currentTimeMillis();
6978
for (BigInteger n : testSet) {

src/test/java/de/tilman_neumann/jml/primes/probable/NextProbablePrimeTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,17 @@ public class NextProbablePrimeTest {
3939
private static final int MAX_BITS = 150;
4040

4141
private static final BPSWTest bpsw = new BPSWTest();
42+
private static final PrPTest prp = new PrPTest();
4243

4344
@BeforeClass
4445
public static void setup() {
4546
ConfigUtil.initProject();
4647
}
4748

4849
@Test
49-
public void testNextProbablePrime() {
50+
public void testNextProbablePrimeBPSW() {
5051
for (int nBits = 20; nBits<=MAX_BITS; nBits+=10) {
51-
LOG.info("Test correctness of " + NCOUNT + " N with " + nBits + " bits:");
52+
LOG.info("Test correctness of " + NCOUNT + " N with " + nBits + " bits using BPSW:");
5253
int i = 0;
5354
while (i < NCOUNT) {
5455
BigInteger n = new BigInteger(nBits, RNG);
@@ -63,4 +64,23 @@ public void testNextProbablePrime() {
6364
LOG.info(" Tested " + NCOUNT + " next probable primes...");
6465
}
6566
}
67+
68+
@Test
69+
public void testNextProbablePrimePrP() {
70+
for (int nBits = 20; nBits<=MAX_BITS; nBits+=10) {
71+
LOG.info("Test correctness of " + NCOUNT + " N with " + nBits + " bits using PrP:");
72+
int i = 0;
73+
while (i < NCOUNT) {
74+
BigInteger n = new BigInteger(nBits, RNG);
75+
if (n.equals(I_0)) continue; // exclude 0 from test set
76+
77+
BigInteger correctValue = n.nextProbablePrime();
78+
BigInteger bpswValue = prp.nextProbablePrime(n);
79+
assertEquals(correctValue, bpswValue);
80+
81+
i++;
82+
}
83+
LOG.info(" Tested " + NCOUNT + " next probable primes...");
84+
}
85+
}
6686
}

0 commit comments

Comments
 (0)