Skip to content

Commit db6d9c8

Browse files
committed
abstract test infrastructure
1 parent c1166ad commit db6d9c8

File tree

4 files changed

+81
-85
lines changed

4 files changed

+81
-85
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
4+
*
5+
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9+
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10+
*
11+
* You should have received a copy of the GNU General Public License along with this program;
12+
* if not, see <http://www.gnu.org/licenses/>.
13+
*/
14+
package de.tilman_neumann.jml.factor;
15+
16+
import static de.tilman_neumann.jml.base.BigIntConstants.I_1;
17+
18+
import java.math.BigInteger;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import de.tilman_neumann.jml.primes.probable.BPSWTest;
23+
import de.tilman_neumann.util.SortedMultiset;
24+
25+
/**
26+
* Infrastructure to simplify unit tests of factoring algorithms.
27+
*
28+
* @author Tilman Neumann
29+
*/
30+
public class FactorTestInfrastructure {
31+
32+
private static BPSWTest bpsw = new BPSWTest();
33+
34+
/**
35+
* Test factorization of all small composites <= nMax by the given factorAlgorithm.<br/><br/>
36+
*
37+
* This test could be made more efficient using a composites sieve.
38+
*
39+
* @param nMax
40+
* @param factorAlgorithm
41+
* @return list of the factor arguments that were not factored correctly
42+
*/
43+
public static List<Integer> testSmallComposites(int nMax, FactorAlgorithm factorAlgorithm) {
44+
ArrayList<Integer> fails = new ArrayList<>();
45+
for (int n=4; n<=nMax; n++) {
46+
if (bpsw.isProbablePrime(n)) continue; // skip primes
47+
BigInteger nBig = BigInteger.valueOf(n);
48+
SortedMultiset<BigInteger> factors = factorAlgorithm.factor(nBig);
49+
boolean isFail = false;
50+
BigInteger testProd = I_1;
51+
for (BigInteger factor : factors.keySet()) {
52+
if (!bpsw.isProbablePrime(factor)) {
53+
isFail = true;
54+
break;
55+
}
56+
int exp = factors.get(factor).intValue();
57+
BigInteger pow = factor.pow(exp);
58+
testProd = testProd.multiply(pow);
59+
}
60+
if (!testProd.equals(nBig)) {
61+
isFail = true;
62+
}
63+
if (isFail) {
64+
fails.add(n);
65+
}
66+
}
67+
return fails;
68+
}
69+
}

src/test/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MHInlinedTest.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,26 +14,24 @@
1414
package de.tilman_neumann.jml.factor.ecm;
1515

1616
import java.math.BigInteger;
17-
import java.util.ArrayList;
17+
import java.util.List;
1818

1919
import org.apache.logging.log4j.LogManager;
2020
import org.apache.logging.log4j.Logger;
2121
import org.junit.BeforeClass;
2222
import org.junit.Test;
2323

24-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
24+
import de.tilman_neumann.jml.factor.FactorTestInfrastructure;
2525
import de.tilman_neumann.util.ConfigUtil;
2626
import de.tilman_neumann.util.SortedMultiset;
2727

28-
import static de.tilman_neumann.jml.base.BigIntConstants.I_1;
2928
import static org.junit.Assert.assertEquals;
3029
import static org.junit.Assert.assertNotEquals;
3130

3231
public class TinyEcm64MHInlinedTest {
3332
private static final Logger LOG = LogManager.getLogger(TinyEcm64MHInlinedTest.class);
3433

3534
private static TinyEcm64MHInlined tinyEcm = new TinyEcm64MHInlined();
36-
private static BPSWTest bpsw = new BPSWTest();
3735

3836
@BeforeClass
3937
public static void setup() {
@@ -42,29 +40,7 @@ public static void setup() {
4240

4341
@Test
4442
public void testSmallestComposites() {
45-
ArrayList<Integer> fails = new ArrayList<>();
46-
for (int n=4; n<100000; n++) {
47-
if (bpsw.isProbablePrime(n)) continue; // skip primes
48-
BigInteger nBig = BigInteger.valueOf(n);
49-
SortedMultiset<BigInteger> factors = tinyEcm.factor(nBig);
50-
boolean isFail = false;
51-
BigInteger testProd = I_1;
52-
for (BigInteger factor : factors.keySet()) {
53-
if (!bpsw.isProbablePrime(factor)) {
54-
isFail = true;
55-
break;
56-
}
57-
int exp = factors.get(factor).intValue();
58-
BigInteger pow = factor.pow(exp);
59-
testProd = testProd.multiply(pow);
60-
}
61-
if (!testProd.equals(nBig)) {
62-
isFail = true;
63-
}
64-
if (isFail) {
65-
fails.add(n);
66-
}
67-
}
43+
List<Integer> fails = FactorTestInfrastructure.testSmallComposites(100000, tinyEcm);
6844
assertEquals("Failed to factor n = " + fails, 0, fails.size());
6945
}
7046

src/test/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64MHTest.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,26 +14,24 @@
1414
package de.tilman_neumann.jml.factor.ecm;
1515

1616
import java.math.BigInteger;
17-
import java.util.ArrayList;
17+
import java.util.List;
1818

1919
import org.apache.logging.log4j.LogManager;
2020
import org.apache.logging.log4j.Logger;
2121
import org.junit.BeforeClass;
2222
import org.junit.Test;
2323

24-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
24+
import de.tilman_neumann.jml.factor.FactorTestInfrastructure;
2525
import de.tilman_neumann.util.ConfigUtil;
2626
import de.tilman_neumann.util.SortedMultiset;
2727

28-
import static de.tilman_neumann.jml.base.BigIntConstants.I_1;
2928
import static org.junit.Assert.assertEquals;
3029
import static org.junit.Assert.assertNotEquals;
3130

3231
public class TinyEcm64MHTest {
3332
private static final Logger LOG = LogManager.getLogger(TinyEcm64MHTest.class);
3433

3534
private static TinyEcm64MH tinyEcm = new TinyEcm64MH();
36-
private static BPSWTest bpsw = new BPSWTest();
3735

3836
@BeforeClass
3937
public static void setup() {
@@ -42,29 +40,7 @@ public static void setup() {
4240

4341
@Test
4442
public void testSmallestComposites() {
45-
ArrayList<Integer> fails = new ArrayList<>();
46-
for (int n=4; n<100000; n++) {
47-
if (bpsw.isProbablePrime(n)) continue; // skip primes
48-
BigInteger nBig = BigInteger.valueOf(n);
49-
SortedMultiset<BigInteger> factors = tinyEcm.factor(nBig);
50-
boolean isFail = false;
51-
BigInteger testProd = I_1;
52-
for (BigInteger factor : factors.keySet()) {
53-
if (!bpsw.isProbablePrime(factor)) {
54-
isFail = true;
55-
break;
56-
}
57-
int exp = factors.get(factor).intValue();
58-
BigInteger pow = factor.pow(exp);
59-
testProd = testProd.multiply(pow);
60-
}
61-
if (!testProd.equals(nBig)) {
62-
isFail = true;
63-
}
64-
if (isFail) {
65-
fails.add(n);
66-
}
67-
}
43+
List<Integer> fails = FactorTestInfrastructure.testSmallComposites(100000, tinyEcm);
6844
assertEquals("Failed to factor n = " + fails, 0, fails.size());
6945
}
7046

src/test/java/de/tilman_neumann/jml/factor/ecm/TinyEcm64Test.java

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3-
* Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+
* Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
44
*
55
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
@@ -14,27 +14,24 @@
1414
package de.tilman_neumann.jml.factor.ecm;
1515

1616
import java.math.BigInteger;
17-
import java.util.ArrayList;
17+
import java.util.List;
1818

1919
import org.apache.logging.log4j.LogManager;
2020
import org.apache.logging.log4j.Logger;
2121
import org.junit.BeforeClass;
2222
import org.junit.Test;
2323

24-
import de.tilman_neumann.jml.primes.probable.BPSWTest;
24+
import de.tilman_neumann.jml.factor.FactorTestInfrastructure;
2525
import de.tilman_neumann.util.ConfigUtil;
2626
import de.tilman_neumann.util.SortedMultiset;
2727

2828
import static org.junit.Assert.assertEquals;
2929
import static org.junit.Assert.assertNotEquals;
3030

31-
import static de.tilman_neumann.jml.base.BigIntConstants.*;
32-
3331
public class TinyEcm64Test {
3432
private static final Logger LOG = LogManager.getLogger(TinyEcm64Test.class);
3533

3634
private static TinyEcm64 tinyEcm = new TinyEcm64();
37-
private static BPSWTest bpsw = new BPSWTest();
3835

3936
@BeforeClass
4037
public static void setup() {
@@ -43,29 +40,7 @@ public static void setup() {
4340

4441
@Test
4542
public void testSmallestComposites() {
46-
ArrayList<Integer> fails = new ArrayList<>();
47-
for (int n=4; n<100000; n++) {
48-
if (bpsw.isProbablePrime(n)) continue; // skip primes
49-
BigInteger nBig = BigInteger.valueOf(n);
50-
SortedMultiset<BigInteger> factors = tinyEcm.factor(nBig);
51-
boolean isFail = false;
52-
BigInteger testProd = I_1;
53-
for (BigInteger factor : factors.keySet()) {
54-
if (!bpsw.isProbablePrime(factor)) {
55-
isFail = true;
56-
break;
57-
}
58-
int exp = factors.get(factor).intValue();
59-
BigInteger pow = factor.pow(exp);
60-
testProd = testProd.multiply(pow);
61-
}
62-
if (!testProd.equals(nBig)) {
63-
isFail = true;
64-
}
65-
if (isFail) {
66-
fails.add(n);
67-
}
68-
}
43+
List<Integer> fails = FactorTestInfrastructure.testSmallComposites(100000, tinyEcm);
6944
assertEquals("Failed to factor n = " + fails, 0, fails.size());
7045
}
7146

0 commit comments

Comments
 (0)