{-# LANGUAGE CPP, MultiParamTypeClasses,
FlexibleInstances, TypeSynonymInstances #-}
module QuickCheckUtils where
import Test.QuickCheck
import Text.Show.Functions
import Control.Monad ( liftM2 )
import Control.Monad.Instances
import Data.Char
import Data.List
import Data.Word
import Data.Int
import System.Random
import System.IO
import Foreign.C (CChar)
import qualified Data.ByteString as P
import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.Internal as L (checkInvariant,ByteString(..))
import qualified Data.ByteString.Char8 as PC
import qualified Data.ByteString.Lazy.Char8 as LC
adjustSize :: Testable prop => (Int -> Int) -> prop -> Property
adjustSize f p = sized $ \sz -> resize (f sz) (property p)
integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
fromIntegral b :: Integer) g of
(x,g) -> (fromIntegral x, g)
instance Arbitrary L.ByteString where
arbitrary = return . L.checkInvariant
. L.fromChunks
. filter (not. P.null)
=<< arbitrary
instance CoArbitrary L.ByteString where
coarbitrary s = coarbitrary (L.unpack s)
instance Arbitrary P.ByteString where
arbitrary = do
bs <- P.pack `fmap` arbitrary
n <- choose (0, 2)
return (P.drop n bs)
instance CoArbitrary P.ByteString where
coarbitrary s = coarbitrary (P.unpack s)
newtype CByteString = CByteString P.ByteString
deriving Show
instance Arbitrary CByteString where
arbitrary = fmap (CByteString . P.pack . map fromCChar) arbitrary
where
fromCChar :: CChar -> Word8
fromCChar = fromIntegral
instance Arbitrary CChar where
arbitrary = fmap (fromIntegral :: Int -> CChar)
$ oneof [choose (-128,-1), choose (1,127)]
class Model a b where
model :: a -> b
instance Model B P where model = abstr . checkInvariant
instance Model P [W] where model = P.unpack
instance Model P [Char] where model = PC.unpack
instance Model B [W] where model = L.unpack . checkInvariant
instance Model B [Char] where model = LC.unpack . checkInvariant
instance Model Char Word8 where model = fromIntegral . ord
instance Model Bool Bool where model = id
instance Model Int Int where model = id
instance Model P P where model = id
instance Model B B where model = id
instance Model Int64 Int64 where model = id
instance Model Word8 Word8 where model = id
instance Model Ordering Ordering where model = id
instance Model Char Char where model = id
class (Functor f, Functor g) => NatTrans f g where
eta :: f a -> g a
instance NatTrans [] [] where eta = id
instance NatTrans Maybe Maybe where eta = id
instance NatTrans ((->) X) ((->) X) where eta = id
instance NatTrans ((->) Char) ((->) Char) where eta = id
instance NatTrans ((->) W) ((->) W) where eta = id
instance Model f g => NatTrans ((,) f) ((,) g) where eta (f,a) = (model f, a)
instance (NatTrans m n, Model a b) => Model (m a) (n b) where model x = fmap model (eta x)
checkInvariant :: L.ByteString -> L.ByteString
checkInvariant = L.checkInvariant
abstr :: L.ByteString -> P.ByteString
abstr = P.concat . L.toChunks
type X = Int
type W = Word8
type P = P.ByteString
type B = L.ByteString
eq1 f g = \a ->
model (f a) == g (model a)
eq2 f g = \a b ->
model (f a b) == g (model a) (model b)
eq3 f g = \a b c ->
model (f a b c) == g (model a) (model b) (model c)
eqnotnull1 f g = \x -> (not (isNull x)) ==> eq1 f g x
eqnotnull2 f g = \x y -> (not (isNull y)) ==> eq2 f g x y
eqnotnull3 f g = \x y z -> (not (isNull z)) ==> eq3 f g x y z
class IsNull t where isNull :: t -> Bool
instance IsNull L.ByteString where isNull = L.null
instance IsNull P.ByteString where isNull = P.null