packages feed

hsmt-0.1.0.0: test/HSMT/RegressionSpec.hs

-- | Replays the SMT-LIB files in @test/regressions@ and the benchmarks in
-- @bench/smtlib@ ("HSMT.Benchmarks").
--
-- Regressions Each file states the
-- expected answers of its @check-sat@ commands in a comment line of the form
-- @; expected: sat unsat ...@; an expected @unknown@ accepts any answer.
--
-- A file containing the line @; allow-unknown@ also accepts @unknown@ from
-- hsmt; it is meant for regressions over theories without a decision
-- procedure yet. Wrong definite answers always fail.
module HSMT.RegressionSpec (tests, testsWith) where

import Data.IORef
import Data.List (isPrefixOf, isSuffixOf, sort)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import System.Directory (doesDirectoryExist, listDirectory)
import System.FilePath ((</>))
import Test.Tasty
import Test.Tasty.HUnit

import HSMT.Backend
import HSMT.Benchmarks
import HSMT.SMT.Engine
import HSMT.SMTLib.Run

regressionDir :: FilePath
regressionDir = "test" </> "regressions"

tests :: IO TestTree
tests = testsWith Native

-- | The files, decided by the given backend. With an external solver,
-- the regressions named @z3-*@ (errors of Z3 found by the differential
-- tests) are skipped.
testsWith :: Backend -> IO TestTree
testsWith backend = do
  exists <- doesDirectoryExist regressionDir
  files <- if exists then sort . filter (".smt2" `isSuffixOf`) <$> listDirectory regressionDir else pure []
  benches <- benchmarkFiles
  let keep f = backend == Native || not ("z3-" `isPrefixOf` f)
  pure $
    testGroup
      "SMT-LIB files"
      [ testGroup "regressions" (map (regression backend) (filter keep files))
      , testGroup "benchmarks" (map (benchmark backend) benches)
      ]

-- | A benchmark must give the annotated answers. Files in
-- 'knownLimitations' may answer @unknown@, within a time limit.
benchmark :: Backend -> FilePath -> TestTree
benchmark backend file = testCase file $ do
  txt <- T.readFile (benchmarkDir </> file)
  out <- newIORef []
  let limited = file `elem` map fst knownLimitations
      opts = defaultCheckOptions {coValidate = True, coTimeout = if limited then Just 2 else Nothing}
  runScriptWithBackend backend opts (\r -> modifyIORef' out (r :)) txt
  responses <- reverse <$> readIORef out
  case [e | CommandError e <- responses] of
    e : _ -> assertFailure ("script error: " <> T.unpack e)
    [] -> pure ()
  let answers = [answerText r | CheckResult r <- responses]
      expected = expectedStatuses txt
  length answers @?= length expected
  sequence_
    [ assertBool ("check-sat #" <> show i <> ": expected " <> T.unpack e <> ", got " <> T.unpack a) $
        a == e || (limited && a == "unknown")
    | (i, e, a) <- zip3 [1 :: Int ..] expected answers
    ]

regression :: Backend -> FilePath -> TestTree
regression backend file = testCase file $ do
  txt <- T.readFile (regressionDir </> file)
  expected <- case [T.words rest | l <- T.lines txt, Just rest <- [T.stripPrefix "; expected:" l]] of
    [ws] -> pure ws
    _ -> assertFailure "missing or duplicated '; expected:' line"
  let allowUnknown = "; allow-unknown" `elem` map T.strip (T.lines txt)
  responses <- runScriptResponsesWith backend True txt
  case [e | CommandError e <- responses] of
    err : _ -> assertFailure ("script error: " <> T.unpack err)
    [] -> do
      let results = [r | CheckResult r <- responses]
      length results @?= length expected
      sequence_
        [ assertBool ("check-sat #" <> show i <> ": expected " <> T.unpack e <> ", got " <> T.unpack (answer r)) $
            e == "unknown" || e == answer r || (allowUnknown && answer r == "unknown")
        | (i, e, r) <- zip3 [1 :: Int ..] expected results
        ]
  where
    answer = \case
      ESat _ -> "sat"
      EUnsat _ -> "unsat"
      EUnknown _ -> "unknown"