2010-08-17 01:33:18 +04:00
|
|
|
module Main where
|
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
import Data.Binary ( encode, Binary(..) )
|
2010-08-26 17:19:02 +04:00
|
|
|
import Data.Binary.Get ( runGet )
|
|
|
|
import Data.Binary.Put ( runPut )
|
2010-08-17 01:33:18 +04:00
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
import qualified Data.Binary.Get as BG ( getWord8, getWord16be, getWord32be, getWord64be )
|
|
|
|
import qualified Data.Binary.Put as BP ( putWord8, putWord16be, putWord32be, putWord64be )
|
|
|
|
|
2010-08-26 18:41:05 +04:00
|
|
|
import Bits
|
|
|
|
|
2010-08-17 23:30:23 +04:00
|
|
|
import Data.Bits
|
2010-08-26 23:56:27 +04:00
|
|
|
import Data.Monoid
|
2010-08-17 01:33:18 +04:00
|
|
|
import Data.Word
|
2010-08-26 18:41:05 +04:00
|
|
|
import Foreign.Storable
|
2010-08-17 01:33:18 +04:00
|
|
|
import System.Random
|
2010-08-26 23:56:27 +04:00
|
|
|
import System
|
2010-08-17 01:33:18 +04:00
|
|
|
|
2010-08-26 17:19:02 +04:00
|
|
|
import BitsGet
|
|
|
|
import BitsPut
|
2010-08-17 01:33:18 +04:00
|
|
|
|
2010-08-26 23:56:27 +04:00
|
|
|
import Test.Framework.Options ( TestOptions'(..) )
|
|
|
|
import Test.Framework.Providers.QuickCheck2 ( testProperty )
|
2011-06-01 08:26:33 +04:00
|
|
|
import Test.Framework.Runners.Console ( defaultMain )
|
2010-08-26 23:56:27 +04:00
|
|
|
import Test.Framework.Runners.Options ( RunnerOptions'(..) )
|
|
|
|
import Test.Framework ( Test, testGroup )
|
2010-08-17 01:33:18 +04:00
|
|
|
import Test.QuickCheck
|
|
|
|
|
2011-06-01 08:26:33 +04:00
|
|
|
main = defaultMain tests
|
2010-08-26 18:41:05 +04:00
|
|
|
|
2010-08-26 23:56:27 +04:00
|
|
|
tests :: [Test]
|
|
|
|
tests =
|
|
|
|
[ testGroup "Internal test functions"
|
|
|
|
[ testProperty "prop_bitreq" prop_bitreq ]
|
2010-08-26 18:41:05 +04:00
|
|
|
|
2010-08-26 23:56:27 +04:00
|
|
|
, testGroup "Custom test cases"
|
|
|
|
[ testProperty "prop_composite_case" prop_composite_case ]
|
2010-08-26 18:41:05 +04:00
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
, testGroup "prop_bitput_with_get_from_binary"
|
|
|
|
[ testProperty "Word8" (prop_bitput_with_get_from_binary :: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_bitput_with_get_from_binary :: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_bitput_with_get_from_binary :: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_bitput_with_get_from_binary :: [Word64] -> Property)
|
2010-08-27 17:01:09 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
, testGroup "prop_bitget_with_put_from_binary"
|
|
|
|
[ testProperty "Word8" (prop_bitget_with_put_from_binary :: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_bitget_with_put_from_binary :: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_bitget_with_put_from_binary :: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_bitget_with_put_from_binary :: [Word64] -> Property)
|
2010-08-27 17:01:09 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
, testGroup "prop_compare_put_with_naive"
|
|
|
|
[ testProperty "Word8" (prop_compare_put_with_naive :: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_compare_put_with_naive :: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_compare_put_with_naive :: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_compare_put_with_naive :: [Word64] -> Property)
|
2010-08-27 17:01:09 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
, testGroup "prop_compare_get_with_naive"
|
|
|
|
[ testProperty "Word8" (prop_compare_get_with_naive:: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_compare_get_with_naive:: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_compare_get_with_naive:: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_compare_get_with_naive:: [Word64] -> Property)
|
2010-08-27 17:01:09 +04:00
|
|
|
]
|
|
|
|
|
2010-08-26 23:56:27 +04:00
|
|
|
, testGroup "prop_put_with_bitreq"
|
|
|
|
[ testProperty "Word8" (prop_putget_with_bitreq :: Word8 -> Property)
|
|
|
|
, testProperty "Word16" (prop_putget_with_bitreq :: Word16 -> Property)
|
|
|
|
, testProperty "Word32" (prop_putget_with_bitreq :: Word32 -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_putget_with_bitreq :: Word64 -> Property)
|
2010-08-26 23:56:27 +04:00
|
|
|
]
|
2010-08-17 23:30:23 +04:00
|
|
|
|
2010-08-26 23:56:27 +04:00
|
|
|
, testGroup "prop_putget_list_simple"
|
|
|
|
[ testProperty "Bool" (prop_putget_list_simple :: [Bool] -> Property)
|
|
|
|
, testProperty "Word8" (prop_putget_list_simple :: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_putget_list_simple :: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_putget_list_simple :: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_putget_list_simple :: [Word64] -> Property)
|
2010-08-26 23:56:27 +04:00
|
|
|
]
|
|
|
|
|
|
|
|
, testGroup "prop_putget_list_with_bitreq"
|
|
|
|
[ testProperty "Word8" (prop_putget_list_with_bitreq :: [Word8] -> Property)
|
|
|
|
, testProperty "Word16" (prop_putget_list_with_bitreq :: [Word16] -> Property)
|
|
|
|
, testProperty "Word32" (prop_putget_list_with_bitreq :: [Word32] -> Property)
|
2010-08-27 18:20:00 +04:00
|
|
|
, testProperty "Word64" (prop_putget_list_with_bitreq :: [Word64] -> Property)
|
2010-08-26 23:56:27 +04:00
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
{-
|
2010-08-24 22:15:29 +04:00
|
|
|
-- these tests use the R structure
|
|
|
|
--
|
|
|
|
-- quickCheck prop_Word32_from_2_Word16
|
|
|
|
-- quickCheck prop_Word32_from_Word8_and_Word16
|
2010-08-26 23:56:27 +04:00
|
|
|
-}
|
2010-08-24 22:15:29 +04:00
|
|
|
|
2010-08-26 18:41:05 +04:00
|
|
|
prop_putget_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => a -> Property
|
|
|
|
prop_putget_with_bitreq w = property $
|
2010-08-26 17:24:53 +04:00
|
|
|
-- write all words with as many bits as it's required
|
2010-08-26 18:41:05 +04:00
|
|
|
let p = putBits (bitreq w) w
|
|
|
|
g = getBits (bitreq w)
|
2010-08-26 17:19:02 +04:00
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
w' = runGet (runBitGetSimple g) lbs
|
|
|
|
in w == w'
|
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
-- | Write a list of items. Each item is written with the maximum amount of
|
|
|
|
-- bits, i.e. 8 for Word8, 16 for Word16, etc.
|
2010-08-26 18:41:05 +04:00
|
|
|
prop_putget_list_simple :: (BinaryBit a, Eq a, Storable a) => [a] -> Property
|
|
|
|
prop_putget_list_simple ws = property $
|
|
|
|
let s = sizeOf (head ws) * 8
|
|
|
|
p = mapM_ (\v -> putBits s v) ws
|
|
|
|
g = mapM (const (getBits s)) ws
|
2010-08-26 17:19:02 +04:00
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
ws' = runGet (runBitGetSimple g) lbs
|
|
|
|
in ws == ws'
|
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
-- | Write a list of items. Each item is written with exactly as many bits
|
|
|
|
-- as required. Then read it back.
|
2010-08-26 18:41:05 +04:00
|
|
|
prop_putget_list_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => [a] -> Property
|
|
|
|
prop_putget_list_with_bitreq ws = property $
|
2010-08-26 17:24:53 +04:00
|
|
|
-- write all words with as many bits as it's required
|
2010-08-26 18:41:05 +04:00
|
|
|
let p = mapM_ (\v -> putBits (bitreq v) v) ws
|
|
|
|
g = mapM getBits bitlist
|
2010-08-26 17:19:02 +04:00
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
ws' = runGet (runBitGetSimple g) lbs
|
|
|
|
in ws == ws'
|
|
|
|
where
|
|
|
|
bitlist = map bitreq ws
|
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
-- | Write bits using this library, and read them back using the binary
|
|
|
|
-- library.
|
|
|
|
prop_bitput_with_get_from_binary :: (BinaryBit a, Binary a, Storable a, Eq a) => [a] -> Property
|
|
|
|
prop_bitput_with_get_from_binary ws = property $
|
|
|
|
let s = sizeOf (head ws) * 8
|
|
|
|
p = mapM_ (putBits s) ws
|
|
|
|
g = mapM (const get) ws
|
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
ws' = runGet g lbs
|
|
|
|
in ws == ws'
|
|
|
|
|
|
|
|
-- | Write bits using the binary library, and read them back using this
|
|
|
|
-- library.
|
|
|
|
prop_bitget_with_put_from_binary :: (BinaryBit a, Binary a, Storable a, Eq a) => [a] -> Property
|
|
|
|
prop_bitget_with_put_from_binary ws = property $
|
|
|
|
let s = sizeOf (head ws) * 8
|
|
|
|
p = mapM_ put ws
|
|
|
|
g = mapM (const (getBits s)) ws
|
|
|
|
lbs = runPut p
|
|
|
|
ws' = runGet (runBitGetSimple g) lbs
|
|
|
|
in ws == ws'
|
|
|
|
|
2010-08-26 17:19:02 +04:00
|
|
|
-- number of bits required to write 'v'
|
|
|
|
bitreq :: (Num b, Bits a, Ord a) => a -> b
|
2010-08-24 22:07:59 +04:00
|
|
|
bitreq v = fromIntegral . head $ [ req | (req, top) <- bittable, v <= top ]
|
2010-08-26 17:19:02 +04:00
|
|
|
|
|
|
|
bittable :: Bits a => [(Integer, a)]
|
2010-08-26 17:24:53 +04:00
|
|
|
bittable = [ (fromIntegral x, (1 `shiftL` x) - 1) | x <- [1..64] ]
|
2010-08-24 22:07:59 +04:00
|
|
|
|
2010-08-26 18:49:14 +04:00
|
|
|
prop_bitreq :: Word64 -> Property
|
|
|
|
prop_bitreq w = property $
|
|
|
|
( w == 0 && bitreq w == 1 )
|
|
|
|
|| bitreq w == (bitreq (w `shiftR` 1)) + 1
|
2010-08-26 17:19:02 +04:00
|
|
|
|
2010-08-26 18:49:14 +04:00
|
|
|
prop_composite_case :: Bool -> Word16 -> Property
|
|
|
|
prop_composite_case b w = w < 0x8000 ==>
|
2010-08-26 17:19:02 +04:00
|
|
|
let p = do putBool b
|
|
|
|
putWord16be 15 w
|
|
|
|
g = do v <- getBool
|
|
|
|
case v of
|
|
|
|
True -> getWord16be 15
|
|
|
|
False -> do
|
|
|
|
msb <- getWord8 7
|
|
|
|
lsb <- getWord8 8
|
|
|
|
return ((fromIntegral msb `shiftL` 8) .|. fromIntegral lsb)
|
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
w' = runGet (runBitGetSimple g) lbs
|
|
|
|
in w == w'
|
2010-08-24 22:15:29 +04:00
|
|
|
|
2010-08-27 17:01:09 +04:00
|
|
|
|
|
|
|
prop_compare_put_with_naive :: (Bits a, BinaryBit a, Ord a) => [a] -> Property
|
|
|
|
prop_compare_put_with_naive ws = property $
|
|
|
|
let pn = mapM_ (\v -> naive_put (bitreq v) v) ws
|
|
|
|
p = mapM_ (\v -> putBits (bitreq v) v) ws
|
|
|
|
lbs_n = runPut (runBitPut pn)
|
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
in lbs_n == lbs
|
|
|
|
|
|
|
|
prop_compare_get_with_naive :: (Bits a, BinaryBit a, Ord a, Num a) => [a] -> Property
|
|
|
|
prop_compare_get_with_naive ws = property $
|
|
|
|
let gn = mapM (\v -> naive_get (bitreq v)) ws
|
|
|
|
g = mapM (\v -> getBits (bitreq v)) ws
|
|
|
|
p = mapM_ (\v -> naive_put (bitreq v) v) ws
|
|
|
|
lbs = runPut (runBitPut p)
|
|
|
|
rn = runGet (runBitGetSimple gn) lbs
|
|
|
|
r = runGet (runBitGetSimple g ) lbs
|
|
|
|
-- we must help our compiler to resolve the types of 'gn' and 'g'
|
|
|
|
types = rn == ws && r == ws
|
|
|
|
in rn == r
|
|
|
|
|
|
|
|
-- | Write one bit at a time until the full word has been written
|
|
|
|
naive_put :: (Bits a) => Int -> a -> BitPut ()
|
|
|
|
naive_put n w = mapM_ (\b -> putBool (testBit w b)) [n-1,n-2..0]
|
|
|
|
|
|
|
|
-- | Read one bit at a time until we've reconstructed the whole word
|
|
|
|
naive_get :: (Bits a, Num a) => Int -> BitGet a
|
|
|
|
naive_get n0 =
|
|
|
|
let loop 0 acc = return acc
|
|
|
|
loop n acc = do
|
|
|
|
b <- getBool
|
|
|
|
case b of
|
|
|
|
False -> loop (n-1) (acc `shiftL` 1)
|
|
|
|
True -> loop (n-1) ((acc `shiftL` 1) + 1)
|
|
|
|
in loop n0 0
|
|
|
|
|
2010-08-24 22:15:29 +04:00
|
|
|
{-
|
2010-08-17 23:30:23 +04:00
|
|
|
prop_Word32_from_Word8_and_Word16 :: Word8 -> Word16 -> Property
|
|
|
|
prop_Word32_from_Word8_and_Word16 w8 w16 = property $
|
|
|
|
let p = RWord32be 24
|
|
|
|
w' = runGet (get p) lbs
|
|
|
|
in w0 == w'
|
|
|
|
where
|
|
|
|
lbs = runPut (putWord8 w8 >> putWord16be w16)
|
|
|
|
w0 = ((fromIntegral w8) `shiftL` 16) .|. fromIntegral w16
|
|
|
|
|
|
|
|
prop_Word32_from_2_Word16 :: Word16 -> Word16 -> Property
|
|
|
|
prop_Word32_from_2_Word16 w1 w2 = property $
|
|
|
|
let p = RWord32be 32
|
|
|
|
w' = runGet (get p) lbs
|
|
|
|
in w0 == w'
|
|
|
|
where
|
|
|
|
lbs = encode w0
|
|
|
|
w0 = ((fromIntegral w1) `shiftL` 16) .|. fromIntegral w2
|
2010-08-24 22:15:29 +04:00
|
|
|
-}
|
2010-08-17 23:30:23 +04:00
|
|
|
|
2010-08-26 22:30:30 +04:00
|
|
|
|
|
|
|
shrinker :: (Num a, Ord a, Bits a) => a -> [a]
|
|
|
|
shrinker 0 = []
|
|
|
|
shrinker w = [ w `shiftR` 1 -- try to make everything roughly half size
|
|
|
|
] ++ [ w' -- flip bits to zero, left->right
|
|
|
|
| m <- [n, n-1..1]
|
|
|
|
, let w' = w `clearBit` m
|
|
|
|
, w /= w'
|
|
|
|
] ++ [w-1] -- just make it a little smaller
|
|
|
|
where
|
|
|
|
n = bitreq w
|
|
|
|
|
2010-08-17 01:33:18 +04:00
|
|
|
instance Arbitrary Word8 where
|
|
|
|
arbitrary = choose (minBound, maxBound)
|
2010-08-26 22:30:30 +04:00
|
|
|
shrink = shrinker
|
2010-08-17 01:33:18 +04:00
|
|
|
|
|
|
|
instance Arbitrary Word16 where
|
|
|
|
arbitrary = choose (minBound, maxBound)
|
2010-08-26 22:30:30 +04:00
|
|
|
shrink = shrinker
|
2010-08-17 01:33:18 +04:00
|
|
|
|
|
|
|
instance Arbitrary Word32 where
|
|
|
|
arbitrary = choose (minBound, maxBound)
|
2010-08-26 22:30:30 +04:00
|
|
|
shrink = shrinker
|
2010-08-17 01:33:18 +04:00
|
|
|
|
|
|
|
instance Arbitrary Word64 where
|
|
|
|
arbitrary = choose (minBound, maxBound)
|
2010-08-26 22:30:30 +04:00
|
|
|
shrink = shrinker
|
2010-08-17 01:33:18 +04:00
|
|
|
|
|
|
|
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 Random Word where
|
|
|
|
randomR = integralRandomR
|
|
|
|
random = randomR (minBound,maxBound)
|
|
|
|
|
|
|
|
instance Random Word8 where
|
|
|
|
randomR = integralRandomR
|
|
|
|
random = randomR (minBound,maxBound)
|
|
|
|
|
|
|
|
instance Random Word16 where
|
|
|
|
randomR = integralRandomR
|
|
|
|
random = randomR (minBound,maxBound)
|
|
|
|
|
|
|
|
instance Random Word32 where
|
|
|
|
randomR = integralRandomR
|
|
|
|
random = randomR (minBound,maxBound)
|
|
|
|
|
|
|
|
instance Random Word64 where
|
|
|
|
randomR = integralRandomR
|
|
|
|
random = randomR (minBound,maxBound)
|