@@ -9,7 +9,6 @@ import Data.Binary.Put ( runPut )
99import Data.Binary.Bits
1010import Data.Binary.Bits.Get
1111import Data.Binary.Bits.Put
12−import Data.Binary.Bits.BitOrder
1312
1413import qualified Data.ByteString as B
1514import qualified Data.ByteString.Lazy as L
@@ -18,8 +17,7 @@ import Control.Applicative
1817import Data.Bits
1918import Data.Word
2019import Foreign.Storable
21−import Data.Traversable (traverse)
22−import Data.Foldable (traverse_)
20+import System.Random
2321
2422import Test.Framework.Providers.QuickCheck2 ( testProperty )
2523import Test.Framework.Runners.Console ( defaultMain )
@@ -38,10 +36,7 @@ tests =
3836 [ testProperty "prop_composite_case" prop_composite_case ]
3937
4038 , testGroup "getByteString"
41− [ testProperty "prop_getByteString_negative" prop_getByteString_negative
42− , testProperty "prop_putByteString_getByteString" (prop_putByteString_getByteString :: BitOrder -> B.ByteString -> Property)
43− , testProperty "prop_putByteString_getByteString_many" (prop_putByteString_getByteString_many :: BitOrder -> [B.ByteString] -> Property)
44− ]
39+ [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
4540
4641 , testGroup "getLazyByteString"
4742 [ testProperty "getLazyByteString == getByteString"
@@ -89,10 +84,10 @@ tests =
8984 ]
9085
9186 , testGroup "prop_put_with_bitreq"
92− [ testProperty "Word8" (prop_putget_with_bitreq :: BitOrder -> W Word8 -> Property)
93− , testProperty "Word16" (prop_putget_with_bitreq :: BitOrder -> W Word16 -> Property)
94− , testProperty "Word32" (prop_putget_with_bitreq :: BitOrder -> W Word32 -> Property)
95− , testProperty "Word64" (prop_putget_with_bitreq :: BitOrder -> W Word64 -> Property)
87+ [ testProperty "Word8" (prop_putget_with_bitreq :: W Word8 -> Property)
88+ , testProperty "Word16" (prop_putget_with_bitreq :: W Word16 -> Property)
89+ , testProperty "Word32" (prop_putget_with_bitreq :: W Word32 -> Property)
90+ , testProperty "Word64" (prop_putget_with_bitreq :: W Word64 -> Property)
9691 ]
9792
9893 , testGroup "prop_putget_list_simple"
@@ -117,10 +112,10 @@ tests =
117112 , testProperty "Word64" (prop_putget_list_with_bitreq :: W [Word64] -> Property)
118113 ]
119114 , testGroup "prop_bitget_bytestring_interspersed"
120− [ testProperty "Word8" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word8 -> [B.ByteString] -> Property)
121− , testProperty "Word16" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word16 -> [B.ByteString] -> Property)
122− , testProperty "Word32" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word32 -> [B.ByteString] -> Property)
123− , testProperty "Word64" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word64 -> [B.ByteString] -> Property)
115+ [ testProperty "Word8" (prop_bitget_bytestring_interspersed :: W Word8 -> [B.ByteString] -> Property)
116+ , testProperty "Word16" (prop_bitget_bytestring_interspersed :: W Word16 -> [B.ByteString] -> Property)
117+ , testProperty "Word32" (prop_bitget_bytestring_interspersed :: W Word32 -> [B.ByteString] -> Property)
118+ , testProperty "Word64" (prop_bitget_bytestring_interspersed :: W Word64 -> [B.ByteString] -> Property)
124119 ]
125120 , testGroup "Simulate programs"
126121 [ testProperty "primitive" prop_primitive
@@ -163,28 +158,13 @@ prop_getByteString_negative n =
163158 n < 1 ==>
164159 runGet (runBitGet (getByteString n)) L.empty == B.empty
165160
166−prop_putByteString_getByteString :: BitOrder -> B.ByteString -> Property
167−prop_putByteString_getByteString bo bs = property $ bs' == bs
168− where
169− n = B.length bs
170− w = runPut (runBitPut (withBitOrder bo (putByteString bs)))
171− bs' = runGet (runBitGet (withBitOrder bo (getByteString n))) w
172−
173−prop_putByteString_getByteString_many :: BitOrder -> [B.ByteString] -> Property
174−prop_putByteString_getByteString_many bo bs = property $ bs' == bs
175− where
176− n = fmap B.length bs
177− w = runPut (runBitPut (withBitOrder bo (traverse_ putByteString bs)))
178− bs' = runGet (runBitGet (withBitOrder bo (traverse getByteString n))) w
179−
180−
181−prop_putget_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => BitOrder -> W a -> Property
182−prop_putget_with_bitreq bo (W w) = property $
161+prop_putget_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => W a -> Property
162+prop_putget_with_bitreq (W w) = property $
183163 -- write all words with as many bits as it's required
184164 let p = putBits (bitreq w) w
185165 g = getBits (bitreq w)
186− lbs = runPut (runBitPut (withBitOrder bo p))
187− w' = runGet (runBitGet (withBitOrder bo g)) lbs
166+ lbs = runPut (runBitPut p)
167+ w' = runGet (runBitGet g) lbs
188168 in w == w'
189169
190170-- | Write a list of items. Each item is written with the maximum amount of
@@ -246,12 +226,12 @@ prop_bitget_with_put_from_binary (W ws) = property $
246226 in ws == ws'
247227
248228-- | Write each 'ByteString' with a variable sized value as a separator.
249−prop_bitget_bytestring_interspersed :: (BinaryBit a, Binary a, Num a, Ord a, Bits a) => BitOrder -> W a -> [B.ByteString] -> Property
250−prop_bitget_bytestring_interspersed bo (W ws) bss = property $
229+prop_bitget_bytestring_interspersed :: (BinaryBit a, Binary a, Num a, Ord a, Bits a) => W a -> [B.ByteString] -> Property
230+prop_bitget_bytestring_interspersed (W ws) bss = property $
251231 let p = mapM_ (\bs -> putBits (bitreq ws) ws >> putByteString bs) bss
252232 g = mapM (\bs -> (,) <$> getBits (bitreq ws) <*> getByteString (B.length bs)) bss
253− lbs = runPut (runBitPut (withBitOrder bo p))
254− r = runGet (runBitGet (withBitOrder bo g)) lbs
233+ lbs = runPut (runBitPut p)
234+ r = runGet (runBitGet g) lbs
255235 in map (ws,) bss == r
256236
257237-- | Test failing.
@@ -261,8 +241,8 @@ prop_fail lbs errMsg0 = forAll (choose (0, 8 * L.length lbs)) $ \len ->
261241 expectedBytesConsumed
262242 | bits == 0 = bytes
263243 | otherwise = bytes + 1
264− p = do _ <- getByteString (fromIntegral bytes)
265− _ <- getBits (fromIntegral bits) :: BitGet Word8
244+ p = do getByteString (fromIntegral bytes)
245+ getBits (fromIntegral bits) :: BitGet Word8
266246 fail errMsg0
267247 r = runGetIncremental (runBitGet p) `pushChunks` lbs
268248 in case r of
@@ -394,6 +374,11 @@ instance (Arbitrary (W a), Arbitrary (W b), Arbitrary (W c)) => Arbitrary (W (a,
394374 arbitrary = ((W .) .) . (,,) <$> arbitraryW <*> arbitraryW <*> arbitraryW
395375 shrink (W (a,b,c)) = ((W .) .) . (,,) <$> shrinkW a <*> shrinkW b <*> shrinkW c
396376
377+integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
378+integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
379+ fromIntegral b :: Integer) g of
380+ (x,g) -> (fromIntegral x, g)
381+
397382data Primitive
398383 = Bool Bool
399384 | W8 Int Word8
@@ -402,7 +387,6 @@ data Primitive
402387 | W64 Int Word64
403388 | BS Int B.ByteString
404389 | LBS Int L.ByteString
405− | Skip Int
406390 | IsEmpty
407391 deriving (Eq, Show)
408392
@@ -421,7 +405,6 @@ instance Arbitrary Primitive where
421405 , gen W16
422406 , gen W32
423407 , gen W64
424− , Skip <$> choose (0, 3000)
425408 , do n <- choose (0,10)
426409 cs <- vector n
427410 return (BS n (B.pack cs))
@@ -438,19 +421,10 @@ instance Arbitrary Primitive where
438421 W16 _ x -> snk W16 x
439422 W32 _ x -> snk W32 x
440423 W64 _ x -> snk W64 x
441− Skip x -> Skip <$> shrink x
442424 BS _ bs -> let ws = B.unpack bs in map (\ws' -> BS (length ws') (B.pack ws')) (shrink ws)
443425 LBS _ lbs -> let ws = L.unpack lbs in map (\ws' -> LBS (length ws') (L.pack ws')) (shrink ws)
444426 IsEmpty -> []
445427
446−instance Arbitrary BitOrder where
447− arbitrary = elements [BB, LB, LL, BL]
448− shrink LL = [BB,LB,BL]
449− shrink BL = [BB,LB]
450− shrink LB = [BB]
451− shrink BB = []
452−
453−
454428prop_primitive :: Primitive -> Property
455429prop_primitive prim = property $
456430 let p = putPrimitive prim
@@ -475,7 +449,6 @@ putPrimitive p =
475449 W16 n x -> putWord16 n x
476450 W32 n x -> putWord32 n x
477451 W64 n x -> putWord64 n x
478− Skip n -> skipBits n
479452 BS _ bs -> putByteString bs
480453 LBS _ lbs -> mapM_ putByteString (L.toChunks lbs)
481454 IsEmpty -> return ()
@@ -488,22 +461,10 @@ getPrimitive p =
488461 W16 n _ -> W16 n <$> getWord16 n
489462 W32 n _ -> W32 n <$> getWord32 n
490463 W64 n _ -> W64 n <$> getWord64 n
491− Skip n -> skipBits n >> return (Skip n)
492464 BS n _ -> BS n <$> getByteString n
493465 LBS n _ -> LBS n <$> getLazyByteString n
494466 IsEmpty -> isEmpty >> return IsEmpty
495467
496−getPrimitiveSize :: Primitive -> Int
497−getPrimitiveSize p = case p of
498− Bool _ -> 1
499− W8 n _ -> n
500− W16 n _ -> n
501− W32 n _ -> n
502− W64 n _ -> n
503− Skip n -> n
504− BS n _ -> n*8
505− LBS n _ -> n*8
506− IsEmpty -> 0
507468
508469verifyProgram :: Int -> Program -> BitGet Bool
509470verifyProgram totalLength ps0 = go 0 ps0
@@ -511,6 +472,13 @@ verifyProgram totalLength ps0 = go 0 ps0
511472 go _ [] = return True
512473 go pos (p:ps) =
513474 case p of
475+ Bool x -> check x getBool >> go (pos+1) ps
476+ W8 n x -> check x (getWord8 n) >> go (pos+n) ps
477+ W16 n x -> check x (getWord16 n) >> go (pos+n) ps
478+ W32 n x -> check x (getWord32 n) >> go (pos+n) ps
479+ W64 n x -> check x (getWord64 n) >> go (pos+n) ps
480+ BS n x -> check x (getByteString n) >> go (pos+(8*n)) ps
481+ LBS n x -> check x (getLazyByteString n) >> go (pos+(8*n)) ps
514482 IsEmpty -> do
515483 let expected = pos == totalLength
516484 actual <- isEmpty
@@ -518,7 +486,6 @@ verifyProgram totalLength ps0 = go 0 ps0
518486 then go pos ps
519487 else error $ "isEmpty returned wrong value, expected "
520488 ++ show expected ++ " but got " ++ show actual
521− _ -> check p (getPrimitive p) >> go (pos + getPrimitiveSize p) ps
522489 check x g = do
523490 y <- g
524491 if x == y