mirror of
https://github.com/ilyakooo0/binary-bits.git
synced 2024-09-11 06:35:26 +03:00
Make getLazyByteString read from current byte
This commit is contained in:
parent
50e2ce0d92
commit
dc3e9095a4
18
BitsQC.hs
18
BitsQC.hs
@ -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 =
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user