|
32 | 32 | import org.sosy_lab.java_smt.api.FloatingPointNumber;
|
33 | 33 | import org.sosy_lab.java_smt.api.FloatingPointNumber.Sign;
|
34 | 34 | import org.sosy_lab.java_smt.api.FloatingPointRoundingMode;
|
| 35 | +import org.sosy_lab.java_smt.api.FloatingPointRoundingModeFormula; |
| 36 | +import org.sosy_lab.java_smt.api.Formula; |
35 | 37 | import org.sosy_lab.java_smt.api.FormulaType;
|
36 | 38 | import org.sosy_lab.java_smt.api.FormulaType.FloatingPointType;
|
| 39 | +import org.sosy_lab.java_smt.api.FunctionDeclaration; |
| 40 | +import org.sosy_lab.java_smt.api.FunctionDeclarationKind; |
37 | 41 | import org.sosy_lab.java_smt.api.InterpolatingProverEnvironment;
|
38 | 42 | import org.sosy_lab.java_smt.api.Model;
|
39 | 43 | import org.sosy_lab.java_smt.api.Model.ValueAssignment;
|
40 | 44 | import org.sosy_lab.java_smt.api.NumeralFormula;
|
41 | 45 | import org.sosy_lab.java_smt.api.ProverEnvironment;
|
| 46 | +import org.sosy_lab.java_smt.api.QuantifiedFormulaManager.Quantifier; |
42 | 47 | import org.sosy_lab.java_smt.api.SolverContext.ProverOptions;
|
43 | 48 | import org.sosy_lab.java_smt.api.SolverException;
|
| 49 | +import org.sosy_lab.java_smt.api.visitors.FormulaVisitor; |
44 | 50 |
|
45 | 51 | public class FloatingPointFormulaManagerTest
|
46 | 52 | extends SolverBasedTest0.ParameterizedSolverBasedTest0 {
|
@@ -87,6 +93,66 @@ public void floatingPointType() {
|
87 | 93 | .isEqualTo(type.getMantissaSize());
|
88 | 94 | }
|
89 | 95 |
|
| 96 | + @Test |
| 97 | + public void roundingModeVisitor() { |
| 98 | + FloatingPointFormula var = |
| 99 | + fpmgr.makeVariable("a", FloatingPointType.getSinglePrecisionFloatingPointType()); |
| 100 | + FloatingPointFormula f = fpmgr.sqrt(var, FloatingPointRoundingMode.NEAREST_TIES_TO_EVEN); |
| 101 | + |
| 102 | + for (FloatingPointRoundingMode rm : FloatingPointRoundingMode.values()) { |
| 103 | + if (solver == Solvers.MATHSAT5 && rm == FloatingPointRoundingMode.NEAREST_TIES_AWAY) { |
| 104 | + // SKIP MathSAT does not support rounding mode "nearest-ties-away" |
| 105 | + continue; |
| 106 | + } |
| 107 | + // Build a term with a different rounding mode, then replace it in the visitor |
| 108 | + FloatingPointFormula g = |
| 109 | + (FloatingPointFormula) |
| 110 | + mgr.visit( |
| 111 | + fpmgr.sqrt(var, rm), |
| 112 | + new FormulaVisitor<Formula>() { |
| 113 | + @Override |
| 114 | + public Formula visitFreeVariable(Formula f, String name) { |
| 115 | + return f; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Formula visitConstant(Formula f, Object value) { |
| 120 | + assertThat(f).isInstanceOf(FloatingPointRoundingModeFormula.class); |
| 121 | + assertThat(value).isInstanceOf(FloatingPointRoundingMode.class); |
| 122 | + assertThat(value).isEqualTo(rm); |
| 123 | + |
| 124 | + // Return the default rounding mode |
| 125 | + return fpmgr.makeRoundingMode(FloatingPointRoundingMode.NEAREST_TIES_TO_EVEN); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Formula visitFunction( |
| 130 | + Formula f, List<Formula> args, FunctionDeclaration<?> functionDeclaration) { |
| 131 | + assertThat(functionDeclaration.getKind()) |
| 132 | + .isEqualTo(FunctionDeclarationKind.FP_SQRT); |
| 133 | + assertThat(args.size()).isEqualTo(2); |
| 134 | + return mgr.makeApplication( |
| 135 | + functionDeclaration, |
| 136 | + mgr.visit(args.get(0), this), |
| 137 | + mgr.visit(args.get(1), this)); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public Formula visitQuantifier( |
| 142 | + BooleanFormula f, |
| 143 | + Quantifier quantifier, |
| 144 | + List<Formula> boundVariables, |
| 145 | + BooleanFormula body) { |
| 146 | + throw new IllegalArgumentException( |
| 147 | + String.format("Unexpected quantifier %s", quantifier)); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + // Check that after the substitution the rounding mode is the default again |
| 152 | + assertThat(f).isEqualTo(g); |
| 153 | + } |
| 154 | + } |
| 155 | + |
90 | 156 | @Test
|
91 | 157 | public void negative() throws SolverException, InterruptedException {
|
92 | 158 | for (double d : new double[] {-1, -2, -0.0, Double.NEGATIVE_INFINITY}) {
|
|
0 commit comments