Skip to content

Commit dd20647

Browse files
Add tests for #461
1 parent 5900232 commit dd20647

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

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

+112
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,135 @@
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;
18+
import java.util.function.BiFunction;
19+
import java.util.function.Function;
1720
import org.junit.Test;
21+
import org.sosy_lab.java_smt.SolverContextFactory.Solvers;
1822
import org.sosy_lab.java_smt.api.BooleanFormula;
1923
import org.sosy_lab.java_smt.api.Formula;
2024
import org.sosy_lab.java_smt.api.FormulaType;
2125
import org.sosy_lab.java_smt.api.FunctionDeclaration;
26+
import org.sosy_lab.java_smt.api.NumeralFormula;
2227
import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
2328
import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
2429
import org.sosy_lab.java_smt.api.SolverException;
2530
import org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor;
2631

2732
public class NumeralFormulaManagerTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
2833

34+
/** Require that the solver supports mixed integer-real arithmetics */
35+
private void requireMixedArithmetics() {
36+
requireIntegers();
37+
requireRationals();
38+
assume().that(solver).isNotEqualTo(Solvers.OPENSMT); // OpenSMT does not support mixed terms
39+
}
40+
41+
/** Check if this unary operation returns the expected value. */
42+
private void testOperation(
43+
Function<NumeralFormula, NumeralFormula> f, NumeralFormula arg, NumeralFormula expected)
44+
throws SolverException, InterruptedException {
45+
assertThatFormula(rmgr.equal(f.apply(arg), expected)).isSatisfiable();
46+
assertThat(mgr.getFormulaType(f.apply(arg)).isRationalType()).isTrue();
47+
}
48+
49+
/** Same as unary testOperation(), but we expect the result to be an integer term. */
50+
private void testIntegerOperation(
51+
Function<NumeralFormula, IntegerFormula> f, NumeralFormula arg, IntegerFormula expected)
52+
throws SolverException, InterruptedException {
53+
assertThatFormula(imgr.equal(f.apply(arg), expected)).isSatisfiable();
54+
assertThat(mgr.getFormulaType(f.apply(arg)).isIntegerType()).isTrue();
55+
}
56+
57+
/** Check if this binary operation returns the expected value. */
58+
private void testOperation(
59+
BiFunction<NumeralFormula, NumeralFormula, NumeralFormula> f,
60+
NumeralFormula arg1,
61+
NumeralFormula arg2,
62+
NumeralFormula expected)
63+
throws SolverException, InterruptedException {
64+
assertThatFormula(rmgr.equal(f.apply(arg1, arg2), expected)).isSatisfiable();
65+
assertThat(mgr.getFormulaType(f.apply(arg1, arg2)).isRationalType()).isTrue();
66+
}
67+
68+
@Test
69+
public void mixedNegationTest() throws SolverException, InterruptedException {
70+
requireMixedArithmetics();
71+
testOperation(rmgr::negate, imgr.makeNumber(1.5), rmgr.makeNumber(-1.0));
72+
testOperation(rmgr::negate, rmgr.makeNumber(1.5), rmgr.makeNumber(-1.5));
73+
}
74+
75+
@Test
76+
public void mixedFloorTest() throws SolverException, InterruptedException {
77+
requireRationalFloor();
78+
requireMixedArithmetics();
79+
testIntegerOperation(rmgr::floor, imgr.makeNumber(1.0), imgr.makeNumber(1.0));
80+
testIntegerOperation(rmgr::floor, rmgr.makeNumber(1.5), imgr.makeNumber(1.0));
81+
}
82+
83+
@Test
84+
public void mixedAdditionTest() throws SolverException, InterruptedException {
85+
requireMixedArithmetics();
86+
testOperation(rmgr::add, imgr.makeNumber(2), imgr.makeNumber(1), rmgr.makeNumber(3.0));
87+
testOperation(rmgr::add, imgr.makeNumber(2), rmgr.makeNumber(1.5), rmgr.makeNumber(3.5));
88+
testOperation(rmgr::add, rmgr.makeNumber(1.5), imgr.makeNumber(2), rmgr.makeNumber(3.5));
89+
testOperation(rmgr::add, rmgr.makeNumber(1.5), rmgr.makeNumber(2.5), rmgr.makeNumber(4.0));
90+
}
91+
92+
@Test
93+
public void mixedSubtractionTest() throws SolverException, InterruptedException {
94+
requireMixedArithmetics();
95+
testOperation(rmgr::subtract, imgr.makeNumber(2), imgr.makeNumber(1), rmgr.makeNumber(1.0));
96+
testOperation(rmgr::subtract, imgr.makeNumber(2), rmgr.makeNumber(1.5), rmgr.makeNumber(0.5));
97+
testOperation(rmgr::subtract, rmgr.makeNumber(1.5), imgr.makeNumber(2), rmgr.makeNumber(-0.5));
98+
testOperation(rmgr::subtract, rmgr.makeNumber(1.5), rmgr.makeNumber(0.5), rmgr.makeNumber(1.0));
99+
}
100+
101+
@Test
102+
public void mixedDivisionTest() throws SolverException, InterruptedException {
103+
requireMixedArithmetics();
104+
testOperation(rmgr::divide, imgr.makeNumber(1), imgr.makeNumber(2), rmgr.makeNumber(0.5));
105+
testOperation(rmgr::divide, imgr.makeNumber(1), rmgr.makeNumber(2.0), rmgr.makeNumber(0.5));
106+
testOperation(rmgr::divide, rmgr.makeNumber(1.0), imgr.makeNumber(2), rmgr.makeNumber(0.5));
107+
testOperation(rmgr::divide, rmgr.makeNumber(1.0), rmgr.makeNumber(0.5), rmgr.makeNumber(2.0));
108+
}
109+
110+
@Test
111+
public void mixedMultiplicationTest() throws SolverException, InterruptedException {
112+
requireMixedArithmetics();
113+
testOperation(rmgr::multiply, imgr.makeNumber(2), imgr.makeNumber(3), rmgr.makeNumber(6.0));
114+
testOperation(rmgr::multiply, imgr.makeNumber(2), rmgr.makeNumber(1.25), rmgr.makeNumber(2.5));
115+
testOperation(rmgr::multiply, rmgr.makeNumber(1.25), imgr.makeNumber(2), rmgr.makeNumber(2.5));
116+
testOperation(
117+
rmgr::multiply, rmgr.makeNumber(1.5), rmgr.makeNumber(2.5), rmgr.makeNumber(3.75));
118+
}
119+
120+
@Test
121+
public void mixedEqualityTest() throws SolverException, InterruptedException {
122+
requireMixedArithmetics();
123+
assertThatFormula(rmgr.equal(imgr.makeNumber(1.5), rmgr.makeNumber(1.0))).isSatisfiable();
124+
assertThatFormula(rmgr.equal(rmgr.makeNumber(1.0), imgr.makeNumber(1.5))).isSatisfiable();
125+
}
126+
127+
@Test
128+
public void mixedGreaterThanTest() throws SolverException, InterruptedException {
129+
requireMixedArithmetics();
130+
assertThatFormula(rmgr.greaterThan(imgr.makeNumber(1.5), rmgr.makeNumber(0.5))).isSatisfiable();
131+
assertThatFormula(rmgr.greaterThan(rmgr.makeNumber(1.5), imgr.makeNumber(1.5))).isSatisfiable();
132+
}
133+
134+
@Test
135+
public void mixedLessThanTest() throws SolverException, InterruptedException {
136+
requireMixedArithmetics();
137+
assertThatFormula(rmgr.lessThan(imgr.makeNumber(1.5), rmgr.makeNumber(1.5))).isSatisfiable();
138+
assertThatFormula(rmgr.lessThan(rmgr.makeNumber(0.5), imgr.makeNumber(1.5))).isSatisfiable();
139+
}
140+
29141
@Test
30142
public void distinctTest() throws SolverException, InterruptedException {
31143
requireIntegers();

0 commit comments

Comments
 (0)