@@ -28,6 +28,8 @@ import qualified Prelude
2828
2929import Data.List (nub,sort)
3030import qualified Data.List as List
31+ import Data.List.NonEmpty (NonEmpty(..))
32+ import qualified Data.List.NonEmpty as NE
3133import qualified Data.Set as Set
3234import Test.Tasty
3335import Test.Tasty.HUnit
@@ -185,9 +187,13 @@ main = defaultMain $ testGroup "map-properties"
185187 , testProperty "unionWithKeyMerge" prop_unionWithKeyMerge
186188 , testProperty "mergeWithKey model" prop_mergeWithKeyModel
187189 , testProperty "mergeA effects" prop_mergeA_effects
188- , testProperty "fromAscList" prop_ordered
190+ , testProperty "fromAscList" prop_fromAscList
191+ , testProperty "fromAscListWith" prop_fromAscListWith
192+ , testProperty "fromAscListWithKey" prop_fromAscListWithKey
189193 , testProperty "fromDistinctAscList" prop_fromDistinctAscList
190- , testProperty "fromDescList" prop_rev_ordered
194+ , testProperty "fromDescList" prop_fromDescList
195+ , testProperty "fromDescListWith" prop_fromDescListWith
196+ , testProperty "fromDescListWithKey" prop_fromDescListWithKey
191197 , testProperty "fromDistinctDescList" prop_fromDistinctDescList
192198 , testProperty "fromList then toList" prop_list
193199 , testProperty "toDescList" prop_descList
@@ -1271,31 +1277,46 @@ instance Arbitrary WhenMatchedSpec where
12711277
12721278----------------------------------------------------------------
12731279
1274- prop_ordered :: Property
1275- prop_ordered
1276- = forAll (choose (5,100)) $ \n ->
1277- let xs = [(x,()) | x <- [0..n::Int]]
1278- in fromAscList xs == fromList xs
1279-
1280- prop_rev_ordered :: Property
1281- prop_rev_ordered
1282- = forAll (choose (5,100)) $ \n ->
1283- let xs = [(x,()) | x <- [0..n::Int]]
1284- in fromDescList (reverse xs) == fromList xs
1285-
12861280prop_list :: [Int] -> Bool
12871281prop_list xs = (sort (nub xs) == [x | (x,()) <- toList (fromList [(x,()) | x <- xs])])
12881282
12891283prop_descList :: [Int] -> Bool
12901284prop_descList xs = (reverse (sort (nub xs)) == [x | (x,()) <- toDescList (fromList [(x,()) | x <- xs])])
12911285
1286+ prop_fromDescList :: [(Int, A)] -> Property
1287+ prop_fromDescList kxs =
1288+ valid t .&&.
1289+ t === fromList kxs
1290+ where
1291+ downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1292+ t = fromDescList downSortedKxs
1293+
1294+ prop_fromDescListWith :: Fun (A, A) A -> [(Int, A)] -> Property
1295+ prop_fromDescListWith f kxs =
1296+ valid t .&&.
1297+ t === fromListWith (apply2 f) downSortedKxs
1298+ where
1299+ downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1300+ t = fromDescListWith (apply2 f) downSortedKxs
1301+
1302+ prop_fromDescListWithKey :: Fun (Int, A, A) A -> [(Int, A)] -> Property
1303+ prop_fromDescListWithKey f kxs =
1304+ valid t .&&.
1305+ t === fromListWithKey (apply3 f) downSortedKxs
1306+ where
1307+ downSortedKxs = List.sortBy (comparing (Down . fst)) kxs
1308+ t = fromDescListWithKey (apply3 f) downSortedKxs
1309+
12921310prop_fromDistinctDescList :: [(Int, A)] -> Property
1293- prop_fromDistinctDescList xs =
1311+ prop_fromDistinctDescList kxs =
12941312 valid t .&&.
1295- toList t === nub_sort_xs
1313+ toList t === reverse nubDownSortedKxs
12961314 where
1297- t = fromDistinctDescList (reverse nub_sort_xs)
1298- nub_sort_xs = List.map List.head $ List.groupBy ((==) `on` fst) $ List.sortBy (comparing fst) xs
1315+ nubDownSortedKxs =
1316+ List.map NE.head $
1317+ NE.groupBy ((==) `on` fst) $
1318+ List.sortBy (comparing (Down . fst)) kxs
1319+ t = fromDistinctDescList nubDownSortedKxs
12991320
13001321prop_ascDescList :: [Int] -> Bool
13011322prop_ascDescList xs = toAscList m == reverse (toDescList m)
@@ -1308,13 +1329,40 @@ prop_fromList xs
13081329 t == List.foldr (uncurry insert) empty (zip xs xs)
13091330 where sort_xs = sort xs
13101331
1332+ prop_fromAscList :: [(Int, A)] -> Property
1333+ prop_fromAscList kxs =
1334+ valid t .&&.
1335+ t === fromList sortedKxs
1336+ where
1337+ sortedKxs = List.sortBy (comparing fst) kxs
1338+ t = fromAscList sortedKxs
1339+
1340+ prop_fromAscListWith :: Fun (A, A) A -> [(Int, A)] -> Property
1341+ prop_fromAscListWith f kxs =
1342+ valid t .&&.
1343+ t === fromListWith (apply2 f) sortedKxs
1344+ where
1345+ sortedKxs = List.sortBy (comparing fst) kxs
1346+ t = fromAscListWith (apply2 f) sortedKxs
1347+
1348+ prop_fromAscListWithKey :: Fun (Int, A, A) A -> [(Int, A)] -> Property
1349+ prop_fromAscListWithKey f kxs =
1350+ valid t .&&.
1351+ t === fromListWithKey (apply3 f) sortedKxs
1352+ where
1353+ sortedKxs = List.sortBy (comparing fst) kxs
1354+ t = fromAscListWithKey (apply3 f) sortedKxs
1355+
13111356prop_fromDistinctAscList :: [(Int, A)] -> Property
1312- prop_fromDistinctAscList xs =
1357+ prop_fromDistinctAscList kxs =
13131358 valid t .&&.
1314- toList t === nub_sort_xs
1359+ toList t === nubSortedKxs
13151360 where
1316- t = fromDistinctAscList nub_sort_xs
1317- nub_sort_xs = List.map List.head $ List.groupBy ((==) `on` fst) $ List.sortBy (comparing fst) xs
1361+ nubSortedKxs =
1362+ List.map NE.head $
1363+ NE.groupBy ((==) `on` fst) $
1364+ List.sortBy (comparing fst) kxs
1365+ t = fromDistinctAscList nubSortedKxs
13181366
13191367----------------------------------------------------------------
13201368
0 commit comments