Skip to content

Commit da601d1

Browse files
committed
add extensive testing
1 parent 64e3539 commit da601d1

File tree

4 files changed

+246
-4
lines changed

4 files changed

+246
-4
lines changed

README.org

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ It is based on [[https://github.com/chocoteam/choco-solver][choco-solver]] which
4242
The next step is simply to run one of the two console applications.
4343

4444
Execute the main classes without argument to display the help message.
45-
*** Running the solver
45+
*** Run the solver
4646

4747
: java -cp target/cryptator-0.2.0-SNAPSHOT-with-dependencies.jar cryptator.Cryptator -s 1 'send+more=money'
4848

49-
*** Playing the game
49+
*** Play the game
5050

5151
: java -cp target/cryptator-0.2.0-SNAPSHOT-with-dependencies.jar cryptator.Cryptamancer 'send+more=money'
5252

@@ -69,3 +69,12 @@ It is based on [[https://github.com/chocoteam/choco-solver][choco-solver]] which
6969
<version>0.2.0-SNAPSHOT</version>
7070
</dependency>
7171
#+END_EXAMPLE
72+
73+
** Integration tests
74+
The tests solve around 150 cryptarithms. \\
75+
By default, the tests are not executed by the regular maven lifecycle test execution.
76+
Indeed, their names are outside of the standard maven test runner nomenclature.
77+
78+
Just include it manually by feeding it as a parameter to the runner.
79+
80+
: mvn test -Dtest=ExtensiveTesting
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* This file is part of cryptator, https://github.com/arnaud-m/cryptator
3+
*
4+
* Copyright (c) 2021, Université Côte d'Azur. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-clause license.
7+
* See LICENSE file in the project root for full license information.
8+
*/
9+
package cryptator;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
import static org.junit.Assert.fail;
14+
15+
import java.io.InputStream;
16+
import java.util.Scanner;
17+
18+
import org.junit.BeforeClass;
19+
import org.junit.Test;
20+
21+
import cryptator.parser.CryptaParserException;
22+
import cryptator.parser.CryptaParserWrapper;
23+
import cryptator.solver.CryptaModelException;
24+
import cryptator.solver.CryptaSolver;
25+
import cryptator.solver.CryptaSolverException;
26+
import cryptator.specs.ICryptaEvaluation;
27+
import cryptator.specs.ICryptaNode;
28+
import cryptator.specs.ICryptaSolver;
29+
import cryptator.tree.CryptaEvaluation;
30+
import cryptator.tree.CryptaEvaluationException;
31+
32+
public class ExtensiveTesting {
33+
34+
public CryptaConfig config = new CryptaConfig();
35+
36+
public final CryptaParserWrapper parser = new CryptaParserWrapper();
37+
38+
public final ICryptaSolver solver= new CryptaSolver();
39+
40+
public final ICryptaEvaluation eval = new CryptaEvaluation();
41+
42+
public ExtensiveTesting() {}
43+
44+
@BeforeClass
45+
public static void configureTestLoggers() {
46+
JULogUtil.configureTestLoggers();
47+
}
48+
49+
50+
private void testCryptarithm(String cryptarithm) throws CryptaParserException, CryptaModelException, CryptaSolverException {
51+
final int[] sols = new int[1];
52+
final ICryptaNode node = parser.parse(cryptarithm);
53+
assertTrue(cryptarithm,
54+
solver.solve(node, config, (s) -> {
55+
//System.out.println(s);
56+
sols[0]++;
57+
try {
58+
assertEquals(1, eval.evaluate(node, s, config.getArithmeticBase()));
59+
} catch (CryptaEvaluationException e) {
60+
e.printStackTrace();
61+
fail();
62+
}
63+
} )
64+
);
65+
assertEquals(cryptarithm, 1, sols[0]);
66+
}
67+
68+
69+
@Test
70+
public void testAll() throws CryptaParserException, CryptaModelException, CryptaSolverException {
71+
final InputStream in = getClass().getClassLoader().getResourceAsStream("cryptarithms-barker.txt");
72+
final Scanner s = new Scanner(in);
73+
try {
74+
// s.skip("\\s*#.*"); // not working
75+
while(s.hasNextLine()) {
76+
final String line = s.nextLine();
77+
if(! line.matches("\\s*#.*")) {
78+
testCryptarithm(line);
79+
}
80+
}
81+
} finally {
82+
s.close();
83+
}
84+
}
85+
}

src/test/java/cryptator/SolverTest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public void setDefaultConfig() {
5757
private int testCryptarithmWithSolutions(String cryptarithm) throws CryptaParserException, CryptaModelException, CryptaSolverException {
5858
final int[] sols = new int[1];
5959
final ICryptaNode node = parser.parse(cryptarithm);
60-
assertTrue(
60+
assertTrue(cryptarithm,
6161
solver.solve(node, config, (s) -> {
6262
//System.out.println(s);
6363
sols[0]++;
@@ -75,7 +75,7 @@ private int testCryptarithmWithSolutions(String cryptarithm) throws CryptaParser
7575

7676
private void testCryptarithmWithoutSolutions(String cryptarithm) throws CryptaParserException, CryptaModelException, CryptaSolverException {
7777
final ICryptaNode node = parser.parse(cryptarithm);
78-
assertFalse(
78+
assertFalse(cryptarithm,
7979
solver.solve(node, config, (s) -> {
8080
fail("solution should not exist.");
8181
} )
@@ -302,6 +302,12 @@ public void testBarker5() throws CryptaParserException, CryptaModelException, Cr
302302
assertEquals(1, testCryptarithmWithSolutions("cinq*six=trente"));
303303
}
304304

305+
@Test
306+
@Ignore // Hypothesis: integer overflow
307+
public void testBarker6() throws CryptaParserException, CryptaModelException, CryptaSolverException {
308+
assertEquals(1, testCryptarithmWithSolutions("iron*radium=neon*sodium"));
309+
}
310+
305311

306312

307313

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
## Source: http:##colin.barker.pagesperso-orange.fr/crypta.htm
2+
## American States
3+
texas+nevada=alaska
4+
iowa+ohio+kansas=alaska
5+
kansas+oregon=arizona
6+
nevada+oregon+indiana=arizona
7+
oregon+georgia+indiana=arizona
8+
iowa+florida+colorado=delaware
9+
kansas+indiana=florida
10+
alaska+oregon=georgia
11+
iowa+nevada+indiana=georgia
12+
maine+arizona=georgia
13+
ohio+alabama+oklahoma=illinois
14+
texas+kansas+nevada=indiana
15+
iowa+kansas+nevada=indiana
16+
maine+nevada=kansas
17+
indiana+illinois=missouri
18+
maine+arizona+georgia=montana
19+
idaho+maine=nevada
20+
ohio+idaho+kansas=nevada
21+
oregon+georgia=newyork
22+
ohio+idaho+nevada=oregon
23+
ohio+kansas=oregon
24+
maine+nevada+indiana=vermont
25+
oregon+georgia+vermont=virginia
26+
## American Presidents
27+
carter+reagan+lincoln=clinton
28+
grant+arthur+reagan=harding
29+
grant+arthur+carter=reagan
30+
grant+hayes+reagan=arthur
31+
wilson+johnson=lincoln
32+
monroe+reagan=taylor
33+
arthur+taylor=hoover
34+
grant+carter=pierce
35+
ford+pierce=carter
36+
adams+grant=truman
37+
taft+truman=monroe
38+
## Planets
39+
earth+uranus=saturn
40+
saturn+uranus=jupiter
41+
mars+saturn+uranus=mercury
42+
## Fruit
43+
pear+apple=grape
44+
date+banana=cherry
45+
apple+lemon=banana
46+
plum+apple+grape=banana
47+
pear+peach+banana=cherry
48+
pear+lemon+banana=orange
49+
apple+grape+lemon=orange
50+
apple+grape+banana=orange
51+
apple+lemon+banana=orange
52+
grape+peach+cherry=orange
53+
pear+apple+lemon+banana=orange
54+
pear+plum+apple+grape+lemon=orange
55+
## Numbers
56+
ten+nine+eight=seven
57+
two+eight+twelve=eleven
58+
ten+three+eleven=twelve
59+
five+nine+twelve=eleven
60+
trois+treize=quatre
61+
douze+treize=quatre
62+
un+onze+douze+trois=treize
63+
## Greek Letters
64+
gamma+sigma=theta
65+
gamma+theta=lambda
66+
gamma+sigma=lambda
67+
kappa+theta=lambda
68+
mu+tau+gamma=theta
69+
pi+psi+kappa=theta
70+
pi+psi+gamma=sigma
71+
mu+iota+omega=gamma
72+
pi+alpha+kappa=lambda
73+
eta+beta+gamma=omega
74+
eta+zeta+gamma=omega
75+
tau+delta+omega=lambda
76+
phi+alpha+kappa=lambda
77+
## Elements
78+
neon+nickel=cobalt
79+
neon+carbon=nickel
80+
neon+cobalt=nickel
81+
lead+silver=radium
82+
argon+carbon=oxygen
83+
argon+iodine=copper
84+
tin+neon+iodine=helium
85+
iron+neon+helium=iodine
86+
gold+iron+silver=iodine
87+
iron+lead+iodine=radium
88+
iron+radium+sodium=iodine
89+
tin+gold+neon+iodine=oxygen
90+
tin+iron+neon+radium=iodine
91+
iron+neon+zinc+carbon=iodine
92+
iron+neon+argon+copper=iodine
93+
iron+neon+argon+radium=iodine
94+
gold*iron=lead*tin
95+
gold*neon=lead*zinc
96+
## FIXME iron*radium=neon*sodium
97+
argon*tin=iron*zinc
98+
copper*neon=iron*silver
99+
## Colours of the Rainbow
100+
green+violet=indigo
101+
red+green+orange=yellow
102+
red+green+orange=indigo
103+
## European Countries
104+
france+greece=finland
105+
finland+ireland=denmark
106+
wales+greece+sweden=england
107+
## "Doubly correct" multiplications
108+
cinq*six=trente
109+
un*cent=dix*dix
110+
deux*cent=dix*vingt
111+
## Roman:
112+
v*vi=xxx
113+
iv*xi=xliv
114+
v*xiv=lxx
115+
i*xlii=vi*vii
116+
iii*lv=xi*xv
117+
ii*xcvi=xii*xvi
118+
v*xlv=xv*xv
119+
iii*c=vi*l
120+
vi*l=x*xxx
121+
vii*lv=xi*xxxv
122+
v*lxxviii=xiii*xxx
123+
iv*xcix=vi*lxvi
124+
v*xc=x*xlv
125+
vii*lxxii=xii*xlii
126+
vi*xc=x*liv
127+
ix*lx=x*liv
128+
vi*xcii=xii*xlvi
129+
vi*xcv=x*lvii
130+
x*lxiii=xv*xlii
131+
x*lxiv=xvi*xl
132+
x*lxvi=xii*lv
133+
vii*c=xiv*l
134+
xv*lvi=xxi*xl
135+
xiii*c=xxvi*l
136+
xiv*xciii=xxxi*xlii
137+
xv*xcvi=xxiv*lx
138+
xv*xcviii=xxxv*xlii
139+
xv*c=xxx*l
140+
xxvii*lxx=xxx*lxiii
141+
xxv*xc=xlv*l
142+
xxvi*xcii=xlvi*lii

0 commit comments

Comments
 (0)