Skip to content

Commit 40ec014

Browse files
Add tests for division by zero
1 parent e04d83c commit 40ec014

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/org/sosy_lab/java_smt/test/NumeralFormulaManagerTest.java

+74
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
package org.sosy_lab.java_smt.test;
1010

1111
import static com.google.common.truth.Truth.assertThat;
12+
import static com.google.common.truth.TruthJUnit.assume;
1213
import static org.junit.Assert.assertThrows;
1314

1415
import com.google.common.collect.ImmutableList;
1516
import java.util.ArrayList;
1617
import java.util.List;
1718
import org.junit.Test;
19+
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
1820
import org.sosy_lab.java_smt.api.BooleanFormula;
1921
import org.sosy_lab.java_smt.api.Formula;
2022
import org.sosy_lab.java_smt.api.FormulaType;
@@ -26,6 +28,78 @@
2628

2729
public class NumeralFormulaManagerTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
2830

31+
@Test
32+
public void divZeroTest() throws SolverException, InterruptedException {
33+
requireIntegers();
34+
assume().that(solver).isNoneOf(Solvers.OPENSMT, Solvers.YICES2); // No division by zero
35+
36+
IntegerFormula zero = imgr.makeNumber(0);
37+
IntegerFormula three = imgr.makeNumber(3);
38+
IntegerFormula seven = imgr.makeNumber(7);
39+
40+
// Show that 3/0=0 and 3/0=7 can hold separately
41+
IntegerFormula f1 = imgr.divide(three, zero);
42+
assertThatFormula(imgr.equal(f1, zero)).isSatisfiable();
43+
assertThatFormula(imgr.equal(f1, seven)).isSatisfiable();
44+
45+
// But if 3/0=0 in the model, it can't also be another value
46+
IntegerFormula var = imgr.makeVariable("var");
47+
IntegerFormula f2 = imgr.divide(var, zero);
48+
IntegerFormula res = imgr.makeVariable("res");
49+
assertThatFormula(
50+
bmgr.and(
51+
imgr.equal(f1, zero),
52+
imgr.equal(var, three),
53+
imgr.equal(f2, res),
54+
bmgr.not(imgr.equal(res, zero))))
55+
.isUnsatisfiable();
56+
57+
// Show that a=b => a/0=b/0
58+
IntegerFormula var1 = imgr.makeVariable("arg1");
59+
IntegerFormula var2 = imgr.makeVariable("arg2");
60+
assertThatFormula(
61+
bmgr.implication(
62+
imgr.equal(var1, var2),
63+
imgr.equal(imgr.divide(var1, zero), imgr.divide(var2, zero))))
64+
.isTautological();
65+
}
66+
67+
@Test
68+
public void modZeroTest() throws SolverException, InterruptedException {
69+
requireIntegers();
70+
assume().that(solver).isNoneOf(Solvers.OPENSMT, Solvers.YICES2); // No division by zero
71+
72+
IntegerFormula zero = imgr.makeNumber(0);
73+
IntegerFormula three = imgr.makeNumber(3);
74+
IntegerFormula seven = imgr.makeNumber(7);
75+
76+
// Show that 3%0=0 and 3%0=7 can hold separately
77+
IntegerFormula f1 = imgr.modulo(three, zero);
78+
assertThatFormula(imgr.equal(f1, zero)).isSatisfiable();
79+
assertThatFormula(imgr.equal(f1, seven)).isSatisfiable();
80+
81+
// But if 3%0=0 in the model, it can't also be another value
82+
IntegerFormula var = imgr.makeVariable("var");
83+
IntegerFormula f2 = imgr.modulo(var, zero);
84+
IntegerFormula res = imgr.makeVariable("res");
85+
assertThatFormula(
86+
bmgr.and(
87+
imgr.equal(f1, zero),
88+
imgr.equal(var, three),
89+
imgr.equal(f2, res),
90+
bmgr.not(imgr.equal(res, zero))))
91+
.isUnsatisfiable();
92+
93+
// Show that a=b => a%0=b%0
94+
IntegerFormula var1 = imgr.makeVariable("arg1");
95+
IntegerFormula var2 = imgr.makeVariable("arg2");
96+
assertThatFormula(
97+
bmgr.implication(
98+
imgr.equal(var1, var2),
99+
imgr.equal(imgr.modulo(var1, zero), imgr.modulo(var2, zero))))
100+
.isTautological();
101+
}
102+
29103
@Test
30104
public void distinctTest() throws SolverException, InterruptedException {
31105
requireIntegers();

0 commit comments

Comments
 (0)