19a678a8bcc241f90fd39d8e8352fb1fe2357f40
[packages/containers.git] / tests / set-properties.hs
1 import qualified Data.IntSet as IntSet
2 import Data.List (nub,sort)
3 import qualified Data.List as List
4 import Data.Monoid (mempty)
5 import Data.Set
6 import Prelude hiding (lookup, null, map, filter, foldr, foldl)
7 import Test.QuickCheck
8 import Test.Framework
9 import Test.Framework.Providers.QuickCheck2
10
11 main :: IO ()
12 main = defaultMainWithOpts [ testProperty "prop_Valid" prop_Valid
13 , testProperty "prop_Single" prop_Single
14 , testProperty "prop_InsertValid" prop_InsertValid
15 , testProperty "prop_InsertDelete" prop_InsertDelete
16 , testProperty "prop_DeleteValid" prop_DeleteValid
17 , testProperty "prop_Join" prop_Join
18 , testProperty "prop_Merge" prop_Merge
19 , testProperty "prop_UnionValid" prop_UnionValid
20 , testProperty "prop_UnionInsert" prop_UnionInsert
21 , testProperty "prop_UnionAssoc" prop_UnionAssoc
22 , testProperty "prop_UnionComm" prop_UnionComm
23 , testProperty "prop_DiffValid" prop_DiffValid
24 , testProperty "prop_Diff" prop_Diff
25 , testProperty "prop_IntValid" prop_IntValid
26 , testProperty "prop_Int" prop_Int
27 , testProperty "prop_Ordered" prop_Ordered
28 , testProperty "prop_List" prop_List
29 , testProperty "prop_DescList" prop_DescList
30 , testProperty "prop_AscDescList" prop_AscDescList
31 , testProperty "prop_fromList" prop_fromList
32 , testProperty "prop_isProperSubsetOf" prop_isProperSubsetOf
33 , testProperty "prop_isProperSubsetOf2" prop_isProperSubsetOf2
34 , testProperty "prop_isSubsetOf" prop_isSubsetOf
35 , testProperty "prop_isSubsetOf2" prop_isSubsetOf2
36 , testProperty "prop_size" prop_size
37 , testProperty "prop_findMax" prop_findMax
38 , testProperty "prop_findMin" prop_findMin
39 , testProperty "prop_ord" prop_ord
40 , testProperty "prop_readShow" prop_readShow
41 , testProperty "prop_foldR" prop_foldR
42 , testProperty "prop_foldR'" prop_foldR'
43 , testProperty "prop_foldL" prop_foldL
44 , testProperty "prop_foldL'" prop_foldL'
45 , testProperty "prop_map" prop_map
46 , testProperty "prop_maxView" prop_maxView
47 , testProperty "prop_minView" prop_minView
48 , testProperty "prop_split" prop_split
49 , testProperty "prop_splitMember" prop_splitMember
50 , testProperty "prop_partition" prop_partition
51 , testProperty "prop_filter" prop_filter
52 ] opts
53 where
54 opts = mempty { ropt_test_options = Just $ mempty { topt_maximum_generated_tests = Just 500
55 , topt_maximum_unsuitable_generated_tests = Just 500
56 }
57 }
58
59 {--------------------------------------------------------------------
60 Arbitrary, reasonably balanced trees
61 --------------------------------------------------------------------}
62 instance (Enum a) => Arbitrary (Set a) where
63 arbitrary = sized (arbtree 0 maxkey)
64 where maxkey = 10000
65
66 arbtree :: (Enum a) => Int -> Int -> Int -> Gen (Set a)
67 arbtree lo hi n = do t <- gentree lo hi n
68 if balanced t then return t else arbtree lo hi n
69 where gentree lo hi n
70 | n <= 0 = return Tip
71 | lo >= hi = return Tip
72 | otherwise = do i <- choose (lo,hi)
73 m <- choose (1,70)
74 let (ml,mr) | m==(1::Int) = (1,2)
75 | m==2 = (2,1)
76 | m==3 = (1,1)
77 | otherwise = (2,2)
78 l <- gentree lo (i-1) (n `div` ml)
79 r <- gentree (i+1) hi (n `div` mr)
80 return (bin (toEnum i) l r)
81
82 {--------------------------------------------------------------------
83 Valid tree's
84 --------------------------------------------------------------------}
85 forValid :: (Enum a,Show a,Testable b) => (Set a -> b) -> Property
86 forValid f = forAll arbitrary $ \t ->
87 -- classify (balanced t) "balanced" $
88 classify (size t == 0) "empty" $
89 classify (size t > 0 && size t <= 10) "small" $
90 classify (size t > 10 && size t <= 64) "medium" $
91 classify (size t > 64) "large" $
92 balanced t ==> f t
93
94 forValidUnitTree :: Testable a => (Set Int -> a) -> Property
95 forValidUnitTree f = forValid f
96
97 prop_Valid :: Property
98 prop_Valid = forValidUnitTree $ \t -> valid t
99
100 {--------------------------------------------------------------------
101 Single, Insert, Delete
102 --------------------------------------------------------------------}
103 prop_Single :: Int -> Bool
104 prop_Single x = (insert x empty == singleton x)
105
106 prop_InsertValid :: Int -> Property
107 prop_InsertValid k = forValidUnitTree $ \t -> valid (insert k t)
108
109 prop_InsertDelete :: Int -> Set Int -> Property
110 prop_InsertDelete k t = not (member k t) ==> delete k (insert k t) == t
111
112 prop_DeleteValid :: Int -> Property
113 prop_DeleteValid k = forValidUnitTree $ \t -> valid (delete k (insert k t))
114
115 {--------------------------------------------------------------------
116 Balance
117 --------------------------------------------------------------------}
118 prop_Join :: Int -> Property
119 prop_Join x = forValidUnitTree $ \t ->
120 let (l,r) = split x t
121 in valid (join x l r)
122
123 prop_Merge :: Int -> Property
124 prop_Merge x = forValidUnitTree $ \t ->
125 let (l,r) = split x t
126 in valid (merge l r)
127
128 {--------------------------------------------------------------------
129 Union
130 --------------------------------------------------------------------}
131 prop_UnionValid :: Property
132 prop_UnionValid
133 = forValidUnitTree $ \t1 ->
134 forValidUnitTree $ \t2 ->
135 valid (union t1 t2)
136
137 prop_UnionInsert :: Int -> Set Int -> Bool
138 prop_UnionInsert x t = union t (singleton x) == insert x t
139
140 prop_UnionAssoc :: Set Int -> Set Int -> Set Int -> Bool
141 prop_UnionAssoc t1 t2 t3 = union t1 (union t2 t3) == union (union t1 t2) t3
142
143 prop_UnionComm :: Set Int -> Set Int -> Bool
144 prop_UnionComm t1 t2 = (union t1 t2 == union t2 t1)
145
146 prop_DiffValid :: Property
147 prop_DiffValid = forValidUnitTree $ \t1 ->
148 forValidUnitTree $ \t2 ->
149 valid (difference t1 t2)
150
151 prop_Diff :: [Int] -> [Int] -> Bool
152 prop_Diff xs ys = toAscList (difference (fromList xs) (fromList ys))
153 == List.sort ((List.\\) (nub xs) (nub ys))
154
155 prop_IntValid :: Property
156 prop_IntValid = forValidUnitTree $ \t1 ->
157 forValidUnitTree $ \t2 ->
158 valid (intersection t1 t2)
159
160 prop_Int :: [Int] -> [Int] -> Bool
161 prop_Int xs ys = toAscList (intersection (fromList xs) (fromList ys))
162 == List.sort (nub ((List.intersect) (xs) (ys)))
163
164 {--------------------------------------------------------------------
165 Lists
166 --------------------------------------------------------------------}
167 prop_Ordered :: Property
168 prop_Ordered = forAll (choose (5,100)) $ \n ->
169 let xs = [0..n::Int]
170 in fromAscList xs == fromList xs
171
172 prop_List :: [Int] -> Bool
173 prop_List xs = (sort (nub xs) == toList (fromList xs))
174
175 prop_DescList :: [Int] -> Bool
176 prop_DescList xs = (reverse (sort (nub xs)) == toDescList (fromList xs))
177
178 prop_AscDescList :: [Int] -> Bool
179 prop_AscDescList xs = toAscList s == reverse (toDescList s)
180 where s = fromList xs
181
182 prop_fromList :: [Int] -> Bool
183 prop_fromList xs
184 = case fromList xs of
185 t -> t == fromAscList sort_xs &&
186 t == fromDistinctAscList nub_sort_xs &&
187 t == List.foldr insert empty xs
188 where sort_xs = sort xs
189 nub_sort_xs = List.map List.head $ List.group sort_xs
190
191 {--------------------------------------------------------------------
192 Set operations are like IntSet operations
193 --------------------------------------------------------------------}
194 toIntSet :: Set Int -> IntSet.IntSet
195 toIntSet = IntSet.fromList . toList
196
197 -- Check that Set Int.isProperSubsetOf is the same as Set.isProperSubsetOf.
198 prop_isProperSubsetOf :: Set Int -> Set Int -> Bool
199 prop_isProperSubsetOf a b = isProperSubsetOf a b == IntSet.isProperSubsetOf (toIntSet a) (toIntSet b)
200
201 -- In the above test, isProperSubsetOf almost always returns False (since a
202 -- random set is almost never a subset of another random set). So this second
203 -- test checks the True case.
204 prop_isProperSubsetOf2 :: Set Int -> Set Int -> Bool
205 prop_isProperSubsetOf2 a b = isProperSubsetOf a c == (a /= c) where
206 c = union a b
207
208 prop_isSubsetOf :: Set Int -> Set Int -> Bool
209 prop_isSubsetOf a b = isSubsetOf a b == IntSet.isSubsetOf (toIntSet a) (toIntSet b)
210
211 prop_isSubsetOf2 :: Set Int -> Set Int -> Bool
212 prop_isSubsetOf2 a b = isSubsetOf a (union a b)
213
214 prop_size :: Set Int -> Bool
215 prop_size s = size s == List.length (toList s)
216
217 prop_findMax :: Set Int -> Property
218 prop_findMax s = not (null s) ==> findMax s == maximum (toList s)
219
220 prop_findMin :: Set Int -> Property
221 prop_findMin s = not (null s) ==> findMin s == minimum (toList s)
222
223 prop_ord :: Set Int -> Set Int -> Bool
224 prop_ord s1 s2 = s1 `compare` s2 == toList s1 `compare` toList s2
225
226 prop_readShow :: Set Int -> Bool
227 prop_readShow s = s == read (show s)
228
229 prop_foldR :: Set Int -> Bool
230 prop_foldR s = foldr (:) [] s == toList s
231
232 prop_foldR' :: Set Int -> Bool
233 prop_foldR' s = foldr' (:) [] s == toList s
234
235 prop_foldL :: Set Int -> Bool
236 prop_foldL s = foldl (flip (:)) [] s == List.foldl (flip (:)) [] (toList s)
237
238 prop_foldL' :: Set Int -> Bool
239 prop_foldL' s = foldl' (flip (:)) [] s == List.foldl' (flip (:)) [] (toList s)
240
241 prop_map :: Set Int -> Bool
242 prop_map s = map id s == s
243
244 prop_maxView :: Set Int -> Bool
245 prop_maxView s = case maxView s of
246 Nothing -> null s
247 Just (m,s') -> m == maximum (toList s) && s == insert m s' && m `notMember` s'
248
249 prop_minView :: Set Int -> Bool
250 prop_minView s = case minView s of
251 Nothing -> null s
252 Just (m,s') -> m == minimum (toList s) && s == insert m s' && m `notMember` s'
253
254 prop_split :: Set Int -> Int -> Bool
255 prop_split s i = case split i s of
256 (s1,s2) -> all (<i) (toList s1) && all (>i) (toList s2) && i `delete` s == union s1 s2
257
258 prop_splitMember :: Set Int -> Int -> Bool
259 prop_splitMember s i = case splitMember i s of
260 (s1,t,s2) -> all (<i) (toList s1) && all (>i) (toList s2) && t == i `member` s && i `delete` s == union s1 s2
261
262 prop_partition :: Set Int -> Int -> Bool
263 prop_partition s i = case partition odd s of
264 (s1,s2) -> all odd (toList s1) && all even (toList s2) && s == s1 `union` s2
265
266 prop_filter :: Set Int -> Int -> Bool
267 prop_filter s i = partition odd s == (filter odd s, filter even s)