packages feed

hsmt-0.1.0.0: test-diff/Main.hs

-- | Differential tests of hsmt against Z3 (see section 7.1 of PLANO.md).
--
-- Z3 is located through @HSMT_Z3@ or the @PATH@; without it the suite is
-- skipped. Every divergence is shrunk and saved to @test/regressions@, which
-- the regular test suite replays without Z3.
module Main (main) where

import Data.Bits (xor)
import Data.Char (ord)
import Data.List (isPrefixOf)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Numeric (showHex)
import System.Directory (createDirectoryIfMissing)
import System.Environment (getArgs)
import System.FilePath ((</>))
import Test.QuickCheck
import Test.QuickCheck.Monadic (monadicIO, run)
import Test.Tasty
import Test.Tasty.QuickCheck

import qualified Data.Map.Strict as Map
import HSMT.Core.Sort
import HSMT.Gen.Expr
import HSMT.SMTLib.Printer

import Differential.Oracle
import Differential.Z3

main :: IO ()
main = do
  args <- getArgs
  findZ3 >>= \case
    Nothing -> do
      putStrLn "z3 not found (set HSMT_Z3 or add it to PATH): differential tests skipped"
      defaultMain (testGroup "differential (skipped: z3 not found)" [])
    Just path -> do
      let z3 = Z3 path 10 []
          -- About 1000 cases per property unless given on the command line.
          sized'
            | any ("--quickcheck-tests" `isPrefixOf`) args = id
            | otherwise = localOption (QuickCheckTests 1000)
      defaultMain . sized' $
        testGroup
          "differential vs Z3"
          [ testProperty "evaluator agrees with Z3 on ground terms" (prop_groundTerms z3 groundConfig)
          , testProperty "evaluator agrees with Z3 on floating-point ground terms" (prop_groundTerms z3 fpGroundConfig)
          , testProperty "QF_UF: results and models agree" (prop_problems z3 eufConfig)
          , testProperty "QF_UF: incremental sequences agree" (prop_incremental z3 eufConfig)
          , testProperty "QF_LIA: results and models agree" (prop_problems z3 liaConfig)
          , testProperty "QF_UFLIA: results and models agree" (prop_problems z3 ufliaConfig)
          , testProperty "QF_UFLIA: incremental sequences agree" (prop_incremental z3 ufliaConfig)
          , testProperty "QF_BV: results and models agree" (prop_problems z3 bvConfig)
          , testProperty "QF_UFBV: results and models agree" (prop_problems z3 ufbvConfig)
          , testProperty "QF_UFBV: incremental sequences agree" (prop_incremental z3 ufbvConfig)
          , testProperty "QF_AUF: results and models agree" (prop_problems z3 axConfig)
          , testProperty "QF_AUF: incremental sequences agree" (prop_incremental z3 axConfig)
          , testProperty "QF_ALIA: results and models agree" (prop_problems z3 aliaConfig)
          , testProperty "QF_ABV: results and models agree" (prop_problems z3 abvConfig)
          , testProperty "QF_ABV: incremental sequences agree" (prop_incremental z3 abvConfig)
          , testProperty "records over UF: results and models agree" (prop_problems z3 recordConfig)
          , testProperty "records over UF: incremental sequences agree" (prop_incremental z3 recordConfig)
          , testProperty "records with LIA, BV and arrays: results and models agree" (prop_problems z3 recordMixConfig)
          , testProperty "records with LIA, BV and arrays: incremental sequences agree" (prop_incremental z3 recordMixConfig)
          , testProperty "QF_S: results and models agree" (prop_problems z3 stringConfig)
          , testProperty "QF_S: incremental sequences agree" (prop_incremental z3 stringConfig)
          , testProperty "QF_SLIA: results and models agree" (prop_problems z3 sliaConfig)
          , testProperty "QF_SLIA: incremental sequences agree" (prop_incremental z3 sliaConfig)
          , testProperty "QF_S with regular expressions: results and models agree" (prop_problems z3 regexConfig)
          , testProperty "QF_S with regular expressions: incremental sequences agree" (prop_incremental z3 regexConfig)
          , testProperty "QF_SLIA with regular expressions: results and models agree" (prop_problems z3 sliaRegexConfig)
          , testProperty "QF_SLIA with regular expressions: incremental sequences agree" (prop_incremental z3 sliaRegexConfig)
          , testProperty "QF_FP: results and models agree" (prop_problems z3 fpConfig)
          , testProperty "QF_FP: incremental sequences agree" (prop_incremental z3 fpConfig)
          , testProperty "QF_UFBVFP: results and models agree" (prop_problems z3 fpMixConfig)
          , testProperty "QF_UFBVFP: incremental sequences agree" (prop_incremental z3 fpMixConfig)
          , testProperty "API, engine and Z3 backend: QF_UFLIA" (prop_backends z3 ufliaConfig)
          , testProperty "API, engine and Z3 backend: QF_AUFBV and records" (prop_backends z3 recordMixConfig)
          , testProperty "API, engine and Z3 backend: QF_SLIA with regular expressions" (prop_backends z3 sliaRegexConfig)
          , testProperty "API, engine and Z3 backend: QF_UFBVFP" (prop_backends z3 fpMixConfig)
          , testProperty "API, engine and Z3 backend: every theory" (prop_backends z3 allTheoriesConfig)
          ]

-- | Turn an outcome into a property; divergences are saved as regressions.
judge :: Outcome -> Property
judge = \case
  Agree what -> label (T.unpack what) True
  Skip why -> label ("skipped: " <> T.unpack why) True
  Mismatch why cmds expected ->
    whenFail (saveRegression (regressionText why cmds expected)) $
      counterexample (T.unpack why) False

-- | Check a case, confirming divergences with a second Z3 configuration.
check :: Z3 -> IO Outcome -> Property
check z3 act = monadicIO (judge <$> run (act >>= confirmMismatch z3))

prop_problems :: Z3 -> GenConfig -> Property
prop_problems z3 cfg =
  forAllShrinkBlind (genProblem cfg) shrinkProblem $ \as ->
    counterexample (T.unpack (renderScript (problemScript as))) $
      check z3 (checkProblem z3 as)

prop_incremental :: Z3 -> GenConfig -> Property
prop_incremental z3 cfg =
  forAllShrinkBlind (genSteps cfg) shrinkSteps $ \steps ->
    counterexample (T.unpack (renderScript (stepsScript steps))) $
      check z3 (checkSteps z3 steps)

-- | The same sequence through the API with the engine and with Z3 as
-- external backend.
prop_backends :: Z3 -> GenConfig -> Property
prop_backends z3 cfg =
  forAllShrinkBlind (genSteps cfg) shrinkSteps $ \steps ->
    counterexample (T.unpack (renderScript (stepsScript steps))) $
      check z3 (checkBackends z3 steps)

-- | Ground terms over every theory: no symbols, only literals and
-- operators. Integer division only takes non-zero literal divisors, whose
-- semantics is fully specified by SMT-LIB.
prop_groundTerms :: Z3 -> GenConfig -> Property
prop_groundTerms z3 cfg =
  forAllShrinkBlind gen shrinkExpr $ \t ->
    counterexample (T.unpack (renderExpr t)) $
      check z3 (checkGroundTerm z3 t)
  where
    gen = do
      s <- elements (BoolSort : gcSorts cfg)
      d <- chooseInt (1, 4)
      genTerm cfg (Signature Map.empty []) d s

-- | Every theory, without uninterpreted sorts.
groundConfig :: GenConfig
groundConfig = allTheoriesConfig {gcSorts = filter (not . uninterp) (gcSorts allTheoriesConfig)}
  where
    uninterp = \case
      UninterpretedSort _ -> True
      _ -> False

-- | Floating point in several formats (including Float32), with
-- bit-vectors for the conversions.
fpGroundConfig :: GenConfig
fpGroundConfig =
  fpMixConfig
    { gcSorts = [FPSort 3 3, FPSort 3 5, FPSort 4 2, FPSort 5 11, FPSort 8 24, RoundingModeSort, BVSort 4, BVSort 8, BVSort 16]
    , gcDepth = 4
    , gcFunctions = False
    }

-- | Save a regression under a name derived from its contents.
saveRegression :: T.Text -> IO ()
saveRegression txt = do
  let dir = "test" </> "regressions"
      file = dir </> ("diff-" <> showHex (fnv1a txt) "" <> ".smt2")
  createDirectoryIfMissing True dir
  T.writeFile file txt
  putStrLn ("regression saved to " <> file)

fnv1a :: T.Text -> Word
fnv1a = T.foldl' (\h c -> (h `xor` fromIntegral (ord c)) * 1099511628211) 14695981039346656037