packages feed

hsmt-0.1.0.0: test/HSMT/StringSpec.hs

module HSMT.StringSpec (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
    "Strings"
    [ testCase "concatenation and length" $ do
        expect (base <> ["(assert (= (str.++ x \"b\") (str.++ \"a\" y)))", "(assert (> (str.len x) 2))"]) ["sat"]
        expect (base <> ["(assert (= (str.++ x y) \"abc\"))", "(assert (= (str.len x) (str.len y)))"]) ["unsat"]
        -- Lengths alone refute it, for strings of any length.
        expect (base <> ["(assert (= (str.++ x x) (str.++ y \"a\" y)))"]) ["unsat"]
        expect (base <> ["(assert (= (str.len (str.++ x y)) 20))"]) ["sat"]
    , testCase "substrings and characters" $ do
        expect (base <> ["(assert (= (str.substr x 1 2) \"bc\"))", "(assert (= (str.at x 0) \"a\"))", "(assert (= (str.len x) 3))"]) ["sat"]
        expect (base <> ["(assert (= x \"abc\"))", "(assert (not (= (str.substr x 1 5) \"bc\")))"]) ["unsat"]
        expect (base <> ["(assert (= x \"abc\"))", "(assert (not (= (str.at x 3) \"\")))"]) ["unsat"]
        expect (base <> ["(assert (not (= (str.substr x (- 1) 2) \"\")))"]) ["unsat"]
    , testCase "search" $ do
        expect (base <> ["(assert (str.contains x \"ab\"))", "(assert (str.prefixof \"b\" x))", "(assert (str.suffixof \"a\" x))"]) ["sat"]
        expect (base <> ["(assert (str.contains x \"ab\"))", "(assert (< (str.len x) 2))"]) ["unsat"]
        expect (base <> ["(assert (= (str.indexof x \"a\" 0) 2))", "(assert (= (str.at x 1) \"a\"))"]) ["unsat"]
        expect (base <> ["(assert (= (str.indexof \"aba\" x 1) 2))"]) ["sat"]
        expect (base <> ["(assert (= (str.replace x \"ab\" \"c\") \"xcy\"))", "(assert (= (str.substr x 1 2) \"aa\"))"]) ["unsat"]
    , testCase "conversions" $ do
        expect (base <> ["(assert (= (str.to_int x) 123))", "(assert (= (str.len x) 5))"]) ["sat"]
        expect (base <> ["(assert (= (str.from_int n) \"012\"))"]) ["unsat"]
        expect (base <> ["(assert (= (str.from_int n) x))", "(assert (str.prefixof \"4\" x))", "(assert (< n 50))", "(assert (> n 40))"]) ["sat"]
        expect (base <> ["(assert (= (str.to_int x) (- 2)))"]) ["unsat"]
    , testCase "lexicographic order" $ do
        expect (base <> ["(assert (str.< x y))", "(assert (str.< y \"b\"))", "(assert (str.prefixof \"a\" x))"]) ["sat"]
        expect (base <> ["(assert (str.< x y))", "(assert (str.< y x))"]) ["unsat"]
        expect (base <> ["(assert (str.<= x \"\"))", "(assert (not (= x \"\")))"]) ["unsat"]
    , testCase "uninterpreted functions over strings" $ do
        let funs = base <> ["(declare-fun f (String) Int)"]
        expect (funs <> ["(assert (= x (str.++ \"a\" y)))", "(assert (= y \"b\"))", "(assert (distinct (f x) (f \"ab\")))"]) ["unsat"]
        expect (funs <> ["(assert (distinct (f x) (f y)))", "(assert (= (str.len x) (str.len y)))"]) ["sat"]
    , testCase "regular expressions" $ do
        let digits = "(re.+ (re.range \"0\" \"9\"))"
        expect (base <> ["(assert (str.in_re x (re.++ (str.to_re \"id\") " <> digits <> ")))", "(assert (> (str.len x) 4))"]) ["sat"]
        -- (ab)* has only even lengths.
        expect (base <> ["(assert (str.in_re x (re.* (str.to_re \"ab\"))))", "(assert (= (str.len x) 7))"]) ["unsat"]
        expect (base <> ["(assert (str.in_re x (re.* (str.to_re \"ab\"))))", "(assert (str.contains x \"bb\"))", "(assert (< (str.len x) 20))"]) ["unsat"]
        -- Without the length limit the refutation needs strings longer than
        -- the bound: the occurrence of bb may lie past it.
        r0 <- run (base <> ["(assert (str.in_re x (re.* (str.to_re \"ab\"))))", "(assert (str.contains x \"bb\"))"])
        r0 @?= ["unknown: string length bound reached"]
        -- Complement and intersection.
        expect (base <> ["(assert (str.in_re x (re.inter (re.comp (re.++ re.all (str.to_re \"a\") re.all)) ((_ re.^ 3) re.allchar))))", "(assert (str.prefixof \"b\" x))"]) ["sat"]
        expect (base <> ["(assert (str.in_re x (re.diff (re.* (re.range \"a\" \"b\")) (re.* (str.to_re \"a\")))))", "(assert (not (str.contains x \"b\")))", "(assert (<= (str.len x) 8))"]) ["unsat"]
        -- Membership is exact for strings in the bound, also when negated.
        expect (base <> ["(assert (not (str.in_re x ((_ re.loop 1 3) (str.to_re \"a\")))))", "(assert (str.in_re x (re.+ (str.to_re \"a\"))))", "(assert (< (str.len x) 4))"]) ["unsat"]
        -- Relations between the languages hold for any length: the last
        -- character cannot be both a and b.
        expect (base <> ["(assert (str.in_re x (re.++ re.all (str.to_re \"a\"))))", "(assert (str.in_re x (re.++ re.all (str.to_re \"b\"))))"]) ["unsat"]
        expect (base <> ["(assert (str.in_re x (re.* (str.to_re \"ab\"))))", "(assert (not (str.in_re x (re.* (re.range \"a\" \"b\")))))"]) ["unsat"]
        -- The shortest member is longer than the bound.
        expect (base <> ["(assert (str.in_re x ((_ re.^ 40) (str.to_re \"a\"))))", "(assert (str.in_re x (re.* (str.to_re \"b\"))))"]) ["unsat"]
        -- Ground regular expressions are evaluated, including equalities.
        expect ["(assert (= (re.* (re.* (str.to_re \"a\"))) (re.* (str.to_re \"a\"))))"] ["sat"]
        expect ["(assert (str.in_re \"aab\" (re.++ (re.* (str.to_re \"a\")) (str.to_re \"c\"))))"] ["unsat"]
        -- Regular expressions that depend on variables are not supported.
        r <- run (base <> ["(assert (str.in_re x (re.* (str.to_re y))))"])
        r @?= ["unknown: regular expressions must be ground (str.to_re of string literals)"]
    , testCase "beyond the length bound" $ do
        -- x ++ "a" = "b" ++ x has no solution of any length; bounded
        -- reasoning cannot refute it.
        r <- run (base <> ["(assert (= (str.++ x \"a\") (str.++ \"b\" x)))"])
        r @?= ["unknown: string length bound reached"]
    ]
  where
    base = ["(declare-const x String)", "(declare-const y String)", "(declare-const n Int)"]

-- | Run a script (with model validation) and compare its answers; a final
-- @check-sat@ is appended.
expect :: [Text] -> [Text] -> Assertion
expect cmds answers = run cmds >>= (@?= answers)

run :: [Text] -> IO [Text]
run cmds =
  runScriptText True (T.unlines (cmds <> ["(check-sat)"])) >>= \case
    Left err -> assertFailure (T.unpack err) >> pure []
    Right rs -> pure (map answer rs)
  where
    answer = \case
      ESat _ -> "sat"
      EUnsat _ -> "unsat"
      EUnknown why -> "unknown: " <> why