Skip to content

Commit e34c1ff

Browse files
committed
Added test for Modulo Translation
1 parent 174243f commit e34c1ff

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package de.uka.ilkd.key.smt;
2+
3+
import de.uka.ilkd.key.control.DefaultUserInterfaceControl;
4+
import de.uka.ilkd.key.control.KeYEnvironment;
5+
import de.uka.ilkd.key.proof.Goal;
6+
import de.uka.ilkd.key.proof.Proof;
7+
import de.uka.ilkd.key.proof.io.ProblemLoaderException;
8+
import de.uka.ilkd.key.smt.solvertypes.SolverType;
9+
import de.uka.ilkd.key.smt.solvertypes.SolverTypeImplementation;
10+
import de.uka.ilkd.key.smt.solvertypes.SolverTypes;
11+
import org.junit.Test;
12+
import org.key_project.util.helper.FindResources;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
17+
import static org.junit.jupiter.api.Assertions.*;
18+
19+
import java.io.File;
20+
21+
/**
22+
* This class is for testing the translation of modulo from KeY to SMT
23+
* @author Nils Buchholz, 2024
24+
*/
25+
public class TestSMTMod {
26+
27+
private static final Logger LOGGER = LoggerFactory.getLogger(TestSMTMod.class);
28+
29+
private static final File testCaseDirectory = FindResources.getTestCasesDirectory();
30+
31+
private static final SolverType Z3_SOLVER = SolverTypes.getSolverTypes().stream()
32+
.filter(it -> it.getClass().equals(SolverTypeImplementation.class)
33+
&& it.getName().equals("Z3"))
34+
.findFirst().orElse(null);
35+
36+
private static final SolverType CVC4_SOLVER = SolverTypes.getSolverTypes().stream()
37+
.filter(it -> it.getClass().equals(SolverTypeImplementation.class)
38+
&& it.getName().equals("CVC4"))
39+
.findFirst().orElse(null);
40+
41+
/**
42+
* This tests if x mod y is non-negative and x mod y < |y| for y != 0
43+
* thus satisfying the definition of euclidean modulo
44+
* @throws ProblemLoaderException Occured Exception during load of problem file
45+
*/
46+
@Test
47+
public void testModSpec() throws ProblemLoaderException {
48+
KeYEnvironment<DefaultUserInterfaceControl> env =
49+
KeYEnvironment.load(new File(testCaseDirectory, "smt/modSpec.key"));
50+
try {
51+
Proof proof = env.getLoadedProof();
52+
assertNotNull(proof);
53+
assertEquals(1, proof.openGoals().size());
54+
Goal g = proof.openGoals().iterator().next();
55+
SMTSolverResult result;
56+
if (Z3_SOLVER.isInstalled(true)) {
57+
result = checkGoal(g, Z3_SOLVER);
58+
assertSame(SMTSolverResult.ThreeValuedTruth.VALID, result.isValid());
59+
} else {
60+
LOGGER.warn("Warning:Z3 solver not installed, tests skipped.");
61+
}
62+
if (CVC4_SOLVER.isInstalled(true)) {
63+
result = checkGoal(g, CVC4_SOLVER);
64+
assertSame(SMTSolverResult.ThreeValuedTruth.VALID, result.isValid());
65+
} else {
66+
LOGGER.warn("Warning:CVC4 solver not installed, tests skipped.");
67+
}
68+
} finally {
69+
env.dispose();
70+
}
71+
}
72+
73+
private SMTSolverResult checkGoal(Goal g, SolverType solverType) {
74+
SolverLauncher launcher = new SolverLauncher(new SMTTestSettings());
75+
SMTProblem problem = new SMTProblem(g);
76+
launcher.launch(problem, g.proof().getServices(), solverType);
77+
return problem.getFinalResult();
78+
}
79+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\functions {
2+
int y;
3+
int x;
4+
}
5+
6+
\problem {
7+
((y>0) -> x % y >= 0 & x % y < y) & ((y<0) -> x%y >= 0 & x % y < -y)
8+
}
9+

0 commit comments

Comments
 (0)