|
| 1 | +/* |
| 2 | + * This file is part of JavaSMT, |
| 3 | + * an API wrapper for a collection of SMT solvers: |
| 4 | + * https://github.com/sosy-lab/java-smt |
| 5 | + * |
| 6 | + * SPDX-FileCopyrightText: 2024 Dirk Beyer <https://www.sosy-lab.org> |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: Apache-2.0 |
| 9 | + */ |
| 10 | + |
| 11 | +package org.sosy_lab.java_smt.test; |
| 12 | + |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.Test; |
| 15 | +import org.sosy_lab.java_smt.api.BooleanFormula; |
| 16 | +import org.sosy_lab.java_smt.api.SolverException; |
| 17 | + |
| 18 | +public class SanitizerTest extends SolverBasedTest0.ParameterizedSolverBasedTest0 { |
| 19 | + @Before |
| 20 | + public void init() { |
| 21 | + requireParser(); |
| 22 | + requireIntegers(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void validLogicTest() throws SolverException, InterruptedException { |
| 27 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 28 | + BooleanFormula broken = |
| 29 | + mgr.parse("(set-logic QF_ALL)" + "(declare-const v Int)" + "(assert (= v 3))"); |
| 30 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 31 | + } |
| 32 | + |
| 33 | + @Test(expected = IllegalArgumentException.class) |
| 34 | + public void wrongLogicTest() throws SolverException, InterruptedException { |
| 35 | + // When we change the code to not use sanitize() solver seem to just ignore set-logic |
| 36 | + // Except for OpenSMT, which always crashes |
| 37 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 38 | + BooleanFormula broken = |
| 39 | + mgr.parse("(set-logic QF_UF)" + "(declare-const v Int)" + "(assert (= v 3))"); |
| 40 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 41 | + } |
| 42 | + |
| 43 | + @Test(expected = IllegalArgumentException.class) |
| 44 | + public void logicAfterOptionTest() throws SolverException, InterruptedException { |
| 45 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 46 | + BooleanFormula broken = |
| 47 | + mgr.parse( |
| 48 | + "(set-option :produce-models true)" |
| 49 | + + "(set-logic ALL)" |
| 50 | + + "(declare-const v Int)" |
| 51 | + + "(assert (= v 3))"); |
| 52 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 53 | + } |
| 54 | + |
| 55 | + @Test(expected = IllegalArgumentException.class) |
| 56 | + public void logicUsedTwiceTest() throws SolverException, InterruptedException { |
| 57 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 58 | + BooleanFormula broken = |
| 59 | + mgr.parse( |
| 60 | + "(set-logic ALL)" + "(declare-const v Int)" + "(set-logic QF_UF)" + "(assert (= v 3))"); |
| 61 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void exitAtTheEnd() throws SolverException, InterruptedException { |
| 66 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 67 | + BooleanFormula broken = mgr.parse("(declare-const v Int)" + "(assert (= v 3))" + "(exit)"); |
| 68 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 69 | + } |
| 70 | + |
| 71 | + @Test(expected = IllegalArgumentException.class) |
| 72 | + public void exitNotTheEnd() throws SolverException, InterruptedException { |
| 73 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 74 | + BooleanFormula broken = |
| 75 | + mgr.parse("(declare-const v Int)" + "(assert (= v 3))" + "(exit)" + "(assert (= v 0))"); |
| 76 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void logicResetTest() throws SolverException, InterruptedException { |
| 81 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 82 | + BooleanFormula broken = |
| 83 | + mgr.parse( |
| 84 | + "(set-logic QF_UF)" |
| 85 | + + "(reset)" |
| 86 | + + "(set-logic ALL)" |
| 87 | + + "(declare-const v Int)" |
| 88 | + + "(assert (= v 3))"); |
| 89 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void stackPushTest() throws SolverException, InterruptedException { |
| 94 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 95 | + BooleanFormula broken = |
| 96 | + mgr.parse( |
| 97 | + "(declare-const v Int)" |
| 98 | + + "(push 1)" |
| 99 | + + "(assert (= v 0))" |
| 100 | + + "(pop 1)" |
| 101 | + + "(assert (= v 3))"); |
| 102 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void stackResetAssertionsTest() throws SolverException, InterruptedException { |
| 107 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 108 | + BooleanFormula broken = |
| 109 | + mgr.parse( |
| 110 | + "(declare-const v Int)" |
| 111 | + + "(assert (= v 3))" |
| 112 | + + "(reset-assertions)" |
| 113 | + + "(declare-const v Int)" |
| 114 | + + "(assert (= v 0))"); |
| 115 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void globalStackResetAssertionsTest() throws SolverException, InterruptedException { |
| 120 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 121 | + BooleanFormula broken = |
| 122 | + mgr.parse( |
| 123 | + "(set-option :global-declarations true)" |
| 124 | + + "(declare-const v Int)" |
| 125 | + + "(assert (= v 3))" |
| 126 | + + "(reset-assertions)" |
| 127 | + + "(assert (= v 0))"); |
| 128 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void stackResetTest() throws SolverException, InterruptedException { |
| 133 | + requireParser(); |
| 134 | + requireIntegers(); |
| 135 | + BooleanFormula expected = mgr.parse("(declare-const v Int)(assert (= v 3))"); |
| 136 | + BooleanFormula broken = |
| 137 | + mgr.parse( |
| 138 | + "(declare-const v Int)" |
| 139 | + + "(assert (= v 0))" |
| 140 | + + "(reset)" |
| 141 | + + "(declare-const v Int)" |
| 142 | + + "(assert (= v 3))"); |
| 143 | + assertThatFormula(expected).isEquivalentTo(broken); |
| 144 | + } |
| 145 | +} |
0 commit comments