packages feed

HaskellTorrent-0.1.1: src/TestInstance.hs

-- Define a set of test instances of common things used in HaskellTorrent
-- Portions of this code is taken from "Real World Haskell"
module TestInstance
    ()
where

import Data.Word
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L

import System.Random
import Test.QuickCheck

integralRandomR (a,b) g = case randomR (c,d) g of
                            (x,h) -> (fromIntegral x, h)
    where (c,d) = (fromIntegral a :: Integer,
                   fromIntegral b :: Integer)

instance Random Word32 where
  randomR = integralRandomR
  random = randomR (minBound, maxBound)

instance Arbitrary Word32 where
    arbitrary = arbitraryBoundedRandom

instance CoArbitrary Word32 where
    coarbitrary = coarbitraryIntegral

instance Random Word8 where
    randomR = integralRandomR
    random = randomR (minBound, maxBound)

instance Arbitrary Word8 where
    arbitrary = arbitraryBoundedRandom

instance CoArbitrary Word8 where
    coarbitrary = coarbitraryIntegral

instance Arbitrary L.ByteString where
    arbitrary = L.pack `fmap` arbitrary

instance CoArbitrary L.ByteString where
    coarbitrary = coarbitrary . L.unpack

instance Arbitrary B.ByteString where
    arbitrary = B.pack `fmap` arbitrary

instance CoArbitrary B.ByteString where
    coarbitrary = coarbitrary . B.unpack