Skip to content

Commit 824cca1

Browse files
committed
Use QuickCheck Fun throughout property and strictness tests.
TODO: Fix pInsertWithKeyStrict. [ci skip]
1 parent 9854de3 commit 824cca1

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

tests/HashMapProperties.hs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ import qualified Data.HashMap.Strict as HM
1717
import qualified Data.HashMap.Lazy as HM
1818
#endif
1919
import qualified Data.Map as M
20-
import Test.QuickCheck (Arbitrary, Property, (==>), (===))
21-
import Test.QuickCheck.Function (Fun, apply)
20+
import Test.QuickCheck (Arbitrary, CoArbitrary, Property, (==>), (===))
21+
import Test.QuickCheck.Function (Fun, Function(function), apply, functionMap)
2222
import Test.Framework (Test, defaultMain, testGroup)
2323
import Test.Framework.Providers.QuickCheck2 (testProperty)
2424

2525
-- Key type that generates more hash collisions.
2626
newtype Key = K { unK :: Int }
27-
deriving (Arbitrary, Eq, Ord, Read, Show)
27+
deriving (Arbitrary, CoArbitrary, Eq, Ord, Read, Show)
28+
29+
instance Function Key where
30+
function = functionMap unK K
2831

2932
instance Hashable Key where
3033
hashWithSalt salt k = hashWithSalt salt (unK k) `mod` 20
@@ -153,9 +156,9 @@ pInsertWith f k =
153156
M.insertWith f' k 1 `eq_` HM.insertWith f' k 1
154157
where f' = curry . apply $ f
155158

156-
pInsertWithKey :: Fun (Int, Int, Int) Int -> Key -> [(Key, Int)] -> Bool
159+
pInsertWithKey :: Fun (Key, (Int, Int)) Int -> Key -> [(Key, Int)] -> Bool
157160
pInsertWithKey f k = M.insertWithKey f' k 1 `eq_` HM.insertWithKey f' k 1
158-
where f' k' v1 v2 = apply f (unK k', v1, v2)
161+
where f' = curry . curry (apply f)
159162

160163
pAdjust :: Key -> [(Key, Int)] -> Bool
161164
pAdjust k = M.adjust succ k `eq_` HM.adjust succ k

tests/Strictness.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Data.Hashable (Hashable(hashWithSalt))
77
import Test.ChasingBottoms.IsBottom
88
import Test.Framework (Test, defaultMain, testGroup)
99
import Test.Framework.Providers.QuickCheck2 (testProperty)
10-
import Test.QuickCheck (Arbitrary(arbitrary), Property, (===), (.&&.))
10+
import Test.QuickCheck (Arbitrary(arbitrary), CoArbitrary, Property, (===), (.&&.))
1111
import Test.QuickCheck.Function
1212
import Test.QuickCheck.Poly (A)
1313
import Data.Maybe (fromMaybe, isJust)
@@ -25,7 +25,10 @@ import qualified Data.HashMap.Strict as HM
2525

2626
-- Key type that generates more hash collisions.
2727
newtype Key = K { unK :: Int }
28-
deriving (Arbitrary, Eq, Ord, Show)
28+
deriving (Arbitrary, CoArbitrary, Eq, Ord, Show)
29+
30+
instance Function Key where
31+
function = functionMap unK K
2932

3033
instance Hashable Key where
3134
hashWithSalt salt k = hashWithSalt salt (unK k) `mod` 20
@@ -71,23 +74,25 @@ pInsertKeyStrict v m = isBottom $ HM.insert bottom v m
7174
pInsertValueStrict :: Key -> HashMap Key Int -> Bool
7275
pInsertValueStrict k m = isBottom $ HM.insert k bottom m
7376

74-
pInsertWithKeyStrict :: (Int -> Int -> Int) -> Int -> HashMap Key Int -> Bool
75-
pInsertWithKeyStrict f v m = isBottom $ HM.insertWith f bottom v m
77+
pInsertWithKeyStrict :: Fun (Int, Int) Int -> Int -> HashMap Key Int -> Bool
78+
pInsertWithKeyStrict f v m =
79+
isBottom $ HM.insertWith (curry . apply $ f) bottom v m
7680

77-
pInsertWithValueStrict :: (Int -> Int -> Int) -> Key -> Int -> HashMap Key Int
81+
pInsertWithValueStrict :: Fun (Int, Int) Int -> Key -> Int -> HashMap Key Int
7882
-> Bool
7983
pInsertWithValueStrict f k v m
8084
| HM.member k m = isBottom $ HM.insertWith (const2 bottom) k v m
81-
| otherwise = isBottom $ HM.insertWith f k bottom m
85+
| otherwise = isBottom $ HM.insertWith (curry . apply $ f) k bottom m
8286

83-
pInsertWithKeyKeyStrict :: (Key -> Int -> Int -> Int) -> Int -> HashMap Key Int -> Bool
84-
pInsertWithKeyKeyStrict f v m = isBottom $ HM.insertWithKey f bottom v m
87+
pInsertWithKeyKeyStrict :: Fun (Key, (Int, Int)) Int -> Int -> HashMap Key Int -> Bool
88+
pInsertWithKeyKeyStrict f v m =
89+
isBottom $ HM.insertWithKey (curry . curry (apply f)) bottom v m
8590

86-
pInsertWithKeyValueStrict :: (Key -> Int -> Int -> Int) -> Key -> Int -> HashMap Key Int
91+
pInsertWithKeyValueStrict :: Fun (Key, (Int, Int)) Int -> Key -> Int -> HashMap Key Int
8792
-> Bool
8893
pInsertWithKeyValueStrict f k v m
8994
| HM.member k m = isBottom $ HM.insertWithKey (const2 bottom) k v m
90-
| otherwise = isBottom $ HM.insertWithKey f k bottom m
95+
| otherwise = isBottom $ HM.insertWithKey (curry . curry (apply f)) k bottom m
9196

9297
pFromListKeyStrict :: Bool
9398
pFromListKeyStrict = isBottom $ HM.fromList [(undefined :: Key, 1 :: Int)]

0 commit comments

Comments
 (0)