hsmt-0.1.0.0: test/HSMT/APISpec.hs
{-# LANGUAGE OverloadedLabels #-}
module HSMT.APISpec (tests, testsWith) where
import Control.Exception (try)
import qualified Data.Text as T
import Test.Tasty
import Test.Tasty.HUnit hiding (assert)
import HSMT
type Person = 'RecordS '[ '("name", 'StringS), '("age", 'IntS)]
type Point = 'RecordS '[ '("x", 'IntS), '("y", 'IntS)]
isSat :: Result ctx -> Bool
isSat = \case Sat _ -> True; _ -> False
isUnknown :: Result ctx -> Bool
isUnknown = \case Unknown _ -> True; _ -> False
isUnsat :: Result ctx -> Bool
isUnsat = \case Unsat _ -> True; _ -> False
tests :: TestTree
tests = testsWith defaultConfig
-- | The tests with the given configuration (and its backend).
testsWith :: Config -> TestTree
testsWith base =
testGroup
"API"
[ testCase "propositional sat with model" $ do
r <- runSMT base $ do
p <- declare @'BoolS "p"
q <- declare @'BoolS "q"
assert (p .|| q)
assert (notB p)
checkSat >>= \case
Sat m -> pure (Just (eval m p, eval m q))
_ -> pure Nothing
r @?= Just (False, True)
, testCase "propositional unsat" $ do
r <- runSMT base $ do
p <- declare @'BoolS "p"
q <- declare @'BoolS "q"
assert (p .=> q)
assert p
assert (notB q)
isUnsat <$> checkSat
r @?= True
, testCase "Boolean equalities, xor and ite" $ do
r <- runSMT base $ do
a <- declare @'BoolS "a"
b <- declare @'BoolS "b"
c <- declare @'BoolS "c"
assert (a `xorB` b)
assert (ite a (b .== c) (notB c))
assert (distinct [a, c])
checkSat >>= \case
Sat m -> pure (Just (map (eval m) [a, b, c]))
_ -> pure Nothing
r @?= Just [True, False, False]
, testCase "push and pop" $ do
(r1, r2, r3) <- runSMT base $ do
p <- declare @'BoolS "p"
assert p
push
assert (notB p)
r1 <- checkSat
pop
r2 <- checkSat
r3 <- scoped (assert (p .&& notB p) >> checkSat)
pure (isUnsat r1, isSat r2, isUnsat r3)
(r1, r2, r3) @?= (True, True, True)
, testCase "duplicate declarations are rejected" $ do
r <- try $ runSMT base $ do
_ <- declare @'BoolS "p"
_ <- declare @'IntS "p"
pure ()
case r of
Left (DuplicateDeclaration "p") -> pure ()
other -> assertFailure ("unexpected: " <> show other)
, testCase "nonlinear arithmetic: unknown with the engine" $ do
r <- runSMT base $ do
x <- declare @'IntS "x"
y <- declare @'IntS "y"
assert (x * y .== 6)
-- External solvers may decide it.
(if cfgBackend base == Native then isUnknown else isSat) <$> checkSat
r @?= True
, testCase "strings" $ do
r <- runSMT base $ do
s <- declare @'StringS "s"
n <- declare @'IntS "n"
assert (strLen s .== 5)
assert (prefixOf "ab" s)
assert (strToInt (substr s 2 3) .== n)
assert (n .> 900)
m <-
checkSat >>= \case
Sat m -> pure (Just (T.take 2 (eval m s), eval m n > 900))
_ -> pure Nothing
assert (indexOf s "9" 0 .== (-1))
(,) m . isUnsat <$> checkSat
r @?= (Just ("ab", True), True)
, testCase "linear integer arithmetic" $ do
r <- runSMT base $ do
x <- declare @'IntS "x"
y <- declare @'IntS "y"
assert (x + 1 .> 3)
assert (x .< 5)
v <-
checkSat >>= \case
Sat m -> pure (Just (eval m x))
_ -> pure Nothing
assert (2 * x .== 2 * y + 1)
parity <- isUnsat <$> checkSat
pure (v, parity)
fmap (`elem` [3, 4]) (fst r) @?= Just True
snd r @?= True
, testCase "arrays" $ do
r <- runSMT base $ do
a <- declare @('ArrayS 'IntS 'IntS) "a"
b <- declare @('ArrayS 'IntS 'IntS) "b"
i <- declare @'IntS "i"
assert (b .== store a i 7)
assert (select a i .== 3)
assert (distinct [select b 0, select a 0])
m <-
checkSat >>= \case
Sat m -> pure (Just (eval m i, eval m (select b i), eval m (select a i)))
_ -> pure Nothing
-- b differs from a only at i, so b[0] /= a[0] forces i = 0.
assert (i .> 0)
(,) m . isUnsat <$> checkSat
r @?= (Just (0, 7, 3), True)
, testCase "records" $ do
r <- runSMT base $ do
p <- declare @Point "p"
q <- declare @Point "q"
assert (p ! #x .> 2)
assert (q .== setField #y (p ! #x + 1) p)
assert (q ! #y .< 5)
m <-
checkSat >>= \case
Sat m -> pure (Just (eval m (p ! #x), eval m (q ! #y), field #x (eval m q)))
_ -> pure Nothing
assert (p .== record (#x := 4 :& #y := 0 :& FNil))
(,) m . isUnsat <$> checkSat
r @?= (Just (3, 4, 3), True)
, testCase "models evaluate expressions of every theory" $ do
r <- runSMT base $ do
p <- declare @'BoolS "p"
assert p
checkSat >>= \case
Sat m -> do
let person = record (#name := "Ana" :& #age := 30 :& FNil) :: Expr ctx Person
older = setField #age (person ! #age + 1) person
arr = store (constArray @'IntS (bv @8 0)) 3 (bv 200)
pure $
Just
( eval m (2 * 3 - 10 `idiv` 3 :: Expr ctx 'IntS)
, eval m (bvExtract @7 @4 (bv @8 0xA5))
, eval m (strLen ("ab" <+> "cde"))
, eval m (select arr 3 + bv 100)
, eval m (older ! #age)
, field #name (eval m older)
)
_ -> pure Nothing
r @?= Just (3, BitVector 0xA, 5, BitVector 44, 31, "Ana")
, testCase "floating point" $ do
r <- runSMT base $ do
x <- declare @Float32S "x"
y <- declare @Float32S "y"
assert (fpEq (fpAdd rne x y) (fpValue 1))
assert (fpGt x (fpValue 0.75) .&& fpGt y (fpValue 0.125))
sums <-
checkSat >>= \case
Sat m -> pure (Just (toFloat (eval m x) + toFloat (eval m y) == 1, toFloat (eval m x) > 0.75))
_ -> pure Nothing
-- Addition is not associative in Float16.
a <- declare @Float16S "a"
b <- declare @Float16S "b"
c <- declare @Float16S "c"
let lhs = fpAdd rne (fpAdd rne a b) c
rhs = fpAdd rne a (fpAdd rne b c)
nonAssoc <- scoped $ do
assert (notB (fpEq lhs rhs) .&& notB (fpIsNaN lhs) .&& notB (fpIsNaN rhs))
checkSat >>= \case
Sat m -> pure (eval m lhs /= eval m rhs)
_ -> pure False
-- A square is never negative.
square <- scoped (assert (fpLt (fpMul rne x x) (fpZero False)) >> isUnsat <$> checkSat)
pure (sums, nonAssoc, square)
r @?= (Just (True, True), True, True)
, testCase "uninterpreted functions are declared with their domain" $ do
r <- runSMT base $ do
f <- declareFun @'[ 'IntS, 'BoolS] @'BoolS "f"
x <- declare @'IntS "x"
p <- declare @'BoolS "p"
assert (f x p .|| notB (f x p))
isSat <$> checkSat
r @?= True
]