on master help sign in
Split test into smaller property tests.
88c2aedAhmed Zaheer Dadarkar
test/Streamly/Test/Internal/Data/Parser/ParserD.hs+291−119
@@ -4,43 +4,86 @@ import qualified Streamly.Internal.Data.Parser.ParserD as D
4import qualified Streamly.Internal.Prelude as S
4import qualified Streamly.Internal.Prelude as S
5import qualified Streamly.Internal.Data.Fold as FL
5import qualified Streamly.Internal.Data.Fold as FL
6
6
7+-- import Data.List (partition)
8+
7import Test.Hspec(hspec, describe)
9import Test.Hspec(hspec, describe)
8import Test.Hspec.QuickCheck
10import Test.Hspec.QuickCheck
9−import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, (.&&.))
11+import Test.QuickCheck (forAll, choose, Property, property, listOf, vectorOf, counterexample, (.&&.), Gen)
12+
13+import Test.QuickCheck.Monadic (monadicIO, PropertyM, assert, monitor)
14+import Control.Monad.IO.Class (MonadIO(..))
15+import Control.Monad (when)
16+import Data.List ((\\))
17+
18+min_value :: Int
19+min_value = 0
20+
21+mid_value :: Int
22+mid_value = 5000
23+
24+max_value :: Int
25+max_value = 10000
26+
27+max_length :: Int
28+max_length = 1000
29+
30+listEquals :: (Show a, Eq a, MonadIO m)
31+ => ([a] -> [a] -> Bool) -> [a] -> [a] -> PropertyM m ()
32+listEquals eq parsed_list list = do
33+ when (not $ parsed_list `eq` list) $ liftIO $ putStrLn $
34+ "parsed list " ++ show parsed_list
35+ ++ "\nlist " ++ show list
36+ ++ "\nparsed list \\\\ list " ++ show (parsed_list \\ list)
37+ ++ "\nlist \\\\ parsed list " ++ show (list \\ parsed_list)
38+ when (not $ parsed_list `eq` list) $
39+ monitor
40+ (counterexample $
41+ "parsed list " ++ show parsed_list
42+ ++ "\nlist " ++ show list
43+ ++ "\nparsed list \\\\ list " ++ show (parsed_list \\ list)
44+ ++ "\nlist \\\\ parsed list " ++ show (list \\ parsed_list)
45+ )
46+ assert (parsed_list `eq` list)
47+
48+checkListEqual :: (Show a, Eq a) => [a] -> [a] -> Property
49+checkListEqual ls_1 ls_2 = monadicIO (listEquals (==) ls_1 ls_2)
50+
51+chooseInt :: (Int, Int) -> Gen Int
52+chooseInt = choose
10
53
11-- Accumulator Tests
54-- Accumulator Tests
12
55
13fromFold :: Property
56fromFold :: Property
14fromFold =
57fromFold =
15− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
58+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
16 case (==) <$> (S.parseD (D.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
59 case (==) <$> (S.parseD (D.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
17 Right is_equal -> is_equal
60 Right is_equal -> is_equal
18 Left _ -> False
61 Left _ -> False
19
62
20any :: Property
63any :: Property
21any =
64any =
22− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
65+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
23− case S.parseD (D.any (> 5000)) (S.fromList ls) of
66+ case S.parseD (D.any (> mid_value)) (S.fromList ls) of
24− Right r -> r == (Prelude.any (> 5000) ls)
67+ Right r -> r == (Prelude.any (> mid_value) ls)
25 Left _ -> False
68 Left _ -> False
26
69
27all :: Property
70all :: Property
28all =
71all =
29− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
72+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
30− case S.parseD (D.all (> 5000)) (S.fromList ls) of
73+ case S.parseD (D.all (> mid_value)) (S.fromList ls) of
31− Right r -> r == (Prelude.all (> 5000) ls)
74+ Right r -> r == (Prelude.all (> mid_value) ls)
32 Left _ -> False
75 Left _ -> False
33
76
34yield :: Property
77yield :: Property
35yield =
78yield =
36− forAll (chooseInt (0, 10000)) $ \x ->
79+ forAll (chooseInt (min_value, max_value)) $ \x ->
37 case S.parseD (D.yield x) (S.fromList [1 :: Int]) of
80 case S.parseD (D.yield x) (S.fromList [1 :: Int]) of
38 Right r -> r == x
81 Right r -> r == x
39 Left _ -> False
82 Left _ -> False
40
83
41yieldM :: Property
84yieldM :: Property
42yieldM =
85yieldM =
43− forAll (chooseInt (0, 10000)) $ \x ->
86+ forAll (chooseInt (min_value, max_value)) $ \x ->
44 case S.parseD (D.yieldM $ return x) (S.fromList [1 :: Int]) of
87 case S.parseD (D.yieldM $ return x) (S.fromList [1 :: Int]) of
45 Right r -> r == x
88 Right r -> r == x
46 Left _ -> False
89 Left _ -> False
@@ -61,35 +104,51 @@ dieM =
61
104
62-- Element Parser Tests
105-- Element Parser Tests
63
106
64−peek :: Property
107+peekPass :: Property
65−peek =
108+peekPass =
66− forAll (chooseInt (1, 100)) $ \list_length ->
109+ forAll (chooseInt (1, max_length)) $ \list_length ->
67− forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
110+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
68 case S.parseD D.peek (S.fromList ls) of
111 case S.parseD D.peek (S.fromList ls) of
69 Right head_value -> case ls of
112 Right head_value -> case ls of
70 head_ls : _ -> head_value == head_ls
113 head_ls : _ -> head_value == head_ls
71 _ -> False
114 _ -> False
72 Left _ -> False
115 Left _ -> False
73− .&&.
116+
117+peekFail :: Property
118+peekFail =
74 property (case S.parseD D.peek (S.fromList []) of
119 property (case S.parseD D.peek (S.fromList []) of
75 Right _ -> False
120 Right _ -> False
76 Left _ -> True)
121 Left _ -> True)
77
122
78−eof :: Property
123+eofPass :: Property
79−eof =
124+eofPass =
80− forAll (chooseInt (1, 100)) $ \list_length ->
81− forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
82− case S.parseD D.eof (S.fromList ls) of
83− Right _ -> False
84− Left _ -> True
85− .&&.
86 property (case S.parseD D.eof (S.fromList []) of
125 property (case S.parseD D.eof (S.fromList []) of
87 Right _ -> True
126 Right _ -> True
88 Left _ -> False)
127 Left _ -> False)
89
128
129+eofFail :: Property
130+eofFail =
131+ forAll (chooseInt (1, max_length)) $ \list_length ->
132+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
133+ case S.parseD D.eof (S.fromList ls) of
134+ Right _ -> False
135+ Left _ -> True
136+
137+satisfyPass :: Property
138+satisfyPass =
139+ forAll (chooseInt (mid_value, max_value)) $ \first_element ->
140+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls_tail ->
141+ let
142+ ls = first_element : ls_tail
143+ predicate = (>= mid_value)
144+ in
145+ case S.parseD (D.satisfy predicate) (S.fromList ls) of
146+ Right r -> r == first_element
147+ Left _ -> False
148+
90satisfy :: Property
149satisfy :: Property
91satisfy =
150satisfy =
92− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
151+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
93 case S.parseD (D.satisfy predicate) (S.fromList ls) of
152 case S.parseD (D.satisfy predicate) (S.fromList ls) of
94 Right r -> case ls of
153 Right r -> case ls of
95 [] -> False
154 [] -> False
@@ -98,43 +157,101 @@ satisfy =
98 [] -> True
157 [] -> True
99 (x : _) -> not $ predicate x
158 (x : _) -> not $ predicate x
100 where
159 where
101− predicate = (>= 5000)
160+ predicate = (>= mid_value)
102
161
103-- Sequence Parsers Tests
162-- Sequence Parsers Tests
104
163
105take :: Property
164take :: Property
106take =
165take =
107− forAll (chooseInt (0, 10000)) $ \n ->
166+ forAll (chooseInt (min_value, max_value)) $ \n ->
108− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
167+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
109 case S.parseD (D.take n FL.toList) (S.fromList ls) of
168 case S.parseD (D.take n FL.toList) (S.fromList ls) of
169+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
170+ Left _ -> property False
171+
172+takeEQPass :: Property
173+takeEQPass =
174+ forAll (chooseInt (min_value, max_value)) $ \n ->
175+ forAll (chooseInt (n, max_value)) $ \list_length ->
176+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
177+ case S.parseD (D.takeEQ n FL.toList) (S.fromList ls) of
110− Right parsed_list -> parsed_list == Prelude.take n ls
178+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
111− Left _ -> False
179+ Left _ -> property False
112
180
113takeEQ :: Property
181takeEQ :: Property
114takeEQ =
182takeEQ =
115− forAll (chooseInt (0, 10000)) $ \n ->
183+ forAll (chooseInt (min_value, max_value)) $ \n ->
116− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
184+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
117 let
185 let
118 list_length = Prelude.length ls
186 list_length = Prelude.length ls
119 in
187 in
120 case S.parseD (D.takeEQ n FL.toList) (S.fromList ls) of
188 case S.parseD (D.takeEQ n FL.toList) (S.fromList ls) of
189+ Right parsed_list ->
190+ if (n <= list_length) then
121− Right parsed_list -> (n <= list_length) && (parsed_list == Prelude.take n ls)
191+ checkListEqual parsed_list (Prelude.take n ls)
192+ else
193+ property False
122− Left _ -> n > list_length
194+ Left _ -> property (n > list_length)
195+
196+takeGEPass :: Property
197+takeGEPass =
198+ forAll (chooseInt (min_value, max_value)) $ \n ->
199+ forAll (chooseInt (n, max_value)) $ \list_length ->
200+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
201+ case S.parseD (D.takeGE n FL.toList) (S.fromList ls) of
202+ Right parsed_list -> checkListEqual parsed_list ls
203+ Left _ -> property False
123
204
124takeGE :: Property
205takeGE :: Property
125takeGE =
206takeGE =
126− forAll (chooseInt (0, 10000)) $ \n ->
207+ forAll (chooseInt (min_value, max_value)) $ \n ->
127− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
208+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
128 let
209 let
129 list_length = Prelude.length ls
210 list_length = Prelude.length ls
130 in
211 in
131 case S.parseD (D.takeGE n FL.toList) (S.fromList ls) of
212 case S.parseD (D.takeGE n FL.toList) (S.fromList ls) of
132− Right parsed_list -> (n <= list_length) && (parsed_list == ls)
213+ Right parsed_list ->
214+ if (n <= list_length) then
215+ checkListEqual parsed_list ls
216+ else
217+ property False
218+ Left _ -> property (n > list_length)
219+
220+lookAheadPass :: Property
221+lookAheadPass =
222+ forAll (chooseInt (min_value + 1, max_value)) $ \n ->
223+ let
224+ takeWithoutConsume = D.lookAhead $ D.take n FL.toList
225+ parseTwice = do
226+ parsed_list_1 <- takeWithoutConsume
227+ parsed_list_2 <- takeWithoutConsume
228+ return (parsed_list_1, parsed_list_2)
229+ in
230+ forAll (chooseInt (n, max_value)) $ \list_length ->
231+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
232+ case S.parseD parseTwice (S.fromList ls) of
233+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
234+ Left _ -> property $ False
235+
236+lookAheadFail :: Property
237+lookAheadFail =
238+ forAll (chooseInt (min_value + 1, max_value)) $ \n ->
239+ let
240+ takeWithoutConsume = D.lookAhead $ D.take n FL.toList
241+ parseTwice = do
242+ parsed_list_1 <- takeWithoutConsume
243+ parsed_list_2 <- takeWithoutConsume
244+ return (parsed_list_1, parsed_list_2)
245+ in
133− Left _ -> n > list_length
246+ forAll (chooseInt (min_value, n - 1)) $ \list_length ->
247+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
248+ case S.parseD parseTwice (S.fromList ls) of
249+ Right _ -> False
250+ Left _ -> True
134
251
135lookAhead :: Property
252lookAhead :: Property
136lookAhead =
253lookAhead =
137− forAll (chooseInt (0, 10000)) $ \n ->
254+ forAll (chooseInt (min_value, max_value)) $ \n ->
138 let
255 let
139 takeWithoutConsume = D.lookAhead $ D.take n FL.toList
256 takeWithoutConsume = D.lookAhead $ D.take n FL.toList
140 parseTwice = do
257 parseTwice = do
@@ -142,19 +259,19 @@ lookAhead =
142 parsed_list_2 <- takeWithoutConsume
259 parsed_list_2 <- takeWithoutConsume
143 return (parsed_list_1, parsed_list_2)
260 return (parsed_list_1, parsed_list_2)
144 in
261 in
145− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
262+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
146 case S.parseD parseTwice (S.fromList ls) of
263 case S.parseD parseTwice (S.fromList ls) of
147− Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
264+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
148− Left _ -> (list_length < n) || (list_length == n && n == 0)
265+ Left _ -> property ((list_length < n) || (list_length == n && n == 0))
149 where
266 where
150 list_length = Prelude.length ls
267 list_length = Prelude.length ls
151
268
152takeWhile :: Property
269takeWhile :: Property
153takeWhile =
270takeWhile =
154 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
271 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
155− case S.parseD (D.takeWhile predicate FL.toList) (S.fromList ls) of
272+ case S.parseD (D.takeWhile predicate FL.toList) (S.fromList ls) of
156− Right parsed_list -> parsed_list == Prelude.takeWhile predicate ls
273+ Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
157− Left _ -> False
274+ Left _ -> property False
158 where
275 where
159 predicate = (== 0)
276 predicate = (== 0)
160
277
@@ -163,11 +280,15 @@ takeWhile1 =
163 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
280 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
164 case S.parseD (D.takeWhile1 predicate FL.toList) (S.fromList ls) of
281 case S.parseD (D.takeWhile1 predicate FL.toList) (S.fromList ls) of
165 Right parsed_list -> case ls of
282 Right parsed_list -> case ls of
166− [] -> False
283+ [] -> property False
284+ (x : _) ->
285+ if predicate x then
167− (x : _) -> predicate x && (parsed_list == Prelude.takeWhile predicate ls)
286+ checkListEqual parsed_list (Prelude.takeWhile predicate ls)
287+ else
288+ property False
168 Left _ -> case ls of
289 Left _ -> case ls of
169− [] -> True
290+ [] -> property True
170− (x : _) -> not $ predicate x
291+ (x : _) -> property (not $ predicate x)
171 where
292 where
172 predicate = (== 0)
293 predicate = (== 0)
173
294
@@ -175,20 +296,20 @@ sliceSepBy :: Property
175sliceSepBy =
296sliceSepBy =
176 forAll (listOf (chooseInt (0, 1))) $ \ls ->
297 forAll (listOf (chooseInt (0, 1))) $ \ls ->
177 case S.parseD (D.sliceSepBy predicate FL.toList) (S.fromList ls) of
298 case S.parseD (D.sliceSepBy predicate FL.toList) (S.fromList ls) of
178− Right parsed_list -> parsed_list == Prelude.takeWhile (not . predicate) ls
299+ Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile (not . predicate) ls)
179− Left _ -> False
300+ Left _ -> property False
180 where
301 where
181 predicate = (== 1)
302 predicate = (== 1)
182
303
183−-- sliceSepByMax :: Property
304+sliceSepByMax :: Property
184−-- sliceSepByMax =
305+sliceSepByMax =
185−-- forAll (chooseInt (0, 10000)) $ \n ->
306+ forAll (chooseInt (min_value, max_value)) $ \n ->
186−-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
307+ forAll (listOf (chooseInt (0, 1))) $ \ls ->
187−-- case S.parseD (D.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
308+ case S.parseD (D.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
188−-- Right parsed_list -> parsed_list == Prelude.take n (Prelude.takeWhile (not . predicate) ls)
309+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
189−-- Left _ -> False
310+ Left _ -> property False
190−-- where
311+ where
191−-- predicate = (== 1)
312+ predicate = (== 1)
192
313
193splitWith :: Property
314splitWith :: Property
194splitWith =
315splitWith =
@@ -200,86 +321,112 @@ splitWith =
200 Left _ -> case ls of
321 Left _ -> case ls of
201 0 : 1 : _ -> False
322 0 : 1 : _ -> False
202 _ -> True
323 _ -> True
203− .&&.
324+
325+splitWithFailLeft :: Property
326+splitWithFailLeft =
204 property (case S.parseD (D.splitWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
327 property (case S.parseD (D.splitWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
205 Right _ -> False
328 Right _ -> False
206 Left _ -> True)
329 Left _ -> True)
207− .&&.
330+
331+splitWithFailRight :: Property
332+splitWithFailRight =
208 property (case S.parseD (D.splitWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
333 property (case S.parseD (D.splitWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
209 Right _ -> False
334 Right _ -> False
210 Left _ -> True)
335 Left _ -> True)
211− .&&.
336+
337+splitWithFailBoth :: Property
338+splitWithFailBoth =
212 property (case S.parseD (D.splitWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
339 property (case S.parseD (D.splitWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
213 Right _ -> False
340 Right _ -> False
214 Left _ -> True)
341 Left _ -> True)
215
342
216−teeWith :: Property
343+teeWithPass :: Property
217−teeWith =
344+teeWithPass =
218− forAll (chooseInt (0, 10000)) $ \n ->
345+ forAll (chooseInt (min_value, max_value)) $ \n ->
219 forAll (listOf (chooseInt (0, 1))) $ \ls ->
346 forAll (listOf (chooseInt (0, 1))) $ \ls ->
220 let
347 let
221 prsr = D.take n FL.toList
348 prsr = D.take n FL.toList
222 in
349 in
223 case S.parseD (D.teeWith (,) prsr prsr) (S.fromList ls) of
350 case S.parseD (D.teeWith (,) prsr prsr) (S.fromList ls) of
224− Right (ls_1, ls_2) -> (Prelude.take n ls == ls_1) && (ls_1 == ls_2)
351+ Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
225− Left _ -> False
352+ Left _ -> property False
226− .&&.
353+
354+teeWithFailLeft :: Property
355+teeWithFailLeft =
227 property (case S.parseD (D.teeWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
356 property (case S.parseD (D.teeWith (,) (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
228 Right _ -> False
357 Right _ -> False
229 Left _ -> True)
358 Left _ -> True)
230− .&&.
359+
360+teeWithFailRight :: Property
361+teeWithFailRight =
231 property (case S.parseD (D.teeWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
362 property (case S.parseD (D.teeWith (,) (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
232 Right _ -> False
363 Right _ -> False
233 Left _ -> True)
364 Left _ -> True)
234− .&&.
365+
366+teeWithFailBoth :: Property
367+teeWithFailBoth =
235 property (case S.parseD (D.teeWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
368 property (case S.parseD (D.teeWith (,) (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
236 Right _ -> False
369 Right _ -> False
237 Left _ -> True)
370 Left _ -> True)
238
371
239−shortest :: Property
372+shortestPass :: Property
240−shortest =
373+shortestPass =
241− forAll (listOf (chooseInt(0, 10000))) $ \ls ->
374+ forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
242 let
375 let
376+ half_mid_value = mid_value `Prelude.div` 2
243− prsr_1 = D.takeWhile (<= 2500) FL.toList
377+ prsr_1 = D.takeWhile (<= half_mid_value) FL.toList
244− prsr_2 = D.takeWhile (<= 5000) FL.toList
378+ prsr_2 = D.takeWhile (<= mid_value) FL.toList
245 prsr_shortest = D.shortest prsr_1 prsr_2
379 prsr_shortest = D.shortest prsr_1 prsr_2
246 in
380 in
247 case S.parseD prsr_shortest (S.fromList ls) of
381 case S.parseD prsr_shortest (S.fromList ls) of
248− Right short_list -> short_list == Prelude.takeWhile (<= 2500) ls
382+ Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= half_mid_value) ls)
249− Left _ -> False
383+ Left _ -> property False
250− .&&.
384+
385+shortestPassLeft :: Property
386+shortestPassLeft =
251 property (case S.parseD (D.shortest (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
387 property (case S.parseD (D.shortest (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
252 Right r -> r == 1
388 Right r -> r == 1
253 Left _ -> False)
389 Left _ -> False)
254− .&&.
390+
391+shortestPassRight :: Property
392+shortestPassRight =
255 property (case S.parseD (D.shortest (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
393 property (case S.parseD (D.shortest (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
256 Right r -> r == 1
394 Right r -> r == 1
257 Left _ -> False)
395 Left _ -> False)
258− .&&.
396+
397+shortestFailBoth :: Property
398+shortestFailBoth =
259 property (case S.parseD (D.shortest (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
399 property (case S.parseD (D.shortest (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
260 Right _ -> False
400 Right _ -> False
261 Left _ -> True)
401 Left _ -> True)
262
402
263−longest :: Property
403+longestPass :: Property
264−longest =
404+longestPass =
265− forAll (listOf (chooseInt(0, 10000))) $ \ls ->
405+ forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
266 let
406 let
407+ half_mid_value = mid_value `Prelude.div` 2
267− prsr_1 = D.takeWhile (<= 2500) FL.toList
408+ prsr_1 = D.takeWhile (<= half_mid_value) FL.toList
268− prsr_2 = D.takeWhile (<= 5000) FL.toList
409+ prsr_2 = D.takeWhile (<= mid_value) FL.toList
269 prsr_longest = D.longest prsr_1 prsr_2
410 prsr_longest = D.longest prsr_1 prsr_2
270 in
411 in
271 case S.parseD prsr_longest (S.fromList ls) of
412 case S.parseD prsr_longest (S.fromList ls) of
272− Right long_list -> long_list == Prelude.takeWhile (<= 5000) ls
413+ Right long_list -> long_list == Prelude.takeWhile (<= mid_value) ls
273 Left _ -> False
414 Left _ -> False
274− .&&.
415+
416+longestPassLeft :: Property
417+longestPassLeft =
275 property (case S.parseD (D.shortest (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
418 property (case S.parseD (D.shortest (D.die "die") (D.yield (1 :: Int))) (S.fromList [1 :: Int]) of
276 Right r -> r == 1
419 Right r -> r == 1
277 Left _ -> False)
420 Left _ -> False)
278− .&&.
421+
422+longestPassRight :: Property
423+longestPassRight =
279 property (case S.parseD (D.shortest (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
424 property (case S.parseD (D.shortest (D.yield (1 :: Int)) (D.die "die")) (S.fromList [1 :: Int]) of
280 Right r -> r == 1
425 Right r -> r == 1
281 Left _ -> False)
426 Left _ -> False)
282− .&&.
427+
428+longestFailBoth :: Property
429+longestFailBoth =
283 property (case S.parseD (D.shortest (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
430 property (case S.parseD (D.shortest (D.die "die") (D.die "die")) (S.fromList [1 :: Int]) of
284 Right _ -> False
431 Right _ -> False
285 Left _ -> True)
432 Left _ -> True)
@@ -292,12 +439,14 @@ many =
292 prsr = D.many concatFold $ D.sliceSepBy (== 1) FL.toList
439 prsr = D.many concatFold $ D.sliceSepBy (== 1) FL.toList
293 in
440 in
294 case S.parseD prsr (S.fromList ls) of
441 case S.parseD prsr (S.fromList ls) of
295− Right res_list -> res_list == Prelude.filter (== 0) ls
442+ Right res_list -> checkListEqual res_list (Prelude.filter (== 0) ls)
296− Left _ -> False
443+ Left _ -> property False
297− .&&.
444+
445+many_empty :: Property
446+many_empty =
298 property (case S.parseD (D.many FL.toList (D.die "die")) (S.fromList [1 :: Int]) of
447 property (case S.parseD (D.many FL.toList (D.die "die")) (S.fromList [1 :: Int]) of
299− Right res_list -> res_list == ([] :: [Int])
448+ Right res_list -> checkListEqual res_list ([] :: [Int])
300− Left _ -> False)
449+ Left _ -> property False)
301
450
302-- some :: Property
451-- some :: Property
303-- some =
452-- some =
@@ -309,7 +458,9 @@ many =
309-- case S.parseD prsr (S.fromList ls) of
458-- case S.parseD prsr (S.fromList ls) of
310-- Right res_list -> res_list == Prelude.filter (== 0) ls
459-- Right res_list -> res_list == Prelude.filter (== 0) ls
311-- Left _ -> False
460-- Left _ -> False
312−-- .&&.
461+
462+-- someFail :: Property
463+-- someFail =
313-- property (case S.parseD (D.some FL.toList (D.die "die")) (S.fromList [1 :: Int]) of
464-- property (case S.parseD (D.some FL.toList (D.die "die")) (S.fromList [1 :: Int]) of
314-- Right _ -> False
465-- Right _ -> False
315-- Left _ -> True)
466-- Left _ -> True)
@@ -317,31 +468,52 @@ many =
317main :: IO ()
468main :: IO ()
318main = hspec $ do
469main = hspec $ do
319 describe "test for accumulator" $ do
470 describe "test for accumulator" $ do
320− prop "test fromFold function" fromFold
471+ prop "D.fromFold FL.sum = FL.sum" fromFold
321− prop "test any function" Main.any
472+ prop "D.any = Prelude.any" Main.any
322− prop "test all function" Main.all
473+ prop "D.all = Prelude.all" Main.all
323− prop "test yield function" yield
474+ prop "yield value provided" yield
324− prop "test yieldM function" yieldM
475+ prop "yield monadic value provided" yieldM
325− prop "test die function" die
476+ prop "always fail" die
326− prop "test dieM function" dieM
477+ prop "always fail but monadic" dieM
327
478
328 describe "test for element parser" $ do
479 describe "test for element parser" $ do
329− prop "test for peek function" peek
480+ prop "peek = head with list length > 0" peekPass
330− prop "test for eof function" eof
481+ prop "peek fail on []" peekFail
331− prop "test for satisfy function" satisfy
482+ prop "eof pass on []" eofPass
483+ prop "eof fail on non-empty list" eofFail
484+ prop "first element exists and >= mid_value" satisfyPass
485+ prop "check first element exists and satisfies predicate" satisfy
332
486
333 describe "test for sequence parser" $ do
487 describe "test for sequence parser" $ do
334− prop "test for take function" Main.take
488+ prop "D.take = Prelude.take" Main.take
335− prop "test for takeEq function" Main.takeEQ
489+ prop "D.takeEQ = Prelude.take when len >= n" takeEQPass
336− prop "test for takeGE function" Main.takeGE
490+ prop "D.takeEQ = Prelude.take when len >= n and fail otherwise" Main.takeEQ
337− prop "test for LookAhead function" lookAhead
491+ prop "D.takeGE n ls = ls when len >= n" takeGEPass
338− prop "test for takeWhile function" Main.takeWhile
492+ prop "D.takeGE n ls = ls when len >= n and fail otherwise" Main.takeGE
339− prop "test for takeWhile1 function" takeWhile1
493+ prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n" lookAheadPass
340− prop "test for sliceSepBy function" sliceSepBy
494+ prop "Fail when stream length exceeded" lookAheadFail
341− -- prop "test for sliceSepByMax function" sliceSepByMax
495+ prop "lookAhead . take n >> lookAhead . take n = lookAhead . take n, else fail" lookAhead
342− prop "test for splitWith function" splitWith
496+ prop "D.takeWhile = Prelude.takeWhile" Main.takeWhile
343− prop "test for teeWith function" teeWith
497+ prop "D.takeWhile = Prelude.takeWhile if taken something, else check why failed" takeWhile1
344− prop "test for shortest function" shortest
498+ prop "D.sliceSepBy = Prelude.takeWhile (not . predicate)" sliceSepBy
345− prop "test for longest function" longest
499+ prop "D.sliceSepByMax = Prelude.take n (Prelude.takeWhile (not . predicate)" sliceSepByMax
346− prop "test for many function" many
500+ prop "parse 0, then 1, else fail" splitWith
347− -- prop "test for some function" some
501+ prop "fail due to die as left parser" splitWithFailLeft
502+ prop "fail due to die as right parser" splitWithFailRight
503+ prop "fail due to die as both parsers" splitWithFailBoth
504+ prop "parsed two lists should be equal" teeWithPass
505+ prop "fail due to die as left parser" teeWithFailLeft
506+ prop "fail due to die as right parser" teeWithFailRight
507+ prop "fail due to die as both parsers" teeWithFailBoth
508+ prop "D.takeWhile (<= half_mid_value) = Prelude.takeWhile half_mid_value" shortestPass
509+ prop "pass even if die is left parser" shortestPassLeft
510+ prop "pass even if die is right parser" shortestPassRight
511+ prop "fail due to die as both parsers" shortestFailBoth
512+ prop "D.takeWhile (<= mid_value) = Prelude.takeWhile (<= mid_value)" longestPass
513+ prop "pass even if die is left parser" longestPassLeft
514+ prop "pass even if die is right parser" longestPassRight
515+ prop "fail due to die as both parsers" longestFailBoth
516+ prop "D.many concatFold $ D.sliceSepBy (== 1) FL.toList = Prelude.filter (== 0)" many
517+ prop "[] due to parser being die" many_empty
518+ -- prop "" some
519+ -- prop "fail due to parser being die" someFail