Skip to content

Commit cd76046

Browse files
committed
For Map, test against fromList-like
1 parent fa58574 commit cd76046

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

containers-tests/tests/map-properties.hs

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,76 +1278,54 @@ instance Arbitrary WhenMatchedSpec where
12781278

12791279
----------------------------------------------------------------
12801280

1281-
-- fromAscListWith, fromAscListWithKey, fromDescListWith, fromDescListWithKey
1282-
-- all effectively perform a left fold over adjacent elements in the input list
1283-
-- using some function as long as the keys are equal.
1284-
--
1285-
-- The property tests for these functions compare the result against the
1286-
-- sequence we would get if we used NE.groupBy instead. We use Magma to check
1287-
-- the fold direction (left, not right) and the order of arguments to the fold
1288-
-- function (new then old).
1289-
12901281
data Magma a
12911282
= Inj a
12921283
| Magma a :* Magma a
12931284
deriving (Eq, Show)
12941285

1295-
groupByK :: Eq k => [(k, a)] -> [(k, NonEmpty a)]
1296-
groupByK =
1297-
List.map (\ys -> (fst (NE.head ys), NE.map snd ys)) .
1298-
NE.groupBy ((==) `on` fst)
1299-
13001286
prop_list :: [Int] -> Bool
13011287
prop_list xs = (sort (nub xs) == [x | (x,()) <- toList (fromList [(x,()) | x <- xs])])
13021288

13031289
prop_descList :: [Int] -> Bool
13041290
prop_descList xs = (reverse (sort (nub xs)) == [x | (x,()) <- toDescList (fromList [(x,()) | x <- xs])])
13051291

1306-
-- For a list with keys in decreasing order (downSortedKxs), the sequence we get from fromDescList
1307-
-- should be the same as the reverse of the sequence we get if we group by the key and take the last
1308-
-- key-value pair in each group.
13091292
prop_fromDescList :: [(Int, A)] -> Property
13101293
prop_fromDescList kxs =
13111294
valid t .&&.
1312-
toList t === reverse nubLastDownSortedXs
1295+
t === fromList kxs
13131296
where
13141297
downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1315-
nubLastDownSortedXs = [(k, NE.last xs) | (k,xs) <- groupByK downSortedKxs]
13161298
t = fromDescList downSortedKxs
13171299

1318-
-- For a list with keys in decreasing order (downSortedKxs), the sequence we get from
1319-
-- fromDescListWith f should be the same as the reverse of the sequence we get if we group by the
1320-
-- key and fold all the values for a key with f. fromDescListWith applies f as `f newv oldv`.
13211300
prop_fromDescListWith :: [(Int, A)] -> Property
13221301
prop_fromDescListWith kys =
13231302
valid t .&&.
1324-
toList t === reverse foldedDownSortedXs
1303+
t === fromListWith (:*) downSortedKxs
13251304
where
13261305
kxs = [(k, Inj y) | (k,y) <- kys]
13271306
downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1328-
foldedDownSortedXs = [(k, Foldable.foldl1 (flip (:*)) xs) | (k,xs) <- groupByK downSortedKxs]
13291307
t = fromDescListWith (:*) downSortedKxs
13301308

1331-
-- For a list with keys in decreasing order (downSortedKxs), the sequence we get from
1332-
-- fromDescListWithKey f should be the same as the reverse of the sequence we get if we group by the
1333-
-- key and fold all the values for a key with f. fromDescListWith applies f as `f key newv oldv`.
13341309
prop_fromDescListWithKey :: [(Int, A)] -> Property
13351310
prop_fromDescListWithKey kys =
13361311
valid t .&&.
1337-
toList t === reverse foldedDownSortedXs
1312+
t === fromListWithKey f downSortedKxs
13381313
where
13391314
kxs = [(k, Inj (Left y)) | (k,y) <- kys]
13401315
downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1341-
foldedDownSortedXs = [ (k, Foldable.foldl1 (\acc (Inj (Left x)) -> Inj (Right (k,x)) :* acc) xs)
1342-
| (k,xs) <- groupByK downSortedKxs ]
1343-
t = fromDescListWithKey (\k (Inj (Left x)) acc -> Inj (Right (k,x)) :* acc) downSortedKxs
1316+
f k (Inj (Left x)) acc = Inj (Right (k,x)) :* acc
1317+
f _ _ _ = error "prop_fromDescListWithKey"
1318+
t = fromDescListWithKey f downSortedKxs
13441319

13451320
prop_fromDistinctDescList :: [(Int, A)] -> Property
13461321
prop_fromDistinctDescList kxs =
13471322
valid t .&&.
13481323
toList t === reverse nubDownSortedKxs
13491324
where
1350-
nubDownSortedKxs = [(k, NE.head xs) | (k,xs) <- groupByK (List.sortBy (comparing (Down . fst)) kxs)]
1325+
nubDownSortedKxs =
1326+
List.map NE.head $
1327+
NE.groupBy ((==) `on` fst) $
1328+
List.sortBy (comparing (Down . fst)) kxs
13511329
t = fromDistinctDescList nubDownSortedKxs
13521330

13531331
prop_ascDescList :: [Int] -> Bool
@@ -1361,51 +1339,43 @@ prop_fromList xs
13611339
t == List.foldr (uncurry insert) empty (zip xs xs)
13621340
where sort_xs = sort xs
13631341

1364-
-- For a list with keys in increasing order (sortedKxs), the sequence we get from fromAscList
1365-
-- should be the same as the sequence we get if we group by the key and take the last key-value pair
1366-
-- in each group.
13671342
prop_fromAscList :: [(Int, A)] -> Property
13681343
prop_fromAscList kxs =
13691344
valid t .&&.
1370-
toList t === nubLastSortedXs
1345+
t === fromList sortedKxs
13711346
where
13721347
sortedKxs = List.sortBy (comparing fst) kxs
1373-
nubLastSortedXs = [(k, NE.last xs) | (k,xs) <- groupByK sortedKxs]
13741348
t = fromAscList sortedKxs
13751349

1376-
-- For a list with keys in increasing order (sortedKxs), the sequence we get from fromAscListWith f
1377-
-- should be the same as the the sequence we get if we group by the key and fold all the values for
1378-
-- a key with f. fromAscListWith applies f as `f newv oldv`.
13791350
prop_fromAscListWith :: [(Int, A)] -> Property
13801351
prop_fromAscListWith kys =
13811352
valid t .&&.
1382-
toList t === foldedSortedKxs
1353+
t === fromListWith (:*) sortedKxs
13831354
where
13841355
kxs = [(k, Inj y) | (k,y) <- kys]
13851356
sortedKxs = List.sortBy (comparing fst) kxs
1386-
foldedSortedKxs = [(k, Foldable.foldl1 (flip (:*)) x) | (k,x) <- groupByK sortedKxs]
13871357
t = fromAscListWith (:*) sortedKxs
13881358

1389-
-- For a list with keys in increasing order (sortedKxs), the sequence we get from
1390-
-- fromAscListWithKey f should be the same as the the sequence we get if we group by the key and
1391-
-- fold all the values for a key with f. fromAscListWithKey applies f as `f key newv oldv`.
13921359
prop_fromAscListWithKey :: [(Int, A)] -> Property
13931360
prop_fromAscListWithKey kys =
13941361
valid t .&&.
1395-
toList t === foldedSortedKxs
1362+
t === fromListWithKey f sortedKxs
13961363
where
13971364
kxs = [(k, Inj (Left y)) | (k,y) <- kys]
13981365
sortedKxs = List.sortBy (comparing fst) kxs
1399-
foldedSortedKxs = [ (k, Foldable.foldl1 (\acc (Inj (Left x)) -> Inj (Right (k,x)) :* acc) xs)
1400-
| (k,xs) <- groupByK sortedKxs ]
1401-
t = fromAscListWithKey (\k (Inj (Left x)) acc -> Inj (Right (k,x)) :* acc) sortedKxs
1366+
f k (Inj (Left x)) acc = Inj (Right (k,x)) :* acc
1367+
f _ _ _ = error "prop_fromAscListWithKey"
1368+
t = fromAscListWithKey f sortedKxs
14021369

14031370
prop_fromDistinctAscList :: [(Int, A)] -> Property
14041371
prop_fromDistinctAscList kxs =
14051372
valid t .&&.
14061373
toList t === nubSortedKxs
14071374
where
1408-
nubSortedKxs = [(k, NE.head xs) | (k,xs) <- groupByK (List.sortBy (comparing fst) kxs)]
1375+
nubSortedKxs =
1376+
List.map NE.head $
1377+
NE.groupBy ((==) `on` fst) $
1378+
List.sortBy (comparing fst) kxs
14091379
t = fromDistinctAscList nubSortedKxs
14101380

14111381
----------------------------------------------------------------

0 commit comments

Comments
 (0)