c63a778045e4dd520be4c6a36fae573d53d56942
[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, Modelled a, Model a ~ a, EqTestable a, EqTest a ~ Bool
24
25 #define VECTOR_CONTEXT(a, v) \
26 Eq (v a), Show (v a), Arbitrary (v a), Modelled (v a), Model (v a) ~ [a], EqTestable (v a), EqTest (v a) ~ Bool, 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) `eq` (==)
108
109 prop_length = (V.length :: v a -> Int) `eq` length
110 prop_null = (V.null :: v a -> Bool) `eq` null
111
112 prop_empty = (V.empty :: v a) `eq` []
113 prop_singleton = (V.singleton :: a -> v a) `eq` singleton
114 prop_replicate = (V.replicate :: Int -> a -> v a) `eq` replicate
115 prop_cons = (V.cons :: a -> v a -> v a) `eq` (:)
116 prop_snoc = (V.snoc :: v a -> a -> v a) `eq` snoc
117 prop_append = ((V.++) :: v a -> v a -> v a) `eq` (++)
118 prop_copy = (V.copy :: v a -> v a) `eq` id
119
120 prop_head = not . V.null ===>
121 (V.head :: v a -> a) `eq` head
122 prop_last = not . V.null ===>
123 (V.last :: v a -> a) `eq` last
124 prop_index = (\xs i -> i >= 0 && i < V.length xs)
125 ===> ((V.!) :: v a -> Int -> a) `eq` (!!)
126
127
128 prop_slice = forAll arbitrary $ \xs ->
129 forAll (choose (0, V.length xs)) $ \i ->
130 forAll (choose (0, V.length xs - i)) $ \n ->
131 ((V.slice :: v a -> Int -> Int -> v a) `eq` slice)
132 xs i n
133 prop_tail = not . V.null ===>
134 (V.tail :: v a -> v a) `eq` tail
135 prop_init = not . V.null ===>
136 (V.init :: v a -> v a) `eq` init
137 prop_take = (V.take :: Int -> v a -> v a) `eq` take
138 prop_drop = (V.drop :: Int -> v a -> v a) `eq` drop
139
140 prop_accum = forAll arbitrary $ \f ->
141 forAll arbitrary $ \xs ->
142 forAll (index_value_pairs (V.length xs)) $ \ps ->
143 ((V.accum :: (a -> a -> a) -> v a -> [(Int,a)] -> v a)
144 `eq` accum) f xs ps
145 prop_write = forAll arbitrary $ \xs ->
146 forAll (index_value_pairs (V.length xs)) $ \ps ->
147 (((V.//) :: v a -> [(Int,a)] -> v a) `eq` (//)) xs ps
148 prop_backpermute = forAll arbitrary $ \xs ->
149 forAll (indices (V.length xs)) $ \is ->
150 ((V.backpermute :: v a -> v Int -> v a) `eq` backpermute)
151 xs (V.fromList is)
152 prop_reverse = (V.reverse :: v a -> v a) `eq` reverse
153
154 prop_map = (V.map :: (a -> a) -> v a -> v a) `eq` map
155 prop_zipWith = (V.zipWith :: (a -> a -> a) -> v a -> v a -> v a) `eq` zipWith
156 prop_zipWith3 = (V.zipWith3 :: (a -> a -> a -> a) -> v a -> v a -> v a -> v a) `eq` zipWith3
157
158 prop_filter = (V.filter :: (a -> Bool) -> v a -> v a) `eq` filter
159 prop_takeWhile = (V.takeWhile :: (a -> Bool) -> v a -> v a) `eq` takeWhile
160 prop_dropWhile = (V.dropWhile :: (a -> Bool) -> v a -> v a) `eq` dropWhile
161
162 prop_elem = (V.elem :: a -> v a -> Bool) `eq` elem
163 prop_notElem = (V.notElem :: a -> v a -> Bool) `eq` notElem
164 prop_find = (V.find :: (a -> Bool) -> v a -> Maybe a) `eq` find
165 prop_findIndex = (V.findIndex :: (a -> Bool) -> v a -> Maybe Int) `eq` findIndex
166
167 prop_foldl = (V.foldl :: (a -> a -> a) -> a -> v a -> a) `eq` foldl
168 prop_foldl1 = notNull2 ===>
169 (V.foldl1 :: (a -> a -> a) -> v a -> a) `eq` foldl1
170 prop_foldl' = (V.foldl' :: (a -> a -> a) -> a -> v a -> a) `eq` foldl'
171 prop_foldl1' = notNull2 ===>
172 (V.foldl1' :: (a -> a -> a) -> v a -> a) `eq` foldl1'
173 prop_foldr = (V.foldr :: (a -> a -> a) -> a -> v a -> a) `eq` foldr
174 prop_foldr1 = notNull2 ===>
175 (V.foldr1 :: (a -> a -> a) -> v a -> a) `eq` foldr1
176
177 prop_prescanl = (V.prescanl :: (a -> a -> a) -> a -> v a -> v a) `eq` prescanl
178 prop_prescanl' = (V.prescanl' :: (a -> a -> a) -> a -> v a -> v a) `eq` prescanl
179 prop_postscanl = (V.postscanl :: (a -> a -> a) -> a -> v a -> v a) `eq` postscanl
180 prop_postscanl' = (V.postscanl' :: (a -> a -> a) -> a -> v a -> v a) `eq` postscanl
181 prop_scanl = (V.scanl :: (a -> a -> a) -> a -> v a -> v a) `eq` scanl
182 prop_scanl' = (V.scanl' :: (a -> a -> a) -> a -> v a -> v a) `eq` scanl
183 prop_scanl1 = notNull2 ===>
184 (V.scanl1 :: (a -> a -> a) -> v a -> v a) `eq` scanl1
185 prop_scanl1' = notNull2 ===>
186 (V.scanl1' :: (a -> a -> a) -> v a -> v a) `eq` scanl1
187
188 prop_concatMap = (V.concatMap :: (a -> v a) -> v a -> v a) `eq` concatMap
189
190 --prop_span = (V.span :: (a -> Bool) -> v a -> (v a, v a)) `eq2` span
191 --prop_break = (V.break :: (a -> Bool) -> v a -> (v a, v a)) `eq2` break
192 --prop_splitAt = (V.splitAt :: Int -> v a -> (v a, v a)) `eq2` splitAt
193 --prop_all = (V.all :: (a -> Bool) -> v a -> Bool) `eq2` all
194 --prop_any = (V.any :: (a -> Bool) -> v a -> Bool) `eq2` any
195
196 -- Data.List
197 --prop_findIndices = V.findIndices `eq2` (findIndices :: (a -> Bool) -> v a -> v Int)
198 --prop_isPrefixOf = V.isPrefixOf `eq2` (isPrefixOf :: v a -> v a -> Bool)
199 --prop_elemIndex = V.elemIndex `eq2` (elemIndex :: a -> v a -> Maybe Int)
200 --prop_elemIndices = V.elemIndices `eq2` (elemIndices :: a -> v a -> v Int)
201 --
202 --prop_mapAccumL = eq3
203 -- (V.mapAccumL :: (X -> W -> (X,W)) -> X -> B -> (X, B))
204 -- ( mapAccumL :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
205 --
206 --prop_mapAccumR = eq3
207 -- (V.mapAccumR :: (X -> W -> (X,W)) -> X -> B -> (X, B))
208 -- ( mapAccumR :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
209
210 -- Because the vectors are strict, we need to be totally sure that the unfold eventually terminates. This
211 -- is achieved by injecting our own bit of state into the unfold - the maximum number of unfolds allowed.
212 limitUnfolds f (theirs, ours) | ours >= 0
213 , Just (out, theirs') <- f theirs = Just (out, (theirs', ours - 1))
214 | otherwise = Nothing
215 prop_unfoldr = ((\n f a -> V.unfoldr (limitUnfolds f) (a, n)) :: Int -> ((Int, Int) -> Maybe (a, (Int, Int))) -> (Int, Int) -> v a)
216 `eq` (\n f a -> unfoldr (limitUnfolds f) (a, n))
217
218
219 testTuplyFunctions:: forall a v. (COMMON_CONTEXT(a, v), VECTOR_CONTEXT((a, a), v), VECTOR_CONTEXT((a, a, a), v)) => v a -> [Test]
220 testTuplyFunctions _ = $(testProperties ['prop_zip, 'prop_zip3, 'prop_unzip, 'prop_unzip3])
221 where
222 prop_zip = (V.zip :: v a -> v a -> v (a, a)) `eq` zip
223 prop_zip3 = (V.zip3 :: v a -> v a -> v a -> v (a, a, a)) `eq` zip3
224 prop_unzip = (V.unzip :: v (a, a) -> (v a, v a)) `eq` unzip
225 prop_unzip3 = (V.unzip3 :: v (a, a, a) -> (v a, v a, v a)) `eq` unzip3
226
227 testOrdFunctions :: forall a v. (COMMON_CONTEXT(a, v), Ord a, Ord (v a)) => v a -> [Test]
228 testOrdFunctions _ = $(testProperties ['prop_compare, 'prop_maximum, 'prop_minimum])
229 where
230 prop_compare = (compare :: v a -> v a -> Ordering) `eq` compare
231 prop_maximum = not . V.null ===>
232 (V.maximum :: v a -> a) `eq` maximum
233 prop_minimum = not . V.null ===>
234 (V.minimum :: v a -> a) `eq` minimum
235
236 testEnumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Enum a) => v a -> [Test]
237 testEnumFunctions _ = $(testProperties ['prop_enumFromTo, 'prop_enumFromThenTo])
238 where
239 prop_enumFromTo = (V.enumFromTo :: a -> a -> v a) `eq` enumFromTo
240 prop_enumFromThenTo = \i j n -> fromEnum i < fromEnum j ==> ((V.enumFromThenTo :: a -> a -> a -> v a) `eq` enumFromThenTo) i j n
241
242 testBoolFunctions :: forall v. (COMMON_CONTEXT(Bool, v)) => v Bool -> [Test]
243 testBoolFunctions _ = $(testProperties ['prop_and, 'prop_or])
244 where
245 prop_and = (V.and :: v Bool -> Bool) `eq` and
246 prop_or = (V.or :: v Bool -> Bool) `eq` or
247
248 testNumFunctions :: forall a v. (COMMON_CONTEXT(a, v), Num a) => v a -> [Test]
249 testNumFunctions _ = $(testProperties ['prop_sum, 'prop_product])
250 where
251 prop_sum = (V.sum :: v a -> a) `eq` sum
252 prop_product = (V.product :: v a -> a) `eq` product
253
254 testNestedVectorFunctions :: forall a v. (COMMON_CONTEXT(a, v)) => v a -> [Test]
255 testNestedVectorFunctions _ = $(testProperties [])
256 where
257 -- Prelude
258 --prop_concat = (V.concat :: [v a] -> v a) `eq1` concat
259
260 -- Data.List
261 --prop_transpose = V.transpose `eq1` (transpose :: [v a] -> [v a])
262 --prop_group = V.group `eq1` (group :: v a -> [v a])
263 --prop_inits = V.inits `eq1` (inits :: v a -> [v a])
264 --prop_tails = V.tails `eq1` (tails :: v a -> [v a])
265
266
267 testGeneralBoxedVector dummy = concatMap ($ dummy) [
268 testSanity,
269 testPolymorphicFunctions,
270 testOrdFunctions,
271 testEnumFunctions,
272 testTuplyFunctions,
273 testNestedVectorFunctions
274 ]
275
276 testBoolBoxedVector dummy = testGeneralBoxedVector dummy ++ testBoolFunctions dummy
277 testNumericBoxedVector dummy = testGeneralBoxedVector dummy ++ testNumFunctions dummy
278
279 testGeneralPrimitiveVector dummy = concatMap ($ dummy) [
280 testSanity,
281 testPolymorphicFunctions,
282 testOrdFunctions,
283 testEnumFunctions
284 ]
285
286 testBoolPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testBoolFunctions dummy
287 testNumericPrimitiveVector dummy = testGeneralPrimitiveVector dummy ++ testNumFunctions dummy
288
289 tests = [
290 testGroup "Data.Vector.Vector (Bool)" (testBoolBoxedVector (undefined :: Data.Vector.Vector Bool)),
291 testGroup "Data.Vector.Vector (Int)" (testNumericBoxedVector (undefined :: Data.Vector.Vector Int)),
292 testGroup "Data.Vector.Primitive.Vector (Int)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Int)),
293 testGroup "Data.Vector.Primitive.Vector (Float)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Float)),
294 testGroup "Data.Vector.Primitive.Vector (Double)" (testNumericPrimitiveVector (undefined :: Data.Vector.Primitive.Vector Double))
295 ]