Skip to content

Commit 129f27c

Browse files
committed
split main() method into performance and unit tests
1 parent 51afa88 commit 129f27c

File tree

2 files changed

+114
-24
lines changed

2 files changed

+114
-24
lines changed

src/main/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNTest02.java src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNPerformanceTest.java

+5-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package de.tilman_neumann.jml.quadraticResidues;
1515

1616
import java.math.BigInteger;
17-
import java.util.ArrayList;
1817
import java.util.Arrays;
1918
import java.util.List;
2019

@@ -24,14 +23,14 @@
2423
import de.tilman_neumann.util.ConfigUtil;
2524

2625
/**
27-
* Tests of quadratic residue computations modulo 2^n.
26+
* Performance tests of quadratic residue computations modulo 2^n.
2827
*
2928
* @author Tilman Neumann
3029
*/
31-
public class QuadraticResiduesMod2PowNTest02 {
30+
public class QuadraticResiduesMod2PowNPerformanceTest {
31+
32+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNPerformanceTest.class);
3233

33-
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest02.class);
34-
3534
private static final boolean SHOW_ELEMENTS = false;
3635

3736
/**
@@ -40,53 +39,35 @@ public class QuadraticResiduesMod2PowNTest02 {
4039
*/
4140
public static void main(String[] args) {
4241
ConfigUtil.initProject();
43-
44-
ArrayList<Integer> counts_v0 = new ArrayList<Integer>();
45-
ArrayList<Integer> counts_v1 = new ArrayList<Integer>();
46-
ArrayList<Integer> counts_v2 = new ArrayList<Integer>();
47-
ArrayList<Integer> counts_v3 = new ArrayList<Integer>();
48-
ArrayList<Integer> counts_v4 = new ArrayList<Integer>();
4942

50-
for (int n=0; n<30; n++) {
43+
for (int n=0; ; n++) {
5144
long t0, t1;
5245

5346
t0 = System.currentTimeMillis();
5447
List<BigInteger> quadraticResiduesMod2PowN_v0 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
5548
t1 = System.currentTimeMillis();
5649
LOG.info("v0: n = " + n + " has " + quadraticResiduesMod2PowN_v0.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v0 : "") + " -- duration = " + (t1-t0) + "ms");
57-
counts_v0.add(quadraticResiduesMod2PowN_v0.size());
5850

5951
t0 = System.currentTimeMillis();
6052
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
6153
t1 = System.currentTimeMillis();
6254
LOG.info("v1: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : "") + " -- duration = " + (t1-t0) + "ms");
63-
counts_v1.add(quadraticResiduesMod2PowN_v1.size());
6455

6556
t0 = System.currentTimeMillis();
6657
List<Long> quadraticResiduesMod2PowN_v2 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
6758
t1 = System.currentTimeMillis();
6859
LOG.info("v2: n = " + n + " has " + quadraticResiduesMod2PowN_v2.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v2 : "") + " -- duration = " + (t1-t0) + "ms");
69-
counts_v2.add(quadraticResiduesMod2PowN_v2.size());
7060

7161
t0 = System.currentTimeMillis();
7262
List<Long> quadraticResiduesMod2PowN_v3 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
7363
t1 = System.currentTimeMillis();
7464
LOG.info("v3: n = " + n + " has " + quadraticResiduesMod2PowN_v3.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v3 : "") + " -- duration = " + (t1-t0) + "ms");
75-
counts_v3.add(quadraticResiduesMod2PowN_v3.size());
7665

7766
t0 = System.currentTimeMillis();
7867
long[] array = new long[((1<<n) / 6) + 6];
7968
int count = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n, array);
8069
t1 = System.currentTimeMillis();
8170
LOG.info("v4: n = " + n + " has " + count + " quadratic residues" + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : "") + " -- duration = " + (t1-t0) + "ms");
82-
counts_v4.add(count);
8371
}
84-
85-
// A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, 43692, 87383, ...
86-
LOG.info("v0 counts = " + counts_v0);
87-
LOG.info("v1 counts = " + counts_v1);
88-
LOG.info("v2 counts = " + counts_v2);
89-
LOG.info("v3 counts = " + counts_v3);
90-
LOG.info("v4 counts = " + counts_v4);
9172
}
9273
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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-2024 Tilman Neumann - [email protected]
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.quadraticResidues;
15+
16+
import static org.junit.Assert.assertEquals;
17+
18+
import java.math.BigInteger;
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
import org.apache.logging.log4j.Logger;
24+
import org.junit.BeforeClass;
25+
import org.junit.Test;
26+
import org.apache.logging.log4j.LogManager;
27+
28+
import de.tilman_neumann.util.ConfigUtil;
29+
30+
/**
31+
* QA tests for quadratic residue computations modulo 2^n.
32+
* The counts give A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, 43692, 87383, ...
33+
*
34+
* @author Tilman Neumann
35+
*/
36+
public class QuadraticResiduesMod2PowNTest {
37+
38+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest.class);
39+
40+
private static final int NCOUNT = 20;
41+
42+
private static final boolean DEBUG = false;
43+
private static final boolean SHOW_ELEMENTS = false;
44+
45+
private static ArrayList<Integer> correctCounts;
46+
47+
@BeforeClass
48+
public static void setup() {
49+
ConfigUtil.initProject();
50+
51+
// reference computation
52+
correctCounts = new ArrayList<Integer>();
53+
for (int n=0; n<NCOUNT; n++) {
54+
List<BigInteger> quadraticResiduesMod2PowN = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_big(n);
55+
LOG.info("n = " + n + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN : ""));
56+
correctCounts.add(quadraticResiduesMod2PowN.size());
57+
}
58+
LOG.info("v0 counts = " + correctCounts);
59+
}
60+
61+
@Test
62+
public void testV1() {
63+
ArrayList<Integer> counts = new ArrayList<Integer>();
64+
for (int n=0; n<NCOUNT; n++) {
65+
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll(n);
66+
if (DEBUG) LOG.debug("v1: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
67+
counts.add(quadraticResiduesMod2PowN_v1.size());
68+
}
69+
LOG.info("v1 counts = " + counts);
70+
assertEquals(correctCounts, counts);
71+
}
72+
73+
@Test
74+
public void testV2() {
75+
ArrayList<Integer> counts = new ArrayList<Integer>();
76+
for (int n=0; n<NCOUNT; n++) {
77+
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN_testAll_v2(n);
78+
if (DEBUG) LOG.debug("v2: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
79+
counts.add(quadraticResiduesMod2PowN_v1.size());
80+
}
81+
LOG.info("v2 counts = " + counts);
82+
assertEquals(correctCounts, counts);
83+
}
84+
85+
@Test
86+
public void testV3() {
87+
ArrayList<Integer> counts = new ArrayList<Integer>();
88+
for (int n=0; n<NCOUNT; n++) {
89+
List<Long> quadraticResiduesMod2PowN_v1 = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n);
90+
if (DEBUG) LOG.debug("v3: n = " + n + " has " + quadraticResiduesMod2PowN_v1.size() + " quadratic residues" + (SHOW_ELEMENTS ? ": " + quadraticResiduesMod2PowN_v1 : ""));
91+
counts.add(quadraticResiduesMod2PowN_v1.size());
92+
}
93+
LOG.info("v3 counts = " + counts);
94+
assertEquals(correctCounts, counts);
95+
}
96+
97+
@Test
98+
public void testV4() {
99+
ArrayList<Integer> counts = new ArrayList<Integer>();
100+
for (int n=0; n<NCOUNT; n++) {
101+
long[] array = new long[((1<<n) / 6) + 6];
102+
int count = QuadraticResiduesMod2PowN.getQuadraticResiduesMod2PowN(n, array);
103+
if (DEBUG) LOG.debug("v4: n = " + n + " has " + count + " quadratic residues" + (SHOW_ELEMENTS ? ": " + Arrays.toString(array) : ""));
104+
counts.add(count);
105+
}
106+
LOG.info("v4 counts = " + counts);
107+
assertEquals(correctCounts, counts);
108+
}
109+
}

0 commit comments

Comments
 (0)