on master help sign in
Two more QuickCheck tests

* Write a list of bools and read it back * Write a list of Word8s with variable numbers of bits, and read it back

commit53204a2307049deb003cb6bf5fa2770260871e05 authorLennart Kolmodin <kolmodin@gentoo.org> dateTue 24 Aug 2010, 22:07 (16 years ago) parent8897b89
1 file changed+26−1
BitsQC.hs+26−1
BitsQC.hs+26−1
@@ -8,7 +8,8 @@ import Data.Bits
88import Data.Word
99import System.Random
1010
11−import Bits
11+import BitsGet as BG
12+import qualified BitsPut as BP
1213
1314import Test.QuickCheck
1415
@@ -18,6 +19,8 @@ main = do
1819 quickCheck prop_SimpleCase
1920 quickCheck prop_Word32_from_2_Word16
2021 quickCheck prop_Word32_from_Word8_and_Word16
22+ quickCheck prop_Bools
23+ quickCheck prop_Word8_putget
2124
2225prop_SimpleCase :: Word16 -> Property
2326prop_SimpleCase w = w < 0x8000 ==>
@@ -31,6 +34,28 @@ prop_SimpleCase w = w < 0x8000 ==>
3134 where
3235 lbs = runPut (putWord16be w)
3336
37+prop_Word8_putget :: [Word8] -> Property
38+prop_Word8_putget ws = length ws <= fromIntegral (maxBound :: Word8) ==>
39+ -- write all word8s with as many bits as it's required
40+ let p = mapM_ (\v -> BP.putWord8be (bitreq v) v) ws
41+ g = mapM BG.getWord8 bitlist
42+ lbs = runPut (BP.runBitPut p)
43+ Right ws' = runGet (runBitGet g) lbs
44+ in ws == ws'
45+ where
46+ bitlist = map bitreq ws
47+
48+ -- number of bits required to write 'v'
49+bitreq v = fromIntegral . head $ [ req | (req, top) <- bittable, v <= top ]
50+bittable = [ (fromIntegral x, (1 `shiftL` x) - 1) | x <- [1..8] ]
51+
52+prop_Bools :: [Bool] -> Property
53+prop_Bools bs = property $
54+ let p = sequence . replicate (length bs) $ BG.getBool
55+ Right bs' = runGet (BG.runBitGet p) lbs
56+ in bs == bs'
57+ where lbs = runPut $ BP.runBitPut (mapM_ BP.putBool bs)
58+
3459prop_Word16be_with_offset :: Word16 -> Property
3560prop_Word16be_with_offset w = w < 0x8000 ==>
3661 let b :*: w' :*: w'' = runGet (get (RCheck RBool not "fail" `RNextTo` RWord16be 15 `RNextTo` RWord16be 16)) lbs