1module Main (main) where
3import qualified Streamly.Internal.Data.Parser as P
4import qualified Streamly.Internal.Prelude as S
5import qualified Streamly.Internal.Data.Fold as FL
7-- import Data.List (partition)
9import Test.Hspec(Spec, hspec, describe)
10import qualified Test.Hspec as H
11import Test.Hspec.QuickCheck
12import Test.QuickCheck (arbitrary, forAll, choose, elements, Property,
13 property, listOf, vectorOf, counterexample, Gen)
15import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
16import Control.Exception (SomeException(..), displayException)
17import Control.Monad.IO.Class (MonadIO(..))
18import Control.Monad (when)
19import Data.List ((\\))
36listEquals :: (Show a, Eq a, MonadIO m)
37 => ([a] -> [a] -> Bool) -> [a] -> [a] -> PropertyM m ()
38listEquals eq parsed_list list = do
39 when (not $ parsed_list `eq` list) $ liftIO $ putStrLn $
40 "parsed list " ++ show parsed_list
41 ++ "\nlist " ++ show list
42 ++ "\nparsed list \\\\ list " ++ show (parsed_list \\ list)
43 ++ "\nlist \\\\ parsed list " ++ show (list \\ parsed_list)
44 when (not $ parsed_list `eq` list) $
47 "parsed list " ++ show parsed_list
48 ++ "\nlist " ++ show list
49 ++ "\nparsed list \\\\ list " ++ show (parsed_list \\ list)
50 ++ "\nlist \\\\ parsed list " ++ show (list \\ parsed_list)
52 assert (parsed_list `eq` list)
54checkListEqual :: (Show a, Eq a) => [a] -> [a] -> Property
55checkListEqual ls_1 ls_2 = monadicIO (listEquals (==) ls_1 ls_2)
57chooseInt :: (Int, Int) -> Gen Int
64 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
65 case (==) <$> (S.parse (P.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
66 Right is_equal -> is_equal
71 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
72 case S.parse (P.any (> mid_value)) (S.fromList ls) of
73 Right r -> r == (Prelude.any (> mid_value) ls)
78 forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
79 case S.parse (P.all (> mid_value)) (S.fromList ls) of
80 Right r -> r == (Prelude.all (> mid_value) ls)
85 forAll (chooseInt (min_value, max_value)) $ \x ->
86 case S.parse (P.yield x) (S.fromList [1 :: Int]) of
92 forAll (chooseInt (min_value, max_value)) $ \x ->
93 case S.parse (P.yieldM $ return x) (S.fromList [1 :: Int]) of
100 case S.parse (P.die "die test") (S.fromList [0 :: Int]) of
107 case S.parse (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
111parserFail :: Property
114 case S.parse (fail err) (S.fromList [0 :: Int]) of
116 Left (SomeException e) -> err == displayException e
118 err = "Testing MonadFail.fail."
120-- Element Parser Tests
124 forAll (chooseInt (1, max_length)) $ \list_length ->
125 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
126 case S.parse P.peek (S.fromList ls) of
127 Right head_value -> case ls of
128 head_ls : _ -> head_value == head_ls
134 property (case S.parse P.peek (S.fromList []) of
140 property (case S.parse P.eof (S.fromList []) of
146 forAll (chooseInt (1, max_length)) $ \list_length ->
147 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
148 case S.parse P.eof (S.fromList ls) of
152satisfyPass :: Property
154 forAll (chooseInt (mid_value, max_value)) $ \first_element ->
155 forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
157 ls = first_element : ls_tail
158 predicate = (>= mid_value)
160 case S.parse (P.satisfy predicate) (S.fromList ls) of
161 Right r -> r == first_element
166 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
167 case S.parse (P.satisfy predicate) (S.fromList ls) of
168 Right r -> case ls of
170 (x : _) -> predicate x && (r == x)
173 (x : _) -> not $ predicate x
175 predicate = (>= mid_value)
177-- Sequence Parsers Tests
181 forAll (chooseInt (min_value, max_value)) $ \n ->
182 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
183 case S.parse (P.take n FL.toList) (S.fromList ls) of
184 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
185 Left _ -> property False
187takeEQPass :: Property
189 forAll (chooseInt (min_value, max_value)) $ \n ->
190 forAll (chooseInt (n, max_value)) $ \list_length ->
191 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
192 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
193 Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
194 Left _ -> property False
198 forAll (chooseInt (min_value, max_value)) $ \n ->
199 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
201 list_length = Prelude.length ls
203 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
205 if (n <= list_length) then
206 checkListEqual parsed_list (Prelude.take n ls)
209 Left _ -> property (n > list_length)
211takeGEPass :: Property
213 forAll (chooseInt (min_value, max_value)) $ \n ->
214 forAll (chooseInt (n, max_value)) $ \list_length ->
215 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
216 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
217 Right parsed_list -> checkListEqual parsed_list ls
218 Left _ -> property False
222 forAll (chooseInt (min_value, max_value)) $ \n ->
223 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
225 list_length = Prelude.length ls
227 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
229 if (n <= list_length) then
230 checkListEqual parsed_list ls
233 Left _ -> property (n > list_length)
237 -> FL.Fold (Either SomeException) Int [Int]
238 -> P.Parser (Either SomeException) Int [Int]
240 -> (Int -> [Int] -> [Int])
242nLessThanEqual0 tk ltk =
243 forAll (elements [0, (-1)]) $ \n ->
244 forAll (listOf arbitrary) $ \ls ->
245 case S.parse (tk n FL.toList) (S.fromList ls) of
246 Right parsed_list -> checkListEqual parsed_list (ltk n ls)
247 Left _ -> property False
249takeProperties :: Spec
251 describe "take combinators when n <= 0/" $ do
252 prop "take n FL.toList = []" $
253 nLessThanEqual0 P.take (\_ -> const [])
254 prop "takeEQ n FL.toList = []" $
255 nLessThanEqual0 P.takeEQ (\_ -> const [])
256 prop "takeGE n FL.toList xs = xs" $
257 nLessThanEqual0 P.takeGE (\_ -> id)
259-- lookAheadPass :: Property
261-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
263-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
265-- parsed_list_1 <- takeWithoutConsume
266-- parsed_list_2 <- takeWithoutConsume
267-- return (parsed_list_1, parsed_list_2)
269-- forAll (chooseInt (n, max_value)) $ \list_length ->
270-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
271-- case S.parse parseTwice (S.fromList ls) of
272-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
273-- Left _ -> property $ False
275-- lookAheadFail :: Property
277-- forAll (chooseInt (min_value + 1, max_value)) $ \n ->
279-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
281-- parsed_list_1 <- takeWithoutConsume
282-- parsed_list_2 <- takeWithoutConsume
283-- return (parsed_list_1, parsed_list_2)
285-- forAll (chooseInt (min_value, n - 1)) $ \list_length ->
286-- forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
287-- case S.parse parseTwice (S.fromList ls) of
291-- lookAhead :: Property
293-- forAll (chooseInt (min_value, max_value)) $ \n ->
295-- takeWithoutConsume = P.lookAhead $ P.take n FL.toList
297-- parsed_list_1 <- takeWithoutConsume
298-- parsed_list_2 <- takeWithoutConsume
299-- return (parsed_list_1, parsed_list_2)
301-- forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
302-- case S.parse parseTwice (S.fromList ls) of
303-- Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
304-- Left _ -> property ((list_length < n) || (list_length == n && n == 0))
306-- list_length = Prelude.length ls
310 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
311 case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
312 Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
313 Left _ -> property False
317takeWhile1 :: Property
319 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
320 case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
321 Right parsed_list -> case ls of
325 checkListEqual parsed_list (Prelude.takeWhile predicate ls)
330 (x : _) -> property (not $ predicate x)
334sliceSepBy :: Property
336 forAll (listOf (chooseInt (0, 1))) $ \ls ->
337 case S.parse (P.sliceSepBy predicate FL.toList) (S.fromList ls) of
338 Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile (not . predicate) ls)
339 Left _ -> property False
343-- sliceSepByMax :: Property
345-- forAll (chooseInt (min_value, max_value)) $ \n ->
346-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
347-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
348-- Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
349-- Left _ -> property False
353-- splitWithPass :: Property
355-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
356-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
357-- Right (result_first, result_second) -> case ls of
358-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
360-- Left _ -> case ls of
364-- splitWithFailLeft :: Property
365-- splitWithFailLeft =
366-- property (case S.parse (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
370-- splitWithFailRight :: Property
371-- splitWithFailRight =
372-- property (case S.parse (P.splitWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
376-- splitWithFailBoth :: Property
377-- splitWithFailBoth =
378-- property (case S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
382-- teeWithPass :: Property
384-- forAll (chooseInt (0, 10000)) $ \n ->
385-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
387-- prsr = P.take n FL.toList
389-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
390-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
391-- Left _ -> property False
393-- teeWithFailLeft :: Property
395-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
399-- teeWithFailRight :: Property
401-- property (case S.parse (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
405-- teeWithFailBoth :: Property
407-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
411-- deintercalate :: Property
413-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
414-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
415-- Right parsed_list_tuple -> parsed_list_tuple == (partition (== 0) ls)
419-- prsr_1 = (P.takeWhile (== 0) FL.toList)
420-- prsr_2 = (P.takeWhile (== 1) FL.toList)
421-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
423-- shortestPass :: Property
425-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
427-- prsr_1 = P.takeWhile (<= (mid_value `Prelude.div` 2)) FL.toList
428-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
429-- prsr_shortest = P.shortest prsr_1 prsr_2
431-- case S.parse prsr_shortest (S.fromList ls) of
432-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= 2500) ls)
433-- Left _ -> property False
435-- shortestFailLeft :: Property
437-- property (case S.parse (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
441-- shortestFailRight :: Property
442-- shortestFailRight =
443-- property (case S.parse (P.shortest (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
447-- shortestFailBoth :: Property
449-- property (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
457 modifyMaxSuccess (const maxTestCount) $ do
458 describe "test for accumulator" $ do
459 prop "P.fromFold FL.sum = FL.sum" fromFold
460 prop "P.any = Prelude.any" Main.any
461 prop "P.all = Prelude.all" Main.all
462 prop "yield value provided" yield
463 prop "yield monadic value provided" yieldM
464 prop "fail err = Left (SomeException (ParseError err))" parserFail
465 prop "always fail" die
466 prop "always fail but monadic" dieM
468 describe "test for element parser" $ do
469 prop "peek = head with list length > 0" peekPass
470 prop "peek fail on []" peekFail
471 prop "eof pass on []" eofPass
472 prop "eof fail on non-empty list" eofFail
473 prop "first element exists and >= mid_value" satisfyPass
474 prop "check first element exists and satisfies predicate" satisfy
476 describe "test for sequence parser" $ do
477 prop "P.take = Prelude.take" Main.take
478 prop "P.takeEQ = Prelude.take when len >= n" takeEQPass
479 prop "P.takeEQ = Prelude.take when len >= n and fail otherwise" Main.takeEQ
480 prop "P.takeGE n ls = ls when len >= n" takeGEPass
481 prop "P.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
482 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
483 -- prop "Fail when stream length exceeded" lookAheadFail
484 -- prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
485 prop "P.takeWhile = Prelude.takeWhile" Main.takeWhile
486 prop "P.takeWhile = Prelude.takeWhile if taken something, else check why failed" takeWhile1
487 prop "P.sliceSepBy = Prelude.takeWhile (not . predicate)" sliceSepBy
488 -- prop "test for sliceSepByMax function" sliceSepByMax
489 -- prop "pass test for splitWith function" splitWithPass
490 -- prop "left fail test for splitWith function" splitWithFailLeft
491 -- prop "right fail test for splitWith function" splitWithFailRight
492 -- prop "both fail test for splitWith function" splitWithFailBoth
493 -- prop "pass test for teeWith function" teeWithPass
494 -- prop "left fail test for teeWith function" teeWithFailLeft
495 -- prop "right fail test for teeWith function" teeWithFailRight
496 -- prop "both fail test for teeWith function" teeWithFailBoth
497 -- prop "test for deintercalate function" deintercalate
498 -- prop "pass test for shortest function" shortestPass
499 -- prop "left fail test for shortest function" shortestFailLeft
500 -- prop "right fail test for shortest function" shortestFailRight
501 -- prop "both fail test for shortest function" shortestFailBoth
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