——————————————————————————–
— DO NOT MODIFY ANYTHING IN THIS FILE —————————————–
——————————————————————————–
Copyright By PowCoder代写 加微信 powcoder
{-# LANGUAGE ScopedTypeVariables #-}
import Data.IORef
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.Runners
import System.Exit
import Control.Exception
import Language.Elsa
import System.FilePath
main :: IO ()
main = runTests [unit1]
unit1 :: Score -> TestTree
unit1 sc = testGroup “Unit 1”
(check “01_bool.lc”)
“not_false”
“bool: not_false”
(check “01_bool.lc”)
“and_false_true”
“bool: and_false_true”
(check “01_bool.lc”)
“or_true_false”
“bool: or_true_false”
(check “02_plus.lc”)
“add: suc_one”
(check “02_plus.lc”)
“add_one_zero”
“add: add_one_zero”
(check “02_plus.lc”)
“add_one_two”
“add: add_one_two”
(check “03_minus.lc”)
“skip1_false”
“skip1_false”
(check “03_minus.lc”)
“skip1_true_zero”
“skip1_true_zero”
(check “03_minus.lc”)
“skip1_true_one”
“skip1_true_one”
(check “03_minus.lc”)
“decr_zero”
“decr_zero”
(check “03_minus.lc”)
“decr_one”
“decr_one”
(check “03_minus.lc”)
“decr_two”
“decr_two”
(check “03_minus.lc”)
“sub_two_zero”
“sub_two_zero”
(check “03_minus.lc”)
“sub_two_one”
“sub_two_one”
(check “03_minus.lc”)
“sub_two_two”
“sub_two_two”
(check “03_minus.lc”)
“sub_two_three”
“sub_two_three”
(check “03_minus.lc”)
“isz_zero”
“isz_zero”
(check “03_minus.lc”)
(check “03_minus.lc”)
“eq_zero_zero”
“eq_zero_zero”
(check “03_minus.lc”)
“eq_zero_one”
“eq_zero_one”
(check “03_minus.lc”)
“eq_one_two”
“eq_one_two”
(check “03_minus.lc”)
“eq_two_two”
“eq_two_two”
mkTest :: (Show b, Eq b) => (a -> IO b) -> a -> b -> Int -> String -> TestTree
mkTest = scoreTest’ sc
——————————————————————————–
— | runElsa checks if the single eval-target `x` in file `f` is OK.
——————————————————————————–
check :: FilePath -> Id -> IO Bool
check f x = do
r <- runElsaId (testDir > f) x
return (r == Just (OK (Bind x ())))
testDir :: FilePath
testDir = “tests”
——————————————————————————–
——————————————————————————–
——————————————————————————–
type Score = IORef (Int, Int)
runTests :: [Score -> TestTree] -> IO ()
runTests groups = do
sc <- initScore
defaultMainWithIngredients (includingOptions coreOptions : defaultIngredients)
(tests sc groups) `catch` (\(e :: ExitCode) -> do
(n, tot) <- readIORef sc
putStrLn ("OVERALL SCORE = " ++ show n ++ " / "++ show tot)
throwIO e)
tests :: Score -> [Score -> TestTree] -> TestTree
tests x gs = testGroup “Tests” [ g x | g <- gs ]
--------------------------------------------------------------------------------
-- | Construct a single test case
--------------------------------------------------------------------------------
scoreTest'
:: (Show b, Eq b) => Score -> (a -> IO b) -> a -> b -> Int -> String -> TestTree
——————————————————————————–
scoreTest’ sc f x expR points name =
testCase name $ do
updateTotal sc points
actR <- f x
if actR == expR
then updateCurrent sc points
else assertFailure "Wrong Result"
updateTotal :: Score -> Int -> IO ()
updateTotal sc n = modifyIORef sc (\(x, y) -> (x, y + n))
updateCurrent :: Score -> Int -> IO ()
updateCurrent sc n = modifyIORef sc (\(x, y) -> (x + n, y))
initScore :: IO Score
initScore = newIORef (0, 0)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com