on master help sign in
update to use different equality checker for lists
14f4dcdAhmed Zaheer Dadarkar
test/Streamly/Test/Internal/Data/Parser.hs+69−31
@@ -8,7 +8,12 @@ import qualified Streamly.Internal.Data.Fold as FL
8
8
9import Test.Hspec(hspec, describe)
9import Test.Hspec(hspec, describe)
10import Test.Hspec.QuickCheck
10import Test.Hspec.QuickCheck
11−import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf)
11+import Test.QuickCheck (forAll, chooseInt, Property, property, listOf, vectorOf, counterexample, (.&&.))
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 ((\\))
12
17
13min_value :: Int
18min_value :: Int
14min_value = 0
19min_value = 0
@@ -22,6 +27,27 @@ max_value = 10000
22max_length :: Int
27max_length :: Int
23max_length = 1000
28max_length = 1000
24
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+
25-- Accumulator Tests
51-- Accumulator Tests
26
52
27fromFold :: Property
53fromFold :: Property
@@ -137,8 +163,8 @@ take =
137 forAll (chooseInt (min_value, max_value)) $ \n ->
163 forAll (chooseInt (min_value, max_value)) $ \n ->
138 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
164 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
139 case S.parse (P.take n FL.toList) (S.fromList ls) of
165 case S.parse (P.take n FL.toList) (S.fromList ls) of
140− Right parsed_list -> parsed_list == Prelude.take n ls
166+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
141− Left _ -> False
167+ Left _ -> property False
142
168
143takeEQPass :: Property
169takeEQPass :: Property
144takeEQPass =
170takeEQPass =
@@ -146,8 +172,8 @@ takeEQPass =
146 forAll (chooseInt (n, max_value)) $ \list_length ->
172 forAll (chooseInt (n, max_value)) $ \list_length ->
147 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
173 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
148 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
174 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
149− Right parsed_list -> parsed_list == Prelude.take n ls
175+ Right parsed_list -> checkListEqual parsed_list (Prelude.take n ls)
150− Left _ -> False
176+ Left _ -> property False
151
177
152takeEQ :: Property
178takeEQ :: Property
153takeEQ =
179takeEQ =
@@ -157,8 +183,12 @@ takeEQ =
157 list_length = Prelude.length ls
183 list_length = Prelude.length ls
158 in
184 in
159 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
185 case S.parse (P.takeEQ n FL.toList) (S.fromList ls) of
186+ Right parsed_list ->
187+ if (n <= list_length) then
160− Right parsed_list -> (n <= list_length) && (parsed_list == Prelude.take n ls)
188+ checkListEqual parsed_list (Prelude.take n ls)
189+ else
190+ property False
161− Left _ -> n > list_length
191+ Left _ -> property (n > list_length)
162
192
163takeGEPass :: Property
193takeGEPass :: Property
164takeGEPass =
194takeGEPass =
@@ -166,8 +196,8 @@ takeGEPass =
166 forAll (chooseInt (n, max_value)) $ \list_length ->
196 forAll (chooseInt (n, max_value)) $ \list_length ->
167 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
197 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
168 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
198 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
169− Right parsed_list -> parsed_list == ls
199+ Right parsed_list -> checkListEqual parsed_list ls
170− Left _ -> False
200+ Left _ -> property False
171
201
172takeGE :: Property
202takeGE :: Property
173takeGE =
203takeGE =
@@ -177,8 +207,12 @@ takeGE =
177 list_length = Prelude.length ls
207 list_length = Prelude.length ls
178 in
208 in
179 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
209 case S.parse (P.takeGE n FL.toList) (S.fromList ls) of
180− Right parsed_list -> (n <= list_length) && (parsed_list == ls)
210+ Right parsed_list ->
211+ if (n <= list_length) then
212+ checkListEqual parsed_list ls
213+ else
214+ property False
181− Left _ -> n > list_length
215+ Left _ -> property (n > list_length)
182
216
183lookAheadPass :: Property
217lookAheadPass :: Property
184lookAheadPass =
218lookAheadPass =
@@ -193,8 +227,8 @@ lookAheadPass =
193 forAll (chooseInt (n, max_value)) $ \list_length ->
227 forAll (chooseInt (n, max_value)) $ \list_length ->
194 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
228 forAll (vectorOf list_length (chooseInt (min_value, max_value))) $ \ls ->
195 case S.parse parseTwice (S.fromList ls) of
229 case S.parse parseTwice (S.fromList ls) of
196− Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
230+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
197− Left _ -> False
231+ Left _ -> property $ False
198
232
199lookAheadFail :: Property
233lookAheadFail :: Property
200lookAheadFail =
234lookAheadFail =
@@ -224,17 +258,17 @@ lookAhead =
224 in
258 in
225 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
259 forAll (listOf (chooseInt (min_value, max_value))) $ \ls ->
226 case S.parse parseTwice (S.fromList ls) of
260 case S.parse parseTwice (S.fromList ls) of
227− Right (ls_1, ls_2) -> (ls_1 == ls_2) && (ls_1 == Prelude.take n ls)
261+ Right (ls_1, ls_2) -> checkListEqual ls_1 ls_2 .&&. checkListEqual ls_1 (Prelude.take n ls)
228− Left _ -> (list_length < n) || (list_length == n && n == 0)
262+ Left _ -> property ((list_length < n) || (list_length == n && n == 0))
229 where
263 where
230 list_length = Prelude.length ls
264 list_length = Prelude.length ls
231
265
232takeWhile :: Property
266takeWhile :: Property
233takeWhile =
267takeWhile =
234 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
268 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
235− case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
269+ case S.parse (P.takeWhile predicate FL.toList) (S.fromList ls) of
236− Right parsed_list -> parsed_list == Prelude.takeWhile predicate ls
270+ Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile predicate ls)
237− Left _ -> False
271+ Left _ -> property False
238 where
272 where
239 predicate = (== 0)
273 predicate = (== 0)
240
274
@@ -243,11 +277,15 @@ takeWhile1 =
243 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
277 forAll (listOf (chooseInt (0, 1))) $ \ ls ->
244 case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
278 case S.parse (P.takeWhile1 predicate FL.toList) (S.fromList ls) of
245 Right parsed_list -> case ls of
279 Right parsed_list -> case ls of
246− [] -> False
280+ [] -> property False
281+ (x : _) ->
282+ if predicate x then
247− (x : _) -> predicate x && (parsed_list == Prelude.takeWhile predicate ls)
283+ checkListEqual parsed_list (Prelude.takeWhile predicate ls)
284+ else
285+ property False
248 Left _ -> case ls of
286 Left _ -> case ls of
249− [] -> True
287+ [] -> property True
250− (x : _) -> not $ predicate x
288+ (x : _) -> property (not $ predicate x)
251 where
289 where
252 predicate = (== 0)
290 predicate = (== 0)
253
291
@@ -255,8 +293,8 @@ sliceSepBy :: Property
255sliceSepBy =
293sliceSepBy =
256 forAll (listOf (chooseInt (0, 1))) $ \ls ->
294 forAll (listOf (chooseInt (0, 1))) $ \ls ->
257 case S.parse (P.sliceSepBy predicate FL.toList) (S.fromList ls) of
295 case S.parse (P.sliceSepBy predicate FL.toList) (S.fromList ls) of
258− Right parsed_list -> parsed_list == Prelude.takeWhile (not . predicate) ls
296+ Right parsed_list -> checkListEqual parsed_list (Prelude.takeWhile (not . predicate) ls)
259− Left _ -> False
297+ Left _ -> property False
260 where
298 where
261 predicate = (== 1)
299 predicate = (== 1)
262
300
@@ -265,8 +303,8 @@ sliceSepBy =
265-- forAll (chooseInt (min_value, max_value)) $ \n ->
303-- forAll (chooseInt (min_value, max_value)) $ \n ->
266-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
304-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
267-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
305-- case S.parse (P.sliceSepByMax predicate n FL.toList) (S.fromList ls) of
268−-- Right parsed_list -> parsed_list == Prelude.take n (Prelude.takeWhile (not . predicate) ls)
306+-- Right parsed_list -> checkListEqual parsed_list (Prelude.take n (Prelude.takeWhile (not . predicate) ls))
269−-- Left _ -> False
307+-- Left _ -> property False
270-- where
308-- where
271-- predicate = (== 1)
309-- predicate = (== 1)
272
310
@@ -307,8 +345,8 @@ sliceSepBy =
307-- prsr = P.take n FL.toList
345-- prsr = P.take n FL.toList
308-- in
346-- in
309-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
347-- case S.parse (P.teeWith (,) prsr prsr) (S.fromList ls) of
310−-- Right (ls_1, ls_2) -> (Prelude.take n ls == ls_1) && (ls_1 == ls_2)
348+-- Right (ls_1, ls_2) -> checkListEqual (Prelude.take n ls) ls_1 .&&. checkListEqual ls_1 ls_2
311−-- Left _ -> False
349+-- Left _ -> property False
312
350
313-- teeWithFailLeft :: Property
351-- teeWithFailLeft :: Property
314-- teeWithFailLeft =
352-- teeWithFailLeft =
@@ -332,7 +370,7 @@ sliceSepBy =
332-- deintercalate =
370-- deintercalate =
333-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
371-- forAll (listOf (chooseInt (0, 1))) $ \ls ->
334-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
372-- case S.parse (P.deintercalate concatFold prsr_1 concatFold prsr_2) (S.fromList ls) of
335−-- Right parsed_list_tuple -> parsed_list_tuple == partition (== 0) ls
373+-- Right parsed_list_tuple -> parsed_list_tuple == (partition (== 0) ls)
336-- Left _ -> False
374-- Left _ -> False
337
375
338-- where
376-- where
@@ -349,8 +387,8 @@ sliceSepBy =
349-- prsr_shortest = P.shortest prsr_1 prsr_2
387-- prsr_shortest = P.shortest prsr_1 prsr_2
350-- in
388-- in
351-- case S.parse prsr_shortest (S.fromList ls) of
389-- case S.parse prsr_shortest (S.fromList ls) of
352−-- Right short_list -> short_list == Prelude.takeWhile (<= 2500) ls
390+-- Right short_list -> checkListEqual short_list (Prelude.takeWhile (<= 2500) ls)
353−-- Left _ -> False
391+-- Left _ -> property False
354
392
355-- shortestFailLeft :: Property
393-- shortestFailLeft :: Property
356-- shortestFailLeft =
394-- shortestFailLeft =