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