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 Data.List (find)9import Language.QBE (globalFuncs, parse)10import Language.QBE.Simulator.Concolic.State (mkEnv)11import Language.QBE.Simulator.Explorer (defSolver, explore, newEngine)12import Language.QBE.Types qualified as QBE13import System.FilePath14import Test.Tasty15import Test.Tasty.Golden.Advanced1617type Result = Int1819entryFunc :: QBE.GlobalIdent20entryFunc = QBE.GlobalIdent "entry"2122exploreQBE :: FilePath -> [(String, QBE.BaseType)] -> IO Result23exploreQBE filePath params = do24 content <- readFile filePath25 prog <- case parse filePath content of26 Right rt -> pure rt27 Left err -> fail $ "Parsing error: " ++ show err2829 let funcs = globalFuncs prog30 func <- case find (\f -> QBE.fName f == entryFunc) funcs of31 Just x -> pure x32 Nothing -> fail $ "Unable to find entry function: " ++ show entryFunc3334 engine <- newEngine <$> defSolver35 defEnv <- mkEnv prog 0 128 Nothing36 traces <-37 explore engine defEnv func $38 map (second QBE.Base) params39 pure $ length traces4041simpleCmp :: Result -> Result -> IO (Maybe String)42simpleCmp expt act =43 return $44 if expt == act45 then Nothing46 else Just ("Exploration mismatch: " ++ err)47 where48 err :: String49 err = "expected=" ++ show expt ++ " actual=" ++ show act5051runTest :: TestName -> Int -> [(String, QBE.BaseType)] -> TestTree52runTest testName expPaths params =53 goldenTest54 testName55 (pure expPaths)56 (exploreQBE fullPath params)57 simpleCmp58 (\_ -> pure ())59 where60 fullPath :: FilePath61 fullPath = "test" </> "golden" </> (testName ++ ".qbe")6263------------------------------------------------------------------------6465goldenTests :: TestTree66goldenTests =67 testGroup68 "goldenTests"69 [ runTest "three-branches" 3 [("a", QBE.Word), ("b", QBE.Word)],70 runTest "prime-numbers" 21 [("a", QBE.Word)],71 runTest "address-concretization" 2 [("a", QBE.Word)]72 ]