Skip to content

Commit b24068b

Browse files
authored
Add strictness tests for IntMap construction (#1063)
This aims to reduce the chance of introducing strictness bugs. Since we use the same IntMap type for lazy and strict maps, it is not possible to ensure appropriate strictness at the type level. So we turn to property tests. This follows similar tests implemented for Map.
1 parent 171b2e6 commit b24068b

File tree

4 files changed

+1050
-141
lines changed

4 files changed

+1050
-141
lines changed

containers-tests/containers-tests.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ test-suite map-strictness-properties
414414

415415
other-modules:
416416
Utils.ArbitrarySetMap
417+
Utils.MergeFunc
417418
Utils.Strictness
418419

419420
if impl(ghc >= 8.6)
@@ -439,6 +440,8 @@ test-suite intmap-strictness-properties
439440

440441
other-modules:
441442
Utils.IsUnit
443+
Utils.MergeFunc
444+
Utils.Strictness
442445

443446
if impl(ghc >= 8.6)
444447
build-depends:
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module Utils.MergeFunc
2+
( WhenMatchedFunc(..)
3+
, WhenMissingFunc(..)
4+
) where
5+
6+
import Test.QuickCheck
7+
import Utils.Strictness (Func, Func2, Func3)
8+
9+
-- k: key, x: left map value, y: right map value, z: result map value,
10+
-- a,b: fmaps over the result value. a and b are independent variables to allow
11+
-- for coercions involving Bot. See prop_strictMerge in map-strictness.hs for
12+
-- an example.
13+
data WhenMatchedFunc k x y z a b
14+
= MaybeMatchedFunc (Func3 k x y (Maybe b))
15+
| FmapMaybeMatchedFunc (Func a b) (Func3 k x y (Maybe z))
16+
| MatchedFunc (Func3 k x y b)
17+
| FmapMatchedFunc (Func a b) (Func3 k x y z)
18+
deriving Show
19+
20+
instance
21+
( CoArbitrary k, Function k
22+
, CoArbitrary x, Function x
23+
, CoArbitrary y, Function y
24+
, Arbitrary z
25+
, CoArbitrary a, Function a, Arbitrary a
26+
, Arbitrary b
27+
) => Arbitrary (WhenMatchedFunc k x y z a b) where
28+
arbitrary = oneof
29+
[ MaybeMatchedFunc <$> arbitrary
30+
, FmapMaybeMatchedFunc <$> arbitrary <*> arbitrary
31+
, MatchedFunc <$> arbitrary
32+
, FmapMatchedFunc <$> arbitrary <*> arbitrary
33+
]
34+
shrink wmf = case wmf of
35+
MaybeMatchedFunc fun -> MaybeMatchedFunc <$> shrink fun
36+
FmapMaybeMatchedFunc fun2 fun1 ->
37+
uncurry FmapMaybeMatchedFunc <$> shrink (fun2, fun1)
38+
MatchedFunc fun -> MatchedFunc <$> shrink fun
39+
FmapMatchedFunc fun2 fun1 ->
40+
uncurry FmapMatchedFunc <$> shrink (fun2, fun1)
41+
42+
-- k: key, x: map value, y: result map value, a,b: fmaps over the result value.
43+
-- a and b are independent variables to allow for coercions involving Bot. See
44+
-- prop_strictMerge in map-strictness.hs for an example.
45+
data WhenMissingFunc k x y a b
46+
= MapMaybeMissingFunc (Func2 k x (Maybe b))
47+
| FmapMapMaybeMissingFunc (Func a b) (Func2 k x (Maybe y))
48+
| MapMissingFunc (Func2 k x b)
49+
| FmapMapMissingFunc (Func a b) (Func2 k x y)
50+
deriving Show
51+
52+
instance
53+
( CoArbitrary k, Function k
54+
, CoArbitrary x, Function x
55+
, Arbitrary y
56+
, CoArbitrary a, Function a, Arbitrary a
57+
, Arbitrary b
58+
) => Arbitrary (WhenMissingFunc k x y a b) where
59+
arbitrary = oneof
60+
[ MapMaybeMissingFunc <$> arbitrary
61+
, FmapMapMaybeMissingFunc <$> arbitrary <*> arbitrary
62+
, MapMissingFunc <$> arbitrary
63+
, FmapMapMissingFunc <$> arbitrary <*> arbitrary
64+
]
65+
shrink wmf = case wmf of
66+
MapMaybeMissingFunc fun -> MapMaybeMissingFunc <$> shrink fun
67+
FmapMapMaybeMissingFunc fun2 fun1 ->
68+
uncurry FmapMapMaybeMissingFunc <$> shrink (fun2, fun1)
69+
MapMissingFunc fun -> MapMissingFunc <$> shrink fun
70+
FmapMapMissingFunc fun2 fun1 ->
71+
uncurry FmapMapMissingFunc <$> shrink (fun2, fun1)

0 commit comments

Comments
 (0)