Make getLazyByteString read from current byte

This commit is contained in:
Alexander Polakov 2013-03-29 00:15:33 +04:00
parent 50e2ce0d92
commit dc3e9095a4
2 changed files with 27 additions and 4 deletions

View File

@ -43,6 +43,13 @@ tests =
, testGroup "getByteString"
[ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
, testGroup "getLazyByteString"
[ testProperty "getLazyByteString == getByteString"
prop_getLazyByteString_equal_to_ByteString
, testProperty "getLazyByteString == getByteString (with shift)"
prop_getLazyByteString_equal_to_ByteString2
]
, testGroup "Fail"
[ testProperty "monadic fail" prop_fail ]
@ -110,6 +117,17 @@ tests =
]
]
prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
prop_getLazyByteString_equal_to_ByteString bs n =
(fromIntegral n) <= L.length bs ==>
runGet (runBitGet (getLazyByteString (fromIntegral n))) bs ==
(L.fromChunks . (:[]) $ runGet (runBitGet (getByteString n)) bs)
prop_getLazyByteString_equal_to_ByteString2 :: L.ByteString -> Int -> Property
prop_getLazyByteString_equal_to_ByteString2 bs n =
(L.length bs > 1) && (fromIntegral n) < L.length bs ==>
runGet (runBitGet (getWord8 2 >> getLazyByteString (fromIntegral n))) bs ==
(L.fromChunks . (:[]) $ runGet (runBitGet (getWord8 2 >> getByteString n)) bs)
prop_getByteString_negative :: Int -> Property
prop_getByteString_negative n =

View File

@ -416,11 +416,16 @@ getWord64be n = block (word64be n)
getByteString :: Int -> BitGet ByteString
getByteString n = block (byteString n)
-- | Get @n@ bytes as a lazy ByteString.
getLazyByteString :: Int -> BitGet L.ByteString
getLazyByteString m = B $ \ (S n bs) -> do
putBackState n bs
lbs <- B.getLazyByteString (fromIntegral m)
return (S B.empty 0, lbs)
getLazyByteString n = do
(S _ o) <- getState
case o of
0 -> B $ \ (S bs o') -> do
putBackState bs o'
lbs <- B.getLazyByteString (fromIntegral n)
return (S B.empty 0, lbs)
_ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
-- | Read a 1 bit 'Bool'.
bool :: Block Bool