Skip to content

Commit

Permalink
Add strictness tests for IntMap construction (#1063)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
meooow25 authored Nov 17, 2024
1 parent 171b2e6 commit b24068b
Show file tree
Hide file tree
Showing 4 changed files with 1,050 additions and 141 deletions.
3 changes: 3 additions & 0 deletions containers-tests/containers-tests.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ test-suite map-strictness-properties

other-modules:
Utils.ArbitrarySetMap
Utils.MergeFunc
Utils.Strictness

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

other-modules:
Utils.IsUnit
Utils.MergeFunc
Utils.Strictness

if impl(ghc >= 8.6)
build-depends:
Expand Down
71 changes: 71 additions & 0 deletions containers-tests/tests/Utils/MergeFunc.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
module Utils.MergeFunc
( WhenMatchedFunc(..)
, WhenMissingFunc(..)
) where

import Test.QuickCheck
import Utils.Strictness (Func, Func2, Func3)

-- k: key, x: left map value, y: right map value, z: result map value,
-- a,b: fmaps over the result value. a and b are independent variables to allow
-- for coercions involving Bot. See prop_strictMerge in map-strictness.hs for
-- an example.
data WhenMatchedFunc k x y z a b
= MaybeMatchedFunc (Func3 k x y (Maybe b))
| FmapMaybeMatchedFunc (Func a b) (Func3 k x y (Maybe z))
| MatchedFunc (Func3 k x y b)
| FmapMatchedFunc (Func a b) (Func3 k x y z)
deriving Show

instance
( CoArbitrary k, Function k
, CoArbitrary x, Function x
, CoArbitrary y, Function y
, Arbitrary z
, CoArbitrary a, Function a, Arbitrary a
, Arbitrary b
) => Arbitrary (WhenMatchedFunc k x y z a b) where
arbitrary = oneof
[ MaybeMatchedFunc <$> arbitrary
, FmapMaybeMatchedFunc <$> arbitrary <*> arbitrary
, MatchedFunc <$> arbitrary
, FmapMatchedFunc <$> arbitrary <*> arbitrary
]
shrink wmf = case wmf of
MaybeMatchedFunc fun -> MaybeMatchedFunc <$> shrink fun
FmapMaybeMatchedFunc fun2 fun1 ->
uncurry FmapMaybeMatchedFunc <$> shrink (fun2, fun1)
MatchedFunc fun -> MatchedFunc <$> shrink fun
FmapMatchedFunc fun2 fun1 ->
uncurry FmapMatchedFunc <$> shrink (fun2, fun1)

-- k: key, x: map value, y: result map value, a,b: fmaps over the result value.
-- a and b are independent variables to allow for coercions involving Bot. See
-- prop_strictMerge in map-strictness.hs for an example.
data WhenMissingFunc k x y a b
= MapMaybeMissingFunc (Func2 k x (Maybe b))
| FmapMapMaybeMissingFunc (Func a b) (Func2 k x (Maybe y))
| MapMissingFunc (Func2 k x b)
| FmapMapMissingFunc (Func a b) (Func2 k x y)
deriving Show

instance
( CoArbitrary k, Function k
, CoArbitrary x, Function x
, Arbitrary y
, CoArbitrary a, Function a, Arbitrary a
, Arbitrary b
) => Arbitrary (WhenMissingFunc k x y a b) where
arbitrary = oneof
[ MapMaybeMissingFunc <$> arbitrary
, FmapMapMaybeMissingFunc <$> arbitrary <*> arbitrary
, MapMissingFunc <$> arbitrary
, FmapMapMissingFunc <$> arbitrary <*> arbitrary
]
shrink wmf = case wmf of
MapMaybeMissingFunc fun -> MapMaybeMissingFunc <$> shrink fun
FmapMapMaybeMissingFunc fun2 fun1 ->
uncurry FmapMapMaybeMissingFunc <$> shrink (fun2, fun1)
MapMissingFunc fun -> MapMissingFunc <$> shrink fun
FmapMapMissingFunc fun2 fun1 ->
uncurry FmapMapMissingFunc <$> shrink (fun2, fun1)
Loading

0 comments on commit b24068b

Please sign in to comment.