Revision 7ddd8e4c test/hs/Test/Ganeti/BasicTypes.hs
b/test/hs/Test/Ganeti/BasicTypes.hs | ||
---|---|---|
7 | 7 |
|
8 | 8 |
{- |
9 | 9 |
|
10 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |
|
10 |
Copyright (C) 2009, 2010, 2011, 2012, 2013 Google Inc.
|
|
11 | 11 |
|
12 | 12 |
This program is free software; you can redistribute it and/or modify |
13 | 13 |
it under the terms of the GNU General Public License as published by |
... | ... | |
56 | 56 |
|
57 | 57 |
-- * Test cases |
58 | 58 |
|
59 |
-- | Tests the functor identity law (fmap id == id). |
|
59 |
-- | Tests the functor identity law: |
|
60 |
-- |
|
61 |
-- > fmap id == id |
|
60 | 62 |
prop_functor_id :: Result Int -> Property |
61 | 63 |
prop_functor_id ri = |
62 | 64 |
fmap id ri ==? ri |
63 | 65 |
|
64 |
-- | Tests the functor composition law (fmap (f . g) == fmap f . fmap g). |
|
66 |
-- | Tests the functor composition law: |
|
67 |
-- |
|
68 |
-- > fmap (f . g) == fmap f . fmap g |
|
65 | 69 |
prop_functor_composition :: Result Int |
66 | 70 |
-> Fun Int Int -> Fun Int Int -> Property |
67 | 71 |
prop_functor_composition ri (Fun _ f) (Fun _ g) = |
68 | 72 |
fmap (f . g) ri ==? (fmap f . fmap g) ri |
69 | 73 |
|
70 |
-- | Tests the applicative identity law (pure id <*> v = v). |
|
74 |
-- | Tests the applicative identity law: |
|
75 |
-- |
|
76 |
-- > pure id <*> v = v |
|
71 | 77 |
prop_applicative_identity :: Result Int -> Property |
72 | 78 |
prop_applicative_identity v = |
73 | 79 |
pure id <*> v ==? v |
74 | 80 |
|
75 |
-- | Tests the applicative composition law (pure (.) <*> u <*> v <*> w |
|
76 |
-- = u <*> (v <*> w)). |
|
81 |
-- | Tests the applicative composition law: |
|
82 |
-- |
|
83 |
-- > pure (.) <*> u <*> v <*> w = u <*> (v <*> w) |
|
77 | 84 |
prop_applicative_composition :: Result (Fun Int Int) |
78 | 85 |
-> Result (Fun Int Int) |
79 | 86 |
-> Result Int |
... | ... | |
83 | 90 |
v' = fmap apply v |
84 | 91 |
in pure (.) <*> u' <*> v' <*> w ==? u' <*> (v' <*> w) |
85 | 92 |
|
86 |
-- | Tests the applicative homomorphism law (pure f <*> pure x = pure (f x)). |
|
93 |
-- | Tests the applicative homomorphism law: |
|
94 |
-- |
|
95 |
-- > pure f <*> pure x = pure (f x) |
|
87 | 96 |
prop_applicative_homomorphism :: Fun Int Int -> Int -> Property |
88 | 97 |
prop_applicative_homomorphism (Fun _ f) x = |
89 | 98 |
((pure f <*> pure x)::Result Int) ==? pure (f x) |
90 | 99 |
|
91 |
-- | Tests the applicative interchange law (u <*> pure y = pure ($ y) <*> u). |
|
100 |
-- | Tests the applicative interchange law: |
|
101 |
-- |
|
102 |
-- > u <*> pure y = pure ($ y) <*> u |
|
92 | 103 |
prop_applicative_interchange :: Result (Fun Int Int) |
93 | 104 |
-> Int -> Property |
94 | 105 |
prop_applicative_interchange f y = |
95 | 106 |
let u = fmap apply f -- need to extract the actual function from Fun |
96 | 107 |
in u <*> pure y ==? pure ($ y) <*> u |
97 | 108 |
|
98 |
-- | Tests the applicative\/functor correspondence (fmap f x = pure f <*> x). |
|
109 |
-- | Tests the applicative\/functor correspondence: |
|
110 |
-- |
|
111 |
-- > fmap f x = pure f <*> x |
|
99 | 112 |
prop_applicative_functor :: Fun Int Int -> Result Int -> Property |
100 | 113 |
prop_applicative_functor (Fun _ f) x = |
101 | 114 |
fmap f x ==? pure f <*> x |
102 | 115 |
|
103 |
-- | Tests the applicative\/monad correspondence (pure = return and |
|
104 |
-- (<*>) = ap). |
|
116 |
-- | Tests the applicative\/monad correspondence: |
|
117 |
-- |
|
118 |
-- > pure = return |
|
119 |
-- |
|
120 |
-- > (<*>) = ap |
|
105 | 121 |
prop_applicative_monad :: Int -> Result (Fun Int Int) -> Property |
106 | 122 |
prop_applicative_monad v f = |
107 | 123 |
let v' = pure v :: Result Int |
108 | 124 |
f' = fmap apply f -- need to extract the actual function from Fun |
109 | 125 |
in v' ==? return v .&&. (f' <*> v') ==? f' `ap` v' |
110 | 126 |
|
111 |
-- | Tests the monad laws (return a >>= k == k a, m >>= return == m, m |
|
112 |
-- >>= (\x -> k x >>= h) == (m >>= k) >>= h). |
|
127 |
-- | Tests the monad laws: |
|
128 |
-- |
|
129 |
-- > return a >>= k == k a |
|
130 |
-- |
|
131 |
-- > m >>= return == m |
|
132 |
-- |
|
133 |
-- > m >>= (\x -> k x >>= h) == (m >>= k) >>= h |
|
113 | 134 |
prop_monad_laws :: Int -> Result Int |
114 | 135 |
-> Fun Int (Result Int) |
115 | 136 |
-> Fun Int (Result Int) |
... | ... | |
122 | 143 |
((m >>= (\x -> k x >>= h)) ==? ((m >>= k) >>= h)) |
123 | 144 |
] |
124 | 145 |
|
125 |
-- | Tests the monad plus laws ( mzero >>= f = mzero, v >> mzero = mzero). |
|
146 |
-- | Tests the monad plus laws: |
|
147 |
-- |
|
148 |
-- > mzero >>= f = mzero |
|
149 |
-- |
|
150 |
-- > v >> mzero = mzero |
|
126 | 151 |
prop_monadplus_mzero :: Result Int -> Fun Int (Result Int) -> Property |
127 | 152 |
prop_monadplus_mzero v (Fun _ f) = |
128 | 153 |
printTestCase "mzero >>= f = mzero" ((mzero >>= f) ==? mzero) .&&. |
Also available in: Unified diff