1module Main (main) where
3import Control.Applicative ((<|>))
4import Control.Exception (displayException)
5import Data.Foldable (for_)
6import Data.Word (Word8, Word32, Word64)
7import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
8import Test.Hspec (Spec, hspec, describe)
9import Test.Hspec.QuickCheck
11 (arbitrary, forAll, elements, Property, property, listOf,
12 vectorOf, Gen, (.&&.))
13import Test.QuickCheck.Monadic (monadicIO, assert, run)
15import Prelude hiding (sequence)
17import qualified Control.Monad.Fail as Fail
18import qualified Data.List as List
19import qualified Prelude
20import qualified Streamly.Data.Stream as S
21import qualified Streamly.Internal.Data.Array as A
22import qualified Streamly.Internal.Data.Fold as FL
23import qualified Streamly.Internal.Data.Parser as P
24import qualified Streamly.Internal.Data.Producer as Producer
25import qualified Streamly.Internal.Data.Unfold as Unfold
26import qualified Test.Hspec as H
28#if MIN_VERSION_QuickCheck(2,14,0)
30import Test.QuickCheck (chooseAny)
31import Control.Monad.Identity (Identity(runIdentity, Identity))
32import Streamly.Internal.Data.Parser (ParseError(..))
36import System.Random (Random(random))
37import Test.QuickCheck.Gen (Gen(MkGen))
39-- | Generates a random element over the natural range of `a`.
40chooseAny :: Random a => Gen a
41chooseAny = MkGen (\r _ -> let (x,_) = random r in x)
64 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
66 s1 <- S.parse (P.fromFold FL.sum) (S.fromList ls)
67 o2 <- S.fold FL.sum (S.fromList ls)
75 forAll (chooseInt (min_value, max_value)) $ \x ->
76 case runIdentity $ S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
82 forAll (chooseInt (min_value, max_value)) $ \x ->
83 case runIdentity $ S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
90 case runIdentity $ S.parse (P.die "die test") (S.fromList [0 :: Int]) of
97 case runIdentity $ S.parse (P.dieM (Identity "die test")) (S.fromList [0 :: Int]) of
101parserFail :: Property
104 case runIdentity $ S.parse (Fail.fail err) (S.fromList [0 :: Int]) of
106 Left (ParseError e) -> err == e
108 err = "Testing MonadFail.fail."
110-- Element Parser Tests
114 forAll (chooseInt (1, max_length)) $ \list_length ->
115 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
116 case runIdentity $ S.parse P.peek (S.fromList ls) of
117 Right head_value -> case ls of
118 head_ls : _ -> head_value == head_ls
124 property (case runIdentity $ S.parse P.peek (S.fromList []) of
130 property (case runIdentity $ S.parse P.eof (S.fromList []) of
136 forAll (chooseInt (1, max_length)) $ \list_length ->
137 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
138 case runIdentity $ S.parse P.eof (S.fromList ls) of
142satisfyPass :: Property
144 forAll (chooseInt (mid_value, max_value)) $ \first_element ->
145 forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
147 ls = first_element : ls_tail
148 predicate = (>= mid_value)
150 case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
151 Right r -> r == first_element
156 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
157 case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
158 Right r -> case ls of
160 (x : _) -> predicate x && (r == x)
163 (x : _) -> not $ predicate x
165 predicate = (>= mid_value)
169 forAll (chooseInt (1, max_value)) $ \int ->
170 property (case runIdentity $ S.parse P.one (S.fromList [int]) of
177 case runIdentity $ S.parse P.one (S.fromList []) of
181-- Sequence Parsers Tests
182takeBetweenPass :: Property
184 forAll (chooseInt (min_value, max_value)) $ \m ->
185 forAll (chooseInt (m, max_value)) $ \n ->
186 forAll (chooseInt (m, max_value)) $ \list_length ->
187 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
189 case runIdentity $ S.parse (P.takeBetween m n FL.toList)
192 let lpl = Prelude.length parsed_list
193 in checkListEqual parsed_list
194 $ Prelude.take lpl ls
195 Left _ -> property False
197takeBetween :: Property
199 forAll (chooseInt (min_value, max_value)) $ \m ->
200 forAll (chooseInt (min_value, max_value)) $ \n ->
201 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
207 let inputLen = Prelude.length ls
209 let p = P.takeBetween m n FL.toList
210 case runIdentity $ S.parse p (S.fromList ls) of
212 let parsedLen = Prelude.length xs
213 in if inputLen >= m && parsedLen >= m && parsedLen <= n
214 then checkListEqual xs $ Prelude.take parsedLen ls
217 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
221 forAll (chooseInt (min_value, max_value)) $ \n ->
222 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
223 case runIdentity $ S.parse (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
224 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
225 Left _ -> property False
227takeEQPass :: Property
229 forAll (chooseInt (min_value, max_value)) $ \n ->
230 forAll (chooseInt (n, max_value)) $ \list_length ->
231 forAll (vectorOf list_length
232 (chooseInt (min_value, max_value))) $ \ls ->
233 case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
235 checkListEqual parsed_list (Prelude.take n ls)
236 Left _ -> property False
240 forAll (chooseInt (min_value, max_value)) $ \n ->
241 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
243 list_length = Prelude.length ls
245 case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
248 then checkListEqual parsed_list (Prelude.take n ls)
250 Left _ -> property (n > list_length)
252takeGEPass :: Property
254 forAll (chooseInt (min_value, max_value)) $ \n ->
255 forAll (chooseInt (n, max_value)) $ \list_length ->
256 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
258 case runIdentity $ S.parse (P.takeGE n FL.toList) (S.fromList ls) of
259 Right parsed_list -> checkListEqual parsed_list ls
260 Left _ -> property False
264 forAll (chooseInt (min_value, max_value)) $ \n ->
265 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
267 list_length = Prelude.length ls
269 case runIdentity $ S.parse (P.takeGE n FL.toList) (S.fromList ls) of
272 then checkListEqual parsed_list ls
274 Left _ -> property (n > list_length)
278 -> FL.Fold Identity Int [Int]
279 -> P.Parser Int Identity [Int]
281 -> (Int -> [Int] -> [Int])
283nLessThanEqual0 tk ltk =
284 forAll (elements [0, (-1)]) $ \n ->
285 forAll (listOf arbitrary) $ \ls ->
286 case runIdentity $ S.parse (tk n FL.toList) (S.fromList ls) of
287 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
288 Left _ -> property False
290takeProperties :: Spec
292 describe "take combinators when n <= 0/" $ do
293 prop "takeEQ n FL.toList = []" $
294 nLessThanEqual0 P.takeEQ (\_ -> const [])
295 prop "takeGE n FL.toList xs = xs" $
296 nLessThanEqual0 P.takeGE (\_ -> id)
298-- XXX lookAhead can't deal with EOF which in this case means when
299-- n==list_length, this test will fail. So excluding that case for now.
300lookAheadPass :: Property
302 forAll (chooseInt (min_value, max_value)) $ \n ->
304 takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
306 parsed_list_1 <- takeWithoutConsume
307 parsed_list_2 <- takeWithoutConsume
308 return (parsed_list_1, parsed_list_2)
310 forAll (chooseInt (n+1, max_value)) $ \list_length ->
311 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
312 case runIdentity $ S.parse parseTwice (S.fromList ls) of
313 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
314 Left _ -> property $ False
316-- lookAheadFail :: Property
318-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
320-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
322-- parsed_list_1 <- takeWithoutConsume
323-- parsed_list_2 <- takeWithoutConsume
324-- return (parsed_list_1, parsed_list_2)
326-- forAll (chooseInt (min_value, n - 1)) $ \list_length ->
327-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
328-- case S.parse parseTwice (S.fromList ls) of
334 forAll (chooseInt (min_value, max_value)) $ \n ->
336 takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
338 parsed_list_1 <- takeWithoutConsume
339 parsed_list_2 <- takeWithoutConsume
340 return (parsed_list_1, parsed_list_2)
342 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
343 case runIdentity $ S.parse parseTwice (S.fromList ls) of
344 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
345 Left _ -> property ((list_length < n) || (list_length == n && n == 0))
347 list_length = Prelude.length ls
349takeEndBy_ :: Property
351 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
352 case runIdentity $ S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
354 checkListEqual parsed_list (tkwhl ls)
355 Left _ -> property False
358 prsr = P.many (P.satisfy (const True)) FL.toList
359 tkwhl ls = Prelude.takeWhile (not . predicate) ls
361takeEndByOrMax_ :: Property
363 forAll (chooseInt (min_value, max_value)) $ \n ->
364 forAll (listOf (chooseInt (0, 1))) $ \ls ->
365 case runIdentity $ S.parse (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
366 Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
367 Left _ -> property False
371takeStartBy :: Property
373 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
376 case runIdentity $ S.parse parser (S.fromList ls1) of
378 if not $ Prelude.null ls1
380 let tls = Prelude.takeWhile (not . predicate) (tail ls1)
381 in checkListEqual parsed_list $
382 if predicate (head ls1)
384 else Prelude.takeWhile (not . predicate) ls1
385 else property $ Prelude.null parsed_list
386 Left _ -> property False
389 parser = P.takeStartBy predicate FL.toList
393 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
394 case runIdentity $ S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
396 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
397 Left _ -> property False
404 ((,) <$> chooseInt (min_value, max_value)
405 <*> listOf (chooseInt (0, 1)))
407 case runIdentity $ S.parse
408 (P.takeP takeNum (P.fromFold FL.toList))
411 checkListEqual parsed_list (Prelude.take takeNum ls)
412 Left _ -> property False
414takeWhile1 :: Property
416 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
417 case runIdentity $ S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
418 Right parsed_list -> case ls of
423 checkListEqual parsed_list
424 $ Prelude.takeWhile predicate ls
429 (x : _) -> property (not $ predicate x)
433takeWhileP :: Property
435 forAll (listOf (chooseInt (0, 1))) $ \ls ->
436 forAll (chooseInt (min_value, max_value)) $ \n ->
441 P.takeWhileP predicate
442 $ P.fromFold (FL.take n FL.toList)
444 takeWhileTillLen maxLen prd list =
445 Prelude.take maxLen $ Prelude.takeWhile prd list
447 case runIdentity $ S.parse prsr (S.fromList ls) of
451 (takeWhileTillLen n predicate ls)
452 Left _ -> property False
458 ((,,) <$> chooseInt (min_value, max_value)
459 <*> chooseInt (min_value, max_value)
460 <*> listOf (chooseInt (0, 1)))
462 case S.parse (P.choice [parser i, parser j]) (S.fromList ls) of
464 checkListEqual parsed_list $ take (min i j) ls
465 Left _ -> property False
469 parser i = P.fromFold (FL.take i FL.toList)
474 forAll (listOf (chooseInt (0, 1)))
476 case runIdentity $ S.parse parser (S.fromList ls) of
477 Right parsed -> checkListEqual parsed (groupByLF ls)
478 Left _ -> property False
483 parser = P.groupBy cmp FL.toList
486 | otherwise = head $ List.groupBy cmp lst
488groupByRolling :: Property
490 forAll (listOf (chooseInt (0, 1)))
492 case runIdentity $ S.parse parser (S.fromList ls) of
493 Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
494 Left _ -> property False
499 parser = P.groupBy cmp FL.toList
501 groupByLF Nothing (x:xs) = x : groupByLF (Just x) xs
502 groupByLF (Just y) (x:xs) =
504 then x : groupByLF (Just x) xs
509 forAll (listOf (elements [' ', 's']))
511 case runIdentity $ S.parse parser (S.fromList ls) of
512 Right parsed -> checkListEqual parsed (words' ls)
513 Left _ -> property False
518 parser = P.many (P.wordBy predicate FL.toList) FL.toList
521 in if wrds == [] && length lst > 0 then [""] else wrds
523parseManyWordQuotedBy :: H.SpecWith ()
524parseManyWordQuotedBy =
525 describe "parseMany wordQuotedBy"
527 $ \c@(kQ, isQ, input, expected) -> do
528 let inpStrm = S.fromList input
537 parser = P.wordWithQuotes kQ tr esc isQ spc FL.toList
538 result <- H.runIO $ S.fold FL.toList $ S.catRights $ S.parseMany parser inpStrm
539 H.it (showCase c) $ result `H.shouldBe` expected
543 showCase (kQ, _, input, expected) =
544 show kQ ++ ", " ++ input ++ " -> " ++ show expected
548 , \x -> if x == '\'' then Just '\'' else Nothing
549 , "The quick brown fox"
550 , ["The", "quick", "brown", "fox"])
552 , \x -> if x == '\'' then Just '\'' else Nothing
553 , "The' quick brown' fox"
554 , ["The' quick brown'", "fox"])
556 , \x -> if x == '\'' then Just '\'' else Nothing
557 , "The' quick brown' fox"
558 , ["The quick brown", "fox"])
560 , \x -> if x == '[' then Just ']' else Nothing
561 , "The[ quick brown] fox"
562 , ["The[ quick brown]", "fox"])
564 , \x -> if x == '[' then Just ']' else Nothing
565 , "The[ qui[ck] brown] \\ f[ ox]"
566 , ["The[ qui[ck] brown]", " f[ ox]"])
568 , \x -> if x == '[' then Just ']' else Nothing
569 , "The[ qui[ck] brown] fox"
570 , ["The qui[ck] brown", "fox"])
575 forAll (listOf (chooseInt (0, 1))) $ \ls ->
576 case runIdentity $ S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
577 Right (result_first, result_second) -> case ls of
578 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
584splitWithFailLeft :: Property
586 property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
590splitWithFailRight :: Property
592 property (case runIdentity $ S.parse (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
596splitWithFailBoth :: Property
598 property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
602-- teeWithPass :: Property
604-- forAll (chooseInt (min_value, max_value)) $ \n ->
605-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
607-- prsr = P.fromFold $ FL.take n FL.toList
609-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
610-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
611-- Left _ -> property False
613-- teeWithFailLeft :: Property
615-- property (case S.parse (P.teeWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
619-- teeWithFailRight :: Property
621-- property (case S.parse (P.teeWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
625-- teeWithFailBoth :: Property
627-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
632deintercalate :: Property
634 forAll (listOf (chooseAny :: Gen Int)) $ \ls ->
635 case runIdentity $ S.parse p (S.fromList ls) of
636 Right evenOdd -> evenOdd == List.partition even ls
640 p1 = P.takeWhile even FL.toList
641 p2 = P.takeWhile odd FL.toList
643 FL.tee (fmap concat $ FL.catLefts FL.toList)
644 (fmap concat $ FL.catRights FL.toList)
645 p = P.deintercalate p1 p2 partition
648-- shortestPass :: Property
650-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
652-- half_mid_value = mid_value `Prelude.div` 2
653-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
654-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
655-- prsr_shortest = P.shortest prsr_1 prsr_2
657-- case S.parse prsr_shortest (S.fromList ls) of
658-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= half_mid_value) ls)
659-- Left _ -> property False
661-- shortestPassLeft :: Property
663-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
667-- shortestPassRight :: Property
668-- shortestPassRight =
669-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
673-- shortestFailBoth :: Property
677-- (P.shortest (P.die "die") (P.die "die"))
678-- (S.fromList [1 :: Int]) of
682-- longestPass :: Property
684-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
686-- half_mid_value = mid_value `Prelude.div` 2
687-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
688-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
689-- prsr_longest = P.longest prsr_1 prsr_2
691-- case S.parse prsr_longest (S.fromList ls) of
692-- Right long_list -> long_list == Prelude.takeWhile (<= mid_value) ls
695-- longestPassLeft :: Property
697-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
701-- longestPassRight :: Property
703-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
707-- longestFailBoth :: Property
710-- (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
716 forAll (listOf (chooseInt (0, 1))) $ \ls ->
717 let fldstp conL currL = return $ FL.Partial (conL ++ currL)
718 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
720 flip P.many concatFold
721 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
722 in case runIdentity $ S.parse prsr (S.fromList ls) of
724 checkListEqual res_list (Prelude.filter (== 0) ls)
725 Left _ -> property False
727many_empty :: Property
729 property (case runIdentity $ S.parse (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
730 Right res_list -> checkListEqual res_list ([] :: [Int])
731 Left _ -> property False)
735 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
738 fldstp conL currL = return $ FL.Partial $ conL ++ currL
739 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
741 flip P.some concatFold
742 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
743 in case runIdentity $ S.parse prsr (S.fromList ls) of
744 Right res_list -> res_list == Prelude.filter (== 0) ls
749 property (case runIdentity $ S.parse (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
753-------------------------------------------------------------------------------
755-------------------------------------------------------------------------------
757applicative :: Property
759 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
760 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
763 <$> P.fromFold (FL.take (length list1) FL.toList)
764 <*> P.fromFold (FL.take (length list2) FL.toList)
766 case runIdentity $ S.parse parser (S.fromList $ list1 ++ list2) of
767 Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
772 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
773 let p xs = P.fromFold (FL.take (length xs) FL.toList)
777 (Prelude.sequence $ fmap p ins)
778 (S.fromList $ concat ins)
781 Right ls -> ls == ins
788 (P.satisfy (> 0) <|> return 66)
789 (S.fromList ([]::[Int]))
799 ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
800 (S.fromList ([51]::[Int]))
808 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
809 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
811 olist1 <- P.fromFold (FL.take (length list1) FL.toList)
812 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
813 return (olist1, olist2)
815 s <- S.parse parser (S.fromList $ list1 ++ list2)
818 Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
821-------------------------------------------------------------------------------
823-------------------------------------------------------------------------------
827 forAll (chooseInt (1,100)) $ \len ->
828 forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
831 let p = P.fromFold $ FL.take len FL.toList
835 $ S.parseMany p (S.fromList $ concat ins)
836 listEquals (==) outs ins
838-- basic sanity test for parsing from arrays
839parseUnfold :: Property
842 -- ls = input list (stream)
844 -- tlen = parser take size
847 <$> vectorOf len (chooseAny :: Gen Int)
848 <*> chooseInt (1, len)
849 <*> chooseInt (1, len)) $ \(ls, clen, tlen) ->
851 arrays <- S.toList $ S.chunksOf clen (S.fromList ls)
852 let src = Producer.source (Just (Producer.OuterLoop arrays))
853 let parser = P.fromFold (FL.take tlen FL.toList)
856 $ Producer.concat Producer.fromList A.producer
858 Producer.simplify (Producer.parseManyD parser readSrc)
861 $ S.unfoldMany Unfold.fromList
863 $ S.unfold streamParser src
865 listEquals (==) xs ls
867parserSequence :: Property
869 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
871 let parsers = S.fromList
872 $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
873 let sequencedParser = P.sequence parsers FL.sum
875 S.parse sequencedParser $ S.concatMap S.fromList (S.fromList ins)
878 Right x -> x == sum (map sum ins)
881-------------------------------------------------------------------------------
882-- Test for a particular case hit during fs events testing
883-------------------------------------------------------------------------------
886evId = [96,238,17,9,0,0,0,0]
889evFlags = [0,4,1,0,0,0,0,0]
892evPathLen = [71,0,0,0,0,0,0,0]
896 [47,85,115,101,114,115,47,118,111,108,47,118,101,109,98,97,47,99,111,109
897 ,112,111,115,101,119,101 ,108,108,45,116,101,99,104,47,69,110,103,47,112
898 ,114,111,106,101,99,116,115,47,115,116,114,101,97,109,108,121,47,115,116
899 ,114,101,97,109,108,121,47,116,109,112,47,122,122
903event = evId ++ evFlags ++ evPathLen ++ evPath
907 , eventFlags :: Word32
908 , eventAbsPath :: A.Array Word8
909 } deriving (Show, Ord, Eq)
911readOneEvent :: P.Parser Word8 IO Event
913 arr <- P.takeEQ 24 (A.writeN 24)
914 let arr1 = A.castUnsafe arr :: A.Array Word64
915 eid = A.getIndexUnsafe 0 arr1
916 eflags = A.getIndexUnsafe 1 arr1
917 pathLen = fromIntegral $ A.getIndexUnsafe 2 arr1
918 -- XXX handle if pathLen is 0
919 path <- P.takeEQ pathLen (A.writeN pathLen)
922 , eventFlags = fromIntegral eflags
923 , eventAbsPath = path
926parseMany2Events :: Property
933 $ S.parseMany readOneEvent
934 $ S.fromList (concat (replicate 2 event))
936 assert (length xs == 2)
937 -- XXX assuming little endian machine
939 { eventId = 152170080
941 , eventAbsPath = A.fromList evPath
943 in listEquals (==) xs (replicate 2 ev)
945manyEqParseMany :: Property
947 forAll (listOf (chooseInt (0, 100))) $ \lst ->
948 forAll (chooseInt (1, 100)) $ \i ->
950 let strm = S.fromList lst
951 r1 <- run $ S.parse (P.many (split i) FL.toList) strm
952 r2 <- run $ S.fold FL.toList $ S.catRights $ S.parseMany (split i) strm
960 split i = P.fromFold (FL.take i FL.toList)
963takeEndBy1 :: Property
965 forAll (listOf (chooseInt (0, 1))) $ \ls ->
966 case runIdentity $ S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
970 (takeWhileAndFirstFail (not . predicate) ls)
971 Left _ -> property False
973 prsr = P.many (P.satisfy (const True)) FL.toList
977 takeWhileAndFirstFail prd (x : xs) =
979 then x : takeWhileAndFirstFail prd xs
981 takeWhileAndFirstFail _ [] = []
985 => (a -> Bool) -> FL.Fold m a b -> S.Stream m a -> S.Stream m b
986splitWithSuffix predicate f = S.foldMany (FL.takeEndBy predicate f)
988takeEndBy2 :: Property
990 forAll (listOf (chooseInt (0, 1))) $ \ls ->
999 $ S.parseMany (P.takeEndBy predicate prsr) strm
1003 prsr = P.many (P.satisfy (const True)) FL.toList
1010 then S.fold FL.toList $ S.append strm1 (S.fromList [])
1011 else S.fold FL.toList strm1
1015 strm1 = splitWithSuffix predicate FL.toList strm
1017 case eitherParsedList of
1018 Left _ -> property False
1020 case eitherSplitList of
1021 Left _ -> property False
1022 Right splitList -> checkListEqual parsedList splitList
1024takeEndByEsc :: Property
1026 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1028 msg = "takeEndByEsc: trailing escape"
1032 isEsc x = x `mod` 6 == 0
1034 prsr = P.takeEndByEsc isEsc isSep prsr0
1038 prsr0 = P.many (P.satisfy (const True)) FL.toList
1040 escapeSep maybePrevEsc [] =
1041 case maybePrevEsc of
1043 Just prevEsc -> [prevEsc]
1044 escapeSep maybePrevEsc (x : xs) =
1045 case maybePrevEsc of
1048 then escapeSep (Just x) xs
1052 else x : escapeSep Nothing xs
1054 x : escapeSep Nothing xs
1056 case runIdentity $ S.parse prsr (S.fromList ls) of
1057 Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
1058 Left err -> property (displayException err == msg)
1060takeFramedByEsc_ :: Property
1062 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1070 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1072 checkPass (x : xs) maybePrevEsc openMinusClose =
1073 case maybePrevEsc of
1076 then checkPass xs (Just x) openMinusClose
1079 then checkPass xs Nothing (openMinusClose + 1)
1083 case openMinusClose of
1090 (openMinusClose - 1)
1092 checkPass xs Nothing openMinusClose
1093 Just _ -> checkPass xs Nothing openMinusClose
1094 checkPass [] _ _ = False
1096 checkPassBeg [] = False
1097 checkPassBeg xxs@(x:_)
1098 | isBegin x = checkPass xxs Nothing (0 :: Int)
1101 escapeFrame begin end escape l =
1103 helper (x : xs) maybePrevEsc openMinusClose =
1104 case maybePrevEsc of
1107 then helper xs (Just x) openMinusClose
1110 then helper xs Nothing (openMinusClose + 1)
1114 if openMinusClose - 1 == 0
1120 (openMinusClose - 1)
1122 x : helper xs Nothing openMinusClose
1124 if escape x || begin x || end x
1125 then x : helper xs Nothing openMinusClose
1127 prevEsc : x : helper xs Nothing openMinusClose
1128 helper [] _ _ = error "Cannot Reach Here"
1130 helper l Nothing (0 :: Int)
1132 case runIdentity $ S.parse prsr (S.fromList ls) of
1133 Right parsed_list ->
1135 then checkListEqual parsed_list $
1136 escapeFrame isBegin isEnd isEsc ls
1143takeFramedByEsc_Pass :: Property
1144takeFramedByEsc_Pass =
1145 forAll (listOf (chooseInt (min_value, max_value))) $ \list ->
1147 ls = (0 : list) ++ (Prelude.replicate (Prelude.length list + 1) 1)
1155 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1157 escapeFrame begin end escape l =
1159 helper (x : xs) maybePrevEsc openMinusClose =
1160 case maybePrevEsc of
1163 then helper xs (Just x) openMinusClose
1167 if openMinusClose == 0
1168 then helper xs Nothing (openMinusClose + 1)
1169 else x : helper xs Nothing (openMinusClose + 1)
1173 if openMinusClose - 1 == 0
1180 (openMinusClose - 1)
1182 x : helper xs Nothing openMinusClose
1184 x : helper xs Nothing openMinusClose
1185 helper [] _ _ = error "Cannot Reach Here"
1187 helper l Nothing (0 :: Int)
1189 case runIdentity $ S.parse prsr (S.fromList ls) of
1190 Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
1193takeFramedByEsc_Fail1 :: Property
1194takeFramedByEsc_Fail1 =
1196 msg = "takeFramedByEsc_: missing frame end"
1204 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1208 case runIdentity $ S.parse prsr (S.fromList ls) of
1209 Right _ -> property False
1210 Left err -> property (displayException err == msg)
1212takeFramedByEsc_Fail2 :: Property
1213takeFramedByEsc_Fail2 =
1215 msg = "takeFramedByEsc_: missing frame start"
1223 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1227 case runIdentity $ S.parse prsr (S.fromList ls) of
1228 Right _ -> property False
1229 Left err -> property (displayException err == msg)
1231takeFramedByEsc_Fail3 :: Property
1232takeFramedByEsc_Fail3 =
1234 msg = "takeFramedByEsc_: missing frame end"
1242 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1246 case runIdentity $ S.parse prsr (S.fromList ls) of
1247 Right _ -> property False
1248 Left err -> property $ (displayException err == msg)
1250takeStartBy_ :: Property
1252 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1254 msg = "takeFramedByGeneric: empty token"
1256 case runIdentity $ S.parse parser (S.fromList ls1) of
1257 Right parsed_list ->
1258 if not $ Prelude.null ls1
1260 let tls = Prelude.takeWhile (not . predicate) (tail ls1)
1261 in checkListEqual parsed_list $
1262 if predicate (head ls1)
1264 else Prelude.takeWhile (not . predicate) ls1
1265 else property $ Prelude.null parsed_list
1266 Left err -> property (displayException err == msg)
1269 parser = P.takeStartBy_ predicate FL.toList
1270-------------------------------------------------------------------------------
1272-------------------------------------------------------------------------------
1275moduleName = "Data.Parser"
1281 modifyMaxSuccess (const maxTestCount) $ do
1282 describe moduleName $ do
1284 describe "Instances" $ do
1285 prop "applicative" applicative
1286 prop "Alternative: end of input 1" altEOF1
1287 prop "Alternative: end of input 2" altEOF2
1289 prop "sequence" sequence
1290 describe "Stream parsing" $ do
1291 prop "parseMany" parseMany
1292 prop "parseMany2Events" parseMany2Events
1293 prop "parseUnfold" parseUnfold
1294 prop "parserSequence" parserSequence
1296 describe "test for accumulator" $ do
1297 prop "P.fromFold FL.sum = FL.sum" fromFold
1298 prop "fromPure value provided" fromPure
1299 prop "fromPure monadic value provided" fromEffect
1300 prop "fail err = Left (SomeException (ParseError err))" parserFail
1301 prop "always fail" die
1302 prop "always fail but monadic" dieM
1304 describe "test for element parser" $ do
1305 prop "peek = head with list length > 0" peekPass
1306 prop "peek fail on []" peekFail
1307 prop "eof pass on []" eofPass
1308 prop "eof fail on non-empty list" eofFail
1309 prop "first element exists and >= mid_value" satisfyPass
1310 prop "one pass on [Int]" onePass
1311 prop "one fail on []" one
1312 prop "check first element exists and satisfies predicate" satisfy
1314 describe "test for sequence parser" $ do
1315 prop "P.takeBetween = Prelude.take when len >= m and len <= n"
1317 prop ("P.takeBetween = Prelude.take when len >= m and len <= n and fail"
1318 ++ "otherwise fail") Main.takeBetween
1319 prop "P.take = Prelude.take" Main.take
1320 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
1321 prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
1323 prop "P.takeGE n ls = ls when len >= n" takeGEPass
1324 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
1325 prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
1326 -- prop "Fail when stream length exceeded" lookAheadFail
1327 prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
1328 prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
1329 ++ " tail") takeStartBy
1330 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
1331 prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
1332 ++ " else check why failed") takeWhile1
1333 prop "takeWhileP prd P.take = takeWhileMaxLen prd" takeWhileP
1334 prop ("P.takeP = Prelude.take") takeP
1335 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
1336 prop "groupByRolling" groupByRolling
1337 prop "many (P.wordBy ' ') = words'" wordBy
1338 parseManyWordQuotedBy
1339 -- prop "choice" choice
1340 prop "parse 0, then 1, else fail" splitWith
1341 prop "fail due to die as left parser" splitWithFailLeft
1342 prop "fail due to die as right parser" splitWithFailRight
1343 prop "fail due to die as both parsers" splitWithFailBoth
1344 -- prop "" teeWithPass
1345 -- prop "" teeWithFailLeft
1346 -- prop "" teeWithFailRight
1347 -- prop "" teeWithFailBoth
1348 -- prop "deintercalate" deintercalate
1349 -- prop "" shortestPass
1350 -- prop "" shortestFailLeft
1351 -- prop "" shortestFailRight
1352 -- prop "" shortestFailBoth
1353 prop ("P.many concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1354 ++ "Prelude.filter (== 0)") many
1355 prop "[] due to parser being die" many_empty
1356 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1357 ++ "Prelude.filter (== 0)") some
1358 prop "fail due to parser being die" someFail
1359 prop "P.many == S.parseMany" manyEqParseMany
1361 prop "takeEndBy_" takeEndBy_
1362 prop "takeEndByOrMax_" takeEndByOrMax_
1363 prop "takeEndBy1" takeEndBy1
1364 prop "takeEndBy2" takeEndBy2
1365 prop "takeEndByEsc" takeEndByEsc
1366 prop "takeFramedByEsc_" takeFramedByEsc_
1367 prop "takeFramedByEsc_Pass" takeFramedByEsc_Pass
1368 prop "takeFramedByEsc_Fail1" takeFramedByEsc_Fail1
1369 prop "takeFramedByEsc_Fail2" takeFramedByEsc_Fail2
1370 prop "takeFramedByEsc_Fail3" takeFramedByEsc_Fail3
1371 prop "takeStartBy_" takeStartBy_
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list