@@ -1,6 +1,7 @@
1module Main (main) where
1module Main (main) where
3+import Prelude hiding (maximum, minimum, elem, notElem, null, product, sum, head, last)
3−import Streamly.Internal.Data.Fold
4+import qualified Streamly.Internal.Data.Fold as F
4import qualified Streamly.Prelude as S
5import qualified Streamly.Prelude as S
5import qualified Streamly.Data.Fold as FL
6import qualified Streamly.Data.Fold as FL
@@ -22,33 +23,33 @@ intMax = maxBound
22{-# INLINE intMin #-}
23{-# INLINE intMin #-}
23{-# INLINE intMax #-}
24{-# INLINE intMax #-}
25−testRollingHashFirstN :: Property
26+rollingHashFirstN :: Property
26−testRollingHashFirstN =
27+rollingHashFirstN =
27 forAll (choose (0, maxStreamLen)) $ \len ->
28 forAll (choose (0, maxStreamLen)) $ \len ->
28 forAll (choose (0, len)) $ \n ->
29 forAll (choose (0, len)) $ \n ->
29 forAll (vectorOf len (arbitrary :: Gen Int)) $ \vec -> monadicIO $ do
30 forAll (vectorOf len (arbitrary :: Gen Int)) $ \vec -> monadicIO $ do
30− a <- run $ S.fold rollingHash $ S.take n $ S.fromList vec
31+ a <- run $ S.fold F.rollingHash $ S.take n $ S.fromList vec
31− b <- run $ S.fold (rollingHashFirstN n) $ S.fromList vec
32+ b <- run $ S.fold (F.rollingHashFirstN n) $ S.fromList vec
32 assert $ a == b
33 assert $ a == b
35−testHead :: [Int] -> Expectation
36+head :: [Int] -> Expectation
36−testHead ls = S.fold FL.head (S.fromList ls) `shouldReturn` headl ls
37+head ls = S.fold FL.head (S.fromList ls) `shouldReturn` headl ls
38 headl [] = Nothing
39 headl [] = Nothing
39 headl (x:_) = Just x
40 headl (x:_) = Just x
42−testLength :: [Int] -> Expectation
43+length :: [Int] -> Expectation
43−testLength ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
44+length ls = S.fold FL.length (S.fromList ls) `shouldReturn` Prelude.length ls
46−testSum :: [Int] -> Expectation
47+sum :: [Int] -> Expectation
47−testSum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` foldl (+) 0 ls
48+sum ls = S.fold FL.sum (S.fromList ls) `shouldReturn` foldl (+) 0 ls
50−testProduct :: [Int] -> Expectation
51+product :: [Int] -> Expectation
51−testProduct ls = S.fold FL.product (S.fromList ls) `shouldReturn` foldl (*) 1 ls
52+product ls = S.fold FL.product (S.fromList ls) `shouldReturn` foldl (*) 1 ls
54lesser :: (a -> a -> Ordering) -> a -> a -> a
55lesser :: (a -> a -> Ordering) -> a -> a -> a
@@ -60,29 +61,30 @@ greater f x y = if f x y == GT then x else y
62foldMaybe :: (b -> a -> b) -> b -> [a] -> Maybe b
63foldMaybe :: (b -> a -> b) -> b -> [a] -> Maybe b
63−foldMaybe f acc ls = case ls of
64+foldMaybe f acc ls =
64− [] -> Nothing
66+ [] -> Nothing
65− _ -> Just (foldl f acc ls)
67+ _ -> Just (foldl f acc ls)
68−testMaximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
70+maximumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
69−testMaximumBy genmin f ls = S.fold (FL.maximumBy f) (S.fromList ls) `shouldReturn` foldMaybe (greater f) genmin ls
71+maximumBy genmin f ls = S.fold (FL.maximumBy f) (S.fromList ls) `shouldReturn` foldMaybe (greater f) genmin ls
72−testMaximum :: (Show a, Ord a) => a -> [a] -> Expectation
74+maximum :: (Show a, Ord a) => a -> [a] -> Expectation
73−testMaximum genmin ls = S.fold FL.maximum (S.fromList ls) `shouldReturn` foldMaybe (greater compare) genmin ls
75+maximum genmin ls = S.fold FL.maximum (S.fromList ls) `shouldReturn` foldMaybe (greater compare) genmin ls
76−testMinimumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
78+minimumBy :: (Ord a, Show a) => a -> (a -> a -> Ordering) -> [a] -> Expectation
77−testMinimumBy genmax f ls = S.fold (FL.minimumBy f) (S.fromList ls) `shouldReturn` foldMaybe (lesser f) genmax ls
79+minimumBy genmax f ls = S.fold (FL.minimumBy f) (S.fromList ls) `shouldReturn` foldMaybe (lesser f) genmax ls
80−testMinimum :: (Show a, Ord a) => a -> [a] -> Expectation
82+minimum :: (Show a, Ord a) => a -> [a] -> Expectation
81−testMinimum genmax ls = S.fold FL.minimum (S.fromList ls) `shouldReturn` foldMaybe (lesser compare) genmax ls
83+minimum genmax ls = S.fold FL.minimum (S.fromList ls) `shouldReturn` foldMaybe (lesser compare) genmax ls
84−testToList :: [Int] -> Expectation
86+toList :: [Int] -> Expectation
85−testToList ls = S.fold FL.toList (S.fromList ls) `shouldReturn` ls
87+toList ls = S.fold FL.toList (S.fromList ls) `shouldReturn` ls
88safeLast :: [a] -> Maybe a
90safeLast :: [a] -> Maybe a
@@ -91,8 +93,8 @@ safeLast (x:[]) = Just x
91safeLast (_:xs) = safeLast xs
93safeLast (_:xs) = safeLast xs
94−testLast :: [String] -> Expectation
96+last :: [String] -> Expectation
95−testLast ls = S.fold FL.last (S.fromList ls) `shouldReturn` safeLast ls
97+last ls = S.fold FL.last (S.fromList ls) `shouldReturn` safeLast ls
98nth :: Int -> [a] -> Maybe a
100nth :: Int -> [a] -> Maybe a
@@ -104,102 +106,110 @@ nth idx (x:xs) = if idx == 0
104nth _ [] = Nothing
106nth _ [] = Nothing
107−testIndex :: Int -> [String] -> Expectation
109+index :: Int -> [String] -> Expectation
108−testIndex idx ls = let
109− x = S.fold (FL.index idx) (S.fromList ls)
110+index idx ls = let x = S.fold (FL.index idx) (S.fromList ls)
111− x `shouldReturn` (nth idx ls)
111+ in x `shouldReturn` (nth idx ls)
114−testFind :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
114+find :: (Show a, Eq a) => (a -> Bool) -> [a] -> Expectation
115−testFind f ls = do
115+find f ls = do
116− let x = S.fold (FL.findIndex f) (S.fromList ls)
116+ let x = S.fold (FL.findIndex f) (S.fromList ls)
118− case y of
118+ case y of
119− Nothing -> S.fold (FL.find f) (S.fromList ls) `shouldReturn` Nothing
119+ Nothing -> S.fold (FL.find f) (S.fromList ls) `shouldReturn` Nothing
120− Just idx -> S.fold (FL.any f) (S.fromList $ take idx ls) `shouldReturn` False
120+ Just idx -> S.fold (FL.any f) (S.fromList $ take idx ls) `shouldReturn` False
123neg :: (a -> Bool) -> a -> Bool
123neg :: (a -> Bool) -> a -> Bool
124neg f x = if f x == True then False else True
124neg f x = if f x == True then False else True
126−testFindIndex :: (a -> Bool) -> [a] -> Expectation
126+findIndex :: (a -> Bool) -> [a] -> Expectation
127−testFindIndex f ls = do
127+findIndex f ls = do
128− let x = S.fold (FL.findIndex f) (S.fromList ls)
128+ let x = S.fold (FL.findIndex f) (S.fromList ls)
130− case y of
130+ case y of
131− Nothing -> S.fold (FL.all $ neg f) (S.fromList ls) `shouldReturn` True
131+ Nothing -> S.fold (FL.all $ neg f) (S.fromList ls) `shouldReturn` True
132− Just idx -> if idx == 0
132+ Just idx -> if idx == 0
133− then S.fold (FL.all f) (S.fromList []) `shouldReturn` True
133+ then S.fold (FL.all f) (S.fromList []) `shouldReturn` True
134− else S.fold (FL.all f) (S.fromList $ (take idx ls)) `shouldReturn` False
134+ else S.fold (FL.all f) (S.fromList $ (take idx ls)) `shouldReturn` False
136predicate :: Int -> Bool
136predicate :: Int -> Bool
137predicate x = if x * x < 100 then True else False
137predicate x = if x * x < 100 then True else False
139−testElemIndex :: Int -> [Int] -> Expectation
139+elemIndex :: Int -> [Int] -> Expectation
140−testElemIndex elm ls = do
140+elemIndex elm ls = do
141− let x = S.fold (FL.elemIndex elm) (S.fromList ls)
141+ let x = S.fold (FL.elemIndex elm) (S.fromList ls)
143− case y of
143+ case y of
144+ Nothing -> S.fold (FL.any (\z -> if z == elm
144− Nothing -> S.fold (FL.any (\z -> if z == elm then True else False)) (S.fromList ls) `shouldReturn` False
146+ else False)) (S.fromList ls) `shouldReturn` False
147+ Just idx -> S.fold (FL.any (\z -> if z == elm
145− Just idx -> S.fold (FL.any (\z -> if z == elm then True else False)) (S.fromList $ (take idx ls)) `shouldReturn` False
149+ else False)) (S.fromList $ (take idx ls)) `shouldReturn` False
147−testNull :: [Int] -> Expectation
151+null :: [Int] -> Expectation
148−testNull ls = S.fold FL.null (S.fromList ls) `shouldReturn` case ls of
152+null ls = S.fold FL.null (S.fromList ls) `shouldReturn` case ls of
149− [] -> True
153+ [] -> True
150− _ -> False
154+ _ -> False
151−testElem :: Int -> [Int] -> Expectation
155+elem :: Int -> [Int] -> Expectation
152−testElem elm ls = do
156+elem elm ls = do
153− let x = S.fold (FL.elem elm) (S.fromList ls)
157+ let x = S.fold (FL.elem elm) (S.fromList ls)
159+ S.fold (FL.any (\z -> if z == elm
155− S.fold (FL.any (\z -> if z == elm then True else False)) (S.fromList ls) `shouldReturn` y
161+ else False)) (S.fromList ls) `shouldReturn` y
158−testNotElem :: Int -> [Int] -> Expectation
164+notElem :: Int -> [Int] -> Expectation
159−testNotElem elm ls = do
165+notElem elm ls = do
160− let x = S.fold (FL.notElem elm) (S.fromList ls)
166+ let x = S.fold (FL.notElem elm) (S.fromList ls)
168+ S.fold (FL.any (\z -> if z == elm
162− S.fold (FL.any (\z -> if z == elm then True else False)) (S.fromList ls) `shouldReturn` (if y == True then False else True)
170+ else False)) (S.fromList ls) `shouldReturn` (if y == True
165−testAll :: (a -> Bool) -> [a] -> Expectation
175+all :: (a -> Bool) -> [a] -> Expectation
166−testAll f ls = S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.and (map f ls)
176+all f ls = S.fold (FL.all f) (S.fromList ls) `shouldReturn` Prelude.and (map f ls)
169−testAny :: (a -> Bool) -> [a] -> Expectation
179+any :: (a -> Bool) -> [a] -> Expectation
170−testAny f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
180+any f ls = S.fold (FL.any f) (S.fromList ls) `shouldReturn` Prelude.any f ls
173−testAnd :: [Bool] -> Expectation
183+and :: [Bool] -> Expectation
174−testAnd ls = S.fold FL.and (S.fromList ls) `shouldReturn` Prelude.and ls
184+and ls = S.fold FL.and (S.fromList ls) `shouldReturn` Prelude.and ls
177−testOr :: [Bool] -> Expectation
187+or :: [Bool] -> Expectation
178−testOr ls = S.fold FL.or (S.fromList ls) `shouldReturn` Prelude.or ls
188+or ls = S.fold FL.or (S.fromList ls) `shouldReturn` Prelude.or ls
181main :: IO ()
191main :: IO ()
182main = hspec $
192main = hspec $
183− describe "Fold Tests" $ do
193+ describe "Fold s" $ do
184− prop "testRollingHashFirstN" testRollingHashFirstN
194+ prop "RollingHashFirstN" rollingHashFirstN
185− prop "testIndex" $ testIndex
195+ prop "Index" $ index
186− prop "testHead" testHead
196+ prop "Head" head
187− prop "testLast" testLast
197+ prop "Last" last
188− prop "testLength" testLength
198+ prop "Length" Main.length
189− prop "testSum" testSum
199+ prop "Sum" sum
190− prop "testProduct" testProduct
200+ prop "Product" product
191− prop "testMaximumBy" $ testMaximumBy intMin compare
201+ prop "MaximumBy" $ maximumBy intMin compare
192− prop "testMaximum" $ testMaximum intMin
202+ prop "Maximum" $ maximum intMin
193− prop "testMinimumBy" $ testMinimumBy intMax compare
203+ prop "MinimumBy" $ minimumBy intMax compare
194− prop "testMinimum" $ testMinimum intMax
204+ prop "Minimum" $ minimum intMax
195− prop "testToList" testToList
205+ prop "ToList" toList
196− prop "testFind" $ testFind predicate
206+ prop "Find" $ find predicate
197− prop "testFindIndex" $ testFindIndex predicate
207+ prop "FindIndex" $ findIndex predicate
198− prop "testElemIndex" $ testElemIndex 10
208+ prop "ElemIndex" $ elemIndex 10
199− prop "testNull" testNull
209+ prop "Null" null
200− prop "testElem" $ testElem 10
210+ prop "Elem" $ elem 10
201− prop "testNotElem" $ testNotElem 10
211+ prop "NotElem" $ notElem 10
202− prop "testAll" $ testAll predicate
212+ prop "All" $ Main.all predicate
203− prop "testAny" $ testAny predicate
213+ prop "Any" $ Main.any predicate
204− prop "testAnd" testAnd
214+ prop "And" Main.and
205− prop "testOr" testOr
215+ prop "Or" Main.or