|
1 | 1 | package smtlib
|
2 | 2 | package it
|
3 | 3 |
|
4 |
| -import scala.sys.process._ |
5 |
| - |
6 | 4 | import org.scalatest.funsuite.AnyFunSuite
|
7 |
| - |
8 |
| -import java.io.File |
9 |
| -import java.io.FileReader |
10 |
| - |
11 |
| -import interpreters._ |
12 |
| - |
13 |
| -import trees.Terms._ |
14 |
| -import trees.Commands._ |
15 |
| -import trees.CommandsResponses._ |
| 5 | +import smtlib.trees.Commands._ |
| 6 | +import smtlib.trees.CommandsResponses._ |
| 7 | +import smtlib.trees.Terms._ |
16 | 8 |
|
17 | 9 |
|
18 | 10 | /** Checks that formula build with theories module are correctly handled by solvers */
|
19 | 11 | class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
|
20 | 12 |
|
| 13 | + def mkTest(formula: Term, expectedStatus: Status, prefix: String): Unit = mkTest(Nil, formula, expectedStatus, prefix) |
21 | 14 |
|
22 |
| - def mkTest(formula: Term, expectedStatus: Status, prefix: String) = { |
23 |
| - |
24 |
| - if(isZ3Available) { |
25 |
| - test(prefix + ": with Z3") { |
26 |
| - val z3Interpreter = getZ3Interpreter |
27 |
| - val assertion = Assert(formula) |
28 |
| - assert(z3Interpreter.eval(assertion) === Success) |
29 |
| - val res = z3Interpreter.eval(CheckSat()) |
| 15 | + def mkTest(cmds: List[Command], formula: Term, expectedStatus: Status, prefix: String): Unit = { |
| 16 | + def runAndCheck(mkInterp: => Interpreter): Unit = { |
| 17 | + val interp = mkInterp |
| 18 | + for (cmd <- cmds) { |
| 19 | + val res = interp.eval(cmd) |
30 | 20 | res match {
|
31 |
| - case CheckSatStatus(status) => assert(status === expectedStatus) |
32 |
| - case res => assert(false, "expected a check sat status, but got: " + res) |
| 21 | + case Success => () |
| 22 | + case other => fail(s"expected a success, but got: $other") |
33 | 23 | }
|
34 | 24 | }
|
| 25 | + val assertion = Assert(formula) |
| 26 | + assert(interp.eval(assertion) === Success) |
| 27 | + val res = interp.eval(CheckSat()) |
| 28 | + res match { |
| 29 | + case CheckSatStatus(status) => assert(status === expectedStatus) |
| 30 | + case other => fail(s"expected a check sat status, but got: $other") |
| 31 | + } |
35 | 32 | }
|
36 | 33 |
|
37 |
| - if(isCVC4Available) { |
38 |
| - test(prefix + ": with CVC4") { |
39 |
| - val cvc4Interpreter = getCVC4Interpreter |
40 |
| - val assertion = Assert(formula) |
41 |
| - assert(cvc4Interpreter.eval(assertion) === Success) |
42 |
| - val res = cvc4Interpreter.eval(CheckSat()) |
43 |
| - res match { |
44 |
| - case CheckSatStatus(status) => assert(status === expectedStatus) |
45 |
| - case res => assert(false, "expected a check sat status, but got: " + res) |
46 |
| - } |
| 34 | + val z3Test = prefix + ": with Z3" |
| 35 | + if (isZ3Available) { |
| 36 | + test(z3Test) { |
| 37 | + runAndCheck(getZ3Interpreter) |
| 38 | + } |
| 39 | + } else { |
| 40 | + ignore(z3Test) {} |
| 41 | + } |
| 42 | + |
| 43 | + val cvc4Test = prefix + ": with CVC4" |
| 44 | + if (isCVC4Available) { |
| 45 | + test(cvc4Test) { |
| 46 | + runAndCheck(getCVC4Interpreter) |
47 | 47 | }
|
| 48 | + } else { |
| 49 | + ignore(cvc4Test) {} |
48 | 50 | }
|
49 | 51 |
|
| 52 | + val cvc5Test = prefix + ": with cvc5" |
| 53 | + if (isCVC5Available) { |
| 54 | + test(cvc5Test) { |
| 55 | + runAndCheck(getCVC5Interpreter) |
| 56 | + } |
| 57 | + } else { |
| 58 | + ignore(cvc5Test) {} |
| 59 | + } |
50 | 60 | }
|
51 | 61 |
|
52 | 62 |
|
@@ -146,8 +156,8 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
|
146 | 156 |
|
147 | 157 | {
|
148 | 158 | import theories.Core.Equals
|
149 |
| - import theories.Ints.NumeralLit |
150 | 159 | import theories.FixedSizeBitVectors._
|
| 160 | + import theories.Ints.NumeralLit |
151 | 161 | val theoryString = "Theory of Bit Vectors"
|
152 | 162 | var counter = 0
|
153 | 163 | def uniqueName(): String = {
|
@@ -214,4 +224,69 @@ class TheoriesBuilderTests extends AnyFunSuite with TestHelpers {
|
214 | 224 | val f20 = Equals(Mul(BitVectorConstant(1, 32), BitVectorConstant(2, 32), BitVectorConstant(3, 32), BitVectorConstant(4, 32)), BitVectorConstant(24, 32))
|
215 | 225 | mkTest(f20, SatStatus, uniqueName())
|
216 | 226 | }
|
| 227 | + |
| 228 | + { |
| 229 | + import theories.Core._ |
| 230 | + import theories.Ints._ |
| 231 | + val theoryADTs = "Theory of ADTs" |
| 232 | + var counter = 0 |
| 233 | + |
| 234 | + def uniqueName(): String = { |
| 235 | + counter += 1 |
| 236 | + "%d - %s".format(counter, theoryADTs) |
| 237 | + } |
| 238 | + |
| 239 | + val list = SSymbol("MyList") |
| 240 | + val listSort = Sort(Identifier(list)) |
| 241 | + val nil = SSymbol("Nil") |
| 242 | + val cons = SSymbol("Cons") |
| 243 | + val head = SSymbol("head") |
| 244 | + val tail = SSymbol("tail") |
| 245 | + val listCtors = List( |
| 246 | + Constructor(nil, Seq.empty), |
| 247 | + Constructor(cons, Seq((head, IntSort()), (tail, listSort))) |
| 248 | + ) |
| 249 | + |
| 250 | + def consApp(hd: Term, tl: Term): Term = |
| 251 | + FunctionApplication( |
| 252 | + QualifiedIdentifier(Identifier(cons), Some(listSort)), |
| 253 | + Seq(hd, tl) |
| 254 | + ) |
| 255 | + |
| 256 | + def nilApp: Term = |
| 257 | + QualifiedIdentifier(Identifier(nil), Some(listSort)) |
| 258 | + |
| 259 | + val x = QualifiedIdentifier(Identifier(SSymbol("x")), Some(IntSort())) |
| 260 | + val y = QualifiedIdentifier(Identifier(SSymbol("y")), Some(IntSort())) |
| 261 | + val z = QualifiedIdentifier(Identifier(SSymbol("z")), Some(listSort)) |
| 262 | + val a = QualifiedIdentifier(Identifier(SSymbol("a")), Some(IntSort())) |
| 263 | + val b = QualifiedIdentifier(Identifier(SSymbol("b")), Some(IntSort())) |
| 264 | + val c = QualifiedIdentifier(Identifier(SSymbol("c")), Some(IntSort())) |
| 265 | + |
| 266 | + val ids = Seq(x, y, z, a, b, c) |
| 267 | + val idsDecls = ids.map(id => DeclareFun(id.id.symbol, Seq.empty, id.sort.get)) |
| 268 | + |
| 269 | + mkTest( |
| 270 | + List(DeclareDatatypes(List((list, listCtors)))) ++ idsDecls, |
| 271 | + Equals( |
| 272 | + consApp(x, consApp(y, z)), |
| 273 | + consApp(a, consApp(b, consApp(c, nilApp))) |
| 274 | + ), |
| 275 | + SatStatus, |
| 276 | + uniqueName() |
| 277 | + ) |
| 278 | + |
| 279 | + mkTest( |
| 280 | + List(DeclareDatatypes(List((list, listCtors)))) ++ idsDecls, |
| 281 | + And( |
| 282 | + Equals( |
| 283 | + consApp(x, consApp(y, z)), |
| 284 | + consApp(a, consApp(b, consApp(c, nilApp))) |
| 285 | + ), |
| 286 | + Not(Equals(z, consApp(c, nilApp))) |
| 287 | + ), |
| 288 | + UnsatStatus, |
| 289 | + uniqueName() |
| 290 | + ) |
| 291 | + } |
217 | 292 | }
|
0 commit comments