Tests of minIndex and maxIndex
[darcs-mirrors/vector.git] / tests / Tests / Vector.hs
1 module Tests.Vector (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.Storable
10 import qualified Data.Vector.Fusion.Stream as S
11
12 import Test.QuickCheck
13
14 import Test.Framework
15 import Test.Framework.Providers.QuickCheck2
16
17 import Text.Show.Functions ()
18 import Data.List
19 import System.Random (Random)
20
21 #define COMMON_CONTEXT(a, v) \
22 VANILLA_CONTEXT(a, v), VECTOR_CONTEXT(a, v)
23
24 #define VANILLA_CONTEXT(a, v) \
25 Eq a, Show a, Arbitrary a, CoArbitrary a, TestData a, Model a ~ a, EqTest a ~ Property
26
27 #define VECTOR_CONTEXT(a, v) \
28 Eq (v a), Show (v a), Arbitrary (v a), CoArbitrary (v a), TestData (v a), Model (v a) ~ [a], EqTest (v a) ~ Property, V.Vector v a
29
30 -- TODO: implement Vector equivalents of list functions for some of the commented out properties
31
32 -- TODO: test and implement some of these other Prelude functions:
33 -- mapM *
34 -- mapM_ *
35 -- sequence
36 -- sequence_
37 -- sum *
38 -- product *
39 -- scanl *
40 -- scanl1 *
41 -- scanr *
42 -- scanr1 *
43 -- lookup *
44 -- lines
45 -- words
46 -- unlines
47 -- unwords
48 -- NB: this is an exhaustive list of all Prelude list functions that make sense for vectors.
49 -- Ones with *s are the most plausible candidates.
50
51 -- TODO: add tests for the other extra functions
52 -- IVector exports still needing tests:
53 -- copy,
54 -- slice,
55 -- (//), update, bpermute,
56 -- prescanl, prescanl',
57 -- new,
58 -- unsafeSlice, unsafeIndex,
59 -- vlength, vnew
60
61 -- TODO: test non-IVector stuff?
62
63 testSanity :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test]
64 testSanity _ = [
65 testProperty "fromList.toList == id" prop_fromList_toList,
66 testProperty "toList.fromList == id" prop_toList_fromList,
67 testProperty "unstream.stream == id" prop_unstream_stream,
68 testProperty "stream.unstream == id" prop_stream_unstream
69 ]
70 where
71 prop_fromList_toList (v :: v a) = (V.fromList . V.toList) v == v
72 prop_toList_fromList (l :: [a]) = ((V.toList :: v a -> [a]) . V.fromList) l == l
73 prop_unstream_stream (v :: v a) = (V.unstream . V.stream) v == v
74 prop_stream_unstream (s :: S.Stream a) = ((V.stream :: v a -> S.Stream a) . V.unstream) s == s
75
76 testPolymorphicFunctions :: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT(Int, v)) => v a -> [Test]
77 testPolymorphicFunctions _ = $(testProperties [
78 'prop_eq,
79
80 'prop_length, 'prop_null,
81
82 'prop_empty, 'prop_singleton, 'prop_replicate,
83 'prop_cons, 'prop_snoc, 'prop_append, 'prop_copy, 'prop_generate,
84
85 'prop_head, 'prop_last, 'prop_index,
86 'prop_unsafeHead, 'prop_unsafeLast, 'prop_unsafeIndex,
87
88 'prop_slice, 'prop_init, 'prop_tail, 'prop_take, 'prop_drop,
89
90 'prop_accum, 'prop_upd, 'prop_backpermute, 'prop_reverse,
91
92 'prop_map, 'prop_zipWith, 'prop_zipWith3,
93 'prop_imap, 'prop_izipWith, 'prop_izipWith3,
94
95 'prop_filter, 'prop_ifilter, 'prop_takeWhile, 'prop_dropWhile,
96 'prop_span, 'prop_break,
97
98 'prop_elem, 'prop_notElem,
99 'prop_find, 'prop_findIndex, 'prop_findIndices,
100 'prop_elemIndex, 'prop_elemIndices,
101
102 'prop_foldl, 'prop_foldl1, 'prop_foldl', 'prop_foldl1',
103 'prop_foldr, 'prop_foldr1,
104 'prop_ifoldl, 'prop_ifoldl', 'prop_ifoldr,
105
106 'prop_all, 'prop_any,
107
108 'prop_prescanl, 'prop_prescanl',
109 'prop_postscanl, 'prop_postscanl',
110 'prop_scanl, 'prop_scanl', 'prop_scanl1, 'prop_scanl1',
111
112 'prop_prescanr, 'prop_prescanr',
113 'prop_postscanr, 'prop_postscanr',
114 'prop_scanr, 'prop_scanr', 'prop_scanr1, 'prop_scanr1',
115
116 'prop_concatMap,
117 'prop_unfoldr
118 ])
119 where
120 -- Prelude
121 prop_eq :: P (v a -> v a -> Bool) = (==) `eq` (==)
122
123 prop_length :: P (v a -> Int) = V.length `eq` length
124 prop_null :: P (v a -> Bool) = V.null `eq` null
125
126 prop_empty :: P (v a) = V.empty `eq` []
127 prop_singleton :: P (a -> v a) = V.singleton `eq` singleton
128 prop_replicate :: P (Int -> a -> v a)
129 = (\n _ -> n < 1000) ===> V.replicate `eq` replicate
130 prop_cons :: P (a -> v a -> v a) = V.cons `eq` (:)
131 prop_snoc :: P (v a -> a -> v a) = V.snoc `eq` snoc
132 prop_append :: P (v a -> v a -> v a) = (V.++) `eq` (++)
133 prop_copy :: P (v a -> v a) = V.copy `eq` id
134 prop_generate :: P (Int -> (Int -> a) -> v a)
135 = (\n _ -> n < 1000) ===> V.generate `eq` generate
136
137 prop_head :: P (v a -> a) = not . V.null ===> V.head `eq` head
138 prop_last :: P (v a -> a) = not . V.null ===> V.last `eq` last
139 prop_index = \xs ->
140 not (V.null xs) ==>
141 forAll (choose (0, V.length xs-1)) $ \i ->
142 unP prop xs i
143 where
144 prop :: P (v a -> Int -> a) = (V.!) `eq` (!!)
145 prop_unsafeHead :: P (v a -> a) = not . V.null ===> V.unsafeHead `eq` head
146 prop_unsafeLast :: P (v a -> a) = not . V.null ===> V.unsafeLast `eq` last
147 prop_unsafeIndex = \xs ->
148 not (V.null xs) ==>
149 forAll (choose (0, V.length xs-1)) $ \i ->
150 unP prop xs i
151 where
152 prop :: P (v a -> Int -> a) = V.unsafeIndex `eq` (!!)
153
154 prop_slice = \xs ->
155 forAll (choose (0, V.length xs)) $ \i ->
156 forAll (choose (0, V.length xs - i)) $ \n ->
157 unP prop i n xs
158 where
159 prop :: P (Int -> Int -> v a -> v a) = V.slice `eq` slice
160
161 prop_tail :: P (v a -> v a) = not . V.null ===> V.tail `eq` tail
162 prop_init :: P (v a -> v a) = not . V.null ===> V.init `eq` init
163 prop_take :: P (Int -> v a -> v a) = V.take `eq` take
164 prop_drop :: P (Int -> v a -> v a) = V.drop `eq` drop
165
166 prop_accum = \f xs ->
167 forAll (index_value_pairs (V.length xs)) $ \ps ->
168 unP prop f xs ps
169 where
170 prop :: P ((a -> a -> a) -> v a -> [(Int,a)] -> v a)
171 = V.accum `eq` accum
172
173 prop_upd = \xs ->
174 forAll (index_value_pairs (V.length xs)) $ \ps ->
175 unP prop xs ps
176 where
177 prop :: P (v a -> [(Int,a)] -> v a) = (V.//) `eq` (//)
178
179 prop_backpermute = \xs ->
180 forAll (indices (V.length xs)) $ \is ->
181 unP prop xs (V.fromList is)
182 where
183 prop :: P (v a -> v Int -> v a) = V.backpermute `eq` backpermute
184
185 prop_reverse :: P (v a -> v a) = V.reverse `eq` reverse
186
187 prop_map :: P ((a -> a) -> v a -> v a) = V.map `eq` map
188 prop_zipWith :: P ((a -> a -> a) -> v a -> v a -> v a) = V.zipWith `eq` zipWith
189 prop_zipWith3 :: P ((a -> a -> a -> a) -> v a -> v a -> v a -> v a)
190 = V.zipWith3 `eq` zipWith3
191 prop_imap :: P ((Int -> a -> a) -> v a -> v a) = V.imap `eq` imap
192 prop_izipWith :: P ((Int -> a -> a -> a) -> v a -> v a -> v a) = V.izipWith `eq` izipWith
193 prop_izipWith3 :: P ((Int -> a -> a -> a -> a) -> v a -> v a -> v a -> v a)
194 = V.izipWith3 `eq` izipWith3
195
196 prop_filter :: P ((a -> Bool) -> v a -> v a) = V.filter `eq` filter
197 prop_ifilter :: P ((Int -> a -> Bool) -> v a -> v a) = V.ifilter `eq` ifilter
198 prop_takeWhile :: P ((a -> Bool) -> v a -> v a) = V.takeWhile `eq` takeWhile
199 prop_dropWhile :: P ((a -> Bool) -> v a -> v a) = V.dropWhile `eq` dropWhile
200 prop_span :: P ((a -> Bool) -> v a -> (v a, v a)) = V.span `eq` span
201 prop_break :: P ((a -> Bool) -> v a -> (v a, v a)) = V.break `eq` break
202
203 prop_elem :: P (a -> v a -> Bool) = V.elem `eq` elem
204 prop_notElem :: P (a -> v a -> Bool) = V.notElem `eq` notElem
205 prop_find :: P ((a -> Bool) -> v a -> Maybe a) = V.find `eq` find
206 prop_findIndex :: P ((a -> Bool) -> v a -> Maybe Int)
207 = V.findIndex `eq` findIndex
208 prop_findIndices :: P ((a -> Bool) -> v a -> v Int)
209 = V.findIndices `eq` findIndices
210 prop_elemIndex :: P (a -> v a -> Maybe Int) = V.elemIndex `eq` elemIndex
211 prop_elemIndices :: P (a -> v a -> v Int) = V.elemIndices `eq` elemIndices
212
213 prop_foldl :: P ((a -> a -> a) -> a -> v a -> a) = V.foldl `eq` foldl
214 prop_foldl1 :: P ((a -> a -> a) -> v a -> a) = notNull2 ===>
215 V.foldl1 `eq` foldl1
216 prop_foldl' :: P ((a -> a -> a) -> a -> v a -> a) = V.foldl' `eq` foldl'
217 prop_foldl1' :: P ((a -> a -> a) -> v a -> a) = notNull2 ===>
218 V.foldl1' `eq` foldl1'
219 prop_foldr :: P ((a -> a -> a) -> a -> v a -> a) = V.foldr `eq` foldr
220 prop_foldr1 :: P ((a -> a -> a) -> v a -> a) = notNull2 ===>
221 V.foldr1 `eq` foldr1
222 prop_ifoldl :: P ((a -> Int -> a -> a) -> a -> v a -> a)
223 = V.ifoldl `eq` ifoldl
224 prop_ifoldl' :: P ((a -> Int -> a -> a) -> a -> v a -> a)
225 = V.ifoldl' `eq` ifoldl
226 prop_ifoldr :: P ((Int -> a -> a -> a) -> a -> v a -> a)
227 = V.ifoldr `eq` ifoldr
228
229 prop_all :: P ((a -> Bool) -> v a -> Bool) = V.all `eq` all
230 prop_any :: P ((a -> Bool) -> v a -> Bool) = V.any `eq` any
231
232 prop_prescanl :: P ((a -> a -> a) -> a -> v a -> v a)
233 = V.prescanl `eq` prescanl
234 prop_prescanl' :: P ((a -> a -> a) -> a -> v a -> v a)
235 = V.prescanl' `eq` prescanl
236 prop_postscanl :: P ((a -> a -> a) -> a -> v a -> v a)
237 = V.postscanl `eq` postscanl
238 prop_postscanl' :: P ((a -> a -> a) -> a -> v a -> v a)
239 = V.postscanl' `eq` postscanl
240 prop_scanl :: P ((a -> a -> a) -> a -> v a -> v a)
241 = V.scanl `eq` scanl
242 prop_scanl' :: P ((a -> a -> a) -> a -> v a -> v a)
243 = V.scanl' `eq` scanl
244 prop_scanl1 :: P ((a -> a -> a) -> v a -> v a) = notNull2 ===>
245 V.scanl1 `eq` scanl1
246 prop_scanl1' :: P ((a -> a -> a) -> v a -> v a) = notNull2 ===>
247 V.scanl1' `eq` scanl1
248
249 prop_prescanr :: P ((a -> a -> a) -> a -> v a -> v a)
250 = V.prescanr `eq` prescanr
251 prop_prescanr' :: P ((a -> a -> a) -> a -> v a -> v a)
252 = V.prescanr' `eq` prescanr
253 prop_postscanr :: P ((a -> a -> a) -> a -> v a -> v a)
254 = V.postscanr `eq` postscanr
255 prop_postscanr' :: P ((a -> a -> a) -> a -> v a -> v a)
256 = V.postscanr' `eq` postscanr
257 prop_scanr :: P ((a -> a -> a) -> a -> v a -> v a)
258 = V.scanr `eq` scanr
259 prop_scanr' :: P ((a -> a -> a) -> a -> v a -> v a)
260 = V.scanr' `eq` scanr
261 prop_scanr1 :: P ((a -> a -> a) -> v a -> v a) = notNull2 ===>
262 V.scanr1 `eq` scanr1
263 prop_scanr1' :: P ((a -> a -> a) -> v a -> v a) = notNull2 ===>
264 V.scanr1' `eq` scanr1
265
266 prop_concatMap = forAll arbitrary $ \xs ->
267 forAll (sized (\n -> resize (n `div` V.length xs) arbitrary)) $ \f -> unP prop f xs
268 where
269 prop :: P ((a -> v a) -> v a -> v a) = V.concatMap `eq` concatMap
270
271 --prop_span = (V.span :: (a -> Bool) -> v a -> (v a, v a)) `eq2` span
272 --prop_break = (V.break :: (a -> Bool) -> v a -> (v a, v a)) `eq2` break
273 --prop_splitAt = (V.splitAt :: Int -> v a -> (v a, v a)) `eq2` splitAt
274 --prop_all = (V.all :: (a -> Bool) -> v a -> Bool) `eq2` all
275 --prop_any = (V.any :: (a -> Bool) -> v a -> Bool) `eq2` any
276
277 -- Data.List
278 --prop_findIndices = V.findIndices `eq2` (findIndices :: (a -> Bool) -> v a -> v Int)
279 --prop_isPrefixOf = V.isPrefixOf `eq2` (isPrefixOf :: v a -> v a -> Bool)
280 --prop_elemIndex = V.elemIndex `eq2` (elemIndex :: a -> v a -> Maybe Int)
281 --prop_elemIndices = V.elemIndices `eq2` (elemIndices :: a -> v a -> v Int)
282 --
283 --prop_mapAccumL = eq3
284 -- (V.mapAccumL :: (X -> W -> (X,W)) -> X -> B -> (X, B))
285 -- ( mapAccumL :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
286 --
287 --prop_mapAccumR = eq3
288 -- (V.mapAccumR :: (X -> W -> (X,W)) -> X -> B -> (X, B))
289 -- ( mapAccumR :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
290
291 -- Because the vectors are strict, we need to be totally sure that the unfold eventually terminates. This
292 -- is achieved by injecting our own bit of state into the unfold - the maximum number of unfolds allowed.
293 limitUnfolds f (theirs, ours) | ours >= 0
294 , Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
295 | otherwise = Nothing
296 prop_unfoldr :: P (Int -> (Int -> Maybe (a,Int)) -> Int -> v a)
297 = (\n f a -> V.unfoldr (limitUnfolds f) (a, n))
298 `eq` (\n f a -> unfoldr (limitUnfolds f) (a, n))
299
300
301 testTuplyFunctions:: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT((a, a), v), VECTOR_CONTEXT((a, a, a), v)) => v a -> [Test]
302 testTuplyFunctions _ = $(testProperties ['prop_zip, 'prop_zip3, 'prop_unzip, 'prop_unzip3])
303 where
304 prop_zip :: P (v a -> v a -> v (a, a)) = V.zip `eq` zip
305 prop_zip3 :: P (v a -> v a -> v a -> v (a, a, a)) = V.zip3 `eq` zip3
306 prop_unzip :: P (v (a, a) -> (v a, v a)) = V.unzip `eq` unzip
307 prop_unzip3 :: P (v (a, a, a) -> (v a, v a, v a)) = V.unzip3 `eq` unzip3
308
309 testOrdFunctions :: forall a v. (COMMON_CONTEXT(a, v), Ord a, Ord (v a)) => v a -> [Test]
310 testOrdFunctions _ = $(testProperties
311 ['prop_compare,
312 'prop_maximum, 'prop_minimum,
313 'prop_minIndex, 'prop_maxIndex ])
314 where
315 prop_compare :: P (v a -> v a -> Ordering) = compare `eq` compare
316 prop_maximum :: P (v a -> a) = not . V.null ===> V.maximum `eq` maximum
317 prop_minimum :: P (v a -> a) = not . V.null ===> V.minimum `eq` minimum
318 prop_minIndex :: P (v a -> Int) = not . V.null ===> V.minIndex `eq` minIndex
319 prop_maxIndex :: P (v a -> Int) = not . V.null ===> V.maxIndex `eq` maxIndex
320
321 testEnumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Enum a, Ord a, Num a, Random a) => v a -> [Test]
322 testEnumFunctions _ = $(testProperties ['prop_enumFromTo, 'prop_enumFromThenTo])
323 where
324 prop_enumFromTo = \m ->
325 forAll (choose (-2,100)) $ \n ->
326 unP prop m (m+n)
327 where
328 prop :: P (a -> a -> v a) = V.enumFromTo `eq` enumFromTo
329
330 prop_enumFromThenTo = \i j ->
331 j /= i ==>
332 forAll (choose (ks i j)) $ \k ->
333 unP prop i j k
334 where
335 prop :: P (a -> a -> a -> v a) = V.enumFromThenTo `eq` enumFromThenTo
336
337 ks i j | j < i = (i-d*100, i+d*2)
338 | otherwise = (i-d*2, i+d*100)
339 where
340 d = abs (j-i)
341
342 testBoolFunctions :: forall v. (COMMON_CONTEXT(Bool, v)) => v Bool -> [Test]
343 testBoolFunctions _ = $(testProperties ['prop_and, 'prop_or])
344 where
345 prop_and :: P (v Bool -> Bool) = V.and `eq` and
346 prop_or :: P (v Bool -> Bool) = V.or `eq` or
347
348 testNumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Num a) => v a -> [Test]
349 testNumFunctions _ = $(testProperties ['prop_sum, 'prop_product])
350 where
351 prop_sum :: P (v a -> a) = V.sum `eq` sum
352 prop_product :: P (v a -> a) = V.product `eq` product
353
354 testNestedVectorFunctions :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test]
355 testNestedVectorFunctions _ = $(testProperties [])
356 where
357 -- Prelude
358 --prop_concat = (V.concat :: [v a] -> v a) `eq1` concat
359
360 -- Data.List
361 --prop_transpose = V.transpose `eq1` (transpose :: [v a] -> [v a])
362 --prop_group = V.group `eq1` (group :: v a -> [v a])
363 --prop_inits = V.inits `eq1` (inits :: v a -> [v a])
364 --prop_tails = V.tails `eq1` (tails :: v a -> [v a])
365
366
367 testGeneralBoxedVector dummy = concatMap ($ dummy) [
368 testSanity,
369 testPolymorphicFunctions,
370 testOrdFunctions,
371 testTuplyFunctions,
372 testNestedVectorFunctions
373 ]
374
375 testBoolBoxedVector dummy = testGeneralBoxedVector dummy ++ testBoolFunctions dummy
376 testNumericBoxedVector dummy = testGeneralBoxedVector dummy ++ testNumFunctions dummy ++ testEnumFunctions dummy
377
378 testGeneralPrimitiveVector dummy = concatMap ($ dummy) [
379 testSanity,
380 testPolymorphicFunctions,
381 testOrdFunctions
382 ]
383
384 testGeneralStorableVector dummy = concatMap ($ dummy) [
385 testSanity,
386 testPolymorphicFunctions,
387 testOrdFunctions
388 ]
389
390 testBoolPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testBoolFunctions dummy
391 testNumericPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testNumFunctions dummy ++ testEnumFunctions dummy
392 testNumericStorableVector dummy = testGeneralStorableVector dummy ++ testNumFunctions dummy ++ testEnumFunctions dummy
393
394 tests = [
395 testGroup "Data.Vector.Vector (Bool)" (testBoolBoxedVector (undefined :: Data.Vector.Vector Bool)),
396 testGroup "Data.Vector.Vector (Int)" (testNumericBoxedVector (undefined :: Data.Vector.Vector Int)),
397
398 testGroup "Data.Vector.Primitive.Vector (Int)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Int)),
399 testGroup "Data.Vector.Primitive.Vector (Float)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Float)),
400 testGroup "Data.Vector.Primitive.Vector (Double)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Double)),
401
402 testGroup "Data.Vector.Storable.Vector (Int)" (testNumericStorableVector (undefined :: Data.Vector.Storable.Vector Int)),
403 testGroup "Data.Vector.Storable.Vector (Float)" (testNumericStorableVector (undefined :: Data.Vector.Storable.Vector Float)),
404 testGroup "Data.Vector.Storable.Vector (Double)" (testNumericStorableVector (undefined :: Data.Vector.Storable.Vector Double))
405 ]
406