Skip to content

Commit 3c9c284

Browse files
committed
Clarify Hashable properties in tests
1 parent 2b23b81 commit 3c9c284

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

tests/HashMapProperties.hs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import qualified Data.Foldable as Foldable
1010
import Data.Function (on)
1111
import Data.Hashable (Hashable(hashWithSalt))
1212
import qualified Data.List as L
13+
import Data.Ord (comparing)
1314
#if defined(STRICT)
1415
import qualified Data.HashMap.Strict as HM
1516
#else
@@ -53,9 +54,14 @@ pHashable :: [(Key, Int)] -> [Int] -> Int -> Property
5354
pHashable xs is salt =
5455
x == y ==> hashWithSalt salt x === hashWithSalt salt y
5556
where
56-
ys = L.map snd . L.sort . L.zip (is ++ [L.maximum (0:is) + 1..]) $ xs
57-
x = HM.fromList xs
58-
y = HM.fromList ys
57+
ys = shuffle is xs
58+
x = HM.fromList xs
59+
y = HM.fromList ys
60+
-- Shuffle the list using indexes in the second
61+
shuffle :: [Int] -> [a] -> [a]
62+
shuffle idxs = L.map snd
63+
. L.sortBy (comparing fst)
64+
. L.zip (idxs ++ [L.maximum (0:is) + 1 ..])
5965

6066
------------------------------------------------------------------------
6167
-- ** Basic interface

tests/HashSetProperties.hs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +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 Data.Ord (comparing)
1314
import Test.QuickCheck (Arbitrary, Property, (==>), (===))
1415
import Test.Framework (Test, defaultMain, testGroup)
1516
import Test.Framework.Providers.QuickCheck2 (testProperty)
@@ -43,15 +44,21 @@ pFoldable = (L.sort . Foldable.foldr (:) []) `eq`
4344
pPermutationEq :: [Key] -> [Int] -> Bool
4445
pPermutationEq xs is = S.fromList xs == S.fromList ys
4546
where
46-
ys = L.map snd . L.sort . L.zip (is ++ [L.maximum (0:is) + 1..]) $ xs
47+
ys = shuffle is xs
48+
shuffle idxs = L.map snd
49+
. L.sortBy (comparing fst)
50+
. L.zip (idxs ++ [L.maximum (0:is) + 1 ..])
4751

4852
pHashable :: [Key] -> [Int] -> Int -> Property
4953
pHashable xs is salt =
5054
x == y ==> hashWithSalt salt x === hashWithSalt salt y
5155
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
56+
ys = shuffle is xs
57+
x = S.fromList xs
58+
y = S.fromList ys
59+
shuffle idxs = L.map snd
60+
. L.sortBy (comparing fst)
61+
. L.zip (idxs ++ [L.maximum (0:is) + 1 ..])
5562

5663
------------------------------------------------------------------------
5764
-- ** Basic interface

0 commit comments

Comments
 (0)