QuickCheck-2.11.3: Automatic testing of Haskell programs

Safe HaskellSafe
LanguageHaskell98

Test.QuickCheck.Gen

Contents

Description

Test case generation.

Synopsis

Generator type

newtype Gen a Source #

A generator for values of type a.

The third-party package QuickCheck-GenT provides a monad transformer version of GenT.

Constructors

MkGen 

Fields

  • unGen :: QCGen -> Int -> a

    Run the generator on a particular seed. If you just want to get a random value out, consider using generate.

Instances
Monad Gen Source # 
Instance details

Defined in Test.QuickCheck.Gen

Methods

(>>=) :: Gen a -> (a -> Gen b) -> Gen b #

(>>) :: Gen a -> Gen b -> Gen b #

return :: a -> Gen a #

fail :: String -> Gen a #

Functor Gen Source # 
Instance details

Defined in Test.QuickCheck.Gen

Methods

fmap :: (a -> b) -> Gen a -> Gen b #

(<$) :: a -> Gen b -> Gen a #

Applicative Gen Source # 
Instance details

Defined in Test.QuickCheck.Gen

Methods

pure :: a -> Gen a #

(<*>) :: Gen (a -> b) -> Gen a -> Gen b #

liftA2 :: (a -> b -> c) -> Gen a -> Gen b -> Gen c #

(*>) :: Gen a -> Gen b -> Gen b #

(<*) :: Gen a -> Gen b -> Gen a #

Testable prop => Testable (Gen prop) Source # 
Instance details

Defined in Test.QuickCheck.Property

Methods

property :: Gen prop -> Property Source #

Primitive generator combinators

variant :: Integral n => n -> Gen a -> Gen a Source #

Modifies a generator using an integer seed.

sized :: (Int -> Gen a) -> Gen a Source #

Used to construct generators that depend on the size parameter.

For example, listOf, which uses the size parameter as an upper bound on length of lists it generates, can be defined like this:

listOf :: Gen a -> Gen [a]
listOf gen = sized $ \n ->
  do k <- choose (0,n)
     vectorOf k gen

You can also do this using getSize.

getSize :: Gen Int Source #

Generates the size parameter. Used to construct generators that depend on the size parameter.

For example, listOf, which uses the size parameter as an upper bound on length of lists it generates, can be defined like this:

listOf :: Gen a -> Gen [a]
listOf gen = do
  n <- getSize
  k <- choose (0,n)
  vectorOf k gen

You can also do this using sized.

resize :: Int -> Gen a -> Gen a Source #

Overrides the size parameter. Returns a generator which uses the given size instead of the runtime-size parameter.

scale :: (Int -> Int) -> Gen a -> Gen a Source #

Adjust the size parameter, by transforming it with the given function.

choose :: Random a => (a, a) -> Gen a Source #

Generates a random element in the given inclusive range.

chooseAny :: Random a => Gen a Source #

Generates a random element over the natural range of a.

generate :: Gen a -> IO a Source #

Run a generator. The size passed to the generator is always 30; if you want another size then you should explicitly use resize.

sample' :: Gen a -> IO [a] Source #

Generates some example values.

sample :: Show a => Gen a -> IO () Source #

Generates some example values and prints them to stdout.

Common generator combinators

suchThat :: Gen a -> (a -> Bool) -> Gen a Source #

Generates a value that satisfies a predicate.

suchThatMap :: Gen a -> (a -> Maybe b) -> Gen b Source #

Generates a value for which the given function returns a Just, and then applies the function.

suchThatMaybe :: Gen a -> (a -> Bool) -> Gen (Maybe a) Source #

Tries to generate a value that satisfies a predicate. If it fails to do so after enough attempts, returns Nothing.

oneof :: [Gen a] -> Gen a Source #

Randomly uses one of the given generators. The input list must be non-empty.

frequency :: [(Int, Gen a)] -> Gen a Source #

Chooses one of the given generators, with a weighted random distribution. The input list must be non-empty.

elements :: [a] -> Gen a Source #

Generates one of the given values. The input list must be non-empty.

sublistOf :: [a] -> Gen [a] Source #

Generates a random subsequence of the given list.

shuffle :: [a] -> Gen [a] Source #

Generates a random permutation of the given list.

growingElements :: [a] -> Gen a Source #

Takes a list of elements of increasing size, and chooses among an initial segment of the list. The size of this initial segment increases with the size parameter. The input list must be non-empty.

listOf :: Gen a -> Gen [a] Source #

Generates a list of random length. The maximum length depends on the size parameter.

listOf1 :: Gen a -> Gen [a] Source #

Generates a non-empty list of random length. The maximum length depends on the size parameter.

vectorOf :: Int -> Gen a -> Gen [a] Source #

Generates a list of the given length.

infiniteListOf :: Gen a -> Gen [a] Source #

Generates an infinite list.