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