Skip to content

Commit fe57019

Browse files
committed
convert first test of quadratic residues into unit tests
1 parent bb7ac05 commit fe57019

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

src/main/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNTest01.java src/test/java/de/tilman_neumann/jml/quadraticResidues/QuadraticResiduesMod2PowNTest.java

+30-13
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,67 @@
1313
*/
1414
package de.tilman_neumann.jml.quadraticResidues;
1515

16+
import static org.junit.Assert.assertEquals;
17+
1618
import java.util.ArrayList;
1719
import java.util.TreeSet;
1820

1921
import org.apache.logging.log4j.Logger;
22+
import org.junit.BeforeClass;
23+
import org.junit.Test;
2024
import org.apache.logging.log4j.LogManager;
2125

2226
import de.tilman_neumann.util.ConfigUtil;
2327

2428
/**
25-
* Tests of quadratic residue computations modulo general m.
29+
* Test quadratic residue computations modulo 2^n.
2630
*
2731
* @author Tilman Neumann
2832
*/
29-
public class QuadraticResiduesMod2PowNTest01 {
33+
public class QuadraticResiduesMod2PowNTest {
3034

31-
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest01.class);
35+
private static final Logger LOG = LogManager.getLogger(QuadraticResiduesMod2PowNTest.class);
3236

33-
/**
34-
* Test.
35-
* @param args ignored
36-
*/
37-
public static void main(String[] args) {
37+
private static final boolean DEBUG = false;
38+
39+
@BeforeClass
40+
public static void setup() {
3841
ConfigUtil.initProject();
42+
}
43+
44+
@Test
45+
public void testSetOfQuadraticResidues() {
3946
TreeSet<Long> quadraticResiduesMod100 = QuadraticResidues.getQuadraticResidues(100);
4047
LOG.info("m = 100 has " + quadraticResiduesMod100.size() + " quadratic residues: " + quadraticResiduesMod100);
41-
48+
assertEquals("[0, 1, 4, 9, 16, 21, 24, 25, 29, 36, 41, 44, 49, 56, 61, 64, 69, 76, 81, 84, 89, 96]", quadraticResiduesMod100.toString());
49+
}
50+
51+
/**
52+
* Test the number of quadratic residues modulo 2^n for n = 0, 1, 2, ...
53+
* This gives number sequences
54+
* A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, ... for general quadratic residues modulo 2^n, and
55+
* a(n) = {1, 1} + A023105(n-2) = 1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, ... for "even" quadratic residues modulo 2^n.
56+
*/
57+
@Test
58+
public void testQuadraticResidueCounts() {
4259
ArrayList<Integer> counts = new ArrayList<Integer>();
4360
ArrayList<Integer> evenCounts = new ArrayList<Integer>();
4461

4562
for (int n=0; n<20; n++) {
4663
int m = 1<<n;
4764

4865
TreeSet<Long> quadraticResiduesMod2PowN = QuadraticResidues.getQuadraticResidues(m);
49-
LOG.info("m = " + m + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues: " + quadraticResiduesMod2PowN);
66+
if (DEBUG) LOG.debug("m = " + m + " has " + quadraticResiduesMod2PowN.size() + " quadratic residues: " + quadraticResiduesMod2PowN);
5067
counts.add(quadraticResiduesMod2PowN.size());
5168

5269
TreeSet<Long> evenQuadraticResiduesMod2PowN = QuadraticResidues.getEvenQuadraticResidues(m);
53-
LOG.info("m = " + m + " has " + evenQuadraticResiduesMod2PowN.size() + " 'even' quadratic residues: " + evenQuadraticResiduesMod2PowN);
70+
if (DEBUG) LOG.debug("m = " + m + " has " + evenQuadraticResiduesMod2PowN.size() + " 'even' quadratic residues: " + evenQuadraticResiduesMod2PowN);
5471
evenCounts.add(evenQuadraticResiduesMod2PowN.size());
5572
}
5673

5774
LOG.info("counts = " + counts);
58-
// A023105(n) = 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, ...
75+
assertEquals("[1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847, 43692, 87383]", counts.toString());
5976
LOG.info("evenCounts = " + evenCounts);
60-
// a(n) = {1, 1} + A023105(n-2) = 1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847
77+
assertEquals("[1, 1, 1, 2, 2, 3, 4, 7, 12, 23, 44, 87, 172, 343, 684, 1367, 2732, 5463, 10924, 21847]", evenCounts.toString());
6178
}
6279
}

0 commit comments

Comments
 (0)