Haxl/tests/MemoizationTests.hs
Andrew Farmer 8a69ddcc0d Import Control.Applicative in MemoizationTests for GHC < 7.10
Summary: GHC prior to 7.10 didn't export <$> in the prelude.

Reviewed By: watashi

Differential Revision: D3659220

fbshipit-source-id: af95574bc33f593fe0deb9217a963a7d9d9b0a16
2016-08-02 20:16:33 -07:00

75 lines
1.9 KiB
Haskell

{-# LANGUAGE CPP #-}
module MemoizationTests (tests) where
#if __GLASGOW_HASKELL__ < 710
import Control.Applicative
#endif
import Data.IORef
import Test.HUnit
import Haxl.Core
import Haxl.Core.Monad
import ExampleDataSource
memoSoundness :: Test
memoSoundness = TestCase $ do
iEnv <- do
exState <- ExampleDataSource.initGlobalState
initEnv (stateSet exState stateEmpty) ()
unMemoizedWombats <- runHaxl iEnv $ listWombats 100
(initialGet, subsequentGet) <- runHaxl iEnv $ do
wombatsMemo <- newMemoWith (listWombats 100)
let memoizedWombats = runMemo wombatsMemo
initialGet <- memoizedWombats
subsequentGet <- memoizedWombats
return (initialGet, subsequentGet)
assertBool "Memo Soundness 1" $ initialGet == unMemoizedWombats
assertBool "Memo Soundness 2" $ subsequentGet == unMemoizedWombats
let impure runCounterRef = unsafeLiftIO $ do
modifyIORef runCounterRef succ
readIORef runCounterRef
initialRunCounter = 0 :: Int
runCounterRef <- newIORef initialRunCounter
(initialImpureGet, subsequentImpureGet) <- runHaxl iEnv $ do
impureMemo <- newMemoWith (impure runCounterRef)
let memoizedImpure = runMemo impureMemo
initialImpureGet <- memoizedImpure
subsequentImpureGet <- memoizedImpure
return (initialImpureGet, subsequentImpureGet)
assertBool "Memo Soundness 3" $ initialImpureGet == succ initialRunCounter
assertBool "Memo Soundness 4" $ subsequentImpureGet == initialImpureGet
let fMemoVal = 42 :: Int
dependentResult <- runHaxl iEnv $ do
fMemoRef <- newMemo
gMemoRef <- newMemo
let f = runMemo fMemoRef
g = runMemo gMemoRef
prepareMemo fMemoRef $ return fMemoVal
prepareMemo gMemoRef $ succ <$> f
a <- f
b <- g
return (a + b)
assertBool "Memo Soundness 5" $ dependentResult == fMemoVal + succ fMemoVal
tests = TestList [TestLabel "Memo Soundness" memoSoundness]