File: test3.hs

package info (click to toggle)
haskell-memoize 1.1.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 108 kB
  • sloc: haskell: 401; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 1,093 bytes parent folder | download | duplicates (2)
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."