1-- SPDX-FileCopyrightText: 2025 Sören Tempel <soeren+git@soeren-tempel.net>2--3-- SPDX-License-Identifier: GPL-3.0-only45module Golden (goldenTests) where67import Data.Bifunctor (second)8import Language.QBE (parseAndFind)9import Language.QBE.Simulator.Concolic.State (mkEnv)10import Language.QBE.Simulator.Explorer (defSolver, explore, newEngine)11import Language.QBE.Types qualified as QBE12import System.FilePath13import Test.Tasty14import Test.Tasty.Golden.Advanced1516type Result = Int1718entryFunc :: QBE.GlobalIdent19entryFunc = QBE.GlobalIdent "entry"2021exploreQBE :: FilePath -> [(String, QBE.BaseType)] -> IO Result22exploreQBE filePath params = do23 (prog, func) <- readFile filePath >>= parseAndFind entryFunc2425 engine <- newEngine <$> defSolver26 defEnv <- mkEnv prog 0 128 Nothing2728 traces <-29 explore engine defEnv func $30 map (second QBE.Base) params31 pure $ length traces3233simpleCmp :: Result -> Result -> IO (Maybe String)34simpleCmp expt act =35 return $36 if expt == act37 then Nothing38 else Just ("Exploration mismatch: " ++ err)39 where40 err :: String41 err = "expected=" ++ show expt ++ " actual=" ++ show act4243runTest :: TestName -> Int -> [(String, QBE.BaseType)] -> TestTree44runTest testName expPaths params =45 goldenTest46 testName47 (pure expPaths)48 (exploreQBE fullPath params)49 simpleCmp50 (\_ -> pure ())51 where52 fullPath :: FilePath53 fullPath = "test" </> "golden" </> (testName ++ ".qbe")5455------------------------------------------------------------------------5657goldenTests :: TestTree58goldenTests =59 testGroup60 "goldenTests"61 [ runTest "three-branches" 3 [("a", QBE.Word), ("b", QBE.Word)],62 runTest "prime-numbers" 21 [("a", QBE.Word)],63 runTest "address-concretization" 2 [("a", QBE.Word)]64 ]