on master help sign in
add top level variables and split test functions with multiple properties
75f1f8eAhmed Zaheer Dadarkar
test/Streamly/Test/Internal/Data/Parser.hs+67−43
@@ -10,39 +10,51 @@ import Test.Hspec(hspec, describe)
10import Test.Hspec.QuickCheck
10import Test.Hspec.QuickCheck
11import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, (.&&.))
11import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, (.&&.))
12
12
13+min_value :: Int
14+min_value = 0
15+
16+mid_value :: Int
17+mid_value = 5000
18+
19+max_value :: Int
20+max_value = 10000
21+
22+max_length :: Int
23+max_length = 1000
24+
13-- Accumulator Tests
25-- Accumulator Tests
14
26
15fromFold :: Property
27fromFold :: Property
16fromFold =
28fromFold =
17− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
29+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
18 case (==) <$> (S.parse (P.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
30 case (==) <$> (S.parse (P.fromFold FL.sum) (S.fromList ls)) <*> (S.fold FL.sum (S.fromList ls)) of
19 Right is_equal -> is_equal
31 Right is_equal -> is_equal
20 Left _ -> False
32 Left _ -> False
21
33
22any :: Property
34any :: Property
23any =
35any =
24− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
36+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
25− case S.parse (P.any (> 5000)) (S.fromList ls) of
37+ case S.parse (P.any (> mid_value)) (S.fromList ls) of
26− Right r -> r == (Prelude.any (> 5000) ls)
38+ Right r -> r == (Prelude.any (> mid_value) ls)
27 Left _ -> False
39 Left _ -> False
28
40
29all :: Property
41all :: Property
30all =
42all =
31− forAll (listOf $ chooseInt (0, 10000)) $ \ls ->
43+ forAll (listOf $ chooseInt (min_value, max_value)) $ \ls ->
32− case S.parse (P.all (> 5000)) (S.fromList ls) of
44+ case S.parse (P.all (> mid_value)) (S.fromList ls) of
33− Right r -> r == (Prelude.all (> 5000) ls)
45+ Right r -> r == (Prelude.all (> mid_value) ls)
34 Left _ -> False
46 Left _ -> False
35
47
36yield :: Property
48yield :: Property
37yield =
49yield =
38− forAll (chooseInt (0, 10000)) $ \x ->
50+ forAll (chooseInt (min_value, max_value)) $ \x ->
39 case S.parse (P.yield x) (S.fromList [1 :: Int]) of
51 case S.parse (P.yield x) (S.fromList [1 :: Int]) of
40 Right r -> r == x
52 Right r -> r == x
41 Left _ -> False
53 Left _ -> False
42
54
43yieldM :: Property
55yieldM :: Property
44yieldM =
56yieldM =
45− forAll (chooseInt (0, 10000)) $ \x ->
57+ forAll (chooseInt (min_value, max_value)) $ \x ->
46 case S.parse (P.yieldM $ return x) (S.fromList [1 :: Int]) of
58 case S.parse (P.yieldM $ return x) (S.fromList [1 :: Int]) of
47 Right r -> r == x
59 Right r -> r == x
48 Left _ -> False
60 Left _ -> False
@@ -63,24 +75,26 @@ dieM =
63
75
64-- Element Parser Tests
76-- Element Parser Tests
65
77
66−peek :: Property
78+peekPass :: Property
67−peek =
79+peekPass =
68− forAll (chooseInt (1, 100)) $ \list_length ->
80+ forAll (chooseInt (1, max_length)) $ \list_length ->
69− forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
81+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
70 case S.parse P.peek (S.fromList ls) of
82 case S.parse P.peek (S.fromList ls) of
71 Right head_value -> case ls of
83 Right head_value -> case ls of
72 head_ls : _ -> head_value == head_ls
84 head_ls : _ -> head_value == head_ls
73 _ -> False
85 _ -> False
74 Left _ -> False
86 Left _ -> False
75− .&&.
87+
88+peekFail :: Property
89+peekFail =
76 property (case S.parse P.peek (S.fromList []) of
90 property (case S.parse P.peek (S.fromList []) of
77 Right _ -> False
91 Right _ -> False
78 Left _ -> True)
92 Left _ -> True)
79
93
80eof :: Property
94eof :: Property
81eof =
95eof =
82− forAll (chooseInt (1, 100)) $ \list_length ->
96+ forAll (chooseInt (1, max_length)) $ \list_length ->
83− forAll (vectorOf list_length (chooseInt (0, 10000))) $ \ls ->
97+ forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
84 case S.parse P.eof (S.fromList ls) of
98 case S.parse P.eof (S.fromList ls) of
85 Right _ -> False
99 Right _ -> False
86 Left _ -> True
100 Left _ -> True
@@ -91,7 +105,7 @@ eof =
91
105
92satisfy :: Property
106satisfy :: Property
93satisfy =
107satisfy =
94− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
108+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
95 case S.parse (P.satisfy predicate) (S.fromList ls) of
109 case S.parse (P.satisfy predicate) (S.fromList ls) of
96 Right r -> case ls of
110 Right r -> case ls of
97 [] -> False
111 [] -> False
@@ -100,22 +114,22 @@ satisfy =
100 [] -> True
114 [] -> True
101 (x : _) -> not $ predicate x
115 (x : _) -> not $ predicate x
102 where
116 where
103− predicate = (>= 5000)
117+ predicate = (>= mid_value)
104
118
105-- Sequence Parsers Tests
119-- Sequence Parsers Tests
106
120
107take :: Property
121take :: Property
108take =
122take =
109− forAll (chooseInt (0, 10000)) $ \n ->
123+ forAll (chooseInt (min_value, max_value)) $ \n ->
110− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
124+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
111 case S.parse (P.take n FL.toList) (S.fromList ls) of
125 case S.parse (P.take n FL.toList) (S.fromList ls) of
112 Right parsed_list -> parsed_list == Prelude.take n ls
126 Right parsed_list -> parsed_list == Prelude.take n ls
113 Left _ -> False
127 Left _ -> False
114
128
115takeEQ :: Property
129takeEQ :: Property
116takeEQ =
130takeEQ =
117− forAll (chooseInt (0, 10000)) $ \n ->
131+ forAll (chooseInt (min_value, max_value)) $ \n ->
118− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
132+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
119 let
133 let
120 list_length = Prelude.length ls
134 list_length = Prelude.length ls
121 in
135 in
@@ -125,8 +139,8 @@ takeEQ =
125
139
126takeGE :: Property
140takeGE :: Property
127takeGE =
141takeGE =
128− forAll (chooseInt (0, 10000)) $ \n ->
142+ forAll (chooseInt (min_value, max_value)) $ \n ->
129− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
143+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
130 let
144 let
131 list_length = Prelude.length ls
145 list_length = Prelude.length ls
132 in
146 in
@@ -136,7 +150,7 @@ takeGE =
136
150
137lookAhead :: Property
151lookAhead :: Property
138lookAhead =
152lookAhead =
139− forAll (chooseInt (0, 10000)) $ \n ->
153+ forAll (chooseInt (min_value, max_value)) $ \n ->
140 let
154 let
141 takeWithoutConsume = P.lookAhead $ P.take n FL.toList
155 takeWithoutConsume = P.lookAhead $ P.take n FL.toList
142 parseTwice = do
156 parseTwice = do
@@ -144,7 +158,7 @@ lookAhead =
144 parsed_list_2 <- takeWithoutConsume
158 parsed_list_2 <- takeWithoutConsume
145 return (parsed_list_1, parsed_list_2)
159 return (parsed_list_1, parsed_list_2)
146 in
160 in
147− forAll (listOf (chooseInt (0, 10000))) $ \ls ->
161+ forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
148 case S.parse parseTwice (S.fromList ls) of
162 case S.parse parseTwice (S.fromList ls) of
149 Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
163 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)
164 Left _ -> (list_length < n) || (list_length == n && n == 0)
@@ -184,7 +198,7 @@ sliceSepBy =
184
198
185-- sliceSepByMax :: Property
199-- sliceSepByMax :: Property
186-- sliceSepByMax =
200-- sliceSepByMax =
187−-- forAll (chooseInt (0, 10000)) $ \n ->
201+-- forAll (chooseInt (min_value, max_value)) $ \n ->
188-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
202-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
189-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
203-- 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)
204-- Right parsed_list -> parsed_list == Prelude.take n (Prelude.takeWhile (not . predicate) ls)
@@ -192,8 +206,8 @@ sliceSepBy =
192-- where
206-- where
193-- predicate = (== 1)
207-- predicate = (== 1)
194
208
195−-- splitWith :: Property
209+-- splitWithPass :: Property
196−-- splitWith =
210+-- splitWithPass =
197-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
211-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
198-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
212-- case S.parse (P.splitWith (,) (P.satisfy (== 0)) (P.satisfy (== 1))) (S.fromList ls) of
199-- Right (result_first, result_second) -> case ls of
213-- Right (result_first, result_second) -> case ls of
@@ -202,7 +216,9 @@ sliceSepBy =
202-- Left _ -> case ls of
216-- Left _ -> case ls of
203-- 0 : 1 : _ -> False
217-- 0 : 1 : _ -> False
204-- _ -> True
218-- _ -> True
205−-- .&&.
219+
220+-- splitWithFail :: Property
221+-- splitWithFail =
206-- property (case S.parse (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
222-- property (case S.parse (P.splitWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
207-- Right _ -> False
223-- Right _ -> False
208-- Left _ -> True)
224-- Left _ -> True)
@@ -215,8 +231,8 @@ sliceSepBy =
215-- Right _ -> False
231-- Right _ -> False
216-- Left _ -> True)
232-- Left _ -> True)
217
233
218−-- teeWith :: Property
234+-- teeWithPass :: Property
219−-- teeWith =
235+-- teeWithPass =
220-- forAll (chooseInt (0, 10000)) $ \n ->
236-- forAll (chooseInt (0, 10000)) $ \n ->
221-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
237-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
222-- let
238-- let
@@ -225,7 +241,9 @@ sliceSepBy =
225-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
241-- 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)
242-- Right (ls_1, ls_2) -> (Prelude.take n ls == ls_1) && (ls_1 == ls_2)
227-- Left _ -> False
243-- Left _ -> False
228−-- .&&.
244+
245+-- teeWithFail :: Property
246+-- teeWithFail =
229-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
247-- property (case S.parse (P.teeWith (,) (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
230-- Right _ -> False
248-- Right _ -> False
231-- Left _ -> True)
249-- Left _ -> True)
@@ -250,18 +268,20 @@ sliceSepBy =
250-- prsr_2 = (P.takeWhile (== 1) FL.toList)
268-- prsr_2 = (P.takeWhile (== 1) FL.toList)
251-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
269-- concatFold = FL.Fold (\concatList curr_list -> return $ concatList ++ curr_list) (return []) return
252
270
253−-- shortest :: Property
271+-- shortestPass :: Property
254−-- shortest =
272+-- shortestPass =
255−-- forAll (listOf (chooseInt(0, 10000))) $ \ls ->
273+-- forAll (listOf (chooseInt(min_value, max_value))) $ \ls ->
256-- let
274-- let
257−-- prsr_1 = P.takeWhile (<= 2500) FL.toList
275+-- prsr_1 = P.takeWhile (<= (mid_value `Prelude.div` 2)) FL.toList
258−-- prsr_2 = P.takeWhile (<= 5000) FL.toList
276+-- prsr_2 = P.takeWhile (<= mid_value) FL.toList
259-- prsr_shortest = P.shortest prsr_1 prsr_2
277-- prsr_shortest = P.shortest prsr_1 prsr_2
260-- in
278-- in
261-- case S.parse prsr_shortest (S.fromList ls) of
279-- case S.parse prsr_shortest (S.fromList ls) of
262-- Right short_list -> short_list == Prelude.takeWhile (<= 2500) ls
280-- Right short_list -> short_list == Prelude.takeWhile (<= 2500) ls
263-- Left _ -> False
281-- Left _ -> False
264−-- .&&.
282+
283+-- shortestFail :: Property
284+-- shortestFail =
265-- property (case S.parse (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
285-- property (case S.parse (P.shortest (P.die "die") (P.yield (1 :: Int))) (S.fromList [1 :: Int]) of
266-- Right r -> r == 1
286-- Right r -> r == 1
267-- Left _ -> False)
287-- Left _ -> False)
@@ -286,7 +306,8 @@ main = hspec $ do
286 prop "test dieM function" dieM
306 prop "test dieM function" dieM
287
307
288 describe "test for element parser" $ do
308 describe "test for element parser" $ do
289− prop "test for peek function" peek
309+ prop "test for peek function" peekPass
310+ prop "test for peek function" peekFail
290 prop "test for eof function" eof
311 prop "test for eof function" eof
291 prop "test for satisfy function" satisfy
312 prop "test for satisfy function" satisfy
292
313
@@ -299,7 +320,10 @@ main = hspec $ do
299 prop "test for takeWhile1 function" takeWhile1
320 prop "test for takeWhile1 function" takeWhile1
300 prop "test for sliceSepBy function" sliceSepBy
321 prop "test for sliceSepBy function" sliceSepBy
301 -- prop "test for sliceSepByMax function" sliceSepByMax
322 -- prop "test for sliceSepByMax function" sliceSepByMax
302− -- prop "test for splitWith function" splitWith
323+ -- prop "test for splitWithPass function" splitWithPass
324+ -- prop "test for splitWithFail function" splitWithFail
325+ -- prop "test for teeWithPass function" teeWithPass
303− -- prop "test for teeWith function" teeWith
326+ -- prop "test for teeWithFail function" teeWithFail
304 -- prop "test for deintercalate function" deintercalate
327 -- prop "test for deintercalate function" deintercalate
305− -- prop "test for shortest function" shortest
328+ -- prop "test for shortestPass function" shortestPass
329+ -- prop "test for shortestFail function" shortestFail