on master help sign in
Merge Data.Parser.ParserD test cases to Data.Parser (#2458)
9bf458fRanjeet Ranjan
.hlint.ignore+2−2
@@ -4,7 +4,7 @@ core/src/Streamly/Internal/Unicode/Stream.hs
4src/Streamly/Internal/Data/SmallArray/Type.hs
4src/Streamly/Internal/Data/SmallArray/Type.hs
5test/Streamly/Test/Data/Array.hs
5test/Streamly/Test/Data/Array.hs
6test/Streamly/Test/Data/Parser.hs
6test/Streamly/Test/Data/Parser.hs
7−test/Streamly/Test/Data/Parser/ParserD.hs
7+test/Streamly/Test/Data/ParserK.hs
8test/Streamly/Test/Data/SmallArray.hs
8test/Streamly/Test/Data/SmallArray.hs
9test/Streamly/Test/Data/Unfold.hs
9test/Streamly/Test/Data/Unfold.hs
10test/Streamly/Test/FileSystem/Event.hs
10test/Streamly/Test/FileSystem/Event.hs
@@ -21,7 +21,7 @@ benchmark/lib/Streamly/Benchmark/Prelude.hs
21benchmark/NanoBenchmarks.hs
21benchmark/NanoBenchmarks.hs
22benchmark/Streamly/Benchmark/Data/Array.hs
22benchmark/Streamly/Benchmark/Data/Array.hs
23benchmark/Streamly/Benchmark/Data/Parser.hs
23benchmark/Streamly/Benchmark/Data/Parser.hs
24−benchmark/Streamly/Benchmark/Data/Parser/ParserD.hs
24+benchmark/Streamly/Benchmark/Data/ParserK.hs
25benchmark/Streamly/Benchmark/Data/Stream/StreamDK.hs
25benchmark/Streamly/Benchmark/Data/Stream/StreamDK.hs
26benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs
26benchmark/Streamly/Benchmark/Data/Stream/StreamK.hs
27benchmark/Streamly/Benchmark/Data/Unfold.hs
27benchmark/Streamly/Benchmark/Data/Unfold.hs
streamly.cabal+2−1
@@ -107,7 +107,8 @@ extra-source-files:
107 test/Streamly/Test/Data/MutArray.hs
107 test/Streamly/Test/Data/MutArray.hs
108 test/Streamly/Test/Data/Ring/Unboxed.hs
108 test/Streamly/Test/Data/Ring/Unboxed.hs
109 test/Streamly/Test/Data/Array/Stream.hs
109 test/Streamly/Test/Data/Array/Stream.hs
110− test/Streamly/Test/Data/Parser/ParserD.hs
110+ test/Streamly/Test/Data/Parser.hs
111+ test/Streamly/Test/Data/ParserK.hs
111 test/Streamly/Test/Data/Stream/Concurrent.hs
112 test/Streamly/Test/Data/Stream/Concurrent.hs
112 test/Streamly/Test/FileSystem/Event.hs
113 test/Streamly/Test/FileSystem/Event.hs
113 test/Streamly/Test/FileSystem/Event/Common.hs
114 test/Streamly/Test/FileSystem/Event/Common.hs
test/Streamly/Test/Data/Parser.hs+249−105
@@ -1,5 +1,6 @@
1module Main (main) where
1module Main (main) where
2
2
3+import Control.Applicative ((<|>))
3import Control.Exception (displayException)
4import Control.Exception (displayException)
4import Data.Foldable (for_)
5import Data.Foldable (for_)
5import Data.Word (Word8, Word32, Word64)
6import Data.Word (Word8, Word32, Word64)
@@ -8,7 +9,7 @@ import Test.Hspec (Spec, hspec, describe)
8import Test.Hspec.QuickCheck
9import Test.Hspec.QuickCheck
9import Test.QuickCheck
10import Test.QuickCheck
10 (arbitrary, forAll, elements, Property, property, listOf,
11 (arbitrary, forAll, elements, Property, property, listOf,
11− vectorOf, Gen)
12+ vectorOf, Gen, (.&&.))
12import Test.QuickCheck.Monadic (monadicIO, assert, run)
13import Test.QuickCheck.Monadic (monadicIO, assert, run)
13
14
14import Prelude hiding (sequence)
15import Prelude hiding (sequence)
@@ -16,10 +17,13 @@ import Prelude hiding (sequence)
16import qualified Control.Monad.Fail as Fail
17import qualified Control.Monad.Fail as Fail
17import qualified Data.List as List
18import qualified Data.List as List
18import qualified Prelude
19import qualified Prelude
20+import qualified Streamly.Data.Stream as S
19import qualified Streamly.Internal.Data.Array as A
21import qualified Streamly.Internal.Data.Array as A
20import qualified Streamly.Internal.Data.Fold as FL
22import qualified Streamly.Internal.Data.Fold as FL
21import qualified Streamly.Internal.Data.Parser as P
23import qualified Streamly.Internal.Data.Parser as P
22−import qualified Streamly.Internal.Data.Stream as S
24+import qualified Streamly.Internal.Data.Producer.Source as Source
25+import qualified Streamly.Internal.Data.Producer as Producer
26+import qualified Streamly.Internal.Data.Unfold as Unfold
23import qualified Test.Hspec as H
27import qualified Test.Hspec as H
24
28
25#if MIN_VERSION_QuickCheck(2,14,0)
29#if MIN_VERSION_QuickCheck(2,14,0)
@@ -213,6 +217,13 @@ takeBetween =
213 Left _ ->
217 Left _ ->
214 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
218 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
215
219
220+take :: Property
221+take =
222+ forAll (chooseInt (min_value, max_value)) $ \n ->
223+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
224+ case runIdentity $ S.parse (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
225+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
226+ Left _ -> property False
216
227
217takeEQPass :: Property
228takeEQPass :: Property
218takeEQPass =
229takeEQPass =
@@ -263,7 +274,6 @@ takeGE =
263 else property False
274 else property False
264 Left _ -> property (n > list_length)
275 Left _ -> property (n > list_length)
265
276
266−
267nLessThanEqual0 ::
277nLessThanEqual0 ::
268 ( Int
278 ( Int
269 -> FL.Fold Identity Int [Int]
279 -> FL.Fold Identity Int [Int]
@@ -286,21 +296,23 @@ takeProperties =
286 prop "takeGE n FL.toList xs = xs" $
296 prop "takeGE n FL.toList xs = xs" $
287 nLessThanEqual0 P.takeGE (\_ -> id)
297 nLessThanEqual0 P.takeGE (\_ -> id)
288
298
299+-- XXX lookAhead can't deal with EOF which in this case means when
300+-- n==list_length, this test will fail. So excluding that case for now.
289−-- lookAheadPass :: Property
301+lookAheadPass :: Property
290−-- lookAheadPass =
302+lookAheadPass =
291−-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
303+ forAll (chooseInt (min_value, max_value)) $ \n ->
292−-- let
304+ let
293−-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
305+ takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
294−-- parseTwice = do
306+ parseTwice = do
295−-- parsed_list_1 <- takeWithoutConsume
307+ parsed_list_1 <- takeWithoutConsume
296−-- parsed_list_2 <- takeWithoutConsume
308+ parsed_list_2 <- takeWithoutConsume
297−-- return (parsed_list_1, parsed_list_2)
309+ return (parsed_list_1, parsed_list_2)
298−-- in
310+ in
299−-- forAll (chooseInt (n, max_value)) $ \list_length ->
311+ forAll (chooseInt (n+1, max_value)) $ \list_length ->
300−-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
312+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
301−-- case S.parse parseTwice (S.fromList ls) of
313+ case runIdentity $ S.parse parseTwice (S.fromList ls) of
302−-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
314+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
303−-- Left _ -> property $ False
315+ Left _ -> property $ False
304
316
305-- lookAheadFail :: Property
317-- lookAheadFail :: Property
306-- lookAheadFail =
318-- lookAheadFail =
@@ -318,22 +330,22 @@ takeProperties =
318-- Right _ -> False
330-- Right _ -> False
319-- Left _ -> True
331-- Left _ -> True
320
332
321−-- lookAhead :: Property
333+lookAhead :: Property
322−-- lookAhead =
334+lookAhead =
323−-- forAll (chooseInt (min_value, max_value)) $ \n ->
335+ forAll (chooseInt (min_value, max_value)) $ \n ->
324−-- let
336+ let
325−-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
337+ takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
326−-- parseTwice = do
338+ parseTwice = do
327−-- parsed_list_1 <- takeWithoutConsume
339+ parsed_list_1 <- takeWithoutConsume
328−-- parsed_list_2 <- takeWithoutConsume
340+ parsed_list_2 <- takeWithoutConsume
329−-- return (parsed_list_1, parsed_list_2)
341+ return (parsed_list_1, parsed_list_2)
330−-- in
342+ in
331−-- forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
343+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
332−-- case S.parse parseTwice (S.fromList ls) of
344+ case runIdentity $ S.parse parseTwice (S.fromList ls) of
333−-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
345+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
334−-- Left _ -> property ((list_length < n) || (list_length == n && n == 0))
346+ Left _ -> property ((list_length < n) || (list_length == n && n == 0))
335−-- where
347+ where
336−-- list_length = Prelude.length ls
348+ list_length = Prelude.length ls
337
349
338takeEndBy_ :: Property
350takeEndBy_ :: Property
339takeEndBy_ =
351takeEndBy_ =
@@ -347,6 +359,16 @@ takeEndBy_ =
347 prsr = P.many (P.satisfy (const True)) FL.toList
359 prsr = P.many (P.satisfy (const True)) FL.toList
348 tkwhl ls = Prelude.takeWhile (not . predicate) ls
360 tkwhl ls = Prelude.takeWhile (not . predicate) ls
349
361
362+takeEndByOrMax_ :: Property
363+takeEndByOrMax_ =
364+ forAll (chooseInt (min_value, max_value)) $ \n ->
365+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
366+ case runIdentity $ S.parse (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
367+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
368+ Left _ -> property False
369+ where
370+ predicate = (== 1)
371+
350takeStartBy :: Property
372takeStartBy :: Property
351takeStartBy =
373takeStartBy =
352 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
374 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
@@ -464,6 +486,25 @@ groupBy =
464 | null lst = []
486 | null lst = []
465 | otherwise = head $ List.groupBy cmp lst
487 | otherwise = head $ List.groupBy cmp lst
466
488
489+groupByRolling :: Property
490+groupByRolling =
491+ forAll (listOf (chooseInt (0, 1)))
492+ $ \ls ->
493+ case runIdentity $ S.parse parser (S.fromList ls) of
494+ Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
495+ Left _ -> property False
496+
497+ where
498+
499+ cmp = (==)
500+ parser = P.groupBy cmp FL.toList
501+ groupByLF _ [] = []
502+ groupByLF Nothing (x:xs) = x : groupByLF (Just x) xs
503+ groupByLF (Just y) (x:xs) =
504+ if cmp y x
505+ then x : groupByLF (Just x) xs
506+ else []
507+
467wordBy :: Property
508wordBy :: Property
468wordBy =
509wordBy =
469 forAll (listOf (elements [' ', 's']))
510 forAll (listOf (elements [' ', 's']))
@@ -530,42 +571,41 @@ parseManyWordQuotedBy =
530 , ["The qui[ck] brown", "fox"])
571 , ["The qui[ck] brown", "fox"])
531 ]
572 ]
532
573
574+splitWith :: Property
575+splitWith =
576+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
577+ case runIdentity $ S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
578+ Right (result_first, result_second) -> case ls of
579+ 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
580+ _ -> False
581+ Left _ -> case ls of
582+ 0 : 1 : _ -> False
583+ _ -> True
533
584
534−-- splitWithPass :: Property
535−-- splitWithPass =
536−-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
537−-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
538−-- Right (result_first, result_second) -> case ls of
539−-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
540−-- _ -> False
541−-- Left _ -> case ls of
542−-- 0 : 1 : _ -> False
543−-- _ -> True
544−
545−-- splitWithFailLeft :: Property
585+splitWithFailLeft :: Property
546−-- splitWithFailLeft =
586+splitWithFailLeft =
547−-- property (case S.parse (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
587+ property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
548−-- Right _ -> False
588+ Right _ -> False
549−-- Left _ -> True)
589+ Left _ -> True)
550
590
551−-- splitWithFailRight :: Property
591+splitWithFailRight :: Property
552−-- splitWithFailRight =
592+splitWithFailRight =
553−-- property (case S.parse (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
593+ property (case runIdentity $ S.parse (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
554−-- Right _ -> False
594+ Right _ -> False
555−-- Left _ -> True)
595+ Left _ -> True)
556
596
557−-- splitWithFailBoth :: Property
597+splitWithFailBoth :: Property
558−-- splitWithFailBoth =
598+splitWithFailBoth =
559−-- property (case S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
599+ property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
560−-- Right _ -> False
600+ Right _ -> False
561−-- Left _ -> True)
601+ Left _ -> True)
562
602
563-- teeWithPass :: Property
603-- teeWithPass :: Property
564-- teeWithPass =
604-- teeWithPass =
565−-- forAll (chooseInt (0, 10000)) $ \n ->
605+-- forAll (chooseInt (min_value, max_value)) $ \n ->
566-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
606-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
567-- let
607-- let
568−-- prsr = P.take n FL.toList
608+-- prsr = P.fromFold $ FL.take n FL.toList
569-- in
609-- in
570-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
610-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
571-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
611-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
@@ -610,50 +650,86 @@ deintercalate =
610-- shortestPass =
650-- shortestPass =
611-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
651-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
612-- let
652-- let
653+-- half_mid_value = mid_value `Prelude.div` 2
613−-- prsr_1 = P.takeWhile (<= (mid_value `Prelude.div` 2)) FL.toList
654+-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
614-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
655-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
615-- prsr_shortest = P.shortest prsr_1 prsr_2
656-- prsr_shortest = P.shortest prsr_1 prsr_2
616-- in
657-- in
617-- case S.parse prsr_shortest (S.fromList ls) of
658-- case S.parse prsr_shortest (S.fromList ls) of
618−-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= 2500) ls)
659+-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= half_mid_value) ls)
619-- Left _ -> property False
660-- Left _ -> property False
620
661
621−-- shortestFailLeft :: Property
662+-- shortestPassLeft :: Property
622−-- shortestFailLeft =
663+-- shortestPassLeft =
623-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
664-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
624-- Right r -> r == 1
665-- Right r -> r == 1
625-- Left _ -> False)
666-- Left _ -> False)
626−
667+--
627−-- shortestFailRight :: Property
668+-- shortestPassRight :: Property
628−-- shortestFailRight =
669+-- shortestPassRight =
629-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
670-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
630-- Right r -> r == 1
671-- Right r -> r == 1
631-- Left _ -> False)
672-- Left _ -> False)
632
673
633-- shortestFailBoth :: Property
674-- shortestFailBoth :: Property
634-- shortestFailBoth =
675-- shortestFailBoth =
676+-- property
677+-- (case S.parse
678+-- (P.shortest (P.die "die") (P.die "die"))
679+-- (S.fromList [1 :: Int]) of
680+-- Right _ -> False
681+-- Left _ -> True)
682+--
683+-- longestPass :: Property
684+-- longestPass =
685+-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
686+-- let
687+-- half_mid_value = mid_value `Prelude.div` 2
688+-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
689+-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
690+-- prsr_longest = P.longest prsr_1 prsr_2
691+-- in
692+-- case S.parse prsr_longest (S.fromList ls) of
693+-- Right long_list -> long_list == Prelude.takeWhile (<= mid_value) ls
694+-- Left _ -> False
695+--
696+-- longestPassLeft :: Property
697+-- longestPassLeft =
698+-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
699+-- Right r -> r == 1
700+-- Left _ -> False)
701+--
702+-- longestPassRight :: Property
703+-- longestPassRight =
704+-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
705+-- Right r -> r == 1
706+-- Left _ -> False)
707+--
708+-- longestFailBoth :: Property
709+-- longestFailBoth =
710+-- property
635−-- property (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
711+-- (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
636-- Right _ -> False
712-- Right _ -> False
637-- Left _ -> True)
713-- Left _ -> True)
638
714
639many :: Property
715many :: Property
640many =
716many =
641 forAll (listOf (chooseInt (0, 1))) $ \ls ->
717 forAll (listOf (chooseInt (0, 1))) $ \ls ->
642− let fldstp conL currL = return $ FL.Partial $ conL ++ currL
718+ let fldstp conL currL = return $ FL.Partial (conL ++ currL)
643 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
719 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
644 prsr =
720 prsr =
721+ flip P.many concatFold
645− flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
722+ $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
646− in
647− case runIdentity $ S.parse prsr (S.fromList ls) of
723+ in case runIdentity $ S.parse prsr (S.fromList ls) of
724+ Right res_list ->
648− Right res_list -> checkListEqual res_list
725+ checkListEqual res_list (Prelude.filter (== 0) ls)
649− $ Prelude.filter (== 0) ls
650 Left _ -> property False
726 Left _ -> property False
651
727
652−-- many_empty :: Property
728+many_empty :: Property
653−-- many_empty =
729+many_empty =
654−-- property (case S.parse (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
730+ property (case runIdentity $ S.parse (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
655−-- Right res_list -> checkListEqual res_list ([] :: [Int])
731+ Right res_list -> checkListEqual res_list ([] :: [Int])
656−-- Left _ -> property False)
732+ Left _ -> property False)
657
733
658some :: Property
734some :: Property
659some =
735some =
@@ -663,17 +739,17 @@ some =
663 fldstp conL currL = return $ FL.Partial $ conL ++ currL
739 fldstp conL currL = return $ FL.Partial $ conL ++ currL
664 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
740 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
665 prsr =
741 prsr =
742+ flip P.some concatFold
666− flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
743+ $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
667− in
668− case runIdentity $ S.parse prsr (S.fromList ls) of
744+ in case runIdentity $ S.parse prsr (S.fromList ls) of
669 Right res_list -> res_list == Prelude.filter (== 0) ls
745 Right res_list -> res_list == Prelude.filter (== 0) ls
670 Left _ -> False
746 Left _ -> False
671
747
672−-- someFail :: Property
748+someFail :: Property
673−-- someFail =
749+someFail =
674−-- property (case S.parse (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
750+ property (case runIdentity $ S.parse (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
675−-- Right _ -> False
751+ Right _ -> False
676−-- Left _ -> True)
752+ Left _ -> True)
677
753
678-------------------------------------------------------------------------------
754-------------------------------------------------------------------------------
679-- Instances
755-- Instances
@@ -687,12 +763,10 @@ applicative =
687 (,)
763 (,)
688 <$> P.fromFold (FL.take (length list1) FL.toList)
764 <$> P.fromFold (FL.take (length list1) FL.toList)
689 <*> P.fromFold (FL.take (length list2) FL.toList)
765 <*> P.fromFold (FL.take (length list2) FL.toList)
690− in monadicIO $ do
766+ in
691− s <- S.parse parser (S.fromList $ list1 ++ list2)
767+ case runIdentity $ S.parse parser (S.fromList $ list1 ++ list2) of
692− return $
693− case s of
694− Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
768+ Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
695− Left _ -> False
769+ Left _ -> False
696
770
697sequence :: Property
771sequence :: Property
698sequence =
772sequence =
@@ -708,6 +782,28 @@ sequence =
708 Right ls -> ls == ins
782 Right ls -> ls == ins
709 Left _ -> False
783 Left _ -> False
710
784
785+altEOF1 :: Property
786+altEOF1 =
787+ monadicIO $ do
788+ s1 <- S.parse
789+ (P.satisfy (> 0) <|> return 66)
790+ (S.fromList ([]::[Int]))
791+ return $
792+ case s1 of
793+ Right x -> x == 66
794+ Left _ -> False
795+
796+altEOF2 :: Property
797+altEOF2 =
798+ monadicIO $ do
799+ s1 <- S.parse
800+ ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
801+ (S.fromList ([51]::[Int]))
802+ return $
803+ case s1 of
804+ Right x -> x == [51]
805+ Left _ -> False
806+
711monad :: Property
807monad :: Property
712monad =
808monad =
713 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
809 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
@@ -740,6 +836,49 @@ parseMany =
740 $ S.parseMany p (S.fromList $ concat ins)
836 $ S.parseMany p (S.fromList $ concat ins)
741 listEquals (==) outs ins
837 listEquals (==) outs ins
742
838
839+-- basic sanity test for parsing from arrays
840+parseUnfold :: Property
841+parseUnfold = do
842+ let len = 200
843+ -- ls = input list (stream)
844+ -- clen = chunk size
845+ -- tlen = parser take size
846+ forAll
847+ ((,,)
848+ <$> vectorOf len (chooseAny :: Gen Int)
849+ <*> chooseInt (1, len)
850+ <*> chooseInt (1, len)) $ \(ls, clen, tlen) ->
851+ monadicIO $ do
852+ arrays <- S.toList $ S.chunksOf clen (S.fromList ls)
853+ let src = Source.source (Just (Producer.OuterLoop arrays))
854+ let parser = P.fromFold (FL.take tlen FL.toList)
855+ let readSrc =
856+ Source.producer
857+ $ Producer.concat Producer.fromList A.producer
858+ let streamParser =
859+ Producer.simplify (Source.parseManyD parser readSrc)
860+ xs <- run
861+ $ S.toList
862+ $ S.unfoldMany Unfold.fromList
863+ $ S.catRights
864+ $ S.unfold streamParser src
865+
866+ listEquals (==) xs ls
867+
868+parserSequence :: Property
869+parserSequence =
870+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
871+ monadicIO $ do
872+ let parsers = S.fromList
873+ $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
874+ let sequencedParser = P.sequence parsers FL.sum
875+ outs <-
876+ S.parse sequencedParser $ S.concatMap S.fromList (S.fromList ins)
877+ return $
878+ case outs of
879+ Right x -> x == sum (map sum ins)
880+ Left _ -> False
881+
743-------------------------------------------------------------------------------
882-------------------------------------------------------------------------------
744-- Test for a particular case hit during fs events testing
883-- Test for a particular case hit during fs events testing
745-------------------------------------------------------------------------------
884-------------------------------------------------------------------------------
@@ -1145,11 +1284,15 @@ main =
1145
1284
1146 describe "Instances" $ do
1285 describe "Instances" $ do
1147 prop "applicative" applicative
1286 prop "applicative" applicative
1287+ prop "Alternative: end of input 1" altEOF1
1288+ prop "Alternative: end of input 2" altEOF2
1148 prop "monad" monad
1289 prop "monad" monad
1149 prop "sequence" sequence
1290 prop "sequence" sequence
1150 describe "Stream parsing" $ do
1291 describe "Stream parsing" $ do
1151 prop "parseMany" parseMany
1292 prop "parseMany" parseMany
1152 prop "parseMany2Events" parseMany2Events
1293 prop "parseMany2Events" parseMany2Events
1294+ prop "parseUnfold" parseUnfold
1295+ prop "parserSequence" parserSequence
1153
1296
1154 describe "test for accumulator" $ do
1297 describe "test for accumulator" $ do
1155 prop "P.fromFold FL.sum = FL.sum" fromFold
1298 prop "P.fromFold FL.sum = FL.sum" fromFold
@@ -1174,16 +1317,15 @@ main =
1174 takeBetweenPass
1317 takeBetweenPass
1175 prop ("P.takeBetween = Prelude.take when len >= m and len <= n and fail"
1318 prop ("P.takeBetween = Prelude.take when len >= m and len <= n and fail"
1176 ++ "otherwise fail") Main.takeBetween
1319 ++ "otherwise fail") Main.takeBetween
1177−
1320+ prop "P.take = Prelude.take" Main.take
1178 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
1321 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
1179 prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
1322 prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
1180 Main.takeEQ
1323 Main.takeEQ
1181 prop "P.takeGE n ls = ls when len >= n" takeGEPass
1324 prop "P.takeGE n ls = ls when len >= n" takeGEPass
1182 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
1325 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
1183− -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
1326+ prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
1184 -- prop "Fail when stream length exceeded" lookAheadFail
1327 -- prop "Fail when stream length exceeded" lookAheadFail
1185− -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
1328+ prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
1186− prop "P.takeEndBy_ test" Main.takeEndBy_
1187 prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
1329 prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
1188 ++ " tail") takeStartBy
1330 ++ " tail") takeStartBy
1189 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
1331 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
@@ -1192,13 +1334,14 @@ main =
1192 prop "takeWhileP prd P.take = takeWhileMaxLen prd" takeWhileP
1334 prop "takeWhileP prd P.take = takeWhileMaxLen prd" takeWhileP
1193 prop ("P.takeP = Prelude.take") takeP
1335 prop ("P.takeP = Prelude.take") takeP
1194 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
1336 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
1337+ prop "groupByRolling" groupByRolling
1195 prop "many (P.wordBy ' ') = words'" wordBy
1338 prop "many (P.wordBy ' ') = words'" wordBy
1196 parseManyWordQuotedBy
1339 parseManyWordQuotedBy
1197 -- prop "choice" choice
1340 -- prop "choice" choice
1198− -- prop "" splitWithPass
1341+ prop "parse 0, then 1, else fail" splitWith
1199− -- prop "" splitWithFailLeft
1342+ prop "fail due to die as left parser" splitWithFailLeft
1200− -- prop "" splitWithFailRight
1343+ prop "fail due to die as right parser" splitWithFailRight
1201− -- prop "" splitWithFailBoth
1344+ prop "fail due to die as both parsers" splitWithFailBoth
1202 -- prop "" teeWithPass
1345 -- prop "" teeWithPass
1203 -- prop "" teeWithFailLeft
1346 -- prop "" teeWithFailLeft
1204 -- prop "" teeWithFailRight
1347 -- prop "" teeWithFailRight
@@ -1210,13 +1353,14 @@ main =
1210 -- prop "" shortestFailBoth
1353 -- prop "" shortestFailBoth
1211 prop ("P.many concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1354 prop ("P.many concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1212 ++ "Prelude.filter (== 0)") many
1355 ++ "Prelude.filter (== 0)") many
1213− -- prop "[] due to parser being die" many_empty
1356+ prop "[] due to parser being die" many_empty
1214 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1357 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1215 ++ "Prelude.filter (== 0)") some
1358 ++ "Prelude.filter (== 0)") some
1216− -- prop "fail due to parser being die" someFail
1359+ prop "fail due to parser being die" someFail
1217 prop "P.many == S.parseMany" manyEqParseMany
1360 prop "P.many == S.parseMany" manyEqParseMany
1218
1361
1219 prop "takeEndBy_" takeEndBy_
1362 prop "takeEndBy_" takeEndBy_
1363+ prop "takeEndByOrMax_" takeEndByOrMax_
1220 prop "takeEndBy1" takeEndBy1
1364 prop "takeEndBy1" takeEndBy1
1221 prop "takeEndBy2" takeEndBy2
1365 prop "takeEndBy2" takeEndBy2
1222 prop "takeEndByEsc" takeEndByEsc
1366 prop "takeEndByEsc" takeEndByEsc
test/Streamly/Test/Data/Parser/ParserD.hs → test/Streamly/Test/Data/ParserK.hsrenamed+1−1
@@ -757,7 +757,7 @@ parseMany2Events =
757-------------------------------------------------------------------------------
757-------------------------------------------------------------------------------
758
758
759moduleName :: String
759moduleName :: String
760−moduleName = "Data.Parser.ParserD"
760+moduleName = "Data.ParserK"
761
761
762main :: IO ()
762main :: IO ()
763main =
763main =
test/streamly-tests.cabal+2−2
@@ -293,10 +293,10 @@ test-suite Data.Parser
293 if flag(limit-build-mem)
293 if flag(limit-build-mem)
294 ghc-options: +RTS -M4000M -RTS
294 ghc-options: +RTS -M4000M -RTS
295
295
296−test-suite Data.Parser.ParserD
296+test-suite Data.ParserK
297 import: test-options
297 import: test-options
298 type: exitcode-stdio-1.0
298 type: exitcode-stdio-1.0
299− main-is: Streamly/Test/Data/Parser/ParserD.hs
299+ main-is: Streamly/Test/Data/ParserK.hs
300 if flag(limit-build-mem)
300 if flag(limit-build-mem)
301 ghc-options: +RTS -M1500M -RTS
301 ghc-options: +RTS -M1500M -RTS
302
302