Derive simple random generators for QuickCheck using generics.
Automating the Arbitrary boilerplate also ensures that when a type changes to
have more or fewer constructors, then the generator either fixes itself to
generate that new case (when using the uniform distribution) or causes a
compilation error so you remember to fix it (when using an explicit
distribution).
This package also offers a simple (optional) strategy to ensure termination for
recursive types:
make Test.QuickCheck.Gen’s size parameter decrease at every recursive call;
when it reaches zero, sample directly from a trivially terminating generator
given explicitly (genericArbitraryRec and withBaseCase) or implicitly
(genericArbitrary').
Example
{-# LANGUAGE DeriveGeneric #-}
import GHC.Generics (Generic)
import Test.QuickCheck
import Generic.Random
data Tree a = Leaf | Node (Tree a) a (Tree a)
deriving (Show, Generic)
instance Arbitrary a => Arbitrary (Tree a) where
arbitrary = genericArbitraryRec uniform `withBaseCase` return Leaf
-- Equivalent to
-- > arbitrary =
-- > sized $ \n ->
-- > if n == 0 then
-- > return Leaf
-- > else
-- > oneof
-- > [ return Leaf
-- > , resize (n `div` 3) $
-- > Node <$> arbitrary <*> arbitrary <*> arbitrary
-- > ]
main :: IO ()
main = sample (arbitrary :: Gen (Tree ()))
Add ConstrGen (custom generators for fields specified by constructor name
and index).
Stop requiring custom generators lists to be terminated by :+ (), or to be
lists at all.
Breaking minor change: when a record field has a different type than
a FieldGen custom generator for the same field name, this is now a
compilation error. This was simply ignored before.
Miscellaneous documentation improvements in Generic.Random module.
1.2.0.0
Fix a bug where generators did not decrease the size parameter with
single-field constructors
The sized generators now use a custom generator for lists.
Use genericArbitraryRecG () to disable that.
See tutorial for more information.
Lists of custom generators are now constructed using (:+) instead of
GenList
Rename Field to FieldGen
Add Gen1, Gen1_ (custom generators for unary type constructors)
Add listOf', listOf1', vectorOf'
Remove deprecated module Generic.Random.Generic
1.1.0.2
Improved performance
1.1.0.1
Fix build for GHC<8
1.1.0.0
Add option to specify custom generators for certain fields,
overriding Arbitrary instances