on master help sign in
Add tests for ParserD.
commitb2cbde7ecf50715e0d7acf91eca32684795cd95d authorAhmed Zaheer Dadarkar <ahmed.dadarkar@gmail.com> dateMon 18 May 2020, 18:27 (6 years ago) parent646cb86
2 files changed+294
streamly.cabal+14 test/Streamly/Test/Internal/Data/Parser/ParserD.hs+280
streamly.cabal+14
@@ -565,6 +565,20 @@ test-suite internal-data-parser-test
565565 , transformers >= 0.4 && < 0.6
566566 default-language: Haskell2010
567567
568+test-suite internal-data-parser-parserd-test
569+ import: test-options
570+ type: exitcode-stdio-1.0
571+ main-is: Streamly/Test/Internal/Data/Parser/ParserD.hs
572+ js-sources: jsbits/clock.js
573+ hs-source-dirs: test
574+ build-depends:
575+ streamly
576+ , base >= 4.8 && < 5
577+ , hspec >= 2.0 && < 3
578+ , QuickCheck >= 2.10 && < 2.15
579+ , transformers >= 0.4 && < 0.6
580+ default-language: Haskell2010
581+
568582test-suite data-array-test
569583 import: test-options
570584 type: exitcode-stdio-1.0
test/Streamly/Test/Internal/Data/Parser/ParserD.hsnew file+280
@@ -0,0 +1,280 @@
1+module Main (main) where
2+
3+import qualified Streamly.Internal.Data.Parser.ParserD as D
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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD D.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.parseD D.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.parseD D.eof (S.fromList ls) of
85+ Right _ -> False
86+ Left _ -> True
87+ .&&.
88+ property (case S.parseD D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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 = D.lookAhead $ D.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.parseD 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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.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.parseD (D.splitWith (,) (D.satisfy (== 0)) (D.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.parseD (D.splitWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
207+ Right _ -> False
208+ Left _ -> True)
209+ .&&.
210+ property (case S.parseD (D.splitWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
211+ Right _ -> False
212+ Left _ -> True)
213+ .&&.
214+ property (case S.parseD (D.splitWith (,) (D.die "die") (D.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 = D.take n FL.toList
224+ in
225+ case S.parseD (D.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.parseD (D.teeWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
230+ Right _ -> False
231+ Left _ -> True)
232+ .&&.
233+ property (case S.parseD (D.teeWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
234+ Right _ -> False
235+ Left _ -> True)
236+ .&&.
237+ property (case S.parseD (D.teeWith (,) (D.die "die") (D.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.parseD (D.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 = (D.takeWhile (== 0) FL.toList)
250+-- prsr_2 = (D.takeWhile (== 1) FL.toList)
251+-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
252+
253+main :: IO ()
254+main = hspec $ do
255+ describe "test for accumulator" $ do
256+ prop "test fromFold function" fromFold
257+ prop "test any function" Main.any
258+ prop "test all function" Main.all
259+ prop "test yield function" yield
260+ prop "test yieldM function" yieldM
261+ prop "test die function" die
262+ prop "test dieM function" dieM
263+
264+ describe "test for element parser" $ do
265+ prop "test for peek function" peek
266+ prop "test for eof function" eof
267+ prop "test for satisfy function" satisfy
268+
269+ describe "test for sequence parser" $ do
270+ prop "test for take function" Main.take
271+ prop "test for takeEq function" Main.takeEQ
272+ prop "test for takeGE function" Main.takeGE
273+ prop "test for LookAhead function" lookAhead
274+ prop "test for takeWhile function" Main.takeWhile
275+ prop "test for takeWhile1 function" takeWhile1
276+ prop "test for sliceSepBy function" sliceSepBy
277+ -- prop "test for sliceSepByMax function" sliceSepByMax
278+ prop "test for splitWith function" splitWith
279+ prop "test for teeWith function" teeWith
280+ -- prop "test for deintercalate function" deintercalate