Debian Codegraph @

test/main.hs

raw
1 {-# LANGUAGE CPP #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE FlexibleContexts #-}
4 {-# LANGUAGE NoImplicitPrelude #-}
5 {-# LANGUAGE NoMonomorphismRestriction #-}
6 {-# LANGUAGE DeriveDataTypeable #-}
7 {-# LANGUAGE PatternGuards #-}
8 {-# LANGUAGE TypeFamilies #-}
9 {-# OPTIONS_GHC -fno-warn-orphans #-}
10 import Test.Hspec
11 import Test.Hspec.QuickCheck
12 import ClassyPrelude hiding (undefined)
13 import Test.QuickCheck.Arbitrary
14 import Prelude (undefined)
15 import Control.Monad.Trans.Writer (tell, Writer, runWriter)
16 import qualified Data.Set as Set
17 import qualified Data.HashSet as HashSet
18
19 dictionaryProps
20 :: ( MapValue c ~ Char
21 , ContainerKey c ~ Int
22 , Arbitrary c
23 , IsMap c
24 , Eq c
25 , Show c
26 )
27 => c
28 -> Spec
29 dictionaryProps dummy = do
30 prop "insert x y (insert x z c) == insert x y c" $ \x y z c ->
31 insertMap x y (insertMap x z (c `asTypeOf` dummy)) == insertMap x y c
32 prop "insertMap x y (deleteMap x c) == insertMap x y c" $ \x y c ->
33 insertMap x y (deleteMap x (c `asTypeOf` dummy)) == insertMap x y c
34 prop "deleteMap x (insertMap x y c) == deleteMap x c" $ \x y c ->
35 mapFromList (mapToList $ deleteMap x (insertMap x y (c `asTypeOf` dummy))) == (mapFromList (mapToList ((deleteMap x c) `asTypeOf` dummy) :: [(Int, Char)]) `asTypeOf` dummy)
36 prop "lookup k (insertMap k v empty) == Just v" $ \k v ->
37 lookup k (insertMap k v mempty `asTypeOf` dummy) == Just v
38 prop "lookup k (deleteMap k c) == Nothing" $ \k c ->
39 lookup k (deleteMap k c`asTypeOf` dummy) == Nothing
40
41 mapProps :: ( i ~ Element c
42 , MonoFoldable c
43 , Eq c
44 , Arbitrary c
45 , Show c
46 )
47 => ((i -> i) -> c -> c)
48 -> ([i] -> c)
49 -> c
50 -> (i -> i)
51 -> (i -> i)
52 -> Spec
53 mapProps map' pack' dummy f g = do
54 prop "map f c == pack (map f (unpack c))" $ \c ->
55 map' f (c `asTypeOf` dummy) == pack' (fmap f (unpack c))
56 prop "map (f . g) c == map f (map g c)" $ \c ->
57 map' (g . f) (c `asTypeOf` dummy) == map' g (map' f c)
58
59 concatMapProps :: ( MonoFoldable c
60 , IsSequence c
61 , Eq c
62 , Arbitrary c
63 , Show c
64 )
65 => c
66 -> (Element c -> c)
67 -> Spec
68 concatMapProps dummy f = do
69 prop "concatMap f c == pack (concatMap (unpack . f) (unpack c))" $ \c ->
70 concatMap f (c `asTypeOf` dummy) == pack (concatMap (unpack . f) (unpack c))
71
72 filterProps :: ( Eq c
73 , Show c
74 , IsSequence c
75 , Arbitrary c
76 )
77 => c
78 -> (Element c -> Bool)
79 -> Spec
80 filterProps dummy f = do
81 prop "filter f c == pack (filter f (unpack c))" $ \c ->
82 (repack (filter f (c `asTypeOf` dummy)) `asTypeOf` dummy) == pack (filter f (unpack c))
83
84 filterMProps :: ( Eq c
85 , Show c
86 , IsSequence c
87 , Arbitrary c
88 )
89 => c
90 -> (Element c -> Bool)
91 -> Spec
92 filterMProps dummy f' = do
93 prop "filterM f c == fmap pack (filterM f (unpack c))" $ \c ->
94 runIdentity (fmap repack (filterM f (c `asTypeOf` dummy))) `asTypeOf` dummy == runIdentity (fmap pack (filterM f (unpack c)))
95 where
96 f = return . f'
97
98 lengthProps :: ( Show c
99 , MonoFoldable c
100 , Monoid c
101 , Arbitrary c
102 )
103 => c
104 -> Spec
105 lengthProps dummy = do
106 prop "length c == fromIntegral (length (unpack c))" $ \c ->
107 length (c `asTypeOf` dummy) == fromIntegral (length (unpack c))
108 prop "null c == (length c == 0)" $ \c ->
109 null (c `asTypeOf` dummy) == (length c == 0)
110 prop "length (x ++ y) <= length x + length y" $ \x y ->
111 length (x ++ y `asTypeOf` dummy) <= length x + length y
112 prop "length (x ++ y) >= max (length x) (length y)" $ \x y ->
113 length (x ++ y `asTypeOf` dummy) >= max (length x) (length y)
114 prop "length (x ++ empty) == length x" $ \x ->
115 length (x ++ mempty `asTypeOf` dummy) == length x
116 prop "null empty" $ null (mempty `asTypeOf` dummy)
117
118 mapMProps :: ( Eq c
119 , Show c
120 , IsSequence c
121 , Arbitrary c
122 , Element c ~ Int
123 )
124 => c
125 -> Spec
126 mapMProps dummy = do
127 let f :: Int -> Writer [Int] Int
128 f x = tell [x] >> return x
129 prop "omapM f c == omapM f (toList c)" $ \c ->
130 runWriter (omapM f (c `asTypeOf` dummy)) ==
131 let (x, y) = runWriter (omapM f (toList c))
132 in (pack x, y)
133
134 mapM_Props :: ( Eq (Element c)
135 , Show c
136 , MonoFoldable c
137 , Arbitrary c
138 )
139 => c
140 -> Spec
141 mapM_Props dummy = do
142 let f x = tell [x]
143 prop "mapM_ f c == mapM_ f (toList c)" $ \c ->
144 runWriter (mapM_ f (c `asTypeOf` dummy)) == runWriter (mapM_ f (toList c))
145
146 foldProps :: ( Eq a
147 , Show c
148 , MonoFoldable c
149 , Arbitrary c
150 )
151 => c
152 -> (a -> Element c -> a)
153 -> a
154 -> Spec
155 foldProps dummy f accum =
156 prop "foldl' f accum c == foldl' f accum (toList c)" $ \c ->
157 foldl' f accum (c `asTypeOf` dummy) == foldl' f accum (toList c)
158
159 replicateProps :: ( Eq a
160 , Show (Element c)
161 , IsSequence a
162 , IsSequence c
163 , Arbitrary (Element c)
164 , Element a ~ Element c
165 )
166 => a
167 -> (c -> a)
168 -> Spec
169 replicateProps dummy pack' =
170 prop "replicate i a == pack (replicate i a)" $ \{- takes too long i-} a ->
171 (replicate i a `asTypeOf` dummy) == pack' (replicate i a)
172 where
173 i = 3
174
175 chunkProps :: ( Eq a
176 , Show a
177 , Arbitrary a
178 , LazySequence a s
179 )
180 => a
181 -> Spec
182 chunkProps dummy = do
183 prop "fromChunks . toChunks == id" $ \a ->
184 fromChunks (toChunks (a `asTypeOf` dummy)) == a
185 prop "fromChunks . return . concat . toChunks == id" $ \a ->
186 fromChunks [concat $ toChunks (a `asTypeOf` dummy)] == a
187
188 suffixProps :: ( Eq c
189 , Show c
190 , Arbitrary c
191 , IsSequence c
192 , Eq (Element c)
193 )
194 => c
195 -> Spec
196 suffixProps dummy = do
197 prop "y `isSuffixOf` (x ++ y)" $ \x y ->
198 (y `asTypeOf` dummy) `isSuffixOf` (x ++ y)
199 prop "stripSuffix y (x ++ y) == Just x" $ \x y ->
200 stripSuffix y (x ++ y) == Just (x `asTypeOf` dummy)
201 prop "isJust (stripSuffix x y) == isSuffixOf x y" $ \x y ->
202 isJust (stripSuffix x y) == isSuffixOf x (y `asTypeOf` dummy)
203 prop "dropSuffix y (x ++ y) == x" $ \x y ->
204 dropSuffix y (x ++ y) == (x `asTypeOf` dummy)
205 prop "dropSuffix x y == y || x `isSuffixOf` y" $ \x y ->
206 dropSuffix x y == y || x `isSuffixOf` (y `asTypeOf` dummy)
207
208 replicateMProps :: ( Eq a
209 , Show (Index a)
210 , Show (Element a)
211 , IsSequence a
212 , Arbitrary (Index a)
213 , Arbitrary (Element a)
214 )
215 => a
216 -> Spec
217 replicateMProps dummy = do
218 prop "runIdentity (replicateM i (return x)) == replicate i x" $ \i' x ->
219 let i = i' `mod` 20
220 in runIdentity (replicateM i (return x)) == (replicate i x `asTypeOf` dummy)
221
222 utf8Props :: ( Eq t
223 , Show t
224 , Arbitrary t
225 , Textual t
226 , Utf8 t b
227 )
228 => t
229 -> Spec
230 utf8Props dummy = do
231 prop "decodeUtf8 . encodeUtf8 == id" $ \t ->
232 decodeUtf8 (encodeUtf8 t) == (t `asTypeOf` dummy)
233
234 compareLengthProps :: ( MonoFoldable c
235 , Arbitrary c
236 , Show c
237 )
238 => c
239 -> Spec
240 compareLengthProps dummy = do
241 prop "compare (length c) i == compareLength c i" $ \i c ->
242 compare (length c) i == compareLength (c `asTypeOf` dummy) i
243
244 prefixProps :: ( Eq c
245 , IsSequence c
246 , Eq (Element c)
247 , Arbitrary c
248 , Show c
249 )
250 => c
251 -> Spec
252 prefixProps dummy = do
253 prop "x `isPrefixOf` (x ++ y)" $ \x y ->
254 (x `asTypeOf` dummy) `isPrefixOf` (x ++ y)
255 prop "stripPrefix x (x ++ y) == Just y" $ \x y ->
256 stripPrefix x (x ++ y) == Just (y `asTypeOf` dummy)
257 prop "stripPrefix x y == Nothing || x `isPrefixOf` y" $ \x y ->
258 stripPrefix x y == Nothing || x `isPrefixOf` (y `asTypeOf` dummy)
259 prop "dropPrefix x (x ++ y) == y" $ \x y ->
260 dropPrefix x (x ++ y) == (y `asTypeOf` dummy)
261 prop "dropPrefix x y == y || x `isPrefixOf` y" $ \x y ->
262 dropPrefix x y == y || x `isPrefixOf` (y `asTypeOf` dummy)
263
264 main :: IO ()
265 main = hspec $ do
266 describe "dictionary" $ do
267 describe "Data.Map" $ dictionaryProps (undefined :: Map Int Char)
268 describe "Data.HashMap" $ dictionaryProps (undefined :: HashMap Int Char)
269 describe "assoc list" $ dictionaryProps (undefined :: [(Int, Char)])
270 describe "map" $ do
271 describe "list" $ mapProps fmap pack (undefined :: [Int]) (+ 1) (+ 2)
272 describe "Data.Vector" $ mapProps fmap pack (undefined :: Vector Int) (+ 1) (+ 2)
273 describe "Data.Vector.Unboxed" $ mapProps omap pack (undefined :: UVector Int) (+ 1) (+ 2)
274 describe "Data.Set" $ mapProps Set.map setFromList (undefined :: Set Int) (+ 1) (+ 2)
275 describe "Data.HashSet" $ mapProps HashSet.map setFromList (undefined :: HashSet Int) (+ 1) (+ 2)
276 describe "Data.ByteString" $ mapProps omap pack (undefined :: ByteString) (+ 1) (+ 2)
277 describe "Data.ByteString.Lazy" $ mapProps omap pack (undefined :: LByteString) (+ 1) (+ 2)
278 describe "Data.Text" $ mapProps omap pack (undefined :: Text) succ succ
279 describe "Data.Text.Lazy" $ mapProps omap pack (undefined :: LText) succ succ
280 describe "Data.Sequence" $ mapProps fmap pack (undefined :: Seq Int) succ succ
281 describe "concatMap" $ do
282 describe "list" $ concatMapProps (undefined :: [Int]) (\i -> [i + 1, i + 2])
283 describe "Data.Vector" $ concatMapProps (undefined :: Vector Int) (\i -> fromList [i + 1, i + 2])
284 describe "Data.Vector.Unboxed" $ concatMapProps (undefined :: UVector Int) (\i -> fromList [i + 1, i + 2])
285 describe "Data.ByteString" $ concatMapProps (undefined :: ByteString) (\i -> fromList [i + 1, i + 2])
286 describe "Data.ByteString.Lazy" $ concatMapProps (undefined :: LByteString) (\i -> fromList [i + 1, i + 2])
287 describe "Data.Text" $ concatMapProps (undefined :: Text) (\c -> pack [succ c, succ $ succ c])
288 describe "Data.Text.Lazy" $ concatMapProps (undefined :: LText) (\c -> pack [succ c, succ $ succ c])
289 describe "Data.Sequence" $ concatMapProps (undefined :: Seq Int) (\i -> pack [i + 1, i + 2])
290 describe "filter" $ do
291 describe "list" $ filterProps (undefined :: [Int]) (< 20)
292 describe "Data.Vector" $ filterProps (undefined :: Vector Int) (< 20)
293 describe "Data.Vector.Unboxed" $ filterProps (undefined :: UVector Int) (< 20)
294 describe "Data.ByteString" $ filterProps (undefined :: ByteString) (< 20)
295 describe "Data.ByteString.Lazy" $ filterProps (undefined :: LByteString) (< 20)
296 describe "Data.Text" $ filterProps (undefined :: Text) (< 'A')
297 describe "Data.Text.Lazy" $ filterProps (undefined :: LText) (< 'A')
298 {- FIXME
299 describe "Data.Map" $ filterProps (undefined :: Map Int Char) (\(i, _) -> i < 20)
300 describe "Data.HashMap" $ filterProps (undefined :: HashMap Int Char) (\(i, _) -> i < 20)
301 describe "Data.Set" $ filterProps (undefined :: Set Int) (< 20)
302 -}
303 describe "Data.Sequence" $ filterProps (undefined :: Seq Int) (< 20)
304 describe "filterM" $ do
305 describe "list" $ filterMProps (undefined :: [Int]) (< 20)
306 describe "Data.Vector" $ filterMProps (undefined :: Vector Int) (< 20)
307 describe "Data.Vector.Unboxed" $ filterMProps (undefined :: Vector Int) (< 20)
308 describe "Data.Sequence" $ filterMProps (undefined :: Seq Int) (< 20)
309 describe "length" $ do
310 describe "list" $ lengthProps (undefined :: [Int])
311 describe "Data.Vector" $ lengthProps (undefined :: Vector Int)
312 describe "Data.Vector.Unboxed" $ lengthProps (undefined :: UVector Int)
313 describe "Data.ByteString" $ lengthProps (undefined :: ByteString)
314 describe "Data.ByteString.Lazy" $ lengthProps (undefined :: LByteString)
315 describe "Data.Text" $ lengthProps (undefined :: Text)
316 describe "Data.Text.Lazy" $ lengthProps (undefined :: LText)
317 describe "Data.Map" $ lengthProps (undefined :: Map Int Char)
318 describe "Data.HashMap" $ lengthProps (undefined :: HashMap Int Char)
319 describe "Data.Set" $ lengthProps (undefined :: Set Int)
320 describe "Data.HashSet" $ lengthProps (undefined :: HashSet Int)
321 describe "Data.Sequence" $ lengthProps (undefined :: Seq Int)
322 describe "mapM" $ do
323 describe "list" $ mapMProps (undefined :: [Int])
324 describe "Data.Vector" $ mapMProps (undefined :: Vector Int)
325 describe "Data.Vector.Unboxed" $ mapMProps (undefined :: UVector Int)
326 describe "Seq" $ mapMProps (undefined :: Seq Int)
327 describe "mapM_" $ do
328 describe "list" $ mapM_Props (undefined :: [Int])
329 describe "Data.Vector" $ mapM_Props (undefined :: Vector Int)
330 describe "Data.Vector.Unboxed" $ mapM_Props (undefined :: UVector Int)
331 describe "Set" $ mapM_Props (undefined :: Set Int)
332 describe "HashSet" $ mapM_Props (undefined :: HashSet Int)
333 describe "Seq" $ mapM_Props (undefined :: Seq Int)
334 describe "fold" $ do
335 let f = flip (:)
336 describe "list" $ foldProps (undefined :: [Int]) f []
337 describe "Data.Vector" $ foldProps (undefined :: Vector Int) f []
338 describe "Data.Vector.Unboxed" $ foldProps (undefined :: UVector Int) f []
339 describe "Data.ByteString" $ foldProps (undefined :: ByteString) f []
340 describe "Data.ByteString.Lazy" $ foldProps (undefined :: LByteString) f []
341 describe "Data.Text" $ foldProps (undefined :: Text) f []
342 describe "Data.Text.Lazy" $ foldProps (undefined :: LText) f []
343 describe "Data.Set" $ foldProps (undefined :: Set Int) f []
344 describe "Data.HashSet" $ foldProps (undefined :: HashSet Int) f []
345 describe "Data.Sequence" $ foldProps (undefined :: Seq Int) f []
346 describe "replicate" $ do
347 describe "list" $ replicateProps (undefined :: [Int]) pack
348 describe "Data.Vector" $ replicateProps (undefined :: Vector Int) pack
349 describe "Data.Vector.Unboxed" $ replicateProps (undefined :: UVector Int) pack
350 describe "Data.ByteString" $ replicateProps (undefined :: ByteString) pack
351 describe "Data.ByteString.Lazy" $ replicateProps (undefined :: LByteString) pack
352 describe "Data.Text" $ replicateProps (undefined :: Text) pack
353 describe "Data.Text.Lazy" $ replicateProps (undefined :: LText) pack
354 describe "Data.Sequence" $ replicateProps (undefined :: Seq Int) pack
355 describe "chunks" $ do
356 describe "ByteString" $ chunkProps (asLByteString undefined)
357 describe "Text" $ chunkProps (asLText undefined)
358 describe "Suffix" $ do
359 describe "list" $ suffixProps (undefined :: [Int])
360 describe "Text" $ suffixProps (undefined :: Text)
361 describe "LText" $ suffixProps (undefined :: LText)
362 describe "ByteString" $ suffixProps (undefined :: ByteString)
363 describe "LByteString" $ suffixProps (undefined :: LByteString)
364 describe "Vector" $ suffixProps (undefined :: Vector Int)
365 describe "UVector" $ suffixProps (undefined :: UVector Int)
366 describe "Seq" $ suffixProps (undefined :: Seq Int)
367 describe "replicateM" $ do
368 describe "list" $ replicateMProps (undefined :: [Int])
369 describe "Vector" $ replicateMProps (undefined :: Vector Int)
370 describe "UVector" $ replicateMProps (undefined :: UVector Int)
371 describe "Seq" $ replicateMProps (undefined :: Seq Int)
372 describe "encode/decode UTF8" $ do
373 describe "Text" $ utf8Props (undefined :: Text)
374 describe "LText" $ utf8Props (undefined :: LText)
375 describe "compareLength" $ do
376 describe "list" $ compareLengthProps (undefined :: [Int])
377 describe "Text" $ compareLengthProps (undefined :: Text)
378 describe "LText" $ compareLengthProps (undefined :: LText)
379 describe "Prefix" $ do
380 describe "list" $ prefixProps (undefined :: [Int])
381 describe "Text" $ prefixProps (undefined :: Text)
382 describe "LText" $ prefixProps (undefined :: LText)
383 describe "ByteString" $ prefixProps (undefined :: ByteString)
384 describe "LByteString" $ prefixProps (undefined :: LByteString)
385 describe "Vector" $ prefixProps (undefined :: Vector Int)
386 describe "UVector" $ prefixProps (undefined :: UVector Int)
387 describe "Seq" $ prefixProps (undefined :: Seq Int)
388 {- This tests depend on timing and are unreliable. Instead, we're relying
389 on the test suite in safe-exceptions itself.
390 describe "any exceptions" $ do
391 it "catchAny" $ do
392 failed <- newIORef 0
393 tid <- forkIO $ do
394 catchAny
395 (threadDelay 20000)
396 (const $ writeIORef failed 1)
397 writeIORef failed 2
398 threadDelay 10000
399 throwTo tid DummyException
400 threadDelay 50000
401 didFail <- readIORef failed
402 liftIO $ didFail `shouldBe` (0 :: Int)
403 it "tryAny" $ do
404 failed <- newIORef False
405 tid <- forkIO $ do
406 _ <- tryAny $ threadDelay 20000
407 writeIORef failed True
408 threadDelay 10000
409 throwTo tid DummyException
410 threadDelay 50000
411 didFail <- readIORef failed
412 liftIO $ didFail `shouldBe` False
413 it "tryAnyDeep" $ do
414 eres <- tryAny $ return $!! impureThrow DummyException
415 case eres of
416 Left e
417 | Just DummyException <- fromException e -> return ()
418 | otherwise -> error "Expected a DummyException"
419 Right () -> error "Expected an exception" :: IO ()
420 -}
421 it "basic DList functionality" $
422 (toList $ asDList $ mconcat
423 [ fromList [1, 2]
424 , singleton 3
425 , cons 4 mempty
426 , fromList $ applyDList (singleton 5 ++ singleton 6) [7, 8]
427 ]) `shouldBe` [1..8 :: Int]
428
429 describe "Data.ByteVector" $ do
430 prop "toByteVector" $ \ws ->
431 (otoList . toByteVector . fromList $ ws) `shouldBe` ws
432
433 prop "fromByteVector" $ \ws ->
434 (otoList . fromByteVector . fromList $ ws) `shouldBe` ws
435
436 data DummyException = DummyException
437 deriving (Show, Typeable)
438 instance Exception DummyException
439
440 instance Arbitrary (HashMap Int Char) where
441 arbitrary = mapFromList <$> arbitrary
442 instance Arbitrary (Vector Int) where
443 arbitrary = fromList <$> arbitrary
444 instance Arbitrary (UVector Int) where
445 arbitrary = fromList <$> arbitrary
446 instance Arbitrary (HashSet Int) where
447 arbitrary = setFromList <$> arbitrary
448 instance Arbitrary ByteString where
449 arbitrary = fromList <$> arbitrary
450 instance Arbitrary LByteString where
451 arbitrary = fromList <$> arbitrary
452 instance Arbitrary Text where
453 arbitrary = fromList <$> arbitrary
454 instance Arbitrary LText where
455 arbitrary = fromList <$> arbitrary
456
457 #if !MIN_VERSION_QuickCheck(2,8,2)
458 instance Arbitrary (Seq Int) where
459 arbitrary = fromList <$> arbitrary
460 instance Arbitrary (Set Int) where
461 arbitrary = setFromList <$> arbitrary
462 instance Arbitrary (Map Int Char) where
463 arbitrary = mapFromList <$> arbitrary
464 #endif