@@ -0,0 +1,305 @@
1+module Main (main) where
2+
3+import qualified Streamly.Internal.Data.Parser as P
4+import qualified Streamly.Internal.Prelude as S
5+import qualified Streamly.Internal.Data.Fold as FL
6+
7+-- import Data.List (partition)
8+
9+import Test.Hspec(hspec, describe)
10+import Test.Hspec.QuickCheck
11+import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, (.&&.))
12+
13+-- Accumulator Tests
14+
15+fromFold :: Property
16+fromFold =
17+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
18+ case (==) <$> (S.parse (P.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
19+ Right is_equal -> is_equal
20+ Left _ -> False
21+
22+any :: Property
23+any =
24+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
25+ case S.parse (P.any (> 5000)) (S.fromList ls) of
26+ Right r -> r == (Prelude.any (> 5000) ls)
27+ Left _ -> False
28+
29+all :: Property
30+all =
31+ forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
32+ case S.parse (P.all (> 5000)) (S.fromList ls) of
33+ Right r -> r == (Prelude.all (> 5000) ls)
34+ Left _ -> False
35+
36+yield :: Property
37+yield =
38+ forAll (chooseInt (0, 10000)) $ \x ->
39+ case S.parse (P.yield x) (S.fromList [1 :: Int]) of
40+ Right r -> r == x
41+ Left _ -> False
42+
43+yieldM :: Property
44+yieldM =
45+ forAll (chooseInt (0, 10000)) $ \x ->
46+ case S.parse (P.yieldM $ return x) (S.fromList [1 :: Int]) of
47+ Right r -> r == x
48+ Left _ -> False
49+
50+die :: Property
51+die =
52+ property $
53+ case S.parse (P.die "die test") (S.fromList [0 :: Int]) of
54+ Right _ -> False
55+ Left _ -> True
56+
57+dieM :: Property
58+dieM =
59+ property $
60+ case S.parse (P.dieM (Right "die test")) (S.fromList [0 :: Int]) of
61+ Right _ -> False
62+ Left _ -> True
63+
64+-- Element Parser Tests
65+
66+peek :: Property
67+peek =
68+ forAll (chooseInt (1, 100)) $ \list_length ->
69+ forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
70+ case S.parse P.peek (S.fromList ls) of
71+ Right head_value -> case ls of
72+ head_ls : _ -> head_value == head_ls
73+ _ -> False
74+ Left _ -> False
75+ .&&.
76+ property (case S.parse P.peek (S.fromList []) of
77+ Right _ -> False
78+ Left _ -> True)
79+
80+eof :: Property
81+eof =
82+ forAll (chooseInt (1, 100)) $ \list_length ->
83+ forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
84+ case S.parse P.eof (S.fromList ls) of
85+ Right _ -> False
86+ Left _ -> True
87+ .&&.
88+ property (case S.parse P.eof (S.fromList []) of
89+ Right _ -> True
90+ Left _ -> False)
91+
92+satisfy :: Property
93+satisfy =
94+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
95+ case S.parse (P.satisfy predicate) (S.fromList ls) of
96+ Right r -> case ls of
97+ [] -> False
98+ (x : _) -> predicate x && (r == x)
99+ Left _ -> case ls of
100+ [] -> True
101+ (x : _) -> not $ predicate x
102+ where
103+ predicate = (>= 5000)
104+
105+-- Sequence Parsers Tests
106+
107+take :: Property
108+take =
109+ forAll (chooseInt (0, 10000)) $ \n ->
110+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
111+ case S.parse (P.take n FL.toList) (S.fromList ls) of
112+ Right parsed_list -> parsed_list == Prelude.take n ls
113+ Left _ -> False
114+
115+takeEQ :: Property
116+takeEQ =
117+ forAll (chooseInt (0, 10000)) $ \n ->
118+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
119+ let
120+ list_length = Prelude.length ls
121+ in
122+ case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
123+ Right parsed_list -> (n <= list_length) && (parsed_list == Prelude.take n ls)
124+ Left _ -> n > list_length
125+
126+takeGE :: Property
127+takeGE =
128+ forAll (chooseInt (0, 10000)) $ \n ->
129+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
130+ let
131+ list_length = Prelude.length ls
132+ in
133+ case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
134+ Right parsed_list -> (n <= list_length) && (parsed_list == ls)
135+ Left _ -> n > list_length
136+
137+lookAhead :: Property
138+lookAhead =
139+ forAll (chooseInt (0, 10000)) $ \n ->
140+ let
141+ takeWithoutConsume = P.lookAhead $ P.take n FL.toList
142+ parseTwice = do
143+ parsed_list_1 <- takeWithoutConsume
144+ parsed_list_2 <- takeWithoutConsume
145+ return (parsed_list_1, parsed_list_2)
146+ in
147+ forAll (listOf (chooseInt (0, 10000))) $ \ls ->
148+ case S.parse parseTwice (S.fromList ls) of
149+ Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
150+ Left _ -> (list_length < n) || (list_length == n && n == 0)
151+ where
152+ list_length = Prelude.length ls
153+
154+takeWhile :: Property
155+takeWhile =
156+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
157+ case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
158+ Right parsed_list -> parsed_list == Prelude.takeWhile predicate ls
159+ Left _ -> False
160+ where
161+ predicate = (== 0)
162+
163+takeWhile1 :: Property
164+takeWhile1 =
165+ forAll (listOf (chooseInt (0, 1))) $ \ ls ->
166+ case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
167+ Right parsed_list -> case ls of
168+ [] -> False
169+ (x : _) -> predicate x && (parsed_list == Prelude.takeWhile predicate ls)
170+ Left _ -> case ls of
171+ [] -> True
172+ (x : _) -> not $ predicate x
173+ where
174+ predicate = (== 0)
175+
176+sliceSepBy :: Property
177+sliceSepBy =
178+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
179+ case S.parse (P.sliceSepBy predicate FL.toList) (S.fromList ls) of
180+ Right parsed_list -> parsed_list == Prelude.takeWhile (not . predicate) ls
181+ Left _ -> False
182+ where
183+ predicate = (== 1)
184+
185+-- sliceSepByMax :: Property
186+-- sliceSepByMax =
187+-- forAll (chooseInt (0, 10000)) $ \n ->
188+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
189+-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
190+-- Right parsed_list -> parsed_list == Prelude.take n (Prelude.takeWhile (not . predicate) ls)
191+-- Left _ -> False
192+-- where
193+-- predicate = (== 1)
194+
195+-- splitWith :: Property
196+-- splitWith =
197+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
198+-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
199+-- Right (result_first, result_second) -> case ls of
200+-- 0 : 1 : _ -> (result_first == 0) && (result_second == 1)
201+-- _ -> False
202+-- Left _ -> case ls of
203+-- 0 : 1 : _ -> False
204+-- _ -> True
205+-- .&&.
206+-- property (case S.parse (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
207+-- Right _ -> False
208+-- Left _ -> True)
209+-- .&&.
210+-- property (case S.parse (P.splitWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
211+-- Right _ -> False
212+-- Left _ -> True)
213+-- .&&.
214+-- property (case S.parse (P.splitWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
215+-- Right _ -> False
216+-- Left _ -> True)
217+
218+-- teeWith :: Property
219+-- teeWith =
220+-- forAll (chooseInt (0, 10000)) $ \n ->
221+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
222+-- let
223+-- prsr = P.take n FL.toList
224+-- in
225+-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
226+-- Right (ls_1, ls_2) -> (Prelude.take n ls == ls_1) && (ls_1 == ls_2)
227+-- Left _ -> False
228+-- .&&.
229+-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
230+-- Right _ -> False
231+-- Left _ -> True)
232+-- .&&.
233+-- property (case S.parse (P.teeWith (,) (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
234+-- Right _ -> False
235+-- Left _ -> True)
236+-- .&&.
237+-- property (case S.parse (P.teeWith (,) (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
238+-- Right _ -> False
239+-- Left _ -> True)
240+
241+-- deintercalate :: Property
242+-- deintercalate =
243+-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
244+-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
245+-- Right parsed_list_tuple -> parsed_list_tuple == partition (== 0) ls
246+-- Left _ -> False
247+
248+-- where
249+-- prsr_1 = (P.takeWhile (== 0) FL.toList)
250+-- prsr_2 = (P.takeWhile (== 1) FL.toList)
251+-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
252+
253+-- shortest :: Property
254+-- shortest =
255+-- forAll (listOf (chooseInt(0, 10000))) $ \ls ->
256+-- let
257+-- prsr_1 = P.takeWhile (<= 2500) FL.toList
258+-- prsr_2 = P.takeWhile (<= 5000) FL.toList
259+-- prsr_shortest = P.shortest prsr_1 prsr_2
260+-- in
261+-- case S.parse prsr_shortest (S.fromList ls) of
262+-- Right short_list -> short_list == Prelude.takeWhile (<= 2500) ls
263+-- Left _ -> False
264+-- .&&.
265+-- property (case S.parse (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
266+-- Right r -> r == 1
267+-- Left _ -> False)
268+-- .&&.
269+-- property (case S.parse (P.shortest (P.yield (1 :: Int)) (P.die "die")) (S.fromList [1 :: Int]) of
270+-- Right r -> r == 1
271+-- Left _ -> False)
272+-- .&&.
273+-- property (case S.parse (P.shortest (P.die "die") (P.die "die")) (S.fromList [1 :: Int]) of
274+-- Right _ -> False
275+-- Left _ -> True)
276+
277+main :: IO ()
278+main = hspec $ do
279+ describe "test for accumulator" $ do
280+ prop "test fromFold function" fromFold
281+ prop "test any function" Main.any
282+ prop "test all function" Main.all
283+ prop "test yield function" yield
284+ prop "test yieldM function" yieldM
285+ prop "test die function" die
286+ prop "test dieM function" dieM
287+
288+ describe "test for element parser" $ do
289+ prop "test for peek function" peek
290+ prop "test for eof function" eof
291+ prop "test for satisfy function" satisfy
292+
293+ describe "test for sequence parser" $ do
294+ prop "test for take function" Main.take
295+ prop "test for takeEq function" Main.takeEQ
296+ prop "test for takeGE function" Main.takeGE
297+ prop "test for LookAhead function" lookAhead
298+ prop "test for takeWhile function" Main.takeWhile
299+ prop "test for takeWhile1 function" takeWhile1
300+ prop "test for sliceSepBy function" sliceSepBy
301+ -- prop "test for sliceSepByMax function" sliceSepByMax
302+ -- prop "test for splitWith function" splitWith
303+ -- prop "test for teeWith function" teeWith
304+ -- prop "test for deintercalate function" deintercalate
305+ -- prop "test for shortest function" shortest