1{-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-} 2{-# OPTIONS_GHC -fno-warn-orphans #-} -- because of Arbitrary (HashMap k v) 3 4-- | Tests for the 'Data.HashMap.Lazy' module. We test functions by 5-- comparing them to a simpler model, an association list. 6 7module Main (main) where 8 9import Control.Monad ( guard ) 10import qualified Data.Foldable as Foldable 11#if MIN_VERSION_base(4,10,0) 12import Data.Bifoldable 13#endif 14import Data.Function (on) 15import Data.Hashable (Hashable(hashWithSalt)) 16import qualified Data.List as L 17import Data.Ord (comparing) 18#if defined(STRICT) 19import Data.HashMap.Strict (HashMap) 20import qualified Data.HashMap.Strict as HM 21import qualified Data.Map.Strict as M 22#else 23import Data.HashMap.Lazy (HashMap) 24import qualified Data.HashMap.Lazy as HM 25import qualified Data.Map.Lazy as M 26#endif 27import Test.QuickCheck (Arbitrary(..), Property, (==>), (===), forAll, elements) 28import Test.Framework (Test, defaultMain, testGroup) 29import Test.Framework.Providers.QuickCheck2 (testProperty) 30#if MIN_VERSION_base(4,8,0) 31import Data.Functor.Identity (Identity (..)) 32#endif 33import Control.Applicative (Const (..)) 34import Test.QuickCheck.Function (Fun, apply) 35import Test.QuickCheck.Poly (A, B) 36 37-- Key type that generates more hash collisions. 38newtype Key = K { unK :: Int } 39 deriving (Arbitrary, Eq, Ord, Read, Show) 40 41instance Hashable Key where 42 hashWithSalt salt k = hashWithSalt salt (unK k) `mod` 20 43 44instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where 45 arbitrary = fmap (HM.fromList) arbitrary 46 47------------------------------------------------------------------------ 48-- * Properties 49 50------------------------------------------------------------------------ 51-- ** Instances 52 53pEq :: [(Key, Int)] -> [(Key, Int)] -> Bool 54pEq xs = (M.fromList xs ==) `eq` (HM.fromList xs ==) 55 56pNeq :: [(Key, Int)] -> [(Key, Int)] -> Bool 57pNeq xs = (M.fromList xs /=) `eq` (HM.fromList xs /=) 58 59-- We cannot compare to `Data.Map` as ordering is different. 60pOrd1 :: [(Key, Int)] -> Bool 61pOrd1 xs = compare x x == EQ 62 where 63 x = HM.fromList xs 64 65pOrd2 :: [(Key, Int)] -> [(Key, Int)] -> [(Key, Int)] -> Bool 66pOrd2 xs ys zs = case (compare x y, compare y z) of 67 (EQ, o) -> compare x z == o 68 (o, EQ) -> compare x z == o 69 (LT, LT) -> compare x z == LT 70 (GT, GT) -> compare x z == GT 71 (LT, GT) -> True -- ys greater than xs and zs. 72 (GT, LT) -> True 73 where 74 x = HM.fromList xs 75 y = HM.fromList ys 76 z = HM.fromList zs 77 78pOrd3 :: [(Key, Int)] -> [(Key, Int)] -> Bool 79pOrd3 xs ys = case (compare x y, compare y x) of 80 (EQ, EQ) -> True 81 (LT, GT) -> True 82 (GT, LT) -> True 83 _ -> False 84 where 85 x = HM.fromList xs 86 y = HM.fromList ys 87 88pOrdEq :: [(Key, Int)] -> [(Key, Int)] -> Bool 89pOrdEq xs ys = case (compare x y, x == y) of 90 (EQ, True) -> True 91 (LT, False) -> True 92 (GT, False) -> True 93 _ -> False 94 where 95 x = HM.fromList xs 96 y = HM.fromList ys 97 98pReadShow :: [(Key, Int)] -> Bool 99pReadShow xs = M.fromList xs == read (show (M.fromList xs)) 100 101pFunctor :: [(Key, Int)] -> Bool 102pFunctor = fmap (+ 1) `eq_` fmap (+ 1) 103 104pFoldable :: [(Int, Int)] -> Bool 105pFoldable = (L.sort . Foldable.foldr (:) []) `eq` 106 (L.sort . Foldable.foldr (:) []) 107 108pHashable :: [(Key, Int)] -> [Int] -> Int -> Property 109pHashable xs is salt = 110 x == y ==> hashWithSalt salt x === hashWithSalt salt y 111 where 112 xs' = L.nubBy (\(k,_) (k',_) -> k == k') xs 113 ys = shuffle is xs' 114 x = HM.fromList xs' 115 y = HM.fromList ys 116 -- Shuffle the list using indexes in the second 117 shuffle :: [Int] -> [a] -> [a] 118 shuffle idxs = L.map snd 119 . L.sortBy (comparing fst) 120 . L.zip (idxs ++ [L.maximum (0:is) + 1 ..]) 121 122------------------------------------------------------------------------ 123-- ** Basic interface 124 125pSize :: [(Key, Int)] -> Bool 126pSize = M.size `eq` HM.size 127 128pMember :: Key -> [(Key, Int)] -> Bool 129pMember k = M.member k `eq` HM.member k 130 131pLookup :: Key -> [(Key, Int)] -> Bool 132pLookup k = M.lookup k `eq` HM.lookup k 133 134pLookupOperator :: Key -> [(Key, Int)] -> Bool 135pLookupOperator k = M.lookup k `eq` (HM.!? k) 136 137pInsert :: Key -> Int -> [(Key, Int)] -> Bool 138pInsert k v = M.insert k v `eq_` HM.insert k v 139 140pDelete :: Key -> [(Key, Int)] -> Bool 141pDelete k = M.delete k `eq_` HM.delete k 142 143newtype AlwaysCollide = AC Int 144 deriving (Arbitrary, Eq, Ord, Show) 145 146instance Hashable AlwaysCollide where 147 hashWithSalt _ _ = 1 148 149-- White-box test that tests the case of deleting one of two keys from 150-- a map, where the keys' hash values collide. 151pDeleteCollision :: AlwaysCollide -> AlwaysCollide -> AlwaysCollide -> Int 152 -> Property 153pDeleteCollision k1 k2 k3 idx = (k1 /= k2) && (k2 /= k3) && (k1 /= k3) ==> 154 HM.member toKeep $ HM.delete toDelete $ 155 HM.fromList [(k1, 1 :: Int), (k2, 2), (k3, 3)] 156 where 157 which = idx `mod` 3 158 toDelete 159 | which == 0 = k1 160 | which == 1 = k2 161 | which == 2 = k3 162 | otherwise = error "Impossible" 163 toKeep 164 | which == 0 = k2 165 | which == 1 = k3 166 | which == 2 = k1 167 | otherwise = error "Impossible" 168 169pInsertWith :: Key -> [(Key, Int)] -> Bool 170pInsertWith k = M.insertWith (+) k 1 `eq_` HM.insertWith (+) k 1 171 172pAdjust :: Key -> [(Key, Int)] -> Bool 173pAdjust k = M.adjust succ k `eq_` HM.adjust succ k 174 175pUpdateAdjust :: Key -> [(Key, Int)] -> Bool 176pUpdateAdjust k = M.update (Just . succ) k `eq_` HM.update (Just . succ) k 177 178pUpdateDelete :: Key -> [(Key, Int)] -> Bool 179pUpdateDelete k = M.update (const Nothing) k `eq_` HM.update (const Nothing) k 180 181pAlterAdjust :: Key -> [(Key, Int)] -> Bool 182pAlterAdjust k = M.alter (fmap succ) k `eq_` HM.alter (fmap succ) k 183 184pAlterInsert :: Key -> [(Key, Int)] -> Bool 185pAlterInsert k = M.alter (const $ Just 3) k `eq_` HM.alter (const $ Just 3) k 186 187pAlterDelete :: Key -> [(Key, Int)] -> Bool 188pAlterDelete k = M.alter (const Nothing) k `eq_` HM.alter (const Nothing) k 189 190 191-- We choose the list functor here because we don't fuss with 192-- it in alterF rules and because it has a sufficiently interesting 193-- structure to have a good chance of breaking if something is wrong. 194pAlterF :: Key -> Fun (Maybe A) [Maybe A] -> [(Key, A)] -> Property 195pAlterF k f xs = 196 fmap M.toAscList (M.alterF (apply f) k (M.fromList xs)) 197 === 198 fmap toAscList (HM.alterF (apply f) k (HM.fromList xs)) 199 200#if !MIN_VERSION_base(4,8,0) 201newtype Identity a = Identity {runIdentity :: a} 202instance Functor Identity where 203 fmap f (Identity x) = Identity (f x) 204#endif 205 206pAlterFAdjust :: Key -> [(Key, Int)] -> Bool 207pAlterFAdjust k = 208 runIdentity . M.alterF (Identity . fmap succ) k `eq_` 209 runIdentity . HM.alterF (Identity . fmap succ) k 210 211pAlterFInsert :: Key -> [(Key, Int)] -> Bool 212pAlterFInsert k = 213 runIdentity . M.alterF (const . Identity . Just $ 3) k `eq_` 214 runIdentity . HM.alterF (const . Identity . Just $ 3) k 215 216pAlterFInsertWith :: Key -> Fun Int Int -> [(Key, Int)] -> Bool 217pAlterFInsertWith k f = 218 runIdentity . M.alterF (Identity . Just . maybe 3 (apply f)) k `eq_` 219 runIdentity . HM.alterF (Identity . Just . maybe 3 (apply f)) k 220 221pAlterFDelete :: Key -> [(Key, Int)] -> Bool 222pAlterFDelete k = 223 runIdentity . M.alterF (const (Identity Nothing)) k `eq_` 224 runIdentity . HM.alterF (const (Identity Nothing)) k 225 226pAlterFLookup :: Key 227 -> Fun (Maybe A) B 228 -> [(Key, A)] -> Bool 229pAlterFLookup k f = 230 getConst . M.alterF (Const . apply f :: Maybe A -> Const B (Maybe A)) k 231 `eq` 232 getConst . HM.alterF (Const . apply f) k 233 234pSubmap :: [(Key, Int)] -> [(Key, Int)] -> Bool 235pSubmap xs ys = M.isSubmapOf (M.fromList xs) (M.fromList ys) == 236 HM.isSubmapOf (HM.fromList xs) (HM.fromList ys) 237 238pSubmapReflexive :: HashMap Key Int -> Bool 239pSubmapReflexive m = HM.isSubmapOf m m 240 241pSubmapUnion :: HashMap Key Int -> HashMap Key Int -> Bool 242pSubmapUnion m1 m2 = HM.isSubmapOf m1 (HM.union m1 m2) 243 244pNotSubmapUnion :: HashMap Key Int -> HashMap Key Int -> Property 245pNotSubmapUnion m1 m2 = not (HM.isSubmapOf m1 m2) ==> HM.isSubmapOf m1 (HM.union m1 m2) 246 247pSubmapDifference :: HashMap Key Int -> HashMap Key Int -> Bool 248pSubmapDifference m1 m2 = HM.isSubmapOf (HM.difference m1 m2) m1 249 250pNotSubmapDifference :: HashMap Key Int -> HashMap Key Int -> Property 251pNotSubmapDifference m1 m2 = 252 not (HM.null (HM.intersection m1 m2)) ==> 253 not (HM.isSubmapOf m1 (HM.difference m1 m2)) 254 255pSubmapDelete :: HashMap Key Int -> Property 256pSubmapDelete m = not (HM.null m) ==> 257 forAll (elements (HM.keys m)) $ \k -> 258 HM.isSubmapOf (HM.delete k m) m 259 260pNotSubmapDelete :: HashMap Key Int -> Property 261pNotSubmapDelete m = 262 not (HM.null m) ==> 263 forAll (elements (HM.keys m)) $ \k -> 264 not (HM.isSubmapOf m (HM.delete k m)) 265 266pSubmapInsert :: Key -> Int -> HashMap Key Int -> Property 267pSubmapInsert k v m = not (HM.member k m) ==> HM.isSubmapOf m (HM.insert k v m) 268 269pNotSubmapInsert :: Key -> Int -> HashMap Key Int -> Property 270pNotSubmapInsert k v m = not (HM.member k m) ==> not (HM.isSubmapOf (HM.insert k v m) m) 271 272------------------------------------------------------------------------ 273-- ** Combine 274 275pUnion :: [(Key, Int)] -> [(Key, Int)] -> Bool 276pUnion xs ys = M.union (M.fromList xs) `eq_` HM.union (HM.fromList xs) $ ys 277 278pUnionWith :: [(Key, Int)] -> [(Key, Int)] -> Bool 279pUnionWith xs ys = M.unionWith (-) (M.fromList xs) `eq_` 280 HM.unionWith (-) (HM.fromList xs) $ ys 281 282pUnionWithKey :: [(Key, Int)] -> [(Key, Int)] -> Bool 283pUnionWithKey xs ys = M.unionWithKey go (M.fromList xs) `eq_` 284 HM.unionWithKey go (HM.fromList xs) $ ys 285 where 286 go :: Key -> Int -> Int -> Int 287 go (K k) i1 i2 = k - i1 + i2 288 289pUnions :: [[(Key, Int)]] -> Bool 290pUnions xss = M.toAscList (M.unions (map M.fromList xss)) == 291 toAscList (HM.unions (map HM.fromList xss)) 292 293------------------------------------------------------------------------ 294-- ** Transformations 295 296pMap :: [(Key, Int)] -> Bool 297pMap = M.map (+ 1) `eq_` HM.map (+ 1) 298 299pTraverse :: [(Key, Int)] -> Bool 300pTraverse xs = 301 L.sort (fmap (L.sort . M.toList) (M.traverseWithKey (\_ v -> [v + 1, v + 2]) (M.fromList (take 10 xs)))) 302 == L.sort (fmap (L.sort . HM.toList) (HM.traverseWithKey (\_ v -> [v + 1, v + 2]) (HM.fromList (take 10 xs)))) 303 304------------------------------------------------------------------------ 305-- ** Difference and intersection 306 307pDifference :: [(Key, Int)] -> [(Key, Int)] -> Bool 308pDifference xs ys = M.difference (M.fromList xs) `eq_` 309 HM.difference (HM.fromList xs) $ ys 310 311pDifferenceWith :: [(Key, Int)] -> [(Key, Int)] -> Bool 312pDifferenceWith xs ys = M.differenceWith f (M.fromList xs) `eq_` 313 HM.differenceWith f (HM.fromList xs) $ ys 314 where 315 f x y = if x == 0 then Nothing else Just (x - y) 316 317pIntersection :: [(Key, Int)] -> [(Key, Int)] -> Bool 318pIntersection xs ys = M.intersection (M.fromList xs) `eq_` 319 HM.intersection (HM.fromList xs) $ ys 320 321pIntersectionWith :: [(Key, Int)] -> [(Key, Int)] -> Bool 322pIntersectionWith xs ys = M.intersectionWith (-) (M.fromList xs) `eq_` 323 HM.intersectionWith (-) (HM.fromList xs) $ ys 324 325pIntersectionWithKey :: [(Key, Int)] -> [(Key, Int)] -> Bool 326pIntersectionWithKey xs ys = M.intersectionWithKey go (M.fromList xs) `eq_` 327 HM.intersectionWithKey go (HM.fromList xs) $ ys 328 where 329 go :: Key -> Int -> Int -> Int 330 go (K k) i1 i2 = k - i1 - i2 331 332------------------------------------------------------------------------ 333-- ** Folds 334 335pFoldr :: [(Int, Int)] -> Bool 336pFoldr = (L.sort . M.foldr (:) []) `eq` (L.sort . HM.foldr (:) []) 337 338pFoldl :: [(Int, Int)] -> Bool 339pFoldl = (L.sort . M.foldl (flip (:)) []) `eq` (L.sort . HM.foldl (flip (:)) []) 340 341#if MIN_VERSION_base(4,10,0) 342pBifoldMap :: [(Int, Int)] -> Bool 343pBifoldMap xs = concatMap f (HM.toList m) == bifoldMap (:[]) (:[]) m 344 where f (k, v) = [k, v] 345 m = HM.fromList xs 346 347pBifoldr :: [(Int, Int)] -> Bool 348pBifoldr xs = concatMap f (HM.toList m) == bifoldr (:) (:) [] m 349 where f (k, v) = [k, v] 350 m = HM.fromList xs 351 352pBifoldl :: [(Int, Int)] -> Bool 353pBifoldl xs = reverse (concatMap f $ HM.toList m) == bifoldl (flip (:)) (flip (:)) [] m 354 where f (k, v) = [k, v] 355 m = HM.fromList xs 356#endif 357 358pFoldrWithKey :: [(Int, Int)] -> Bool 359pFoldrWithKey = (sortByKey . M.foldrWithKey f []) `eq` 360 (sortByKey . HM.foldrWithKey f []) 361 where f k v z = (k, v) : z 362 363pFoldMapWithKey :: [(Int, Int)] -> Bool 364pFoldMapWithKey = (sortByKey . M.foldMapWithKey f) `eq` 365 (sortByKey . HM.foldMapWithKey f) 366 where f k v = [(k, v)] 367 368pFoldrWithKey' :: [(Int, Int)] -> Bool 369pFoldrWithKey' = (sortByKey . M.foldrWithKey' f []) `eq` 370 (sortByKey . HM.foldrWithKey' f []) 371 where f k v z = (k, v) : z 372 373pFoldlWithKey :: [(Int, Int)] -> Bool 374pFoldlWithKey = (sortByKey . M.foldlWithKey f []) `eq` 375 (sortByKey . HM.foldlWithKey f []) 376 where f z k v = (k, v) : z 377 378pFoldlWithKey' :: [(Int, Int)] -> Bool 379pFoldlWithKey' = (sortByKey . M.foldlWithKey' f []) `eq` 380 (sortByKey . HM.foldlWithKey' f []) 381 where f z k v = (k, v) : z 382 383pFoldl' :: [(Int, Int)] -> Bool 384pFoldl' = (L.sort . M.foldl' (flip (:)) []) `eq` (L.sort . HM.foldl' (flip (:)) []) 385 386pFoldr' :: [(Int, Int)] -> Bool 387pFoldr' = (L.sort . M.foldr' (:) []) `eq` (L.sort . HM.foldr' (:) []) 388 389------------------------------------------------------------------------ 390-- ** Filter 391 392pMapMaybeWithKey :: [(Key, Int)] -> Bool 393pMapMaybeWithKey = M.mapMaybeWithKey f `eq_` HM.mapMaybeWithKey f 394 where f k v = guard (odd (unK k + v)) >> Just (v + 1) 395 396pMapMaybe :: [(Key, Int)] -> Bool 397pMapMaybe = M.mapMaybe f `eq_` HM.mapMaybe f 398 where f v = guard (odd v) >> Just (v + 1) 399 400pFilter :: [(Key, Int)] -> Bool 401pFilter = M.filter odd `eq_` HM.filter odd 402 403pFilterWithKey :: [(Key, Int)] -> Bool 404pFilterWithKey = M.filterWithKey p `eq_` HM.filterWithKey p 405 where p k v = odd (unK k + v) 406 407------------------------------------------------------------------------ 408-- ** Conversions 409 410-- The free magma is used to test that operations are applied in the 411-- same order. 412data Magma a 413 = Leaf a 414 | Op (Magma a) (Magma a) 415 deriving (Show, Eq, Ord) 416 417instance Hashable a => Hashable (Magma a) where 418 hashWithSalt s (Leaf a) = hashWithSalt s (hashWithSalt (1::Int) a) 419 hashWithSalt s (Op m n) = hashWithSalt s (hashWithSalt (hashWithSalt (2::Int) m) n) 420 421-- 'eq_' already calls fromList. 422pFromList :: [(Key, Int)] -> Bool 423pFromList = id `eq_` id 424 425pFromListWith :: [(Key, Int)] -> Bool 426pFromListWith kvs = (M.toAscList $ M.fromListWith Op kvsM) == 427 (toAscList $ HM.fromListWith Op kvsM) 428 where kvsM = fmap (fmap Leaf) kvs 429 430pFromListWithKey :: [(Key, Int)] -> Bool 431pFromListWithKey kvs = (M.toAscList $ M.fromListWithKey combine kvsM) == 432 (toAscList $ HM.fromListWithKey combine kvsM) 433 where kvsM = fmap (\(K k,v) -> (Leaf k, Leaf v)) kvs 434 combine k v1 v2 = Op k (Op v1 v2) 435 436pToList :: [(Key, Int)] -> Bool 437pToList = M.toAscList `eq` toAscList 438 439pElems :: [(Key, Int)] -> Bool 440pElems = (L.sort . M.elems) `eq` (L.sort . HM.elems) 441 442pKeys :: [(Key, Int)] -> Bool 443pKeys = (L.sort . M.keys) `eq` (L.sort . HM.keys) 444 445------------------------------------------------------------------------ 446-- * Test list 447 448tests :: [Test] 449tests = 450 [ 451 -- Instances 452 testGroup "instances" 453 [ testProperty "==" pEq 454 , testProperty "/=" pNeq 455 , testProperty "compare reflexive" pOrd1 456 , testProperty "compare transitive" pOrd2 457 , testProperty "compare antisymmetric" pOrd3 458 , testProperty "Ord => Eq" pOrdEq 459 , testProperty "Read/Show" pReadShow 460 , testProperty "Functor" pFunctor 461 , testProperty "Foldable" pFoldable 462 , testProperty "Hashable" pHashable 463 ] 464 -- Basic interface 465 , testGroup "basic interface" 466 [ testProperty "size" pSize 467 , testProperty "member" pMember 468 , testProperty "lookup" pLookup 469 , testProperty "!?" pLookupOperator 470 , testProperty "insert" pInsert 471 , testProperty "delete" pDelete 472 , testProperty "deleteCollision" pDeleteCollision 473 , testProperty "insertWith" pInsertWith 474 , testProperty "adjust" pAdjust 475 , testProperty "updateAdjust" pUpdateAdjust 476 , testProperty "updateDelete" pUpdateDelete 477 , testProperty "alterAdjust" pAlterAdjust 478 , testProperty "alterInsert" pAlterInsert 479 , testProperty "alterDelete" pAlterDelete 480 , testProperty "alterF" pAlterF 481 , testProperty "alterFAdjust" pAlterFAdjust 482 , testProperty "alterFInsert" pAlterFInsert 483 , testProperty "alterFInsertWith" pAlterFInsertWith 484 , testProperty "alterFDelete" pAlterFDelete 485 , testProperty "alterFLookup" pAlterFLookup 486 , testGroup "isSubmapOf" 487 [ testProperty "container compatibility" pSubmap 488 , testProperty "m ⊆ m" pSubmapReflexive 489 , testProperty "m1 ⊆ m1 ∪ m2" pSubmapUnion 490 , testProperty "m1 ⊈ m2 ⇒ m1 ∪ m2 ⊈ m1" pNotSubmapUnion 491 , testProperty "m1\\m2 ⊆ m1" pSubmapDifference 492 , testProperty "m1 ∩ m2 ≠ ∅ ⇒ m1 ⊈ m1\\m2 " pNotSubmapDifference 493 , testProperty "delete k m ⊆ m" pSubmapDelete 494 , testProperty "m ⊈ delete k m " pNotSubmapDelete 495 , testProperty "k ∉ m ⇒ m ⊆ insert k v m" pSubmapInsert 496 , testProperty "k ∉ m ⇒ insert k v m ⊈ m" pNotSubmapInsert 497 ] 498 ] 499 -- Combine 500 , testProperty "union" pUnion 501 , testProperty "unionWith" pUnionWith 502 , testProperty "unionWithKey" pUnionWithKey 503 , testProperty "unions" pUnions 504 -- Transformations 505 , testProperty "map" pMap 506 , testProperty "traverse" pTraverse 507 -- Folds 508 , testGroup "folds" 509 [ testProperty "foldr" pFoldr 510 , testProperty "foldl" pFoldl 511#if MIN_VERSION_base(4,10,0) 512 , testProperty "bifoldMap" pBifoldMap 513 , testProperty "bifoldr" pBifoldr 514 , testProperty "bifoldl" pBifoldl 515#endif 516 , testProperty "foldrWithKey" pFoldrWithKey 517 , testProperty "foldlWithKey" pFoldlWithKey 518 , testProperty "foldrWithKey'" pFoldrWithKey' 519 , testProperty "foldlWithKey'" pFoldlWithKey' 520 , testProperty "foldl'" pFoldl' 521 , testProperty "foldr'" pFoldr' 522 , testProperty "foldMapWithKey" pFoldMapWithKey 523 ] 524 , testGroup "difference and intersection" 525 [ testProperty "difference" pDifference 526 , testProperty "differenceWith" pDifferenceWith 527 , testProperty "intersection" pIntersection 528 , testProperty "intersectionWith" pIntersectionWith 529 , testProperty "intersectionWithKey" pIntersectionWithKey 530 ] 531 -- Filter 532 , testGroup "filter" 533 [ testProperty "filter" pFilter 534 , testProperty "filterWithKey" pFilterWithKey 535 , testProperty "mapMaybe" pMapMaybe 536 , testProperty "mapMaybeWithKey" pMapMaybeWithKey 537 ] 538 -- Conversions 539 , testGroup "conversions" 540 [ testProperty "elems" pElems 541 , testProperty "keys" pKeys 542 , testProperty "fromList" pFromList 543 , testProperty "fromListWith" pFromListWith 544 , testProperty "fromListWithKey" pFromListWithKey 545 , testProperty "toList" pToList 546 ] 547 ] 548 549------------------------------------------------------------------------ 550-- * Model 551 552type Model k v = M.Map k v 553 554-- | Check that a function operating on a 'HashMap' is equivalent to 555-- one operating on a 'Model'. 556eq :: (Eq a, Eq k, Hashable k, Ord k) 557 => (Model k v -> a) -- ^ Function that modifies a 'Model' 558 -> (HM.HashMap k v -> a) -- ^ Function that modified a 'HashMap' in the same 559 -- way 560 -> [(k, v)] -- ^ Initial content of the 'HashMap' and 'Model' 561 -> Bool -- ^ True if the functions are equivalent 562eq f g xs = g (HM.fromList xs) == f (M.fromList xs) 563 564infix 4 `eq` 565 566eq_ :: (Eq k, Eq v, Hashable k, Ord k) 567 => (Model k v -> Model k v) -- ^ Function that modifies a 'Model' 568 -> (HM.HashMap k v -> HM.HashMap k v) -- ^ Function that modified a 569 -- 'HashMap' in the same way 570 -> [(k, v)] -- ^ Initial content of the 'HashMap' 571 -- and 'Model' 572 -> Bool -- ^ True if the functions are 573 -- equivalent 574eq_ f g = (M.toAscList . f) `eq` (toAscList . g) 575 576infix 4 `eq_` 577 578------------------------------------------------------------------------ 579-- * Test harness 580 581main :: IO () 582main = defaultMain tests 583 584------------------------------------------------------------------------ 585-- * Helpers 586 587sortByKey :: Ord k => [(k, v)] -> [(k, v)] 588sortByKey = L.sortBy (compare `on` fst) 589 590toAscList :: Ord k => HM.HashMap k v -> [(k, v)] 591toAscList = L.sortBy (compare `on` fst) . HM.toList 592