1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
{-# language TemplateHaskell, GADTs #-}
import Data.Function.Memoize
import Control.Monad (forM_, when)
import Test3Helper
-- NonstandardParams is defined by:
--
-- data NonstandardParams a b
-- = NonstandardParams (a -> Bool) b
--
-- This won’t compile because it needs addition typeclass constraints in
-- the instance context:
--
-- $(deriveMemoizable ''NonstandardParams)
instance (Eq a, Enum a, Bounded a, Memoizable b) => Memoizable (NonstandardParams a b) where
memoize = $(deriveMemoize ''NonstandardParams)
applyToLength :: NonstandardParams Bool Int -> Bool
applyToLength (NonstandardParams f z) = f (odd z)
cases = [ (NonstandardParams id 5, True)
, (NonstandardParams id 6, False)
, (NonstandardParams not 5, False)
, (NonstandardParams not 6, True)
]
main :: IO ()
main = do
let memoized = memoize applyToLength
forM_ cases $ \(input, expected) -> do
let actual = applyToLength input
when (actual /= expected) $
fail $ "Test failed: got " ++ show actual ++
" when " ++ show expected ++ " expected."
|