on master help sign in
Make getLazyByteString read from current byte
dc3e909Alexander Polakov
BitsQC.hs+18
@@ -43,6 +43,13 @@ tests =
43 , testGroup "getByteString"
43 , testGroup "getByteString"
44 [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
44 [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
45
45
46+ , testGroup "getLazyByteString"
47+ [ testProperty "getLazyByteString == getByteString"
48+ prop_getLazyByteString_equal_to_ByteString
49+ , testProperty "getLazyByteString == getByteString (with shift)"
50+ prop_getLazyByteString_equal_to_ByteString2
51+ ]
52+
46 , testGroup "Fail"
53 , testGroup "Fail"
47 [ testProperty "monadic fail" prop_fail ]
54 [ testProperty "monadic fail" prop_fail ]
48
55
@@ -110,6 +117,17 @@ tests =
110 ]
117 ]
111 ]
118 ]
112
119
120+prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
121+prop_getLazyByteString_equal_to_ByteString bs n =
122+ (fromIntegral n) <= L.length bs ==>
123+ runGet (runBitGet (getLazyByteString (fromIntegral n))) bs ==
124+ (L.fromChunks . (:[]) $ runGet (runBitGet (getByteString n)) bs)
125+
126+prop_getLazyByteString_equal_to_ByteString2 :: L.ByteString -> Int -> Property
127+prop_getLazyByteString_equal_to_ByteString2 bs n =
128+ (L.length bs > 1) && (fromIntegral n) < L.length bs ==>
129+ runGet (runBitGet (getWord8 2 >> getLazyByteString (fromIntegral n))) bs ==
130+ (L.fromChunks . (:[]) $ runGet (runBitGet (getWord8 2 >> getByteString n)) bs)
113
131
114prop_getByteString_negative :: Int -> Property
132prop_getByteString_negative :: Int -> Property
115prop_getByteString_negative n =
133prop_getByteString_negative n =
Data/Binary/Bits/Get.hs+9−4
@@ -416,11 +416,16 @@ getWord64be n = block (word64be n)
416getByteString :: Int -> BitGet ByteString
416getByteString :: Int -> BitGet ByteString
417getByteString n = block (byteString n)
417getByteString n = block (byteString n)
418
418
419+-- | Get @n@ bytes as a lazy ByteString.
419getLazyByteString :: Int -> BitGet L.ByteString
420getLazyByteString :: Int -> BitGet L.ByteString
420−getLazyByteString m = B $ \ (S n bs) -> do
421+getLazyByteString n = do
422+ (S _ o) <- getState
423+ case o of
424+ 0 -> B $ \ (S bs o') -> do
421− putBackState n bs
425+ putBackState bs o'
422− lbs <- B.getLazyByteString (fromIntegral m)
426+ lbs <- B.getLazyByteString (fromIntegral n)
423− return (S B.empty 0, lbs)
427+ return (S B.empty 0, lbs)
428+ _ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
424
429
425-- | Read a 1 bit 'Bool'.
430-- | Read a 1 bit 'Bool'.
426bool :: Block Bool
431bool :: Block Bool