on 668f2ef help sign in
815 lines 24 kB history raw
1{-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
3module Main (main) where
5import Data.List (sort)
6import Data.Semigroup (Sum(..), getSum)
7import Streamly.Test.Common (checkListEqual, listEquals)
8import Test.QuickCheck
9 ( Gen
10 , Property
11 , arbitrary
12 , choose
13 , forAll
14 , listOf
15 , listOf1
16 , property
17 , vectorOf
18 , withMaxSuccess
19 , generate
20 )
21import Control.Monad.IO.Class (liftIO)
22import Test.QuickCheck.Monadic (monadicIO, assert, run)
24import qualified Data.Map
25import qualified Prelude
26import qualified Streamly.Internal.Data.Array.Mut as MArray
27import qualified Streamly.Internal.Data.Fold as Fold
28import qualified Streamly.Internal.Data.Fold.Container as Fold
29import qualified Streamly.Internal.Data.Stream as Stream
31import Prelude hiding
32 (maximum, minimum, elem, notElem, null, product, sum, head, last, take)
33import Test.Hspec as H
34import Test.Hspec.QuickCheck
36maxStreamLen :: Int
37maxStreamLen = 1000
39intMin :: Int
40intMin = minBound
42intMax :: Int
43intMax = maxBound
45min_value :: Int
46min_value = 0
48max_value :: Int
49max_value = 10000
51chooseInt :: (Int, Int) -> Gen Int
52chooseInt = choose
54{-# INLINE maxStreamLen #-}
55{-# INLINE intMin #-}
56{-# INLINE intMax #-}
58rollingHashFirstN :: Property
59rollingHashFirstN =
60 forAll (choose (0, maxStreamLen)) $ \len ->
61 forAll (choose (0, len)) $ \n ->
62 forAll (vectorOf len (arbitrary :: Gen Int)) $ \vec ->
63 monadicIO $ do
64 a <- run
65 $ Stream.fold Fold.rollingHash
66 $ Stream.take n
67 $ Stream.fromList vec
68 b <- run
69 $ Stream.fold (Fold.rollingHashFirstN n)
70 $ Stream.fromList vec
71 assert $ a == b
73head :: [Int] -> Expectation
74head ls = Stream.fold Fold.one (Stream.fromList ls) `shouldReturn` headl ls
76headl :: [a] -> Maybe a
77headl [] = Nothing
78headl (x:_) = Just x
80length :: [Int] -> Expectation
81length ls = Stream.fold Fold.length (Stream.fromList ls) `shouldReturn` Prelude.length ls
83sum :: [Int] -> Expectation
84sum ls = Stream.fold Fold.sum (Stream.fromList ls) `shouldReturn` Prelude.sum ls
86product :: [Int] -> Expectation
87product ls =
88 Stream.fold Fold.product (Stream.fromList ls) `shouldReturn` Prelude.product ls
90lesser :: (a -> a -> Ordering) -> a -> a -> a
91lesser f x y = if f x y == LT then x else y
93greater :: (a -> a -> Ordering) -> a -> a -> a
94greater f x y = if f x y == GT then x else y
96foldMaybe :: (b -> a -> b) -> b -> [a] -> Maybe b
97foldMaybe f acc ls =
98 case ls of
99 [] -> Nothing
100 _ -> Just (foldl f acc ls)
102maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
103maximumBy genmin f ls =
104 Stream.fold (Fold.maximumBy f) (Stream.fromList ls)
105 `shouldReturn` foldMaybe (greater f) genmin ls
107maximum :: (Show a, Ord a) => a -> [a] -> Expectation
108maximum genmin ls =
109 Stream.fold Fold.maximum (Stream.fromList ls)
110 `shouldReturn` foldMaybe (greater compare) genmin ls
112minimumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
113minimumBy genmax f ls =
114 Stream.fold (Fold.minimumBy f) (Stream.fromList ls)
115 `shouldReturn` foldMaybe (lesser f) genmax ls
117minimum :: (Show a, Ord a) => a -> [a] -> Expectation
118minimum genmax ls =
119 Stream.fold Fold.minimum (Stream.fromList ls)
120 `shouldReturn` foldMaybe (lesser compare) genmax ls
122toList :: [Int] -> Expectation
123toList ls = Stream.fold Fold.toList (Stream.fromList ls) `shouldReturn` ls
125toListRev :: [Int] -> Expectation
126toListRev ls = Stream.fold Fold.toListRev (Stream.fromList ls) `shouldReturn` reverse ls
128safeLast :: [a] -> Maybe a
129safeLast [] = Nothing
130safeLast (x:[]) = Just x
131safeLast (_:xs) = safeLast xs
133last :: [String] -> Expectation
134last ls = Stream.fold Fold.last (Stream.fromList ls) `shouldReturn` safeLast ls
136mapMaybe :: [Int] -> Expectation
137mapMaybe ls =
138 let maybeEven x =
139 if even x
140 then Just x
141 else Nothing
142 f = Fold.mapMaybe maybeEven Fold.toList
143 in Stream.fold f (Stream.fromList ls) `shouldReturn` filter even ls
145nth :: Int -> [a] -> Maybe a
146nth idx (x : xs)
147 | idx == 0 = Just x
148 | idx < 0 = Nothing
149 | otherwise = nth (idx - 1) xs
150nth _ [] = Nothing
152index :: Int -> [String] -> Expectation
153index idx ls =
154 let x = Stream.fold (Fold.index idx) (Stream.fromList ls)
155 in x `shouldReturn` nth idx ls
157find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
158find f ls = do
159 y <- Stream.fold (Fold.findIndex f) (Stream.fromList ls)
160 case y of
161 Nothing ->
162 let fld = Stream.fold (Fold.find f) (Stream.fromList ls)
163 in fld `shouldReturn` Nothing
164 Just idx ->
165 let fld = Stream.fold (Fold.any f) (Stream.fromList $ Prelude.take idx ls)
166 in fld `shouldReturn` False
168neg :: (a -> Bool) -> a -> Bool
169neg f x = not (f x)
171findIndex :: (a -> Bool) -> [a] -> Expectation
172findIndex f ls = do
173 y <- Stream.fold (Fold.findIndex f) (Stream.fromList ls)
174 case y of
175 Nothing ->
176 let fld = Stream.fold (Fold.all $ neg f) (Stream.fromList ls)
177 in fld `shouldReturn` True
178 Just idx ->
179 if idx == 0
180 then
181 Stream.fold (Fold.all f) (Stream.fromList []) `shouldReturn` True
182 else
183 Stream.fold (Fold.all f) (Stream.fromList $ Prelude.take idx ls)
184 `shouldReturn` False
186predicate :: Int -> Bool
187predicate x = x * x < 100
189elemIndex :: Int -> [Int] -> Expectation
190elemIndex elm ls = do
191 y <- Stream.fold (Fold.elemIndex elm) (Stream.fromList ls)
192 case y of
193 Nothing ->
194 let fld = Stream.fold (Fold.any (== elm)) (Stream.fromList ls)
195 in fld `shouldReturn` False
196 Just idx ->
197 let fld =
198 Stream.fold
199 (Fold.any (== elm))
200 (Stream.fromList $ Prelude.take idx ls)
201 in fld `shouldReturn` False
203null :: [Int] -> Expectation
204null ls =
205 Stream.fold Fold.null (Stream.fromList ls)
206 `shouldReturn`
207 case ls of
208 [] -> True
209 _ -> False
211elem :: Int -> [Int] -> Expectation
212elem elm ls = do
213 y <- Stream.fold (Fold.elem elm) (Stream.fromList ls)
214 let fld = Stream.fold (Fold.any (== elm)) (Stream.fromList ls)
215 fld `shouldReturn` y
217notElem :: Int -> [Int] -> Expectation
218notElem elm ls = do
219 y <- Stream.fold (Fold.notElem elm) (Stream.fromList ls)
220 let fld = Stream.fold (Fold.any (== elm)) (Stream.fromList ls)
221 fld `shouldReturn` not y
223all :: (a -> Bool) -> [a] -> Expectation
224all f ls =
225 Stream.fold (Fold.all f) (Stream.fromList ls) `shouldReturn` Prelude.all f ls
227any :: (a -> Bool) -> [a] -> Expectation
228any f ls = Stream.fold (Fold.any f) (Stream.fromList ls) `shouldReturn` Prelude.any f ls
230and :: [Bool] -> Expectation
231and ls = Stream.fold Fold.and (Stream.fromList ls) `shouldReturn` Prelude.and ls
233or :: [Bool] -> Expectation
234or ls = Stream.fold Fold.or (Stream.fromList ls) `shouldReturn` Prelude.or ls
236take :: [Int] -> Property
237take ls =
238 forAll (chooseInt (-1, Prelude.length ls + 2)) $ \n ->
239 Stream.fold (Fold.take n Fold.toList) (Stream.fromList ls)
240 `shouldReturn` Prelude.take n ls
242takeEndBy_ :: Property
243takeEndBy_ =
244 forAll (listOf (chooseInt (0, 1))) $ \ls ->
245 let p = (== 1)
246 f = Fold.takeEndBy_ p Fold.toList
247 ys = Prelude.takeWhile (not . p) ls
248 in case Stream.fold f (Stream.fromList ls) of
249 Right xs -> checkListEqual xs ys
250 Left _ -> property False
252takeEndByOrMax :: Property
253takeEndByOrMax =
254 forAll (chooseInt (min_value, max_value)) $ \n ->
255 forAll (listOf (chooseInt (0, 1))) $ \ls ->
256 let p = (== 1)
257 f = Fold.takeEndBy_ p (Fold.take n Fold.toList)
258 ys = Prelude.take n (Prelude.takeWhile (not . p) ls)
259 in case Stream.fold f (Stream.fromList ls) of
260 Right xs -> checkListEqual xs ys
261 Left _ -> property False
263chooseFloat :: (Float, Float) -> Gen Float
264chooseFloat = choose
266drain :: [Int] -> Expectation
267drain ls = Stream.fold Fold.drain (Stream.fromList ls) `shouldReturn` ()
269drainBy :: [Int] -> Expectation
270drainBy ls = Stream.fold (Fold.drainBy return) (Stream.fromList ls) `shouldReturn` ()
272mean :: Property
273mean =
274 forAll (listOf1 (chooseFloat (-100.0, 100.0)))
275 $ \ls0 -> withMaxSuccess 1000 $ monadicIO $ action ls0
277 where
279 action ls = do
280 v1 <- run $ Stream.fold Fold.mean (Stream.fromList ls)
281 let v2 = Prelude.sum ls / fromIntegral (Prelude.length ls)
282 assert (abs (v1 - v2) < 0.0001)
284stdDev :: Property
285stdDev =
286 forAll (listOf1 (chooseFloat (-100.0, 100.0)))
287 $ \ls0 -> withMaxSuccess 1000 $ monadicIO $ action ls0
289 where
291 action ls = do
292 v1 <- run $ Stream.fold Fold.stdDev (Stream.fromList ls)
293 let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
294 se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
295 sd = sqrt $ se / fromIntegral (Prelude.length ls)
296 assert (abs (v1 - sd) < 0.0001 )
298variance :: Property
299variance =
300 forAll (listOf1 (chooseFloat (-100.0, 100.0)))
301 $ \ls0 -> withMaxSuccess 1000 $ monadicIO $ action ls0
303 where
305 action ls = do
306 v1 <- run $ Stream.fold Fold.variance (Stream.fromList ls)
307 let avg = Prelude.sum ls / fromIntegral (Prelude.length ls)
308 se = Prelude.sum (fmap (\x -> (x - avg) * (x - avg)) ls)
309 vr = se / fromIntegral (Prelude.length ls)
310 assert (abs (v1 - vr) < 0.01 )
312mconcat :: Property
313mconcat =
314 forAll (listOf1 (chooseInt (intMin, intMax)))
315 $ \ls0 -> monadicIO $ action ls0
317 where
319 action ls = do
320 v1 <- run $ Stream.fold Fold.mconcat (fmap Sum $ Stream.fromList ls)
321 let v2 = Prelude.sum ls
322 assert (getSum v1 == v2)
324foldMap :: Property
325foldMap =
326 forAll (listOf1 (chooseInt (intMin, intMax)))
327 $ \ls0 -> monadicIO $ action ls0
329 where
331 action ls = do
332 v1 <- run $ Stream.fold (Fold.foldMap Sum) $ Stream.fromList ls
333 let v2 = Prelude.sum ls
334 assert (getSum v1 == v2)
336foldMapM :: Property
337foldMapM =
338 forAll (listOf1 (chooseInt (intMin, intMax)))
339 $ \ls0 -> monadicIO $ action ls0
341 where
343 action ls = do
344 v1 <- run $ Stream.fold (Fold.foldMapM (return . Sum)) $ Stream.fromList ls
345 let v2 = Prelude.sum ls
346 assert (getSum v1 == v2)
348lookup :: Property
349lookup =
350 forAll (chooseInt (1, 15))
351 $ \key0 ->monadicIO $ action key0
353 where
355 action key = do
356 let ls = [ (1, "first"), (2, "second"), (3, "third"), (4, "fourth")
357 , (5, "fifth"), (6, "fifth+first"), (7, "fifth+second")
358 , (8, "fifth+third"), (9, "fifth+fourth")
359 , (10, "fifth+fifth")
360 ]
361 v1 <- run $ Stream.fold (Fold.lookup key) $ Stream.fromList ls
362 let v2 = Prelude.lookup key ls
363 assert (v1 == v2)
365rmapM :: Property
366rmapM =
367 forAll (listOf1 (chooseInt (intMin, intMax)))
368 $ \ls0 -> monadicIO $ action ls0
370 where
372 action ls = do
373 let addLen x = return $ x + Prelude.length ls
374 fld = Fold.rmapM addLen Fold.sum
375 v2 = foldl (+) (Prelude.length ls) ls
376 v1 <- run $ Stream.fold fld $ Stream.fromList ls
377 assert (v1 == v2)
379teeWithLength :: Property
380teeWithLength =
381 forAll (listOf1 (chooseInt (intMin, intMax)))
382 $ \ls0 -> monadicIO $ action ls0
384 where
386 action ls = do
387 v1 <- run $ Stream.fold (Fold.tee Fold.sum Fold.length) $ Stream.fromList ls
388 let v2 = Prelude.sum ls
389 v3 = Prelude.length ls
390 assert (v1 == (v2, v3))
392teeWithFstLength :: Property
393teeWithFstLength =
394 forAll (listOf1 (chooseInt (intMin, intMax)))
395 $ \ls0 -> monadicIO $ action ls0
397 where
399 action ls = do
400 v1 <-
401 run
402 $ Stream.fold (Fold.teeWithFst (,) (Fold.take 5 Fold.sum) Fold.length)
403 $ Stream.fromList ls
404 let v2 = Prelude.sum (Prelude.take 5 ls)
405 v3 = Prelude.length (Prelude.take 5 ls)
406 assert (v1 == (v2, v3))
408partitionByM :: Property
409partitionByM =
410 forAll (listOf1 (chooseInt (intMin, intMax)))
411 $ \ls0 -> monadicIO $ action ls0
413 where
415 action ls = do
416 let f = \x -> if odd x then return (Left x) else return (Right x)
417 v1 <-
418 run
419 $ Stream.fold (Fold.partitionByM f Fold.length Fold.length)
420 $ Stream.fromList ls
421 let v2 = foldl (\b a -> if odd a then b+1 else b) 0 ls
422 v3 = foldl (\b a -> if even a then b+1 else b) 0 ls
423 assert (v1 == (v2, v3))
425partitionByFstM :: Property
426partitionByFstM =
427 forAll (listOf1 (chooseInt (intMin, intMax)))
428 $ \ls0 -> monadicIO $ action ls0
430 where
432 action _ = do
433 let f = \x -> if odd x then return (Left x) else return (Right x)
434 v1 <-
435 run
436 $ Stream.fold
437 (Fold.partitionByFstM f (Fold.take 25 Fold.length) Fold.length)
438 (Stream.fromList ([1 .. 100] :: [Int]))
439 let v2 = foldl (\b a -> if odd a then b+1 else b) 0 ([1..49] :: [Int])
440 v3 = foldl (\b a -> if even a then b+1 else b) 0 ([1..49] :: [Int])
441 assert (v1 == (v2, v3))
443partitionByMinM1 :: Property
444partitionByMinM1 =
445 forAll (listOf1 (chooseInt (intMin, intMax)))
446 $ \ls0 -> monadicIO $ action ls0
448 where
450 action _ = do
451 let f = \x -> if odd x then return (Left x) else return (Right x)
452 v1 <-
453 run
454 $ Stream.fold
455 (Fold.partitionByMinM f Fold.length (Fold.take 25 Fold.length))
456 (Stream.fromList ([1 .. 100] :: [Int]))
457 let v2 = foldl (\b a -> if odd a then b+1 else b) 0 ([1..50] :: [Int])
458 v3 = foldl (\b a -> if even a then b+1 else b) 0 ([1..50] :: [Int])
459 assert (v1 == (v2, v3))
461partitionByMinM2 :: Property
462partitionByMinM2 =
463 forAll (listOf1 (chooseInt (intMin, intMax)))
464 $ \ls0 -> monadicIO $ action ls0
466 where
468 action _ = do
469 let f = \x -> if odd x then return (Left x) else return (Right x)
470 v1 <-
471 run
472 $ Stream.fold
473 (Fold.partitionByMinM f (Fold.take 25 Fold.length) Fold.length)
474 (Stream.fromList ([1 .. 100] :: [Int]))
475 let v2 = foldl (\b a -> if odd a then b+1 else b) 0 ([1..49] :: [Int])
476 v3 = foldl (\b a -> if even a then b+1 else b) 0 ([1..49] :: [Int])
477 assert (v1 == (v2, v3))
479teeWithMinLength1 :: Property
480teeWithMinLength1 =
481 forAll (listOf1 (chooseInt (intMin, intMax)))
482 $ \ls0 -> monadicIO $ action ls0
484 where
486 action ls = do
487 v1 <-
488 run
489 $ Stream.fold (Fold.teeWithMin (,) (Fold.take 5 Fold.sum) Fold.length)
490 $ Stream.fromList ls
491 let v2 = Prelude.sum (Prelude.take 5 ls)
492 v3 = Prelude.length (Prelude.take 5 ls)
493 assert (v1 == (v2, v3))
496teeWithMinLength2 :: Property
497teeWithMinLength2 =
498 forAll (listOf1 (chooseInt (intMin, intMax)))
499 $ \ls0 -> monadicIO $ action ls0
501 where
503 action ls = do
504 v1 <-
505 run
506 $ Stream.fold (Fold.teeWithMin (,) Fold.sum (Fold.take 5 Fold.length))
507 $ Stream.fromList ls
508 let v2 = Prelude.sum (Prelude.take 5 ls)
509 v3 = Prelude.length (Prelude.take 5 ls)
510 assert (v1 == (v2, v3))
512teeWithMax :: Property
513teeWithMax =
514 forAll (listOf1 (chooseInt (intMin, intMax)))
515 $ \ls0 -> monadicIO $ action ls0
517 where
519 action ls = do
520 v1 <- run $ Stream.fold (Fold.tee Fold.sum Fold.maximum) $ Stream.fromList ls
521 let v2 = Prelude.sum ls
522 v3 = foldMaybe (greater compare) intMin ls
523 assert (v1 == (v2, v3))
525distribute :: Property
526distribute =
527 forAll (listOf1 (chooseInt (intMin, intMax)))
528 $ \ls0 -> monadicIO $ action ls0
530 where
532 action ls = do
533 v1 <-
534 run $ Stream.fold (Fold.distribute [Fold.sum, Fold.length]) $ Stream.fromList ls
535 let v2 = Prelude.sum ls
536 v3 = Prelude.length ls
537 assert (v1 == [v2, v3])
539partition :: Property
540partition =
541 monadicIO $ do
542 v1 :: (Int, [String]) <-
543 run
544 $ Stream.fold (Fold.partition Fold.sum Fold.toList)
545 $ Stream.fromList
546 [Left 1, Right "abc", Left 3, Right "xy", Right "pp2"]
547 let v2 = (4,["abc","xy","pp2"])
548 assert (v1 == v2)
550unzip :: Property
551unzip =
552 monadicIO $ do
553 v1 :: (Int, [String]) <-
554 run
555 $ Stream.fold (Fold.unzip Fold.sum Fold.toList)
556 $ Stream.fromList [(1, "aa"), (2, "bb"), (3, "cc")]
557 let v2 = (6, ["aa", "bb", "cc"])
558 assert (v1 == v2)
560postscan :: Property
561postscan = forAll (listOf (chooseInt (intMin, intMax))) $ \ls ->
562 monadicIO $ do
563 v1 :: [Int] <-
564 run
565 $ Stream.fold (Fold.postscan Fold.sum Fold.toList)
566 $ Stream.fromList ls
567 let v2 = scanl1 (+) ls
568 assert (v1 == v2)
570many :: Property
571many =
572 forAll (listOf (chooseInt (0, 100))) $ \lst ->
573 forAll (chooseInt (1, 100)) $ \i ->
574 monadicIO $ do
575 let strm = Stream.fromList lst
576 r1 <- Stream.fold (Fold.many (split i) Fold.toList) strm
577 r2 <- Stream.fold Fold.toList $ Stream.foldMany (split i) strm
578 assert $ r1 == r2
580 where
582 split i = Fold.take i Fold.toList
584foldBreak :: [Int] -> Property
585foldBreak ls = monadicIO $ do
586 (mbh, rest) <- run $ Stream.foldBreak Fold.one (Stream.fromList ls)
587 rests <- run $ Stream.fold Fold.toList rest
588 assert (mbh == headl ls)
589 listEquals (==) rests (taill ls)
591 where
593 taill :: [a] -> [a]
594 taill [] = []
595 taill (_:xs) = xs
597demux :: Expectation
598demux =
599 let table "SUM" = return Fold.sum
600 table "PRODUCT" = return Fold.product
601 table _ = return Fold.length
602 input = Stream.fromList (
603 [ ("SUM", 1)
604 , ("abc", 1)
605 , ("PRODUCT", 2)
606 , ("abc", 2)
607 , ("SUM",3)
608 , ("xyz", 1)
609 , ("PRODUCT", 4)
610 , ("xyz", 2)
611 , ("abc", 2)
612 ] :: [(String, Int)])
613 in Stream.fold
614 (Fold.demuxKvToMap table)
615 input
616 `shouldReturn`
617 Data.Map.fromList [("PRODUCT", 8),("SUM", 4),("abc",3),("xyz",2)]
620demuxWith :: Expectation
621demuxWith =
622 let getKey x | even x = "SUM"
623 | otherwise = "PRODUCT"
625 getFold "SUM" = return Fold.sum
626 getFold "PRODUCT" = return Fold.product
627 getFold _ = error "demuxWith: bug"
629 input = Stream.fromList [1, 2, 3, 4 :: Int]
630 in Stream.fold
631 (Fold.demuxToContainer getKey (getFold . getKey))
632 input
633 `shouldReturn`
634 Data.Map.fromList [("PRODUCT",3),("SUM",6)]
636classifyWith :: Expectation
637classifyWith =
638 let input = Stream.fromList [("ONE",1),("ONE",1.1),("TWO",2), ("TWO",2.2)]
639 in Stream.fold
640 (Fold.toContainer fst (Fold.lmap snd Fold.toList))
641 input
642 `shouldReturn`
643 Data.Map.fromList
644 [("ONE",[1.0, 1.1 :: Double]), ("TWO",[2.0, 2.2])]
646classify :: Expectation
647classify =
648 let input =
649 Stream.fromList
650 [
651 ("ONE", (1::Int, 1))
652 , ("ONE", (1, 1.1:: Double))
653 , ("TWO", (2, 2))
654 , ("TWO",(2, 2.2))
655 ]
656 in Stream.fold
657 (Fold.kvToMap (Fold.lmap snd Fold.toList))
658 input
659 `shouldReturn`
660 Data.Map.fromList
661 [("ONE",[1.0, 1.1 :: Double]), ("TWO",[2.0, 2.2])]
663splitAt :: Expectation
664splitAt =
665 Stream.fold
666 (Fold.splitAt 6 Fold.toList Fold.toList)
667 (Stream.fromList "Hello World!")
668 `shouldReturn`
669 ("Hello ","World!")
671scan :: Property
672scan = forAll (listOf (chooseInt (0, 100))) $ \lst ->
673 monadicIO $ do
674 v1 :: [Int] <-
675 run
676 $ Stream.fold (Fold.scan Fold.sum Fold.toList)
677 $ Stream.fromList lst
678 let v2 = scanl (+) 0 lst
679 assert (v1 == v2)
681topBy :: Bool -> Property
682topBy isTop = forAll (listOf (chooseInt (-50, 100))) $ \ls0 ->
683 monadicIO $ action ls0
685 where
687 action ls = do
689 let n0 = Prelude.length ls
690 n <- liftIO $ generate $ chooseInt (-2, n0 + 2)
691 if isTop
692 then do
693 lst <- Stream.fold (Fold.top n) (Stream.fromList ls) >>= MArray.toList
694 assert ((Prelude.take n . Prelude.reverse . sort) ls == lst)
695 else do
696 lst <- Stream.fold (Fold.bottom n) (Stream.fromList ls) >>= MArray.toList
697 assert ((Prelude.take n . sort) ls == lst)
699top :: Property
700top = topBy True
702bottom :: Property
703bottom = topBy False
705nub :: Property
706nub = monadicIO $ do
707 vals <- Stream.fold Fold.toList
708 $ Stream.catMaybes
709 $ Stream.postscan Fold.nub
710 $ Stream.fromList [1::Int, 1, 2, 3, 4, 4, 5, 1, 5, 7]
711 assert (vals == [1, 2, 3, 4, 5, 7])
713moduleName :: String
714moduleName = "Data.Fold"
716main :: IO ()
717main = hspec $ do
718 describe moduleName $ do
719 -- Folds
720 -- Accumulators
721 prop "mconcat" Main.mconcat
722 prop "foldMap" Main.foldMap
723 prop "foldMapM" Main.foldMapM
725 prop "drain" Main.drain
726 prop "drainBy" Main.drainBy
727 prop "last" last
728 prop "length" Main.length
729 prop "sum" sum
730 prop "product" product
731 prop "maximumBy" $ maximumBy intMin compare
732 prop "maximum" $ maximum intMin
733 prop "minimumBy" $ minimumBy intMax compare
734 prop "minimum" $ minimum intMax
735 prop "mean" Main.mean
736 prop "stdDev" Main.stdDev
737 prop "variance" Main.variance
738 prop "rollingHashFirstN" rollingHashFirstN
740 prop "toList" toList
741 prop "toListRev" toListRev
742 prop "demux" demux
743 prop "demuxWith" demuxWith
744 prop "classifyWith" classifyWith
745 prop "classify" classify
747 -- Terminating folds
748 prop "index" index
749 prop "head" head
750 prop "find" $ find predicate
751 prop "lookup" Main.lookup
752 prop "findIndex" $ findIndex predicate
753 prop "elemIndex" $ elemIndex 10
754 prop "null" null
755 prop "elem" $ elem 10
756 prop "notElem" $ notElem 10
757 prop "all" $ Main.all predicate
758 prop "any" $ Main.any predicate
759 prop "and" Main.and
760 prop "or" Main.or
761 prop "top" Main.top
762 prop "bottom" Main.bottom
763 prop "nub" Main.nub
765 -- Combinators
767 -- Transformation
768 prop "scan" scan
769 prop "postscan" Main.postscan
770 -- rsequence
771 -- Functor instance
772 prop "rmapM" Main.rmapM
773 -- lmap/lmapM
775 -- Filtering
776 -- filter/filterM
777 -- catMaybes
778 prop "mapMaybe" mapMaybe
780 -- Trimming
781 prop "take" take
782 -- takeEndBy
783 prop "takeEndBy_" takeEndBy_
784 prop "takeEndByOrMax" takeEndByOrMax
786 -- Appending
787 -- splitWith
789 -- Distributing
790 -- tee
791 prop "teeWithLength" Main.teeWithLength
792 prop "teeWithFstLength" Main.teeWithFstLength
793 prop "teeWithMinLength1" Main.teeWithMinLength1
794 prop "teeWithMinLength2" Main.teeWithMinLength2
795 prop "teeWithMax" Main.teeWithMax
796 prop "partitionByM" Main.partitionByM
797 prop "partitionByFstM" Main.partitionByFstM
798 prop "partitionByMinM1" Main.partitionByMinM1
799 prop "partitionByMinM2" Main.partitionByMinM2
800 prop "distribute" Main.distribute
802 -- Partitioning
803 prop "partition" Main.partition
804 prop "partitionByM" partitionByM
806 -- Unzipping
807 prop "unzip" Main.unzip
808 prop "splitAt" Main.splitAt
810 -- Nesting
811 prop "many" Main.many
812 -- concatMap
813 -- chunksOf
815 prop "foldBreak" foldBreak