Test.QuickCheck.Gen

QuickCheck-2.15.0.1: Automatic testing of Haskell programs

Safe Haskell | Safe
Language | Haskell2010

Test.QuickCheck.Gen

Contents

- Generator type
- Primitive generator combinators
- Floating point
- Common generator combinators

Description

Test case generation.

Note: the contents of this module (except for the definition of `Gen`) are re-exported by Test.QuickCheck. You probably do not need to import it directly.

Synopsis

- newtype Gen a = MkGen {
  - unGen :: QCGen -> Int -> a
  }
- variant :: Integral n => n -> Gen a -> Gen a
- sized :: (Int -> Gen a) -> Gen a
- getSize :: Gen Int
- resize :: HasCallStack => Int -> Gen a -> Gen a
- scale :: (Int -> Int) -> Gen a -> Gen a
- choose :: Random a => (a, a) -> Gen a
- chooseAny :: Random a => Gen a
- chooseEnum :: Enum a => (a, a) -> Gen a
- chooseInt :: (Int, Int) -> Gen Int
- chooseBoundedIntegral :: (Bounded a, Integral a) => (a, a) -> Gen a
- chooseInteger :: (Integer, Integer) -> Gen Integer
- chooseWord64 :: (Word64, Word64) -> Gen Word64
- chooseInt64 :: (Int64, Int64) -> Gen Int64
- chooseUpTo :: Word64 -> Gen Word64
- generate :: Gen a -> IO a
- sample' :: Gen a -> IO [a]
- sample :: Show a => Gen a -> IO ()
- genDouble :: Gen Double
- genFloat :: Gen Float
- suchThat :: Gen a -> (a -> Bool) -> Gen a
- suchThatMap :: Gen a -> (a -> Maybe b) -> Gen b
- suchThatMaybe :: Gen a -> (a -> Bool) -> Gen (Maybe a)
- oneof :: HasCallStack => [Gen a] -> Gen a
- frequency :: HasCallStack => [(Int, Gen a)] -> Gen a
- elements :: HasCallStack => [a] -> Gen a
- sublistOf :: [a] -> Gen [a]
- shuffle :: [a] -> Gen [a]
- growingElements :: HasCallStack => [a] -> Gen a
- listOf :: Gen a -> Gen [a]
- listOf1 :: Gen a -> Gen [a]
- vectorOf :: Int -> Gen a -> Gen [a]
- infiniteListOf :: Gen a -> Gen [a]

## Generator type

newtype Gen a #

A generator for values of type `a`.

The third-party packages QuickCheck-GenT and quickcheck-transformer provide monad transformer versions of `Gen`.

Constructors

MkGen |
Fields unGen :: QCGen -> Int -> a Run the generator on a particular seed and size. If you just want to get a random value out, consider using `generate`.

#### Instances

Instances details

Applicative Gen # |
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 #
Functor Gen # |
Instance details Defined in Test.QuickCheck.Gen Methods fmap :: (a -> b) -> Gen a -> Gen b # (<$) :: a -> Gen b -> Gen a #
Monad Gen # |
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 #
MonadFix Gen # |
Instance details Defined in Test.QuickCheck.Gen Methods mfix :: (a -> Gen a) -> Gen a #
Testable prop => Testable (Gen prop) # |
Instance details Defined in Test.QuickCheck.Property Methods property :: Gen prop -> Property # propertyForAllShrinkShow :: Gen a -> (a -> [a]) -> (a -> [String]) -> (a -> Gen prop) -> Property #

## Primitive generator combinators

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

Modifies a generator using an integer seed.

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

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 #

Returns 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 :: HasCallStack => Int -> Gen a -> Gen a #

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 #

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

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

Generates a random element in the given inclusive range. For integral and enumerated types, the specialised variants of `choose` below run much quicker.

chooseAny :: Random a => Gen a #

Generates a random element over the natural range of `a`.

chooseEnum :: Enum a => (a, a) -> Gen a #

A fast implementation of `choose` for enumerated types.

chooseInt :: (Int, Int) -> Gen Int #

A fast implementation of `choose` for `Int`.

chooseBoundedIntegral :: (Bounded a, Integral a) => (a, a) -> Gen a #

A fast implementation of `choose` for bounded integral types.

chooseInteger :: (Integer, Integer) -> Gen Integer #

A fast implementation of `choose` for `Integer`.

chooseWord64 :: (Word64, Word64) -> Gen Word64 #

chooseInt64 :: (Int64, Int64) -> Gen Int64 #

chooseUpTo :: Word64 -> Gen Word64 #

generate :: Gen a -> IO a #

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] #

Generates some example values.

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

Generates some example values and prints them to `stdout`.

## Floating point

genDouble :: Gen Double #

Generate `Double` in 0..1 range

genFloat :: Gen Float #

Generate `Float` in 0..1 range

## Common generator combinators

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

Generates a value that satisfies a predicate.

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

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

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

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

oneof :: HasCallStack => [Gen a] -> Gen a #

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

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

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

elements :: HasCallStack => [a] -> Gen a #

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

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

Generates a random subsequence of the given list.

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

Generates a random permutation of the given list.

growingElements :: HasCallStack => [a] -> Gen a #

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] #

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

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

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

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

Generates a list of the given length.

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

Generates an infinite list.