@@ -0,0 +1,1237 @@
1+module Main (main) where
3+import Control.Monad.Catch (MonadThrow)
4+-- import Control.Exception (SomeException(..), displayException, try)
5+-- import Data.Foldable (for_)
6+-- import Data.Word (Word8, Word32, Word64)
7+import Streamly.Internal.Data.Unboxed (Unbox)
8+import Streamly.Test.Common (listEquals)
9+import Test.Hspec (hspec, describe)
10+import Test.Hspec.QuickCheck
11+import Test.QuickCheck
12+ (forAll, Property, listOf,
14+import Test.QuickCheck.Monadic (monadicIO, run)
16+import Prelude hiding (sequence)
18+-- import qualified Data.List as List
19+-- import qualified Prelude
20+import qualified Streamly.Internal.Data.Array as A
21+import qualified Streamly.Internal.Data.Fold as FL
22+import qualified Streamly.Internal.Data.Parser.Chunked as P
23+import qualified Streamly.Internal.Data.Parser.ParserD as ParserD
24+import qualified Streamly.Internal.Data.Stream as S
25+import qualified Test.Hspec as H
27+#if MIN_VERSION_QuickCheck(2,14,0)
29+import Test.QuickCheck (chooseAny)
33+import System.Random (Random(random))
34+import Test.QuickCheck.Gen (Gen(MkGen))
36+-- | Generates a random element over the natural range of `a`.
37+chooseAny :: Random a => Gen a
38+chooseAny = MkGen (\r _ -> let (x,_) = random r in x)
62+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
63+ case (==) <$> S.parse (P.fromFold FL.sum) (S.fromList ls)
64+ <*> S.fold FL.sum (S.fromList ls) of
65+ Right is_equal -> is_equal
70+ forAll (chooseInt (min_value, max_value)) $ \x ->
71+ case S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
75+fromEffect :: Property
77+ forAll (chooseInt (min_value, max_value)) $ \x ->
78+ case S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
85+ case S.parse (P.die "die test") (S.fromList [0 :: Int]) of
92+ case S.parse (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
96+parserFail :: Property
99+ case S.parse (fail err) (S.fromList [0 :: Int]) of
101+ Left (SomeException e) -> err == displayException e
103+ err = "Testing MonadFail.fail."
105+-- Element Parser Tests
109+ forAll (chooseInt (1, max_length)) $ \list_length ->
110+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
111+ case S.parse P.peek (S.fromList ls) of
112+ Right head_value -> case ls of
113+ head_ls : _ -> head_value == head_ls
119+ property (case S.parse P.peek (S.fromList []) of
125+ property (case S.parse P.eof (S.fromList []) of
131+ forAll (chooseInt (1, max_length)) $ \list_length ->
132+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
133+ case S.parse P.eof (S.fromList ls) of
137+satisfyPass :: Property
139+ forAll (chooseInt (mid_value, max_value)) $ \first_element ->
140+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
142+ ls = first_element : ls_tail
143+ predicate = (>= mid_value)
145+ case S.parse (P.satisfy predicate) (S.fromList ls) of
146+ Right r -> r == first_element
151+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
152+ case S.parse (P.satisfy predicate) (S.fromList ls) of
153+ Right r -> case ls of
155+ (x : _) -> predicate x && (r == x)
156+ Left _ -> case ls of
158+ (x : _) -> not $ predicate x
160+ predicate = (>= mid_value)
164+ forAll (chooseInt (1, max_value)) $ \int ->
165+ property (case S.parse P.one (S.fromList [int]) of
171+ property (case S.parse P.one (S.fromList []) of
175+-- Sequence Parsers Tests
176+takeBetweenPass :: Property
178+ forAll (chooseInt (min_value, max_value)) $ \m ->
179+ forAll (chooseInt (m, max_value)) $ \n ->
180+ forAll (chooseInt (m, max_value)) $ \list_length ->
181+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
183+ case S.parse (P.takeBetween m n FL.toList)
185+ Right parsed_list ->
186+ let lpl = Prelude.length parsed_list
187+ in checkListEqual parsed_list
188+ $ Prelude.take lpl ls
189+ Left _ -> property False
191+takeBetween :: Property
193+ forAll (chooseInt (min_value, max_value)) $ \m ->
194+ forAll (chooseInt (min_value, max_value)) $ \n ->
195+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
201+ let inputLen = Prelude.length ls
203+ let p = P.takeBetween m n FL.toList
204+ r <- run $ try $ S.parse p (S.fromList ls)
207+ let parsedLen = Prelude.length xs
208+ in if inputLen >= m && parsedLen >= m && parsedLen <= n
209+ then checkListEqual xs $ Prelude.take parsedLen ls
211+ Left (_ :: SomeException) ->
212+ property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
214+takeEQPass :: Property
216+ forAll (chooseInt (min_value, max_value)) $ \n ->
217+ forAll (chooseInt (n, max_value)) $ \list_length ->
218+ forAll (vectorOf list_length
219+ (chooseInt (min_value, max_value))) $ \ls ->
220+ case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
221+ Right parsed_list ->
222+ checkListEqual parsed_list (Prelude.take n ls)
223+ Left _ -> property False
227+ forAll (chooseInt (min_value, max_value)) $ \n ->
228+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
230+ list_length = Prelude.length ls
232+ case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
233+ Right parsed_list ->
235+ then checkListEqual parsed_list (Prelude.take n ls)
237+ Left _ -> property (n > list_length)
239+takeGEPass :: Property
241+ forAll (chooseInt (min_value, max_value)) $ \n ->
242+ forAll (chooseInt (n, max_value)) $ \list_length ->
243+ forAll (vectorOf list_length (chooseInt (min_value, max_value)))
245+ case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
246+ Right parsed_list -> checkListEqual parsed_list ls
247+ Left _ -> property False
251+ forAll (chooseInt (min_value, max_value)) $ \n ->
252+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
254+ list_length = Prelude.length ls
256+ case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
257+ Right parsed_list ->
259+ then checkListEqual parsed_list ls
261+ Left _ -> property (n > list_length)
265+ -> FL.Fold (Either SomeException) Int [Int]
266+ -> P.Parser Int (Either SomeException) [Int]
268+ -> (Int -> [Int] -> [Int])
270+nLessThanEqual0 tk ltk =
271+ forAll (elements [0, (-1)]) $ \n ->
272+ forAll (listOf arbitrary) $ \ls ->
273+ case S.parse (tk n FL.toList) (S.fromList ls) of
274+ Right parsed_list -> checkListEqual parsed_list (ltk n ls)
275+ Left _ -> property False
277+takeProperties :: Spec
279+ describe "take combinators when n <= 0/" $ do
280+ prop "takeEQ n FL.toList = []" $
281+ nLessThanEqual0 P.takeEQ (\_ -> const [])
282+ prop "takeGE n FL.toList xs = xs" $
283+ nLessThanEqual0 P.takeGE (\_ -> id)
285+-- lookAheadPass :: Property
287+-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
289+-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
291+-- parsed_list_1 <- takeWithoutConsume
292+-- parsed_list_2 <- takeWithoutConsume
293+-- return (parsed_list_1, parsed_list_2)
295+-- forAll (chooseInt (n, max_value)) $ \list_length ->
296+-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
297+-- case S.parse parseTwice (S.fromList ls) of
298+-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
299+-- Left _ -> property $ False
301+-- lookAheadFail :: Property
303+-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
305+-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
307+-- parsed_list_1 <- takeWithoutConsume
308+-- parsed_list_2 <- takeWithoutConsume
309+-- return (parsed_list_1, parsed_list_2)
311+-- forAll (chooseInt (min_value, n - 1)) $ \list_length ->
312+-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
313+-- case S.parse parseTwice (S.fromList ls) of
317+-- lookAhead :: Property
319+-- forAll (chooseInt (min_value, max_value)) $ \n ->
321+-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
323+-- parsed_list_1 <- takeWithoutConsume
324+-- parsed_list_2 <- takeWithoutConsume
325+-- return (parsed_list_1, parsed_list_2)
327+-- forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
328+-- case S.parse parseTwice (S.fromList ls) of
329+-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
330+-- Left _ -> property ((list_length < n) || (list_length == n && n == 0))
332+-- list_length = Prelude.length ls
334+takeEndBy_ :: Property
336+ forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
337+ case S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
338+ Right parsed_list ->
339+ checkListEqual parsed_list (tkwhl ls)
340+ Left _ -> property False
342+ predicate = (>= 100)
343+ prsr = P.many (P.satisfy (const True)) FL.toList
344+ tkwhl ls = Prelude.takeWhile (not . predicate) ls
346+takeStartBy :: Property
348+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
351+ case S.parse parser (S.fromList ls1) of
352+ Right parsed_list ->
353+ if not $ Prelude.null ls1
355+ let tls = Prelude.takeWhile (not . predicate) (tail ls1)
356+ in checkListEqual parsed_list $
357+ if predicate (head ls1)
359+ else Prelude.takeWhile (not . predicate) ls1
360+ else property $ Prelude.null parsed_list
361+ Left _ -> property False
364+ parser = P.takeStartBy predicate FL.toList
366+takeWhile :: Property
368+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
369+ case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
370+ Right parsed_list ->
371+ checkListEqual parsed_list (Prelude.takeWhile predicate ls)
372+ Left _ -> property False
379+ ((,) <$> chooseInt (min_value, max_value)
380+ <*> listOf (chooseInt (0, 1)))
383+ (P.takeP takeNum (P.fromFold FL.toList))
385+ Right parsed_list ->
386+ checkListEqual parsed_list (Prelude.take takeNum ls)
387+ Left _ -> property False
389+takeWhile1 :: Property
391+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
392+ case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
393+ Right parsed_list -> case ls of
394+ [] -> property False
398+ checkListEqual parsed_list
399+ $ Prelude.takeWhile predicate ls
402+ Left _ -> case ls of
404+ (x : _) -> property (not $ predicate x)
408+takeWhileP :: Property
410+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
411+ forAll (chooseInt (min_value, max_value)) $ \n ->
416+ P.takeWhileP predicate
417+ $ P.fromFold (FL.take n FL.toList)
419+ takeWhileTillLen maxLen prd list =
420+ Prelude.take maxLen $ Prelude.takeWhile prd list
422+ case S.parse prsr (S.fromList ls) of
423+ Right parsed_list ->
426+ (takeWhileTillLen n predicate ls)
427+ Left _ -> property False
433+ ((,,) <$> chooseInt (min_value, max_value)
434+ <*> chooseInt (min_value, max_value)
435+ <*> listOf (chooseInt (0, 1)))
437+ case S.parse (P.choice [parser i, parser j]) (S.fromList ls) of
438+ Right parsed_list ->
439+ checkListEqual parsed_list $ take (min i j) ls
440+ Left _ -> property False
444+ parser i = P.fromFold (FL.take i FL.toList)
449+ forAll (listOf (chooseInt (0, 1)))
451+ case S.parse parser (S.fromList ls) of
452+ Right parsed -> checkListEqual parsed (groupByLF ls)
453+ Left _ -> property False
458+ parser = P.groupBy cmp FL.toList
461+ | otherwise = head $ List.groupBy cmp lst
465+ forAll (listOf (elements [' ', 's']))
467+ case S.parse parser (S.fromList ls) of
468+ Right parsed -> checkListEqual parsed (words' ls)
469+ Left _ -> property False
473+ predicate = (== ' ')
474+ parser = P.many (P.wordBy predicate FL.toList) FL.toList
476+ let wrds = words lst
477+ in if wrds == [] && length lst > 0 then [""] else wrds
479+parseManyWordQuotedBy :: H.SpecWith ()
480+parseManyWordQuotedBy =
481+ describe "parseMany wordQuotedBy"
483+ $ \c@(kQ, lQ, rQ, fromLQ, input, expected) -> do
484+ let inpStrm = S.fromList input
492+ parser = P.wordQuotedBy kQ esc lQ rQ fromLQ spc FL.toList
493+ result <- H.runIO $ S.fold FL.toList $ S.parseMany parser inpStrm
494+ H.it (showCase c) $ result `H.shouldBe` expected
498+ showCase (kQ, _, _, _, input, expected) =
499+ show kQ ++ ", " ++ input ++ " -> " ++ show expected
506+ , "The quick brown fox"
507+ , ["The", "quick", "brown", "fox"])
512+ , "The' quick brown' fox"
513+ , ["The' quick brown'", "fox"])
518+ , "The' quick brown' fox"
519+ , ["The quick brown", "fox"])
523+ , \x -> if x == '[' then ']' else error "Not an opening quote."
524+ , "The[ quick brown] fox"
525+ , ["The[ quick brown]", "fox"])
529+ , \x -> if x == '[' then ']' else error "Not an opening quote."
530+ , "The[ qui[ck] brown] \\ f[ ox]"
531+ , ["The[ qui[ck] brown]", " f[ ox]"])
535+ , \x -> if x == '[' then ']' else error "Not an opening quote."
536+ , "The[ qui[ck] brown] fox"
537+ , ["The qui[ck] brown", "fox"])
541+-- splitWithPass :: Property
543+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
544+-- case S.parse (P.serialWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
545+-- Right (result_first, result_second) -> case ls of
546+-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
548+-- Left _ -> case ls of
549+-- 0 : 1 : _ -> False
552+-- splitWithFailLeft :: Property
553+-- splitWithFailLeft =
554+-- property (case S.parse (P.serialWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
558+-- splitWithFailRight :: Property
559+-- splitWithFailRight =
560+-- property (case S.parse (P.serialWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
564+-- splitWithFailBoth :: Property
565+-- splitWithFailBoth =
566+-- property (case S.parse (P.serialWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
570+-- teeWithPass :: Property
572+-- forAll (chooseInt (0, 10000)) $ \n ->
573+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
575+-- prsr = P.take n FL.toList
577+-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
578+-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
579+-- Left _ -> property False
581+-- teeWithFailLeft :: Property
583+-- property (case S.parse (P.teeWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
587+-- teeWithFailRight :: Property
588+-- teeWithFailRight =
589+-- property (case S.parse (P.teeWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
593+-- teeWithFailBoth :: Property
595+-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
599+deintercalate :: Property
601+ forAll (listOf (chooseAny :: Gen Int)) $ \ls ->
602+ case S.parse p (S.fromList ls) of
603+ Right evenOdd -> evenOdd == List.partition even ls
607+ p1 = P.takeWhile even FL.toList
608+ p2 = P.takeWhile odd FL.toList
610+ FL.tee (fmap concat $ FL.lefts FL.toList)
611+ (fmap concat $ FL.rights FL.toList)
612+ p = P.deintercalate p1 p2 partition
614+-- shortestPass :: Property
616+-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
618+-- prsr_1 = P.takeWhile (<= (mid_value `Prelude.div` 2)) FL.toList
619+-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
620+-- prsr_shortest = P.shortest prsr_1 prsr_2
622+-- case S.parse prsr_shortest (S.fromList ls) of
623+-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= 2500) ls)
624+-- Left _ -> property False
626+-- shortestFailLeft :: Property
627+-- shortestFailLeft =
628+-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
632+-- shortestFailRight :: Property
633+-- shortestFailRight =
634+-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
638+-- shortestFailBoth :: Property
639+-- shortestFailBoth =
640+-- property (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
646+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
647+ let fldstp conL currL = return $ FL.Partial $ conL ++ currL
648+ concatFold = FL.Fold fldstp (return (FL.Partial [])) return
650+ flip P.many concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
652+ case S.parse prsr (S.fromList ls) of
653+ Right res_list -> checkListEqual res_list
654+ $ Prelude.filter (== 0) ls
655+ Left _ -> property False
657+-- many_empty :: Property
659+-- property (case S.parse (P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
660+-- Right res_list -> checkListEqual res_list ([] :: [Int])
661+-- Left _ -> property False)
665+ forAll (listOf (chooseInt (0, 1))) $ \genLs ->
668+ fldstp conL currL = return $ FL.Partial $ conL ++ currL
669+ concatFold = FL.Fold fldstp (return (FL.Partial [])) return
671+ flip P.some concatFold $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
673+ case S.parse prsr (S.fromList ls) of
674+ Right res_list -> res_list == Prelude.filter (== 0) ls
678+-- someFail :: Property
680+-- property (case S.parse (P.some FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
684+-------------------------------------------------------------------------------
686+-------------------------------------------------------------------------------
688+parse :: (MonadThrow f, Unbox a) =>
689+ P.ParserChunked a f b -> S.Stream f (A.Array a) -> f b
690+parse parser stream = fmap fst (P.parseBreak parser stream)
692+applicative :: Property
694+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
695+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
698+ <$> ParserD.fromFold (FL.take (length list1) FL.toList)
699+ <*> ParserD.fromFold (FL.take (length list2) FL.toList)
701+ let arrays = [A.fromList list1, A.fromList list2]
703+ run $ parse (P.fromRaw parser) (S.fromList arrays)
704+ listEquals (==) olist1 list1
705+ listEquals (==) olist2 list2
710+ forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
711+ let p xs = ParserD.fromFold (FL.take (length xs) FL.toList)
715+ (Prelude.sequence $ fmap p ins)
716+ (S.fromList $ concat ins)
717+ listEquals (==) outs ins
722+ forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
723+ forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
725+ olist1 <- ParserD.fromFold (FL.take (length list1) FL.toList)
726+ olist2 <- ParserD.fromFold (FL.take (length list2) FL.toList)
727+ return (olist1, olist2)
729+ let arrays = [A.fromList list1, A.fromList list2]
731+ run $ parse (P.fromRaw parser) (S.fromList arrays)
732+ listEquals (==) olist1 list1
733+ listEquals (==) olist2 list2
736+-------------------------------------------------------------------------------
738+-------------------------------------------------------------------------------
740+parseMany :: Property
742+ forAll (chooseInt (1,100)) $ \len ->
743+ forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
746+ let p = P.fromFold $ FL.take len FL.toList
749+ $ S.parseMany p (S.fromList $ concat ins)
750+ listEquals (==) outs ins
752+-------------------------------------------------------------------------------
753+-- Test for a particular case hit during fs events testing
754+-------------------------------------------------------------------------------
757+evId = [96,238,17,9,0,0,0,0]
760+evFlags = [0,4,1,0,0,0,0,0]
763+evPathLen = [71,0,0,0,0,0,0,0]
767+ [47,85,115,101,114,115,47,118,111,108,47,118,101,109,98,97,47,99,111,109
768+ ,112,111,115,101,119,101 ,108,108,45,116,101,99,104,47,69,110,103,47,112
769+ ,114,111,106,101,99,116,115,47,115,116,114,101,97,109,108,121,47,115,116
770+ ,114,101,97,109,108,121,47,116,109,112,47,122,122
774+event = evId ++ evFlags ++ evPathLen ++ evPath
778+ , eventFlags :: Word32
779+ , eventAbsPath :: A.Array Word8
780+ } deriving (Show, Ord, Eq)
782+readOneEvent :: P.Parser Word8 IO Event
784+ arr <- P.takeEQ 24 (A.writeN 24)
785+ let arr1 = A.castUnsafe arr :: A.Array Word64
786+ eid = A.unsafeIndex 0 arr1
787+ eflags = A.unsafeIndex 1 arr1
788+ pathLen = fromIntegral $ A.unsafeIndex 2 arr1
789+ -- XXX handle if pathLen is 0
790+ path <- P.takeEQ pathLen (A.writeN pathLen)
793+ , eventFlags = fromIntegral eflags
794+ , eventAbsPath = path
797+parseMany2Events :: Property
803+ $ S.parseMany readOneEvent
804+ $ S.fromList (concat (replicate 2 event))
806+ assert (length xs == 2)
807+ -- XXX assuming little endian machine
809+ { eventId = 152170080
810+ , eventFlags = 66560
811+ , eventAbsPath = A.fromList evPath
813+ in listEquals (==) xs (replicate 2 ev)
815+manyEqParseMany :: Property
817+ forAll (listOf (chooseInt (0, 100))) $ \lst ->
818+ forAll (chooseInt (1, 100)) $ \i ->
820+ let strm = S.fromList lst
821+ r1 <- run $ S.parse (P.many (split i) FL.toList) strm
822+ r2 <- run $ S.fold FL.toList $ S.parseMany (split i) strm
827+ split i = P.fromFold (FL.take i FL.toList)
830+takeEndBy1 :: Property
832+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
833+ case S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
834+ Right parsed_list ->
837+ (takeWhileAndFirstFail (not . predicate) ls)
838+ Left _ -> property False
840+ prsr = P.many (P.satisfy (const True)) FL.toList
844+ takeWhileAndFirstFail prd (x : xs) =
846+ then x : takeWhileAndFirstFail prd xs
848+ takeWhileAndFirstFail _ [] = []
852+ => (a -> Bool) -> FL.Fold m a b -> S.Stream m a -> S.Stream m b
853+splitWithSuffix predicate f = S.foldMany (FL.takeEndBy predicate f)
855+takeEndBy2 :: Property
857+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
859+ strm = S.fromList ls
865+ S.parseMany (P.takeEndBy predicate prsr) strm
869+ prsr = P.many (P.satisfy (const True)) FL.toList
876+ then S.fold FL.toList $ S.append strm1 (S.fromList [])
877+ else S.fold FL.toList strm1
881+ strm1 = splitWithSuffix predicate FL.toList strm
883+ case eitherParsedList of
884+ Left _ -> property False
886+ case eitherSplitList of
887+ Left _ -> property False
888+ Right splitList -> checkListEqual parsedList splitList
890+takeEndByEsc :: Property
892+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
894+ msg = "takeEndByEsc: trailing escape"
898+ isEsc x = x `mod` 6 == 0
900+ prsr = P.takeEndByEsc isEsc isSep prsr0
904+ prsr0 = P.many (P.satisfy (const True)) FL.toList
906+ escapeSep maybePrevEsc [] =
907+ case maybePrevEsc of
909+ Just prevEsc -> [prevEsc]
910+ escapeSep maybePrevEsc (x : xs) =
911+ case maybePrevEsc of
914+ then escapeSep (Just x) xs
918+ else x : escapeSep Nothing xs
920+ x : escapeSep Nothing xs
922+ case S.parse prsr (S.fromList ls) of
923+ Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
924+ Left err -> property (displayException err == msg)
926+takeFramedByEsc_ :: Property
928+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
936+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
938+ checkPass (x : xs) maybePrevEsc openMinusClose =
939+ case maybePrevEsc of
942+ then checkPass xs (Just x) openMinusClose
945+ then checkPass xs Nothing (openMinusClose + 1)
949+ case openMinusClose of
956+ (openMinusClose - 1)
958+ checkPass xs Nothing openMinusClose
959+ Just _ -> checkPass xs Nothing openMinusClose
960+ checkPass [] _ _ = False
962+ checkPassBeg [] = False
963+ checkPassBeg xxs@(x:_)
964+ | isBegin x = checkPass xxs Nothing (0 :: Int)
967+ escapeFrame begin end escape l =
969+ helper (x : xs) maybePrevEsc openMinusClose =
970+ case maybePrevEsc of
973+ then helper xs (Just x) openMinusClose
976+ then helper xs Nothing (openMinusClose + 1)
980+ if openMinusClose - 1 == 0
986+ (openMinusClose - 1)
988+ x : helper xs Nothing openMinusClose
990+ if escape x || begin x || end x
991+ then x : helper xs Nothing openMinusClose
993+ prevEsc : x : helper xs Nothing openMinusClose
994+ helper [] _ _ = error "Cannot Reach Here"
996+ helper l Nothing (0 :: Int)
998+ case S.parse prsr (S.fromList ls) of
999+ Right parsed_list ->
1001+ then checkListEqual parsed_list $
1002+ escapeFrame isBegin isEnd isEsc ls
1003+ else property False
1006+ then property False
1009+takeFramedByEsc_Pass :: Property
1010+takeFramedByEsc_Pass =
1011+ forAll (listOf (chooseInt (min_value, max_value))) $ \list ->
1013+ ls = (0 : list) ++ (Prelude.replicate (Prelude.length list + 1) 1)
1021+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1023+ escapeFrame begin end escape l =
1025+ helper (x : xs) maybePrevEsc openMinusClose =
1026+ case maybePrevEsc of
1029+ then helper xs (Just x) openMinusClose
1033+ if openMinusClose == 0
1034+ then helper xs Nothing (openMinusClose + 1)
1035+ else x : helper xs Nothing (openMinusClose + 1)
1039+ if openMinusClose - 1 == 0
1046+ (openMinusClose - 1)
1048+ x : helper xs Nothing openMinusClose
1050+ x : helper xs Nothing openMinusClose
1051+ helper [] _ _ = error "Cannot Reach Here"
1053+ helper l Nothing (0 :: Int)
1055+ case S.parse prsr (S.fromList ls) of
1056+ Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
1057+ _ -> property False
1059+takeFramedByEsc_Fail1 :: Property
1060+takeFramedByEsc_Fail1 =
1062+ msg = "takeFramedByEsc_: missing frame end"
1070+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1074+ case S.parse prsr (S.fromList ls) of
1075+ Right _ -> property False
1076+ Left err -> property (displayException err == msg)
1078+takeFramedByEsc_Fail2 :: Property
1079+takeFramedByEsc_Fail2 =
1081+ msg = "takeFramedByEsc_: missing frame start"
1089+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1093+ case S.parse prsr (S.fromList ls) of
1094+ Right _ -> property False
1095+ Left err -> property (displayException err == msg)
1097+takeFramedByEsc_Fail3 :: Property
1098+takeFramedByEsc_Fail3 =
1100+ msg = "takeFramedByEsc_: missing frame end"
1108+ prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1112+ case S.parse prsr (S.fromList ls) of
1113+ Right _ -> property False
1114+ Left err -> property $ (displayException err == msg)
1116+takeStartBy_ :: Property
1118+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1120+ msg = "takeFramedByGeneric: empty token"
1122+ case S.parse parser (S.fromList ls1) of
1123+ Right parsed_list ->
1124+ if not $ Prelude.null ls1
1126+ let tls = Prelude.takeWhile (not . predicate) (tail ls1)
1127+ in checkListEqual parsed_list $
1128+ if predicate (head ls1)
1130+ else Prelude.takeWhile (not . predicate) ls1
1131+ else property $ Prelude.null parsed_list
1132+ Left err -> property (displayException err == msg)
1135+ parser = P.takeStartBy_ predicate FL.toList
1137+-------------------------------------------------------------------------------
1139+-------------------------------------------------------------------------------
1141+moduleName :: String
1142+moduleName = "Data.Parser"
1148+ modifyMaxSuccess (const maxTestCount) $ do
1149+ describe moduleName $ do
1151+ describe "Instances" $ do
1152+ prop "applicative" applicative
1155+ prop "sequence" sequence
1156+ describe "Stream parsing" $ do
1157+ prop "parseMany" parseMany
1158+ prop "parseMany2Events" parseMany2Events
1160+ describe "test for accumulator" $ do
1161+ prop "P.fromFold FL.sum = FL.sum" fromFold
1162+ prop "fromPure value provided" fromPure
1163+ prop "fromPure monadic value provided" fromEffect
1164+ prop "fail err = Left (SomeException (ParseError err))" parserFail
1165+ prop "always fail" die
1166+ prop "always fail but monadic" dieM
1168+ describe "test for element parser" $ do
1169+ prop "peek = head with list length > 0" peekPass
1170+ prop "peek fail on []" peekFail
1171+ prop "eof pass on []" eofPass
1172+ prop "eof fail on non-empty list" eofFail
1173+ prop "first element exists and >= mid_value" satisfyPass
1174+ prop "one pass on [Int]" onePass
1175+ prop "one fail on []" one
1176+ prop "check first element exists and satisfies predicate" satisfy
1178+ describe "test for sequence parser" $ do
1179+ prop "P.takeBetween = Prelude.take when len >= m and len <= n"
1181+ prop ("P.takeBetween = Prelude.take when len >= m and len <= n and fail"
1182+ ++ "otherwise fail") Main.takeBetween
1184+ prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
1185+ prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
1187+ prop "P.takeGE n ls = ls when len >= n" takeGEPass
1188+ prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
1189+ -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
1190+ -- prop "Fail when stream length exceeded" lookAheadFail
1191+ -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
1192+ prop "P.takeEndBy_ test" Main.takeEndBy_
1193+ prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
1194+ ++ " tail") takeStartBy
1195+ prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
1196+ prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
1197+ ++ " else check why failed") takeWhile1
1198+ prop "takeWhileP prd P.take = takeWhileMaxLen prd" takeWhileP
1199+ prop ("P.takeP = Prelude.take") takeP
1200+ prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
1201+ prop "many (P.wordBy ' ') = words'" wordBy
1202+ parseManyWordQuotedBy
1203+ -- prop "choice" choice
1204+ -- prop "" splitWithPass
1205+ -- prop "" splitWithFailLeft
1206+ -- prop "" splitWithFailRight
1207+ -- prop "" splitWithFailBoth
1208+ -- prop "" teeWithPass
1209+ -- prop "" teeWithFailLeft
1210+ -- prop "" teeWithFailRight
1211+ -- prop "" teeWithFailBoth
1212+ prop "deintercalate" deintercalate
1213+ -- prop "" shortestPass
1214+ -- prop "" shortestFailLeft
1215+ -- prop "" shortestFailRight
1216+ -- prop "" shortestFailBoth
1217+ prop ("P.many concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1218+ ++ "Prelude.filter (== 0)") many
1219+ -- prop "[] due to parser being die" many_empty
1220+ prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1221+ ++ "Prelude.filter (== 0)") some
1222+ -- prop "fail due to parser being die" someFail
1223+ prop "P.many == S.parseMany" manyEqParseMany
1225+ prop "takeEndBy_" takeEndBy_
1226+ prop "takeEndBy1" takeEndBy1
1227+ prop "takeEndBy2" takeEndBy2
1228+ prop "takeEndByEsc" takeEndByEsc
1229+ prop "takeFramedByEsc_" takeFramedByEsc_
1230+ prop "takeFramedByEsc_Pass" takeFramedByEsc_Pass
1231+ prop "takeFramedByEsc_Fail1" takeFramedByEsc_Fail1
1232+ prop "takeFramedByEsc_Fail2" takeFramedByEsc_Fail2
1233+ prop "takeFramedByEsc_Fail3" takeFramedByEsc_Fail3
1234+ prop "takeStartBy_" takeStartBy_