@@ -0,0 +1,280 @@
1+module Main (main) where
3+import qualified Streamly.Internal.Data.Parser.ParserD as D
4+import qualified Streamly.Internal.Prelude as S
5+import qualified Streamly.Internal.Data.Fold as FL
7+import Data.List (partition)
9+import Test.Hspec(hspec, describe)
10+import Test.Hspec.QuickCheck
11+import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, (.&&.))
17+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
18+ case (==) <$> (S.parseD (D.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
19+ Right is_equal -> is_equal
24+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
25+ case S.parseD (D.any (> 5000)) (S.fromList ls) of
26+ Right r -> r == (Prelude.any (> 5000) ls)
31+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
32+ case S.parseD (D.all (> 5000)) (S.fromList ls) of
33+ Right r -> r == (Prelude.all (> 5000) ls)
38+ forAll (chooseInt (0, 10000)) $ \x ->
39+ case S.parseD (D.yield x) (S.fromList [1 :: Int]) of
45+ forAll (chooseInt (0, 10000)) $ \x ->
46+ case S.parseD (D.yieldM $ return x) (S.fromList [1 :: Int]) of
53+ case S.parseD (D.die "die test") (S.fromList [0 :: Int]) of
60+ case S.parseD (D.dieM (Right "die test")) (S.fromList [0 :: Int]) of
64+-- Element Parser Tests
68+ forAll (chooseInt (1, 100)) $ \list_length ->
69+ forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
70+ case S.parseD D.peek (S.fromList ls) of
71+ Right head_value -> case ls of
72+ head_ls : _ -> head_value == head_ls
76+ property (case S.parseD D.peek (S.fromList []) of
82+ forAll (chooseInt (1, 100)) $ \list_length ->
83+ forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
84+ case S.parseD D.eof (S.fromList ls) of
88+ property (case S.parseD D.eof (S.fromList []) of
94+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
95+ case S.parseD (D.satisfy predicate) (S.fromList ls) of
96+ Right r -> case ls of
98+ (x : _) -> predicate x && (r == x)
101+ (x : _) -> not $ predicate x
103+ predicate = (>= 5000)
105+-- Sequence Parsers Tests
109+ forAll (chooseInt (0, 10000)) $ \n ->
110+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
111+ case S.parseD (D.take n FL.toList) (S.fromList ls) of
112+ Right parsed_list -> parsed_list == Prelude.take n ls
117+ forAll (chooseInt (0, 10000)) $ \n ->
118+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
120+ list_length = Prelude.length ls
122+ case S.parseD (D.takeEQ n FL.toList) (S.fromList ls) of
123+ Right parsed_list -> (n <= list_length) && (parsed_list == Prelude.take n ls)
124+ Left _ -> n > list_length
128+ forAll (chooseInt (0, 10000)) $ \n ->
129+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
131+ list_length = Prelude.length ls
133+ case S.parseD (D.takeGE n FL.toList) (S.fromList ls) of
134+ Right parsed_list -> (n <= list_length) && (parsed_list == ls)
135+ Left _ -> n > list_length
137+lookAhead :: Property
139+ forAll (chooseInt (0, 10000)) $ \n ->
141+ takeWithoutConsume = D.lookAhead $ D.take n FL.toList
143+ parsed_list_1 <- takeWithoutConsume
144+ parsed_list_2 <- takeWithoutConsume
145+ return (parsed_list_1, parsed_list_2)
147+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
148+ case S.parseD parseTwice (S.fromList ls) of
149+ Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
150+ Left _ -> (list_length < n) || (list_length == n && n == 0)
152+ list_length = Prelude.length ls
154+takeWhile :: Property
156+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
157+ case S.parseD (D.takeWhile predicate FL.toList) (S.fromList ls) of
158+ Right parsed_list -> parsed_list == Prelude.takeWhile predicate ls
163+takeWhile1 :: Property
165+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
166+ case S.parseD (D.takeWhile1 predicate FL.toList) (S.fromList ls) of
167+ Right parsed_list -> case ls of
169+ (x : _) -> predicate x && (parsed_list == Prelude.takeWhile predicate ls)
170+ Left _ -> case ls of
172+ (x : _) -> not $ predicate x
176+sliceSepBy :: Property
178+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
179+ case S.parseD (D.sliceSepBy predicate FL.toList) (S.fromList ls) of
180+ Right parsed_list -> parsed_list == Prelude.takeWhile (not . predicate) ls
185+-- sliceSepByMax :: Property
187+-- forAll (chooseInt (0, 10000)) $ \n ->
188+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
189+-- case S.parseD (D.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
190+-- Right parsed_list -> parsed_list == Prelude.take n (Prelude.takeWhile (not . predicate) ls)
193+-- predicate = (== 1)
195+splitWith :: Property
197+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
198+ case S.parseD (D.splitWith (,) (D.satisfy (== 0)) (D.satisfy (== 1))) (S.fromList ls) of
199+ Right (result_first, result_second) -> case ls of
200+ 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
202+ Left _ -> case ls of
206+ property (case S.parseD (D.splitWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
210+ property (case S.parseD (D.splitWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
214+ property (case S.parseD (D.splitWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
220+ forAll (chooseInt (0, 10000)) $ \n ->
221+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
223+ prsr = D.take n FL.toList
225+ case S.parseD (D.teeWith (,) prsr prsr) (S.fromList ls) of
226+ Right (ls_1, ls_2) -> (Prelude.take n ls == ls_1) && (ls_1 == ls_2)
229+ property (case S.parseD (D.teeWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
233+ property (case S.parseD (D.teeWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
237+ property (case S.parseD (D.teeWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
241+-- deintercalate :: Property
243+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
244+-- case S.parseD (D.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
245+-- Right parsed_list_tuple -> parsed_list_tuple == partition (== 0) ls
249+-- prsr_1 = (D.takeWhile (== 0) FL.toList)
250+-- prsr_2 = (D.takeWhile (== 1) FL.toList)
251+-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
255+ describe "test for accumulator" $ do
256+ prop "test fromFold function" fromFold
257+ prop "test any function" Main.any
258+ prop "test all function" Main.all
259+ prop "test yield function" yield
260+ prop "test yieldM function" yieldM
261+ prop "test die function" die
262+ prop "test dieM function" dieM
264+ describe "test for element parser" $ do
265+ prop "test for peek function" peek
266+ prop "test for eof function" eof
267+ prop "test for satisfy function" satisfy
269+ describe "test for sequence parser" $ do
270+ prop "test for take function" Main.take
271+ prop "test for takeEq function" Main.takeEQ
272+ prop "test for takeGE function" Main.takeGE
273+ prop "test for LookAhead function" lookAhead
274+ prop "test for takeWhile function" Main.takeWhile
275+ prop "test for takeWhile1 function" takeWhile1
276+ prop "test for sliceSepBy function" sliceSepBy
277+ -- prop "test for sliceSepByMax function" sliceSepByMax
278+ prop "test for splitWith function" splitWith
279+ prop "test for teeWith function" teeWith
280+ -- prop "test for deintercalate function" deintercalate