|
9 | 9 | package org.sosy_lab.java_smt.test;
|
10 | 10 |
|
11 | 11 | import static com.google.common.truth.Truth.assertThat;
|
| 12 | +import static com.google.common.truth.TruthJUnit.assume; |
12 | 13 | import static org.junit.Assert.assertThrows;
|
13 | 14 |
|
14 | 15 | import com.google.common.collect.ImmutableList;
|
15 | 16 | import java.util.ArrayList;
|
16 | 17 | import java.util.List;
|
| 18 | +import java.util.function.BiFunction; |
| 19 | +import java.util.function.Function; |
17 | 20 | import org.junit.Test;
|
| 21 | +import org.sosy_lab.java_smt.SolverContextFactory.Solvers; |
18 | 22 | import org.sosy_lab.java_smt.api.BooleanFormula;
|
19 | 23 | import org.sosy_lab.java_smt.api.Formula;
|
20 | 24 | import org.sosy_lab.java_smt.api.FormulaType;
|
21 | 25 | import org.sosy_lab.java_smt.api.FunctionDeclaration;
|
| 26 | +import org.sosy_lab.java_smt.api.NumeralFormula; |
22 | 27 | import org.sosy_lab.java_smt.api.NumeralFormula.IntegerFormula;
|
23 | 28 | import org.sosy_lab.java_smt.api.NumeralFormula.RationalFormula;
|
24 | 29 | import org.sosy_lab.java_smt.api.SolverException;
|
25 | 30 | import org.sosy_lab.java_smt.api.visitors.DefaultFormulaVisitor;
|
26 | 31 |
|
27 | 32 | public class NumeralFormulaManagerTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
|
28 | 33 |
|
| 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 | + |
29 | 141 | @Test
|
30 | 142 | public void distinctTest() throws SolverException, InterruptedException {
|
31 | 143 | requireIntegers();
|
|
0 commit comments