File: HashMapLazy.hs

package info (click to toggle)
haskell-unordered-containers 0.2.20-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: haskell: 4,446; makefile: 6
file content (472 lines) | stat: -rw-r--r-- 19,039 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
{-# LANGUAGE CPP                       #-}
{-# LANGUAGE NoMonomorphismRestriction #-}
{-# LANGUAGE PatternSynonyms           #-}
{-# LANGUAGE ScopedTypeVariables       #-}

{-# OPTIONS_GHC -fno-warn-orphans            #-} -- because of Arbitrary (HashMap k v)
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} -- https://github.com/nick8325/quickcheck/issues/344

-- | Tests for "Data.HashMap.Lazy" and "Data.HashMap.Strict".  We test functions by
-- comparing them to @Map@ from @containers@. @Map@ is referred to as the /model/
-- for 'HashMap'

#if defined(STRICT)
#define MODULE_NAME Properties.HashMapStrict
#else
#define MODULE_NAME Properties.HashMapLazy
#endif

module MODULE_NAME (tests) where

import Control.Applicative         (Const (..))
import Data.Bifoldable
import Data.Function               (on)
import Data.Functor.Identity       (Identity (..))
import Data.Hashable               (Hashable (hashWithSalt))
import Data.HashMap.Internal.Debug (Validity (..), valid)
import Data.Ord                    (comparing)
import Test.QuickCheck             (Arbitrary (..), Fun, Property, pattern Fn,
                                    pattern Fn2, pattern Fn3, (===), (==>))
import Test.QuickCheck.Poly        (A, B, C)
import Test.Tasty                  (TestTree, testGroup)
import Test.Tasty.QuickCheck       (testProperty)
import Util.Key                    (Key, incKey, keyToInt)

import qualified Data.Foldable   as Foldable
import qualified Data.List       as List
import qualified Test.QuickCheck as QC

#if defined(STRICT)
import           Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import qualified Data.Map.Strict     as M
#else
import           Data.HashMap.Lazy (HashMap)
import qualified Data.HashMap.Lazy as HM
import qualified Data.Map.Lazy     as M
#endif

instance (Eq k, Hashable k, Arbitrary k, Arbitrary v) => Arbitrary (HashMap k v) where
  arbitrary = HM.fromList <$> arbitrary
  shrink = fmap HM.fromList . shrink . HM.toList

------------------------------------------------------------------------
-- Helpers

type HMK  = HashMap Key
type HMKI = HMK Int

sortByKey :: Ord k => [(k, v)] -> [(k, v)]
sortByKey = List.sortBy (compare `on` fst)

toOrdMap :: Ord k => HashMap k v -> M.Map k v
toOrdMap = M.fromList . HM.toList

isValid :: (Eq k, Hashable k, Show k) => HashMap k v -> Property
isValid m = valid m === Valid

-- The free magma is used to test that operations are applied in the
-- same order.
data Magma a
  = Leaf a
  | Op (Magma a) (Magma a)
  deriving (Show, Eq, Ord)

instance Hashable a => Hashable (Magma a) where
  hashWithSalt s (Leaf a) = hashWithSalt s (hashWithSalt (1::Int) a)
  hashWithSalt s (Op m n) = hashWithSalt s (hashWithSalt (hashWithSalt (2::Int) m) n)

------------------------------------------------------------------------
-- Test list

tests :: TestTree
tests =
  testGroup
#if defined(STRICT)
    "Data.HashMap.Strict"
#else
    "Data.HashMap.Lazy"
#endif
    [
    -- Instances
      testGroup "instances"
      [ testGroup "Eq"
        [ testProperty "==" $
          \(x :: HMKI) y -> (x == y) === (toOrdMap x == toOrdMap y)
        , testProperty "/=" $
          \(x :: HMKI) y -> (x == y) === (toOrdMap x == toOrdMap y)
        ]
      , testGroup "Ord"
        [ testProperty "compare reflexive" $
          \(m :: HMKI) -> compare m m === EQ
        , testProperty "compare transitive" $
          \(x :: HMKI) y z -> case (compare x y, compare y z) of
            (EQ, o)  -> compare x z === o
            (o,  EQ) -> compare x z === o
            (LT, LT) -> compare x z === LT
            (GT, GT) -> compare x z === GT
            (LT, GT) -> QC.property True -- ys greater than xs and zs.
            (GT, LT) -> QC.property True
        , testProperty "compare antisymmetric" $
          \(x :: HMKI) y -> case (compare x y, compare y x) of
            (EQ, EQ) -> True
            (LT, GT) -> True
            (GT, LT) -> True
            _        -> False
        , testProperty "Ord => Eq" $
          \(x :: HMKI) y -> case (compare x y, x == y) of
            (EQ, True)  -> True
            (LT, False) -> True
            (GT, False) -> True
            _           -> False
        ]
      , testProperty "Read/Show" $
        \(x :: HMKI) -> x === read (show x)
      , testProperty "Functor" $
        \(x :: HMKI) (Fn f :: Fun Int Int) ->
          toOrdMap (fmap f x) === fmap f (toOrdMap x)
      , testProperty "Foldable" $
        \(x :: HMKI) ->
          let f = List.sort . Foldable.foldr (:) []
          in  f x === f (toOrdMap x)
      , testGroup "Bifoldable"
        [ testProperty "bifoldMap" $
          \(m :: HMK Key) ->
            bifoldMap (:[]) (:[]) m === concatMap (\(k, v) -> [k, v]) (HM.toList m)
        , testProperty "bifoldr" $
          \(m :: HMK Key) ->
            bifoldr (:) (:) [] m === concatMap (\(k, v) -> [k, v]) (HM.toList m)
        , testProperty "bifoldl" $
          \(m :: HMK Key) ->
            bifoldl (flip (:)) (flip (:)) [] m
            ===
            reverse (concatMap (\(k, v) -> [k, v]) (HM.toList m))
        ]
      , testProperty "Hashable" $
        \(xs :: [(Key, Int)]) is salt ->
          let xs' = List.nubBy (\(k,_) (k',_) -> k == k') xs
              -- Shuffle the list using indexes in the second
              shuffle :: [Int] -> [a] -> [a]
              shuffle idxs = List.map snd
                           . List.sortBy (comparing fst)
                           . List.zip (idxs ++ [List.maximum (0:is) + 1 ..])
              ys = shuffle is xs'
              x = HM.fromList xs'
              y = HM.fromList ys
          in  x == y ==> hashWithSalt salt x === hashWithSalt salt y
      ]
    -- Construction
    , testGroup "empty"
      [ testProperty "valid" $ QC.once $
        isValid (HM.empty :: HMKI)
      ]
    , testGroup "singleton"
      [ testProperty "valid" $
        \(k :: Key) (v :: A) -> isValid (HM.singleton k v)
      ]
    -- Basic interface
    , testProperty "size" $
      \(x :: HMKI) -> HM.size x === M.size (toOrdMap x)
    , testProperty "member" $
      \(k :: Key) (m :: HMKI) -> HM.member k m === M.member k (toOrdMap m)
    , testProperty "lookup" $
      \(k :: Key) (m :: HMKI) -> HM.lookup k m === M.lookup k (toOrdMap m)
    , testProperty "!?" $
      \(k :: Key) (m :: HMKI) -> m HM.!? k === M.lookup k (toOrdMap m)
    , testGroup "insert"
      [ testProperty "model" $
        \(k :: Key) (v :: Int) x ->
          let y = HM.insert k v x
          in  toOrdMap y === M.insert k v (toOrdMap x)
      , testProperty "valid" $
        \(k :: Key) (v :: Int) x -> isValid (HM.insert k v x)
      ]
    , testGroup "insertWith"
      [ testProperty "insertWith" $
        \(Fn2 f) k v (x :: HMKI) ->
          toOrdMap (HM.insertWith f k v x) === M.insertWith f k v (toOrdMap x)
      , testProperty "valid" $
        \(Fn2 f) k v (x :: HMKI) -> isValid (HM.insertWith f k v x)
      ]
    , testGroup "delete"
      [ testProperty "model" $
        \(k :: Key) (x :: HMKI) ->
          let y = HM.delete k x
          in  toOrdMap y === M.delete k (toOrdMap x)
      , testProperty "valid" $
        \(k :: Key) (x :: HMKI) -> isValid (HM.delete k x)
      ]
    , testGroup "adjust" 
      [ testProperty "model" $
        \(Fn f) k (x :: HMKI) ->
          toOrdMap (HM.adjust f k x) === M.adjust f k (toOrdMap x)
      , testProperty "valid" $
        \(Fn f) k (x :: HMKI) -> isValid (HM.adjust f k x)
      ]
    , testGroup "update" 
      [ testProperty "model" $
        \(Fn f) k (x :: HMKI) ->
          toOrdMap (HM.update f k x) === M.update f k (toOrdMap x)
      , testProperty "valid" $
        \(Fn f) k (x :: HMKI) -> isValid (HM.update f k x)
      ]
    , testGroup "alter"
      [ testProperty "model" $
        \(Fn f) k (x :: HMKI) ->
          toOrdMap (HM.alter f k x) === M.alter f k (toOrdMap x)
      , testProperty "valid" $
        \(Fn f) k (x :: HMKI) -> isValid (HM.alter f k x)
      ]
    , testGroup "alterF"
      [ testGroup "model"
        [ -- We choose the list functor here because we don't fuss with
          -- it in alterF rules and because it has a sufficiently interesting
          -- structure to have a good chance of breaking if something is wrong.
          testProperty "[]" $
          \(Fn f :: Fun (Maybe A) [Maybe A]) k (x :: HMK A) ->
            map toOrdMap (HM.alterF f k x) === M.alterF f k (toOrdMap x)
        , testProperty "adjust" $
          \(Fn f) k (x :: HMKI) ->
            let g = Identity . fmap f
            in  fmap toOrdMap (HM.alterF g k x) === M.alterF g k (toOrdMap x)
        , testProperty "insert" $
          \v k (x :: HMKI) ->
            let g = const . Identity . Just $ v
            in  fmap toOrdMap (HM.alterF g k x) === M.alterF g k (toOrdMap x)
        , testProperty "insertWith" $
          \(Fn f) k v (x :: HMKI) ->
            let g = Identity . Just . maybe v f
            in  fmap toOrdMap (HM.alterF g k x) === M.alterF g k (toOrdMap x)
        , testProperty "delete" $
          \k (x :: HMKI) ->
            let f = const (Identity Nothing)
            in  fmap toOrdMap (HM.alterF f k x) === M.alterF f k (toOrdMap x)
        , testProperty "lookup" $
          \(Fn f :: Fun (Maybe A) B) k (x :: HMK A) ->
            let g = Const . f
            in  fmap toOrdMap (HM.alterF g k x) === M.alterF g k (toOrdMap x)
        ]
      , testProperty "valid" $
        \(Fn f :: Fun (Maybe A) [Maybe A]) k (x :: HMK A) ->
          let ys = HM.alterF f k x
          in  map valid ys === (Valid <$ ys)
      ]
    , testGroup "isSubmapOf"
      [ testProperty "model" $
        \(x :: HMKI) y -> HM.isSubmapOf x y === M.isSubmapOf (toOrdMap x) (toOrdMap y)
      , testProperty "m ⊆ m" $
        \(x :: HMKI) -> HM.isSubmapOf x x
      , testProperty "m1 ⊆ m1 ∪ m2" $
        \(x :: HMKI) y -> HM.isSubmapOf x (HM.union x y)
      , testProperty "m1 ⊈ m2  ⇒  m1 ∪ m2 ⊈ m1" $
        \(m1 :: HMKI) m2 -> not (HM.isSubmapOf m1 m2) ==> HM.isSubmapOf m1 (HM.union m1 m2)
      , testProperty "m1\\m2 ⊆ m1" $
        \(m1 :: HMKI) (m2 :: HMKI) -> HM.isSubmapOf (HM.difference m1 m2) m1
      , testProperty "m1 ∩ m2 ≠ ∅  ⇒  m1 ⊈ m1\\m2 " $
        \(m1 :: HMKI) (m2 :: HMKI) ->
          not (HM.null (HM.intersection m1 m2)) ==>
          not (HM.isSubmapOf m1 (HM.difference m1 m2))
      , testProperty "delete k m ⊆ m" $
        \(m :: HMKI) ->
          not (HM.null m) ==>
          QC.forAll (QC.elements (HM.keys m)) $ \k ->
          HM.isSubmapOf (HM.delete k m) m
      , testProperty "m ⊈ delete k m " $
        \(m :: HMKI) ->
          not (HM.null m) ==>
          QC.forAll (QC.elements (HM.keys m)) $ \k ->
          not (HM.isSubmapOf m (HM.delete k m))
      , testProperty "k ∉ m  ⇒  m ⊆ insert k v m" $
        \k v (m :: HMKI) -> not (HM.member k m) ==> HM.isSubmapOf m (HM.insert k v m)
      , testProperty "k ∉ m  ⇒  insert k v m ⊈ m" $
        \k v (m :: HMKI) -> not (HM.member k m) ==> not (HM.isSubmapOf (HM.insert k v m) m)
      ]
    -- Combine
    , testGroup "union"
      [ testProperty "model" $
        \(x :: HMKI) y ->
          let z = HM.union x y
          in  toOrdMap z === M.union (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(x :: HMKI) y -> isValid (HM.union x y)
      ]
    , testGroup "unionWith"
      [ testProperty "model" $
        \(Fn2 f) (x :: HMKI) y ->
          toOrdMap (HM.unionWith f x y) === M.unionWith f (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(Fn2 f) (x :: HMKI) y -> isValid (HM.unionWith f x y)
      ]
    , testGroup "unionWithKey"
      [ testProperty "model" $
        \(Fn3 f) (x :: HMKI) y ->
          toOrdMap (HM.unionWithKey f x y) === M.unionWithKey f (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(Fn3 f) (x :: HMKI) y -> isValid (HM.unionWithKey f x y)
      ]
    , testGroup "unions"
      [ testProperty "model" $
        \(ms :: [HMKI]) -> toOrdMap (HM.unions ms) === M.unions (map toOrdMap ms)
      , testProperty "valid" $
        \(ms :: [HMKI]) -> isValid (HM.unions ms)
      ]
    , testGroup "difference"
      [ testProperty "model" $
        \(x :: HMKI) (y :: HMKI) ->
          toOrdMap (HM.difference x y) === M.difference (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(x :: HMKI) (y :: HMKI) -> isValid (HM.difference x y)
      ]
    , testGroup "differenceWith"
      [ testProperty "model" $
        \(Fn2 f) (x :: HMK A) (y :: HMK B) ->
          toOrdMap (HM.differenceWith f x y) === M.differenceWith f (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(Fn2 f) (x :: HMK A) (y :: HMK B) -> isValid (HM.differenceWith f x y)
      ]
    , testGroup "intersection"
      [ testProperty "model" $
        \(x :: HMKI) (y :: HMKI) ->
          toOrdMap (HM.intersection x y) === M.intersection (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(x :: HMKI) (y :: HMKI) ->
          isValid (HM.intersection x y)
      ]
    , testGroup "intersectionWith"
      [ testProperty "model" $
        \(Fn2 f :: Fun (A, B) C) (x :: HMK A) (y :: HMK B) ->
          toOrdMap (HM.intersectionWith f x y) === M.intersectionWith f (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(Fn2 f :: Fun (A, B) C) (x :: HMK A) (y :: HMK B) ->
          isValid (HM.intersectionWith f x y)
      ]
    , testGroup "intersectionWithKey"
      [ testProperty "model" $
        \(Fn3 f :: Fun (Key, A, B) C) (x :: HMK A) (y :: HMK B) ->
          toOrdMap (HM.intersectionWithKey f x y)
          ===
          M.intersectionWithKey f (toOrdMap x) (toOrdMap y)
      , testProperty "valid" $
        \(Fn3 f :: Fun (Key, A, B) C) (x :: HMK A) (y :: HMK B) ->
          isValid (HM.intersectionWithKey f x y)
      ]
    , testGroup "compose"
      [ testProperty "valid" $
        \(x :: HMK Int) (y :: HMK Key) -> isValid (HM.compose x y)
      ]
    -- Transformations
    , testGroup "map"
      [ testProperty "model" $
        \(Fn f :: Fun A B) (m :: HMK A) -> toOrdMap (HM.map f m) === M.map f (toOrdMap m)
      , testProperty "valid" $
        \(Fn f :: Fun A B) (m :: HMK A) -> isValid (HM.map f m)
      ]
    , testGroup "traverseWithKey"
      [ testProperty "model" $ QC.mapSize (\s -> s `div` 8) $
        \(x :: HMKI) ->
          let f k v = [keyToInt k + v + 1, keyToInt k + v + 2]
              ys = HM.traverseWithKey f x
          in  List.sort (fmap toOrdMap ys) === List.sort (M.traverseWithKey f (toOrdMap x))
      , testProperty "valid" $ QC.mapSize (\s -> s `div` 8) $
        \(x :: HMKI) ->
          let f k v = [keyToInt k + v + 1, keyToInt k + v + 2]
              ys = HM.traverseWithKey f x
          in  fmap valid ys === (Valid <$ ys)
      ]
    , testGroup "mapKeys"
      [ testProperty "model" $
        \(m :: HMKI) -> toOrdMap (HM.mapKeys incKey m) === M.mapKeys incKey (toOrdMap m)
      , testProperty "valid" $
        \(Fn f :: Fun Key Key) (m :: HMKI) -> isValid (HM.mapKeys f m)
      ]
    -- Folds
    , testProperty "foldr" $
      \(m :: HMKI) -> List.sort (HM.foldr (:) [] m) === List.sort (M.foldr (:) [] (toOrdMap m))
    , testProperty "foldl" $
      \(m :: HMKI) ->
        List.sort (HM.foldl (flip (:)) [] m) === List.sort (M.foldl (flip (:)) [] (toOrdMap m))
    , testProperty "foldrWithKey" $
      \(m :: HMKI) ->
        let f k v z = (k, v) : z
        in  sortByKey (HM.foldrWithKey f [] m) === sortByKey (M.foldrWithKey f [] (toOrdMap m))
    , testProperty "foldlWithKey" $
      \(m :: HMKI) ->
        let f z k v = (k, v) : z
        in  sortByKey (HM.foldlWithKey f [] m) === sortByKey (M.foldlWithKey f [] (toOrdMap m))
    , testProperty "foldrWithKey'" $
      \(m :: HMKI) ->
        let f k v z = (k, v) : z
        in  sortByKey (HM.foldrWithKey' f [] m) === sortByKey (M.foldrWithKey' f [] (toOrdMap m))
    , testProperty "foldlWithKey'" $
      \(m :: HMKI) ->
        let f z k v = (k, v) : z
        in  sortByKey (HM.foldlWithKey' f [] m) === sortByKey (M.foldlWithKey' f [] (toOrdMap m))
    , testProperty "foldl'" $
      \(m :: HMKI) ->
        List.sort (HM.foldl' (flip (:)) [] m) === List.sort (M.foldl' (flip (:)) [] (toOrdMap m))
    , testProperty "foldr'" $
      \(m :: HMKI) -> List.sort (HM.foldr' (:) [] m) === List.sort (M.foldr' (:) [] (toOrdMap m))
    , testProperty "foldMapWithKey" $
      \(m :: HMKI) ->
        let f k v = [(k, v)]
        in  sortByKey (HM.foldMapWithKey f m) === sortByKey (M.foldMapWithKey f (toOrdMap m))
    -- Filter
    , testGroup "filter"
      [ testProperty "model" $
        \(Fn p) (m :: HMKI) -> toOrdMap (HM.filter p m) === M.filter p (toOrdMap m)
      , testProperty "valid" $
        \(Fn p) (m :: HMKI) -> isValid (HM.filter p m)
      ]
    , testGroup "filterWithKey"
      [ testProperty "model" $
        \(Fn2 p) (m :: HMKI) ->
          toOrdMap (HM.filterWithKey p m) === M.filterWithKey p (toOrdMap m)
      , testProperty "valid" $
        \(Fn2 p) (m :: HMKI) -> isValid (HM.filterWithKey p m)
      ]
    , testGroup "mapMaybe"
      [ testProperty "model" $
        \(Fn f :: Fun A (Maybe B)) (m :: HMK A) ->
          toOrdMap (HM.mapMaybe f m) === M.mapMaybe f (toOrdMap m)
      , testProperty "valid" $
        \(Fn f :: Fun A (Maybe B)) (m :: HMK A) -> isValid (HM.mapMaybe f m)
      ]
    , testGroup "mapMaybeWithKey"
      [ testProperty "model" $
        \(Fn2 f :: Fun (Key, A) (Maybe B)) (m :: HMK A) ->
          toOrdMap (HM.mapMaybeWithKey f m) === M.mapMaybeWithKey f (toOrdMap m)
      , testProperty "valid" $
        \(Fn2 f :: Fun (Key, A) (Maybe B)) (m :: HMK A) ->
          isValid (HM.mapMaybeWithKey f m)
      ]
    -- Conversions
    , testProperty "elems" $
      \(m :: HMKI) -> List.sort (HM.elems m) === List.sort (M.elems (toOrdMap m))
    , testProperty "keys" $
      \(m :: HMKI) -> List.sort (HM.keys m) === List.sort (M.keys (toOrdMap m))
    , testGroup "fromList"
      [ testProperty "model" $
        \(kvs :: [(Key, Int)]) -> toOrdMap (HM.fromList kvs) === M.fromList kvs
      , testProperty "valid" $
        \(kvs :: [(Key, Int)]) -> isValid (HM.fromList kvs)
      ]
    , testGroup "fromListWith"
      [ testProperty "model" $
        \(kvs :: [(Key, Int)]) ->
          let kvsM = map (fmap Leaf) kvs
          in  toOrdMap (HM.fromListWith Op kvsM) === M.fromListWith Op kvsM
      , testProperty "valid" $
        \(Fn2 f) (kvs :: [(Key, A)]) -> isValid (HM.fromListWith f kvs)
      ]
    , testGroup "fromListWithKey"
      [ testProperty "model" $
        \(kvs :: [(Key, Int)]) ->
          let kvsM = fmap (\(k,v) -> (Leaf (keyToInt k), Leaf v)) kvs
              combine k v1 v2 = Op k (Op v1 v2)
          in  toOrdMap (HM.fromListWithKey combine kvsM) === M.fromListWithKey combine kvsM
      , testProperty "valid" $
        \(Fn3 f) (kvs :: [(Key, A)]) -> isValid (HM.fromListWithKey f kvs)
      ]
    , testProperty "toList" $
      \(m :: HMKI) -> List.sort (HM.toList m) === List.sort (M.toList (toOrdMap m))
    ]