{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
#ifndef NO_GENERICS
{-# LANGUAGE DefaultSignatures, FlexibleContexts, TypeOperators #-}
{-# LANGUAGE FlexibleInstances, KindSignatures, ScopedTypeVariables #-}
{-# LANGUAGE MultiParamTypeClasses #-}
#if __GLASGOW_HASKELL__ >= 710
#define OVERLAPPING_ {-# OVERLAPPING #-}
#else
{-# LANGUAGE OverlappingInstances #-}
#define OVERLAPPING_
#endif
#endif
#ifndef NO_POLYKINDS
{-# LANGUAGE PolyKinds #-}
#endif
#ifndef NO_SAFE_HASKELL
{-# LANGUAGE Trustworthy #-}
#endif
#ifndef NO_NEWTYPE_DERIVING
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
#endif
module Test.QuickCheck.Arbitrary
(
Arbitrary(..)
, CoArbitrary(..)
, Arbitrary1(..)
, arbitrary1
, shrink1
, Arbitrary2(..)
, arbitrary2
, shrink2
, applyArbitrary2
, applyArbitrary3
, applyArbitrary4
, arbitrarySizedIntegral
, arbitrarySizedNatural
, arbitraryBoundedIntegral
, arbitrarySizedBoundedIntegral
, arbitrarySizedFractional
, arbitraryBoundedRandom
, arbitraryBoundedEnum
, arbitraryUnicodeChar
, arbitraryASCIIChar
, arbitraryPrintableChar
#ifndef NO_GENERICS
, genericShrink
, subterms
, recursivelyShrink
, genericCoarbitrary
#endif
, shrinkNothing
, shrinkList
, shrinkMap
, shrinkMapBy
, shrinkIntegral
, shrinkRealFrac
, shrinkDecimal
, coarbitraryIntegral
, coarbitraryReal
, coarbitraryShow
, coarbitraryEnum
, (><)
, vector
, orderedList
, infiniteList
)
where
import Control.Applicative
import Data.Foldable(toList)
import System.Random(Random)
import Test.QuickCheck.Gen
import Test.QuickCheck.Random
import Test.QuickCheck.Gen.Unsafe
import Data.Char
( ord
, isLower
, isUpper
, toLower
, isDigit
, isSpace
, isPrint
, generalCategory
, GeneralCategory(..)
)
#ifndef NO_FIXED
import Data.Fixed
( Fixed
, HasResolution
)
#endif
import Data.Ratio
( Ratio
, (%)
, numerator
, denominator
)
import Data.Complex
( Complex((:+)) )
import Data.List
( sort
, nub
)
import Data.Version (Version (..))
import Control.Monad
( liftM
, liftM2
, liftM3
, liftM4
, liftM5
)
import Data.Int(Int8, Int16, Int32, Int64)
import Data.Word(Word, Word8, Word16, Word32, Word64)
import System.Exit (ExitCode(..))
import Foreign.C.Types
#ifndef NO_GENERICS
import GHC.Generics
#endif
import qualified Data.Set as Set
import qualified Data.Map as Map
import qualified Data.IntSet as IntSet
import qualified Data.IntMap as IntMap
import qualified Data.Sequence as Sequence
import qualified Data.Monoid as Monoid
#ifndef NO_TRANSFORMERS
import Data.Functor.Identity
import Data.Functor.Constant
import Data.Functor.Compose
import Data.Functor.Product
#endif
class Arbitrary a where
arbitrary :: Gen a
shrink :: a -> [a]
shrink _ = []
class Arbitrary1 f where
liftArbitrary :: Gen a -> Gen (f a)
liftShrink :: (a -> [a]) -> f a -> [f a]
liftShrink _ _ = []
arbitrary1 :: (Arbitrary1 f, Arbitrary a) => Gen (f a)
arbitrary1 = liftArbitrary arbitrary
shrink1 :: (Arbitrary1 f, Arbitrary a) => f a -> [f a]
shrink1 = liftShrink shrink
class Arbitrary2 f where
liftArbitrary2 :: Gen a -> Gen b -> Gen (f a b)
liftShrink2 :: (a -> [a]) -> (b -> [b]) -> f a b -> [f a b]
liftShrink2 _ _ _ = []
arbitrary2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => Gen (f a b)
arbitrary2 = liftArbitrary2 arbitrary arbitrary
shrink2 :: (Arbitrary2 f, Arbitrary a, Arbitrary b) => f a b -> [f a b]
shrink2 = liftShrink2 shrink shrink
#ifndef NO_GENERICS
genericShrink :: (Generic a, RecursivelyShrink (Rep a), GSubterms (Rep a) a) => a -> [a]
genericShrink x = subterms x ++ recursivelyShrink x
recursivelyShrink :: (Generic a, RecursivelyShrink (Rep a)) => a -> [a]
recursivelyShrink = map to . grecursivelyShrink . from
class RecursivelyShrink f where
grecursivelyShrink :: f a -> [f a]
instance (RecursivelyShrink f, RecursivelyShrink g) => RecursivelyShrink (f :*: g) where
grecursivelyShrink (x :*: y) =
[x' :*: y | x' <- grecursivelyShrink x] ++
[x :*: y' | y' <- grecursivelyShrink y]
instance (RecursivelyShrink f, RecursivelyShrink g) => RecursivelyShrink (f :+: g) where
grecursivelyShrink (L1 x) = map L1 (grecursivelyShrink x)
grecursivelyShrink (R1 x) = map R1 (grecursivelyShrink x)
instance RecursivelyShrink f => RecursivelyShrink (M1 i c f) where
grecursivelyShrink (M1 x) = map M1 (grecursivelyShrink x)
instance Arbitrary a => RecursivelyShrink (K1 i a) where
grecursivelyShrink (K1 x) = map K1 (shrink x)
instance RecursivelyShrink U1 where
grecursivelyShrink U1 = []
instance RecursivelyShrink V1 where
grecursivelyShrink _ = []
subterms :: (Generic a, GSubterms (Rep a) a) => a -> [a]
subterms = gSubterms . from
class GSubterms f a where
gSubterms :: f a -> [a]
instance GSubterms V1 a where
gSubterms _ = []
instance GSubterms U1 a where
gSubterms U1 = []
instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :*: g) a where
gSubterms (l :*: r) = gSubtermsIncl l ++ gSubtermsIncl r
instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubterms (f :+: g) a where
gSubterms (L1 x) = gSubtermsIncl x
gSubterms (R1 x) = gSubtermsIncl x
instance GSubterms f a => GSubterms (M1 i c f) a where
gSubterms (M1 x) = gSubterms x
instance GSubterms (K1 i a) b where
gSubterms (K1 _) = []
class GSubtermsIncl f a where
gSubtermsIncl :: f a -> [a]
instance GSubtermsIncl V1 a where
gSubtermsIncl _ = []
instance GSubtermsIncl U1 a where
gSubtermsIncl U1 = []
instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubtermsIncl (f :*: g) a where
gSubtermsIncl (l :*: r) = gSubtermsIncl l ++ gSubtermsIncl r
instance (GSubtermsIncl f a, GSubtermsIncl g a) => GSubtermsIncl (f :+: g) a where
gSubtermsIncl (L1 x) = gSubtermsIncl x
gSubtermsIncl (R1 x) = gSubtermsIncl x
instance GSubtermsIncl f a => GSubtermsIncl (M1 i c f) a where
gSubtermsIncl (M1 x) = gSubtermsIncl x
instance OVERLAPPING_ GSubtermsIncl (K1 i a) a where
gSubtermsIncl (K1 x) = [x]
instance OVERLAPPING_ GSubtermsIncl (K1 i a) b where
gSubtermsIncl (K1 _) = []
#endif
instance (CoArbitrary a) => Arbitrary1 ((->) a) where
liftArbitrary arbB = promote (`coarbitrary` arbB)
instance (CoArbitrary a, Arbitrary b) => Arbitrary (a -> b) where
arbitrary = arbitrary1
instance Arbitrary () where
arbitrary = return ()
instance Arbitrary Bool where
arbitrary = choose (False,True)
shrink True = [False]
shrink False = []
instance Arbitrary Ordering where
arbitrary = elements [LT, EQ, GT]
shrink GT = [EQ, LT]
shrink LT = [EQ]
shrink EQ = []
instance Arbitrary1 Maybe where
liftArbitrary arb = frequency [(1, return Nothing), (3, liftM Just arb)]
liftShrink shr (Just x) = Nothing : [ Just x' | x' <- shr x ]
liftShrink _ Nothing = []
instance Arbitrary a => Arbitrary (Maybe a) where
arbitrary = arbitrary1
shrink = shrink1
instance Arbitrary2 Either where
liftArbitrary2 arbA arbB = oneof [liftM Left arbA, liftM Right arbB]
liftShrink2 shrA _ (Left x) = [ Left x' | x' <- shrA x ]
liftShrink2 _ shrB (Right y) = [ Right y' | y' <- shrB y ]
instance Arbitrary a => Arbitrary1 (Either a) where
liftArbitrary = liftArbitrary2 arbitrary
liftShrink = liftShrink2 shrink
instance (Arbitrary a, Arbitrary b) => Arbitrary (Either a b) where
arbitrary = arbitrary2
shrink = shrink2
instance Arbitrary1 [] where
liftArbitrary = listOf
liftShrink = shrinkList
instance Arbitrary a => Arbitrary [a] where
arbitrary = arbitrary1
shrink = shrink1
shrinkList :: (a -> [a]) -> [a] -> [[a]]
shrinkList shr xs = concat [ removes k n xs | k <- takeWhile (>0) (iterate (`div`2) n) ]
++ shrinkOne xs
where
n = length xs
shrinkOne [] = []
shrinkOne (x:xs) = [ x':xs | x' <- shr x ]
++ [ x:xs' | xs' <- shrinkOne xs ]
removes k n xs
| k > n = []
| null xs2 = [[]]
| otherwise = xs2 : map (xs1 ++) (removes k (n-k) xs2)
where
xs1 = take k xs
xs2 = drop k xs
instance Integral a => Arbitrary (Ratio a) where
arbitrary = arbitrarySizedFractional
shrink = shrinkRealFrac
instance (RealFloat a, Arbitrary a) => Arbitrary (Complex a) where
arbitrary = liftM2 (:+) arbitrary arbitrary
shrink (x :+ y) = [ x' :+ y | x' <- shrink x ] ++
[ x :+ y' | y' <- shrink y ]
#ifndef NO_FIXED
instance HasResolution a => Arbitrary (Fixed a) where
arbitrary = arbitrarySizedFractional
shrink = shrinkDecimal
#endif
instance Arbitrary2 (,) where
liftArbitrary2 = liftM2 (,)
liftShrink2 shrA shrB (x, y) =
[ (x', y) | x' <- shrA x ]
++ [ (x, y') | y' <- shrB y ]
instance (Arbitrary a) => Arbitrary1 ((,) a) where
liftArbitrary = liftArbitrary2 arbitrary
liftShrink = liftShrink2 shrink
instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where
arbitrary = arbitrary2
shrink = shrink2
instance (Arbitrary a, Arbitrary b, Arbitrary c)
=> Arbitrary (a,b,c)
where
arbitrary = liftM3 (,,) arbitrary arbitrary arbitrary
shrink (x, y, z) =
[ (x', y', z')
| (x', (y', z')) <- shrink (x, (y, z)) ]
instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
=> Arbitrary (a,b,c,d)
where
arbitrary = liftM4 (,,,) arbitrary arbitrary arbitrary arbitrary
shrink (w, x, y, z) =
[ (w', x', y', z')
| (w', (x', (y', z'))) <- shrink (w, (x, (y, z))) ]
instance (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e)
=> Arbitrary (a,b,c,d,e)
where
arbitrary = liftM5 (,,,,) arbitrary arbitrary arbitrary arbitrary arbitrary
shrink (v, w, x, y, z) =
[ (v', w', x', y', z')
| (v', (w', (x', (y', z')))) <- shrink (v, (w, (x, (y, z)))) ]
instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
, Arbitrary f
)
=> Arbitrary (a,b,c,d,e,f)
where
arbitrary = return (,,,,,)
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary
shrink (u, v, w, x, y, z) =
[ (u', v', w', x', y', z')
| (u', (v', (w', (x', (y', z'))))) <- shrink (u, (v, (w, (x, (y, z))))) ]
instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
, Arbitrary f, Arbitrary g
)
=> Arbitrary (a,b,c,d,e,f,g)
where
arbitrary = return (,,,,,,)
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary
shrink (t, u, v, w, x, y, z) =
[ (t', u', v', w', x', y', z')
| (t', (u', (v', (w', (x', (y', z')))))) <- shrink (t, (u, (v, (w, (x, (y, z)))))) ]
instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
, Arbitrary f, Arbitrary g, Arbitrary h
)
=> Arbitrary (a,b,c,d,e,f,g,h)
where
arbitrary = return (,,,,,,,)
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
shrink (s, t, u, v, w, x, y, z) =
[ (s', t', u', v', w', x', y', z')
| (s', (t', (u', (v', (w', (x', (y', z')))))))
<- shrink (s, (t, (u, (v, (w, (x, (y, z))))))) ]
instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
, Arbitrary f, Arbitrary g, Arbitrary h, Arbitrary i
)
=> Arbitrary (a,b,c,d,e,f,g,h,i)
where
arbitrary = return (,,,,,,,,)
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary
shrink (r, s, t, u, v, w, x, y, z) =
[ (r', s', t', u', v', w', x', y', z')
| (r', (s', (t', (u', (v', (w', (x', (y', z'))))))))
<- shrink (r, (s, (t, (u, (v, (w, (x, (y, z)))))))) ]
instance ( Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d, Arbitrary e
, Arbitrary f, Arbitrary g, Arbitrary h, Arbitrary i, Arbitrary j
)
=> Arbitrary (a,b,c,d,e,f,g,h,i,j)
where
arbitrary = return (,,,,,,,,,)
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
<*> arbitrary <*> arbitrary
shrink (q, r, s, t, u, v, w, x, y, z) =
[ (q', r', s', t', u', v', w', x', y', z')
| (q', (r', (s', (t', (u', (v', (w', (x', (y', z')))))))))
<- shrink (q, (r, (s, (t, (u, (v, (w, (x, (y, z))))))))) ]
instance Arbitrary Integer where
arbitrary = arbitrarySizedIntegral
shrink = shrinkIntegral
instance Arbitrary Int where
arbitrary = arbitrarySizedIntegral
shrink = shrinkIntegral
instance Arbitrary Int8 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Int16 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Int32 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Int64 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Word where
arbitrary = arbitrarySizedIntegral
shrink = shrinkIntegral
instance Arbitrary Word8 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Word16 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Word32 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Word64 where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary Char where
arbitrary =
frequency
[(3, arbitraryASCIIChar),
(1, arbitraryUnicodeChar)]
shrink c = filter (<. c) $ nub
$ ['a','b','c']
++ [ toLower c | isUpper c ]
++ ['A','B','C']
++ ['1','2','3']
++ [' ','\n']
where
a <. b = stamp a < stamp b
stamp a = ( (not (isLower a)
, not (isUpper a)
, not (isDigit a))
, (not (a==' ')
, not (isSpace a)
, a)
)
instance Arbitrary Float where
arbitrary = arbitrarySizedFractional
shrink = shrinkDecimal
instance Arbitrary Double where
arbitrary = arbitrarySizedFractional
shrink = shrinkDecimal
instance Arbitrary CChar where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CSChar where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CUChar where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CShort where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CUShort where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CInt where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CUInt where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CLong where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CULong where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CPtrdiff where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CSize where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CWchar where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CSigAtomic where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CLLong where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CULLong where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CIntPtr where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CUIntPtr where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CIntMax where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
instance Arbitrary CUIntMax where
arbitrary = arbitrarySizedBoundedIntegral
shrink = shrinkIntegral
#ifndef NO_CTYPES_CONSTRUCTORS
instance Arbitrary CClock where
arbitrary = fmap CClock arbitrary
shrink (CClock x) = map CClock (shrink x)
instance Arbitrary CTime where
arbitrary = fmap CTime arbitrary
shrink (CTime x) = map CTime (shrink x)
#ifndef NO_FOREIGN_C_USECONDS
instance Arbitrary CUSeconds where
arbitrary = fmap CUSeconds arbitrary
shrink (CUSeconds x) = map CUSeconds (shrink x)
instance Arbitrary CSUSeconds where
arbitrary = fmap CSUSeconds arbitrary
shrink (CSUSeconds x) = map CSUSeconds (shrink x)
#endif
#endif
instance Arbitrary CFloat where
arbitrary = arbitrarySizedFractional
shrink = shrinkDecimal
instance Arbitrary CDouble where
arbitrary = arbitrarySizedFractional
shrink = shrinkDecimal
instance (Ord a, Arbitrary a) => Arbitrary (Set.Set a) where
arbitrary = fmap Set.fromList arbitrary
shrink = map Set.fromList . shrink . Set.toList
instance (Ord k, Arbitrary k) => Arbitrary1 (Map.Map k) where
liftArbitrary = fmap Map.fromList . liftArbitrary . liftArbitrary
liftShrink shr = map Map.fromList . liftShrink (liftShrink shr) . Map.toList
instance (Ord k, Arbitrary k, Arbitrary v) => Arbitrary (Map.Map k v) where
arbitrary = arbitrary1
shrink = shrink1
instance Arbitrary IntSet.IntSet where
arbitrary = fmap IntSet.fromList arbitrary
shrink = map IntSet.fromList . shrink . IntSet.toList
instance Arbitrary1 IntMap.IntMap where
liftArbitrary = fmap IntMap.fromList . liftArbitrary . liftArbitrary
liftShrink shr = map IntMap.fromList . liftShrink (liftShrink shr) . IntMap.toList
instance Arbitrary a => Arbitrary (IntMap.IntMap a) where
arbitrary = arbitrary1
shrink = shrink1
instance Arbitrary1 Sequence.Seq where
liftArbitrary = fmap Sequence.fromList . liftArbitrary
liftShrink shr = map Sequence.fromList . liftShrink shr . toList
instance Arbitrary a => Arbitrary (Sequence.Seq a) where
arbitrary = arbitrary1
shrink = shrink1
instance Arbitrary1 ZipList where
liftArbitrary = fmap ZipList . liftArbitrary
liftShrink shr = map ZipList . liftShrink shr . getZipList
instance Arbitrary a => Arbitrary (ZipList a) where
arbitrary = arbitrary1
shrink = shrink1
#ifndef NO_TRANSFORMERS
instance Arbitrary1 Identity where
liftArbitrary = fmap Identity
liftShrink shr = map Identity . shr . runIdentity
instance Arbitrary a => Arbitrary (Identity a) where
arbitrary = arbitrary1
shrink = shrink1
instance Arbitrary2 Constant where
liftArbitrary2 arbA _ = fmap Constant arbA
liftShrink2 shrA _ = fmap Constant . shrA . getConstant
instance Arbitrary a => Arbitrary1 (Constant a) where
liftArbitrary = liftArbitrary2 arbitrary
liftShrink = liftShrink2 shrink
instance Arbitrary a => Arbitrary (Constant a b) where
arbitrary = fmap Constant arbitrary
shrink = map Constant . shrink . getConstant
instance (Arbitrary1 f, Arbitrary1 g) => Arbitrary1 (Product f g) where
liftArbitrary arb = liftM2 Pair (liftArbitrary arb) (liftArbitrary arb)
liftShrink shr (Pair f g) =
[ Pair f' g | f' <- liftShrink shr f ] ++
[ Pair f g' | g' <- liftShrink shr g ]
instance (Arbitrary1 f, Arbitrary1 g, Arbitrary a) => Arbitrary (Product f g a) where
arbitrary = arbitrary1
shrink = shrink1
instance (Arbitrary1 f, Arbitrary1 g) => Arbitrary1 (Compose f g) where
liftArbitrary = fmap Compose . liftArbitrary . liftArbitrary
liftShrink shr = map Compose . liftShrink (liftShrink shr) . getCompose
instance (Arbitrary1 f, Arbitrary1 g, Arbitrary a) => Arbitrary (Compose f g a) where
arbitrary = arbitrary1
shrink = shrink1
#endif
instance Arbitrary2 Const where
liftArbitrary2 arbA _ = fmap Const arbA
liftShrink2 shrA _ = fmap Const . shrA . getConst
instance Arbitrary a => Arbitrary1 (Const a) where
liftArbitrary = liftArbitrary2 arbitrary
liftShrink = liftShrink2 shrink
instance Arbitrary a => Arbitrary (Const a b) where
arbitrary = fmap Const arbitrary
shrink = map Const . shrink . getConst
instance Arbitrary (m a) => Arbitrary (WrappedMonad m a) where
arbitrary = WrapMonad <$> arbitrary
shrink (WrapMonad a) = map WrapMonad (shrink a)
instance Arbitrary (a b c) => Arbitrary (WrappedArrow a b c) where
arbitrary = WrapArrow <$> arbitrary
shrink (WrapArrow a) = map WrapArrow (shrink a)
instance Arbitrary a => Arbitrary (Monoid.Dual a) where
arbitrary = fmap Monoid.Dual arbitrary
shrink = map Monoid.Dual . shrink . Monoid.getDual
instance (Arbitrary a, CoArbitrary a) => Arbitrary (Monoid.Endo a) where
arbitrary = fmap Monoid.Endo arbitrary
shrink = map Monoid.Endo . shrink . Monoid.appEndo
instance Arbitrary Monoid.All where
arbitrary = fmap Monoid.All arbitrary
shrink = map Monoid.All . shrink . Monoid.getAll
instance Arbitrary Monoid.Any where
arbitrary = fmap Monoid.Any arbitrary
shrink = map Monoid.Any . shrink . Monoid.getAny
instance Arbitrary a => Arbitrary (Monoid.Sum a) where
arbitrary = fmap Monoid.Sum arbitrary
shrink = map Monoid.Sum . shrink . Monoid.getSum
instance Arbitrary a => Arbitrary (Monoid.Product a) where
arbitrary = fmap Monoid.Product arbitrary
shrink = map Monoid.Product . shrink . Monoid.getProduct
#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(3,0,0)
instance Arbitrary a => Arbitrary (Monoid.First a) where
arbitrary = fmap Monoid.First arbitrary
shrink = map Monoid.First . shrink . Monoid.getFirst
instance Arbitrary a => Arbitrary (Monoid.Last a) where
arbitrary = fmap Monoid.Last arbitrary
shrink = map Monoid.Last . shrink . Monoid.getLast
#endif
#if MIN_VERSION_base(4,8,0)
instance Arbitrary (f a) => Arbitrary (Monoid.Alt f a) where
arbitrary = fmap Monoid.Alt arbitrary
shrink = map Monoid.Alt . shrink . Monoid.getAlt
#endif
#endif
instance Arbitrary Version where
arbitrary = sized $ \n ->
do k <- choose (0, log2 n)
xs <- vectorOf (k+1) arbitrarySizedNatural
return (Version xs [])
where
log2 :: Int -> Int
log2 n | n <= 1 = 0
| otherwise = 1 + log2 (n `div` 2)
shrink (Version xs _) =
[ Version xs' []
| xs' <- shrink xs
, length xs' > 0
, all (>=0) xs'
]
instance Arbitrary QCGen where
arbitrary = MkGen (\g _ -> g)
instance Arbitrary ExitCode where
arbitrary = frequency [(1, return ExitSuccess), (3, liftM ExitFailure arbitrary)]
shrink (ExitFailure x) = ExitSuccess : [ ExitFailure x' | x' <- shrink x ]
shrink _ = []
applyArbitrary2 :: (Arbitrary a, Arbitrary b) => (a -> b -> r) -> Gen r
applyArbitrary2 f = liftA2 f arbitrary arbitrary
applyArbitrary3
:: (Arbitrary a, Arbitrary b, Arbitrary c)
=> (a -> b -> c -> r) -> Gen r
applyArbitrary3 f = liftA3 f arbitrary arbitrary arbitrary
applyArbitrary4
:: (Arbitrary a, Arbitrary b, Arbitrary c, Arbitrary d)
=> (a -> b -> c -> d -> r) -> Gen r
applyArbitrary4 f = applyArbitrary3 (uncurry f)
arbitrarySizedIntegral :: Integral a => Gen a
arbitrarySizedIntegral =
sized $ \n ->
inBounds fromInteger (choose (-toInteger n, toInteger n))
arbitrarySizedNatural :: Integral a => Gen a
arbitrarySizedNatural =
sized $ \n ->
inBounds fromInteger (choose (0, toInteger n))
inBounds :: Integral a => (Integer -> a) -> Gen Integer -> Gen a
inBounds fi g = fmap fi (g `suchThat` (\x -> toInteger (fi x) == x))
arbitrarySizedFractional :: Fractional a => Gen a
arbitrarySizedFractional =
sized $ \n ->
let n' = toInteger n in
do b <- choose (1, precision)
a <- choose ((-n') * b, n' * b)
return (fromRational (a % b))
where
precision = 9999999999999 :: Integer
withBounds :: Bounded a => (a -> a -> Gen a) -> Gen a
withBounds k = k minBound maxBound
arbitraryBoundedIntegral :: (Bounded a, Integral a) => Gen a
arbitraryBoundedIntegral =
withBounds $ \mn mx ->
do n <- choose (toInteger mn, toInteger mx)
return (fromInteger n)
arbitraryBoundedRandom :: (Bounded a, Random a) => Gen a
arbitraryBoundedRandom = choose (minBound,maxBound)
arbitraryBoundedEnum :: (Bounded a, Enum a) => Gen a
arbitraryBoundedEnum =
withBounds $ \mn mx ->
do n <- choose (fromEnum mn, fromEnum mx)
return (toEnum n)
arbitrarySizedBoundedIntegral :: (Bounded a, Integral a) => Gen a
arbitrarySizedBoundedIntegral =
withBounds $ \mn mx ->
sized $ \s ->
do let bits n | n == 0 = 0
| otherwise = 1 + bits (n `quot` 2)
k = (toInteger s*(bits mn `max` bits mx `max` 40) `div` 80)
x `minexp` k
| bits x < k = x
| otherwise = x `min` (2^k)
x `maxexpneg` k = -((-x) `minexp` k)
n <- choose (toInteger mn `maxexpneg` k, toInteger mx `minexp` k)
return (fromInteger n)
arbitraryUnicodeChar :: Gen Char
arbitraryUnicodeChar =
arbitraryBoundedEnum `suchThat` (not . isSurrogate)
where
isSurrogate c = generalCategory c == Surrogate
arbitraryASCIIChar :: Gen Char
arbitraryASCIIChar = choose ('\0', '\127')
arbitraryPrintableChar :: Gen Char
arbitraryPrintableChar = arbitrary `suchThat` isPrint
shrinkNothing :: a -> [a]
shrinkNothing _ = []
shrinkMap :: Arbitrary a => (a -> b) -> (b -> a) -> b -> [b]
shrinkMap f g = shrinkMapBy f g shrink
shrinkMapBy :: (a -> b) -> (b -> a) -> (a -> [a]) -> b -> [b]
shrinkMapBy f g shr = map f . shr . g
shrinkIntegral :: Integral a => a -> [a]
shrinkIntegral x =
nub $
[ -x
| x < 0, -x > x
] ++
[ x'
| x' <- takeWhile (<< x) (0:[ x - i | i <- tail (iterate (`quot` 2) x) ])
]
where
a << b = case (a >= 0, b >= 0) of
(True, True) -> a < b
(False, False) -> a > b
(True, False) -> a + b < 0
(False, True) -> a + b > 0
shrinkRealFrac :: RealFrac a => a -> [a]
shrinkRealFrac x
| not (x == x) = 0 : take 10 (iterate (*2) 0)
| not (2*x+1>x) = 0 : takeWhile (<x) (iterate (*2) 0)
| x < 0 = negate x:map negate (shrinkRealFrac (negate x))
| otherwise =
filter (\y -> abs y < abs x) $
map fromInteger (shrink (truncate x) ++ [truncate x]) ++
[fromRational (num' % denom) | num' <- shrink num] ++
[fromRational (truncate (num * denom' % denom) % denom')
| denom' <- shrink denom, denom' /= 0 ]
where
num = numerator (toRational x)
denom = denominator (toRational x)
shrinkDecimal :: RealFrac a => a -> [a]
shrinkDecimal x
| not (x == x) = 0 : take 10 (iterate (*2) 0)
| not (2*x+1>x) = 0 : takeWhile (<x) (iterate (*2) 0)
| otherwise =
[ y
| precision <- take 6 (iterate (*10) 1),
let m = round (toRational x * precision),
m `mod` 10 /= 0,
n <- m:shrink m,
let y = fromRational (fromInteger n / precision),
abs y < abs x ]
#ifndef NO_GENERICS
#else
#endif
class CoArbitrary a where
coarbitrary :: a -> Gen b -> Gen b
#ifndef NO_GENERICS
default coarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
coarbitrary = genericCoarbitrary
genericCoarbitrary :: (Generic a, GCoArbitrary (Rep a)) => a -> Gen b -> Gen b
genericCoarbitrary = gCoarbitrary . from
class GCoArbitrary f where
gCoarbitrary :: f a -> Gen b -> Gen b
instance GCoArbitrary U1 where
gCoarbitrary U1 = id
instance (GCoArbitrary f, GCoArbitrary g) => GCoArbitrary (f :*: g) where
gCoarbitrary (l :*: r) = gCoarbitrary l . gCoarbitrary r
instance (GCoArbitrary f, GCoArbitrary g) => GCoArbitrary (f :+: g) where
gCoarbitrary (L1 x) = variant 0 . gCoarbitrary x
gCoarbitrary (R1 x) = variant 1 . gCoarbitrary x
instance GCoArbitrary f => GCoArbitrary (M1 i c f) where
gCoarbitrary (M1 x) = gCoarbitrary x
instance CoArbitrary a => GCoArbitrary (K1 i a) where
gCoarbitrary (K1 x) = coarbitrary x
#endif
{-# DEPRECATED (><) "Use ordinary function composition instead" #-}
(><) :: (Gen a -> Gen a) -> (Gen a -> Gen a) -> (Gen a -> Gen a)
(><) = (.)
instance (Arbitrary a, CoArbitrary b) => CoArbitrary (a -> b) where
coarbitrary f gen =
do xs <- arbitrary
coarbitrary (map f xs) gen
instance CoArbitrary () where
coarbitrary _ = id
instance CoArbitrary Bool where
coarbitrary False = variant 0
coarbitrary True = variant 1
instance CoArbitrary Ordering where
coarbitrary GT = variant 0
coarbitrary EQ = variant 1
coarbitrary LT = variant 2
instance CoArbitrary a => CoArbitrary (Maybe a) where
coarbitrary Nothing = variant 0
coarbitrary (Just x) = variant 1 . coarbitrary x
instance (CoArbitrary a, CoArbitrary b) => CoArbitrary (Either a b) where
coarbitrary (Left x) = variant 0 . coarbitrary x
coarbitrary (Right y) = variant 1 . coarbitrary y
instance CoArbitrary a => CoArbitrary [a] where
coarbitrary [] = variant 0
coarbitrary (x:xs) = variant 1 . coarbitrary (x,xs)
instance (Integral a, CoArbitrary a) => CoArbitrary (Ratio a) where
coarbitrary r = coarbitrary (numerator r,denominator r)
#ifndef NO_FIXED
instance HasResolution a => CoArbitrary (Fixed a) where
coarbitrary = coarbitraryReal
#endif
instance (RealFloat a, CoArbitrary a) => CoArbitrary (Complex a) where
coarbitrary (x :+ y) = coarbitrary x . coarbitrary y
instance (CoArbitrary a, CoArbitrary b)
=> CoArbitrary (a,b)
where
coarbitrary (x,y) = coarbitrary x
. coarbitrary y
instance (CoArbitrary a, CoArbitrary b, CoArbitrary c)
=> CoArbitrary (a,b,c)
where
coarbitrary (x,y,z) = coarbitrary x
. coarbitrary y
. coarbitrary z
instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d)
=> CoArbitrary (a,b,c,d)
where
coarbitrary (x,y,z,v) = coarbitrary x
. coarbitrary y
. coarbitrary z
. coarbitrary v
instance (CoArbitrary a, CoArbitrary b, CoArbitrary c, CoArbitrary d, CoArbitrary e)
=> CoArbitrary (a,b,c,d,e)
where
coarbitrary (x,y,z,v,w) = coarbitrary x
. coarbitrary y
. coarbitrary z
. coarbitrary v
. coarbitrary w
instance CoArbitrary Integer where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Int where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Int8 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Int16 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Int32 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Int64 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Word where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Word8 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Word16 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Word32 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Word64 where
coarbitrary = coarbitraryIntegral
instance CoArbitrary Char where
coarbitrary = coarbitrary . ord
instance CoArbitrary Float where
coarbitrary = coarbitraryReal
instance CoArbitrary Double where
coarbitrary = coarbitraryReal
instance CoArbitrary a => CoArbitrary (Set.Set a) where
coarbitrary = coarbitrary. Set.toList
instance (CoArbitrary k, CoArbitrary v) => CoArbitrary (Map.Map k v) where
coarbitrary = coarbitrary . Map.toList
instance CoArbitrary IntSet.IntSet where
coarbitrary = coarbitrary . IntSet.toList
instance CoArbitrary a => CoArbitrary (IntMap.IntMap a) where
coarbitrary = coarbitrary . IntMap.toList
instance CoArbitrary a => CoArbitrary (Sequence.Seq a) where
coarbitrary = coarbitrary . toList
instance CoArbitrary a => CoArbitrary (ZipList a) where
coarbitrary = coarbitrary . getZipList
#ifndef NO_TRANSFORMERS
instance CoArbitrary a => CoArbitrary (Identity a) where
coarbitrary = coarbitrary . runIdentity
instance CoArbitrary a => CoArbitrary (Constant a b) where
coarbitrary = coarbitrary . getConstant
#endif
instance CoArbitrary a => CoArbitrary (Const a b) where
coarbitrary = coarbitrary . getConst
instance CoArbitrary a => CoArbitrary (Monoid.Dual a) where
coarbitrary = coarbitrary . Monoid.getDual
instance (Arbitrary a, CoArbitrary a) => CoArbitrary (Monoid.Endo a) where
coarbitrary = coarbitrary . Monoid.appEndo
instance CoArbitrary Monoid.All where
coarbitrary = coarbitrary . Monoid.getAll
instance CoArbitrary Monoid.Any where
coarbitrary = coarbitrary . Monoid.getAny
instance CoArbitrary a => CoArbitrary (Monoid.Sum a) where
coarbitrary = coarbitrary . Monoid.getSum
instance CoArbitrary a => CoArbitrary (Monoid.Product a) where
coarbitrary = coarbitrary . Monoid.getProduct
#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(3,0,0)
instance CoArbitrary a => CoArbitrary (Monoid.First a) where
coarbitrary = coarbitrary . Monoid.getFirst
instance CoArbitrary a => CoArbitrary (Monoid.Last a) where
coarbitrary = coarbitrary . Monoid.getLast
#endif
#if MIN_VERSION_base(4,8,0)
instance CoArbitrary (f a) => CoArbitrary (Monoid.Alt f a) where
coarbitrary = coarbitrary . Monoid.getAlt
#endif
#endif
instance CoArbitrary Version where
coarbitrary (Version a b) = coarbitrary (a, b)
coarbitraryIntegral :: Integral a => a -> Gen b -> Gen b
coarbitraryIntegral = variant
coarbitraryReal :: Real a => a -> Gen b -> Gen b
coarbitraryReal x = coarbitrary (toRational x)
coarbitraryShow :: Show a => a -> Gen b -> Gen b
coarbitraryShow x = coarbitrary (show x)
coarbitraryEnum :: Enum a => a -> Gen b -> Gen b
coarbitraryEnum = variant . fromEnum
vector :: Arbitrary a => Int -> Gen [a]
vector k = vectorOf k arbitrary
orderedList :: (Ord a, Arbitrary a) => Gen [a]
orderedList = sort `fmap` arbitrary
infiniteList :: Arbitrary a => Gen [a]
infiniteList = infiniteListOf arbitrary