1-- XXX We are using head/tail at one place
2#if __GLASGOW_HASKELL__ >= 908
3{-# OPTIONS_GHC -Wno-x-partial #-}
5module Main (main) where
7import Control.Applicative ((<|>))
8import Control.Exception (displayException)
9import Data.Foldable (for_)
10import Data.Word (Word8, Word32, Word64)
11import Streamly.Test.Common (listEquals, checkListEqual, chooseInt)
12import Test.Hspec (Spec, hspec, describe)
13import Test.Hspec.QuickCheck
15 (arbitrary, forAll, elements, Property, property, listOf,
16 vectorOf, Gen, (.&&.))
17import Test.QuickCheck.Monadic (monadicIO, assert, run)
19import Prelude hiding (sequence)
21import qualified Control.Monad.Fail as Fail
22import qualified Data.List as List
23import qualified Prelude
24import qualified Streamly.Data.Stream as S
25import qualified Streamly.Internal.Data.Array as A
26import qualified Streamly.Internal.Data.Fold as FL
27import qualified Streamly.Internal.Data.Parser as P
28import qualified Streamly.Internal.Data.Producer as Producer
29import qualified Streamly.Internal.Data.Unfold as Unfold
30import qualified Test.Hspec as H
32#if MIN_VERSION_QuickCheck(2,14,0)
34import Test.QuickCheck (chooseAny)
35import Control.Monad.Identity (Identity(runIdentity, Identity))
36import Streamly.Internal.Data.Parser (ParseError(..))
40import System.Random (Random(random))
41import Test.QuickCheck.Gen (Gen(MkGen))
43-- | Generates a random element over the natural range of `a`.
44chooseAny :: Random a => Gen a
45chooseAny = MkGen (\r _ -> let (x,_) = random r in x)
68 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
70 s1 <- S.parse (P.fromFold FL.sum) (S.fromList ls)
71 o2 <- S.fold FL.sum (S.fromList ls)
79 forAll (chooseInt (min_value, max_value)) $ \x ->
80 case runIdentity $ S.parse (P.fromPure x) (S.fromList [1 :: Int]) of
86 forAll (chooseInt (min_value, max_value)) $ \x ->
87 case runIdentity $ S.parse (P.fromEffect $ return x) (S.fromList [1 :: Int]) of
94 case runIdentity $ S.parse (P.die "die test") (S.fromList [0 :: Int]) of
101 case runIdentity $ S.parse (P.dieM (Identity "die test")) (S.fromList [0 :: Int]) of
105parserFail :: Property
108 case runIdentity $ S.parse (Fail.fail err) (S.fromList [0 :: Int]) of
110 Left (ParseError e) -> err == e
112 err = "Testing MonadFail.fail."
114-- Element Parser Tests
118 forAll (chooseInt (1, max_length)) $ \list_length ->
119 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
120 case runIdentity $ S.parse P.peek (S.fromList ls) of
121 Right head_value -> case ls of
122 head_ls : _ -> head_value == head_ls
128 property (case runIdentity $ S.parse P.peek (S.fromList []) of
134 property (case runIdentity $ S.parse P.eof (S.fromList []) of
140 forAll (chooseInt (1, max_length)) $ \list_length ->
141 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
142 case runIdentity $ S.parse P.eof (S.fromList ls) of
146satisfyPass :: Property
148 forAll (chooseInt (mid_value, max_value)) $ \first_element ->
149 forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
151 ls = first_element : ls_tail
152 predicate = (>= mid_value)
154 case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
155 Right r -> r == first_element
160 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
161 case runIdentity $ S.parse (P.satisfy predicate) (S.fromList ls) of
162 Right r -> case ls of
164 (x : _) -> predicate x && (r == x)
167 (x : _) -> not $ predicate x
169 predicate = (>= mid_value)
173 forAll (chooseInt (1, max_value)) $ \int ->
174 property (case runIdentity $ S.parse P.one (S.fromList [int]) of
181 case runIdentity $ S.parse P.one (S.fromList []) of
185-- Sequence Parsers Tests
186takeBetweenPass :: Property
188 forAll (chooseInt (min_value, max_value)) $ \m ->
189 forAll (chooseInt (m, max_value)) $ \n ->
190 forAll (chooseInt (m, max_value)) $ \list_length ->
191 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
193 case runIdentity $ S.parse (P.takeBetween m n FL.toList)
196 let lpl = Prelude.length parsed_list
197 in checkListEqual parsed_list
198 $ Prelude.take lpl ls
199 Left _ -> property False
201takeBetween :: Property
203 forAll (chooseInt (min_value, max_value)) $ \m ->
204 forAll (chooseInt (min_value, max_value)) $ \n ->
205 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
211 let inputLen = Prelude.length ls
213 let p = P.takeBetween m n FL.toList
214 case runIdentity $ S.parse p (S.fromList ls) of
216 let parsedLen = Prelude.length xs
217 in if inputLen >= m && parsedLen >= m && parsedLen <= n
218 then checkListEqual xs $ Prelude.take parsedLen ls
221 property ((m >= 0 && n >= 0 && m > n) || inputLen < m)
225 forAll (chooseInt (min_value, max_value)) $ \n ->
226 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
227 case runIdentity $ S.parse (P.fromFold $ FL.take n FL.toList) (S.fromList ls) of
228 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
229 Left _ -> property False
231takeEQPass :: Property
233 forAll (chooseInt (min_value, max_value)) $ \n ->
234 forAll (chooseInt (n, max_value)) $ \list_length ->
235 forAll (vectorOf list_length
236 (chooseInt (min_value, max_value))) $ \ls ->
237 case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
239 checkListEqual parsed_list (Prelude.take n ls)
240 Left _ -> property False
244 forAll (chooseInt (min_value, max_value)) $ \n ->
245 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
247 list_length = Prelude.length ls
249 case runIdentity $ S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
252 then checkListEqual parsed_list (Prelude.take n ls)
254 Left _ -> property (n > list_length)
256takeGEPass :: Property
258 forAll (chooseInt (min_value, max_value)) $ \n ->
259 forAll (chooseInt (n, max_value)) $ \list_length ->
260 forAll (vectorOf list_length (chooseInt (min_value, max_value)))
262 case runIdentity $ S.parse (P.takeGE n FL.toList) (S.fromList ls) of
263 Right parsed_list -> checkListEqual parsed_list ls
264 Left _ -> property False
268 forAll (chooseInt (min_value, max_value)) $ \n ->
269 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
271 list_length = Prelude.length ls
273 case runIdentity $ S.parse (P.takeGE n FL.toList) (S.fromList ls) of
276 then checkListEqual parsed_list ls
278 Left _ -> property (n > list_length)
282 -> FL.Fold Identity Int [Int]
283 -> P.Parser Int Identity [Int]
285 -> (Int -> [Int] -> [Int])
287nLessThanEqual0 tk ltk =
288 forAll (elements [0, (-1)]) $ \n ->
289 forAll (listOf arbitrary) $ \ls ->
290 case runIdentity $ S.parse (tk n FL.toList) (S.fromList ls) of
291 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
292 Left _ -> property False
294takeProperties :: Spec
296 describe "take combinators when n <= 0/" $ do
297 prop "takeEQ n FL.toList = []" $
298 nLessThanEqual0 P.takeEQ (\_ -> const [])
299 prop "takeGE n FL.toList xs = xs" $
300 nLessThanEqual0 P.takeGE (\_ -> id)
302-- XXX lookAhead can't deal with EOF which in this case means when
303-- n==list_length, this test will fail. So excluding that case for now.
304lookAheadPass :: Property
306 forAll (chooseInt (min_value, max_value)) $ \n ->
308 takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
310 parsed_list_1 <- takeWithoutConsume
311 parsed_list_2 <- takeWithoutConsume
312 return (parsed_list_1, parsed_list_2)
314 forAll (chooseInt (n+1, max_value)) $ \list_length ->
315 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
316 case runIdentity $ S.parse parseTwice (S.fromList ls) of
317 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
318 Left _ -> property $ False
320-- lookAheadFail :: Property
322-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
324-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
326-- parsed_list_1 <- takeWithoutConsume
327-- parsed_list_2 <- takeWithoutConsume
328-- return (parsed_list_1, parsed_list_2)
330-- forAll (chooseInt (min_value, n - 1)) $ \list_length ->
331-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
332-- case S.parse parseTwice (S.fromList ls) of
338 forAll (chooseInt (min_value, max_value)) $ \n ->
340 takeWithoutConsume = P.lookAhead $ P.fromFold $ FL.take n FL.toList
342 parsed_list_1 <- takeWithoutConsume
343 parsed_list_2 <- takeWithoutConsume
344 return (parsed_list_1, parsed_list_2)
346 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
347 case runIdentity $ S.parse parseTwice (S.fromList ls) of
348 Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
349 Left _ -> property ((list_length < n) || (list_length == n && n == 0))
351 list_length = Prelude.length ls
353takeEndBy_ :: Property
355 forAll (listOf (chooseInt (min_value, max_value ))) $ \ls ->
356 case runIdentity $ S.parse (P.takeEndBy_ predicate prsr) (S.fromList ls) of
358 checkListEqual parsed_list (tkwhl ls)
359 Left _ -> property False
362 prsr = P.many (P.satisfy (const True)) FL.toList
363 tkwhl ls = Prelude.takeWhile (not . predicate) ls
365takeEndByOrMax_ :: Property
367 forAll (chooseInt (min_value, max_value)) $ \n ->
368 forAll (listOf (chooseInt (0, 1))) $ \ls ->
369 case runIdentity $ S.parse (P.fromFold $ FL.takeEndBy_ predicate (FL.take n FL.toList)) (S.fromList ls) of
370 Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
371 Left _ -> property False
375takeStartBy :: Property
377 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
380 case runIdentity $ S.parse parser (S.fromList ls1) of
382 if not $ Prelude.null ls1
384 let tls = Prelude.takeWhile (not . predicate) (tail ls1)
385 in checkListEqual parsed_list $
386 if predicate (head ls1)
388 else Prelude.takeWhile (not . predicate) ls1
389 else property $ Prelude.null parsed_list
390 Left _ -> property False
393 parser = P.takeStartBy predicate FL.toList
397 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
398 case runIdentity $ S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
400 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
401 Left _ -> property False
408 ((,) <$> chooseInt (min_value, max_value)
409 <*> listOf (chooseInt (0, 1)))
411 case runIdentity $ S.parse
412 (P.takeP takeNum (P.fromFold FL.toList))
415 checkListEqual parsed_list (Prelude.take takeNum ls)
416 Left _ -> property False
418takeWhile1 :: Property
420 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
421 case runIdentity $ S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
422 Right parsed_list -> case ls of
427 checkListEqual parsed_list
428 $ Prelude.takeWhile predicate ls
433 (x : _) -> property (not $ predicate x)
437takeWhileP :: Property
439 forAll (listOf (chooseInt (0, 1))) $ \ls ->
440 forAll (chooseInt (min_value, max_value)) $ \n ->
445 P.takeWhileP predicate
446 $ P.fromFold (FL.take n FL.toList)
448 takeWhileTillLen maxLen prd list =
449 Prelude.take maxLen $ Prelude.takeWhile prd list
451 case runIdentity $ S.parse prsr (S.fromList ls) of
455 (takeWhileTillLen n predicate ls)
456 Left _ -> property False
462 ((,,) <$> chooseInt (min_value, max_value)
463 <*> chooseInt (min_value, max_value)
464 <*> listOf (chooseInt (0, 1)))
466 case S.parse (P.choice [parser i, parser j]) (S.fromList ls) of
468 checkListEqual parsed_list $ take (min i j) ls
469 Left _ -> property False
473 parser i = P.fromFold (FL.take i FL.toList)
478 forAll (listOf (chooseInt (0, 1)))
480 case runIdentity $ S.parse parser (S.fromList ls) of
481 Right parsed -> checkListEqual parsed (groupByLF ls)
482 Left _ -> property False
487 parser = P.groupBy cmp FL.toList
490 | otherwise = head $ List.groupBy cmp lst
492groupByRolling :: Property
494 forAll (listOf (chooseInt (0, 1)))
496 case runIdentity $ S.parse parser (S.fromList ls) of
497 Right parsed -> checkListEqual parsed (groupByLF Nothing ls)
498 Left _ -> property False
503 parser = P.groupBy cmp FL.toList
505 groupByLF Nothing (x:xs) = x : groupByLF (Just x) xs
506 groupByLF (Just y) (x:xs) =
508 then x : groupByLF (Just x) xs
513 forAll (listOf (elements [' ', 's']))
515 case runIdentity $ S.parse parser (S.fromList ls) of
516 Right parsed -> checkListEqual parsed (words' ls)
517 Left _ -> property False
522 parser = P.many (P.wordBy predicate FL.toList) FL.toList
525 in if wrds == [] && length lst > 0 then [""] else wrds
527parseManyWordQuotedBy :: H.SpecWith ()
528parseManyWordQuotedBy =
529 describe "parseMany wordQuotedBy"
531 $ \c@(kQ, isQ, input, expected) -> do
532 let inpStrm = S.fromList input
541 parser = P.wordWithQuotes kQ tr esc isQ spc FL.toList
542 result <- H.runIO $ S.fold FL.toList $ S.catRights $ S.parseMany parser inpStrm
543 H.it (showCase c) $ result `H.shouldBe` expected
547 showCase (kQ, _, input, expected) =
548 show kQ ++ ", " ++ input ++ " -> " ++ show expected
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[ quick brown] fox"
566 , ["The[ quick brown]", "fox"])
568 , \x -> if x == '[' then Just ']' else Nothing
569 , "The[ qui[ck] brown] \\ f[ ox]"
570 , ["The[ qui[ck] brown]", " f[ ox]"])
572 , \x -> if x == '[' then Just ']' else Nothing
573 , "The[ qui[ck] brown] fox"
574 , ["The qui[ck] brown", "fox"])
579 forAll (listOf (chooseInt (0, 1))) $ \ls ->
580 case runIdentity $ S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
581 Right (result_first, result_second) -> case ls of
582 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
588splitWithFailLeft :: Property
590 property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
594splitWithFailRight :: Property
596 property (case runIdentity $ S.parse (P.splitWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
600splitWithFailBoth :: Property
602 property (case runIdentity $ S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
606-- teeWithPass :: Property
608-- forAll (chooseInt (min_value, max_value)) $ \n ->
609-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
611-- prsr = P.fromFold $ FL.take n FL.toList
613-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
614-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
615-- Left _ -> property False
617-- teeWithFailLeft :: Property
619-- property (case S.parse (P.teeWith (,) (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
623-- teeWithFailRight :: Property
625-- property (case S.parse (P.teeWith (,) (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
629-- teeWithFailBoth :: Property
631-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
636deintercalate :: Property
638 forAll (listOf (chooseAny :: Gen Int)) $ \ls ->
639 case runIdentity $ S.parse p (S.fromList ls) of
640 Right evenOdd -> evenOdd == List.partition even ls
644 p1 = P.takeWhile even FL.toList
645 p2 = P.takeWhile odd FL.toList
647 FL.tee (fmap concat $ FL.catLefts FL.toList)
648 (fmap concat $ FL.catRights FL.toList)
649 p = P.deintercalate p1 p2 partition
652-- shortestPass :: Property
654-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
656-- half_mid_value = mid_value `Prelude.div` 2
657-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
658-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
659-- prsr_shortest = P.shortest prsr_1 prsr_2
661-- case S.parse prsr_shortest (S.fromList ls) of
662-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= half_mid_value) ls)
663-- Left _ -> property False
665-- shortestPassLeft :: Property
667-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
671-- shortestPassRight :: Property
672-- shortestPassRight =
673-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
677-- shortestFailBoth :: Property
681-- (P.shortest (P.die "die") (P.die "die"))
682-- (S.fromList [1 :: Int]) of
686-- longestPass :: Property
688-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
690-- half_mid_value = mid_value `Prelude.div` 2
691-- prsr_1 = P.takeWhile (<= half_mid_value) FL.toList
692-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
693-- prsr_longest = P.longest prsr_1 prsr_2
695-- case S.parse prsr_longest (S.fromList ls) of
696-- Right long_list -> long_list == Prelude.takeWhile (<= mid_value) ls
699-- longestPassLeft :: Property
701-- property (case S.parse (P.shortest (P.die "die") (P.fromPure (1 :: Int))) (S.fromList [1 :: Int]) of
705-- longestPassRight :: Property
707-- property (case S.parse (P.shortest (P.fromPure (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
711-- longestFailBoth :: Property
714-- (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
720 forAll (listOf (chooseInt (0, 1))) $ \ls ->
721 let fldstp conL currL = return $ FL.Partial (conL ++ currL)
722 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
724 flip P.many concatFold
725 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
726 in case runIdentity $ S.parse prsr (S.fromList ls) of
728 checkListEqual res_list (Prelude.filter (== 0) ls)
729 Left _ -> property False
731many_empty :: Property
733 property (case runIdentity $ S.parse (flip P.many FL.toList (P.die "die")) (S.fromList [1 :: Int]) of
734 Right res_list -> checkListEqual res_list ([] :: [Int])
735 Left _ -> property False)
739 forAll (listOf (chooseInt (0, 1))) $ \genLs ->
742 fldstp conL currL = return $ FL.Partial $ conL ++ currL
743 concatFold = FL.Fold fldstp (return (FL.Partial [])) return
745 flip P.some concatFold
746 $ P.fromFold $ FL.takeEndBy_ (== 1) FL.toList
747 in case runIdentity $ S.parse prsr (S.fromList ls) of
748 Right res_list -> res_list == Prelude.filter (== 0) ls
753 property (case runIdentity $ S.parse (P.some (P.die "die") FL.toList) (S.fromList [1 :: Int]) of
757-------------------------------------------------------------------------------
759-------------------------------------------------------------------------------
761applicative :: Property
763 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
764 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
767 <$> P.fromFold (FL.take (length list1) FL.toList)
768 <*> P.fromFold (FL.take (length list2) FL.toList)
770 case runIdentity $ S.parse parser (S.fromList $ list1 ++ list2) of
771 Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
776 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ ins ->
777 let p xs = P.fromFold (FL.take (length xs) FL.toList)
781 (Prelude.sequence $ fmap p ins)
782 (S.fromList $ concat ins)
785 Right ls -> ls == ins
792 (P.satisfy (> 0) <|> return 66)
793 (S.fromList ([]::[Int]))
803 ((P.takeEQ 2 FL.toList) <|> (P.takeEQ 1 FL.toList))
804 (S.fromList ([51]::[Int]))
812 forAll (listOf (chooseAny :: Gen Int)) $ \ list1 ->
813 forAll (listOf (chooseAny :: Gen Int)) $ \ list2 ->
815 olist1 <- P.fromFold (FL.take (length list1) FL.toList)
816 olist2 <- P.fromFold (FL.take (length list2) FL.toList)
817 return (olist1, olist2)
819 s <- S.parse parser (S.fromList $ list1 ++ list2)
822 Right (olist1, olist2) -> olist1 == list1 && olist2 == list2
825-------------------------------------------------------------------------------
827-------------------------------------------------------------------------------
831 forAll (chooseInt (1,100)) $ \len ->
832 forAll (listOf (vectorOf len (chooseAny :: Gen Int))) $ \ ins ->
835 let p = P.fromFold $ FL.take len FL.toList
839 $ S.parseMany p (S.fromList $ concat ins)
840 listEquals (==) outs ins
842-- basic sanity test for parsing from arrays
843parseUnfold :: Property
846 -- ls = input list (stream)
848 -- tlen = parser take size
851 <$> vectorOf len (chooseAny :: Gen Int)
852 <*> chooseInt (1, len)
853 <*> chooseInt (1, len)) $ \(ls, clen, tlen) ->
855 arrays <- S.toList $ S.chunksOf clen (S.fromList ls)
856 let src = Producer.source (Just (Producer.OuterLoop arrays))
857 let parser = P.fromFold (FL.take tlen FL.toList)
860 $ Producer.concat Producer.fromList A.producer
862 Producer.simplify (Producer.parseManyD parser readSrc)
865 $ S.unfoldMany Unfold.fromList
867 $ S.unfold streamParser src
869 listEquals (==) xs ls
871parserSequence :: Property
873 forAll (vectorOf 11 (listOf (chooseAny :: Gen Int))) $ \ins ->
875 let parsers = S.fromList
876 $ fmap (\xs -> P.fromFold $ FL.take (length xs) FL.sum) ins
877 let sequencedParser = P.sequence parsers FL.sum
879 S.parse sequencedParser $ S.concatMap S.fromList (S.fromList ins)
882 Right x -> x == sum (map sum ins)
885-------------------------------------------------------------------------------
886-- Test for a particular case hit during fs events testing
887-------------------------------------------------------------------------------
890evId = [96,238,17,9,0,0,0,0]
893evFlags = [0,4,1,0,0,0,0,0]
896evPathLen = [71,0,0,0,0,0,0,0]
900 [47,85,115,101,114,115,47,118,111,108,47,118,101,109,98,97,47,99,111,109
901 ,112,111,115,101,119,101 ,108,108,45,116,101,99,104,47,69,110,103,47,112
902 ,114,111,106,101,99,116,115,47,115,116,114,101,97,109,108,121,47,115,116
903 ,114,101,97,109,108,121,47,116,109,112,47,122,122
907event = evId ++ evFlags ++ evPathLen ++ evPath
911 , eventFlags :: Word32
912 , eventAbsPath :: A.Array Word8
913 } deriving (Show, Ord, Eq)
915readOneEvent :: P.Parser Word8 IO Event
917 arr <- P.takeEQ 24 (A.writeN 24)
918 let arr1 = A.castUnsafe arr :: A.Array Word64
919 eid = A.getIndexUnsafe 0 arr1
920 eflags = A.getIndexUnsafe 1 arr1
921 pathLen = fromIntegral $ A.getIndexUnsafe 2 arr1
922 -- XXX handle if pathLen is 0
923 path <- P.takeEQ pathLen (A.writeN pathLen)
926 , eventFlags = fromIntegral eflags
927 , eventAbsPath = path
930parseMany2Events :: Property
937 $ S.parseMany readOneEvent
938 $ S.fromList (concat (replicate 2 event))
940 assert (length xs == 2)
941 -- XXX assuming little endian machine
943 { eventId = 152170080
945 , eventAbsPath = A.fromList evPath
947 in listEquals (==) xs (replicate 2 ev)
949manyEqParseMany :: Property
951 forAll (listOf (chooseInt (0, 100))) $ \lst ->
952 forAll (chooseInt (1, 100)) $ \i ->
954 let strm = S.fromList lst
955 r1 <- run $ S.parse (P.many (split i) FL.toList) strm
956 r2 <- run $ S.fold FL.toList $ S.catRights $ S.parseMany (split i) strm
964 split i = P.fromFold (FL.take i FL.toList)
967takeEndBy1 :: Property
969 forAll (listOf (chooseInt (0, 1))) $ \ls ->
970 case runIdentity $ S.parse (P.takeEndBy predicate prsr) (S.fromList ls) of
974 (takeWhileAndFirstFail (not . predicate) ls)
975 Left _ -> property False
977 prsr = P.many (P.satisfy (const True)) FL.toList
981 takeWhileAndFirstFail prd (x : xs) =
983 then x : takeWhileAndFirstFail prd xs
985 takeWhileAndFirstFail _ [] = []
989 => (a -> Bool) -> FL.Fold m a b -> S.Stream m a -> S.Stream m b
990splitWithSuffix predicate f = S.foldMany (FL.takeEndBy predicate f)
992takeEndBy2 :: Property
994 forAll (listOf (chooseInt (0, 1))) $ \ls ->
1003 $ S.parseMany (P.takeEndBy predicate prsr) strm
1007 prsr = P.many (P.satisfy (const True)) FL.toList
1014 then S.fold FL.toList $ S.append strm1 (S.fromList [])
1015 else S.fold FL.toList strm1
1019 strm1 = splitWithSuffix predicate FL.toList strm
1021 case eitherParsedList of
1022 Left _ -> property False
1024 case eitherSplitList of
1025 Left _ -> property False
1026 Right splitList -> checkListEqual parsedList splitList
1028takeEndByEsc :: Property
1030 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1032 msg = "takeEndByEsc: trailing escape"
1036 isEsc x = x `mod` 6 == 0
1038 prsr = P.takeEndByEsc isEsc isSep prsr0
1042 prsr0 = P.many (P.satisfy (const True)) FL.toList
1044 escapeSep maybePrevEsc [] =
1045 case maybePrevEsc of
1047 Just prevEsc -> [prevEsc]
1048 escapeSep maybePrevEsc (x : xs) =
1049 case maybePrevEsc of
1052 then escapeSep (Just x) xs
1056 else x : escapeSep Nothing xs
1058 x : escapeSep Nothing xs
1060 case runIdentity $ S.parse prsr (S.fromList ls) of
1061 Right parsed_list -> checkListEqual parsed_list $ escapeSep Nothing ls
1062 Left err -> property (displayException err == msg)
1064takeFramedByEsc_ :: Property
1066 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1074 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1076 checkPass (x : xs) maybePrevEsc openMinusClose =
1077 case maybePrevEsc of
1080 then checkPass xs (Just x) openMinusClose
1083 then checkPass xs Nothing (openMinusClose + 1)
1087 case openMinusClose of
1094 (openMinusClose - 1)
1096 checkPass xs Nothing openMinusClose
1097 Just _ -> checkPass xs Nothing openMinusClose
1098 checkPass [] _ _ = False
1100 checkPassBeg [] = False
1101 checkPassBeg xxs@(x:_)
1102 | isBegin x = checkPass xxs Nothing (0 :: Int)
1105 escapeFrame begin end escape l =
1107 helper (x : xs) maybePrevEsc openMinusClose =
1108 case maybePrevEsc of
1111 then helper xs (Just x) openMinusClose
1114 then helper xs Nothing (openMinusClose + 1)
1118 if openMinusClose - 1 == 0
1124 (openMinusClose - 1)
1126 x : helper xs Nothing openMinusClose
1128 if escape x || begin x || end x
1129 then x : helper xs Nothing openMinusClose
1131 prevEsc : x : helper xs Nothing openMinusClose
1132 helper [] _ _ = error "Cannot Reach Here"
1134 helper l Nothing (0 :: Int)
1136 case runIdentity $ S.parse prsr (S.fromList ls) of
1137 Right parsed_list ->
1139 then checkListEqual parsed_list $
1140 escapeFrame isBegin isEnd isEsc ls
1147takeFramedByEsc_Pass :: Property
1148takeFramedByEsc_Pass =
1149 forAll (listOf (chooseInt (min_value, max_value))) $ \list ->
1151 ls = (0 : list) ++ (Prelude.replicate (Prelude.length list + 1) 1)
1159 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1161 escapeFrame begin end escape l =
1163 helper (x : xs) maybePrevEsc openMinusClose =
1164 case maybePrevEsc of
1167 then helper xs (Just x) openMinusClose
1171 if openMinusClose == 0
1172 then helper xs Nothing (openMinusClose + 1)
1173 else x : helper xs Nothing (openMinusClose + 1)
1177 if openMinusClose - 1 == 0
1184 (openMinusClose - 1)
1186 x : helper xs Nothing openMinusClose
1188 x : helper xs Nothing openMinusClose
1189 helper [] _ _ = error "Cannot Reach Here"
1191 helper l Nothing (0 :: Int)
1193 case runIdentity $ S.parse prsr (S.fromList ls) of
1194 Right parsed_list -> checkListEqual parsed_list $ escapeFrame isBegin isEnd isEsc ls
1197takeFramedByEsc_Fail1 :: Property
1198takeFramedByEsc_Fail1 =
1200 msg = "takeFramedByEsc_: missing frame end"
1208 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1212 case runIdentity $ S.parse prsr (S.fromList ls) of
1213 Right _ -> property False
1214 Left err -> property (displayException err == msg)
1216takeFramedByEsc_Fail2 :: Property
1217takeFramedByEsc_Fail2 =
1219 msg = "takeFramedByEsc_: missing frame start"
1227 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1231 case runIdentity $ S.parse prsr (S.fromList ls) of
1232 Right _ -> property False
1233 Left err -> property (displayException err == msg)
1235takeFramedByEsc_Fail3 :: Property
1236takeFramedByEsc_Fail3 =
1238 msg = "takeFramedByEsc_: missing frame end"
1246 prsr = P.takeFramedByEsc_ isEsc isBegin isEnd FL.toList
1250 case runIdentity $ S.parse prsr (S.fromList ls) of
1251 Right _ -> property False
1252 Left err -> property $ (displayException err == msg)
1254takeStartBy_ :: Property
1256 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
1258 msg = "takeFramedByGeneric: empty token"
1260 case runIdentity $ S.parse parser (S.fromList ls1) of
1261 Right parsed_list ->
1262 if not $ Prelude.null ls1
1264 let tls = Prelude.takeWhile (not . predicate) (tail ls1)
1265 in checkListEqual parsed_list $
1266 if predicate (head ls1)
1268 else Prelude.takeWhile (not . predicate) ls1
1269 else property $ Prelude.null parsed_list
1270 Left err -> property (displayException err == msg)
1273 parser = P.takeStartBy_ predicate FL.toList
1274-------------------------------------------------------------------------------
1276-------------------------------------------------------------------------------
1279moduleName = "Data.Parser"
1285 modifyMaxSuccess (const maxTestCount) $ do
1286 describe moduleName $ do
1288 describe "Instances" $ do
1289 prop "applicative" applicative
1290 prop "Alternative: end of input 1" altEOF1
1291 prop "Alternative: end of input 2" altEOF2
1293 prop "sequence" sequence
1294 describe "Stream parsing" $ do
1295 prop "parseMany" parseMany
1296 prop "parseMany2Events" parseMany2Events
1297 prop "parseUnfold" parseUnfold
1298 prop "parserSequence" parserSequence
1300 describe "test for accumulator" $ do
1301 prop "P.fromFold FL.sum = FL.sum" fromFold
1302 prop "fromPure value provided" fromPure
1303 prop "fromPure monadic value provided" fromEffect
1304 prop "fail err = Left (SomeException (ParseError err))" parserFail
1305 prop "always fail" die
1306 prop "always fail but monadic" dieM
1308 describe "test for element parser" $ do
1309 prop "peek = head with list length > 0" peekPass
1310 prop "peek fail on []" peekFail
1311 prop "eof pass on []" eofPass
1312 prop "eof fail on non-empty list" eofFail
1313 prop "first element exists and >= mid_value" satisfyPass
1314 prop "one pass on [Int]" onePass
1315 prop "one fail on []" one
1316 prop "check first element exists and satisfies predicate" satisfy
1318 describe "test for sequence parser" $ do
1319 prop "P.takeBetween = Prelude.take when len >= m and len <= n"
1321 prop ("P.takeBetween = Prelude.take when len >= m and len <= n and fail"
1322 ++ "otherwise fail") Main.takeBetween
1323 prop "P.take = Prelude.take" Main.take
1324 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
1325 prop "P.takeEQ = Prelude.take when len >= n and fail otherwise"
1327 prop "P.takeGE n ls = ls when len >= n" takeGEPass
1328 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
1329 prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
1330 -- prop "Fail when stream length exceeded" lookAheadFail
1331 prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
1332 prop ("P.takeStartBy pred = head : Prelude.takeWhile (not . pred)"
1333 ++ " tail") takeStartBy
1334 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
1335 prop ("P.takeWhile1 = Prelude.takeWhile if taken something,"
1336 ++ " else check why failed") takeWhile1
1337 prop "takeWhileP prd P.take = takeWhileMaxLen prd" takeWhileP
1338 prop ("P.takeP = Prelude.take") takeP
1339 prop "P.groupBy = Prelude.head . Prelude.groupBy" groupBy
1340 prop "groupByRolling" groupByRolling
1341 prop "many (P.wordBy ' ') = words'" wordBy
1342 parseManyWordQuotedBy
1343 -- prop "choice" choice
1344 prop "parse 0, then 1, else fail" splitWith
1345 prop "fail due to die as left parser" splitWithFailLeft
1346 prop "fail due to die as right parser" splitWithFailRight
1347 prop "fail due to die as both parsers" splitWithFailBoth
1348 -- prop "" teeWithPass
1349 -- prop "" teeWithFailLeft
1350 -- prop "" teeWithFailRight
1351 -- prop "" teeWithFailBoth
1352 -- prop "deintercalate" deintercalate
1353 -- prop "" shortestPass
1354 -- prop "" shortestFailLeft
1355 -- prop "" shortestFailRight
1356 -- prop "" shortestFailBoth
1357 prop ("P.many concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1358 ++ "Prelude.filter (== 0)") many
1359 prop "[] due to parser being die" many_empty
1360 prop ("P.some concatFold $ P.takeEndBy_ (== 1) FL.toList ="
1361 ++ "Prelude.filter (== 0)") some
1362 prop "fail due to parser being die" someFail
1363 prop "P.many == S.parseMany" manyEqParseMany
1365 prop "takeEndBy_" takeEndBy_
1366 prop "takeEndByOrMax_" takeEndByOrMax_
1367 prop "takeEndBy1" takeEndBy1
1368 prop "takeEndBy2" takeEndBy2
1369 prop "takeEndByEsc" takeEndByEsc
1370 prop "takeFramedByEsc_" takeFramedByEsc_
1371 prop "takeFramedByEsc_Pass" takeFramedByEsc_Pass
1372 prop "takeFramedByEsc_Fail1" takeFramedByEsc_Fail1
1373 prop "takeFramedByEsc_Fail2" takeFramedByEsc_Fail2
1374 prop "takeFramedByEsc_Fail3" takeFramedByEsc_Fail3
1375 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