hsmt-0.1.0.0: test/HSMT/ArraySpec.hs
module HSMT.ArraySpec (tests) where
import Data.Text (Text)
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit
import HSMT.SMT.Engine
import HSMT.SMTLib.Run
tests :: TestTree
tests =
testGroup
"Arrays"
[ testCase "read over write" $ do
let base =
[ "(declare-fun a () (Array Int Int))"
, "(declare-fun i () Int)"
, "(declare-fun j () Int)"
]
expect (base <> ["(assert (not (= (select (store a i 5) i) 5)))"]) ["unsat"]
expect
(base <> ["(assert (not (= (select (store a i 5) j) (select a j))))", "(check-sat)", "(assert (distinct i j))"])
["sat", "unsat"]
, testCase "extensionality" $ do
let base =
[ "(declare-fun a () (Array Int Int))"
, "(declare-fun b () (Array Int Int))"
, "(declare-fun i () Int)"
, "(declare-fun j () Int)"
]
-- Writing back the value read leaves the array unchanged.
expect (base <> ["(assert (distinct a (store a i (select a i))))"]) ["unsat"]
-- Stores at different indices commute.
expect
(base <> ["(assert (distinct (store (store a i 1) j 2) (store (store a j 2) i 1)))", "(check-sat)", "(assert (distinct i j))"])
["sat", "unsat"]
expect (base <> ["(assert (distinct a b))", "(check-sat)", "(assert (= (select a i) (select b i)))"]) ["sat", "sat"]
, testCase "constant arrays" $ do
let k v = "((as const (Array Int Int)) " <> v <> ")"
expect ["(assert (= " <> k "0" <> " (store " <> k "1" <> " 3 0)))"] ["unsat"]
expect ["(assert (distinct " <> k "0" <> " (store " <> k "0" <> " 3 0)))"] ["unsat"]
expect
[ "(declare-fun x () Int)"
, "(assert (= (store " <> k "x" <> " 1 2) (store " <> k "3" <> " 1 2)))"
, "(check-sat)"
, "(assert (distinct x 3))"
]
["sat", "unsat"]
, testCase "finite index sorts" $ do
-- Only four arrays from Bool to Bool exist.
let arrays n = ["(declare-fun a" <> tshow i <> " () (Array Bool Bool))" | i <- [1 .. n]]
names n = T.unwords ["a" <> tshow i | i <- [1 .. n :: Int]]
expect (arrays 4 <> ["(assert (distinct " <> names 4 <> "))"]) ["sat"]
expect (arrays 5 <> ["(assert (distinct " <> names 5 <> "))"]) ["unsat"]
-- Overwriting every index changes the default.
let k v = "((as const (Array (_ BitVec 2) Bool)) " <> v <> ")"
full = "(store (store (store (store " <> k "false" <> " #b00 true) #b01 true) #b10 true) #b11 true)"
expect ["(assert (= " <> full <> " " <> k "true" <> "))"] ["sat"]
expect ["(assert (= (store " <> k "false" <> " #b10 true) (store " <> k "true" <> " #b01 false)))"] ["unsat"]
, testCase "uninterpreted functions over arrays" $ do
let base =
[ "(declare-sort U 0)"
, "(declare-fun a () (Array U U))"
, "(declare-fun b () (Array U U))"
, "(declare-fun i () U)"
, "(declare-fun f ((Array U U)) U)"
]
expect (base <> ["(assert (= a (store b i (select b i))))", "(assert (distinct (f a) (f b)))"]) ["unsat"]
-- f distinguishes a and b, so they must differ somewhere.
expect (base <> ["(assert (distinct (f a) (f b)))", "(assert (= (select a i) (select b i)))"]) ["sat"]
, testCase "nested arrays" $ do
let base =
[ "(declare-fun m () (Array Int (Array Int Int)))"
, "(declare-fun i () Int)"
, "(declare-fun j () Int)"
]
expect
(base <> ["(assert (not (= (select (select (store m i (store (select m i) j 7)) i) j) 7)))"])
["unsat"]
expect (base <> ["(assert (distinct (select m i) (select m j)))", "(check-sat)", "(assert (= i j))"]) ["sat", "unsat"]
, testCase "arrays of bit-vectors" $ do
let base =
[ "(declare-fun a () (Array (_ BitVec 8) (_ BitVec 8)))"
, "(declare-fun i () (_ BitVec 8))"
]
expect
(base <> ["(assert (= (bvadd (select a i) #x01) (select (store a (bvadd i #x01) #x00) i)))"])
["unsat"]
expect (base <> ["(assert (bvult (select a i) (select a (bvadd i #x00))))"]) ["unsat"]
]
tshow :: Int -> Text
tshow = T.pack . show
-- | Run a script (with model validation) and compare its answers; a final
-- @check-sat@ is appended.
expect :: [Text] -> [Text] -> Assertion
expect cmds answers =
runScriptText True (T.unlines (cmds <> ["(check-sat)"])) >>= \case
Left err -> assertFailure (T.unpack err)
Right rs -> map answer rs @?= answers
where
answer = \case
ESat _ -> "sat"
EUnsat _ -> "unsat"
EUnknown why -> "unknown: " <> why