Skip to content

Commit

Permalink
Add testing function genNonEmptyDisjointSet.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanknowles committed May 7, 2024
1 parent 2dc4daf commit 2209fa2
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/test-utils/src/Test/QuickCheck/Extra.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ module Test.QuickCheck.Extra
-- * Partitioning lists
, partitionList

-- * Generating and shrinking sets
, genNonEmptyDisjointSet

-- * Generating and shrinking maps
, genMapWith
, genMapFromKeysWith
Expand Down Expand Up @@ -105,6 +108,7 @@ import Prelude
import Control.Monad
( foldM
, liftM2
, replicateM
)
import Data.IntCast
( intCast
Expand Down Expand Up @@ -144,6 +148,7 @@ import Numeric.Natural
import Test.QuickCheck
( Arbitrary (..)
, Gen
, Positive (getPositive)
, Property
, Testable
, chooseInt
Expand Down Expand Up @@ -595,6 +600,24 @@ shrinkNonEmpty shrinkA = mapMaybe NE.nonEmpty . shrinkList shrinkA . NE.toList
genFunction :: (a -> Gen b -> Gen b) -> Gen b -> Gen (a -> b)
genFunction coarbitraryFn gen = promote (`coarbitraryFn` gen)

--------------------------------------------------------------------------------
-- Generating and shrinking sets
--------------------------------------------------------------------------------

-- | Generates a non-empty 'Set' that is disjoint to an existing 'Set'.
--
-- The size of the resultant set depends on the implicit size parameter.
--
-- Caution: if the given generator is incapable of generating values that are
-- outside the existing set, then this function will not terminate.
--
genNonEmptyDisjointSet :: Ord a => Gen a -> Set a -> Gen (Set a)
genNonEmptyDisjointSet genElement0 existingElements = do
size <- getPositive <$> arbitrary @(Positive Int)
Set.fromList <$> replicateM size genElement
where
genElement = genElement0 `suchThat` (`Set.notMember` existingElements)

--------------------------------------------------------------------------------
-- Generating and shrinking key-value maps
--------------------------------------------------------------------------------
Expand Down

0 comments on commit 2209fa2

Please sign in to comment.