Skip to content

Commit 0a13436

Browse files
committed
Add Hashable test
Doesn't pass as instance Hashable (HashMap k v) violates (x == y) ==> hashWithSalt salt x == hashWithSalt salt y property.
1 parent 27e9755 commit 0a13436

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

tests/HashSetProperties.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Data.Hashable (Hashable(hashWithSalt))
1010
import qualified Data.List as L
1111
import qualified Data.HashSet as S
1212
import qualified Data.Set as Set
13-
import Test.QuickCheck (Arbitrary)
13+
import Test.QuickCheck (Arbitrary, Property, (==>), (===))
1414
import Test.Framework (Test, defaultMain, testGroup)
1515
import Test.Framework.Providers.QuickCheck2 (testProperty)
1616

@@ -40,6 +40,19 @@ pFoldable :: [Int] -> Bool
4040
pFoldable = (L.sort . Foldable.foldr (:) []) `eq`
4141
(L.sort . Foldable.foldr (:) [])
4242

43+
pPermutationEq :: [Key] -> [Int] -> Bool
44+
pPermutationEq xs is = S.fromList xs == S.fromList ys
45+
where
46+
ys = L.map snd . L.sort . L.zip (is ++ [L.maximum (0:is) + 1..]) $ xs
47+
48+
pHashable :: [Key] -> [Int] -> Int -> Property
49+
pHashable xs is salt =
50+
x == y ==> hashWithSalt salt x === hashWithSalt salt y
51+
where
52+
ys = L.map snd . L.sort . L.zip (is ++ [L.maximum (0:is) + 1..]) $ xs
53+
x = S.fromList xs
54+
y = S.fromList ys
55+
4356
------------------------------------------------------------------------
4457
-- ** Basic interface
4558

@@ -113,9 +126,11 @@ tests =
113126
-- Instances
114127
testGroup "instances"
115128
[ testProperty "==" pEq
129+
, testProperty "Permutation ==" pPermutationEq
116130
, testProperty "/=" pNeq
117131
, testProperty "Read/Show" pReadShow
118132
, testProperty "Foldable" pFoldable
133+
, testProperty "Hashable" pHashable
119134
]
120135
-- Basic interface
121136
, testGroup "basic interface"

0 commit comments

Comments
 (0)