Merge tag 'v0.5.2.1' of git://github.com/haskell/containers into ghc-head
[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.Maybe
6 import Data.Set
7 import Prelude hiding (lookup, null, map, filter, foldr, foldl)
8 import Test.Framework
9 import Test.Framework.Providers.HUnit
10 import Test.Framework.Providers.QuickCheck2
11 import Test.HUnit hiding (Test, Testable)
12 import Test.QuickCheck
13
14 main :: IO ()
15 main = defaultMain [ testCase "lookupLT" test_lookupLT
16 , testCase "lookupGT" test_lookupGT
17 , testCase "lookupLE" test_lookupLE
18 , testCase "lookupGE" test_lookupGE
19 , testCase "lookupIndex" test_lookupIndex
20 , testCase "findIndex" test_findIndex
21 , testCase "elemAt" test_elemAt
22 , testCase "deleteAt" test_deleteAt
23 , testProperty "prop_Valid" prop_Valid
24 , testProperty "prop_Single" prop_Single
25 , testProperty "prop_Member" prop_Member
26 , testProperty "prop_NotMember" prop_NotMember
27 , testProperty "prop_LookupLT" prop_LookupLT
28 , testProperty "prop_LookupGT" prop_LookupGT
29 , testProperty "prop_LookupLE" prop_LookupLE
30 , testProperty "prop_LookupGE" prop_LookupGE
31 , testProperty "prop_InsertValid" prop_InsertValid
32 , testProperty "prop_InsertDelete" prop_InsertDelete
33 , testProperty "prop_DeleteValid" prop_DeleteValid
34 , testProperty "prop_Join" prop_Join
35 , testProperty "prop_Merge" prop_Merge
36 , testProperty "prop_UnionValid" prop_UnionValid
37 , testProperty "prop_UnionInsert" prop_UnionInsert
38 , testProperty "prop_UnionAssoc" prop_UnionAssoc
39 , testProperty "prop_UnionComm" prop_UnionComm
40 , testProperty "prop_DiffValid" prop_DiffValid
41 , testProperty "prop_Diff" prop_Diff
42 , testProperty "prop_IntValid" prop_IntValid
43 , testProperty "prop_Int" prop_Int
44 , testProperty "prop_Ordered" prop_Ordered
45 , testProperty "prop_List" prop_List
46 , testProperty "prop_DescList" prop_DescList
47 , testProperty "prop_AscDescList" prop_AscDescList
48 , testProperty "prop_fromList" prop_fromList
49 , testProperty "prop_isProperSubsetOf" prop_isProperSubsetOf
50 , testProperty "prop_isProperSubsetOf2" prop_isProperSubsetOf2
51 , testProperty "prop_isSubsetOf" prop_isSubsetOf
52 , testProperty "prop_isSubsetOf2" prop_isSubsetOf2
53 , testProperty "prop_size" prop_size
54 , testProperty "prop_findMax" prop_findMax
55 , testProperty "prop_findMin" prop_findMin
56 , testProperty "prop_ord" prop_ord
57 , testProperty "prop_readShow" prop_readShow
58 , testProperty "prop_foldR" prop_foldR
59 , testProperty "prop_foldR'" prop_foldR'
60 , testProperty "prop_foldL" prop_foldL
61 , testProperty "prop_foldL'" prop_foldL'
62 , testProperty "prop_map" prop_map
63 , testProperty "prop_maxView" prop_maxView
64 , testProperty "prop_minView" prop_minView
65 , testProperty "prop_split" prop_split
66 , testProperty "prop_splitMember" prop_splitMember
67 , testProperty "prop_partition" prop_partition
68 , testProperty "prop_filter" prop_filter
69 ]
70
71 ----------------------------------------------------------------
72 -- Unit tests
73 ----------------------------------------------------------------
74
75 test_lookupLT :: Assertion
76 test_lookupLT = do
77 lookupLT 3 (fromList [3, 5]) @?= Nothing
78 lookupLT 5 (fromList [3, 5]) @?= Just 3
79
80 test_lookupGT :: Assertion
81 test_lookupGT = do
82 lookupGT 4 (fromList [3, 5]) @?= Just 5
83 lookupGT 5 (fromList [3, 5]) @?= Nothing
84
85 test_lookupLE :: Assertion
86 test_lookupLE = do
87 lookupLE 2 (fromList [3, 5]) @?= Nothing
88 lookupLE 4 (fromList [3, 5]) @?= Just 3
89 lookupLE 5 (fromList [3, 5]) @?= Just 5
90
91 test_lookupGE :: Assertion
92 test_lookupGE = do
93 lookupGE 3 (fromList [3, 5]) @?= Just 3
94 lookupGE 4 (fromList [3, 5]) @?= Just 5
95 lookupGE 6 (fromList [3, 5]) @?= Nothing
96
97 {--------------------------------------------------------------------
98 Indexed
99 --------------------------------------------------------------------}
100
101 test_lookupIndex :: Assertion
102 test_lookupIndex = do
103 isJust (lookupIndex 2 (fromList [5,3])) @?= False
104 fromJust (lookupIndex 3 (fromList [5,3])) @?= 0
105 fromJust (lookupIndex 5 (fromList [5,3])) @?= 1
106 isJust (lookupIndex 6 (fromList [5,3])) @?= False
107
108 test_findIndex :: Assertion
109 test_findIndex = do
110 findIndex 3 (fromList [5,3]) @?= 0
111 findIndex 5 (fromList [5,3]) @?= 1
112
113 test_elemAt :: Assertion
114 test_elemAt = do
115 elemAt 0 (fromList [5,3]) @?= 3
116 elemAt 1 (fromList [5,3]) @?= 5
117
118 test_deleteAt :: Assertion
119 test_deleteAt = do
120 deleteAt 0 (fromList [5,3]) @?= singleton 5
121 deleteAt 1 (fromList [5,3]) @?= singleton 3
122
123 {--------------------------------------------------------------------
124 Arbitrary, reasonably balanced trees
125 --------------------------------------------------------------------}
126 instance (Enum a) => Arbitrary (Set a) where
127 arbitrary = sized (arbtree 0 maxkey)
128 where maxkey = 10000
129
130 arbtree :: (Enum a) => Int -> Int -> Int -> Gen (Set a)
131 arbtree lo hi n = do t <- gentree lo hi n
132 if balanced t then return t else arbtree lo hi n
133 where gentree lo hi n
134 | n <= 0 = return Tip
135 | lo >= hi = return Tip
136 | otherwise = do i <- choose (lo,hi)
137 m <- choose (1,70)
138 let (ml,mr) | m==(1::Int) = (1,2)
139 | m==2 = (2,1)
140 | m==3 = (1,1)
141 | otherwise = (2,2)
142 l <- gentree lo (i-1) (n `div` ml)
143 r <- gentree (i+1) hi (n `div` mr)
144 return (bin (toEnum i) l r)
145
146 {--------------------------------------------------------------------
147 Valid tree's
148 --------------------------------------------------------------------}
149 forValid :: (Enum a,Show a,Testable b) => (Set a -> b) -> Property
150 forValid f = forAll arbitrary $ \t ->
151 -- classify (balanced t) "balanced" $
152 classify (size t == 0) "empty" $
153 classify (size t > 0 && size t <= 10) "small" $
154 classify (size t > 10 && size t <= 64) "medium" $
155 classify (size t > 64) "large" $
156 balanced t ==> f t
157
158 forValidUnitTree :: Testable a => (Set Int -> a) -> Property
159 forValidUnitTree f = forValid f
160
161 prop_Valid :: Property
162 prop_Valid = forValidUnitTree $ \t -> valid t
163
164 {--------------------------------------------------------------------
165 Single, Member, Insert, Delete
166 --------------------------------------------------------------------}
167 prop_Single :: Int -> Bool
168 prop_Single x = (insert x empty == singleton x)
169
170 prop_Member :: [Int] -> Int -> Bool
171 prop_Member xs n =
172 let m = fromList xs
173 in all (\k -> k `member` m == (k `elem` xs)) (n : xs)
174
175 prop_NotMember :: [Int] -> Int -> Bool
176 prop_NotMember xs n =
177 let m = fromList xs
178 in all (\k -> k `notMember` m == (k `notElem` xs)) (n : xs)
179
180 test_LookupSomething :: (Int -> Set Int -> Maybe Int) -> (Int -> Int -> Bool) -> [Int] -> Bool
181 test_LookupSomething lookup' cmp xs =
182 let odd_sorted_xs = filter_odd $ nub $ sort xs
183 t = fromList odd_sorted_xs
184 test x = case List.filter (`cmp` x) odd_sorted_xs of
185 [] -> lookup' x t == Nothing
186 cs | 0 `cmp` 1 -> lookup' x t == Just (last cs) -- we want largest such element
187 | otherwise -> lookup' x t == Just (head cs) -- we want smallest such element
188 in all test xs
189
190 where filter_odd [] = []
191 filter_odd [_] = []
192 filter_odd (_ : o : xs) = o : filter_odd xs
193
194 prop_LookupLT :: [Int] -> Bool
195 prop_LookupLT = test_LookupSomething lookupLT (<)
196
197 prop_LookupGT :: [Int] -> Bool
198 prop_LookupGT = test_LookupSomething lookupGT (>)
199
200 prop_LookupLE :: [Int] -> Bool
201 prop_LookupLE = test_LookupSomething lookupLE (<=)
202
203 prop_LookupGE :: [Int] -> Bool
204 prop_LookupGE = test_LookupSomething lookupGE (>=)
205
206 prop_InsertValid :: Int -> Property
207 prop_InsertValid k = forValidUnitTree $ \t -> valid (insert k t)
208
209 prop_InsertDelete :: Int -> Set Int -> Property
210 prop_InsertDelete k t = not (member k t) ==> delete k (insert k t) == t
211
212 prop_DeleteValid :: Int -> Property
213 prop_DeleteValid k = forValidUnitTree $ \t -> valid (delete k (insert k t))
214
215 {--------------------------------------------------------------------
216 Balance
217 --------------------------------------------------------------------}
218 prop_Join :: Int -> Property
219 prop_Join x = forValidUnitTree $ \t ->
220 let (l,r) = split x t
221 in valid (join x l r)
222
223 prop_Merge :: Int -> Property
224 prop_Merge x = forValidUnitTree $ \t ->
225 let (l,r) = split x t
226 in valid (merge l r)
227
228 {--------------------------------------------------------------------
229 Union
230 --------------------------------------------------------------------}
231 prop_UnionValid :: Property
232 prop_UnionValid
233 = forValidUnitTree $ \t1 ->
234 forValidUnitTree $ \t2 ->
235 valid (union t1 t2)
236
237 prop_UnionInsert :: Int -> Set Int -> Bool
238 prop_UnionInsert x t = union t (singleton x) == insert x t
239
240 prop_UnionAssoc :: Set Int -> Set Int -> Set Int -> Bool
241 prop_UnionAssoc t1 t2 t3 = union t1 (union t2 t3) == union (union t1 t2) t3
242
243 prop_UnionComm :: Set Int -> Set Int -> Bool
244 prop_UnionComm t1 t2 = (union t1 t2 == union t2 t1)
245
246 prop_DiffValid :: Property
247 prop_DiffValid = forValidUnitTree $ \t1 ->
248 forValidUnitTree $ \t2 ->
249 valid (difference t1 t2)
250
251 prop_Diff :: [Int] -> [Int] -> Bool
252 prop_Diff xs ys = toAscList (difference (fromList xs) (fromList ys))
253 == List.sort ((List.\\) (nub xs) (nub ys))
254
255 prop_IntValid :: Property
256 prop_IntValid = forValidUnitTree $ \t1 ->
257 forValidUnitTree $ \t2 ->
258 valid (intersection t1 t2)
259
260 prop_Int :: [Int] -> [Int] -> Bool
261 prop_Int xs ys = toAscList (intersection (fromList xs) (fromList ys))
262 == List.sort (nub ((List.intersect) (xs) (ys)))
263
264 {--------------------------------------------------------------------
265 Lists
266 --------------------------------------------------------------------}
267 prop_Ordered :: Property
268 prop_Ordered = forAll (choose (5,100)) $ \n ->
269 let xs = [0..n::Int]
270 in fromAscList xs == fromList xs
271
272 prop_List :: [Int] -> Bool
273 prop_List xs = (sort (nub xs) == toList (fromList xs))
274
275 prop_DescList :: [Int] -> Bool
276 prop_DescList xs = (reverse (sort (nub xs)) == toDescList (fromList xs))
277
278 prop_AscDescList :: [Int] -> Bool
279 prop_AscDescList xs = toAscList s == reverse (toDescList s)
280 where s = fromList xs
281
282 prop_fromList :: [Int] -> Bool
283 prop_fromList xs
284 = case fromList xs of
285 t -> t == fromAscList sort_xs &&
286 t == fromDistinctAscList nub_sort_xs &&
287 t == List.foldr insert empty xs
288 where sort_xs = sort xs
289 nub_sort_xs = List.map List.head $ List.group sort_xs
290
291 {--------------------------------------------------------------------
292 Set operations are like IntSet operations
293 --------------------------------------------------------------------}
294 toIntSet :: Set Int -> IntSet.IntSet
295 toIntSet = IntSet.fromList . toList
296
297 -- Check that Set Int.isProperSubsetOf is the same as Set.isProperSubsetOf.
298 prop_isProperSubsetOf :: Set Int -> Set Int -> Bool
299 prop_isProperSubsetOf a b = isProperSubsetOf a b == IntSet.isProperSubsetOf (toIntSet a) (toIntSet b)
300
301 -- In the above test, isProperSubsetOf almost always returns False (since a
302 -- random set is almost never a subset of another random set). So this second
303 -- test checks the True case.
304 prop_isProperSubsetOf2 :: Set Int -> Set Int -> Bool
305 prop_isProperSubsetOf2 a b = isProperSubsetOf a c == (a /= c) where
306 c = union a b
307
308 prop_isSubsetOf :: Set Int -> Set Int -> Bool
309 prop_isSubsetOf a b = isSubsetOf a b == IntSet.isSubsetOf (toIntSet a) (toIntSet b)
310
311 prop_isSubsetOf2 :: Set Int -> Set Int -> Bool
312 prop_isSubsetOf2 a b = isSubsetOf a (union a b)
313
314 prop_size :: Set Int -> Bool
315 prop_size s = size s == List.length (toList s)
316
317 prop_findMax :: Set Int -> Property
318 prop_findMax s = not (null s) ==> findMax s == maximum (toList s)
319
320 prop_findMin :: Set Int -> Property
321 prop_findMin s = not (null s) ==> findMin s == minimum (toList s)
322
323 prop_ord :: Set Int -> Set Int -> Bool
324 prop_ord s1 s2 = s1 `compare` s2 == toList s1 `compare` toList s2
325
326 prop_readShow :: Set Int -> Bool
327 prop_readShow s = s == read (show s)
328
329 prop_foldR :: Set Int -> Bool
330 prop_foldR s = foldr (:) [] s == toList s
331
332 prop_foldR' :: Set Int -> Bool
333 prop_foldR' s = foldr' (:) [] s == toList s
334
335 prop_foldL :: Set Int -> Bool
336 prop_foldL s = foldl (flip (:)) [] s == List.foldl (flip (:)) [] (toList s)
337
338 prop_foldL' :: Set Int -> Bool
339 prop_foldL' s = foldl' (flip (:)) [] s == List.foldl' (flip (:)) [] (toList s)
340
341 prop_map :: Set Int -> Bool
342 prop_map s = map id s == s
343
344 prop_maxView :: Set Int -> Bool
345 prop_maxView s = case maxView s of
346 Nothing -> null s
347 Just (m,s') -> m == maximum (toList s) && s == insert m s' && m `notMember` s'
348
349 prop_minView :: Set Int -> Bool
350 prop_minView s = case minView s of
351 Nothing -> null s
352 Just (m,s') -> m == minimum (toList s) && s == insert m s' && m `notMember` s'
353
354 prop_split :: Set Int -> Int -> Bool
355 prop_split s i = case split i s of
356 (s1,s2) -> all (<i) (toList s1) && all (>i) (toList s2) && i `delete` s == union s1 s2
357
358 prop_splitMember :: Set Int -> Int -> Bool
359 prop_splitMember s i = case splitMember i s of
360 (s1,t,s2) -> all (<i) (toList s1) && all (>i) (toList s2) && t == i `member` s && i `delete` s == union s1 s2
361
362 prop_partition :: Set Int -> Int -> Bool
363 prop_partition s i = case partition odd s of
364 (s1,s2) -> all odd (toList s1) && all even (toList s2) && s == s1 `union` s2
365
366 prop_filter :: Set Int -> Int -> Bool
367 prop_filter s i = partition odd s == (filter odd s, filter even s)