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