Skip to content

Commit

Permalink
[ADP-3272] Add shrinker function shrinkMapToSubmaps. (#4575)
Browse files Browse the repository at this point in the history
This PR adds a shrinker function `shrinkMapToSubmaps` to
`Test.QuickCheck.Extra`:

```hs
-- | Shrinks a 'Map' to list of proper submaps.
--
-- Satisfies the following property:
--
-- @
-- all (`Map.isProperSubmapOf` m) (shrinkMapToSubmaps m)
-- @
--
shrinkMapToSubmaps :: Ord k => Map k v -> [Map k v]
```

### Issue

ADP-3272
  • Loading branch information
jonathanknowles committed May 3, 2024
2 parents cdc59b9 + 6a5992a commit 7c4eaf4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/test-utils/src/Test/QuickCheck/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module Test.QuickCheck.Extra
-- * Generating and shrinking maps
, genMapWith
, genMapFromKeysWith
, shrinkMapToSubmaps
, shrinkMapWith
, shrinkMapValuesWith

Expand Down Expand Up @@ -610,6 +611,21 @@ genMapFromKeysWith :: Ord k => Gen v -> Set k -> Gen (Map k v)
genMapFromKeysWith genValue =
fmap Map.fromList . mapM (\k -> (k,) <$> genValue) . Set.toList

-- | Shrinks a 'Map' to list of proper submaps.
--
-- Satisfies the following property:
--
-- @
-- all (`Map.isProperSubmapOf` m) (shrinkMapToSubmaps m)
-- @
--
shrinkMapToSubmaps :: Ord k => Map k v -> [Map k v]
shrinkMapToSubmaps =
shrinkMapBy Map.fromList Map.toList shrinkListToSublist
where
shrinkListToSublist :: [a] -> [[a]]
shrinkListToSublist = shrinkList (const [])

-- | Shrinks a 'Map' with the given key and value shrinking functions.
--
shrinkMapWith
Expand Down
42 changes: 42 additions & 0 deletions lib/test-utils/test/Test/QuickCheck/ExtraSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import Test.QuickCheck.Extra
, selectMapEntries
, selectMapEntry
, shrinkDisjointPair
, shrinkMapToSubmaps
, shrinkSpace
, shrinkWhile
, shrinkWhileSteps
Expand Down Expand Up @@ -190,6 +191,17 @@ spec = describe "Test.QuickCheck.ExtraSpec" $ do
prop_selectMapEntries_union
@Int @Int & property

describe "Generating and shrinking associative maps" $ do

describe "Shrinking" $ do

it "prop_shrinkMapToSubmaps_all_isProperSubmapOf" $
prop_shrinkMapToSubmaps_all_isProperSubmapOf
@Int @Int & property
it "prop_shrinkMapToSubmaps_unique" $
prop_shrinkMapToSubmaps_unique
@Int @Int & property

describe "Evaluating shrinkers" $ do

describe "Generating sequences of shrunken values" $ do
Expand Down Expand Up @@ -698,6 +710,36 @@ prop_selectMapEntries_union m0 (Positive (Small i)) =
"number of selected entries = 0" $
Map.fromList kvs `Map.union` m1 === m0

--------------------------------------------------------------------------------
-- Shrinking associative maps
--------------------------------------------------------------------------------

prop_shrinkMapToSubmaps_all_isProperSubmapOf
:: (Ord k, Eq v)
=> Map k v
-> Property
prop_shrinkMapToSubmaps_all_isProperSubmapOf m =
all (`Map.isProperSubmapOf` m) (shrinkMapToSubmaps m)
===
True
& cover 10
(length (shrinkMapToSubmaps m) >= 10)
"length (shrinkMapToSubmaps m) >= 10"
& checkCoverage

prop_shrinkMapToSubmaps_unique
:: (Ord k, Ord v)
=> Map k v
-> Property
prop_shrinkMapToSubmaps_unique m =
length (shrinkMapToSubmaps m)
===
length (Set.fromList (shrinkMapToSubmaps m))
& cover 10
(length (shrinkMapToSubmaps m) >= 10)
"length (shrinkMapToSubmaps m) >= 10"
& checkCoverage

--------------------------------------------------------------------------------
-- Generating sequences of shrunken values
--------------------------------------------------------------------------------
Expand Down

0 comments on commit 7c4eaf4

Please sign in to comment.