module Network.Haskoin.Test.Util
( ArbitraryByteString(..)
, ArbitraryNotNullByteString(..)
, ArbitraryUTCTime(..)
) where
import Test.QuickCheck
( Arbitrary
, Gen
, arbitrary
, choose
, listOf1
)
import Data.Word (Word32)
import Data.Time.Clock (UTCTime(..))
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import qualified Data.ByteString as BS (ByteString, pack, drop)
data ArbitraryByteString = ArbitraryByteString BS.ByteString
deriving (Eq, Show, Read)
instance Arbitrary ArbitraryByteString where
arbitrary = do
bs <- BS.pack `fmap` arbitrary
n <- choose (0, 2)
return $ ArbitraryByteString $ BS.drop n bs
data ArbitraryNotNullByteString = ArbitraryNotNullByteString BS.ByteString
deriving (Eq, Show, Read)
instance Arbitrary ArbitraryNotNullByteString where
arbitrary = do
bs <- BS.pack `fmap` (listOf1 arbitrary)
return $ ArbitraryNotNullByteString bs
newtype ArbitraryUTCTime = ArbitraryUTCTime UTCTime
deriving (Eq, Show, Read)
instance Arbitrary ArbitraryUTCTime where
arbitrary = do
w <- (arbitrary :: Gen Word32)
return $ ArbitraryUTCTime $ posixSecondsToUTCTime $ realToFrac w