hsmt-0.1.0.0: test/HSMT/BVSpec.hs
module HSMT.BVSpec (tests) where
import Data.Text (Text)
import Test.QuickCheck
import Test.QuickCheck.Monadic (monadicIO, run)
import qualified Test.QuickCheck.Monadic as QC
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck (testProperty)
import HSMT.Core.Eval
import HSMT.Core.Expr
import HSMT.Core.Op
import HSMT.Core.Sort
import HSMT.Core.Value
import HSMT.SMT.Engine
tests :: TestTree
tests =
testGroup
"Bit-vectors"
[ testCase "modular arithmetic" $ do
r1 <- solve [eq (App BVMul [x, lit 8 3]) (lit 8 15)]
r1 @?= "sat"
-- 2x is even, 15 is odd.
r2 <- solve [eq (App BVMul [x, lit 8 2]) (lit 8 15)]
r2 @?= "unsat"
r3 <- solve [App BVULt [x, lit 8 0]]
r3 @?= "unsat"
, testCase "division by zero follows SMT-LIB" $ do
r <- solve [App Not [eq (App BVUDiv [x, lit 8 0]) (lit 8 255)]]
r @?= "unsat"
r' <- solve [App Not [eq (App BVURem [x, lit 8 0]) x]]
r' @?= "unsat"
, testCase "congruence meets bit-vectors" $ do
let f t = App (Uninterpreted (Symbol "f" [BVSort 8] (BVSort 8))) [t]
r1 <- solve [eq x (App BVAdd [y, lit 8 1]), App Not [eq (f (App BVSub [x, lit 8 1])) (f y)]]
r1 @?= "unsat"
-- f maps three different arguments to three values, but x is 0 or 1.
r2 <- solve [App Distinct [f (lit 8 0), f (lit 8 1), f x], App BVULe [x, lit 8 1]]
r2 @?= "unsat"
r3 <- solve [App Distinct [f (lit 8 0), f (lit 8 1), f x]]
r3 @?= "sat"
, testProperty "every operator agrees with the evaluator" prop_operators
]
where
x = var "x" 8
y = var "y" 8
var :: Text -> Int -> Expr
var n w = constant (Uninterpreted (Symbol n [] (BVSort w)))
lit :: Int -> Integer -> Expr
lit w v = constant (BVConst w v)
eq :: Expr -> Expr -> Expr
eq a b = App Eq [a, b]
solve :: [Expr] -> IO String
solve as =
solveAssertions True as >>= \case
ESat _ -> pure "sat"
EUnsat _ -> pure "unsat"
EUnknown why -> pure ("unknown: " <> show why)
-- | For random operands, the circuit of each operator must produce exactly
-- the value computed by the reference evaluator. The operands are
-- variables fixed by equalities, so the circuits are not constant-folded.
prop_operators :: Property
prop_operators = forAll gen $ \(op, w, vals) -> monadicIO $ do
let names = ["a", "b"]
args = [var n w | n <- take (length vals) names]
fixes = [eq a (lit w v) | (a, v) <- zip args vals]
term = App op args
expected = either (error . show) id (evalExpr emptyInterpretation (App op [lit w v | v <- vals]))
valueTerm = case expected of
VBV w' v -> lit w' v
VBool b -> constant (BoolConst b)
other -> error (show other)
agrees <- run (solve (fixes <> [eq term valueTerm]))
differs <- run (solve (fixes <> [App Not [eq term valueTerm]]))
QC.monitor (counterexample (show (op, w, vals, expected, agrees, differs)))
QC.assert (agrees == "sat" && differs == "unsat")
where
gen = do
w <- elements [1, 2, 3, 4, 5, 8]
(op, arity) <-
elements $
[(o, 2) | o <- binary]
<> [(o, 1) | o <- [BVNot, BVNeg, BVZeroExtend 2, BVSignExtend 3]]
<> [(BVExtract (w - 1) 0, 1), (BVExtract (w - 1) (w - 1), 1), (BVConcat, 2)]
vals <- vectorOf arity (frequency [(1, elements [0, 2 ^ w - 1, 2 ^ (w - 1)]), (3, chooseInteger (0, 2 ^ w - 1))])
pure (op, w, vals)
binary =
[ BVAnd, BVOr, BVXor, BVAdd, BVSub, BVMul, BVUDiv, BVURem, BVSDiv, BVSRem, BVSMod
, BVShl, BVLShr, BVAShr, BVULt, BVULe, BVUGt, BVUGe, BVSLt, BVSLe, BVSGt, BVSGe
]