7b0dc07143402aa94435e90376ac6234cb883aa7
[darcs-mirrors/vector.git] / tests / Properties.hs
1 module Properties (tests) where
2
3 import Boilerplater
4 import Utilities
5
6 import qualified Data.Vector.Generic as V
7 import qualified Data.Vector
8 import qualified Data.Vector.Primitive
9 import qualified Data.Vector.Fusion.Stream as S
10
11 import Test.QuickCheck
12
13 import Test.Framework
14 import Test.Framework.Providers.QuickCheck
15
16 import Text.Show.Functions ()
17 import Data.List (foldl', foldl1', unfoldr, find, findIndex)
18
19 #define COMMON_CONTEXT(a, v) \
20 VANILLA_CONTEXT(a, v), VECTOR_CONTEXT(a, v)
21
22 #define VANILLA_CONTEXT(a, v) \
23 Eq a, Show a, Arbitrary a, Model a a
24
25 #define VECTOR_CONTEXT(a, v) \
26 Eq (v a), Show (v a), Arbitrary (v a), Model (v a) [a], V.Vector v a
27
28 -- TODO: implement Vector equivalents of list functions for some of the commented out properties
29
30 -- TODO: test and implement some of these other Prelude functions:
31 -- mapM *
32 -- mapM_ *
33 -- sequence
34 -- sequence_
35 -- sum *
36 -- product *
37 -- scanl *
38 -- scanl1 *
39 -- scanr *
40 -- scanr1 *
41 -- lookup *
42 -- lines
43 -- words
44 -- unlines
45 -- unwords
46 -- NB: this is an exhaustive list of all Prelude list functions that make sense for vectors.
47 -- Ones with *s are the most plausible candidates.
48
49 -- TODO: add tests for the other extra functions
50 -- IVector exports still needing tests:
51 -- copy,
52 -- slice,
53 -- (//), update, bpermute,
54 -- prescanl, prescanl',
55 -- new,
56 -- unsafeSlice, unsafeIndex,
57 -- vlength, vnew
58
59 -- TODO: test non-IVector stuff?
60
61 testSanity :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test]
62 testSanity _ = [
63 testProperty "fromList.toList == id" prop_fromList_toList,
64 testProperty "toList.fromList == id" prop_toList_fromList,
65 testProperty "unstream.stream == id" prop_unstream_stream,
66 testProperty "stream.unstream == id" prop_stream_unstream
67 ]
68 where
69 prop_fromList_toList (v :: v a) = (V.fromList . V.toList) v == v
70 prop_toList_fromList (l :: [a]) = ((V.toList :: v a -> [a]) . V.fromList) l == l
71 prop_unstream_stream (v :: v a) = (V.unstream . V.stream) v == v
72 prop_stream_unstream (s :: S.Stream a) = ((V.stream :: v a -> S.Stream a) . V.unstream) s == s
73
74 testPolymorphicFunctions :: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT(Int, v)) => v a -> [Test]
75 testPolymorphicFunctions _ = $(testProperties [
76 'prop_eq,
77
78 'prop_length, 'prop_null,
79
80 'prop_empty, 'prop_singleton, 'prop_replicate,
81 'prop_cons, 'prop_snoc, 'prop_append, 'prop_copy,
82
83 'prop_head, 'prop_last, 'prop_index,
84
85 'prop_slice, 'prop_init, 'prop_tail, 'prop_take, 'prop_drop,
86
87 'prop_accum, 'prop_write, 'prop_backpermute, 'prop_reverse,
88
89 'prop_map, 'prop_zipWith, 'prop_zipWith3,
90 'prop_filter, 'prop_takeWhile, 'prop_dropWhile,
91
92 'prop_elem, 'prop_notElem,
93 'prop_find, 'prop_findIndex,
94
95 'prop_foldl, 'prop_foldl1, 'prop_foldl', 'prop_foldl1',
96 'prop_foldr, 'prop_foldr1,
97
98 'prop_prescanl, 'prop_prescanl',
99 'prop_postscanl, 'prop_postscanl',
100 'prop_scanl, 'prop_scanl', 'prop_scanl1, 'prop_scanl1',
101
102 'prop_concatMap,
103 'prop_unfoldr
104 ])
105 where
106 -- Prelude
107 prop_eq = ((==) :: v a -> v a -> Bool) `eq2` (==)
108
109 prop_length = (V.length :: v a -> Int) `eq1` length
110 prop_null = (V.null :: v a -> Bool) `eq1` null
111
112 prop_empty = (V.empty :: v a) `eq0` []
113 prop_singleton = (V.singleton :: a -> v a) `eq1` singleton
114 prop_replicate = (V.replicate :: Int -> a -> v a) `eq2` replicate
115 prop_cons = (V.cons :: a -> v a -> v a) `eq2` (:)
116 prop_snoc = (V.snoc :: v a -> a -> v a) `eq2` snoc
117 prop_append = ((V.++) :: v a -> v a -> v a) `eq2` (++)
118 prop_copy = (V.copy :: v a -> v a) `eq1` id
119
120 prop_head = (V.head :: v a -> a) `eqNotNull1` head
121 prop_last = (V.last :: v a -> a) `eqNotNull1` last
122 prop_index = \xs i -> (i >= 0 && i < V.length xs)
123 ==> (((V.!) :: v a -> Int -> a) `eq2` (!!)) xs i
124
125
126 prop_slice = forAll arbitrary $ \xs ->
127 forAll (choose (0, V.length xs)) $ \i ->
128 forAll (choose (0, V.length xs - i)) $ \n ->
129 ((V.slice :: v a -> Int -> Int -> v a) `eq3` slice)
130 xs i n
131 prop_tail = (V.tail :: v a -> v a) `eqNotNull1` tail
132 prop_init = (V.init :: v a -> v a) `eqNotNull1` init
133 prop_take = (V.take :: Int -> v a -> v a) `eq2` take
134 prop_drop = (V.drop :: Int -> v a -> v a) `eq2` drop
135
136 prop_accum = forAll arbitrary $ \f ->
137 forAll arbitrary $ \xs ->
138 forAll (index_value_pairs (V.length xs)) $ \ps ->
139 ((V.accum :: (a -> a -> a) -> v a -> [(Int,a)] -> v a)
140 `eq3` accum) f xs ps
141 prop_write = forAll arbitrary $ \xs ->
142 forAll (index_value_pairs (V.length xs)) $ \ps ->
143 (((V.//) :: v a -> [(Int,a)] -> v a) `eq2` (//)) xs ps
144 prop_backpermute = forAll arbitrary $ \xs ->
145 forAll (indices (V.length xs)) $ \is ->
146 ((V.backpermute :: v a -> v Int -> v a) `eq2` backpermute)
147 xs (V.fromList is)
148 prop_reverse = (V.reverse :: v a -> v a) `eq1` reverse
149
150 prop_map = (V.map :: (a -> a) -> v a -> v a) `eq2` map
151 prop_zipWith = (V.zipWith :: (a -> a -> a) -> v a -> v a -> v a) `eq3` zipWith
152 prop_zipWith3 = (V.zipWith3 :: (a -> a -> a -> a) -> v a -> v a -> v a -> v a) `eq4` zipWith3
153
154 prop_filter = (V.filter :: (a -> Bool) -> v a -> v a) `eq2` filter
155 prop_takeWhile = (V.takeWhile :: (a -> Bool) -> v a -> v a) `eq2` takeWhile
156 prop_dropWhile = (V.dropWhile :: (a -> Bool) -> v a -> v a) `eq2` dropWhile
157
158 prop_elem = (V.elem :: a -> v a -> Bool) `eq2` elem
159 prop_notElem = (V.notElem :: a -> v a -> Bool) `eq2` notElem
160 prop_find = (V.find :: (a -> Bool) -> v a -> Maybe a) `eq2` find
161 prop_findIndex = (V.findIndex :: (a -> Bool) -> v a -> Maybe Int) `eq2` findIndex
162
163 prop_foldl = (V.foldl :: (a -> a -> a) -> a -> v a -> a) `eq3` foldl
164 prop_foldl1 = (V.foldl1 :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldl1
165 prop_foldl' = (V.foldl' :: (a -> a -> a) -> a -> v a -> a) `eq3` foldl'
166 prop_foldl1' = (V.foldl1' :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldl1'
167 prop_foldr = (V.foldr :: (a -> a -> a) -> a -> v a -> a) `eq3` foldr
168 prop_foldr1 = (V.foldr1 :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldr1
169
170 prop_prescanl = (V.prescanl :: (a -> a -> a) -> a -> v a -> v a) `eq3` prescanl
171 prop_prescanl' = (V.prescanl' :: (a -> a -> a) -> a -> v a -> v a) `eq3` prescanl
172 prop_postscanl = (V.postscanl :: (a -> a -> a) -> a -> v a -> v a) `eq3` postscanl
173 prop_postscanl' = (V.postscanl' :: (a -> a -> a) -> a -> v a -> v a) `eq3` postscanl
174 prop_scanl = (V.scanl :: (a -> a -> a) -> a -> v a -> v a) `eq3` scanl
175 prop_scanl' = (V.scanl' :: (a -> a -> a) -> a -> v a -> v a) `eq3` scanl
176 prop_scanl1 = (V.scanl1 :: (a -> a -> a) -> v a -> v a) `eqNotNull2` scanl1
177 prop_scanl1' = (V.scanl1' :: (a -> a -> a) -> v a -> v a) `eqNotNull2` scanl1
178
179 prop_concatMap = (V.concatMap :: (a -> v a) -> v a -> v a) `eq2` concatMap
180
181 --prop_span = (V.span :: (a -> Bool) -> v a -> (v a, v a)) `eq2` span
182 --prop_break = (V.break :: (a -> Bool) -> v a -> (v a, v a)) `eq2` break
183 --prop_splitAt = (V.splitAt :: Int -> v a -> (v a, v a)) `eq2` splitAt
184 --prop_all = (V.all :: (a -> Bool) -> v a -> Bool) `eq2` all
185 --prop_any = (V.any :: (a -> Bool) -> v a -> Bool) `eq2` any
186
187 -- Data.List
188 --prop_findIndices = V.findIndices `eq2` (findIndices :: (a -> Bool) -> v a -> v Int)
189 --prop_isPrefixOf = V.isPrefixOf `eq2` (isPrefixOf :: v a -> v a -> Bool)
190 --prop_elemIndex = V.elemIndex `eq2` (elemIndex :: a -> v a -> Maybe Int)
191 --prop_elemIndices = V.elemIndices `eq2` (elemIndices :: a -> v a -> v Int)
192 --
193 --prop_mapAccumL = eq3
194 -- (V.mapAccumL :: (X -> W -> (X,W)) -> X -> B -> (X, B))
195 -- ( mapAccumL :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
196 --
197 --prop_mapAccumR = eq3
198 -- (V.mapAccumR :: (X -> W -> (X,W)) -> X -> B -> (X, B))
199 -- ( mapAccumR :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
200
201 -- Because the vectors are strict, we need to be totally sure that the unfold eventually terminates. This
202 -- is achieved by injecting our own bit of state into the unfold - the maximum number of unfolds allowed.
203 limitUnfolds f (theirs, ours) | ours >= 0
204 , Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
205 | otherwise = Nothing
206 prop_unfoldr = ((\n f a -> V.unfoldr (limitUnfolds f) (a, n)) :: Int -> ((Int, Int) -> Maybe (a, (Int, Int))) -> (Int, Int) -> v a)
207 `eq3` (\n f a -> unfoldr (limitUnfolds f) (a, n))
208
209
210 testTuplyFunctions:: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT((a, a), v), VECTOR_CONTEXT((a, a, a), v)) => v a -> [Test]
211 testTuplyFunctions _ = $(testProperties ['prop_zip, 'prop_zip3, 'prop_unzip, 'prop_unzip3])
212 where
213 prop_zip = (V.zip :: v a -> v a -> v (a, a)) `eq2` zip
214 prop_zip3 = (V.zip3 :: v a -> v a -> v a -> v (a, a, a)) `eq3` zip3
215 prop_unzip = (V.unzip :: v (a, a) -> (v a, v a)) `eq1` unzip
216 prop_unzip3 = (V.unzip3 :: v (a, a, a) -> (v a, v a, v a)) `eq1` unzip3
217
218 testOrdFunctions :: forall a v. (COMMON_CONTEXT(a, v), Ord a, Ord (v a)) => v a -> [Test]
219 testOrdFunctions _ = $(testProperties ['prop_compare, 'prop_maximum, 'prop_minimum])
220 where
221 prop_compare = (compare :: v a -> v a -> Ordering) `eq2` compare
222 prop_maximum = (V.maximum :: v a -> a) `eqNotNull1` maximum
223 prop_minimum = (V.minimum :: v a -> a) `eqNotNull1` minimum
224
225 testEnumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Enum a) => v a -> [Test]
226 testEnumFunctions _ = $(testProperties ['prop_enumFromTo, 'prop_enumFromThenTo])
227 where
228 prop_enumFromTo = (V.enumFromTo :: a -> a -> v a) `eq2` enumFromTo
229 prop_enumFromThenTo = \i j n -> fromEnum i < fromEnum j ==> ((V.enumFromThenTo :: a -> a -> a -> v a) `eq3` enumFromThenTo) i j n
230
231 testBoolFunctions :: forall v. (COMMON_CONTEXT(Bool, v)) => v Bool -> [Test]
232 testBoolFunctions _ = $(testProperties ['prop_and, 'prop_or])
233 where
234 prop_and = (V.and :: v Bool -> Bool) `eq1` and
235 prop_or = (V.or :: v Bool -> Bool) `eq1` or
236
237 testNumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Num a) => v a -> [Test]
238 testNumFunctions _ = $(testProperties ['prop_sum, 'prop_product])
239 where
240 prop_sum = (V.sum :: v a -> a) `eq1` sum
241 prop_product = (V.product :: v a -> a) `eq1` product
242
243 testNestedVectorFunctions :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test]
244 testNestedVectorFunctions _ = $(testProperties [])
245 where
246 -- Prelude
247 --prop_concat = (V.concat :: [v a] -> v a) `eq1` concat
248
249 -- Data.List
250 --prop_transpose = V.transpose `eq1` (transpose :: [v a] -> [v a])
251 --prop_group = V.group `eq1` (group :: v a -> [v a])
252 --prop_inits = V.inits `eq1` (inits :: v a -> [v a])
253 --prop_tails = V.tails `eq1` (tails :: v a -> [v a])
254
255
256 testGeneralBoxedVector dummy = concatMap ($ dummy) [
257 testSanity,
258 testPolymorphicFunctions,
259 testOrdFunctions,
260 testEnumFunctions,
261 testTuplyFunctions,
262 testNestedVectorFunctions
263 ]
264
265 testBoolBoxedVector dummy = testGeneralBoxedVector dummy ++ testBoolFunctions dummy
266 testNumericBoxedVector dummy = testGeneralBoxedVector dummy ++ testNumFunctions dummy
267
268 testGeneralPrimitiveVector dummy = concatMap ($ dummy) [
269 testSanity,
270 testPolymorphicFunctions,
271 testOrdFunctions,
272 testEnumFunctions
273 ]
274
275 testBoolPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testBoolFunctions dummy
276 testNumericPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testNumFunctions dummy
277
278 tests = [
279 testGroup "Data.Vector.Vector (Bool)" (testBoolBoxedVector (undefined :: Data.Vector.Vector Bool)),
280 testGroup "Data.Vector.Vector (Int)" (testNumericBoxedVector (undefined :: Data.Vector.Vector Int)),
281 testGroup "Data.Vector.Primitive.Vector (Int)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Int)),
282 testGroup "Data.Vector.Primitive.Vector (Float)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Float)),
283 testGroup "Data.Vector.Primitive.Vector (Double)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Double))
284 ]