module Test.Ganeti.BasicTypes (testBasicTypes) where
import Test.QuickCheck hiding (Result)
import Test.QuickCheck.Function
import Control.Applicative
import Control.Monad
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Ganeti.BasicTypes
instance (Arbitrary a) => Arbitrary (Result a) where
arbitrary = oneof [ Bad <$> arbitrary
, Ok <$> arbitrary
]
prop_functor_id :: Result Int -> Property
prop_functor_id ri =
fmap id ri ==? ri
prop_functor_composition :: Result Int
-> Fun Int Int -> Fun Int Int -> Property
prop_functor_composition ri (Fun _ f) (Fun _ g) =
fmap (f . g) ri ==? (fmap f . fmap g) ri
prop_applicative_identity :: Result Int -> Property
prop_applicative_identity v =
pure id <*> v ==? v
prop_applicative_composition :: Result (Fun Int Int)
-> Result (Fun Int Int)
-> Result Int
-> Property
prop_applicative_composition u v w =
let u' = fmap apply u
v' = fmap apply v
in pure (.) <*> u' <*> v' <*> w ==? u' <*> (v' <*> w)
prop_applicative_homomorphism :: Fun Int Int -> Int -> Property
prop_applicative_homomorphism (Fun _ f) x =
((pure f <*> pure x)::Result Int) ==? pure (f x)
prop_applicative_interchange :: Result (Fun Int Int)
-> Int -> Property
prop_applicative_interchange f y =
let u = fmap apply f
in u <*> pure y ==? pure ($ y) <*> u
prop_applicative_functor :: Fun Int Int -> Result Int -> Property
prop_applicative_functor (Fun _ f) x =
fmap f x ==? pure f <*> x
prop_applicative_monad :: Int -> Result (Fun Int Int) -> Property
prop_applicative_monad v f =
let v' = pure v :: Result Int
f' = fmap apply f
in v' ==? return v .&&. (f' <*> v') ==? f' `ap` v'
prop_monad_laws :: Int -> Result Int
-> Fun Int (Result Int)
-> Fun Int (Result Int)
-> Property
prop_monad_laws a m (Fun _ k) (Fun _ h) =
conjoin
[ printTestCase "return a >>= k == k a" ((return a >>= k) ==? k a)
, printTestCase "m >>= return == m" ((m >>= return) ==? m)
, printTestCase "m >>= (\\x -> k x >>= h) == (m >>= k) >>= h)"
((m >>= (\x -> k x >>= h)) ==? ((m >>= k) >>= h))
]
prop_monadplus_mzero :: Result Int -> Fun Int (Result Int) -> Property
prop_monadplus_mzero v (Fun _ f) =
printTestCase "mzero >>= f = mzero" ((mzero >>= f) ==? mzero) .&&.
printTestCase "v >> mzero = mzero" (isBad (v >> mzero))
testSuite "BasicTypes"
[ 'prop_functor_id
, 'prop_functor_composition
, 'prop_applicative_identity
, 'prop_applicative_composition
, 'prop_applicative_homomorphism
, 'prop_applicative_interchange
, 'prop_applicative_functor
, 'prop_applicative_monad
, 'prop_monad_laws
, 'prop_monadplus_mzero
]