1d97673b4cd087a940f3d574a822e64ae36e978a
[darcs-mirrors/vector.git] / tests / Properties.hs
1 {-# LANGUAGE FlexibleContexts, Rank2Types, ScopedTypeVariables #-}
2
3 module Properties (tests) where
4
5 import Utilities
6
7 import qualified Data.Vector.IVector as V
8 import qualified Data.Vector
9 import qualified Data.Vector.Unboxed
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)
18
19 testVersusLists :: forall a v.
20 (Eq a, Ord a,
21 Eq (v a), Ord (v a),
22 Show a, Arbitrary a, Model a a,
23 -- This would be slightly nicer if we could put forall quantifiers in the class requirements!
24 Show (v a), Arbitrary (v a), Model (v a) [a], V.IVector v a,
25 Show (v Bool), Arbitrary (v Bool), Model (v Bool) [Bool], V.IVector v Bool)
26 => v a
27 -> [Test]
28 testVersusLists _ = [
29 testGroup "Prelude" prelude_tests,
30 testGroup "Data.List" data_list_tests,
31 testGroup "Extras" extra_tests
32 ]
33 where
34 prelude_tests = [
35 --testProperty "concat" prop_concat,
36 testProperty "length" prop_length,
37 testProperty "null" prop_null,
38 --testProperty "reverse" prop_reverse,
39 --testProperty "all" prop_all,
40 --testProperty "any" prop_any,
41 testProperty "and" prop_and,
42 testProperty "or" prop_or,
43 testProperty "(++)" prop_append,
44 --testProperty "break" prop_break,
45 testProperty "concatMap" prop_concatMap,
46 testProperty "(:)" prop_cons,
47 testProperty "drop" prop_drop,
48 testProperty "dropWhile" prop_dropWhile,
49 testProperty "take" prop_take,
50 testProperty "takeWhile" prop_takeWhile,
51 testProperty "filter" prop_filter,
52 testProperty "map" prop_map,
53 testProperty "replicate" prop_replicate,
54 --testProperty "span" prop_span,
55 --testProperty "splitAt" prop_splitAt,
56 testProperty "elem" prop_elem,
57 testProperty "notElem" prop_notElem,
58 testProperty "foldr" prop_foldr,
59 testProperty "foldl" prop_foldl,
60 testProperty "foldl'" prop_foldl',
61 --testProperty "lines" prop_lines,
62 testProperty "foldr1" prop_foldr1,
63 testProperty "foldl1" prop_foldl1,
64 testProperty "foldl1'" prop_foldl1',
65 testProperty "head" prop_head,
66 testProperty "tail" prop_tail,
67 testProperty "init" prop_init,
68 testProperty "last" prop_last,
69 --testProperty "maximum" prop_maximum,
70 --testProperty "minimum" prop_minimum,
71 testProperty "unfoldr" prop_unfoldr,
72 testProperty "(==)" prop_eq,
73 testProperty "compare" prop_compare
74 ]
75
76 -- TODO: implement Vector equivalents for the commented out list functions from Prelude
77 --prop_concat = (V.concat :: [v a] -> v a) `eq1` concat
78 prop_length = (V.length :: v a -> Int) `eq1` length
79 prop_null = (V.null :: v a -> Bool) `eq1` null
80 --prop_reverse = (V.reverse :: v a -> v a) `eq1` reverse
81 --prop_all = (V.all :: (a -> Bool) -> v a -> Bool) `eq2` all
82 --prop_any = (V.any :: (a -> Bool) -> v a -> Bool) `eq2` any
83 prop_and = (V.and :: v Bool -> Bool) `eq1` and
84 prop_or = (V.or :: v Bool -> Bool) `eq1` or
85 prop_append = ((V.++) :: v a -> v a -> v a) `eq2` (++)
86 --prop_break = (V.break :: (a -> Bool) -> v a -> (v a, v a)) `eq2` break
87 prop_concatMap = (V.concatMap :: (a -> v a) -> v a -> v a) `eq2` concatMap
88 prop_cons = (V.cons :: a -> v a -> v a) `eq2` (:)
89 prop_drop = (V.drop :: Int -> v a -> v a) `eq2` drop
90 prop_dropWhile = (V.dropWhile :: (a -> Bool) -> v a -> v a) `eq2` dropWhile
91 prop_take = (V.take :: Int -> v a -> v a) `eq2` take
92 prop_takeWhile = (V.takeWhile :: (a -> Bool) -> v a -> v a) `eq2` takeWhile
93 prop_filter = (V.filter :: (a -> Bool) -> v a -> v a) `eq2` filter
94 prop_map = (V.map :: (a -> a) -> v a -> v a) `eq2` map
95 prop_replicate = (V.replicate :: Int -> a -> v a) `eq2` replicate
96 --prop_span = (V.span :: (a -> Bool) -> v a -> (v a, v a)) `eq2` span
97 --prop_splitAt = (V.splitAt :: Int -> v a -> (v a, v a)) `eq2` splitAt
98 prop_elem = (V.elem :: a -> v a -> Bool) `eq2` elem
99 prop_notElem = (V.notElem :: a -> v a -> Bool) `eq2` notElem
100 --prop_lines = (V.lines :: String -> [String]) `eq1` lines
101 prop_foldr = (V.foldr :: (a -> a -> a) -> a -> v a -> a) `eq3` foldr
102 prop_foldl = (V.foldl :: (a -> a -> a) -> a -> v a -> a) `eq3` foldl
103 prop_foldr1 = (V.foldr1 :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldr1
104 prop_foldl1 = (V.foldl1 :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldl1
105 prop_head = (V.head :: v a -> a) `eqNotNull1` head
106 prop_tail = (V.tail :: v a -> v a) `eqNotNull1` tail
107 prop_init = (V.init :: v a -> v a) `eqNotNull1` init
108 prop_last = (V.last :: v a -> a) `eqNotNull1` last
109 --prop_maximum = (V.maximum :: v a -> a) `eqNotNull1` maximum
110 --prop_minimum = (V.minimum :: v a -> a) `eqNotNull1` minimum
111 prop_eq = ((==) :: v a -> v a -> Bool) `eq2` (==)
112 prop_compare = (compare :: v a -> v a -> Ordering) `eq2` compare
113
114 data_list_tests = [
115 testProperty "foldl'" prop_foldl',
116 testProperty "foldl1'" prop_foldl1',
117 testProperty "unfoldr" prop_unfoldr
118 --testProperty "transpose" prop_transpose,
119 --testProperty "group" prop_group,
120 --testProperty "inits" prop_inits,
121 --testProperty "tails" prop_tails,
122 --testProperty "find" prop_find,
123 --testProperty "findIndices" prop_findIndices,
124 --testProperty "findIndex" prop_findIndex,
125 --testProperty "isPrefixOf" prop_isPrefixOf,
126 --testProperty "elemIndex" prop_elemIndex,
127 --testProperty "elemIndices" prop_elemIndices,
128 --testProperty "mapAccumL" prop_mapAccumL,
129 --testProperty "mapAccumR" prop_mapAccumR,
130 ]
131
132 -- TODO: implement Vector equivalents for some of the commented out list functions from Data.List
133 prop_foldl' = (V.foldl' :: (a -> a -> a) -> a -> v a -> a) `eq3` foldl'
134 prop_foldl1' = (V.foldl1' :: (a -> a -> a) -> v a -> a) `eqNotNull2` foldl1'
135 prop_unfoldr = ((\n f a -> V.take n $ V.unfoldr f a) :: Int -> (Int -> Maybe (a, Int)) -> Int -> v a)
136 `eq3` (\n f a -> take n $ unfoldr f a)
137 --prop_transpose = V.transpose `eq1` (transpose :: [v a] -> [v a])
138 --prop_group = V.group `eq1` (group :: v a -> [v a])
139 --prop_inits = V.inits `eq1` (inits :: v a -> [v a])
140 --prop_tails = V.tails `eq1` (tails :: v a -> [v a])
141 --prop_find = V.find `eq2` (find :: (a -> Bool) -> v a -> Maybe a)
142 --prop_findIndices = V.findIndices `eq2` (findIndices :: (a -> Bool) -> v a -> [Int])
143 --prop_findIndex = V.findIndex `eq2` (findIndex :: (a -> Bool) -> v a -> Maybe Int)
144 --prop_isPrefixOf = V.isPrefixOf `eq2` (isPrefixOf :: v a -> v a -> Bool)
145 --prop_elemIndex = V.elemIndex `eq2` (elemIndex :: a -> v a -> Maybe Int)
146 --prop_elemIndices = V.elemIndices `eq2` (elemIndices :: a -> v a -> [Int])
147 --
148 --prop_mapAccumL = eq3
149 -- (V.mapAccumL :: (X -> W -> (X,W)) -> X -> B -> (X, B))
150 -- ( mapAccumL :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
151 --
152 --prop_mapAccumR = eq3
153 -- (V.mapAccumR :: (X -> W -> (X,W)) -> X -> B -> (X, B))
154 -- ( mapAccumR :: (X -> W -> (X,W)) -> X -> [W] -> (X, [W]))
155
156 extra_tests = [
157 testProperty "snoc" prop_snoc
158 ]
159
160 -- TODO: add tests for the other extra functions
161 snoc xs x = xs ++ [x]
162 prop_snoc = (V.snoc :: v a -> a -> v a) `eq2` snoc
163
164 tests = [
165 testGroup "Data.Vector.Vector" (testVersusLists (undefined :: Data.Vector.Vector Int)),
166 testGroup "Data.Vector.Unboxed.Vector (Int)" (testVersusLists (undefined :: Data.Vector.Unboxed.Vector Int)),
167 testGroup "Data.Vector.Unboxed.Vector (Bool)" (testVersusLists (undefined :: Data.Vector.Unboxed.Vector Bool))
168 ]