on master help sign in
Merge pull request #2 from polachok/master

getLazyByteString: read current byte + isEmpty

commit743a30dbb580bb39c4b28813db7df580a8922b07 authorLennart Kolmodin <kolmodin@gmail.com> dateSun 31 Mar 2013, 07:55 (14 years ago) parent50e2ce039618a7
2 files changed+61−6
BitsQC.hs+43 Data/Binary/Bits/Get.hs+18−6
BitsQC.hs+43
@@ -43,6 +43,20 @@ tests =
4343 , testGroup "getByteString"
4444 [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
4545
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+
53+ , testGroup "isEmpty"
54+ [ testProperty "prop_isEmptyOfEmptyEmpty" prop_isEmptyOfEmptyEmpty
55+ , testProperty "prop_isEmptyOfNonEmptyEmpty" prop_isEmptyOfNonEmptyEmpty
56+ , testProperty "prop_isEmptyOfConsumedEmpty" prop_isEmptyOfConsumedEmpty
57+ , testProperty "prop_isEmptyOfNotConsumedNotEmpty" prop_isEmptyOfNotConsumedNotEmpty
58+ ]
59+
4660 , testGroup "Fail"
4761 [ testProperty "monadic fail" prop_fail ]
4862
@@ -110,6 +124,35 @@ tests =
110124 ]
111125 ]
112126
127+prop_isEmptyOfEmptyEmpty :: Bool
128+prop_isEmptyOfEmptyEmpty = runGet (runBitGet isEmpty) L.empty
129+
130+prop_isEmptyOfNonEmptyEmpty :: L.ByteString -> Property
131+prop_isEmptyOfNonEmptyEmpty bs =
132+ not (L.null bs) ==> not (runGet (runBitGet isEmpty) bs)
133+
134+prop_isEmptyOfConsumedEmpty :: L.ByteString -> Property
135+prop_isEmptyOfConsumedEmpty bs =
136+ not (L.null bs) ==>
137+ runGet (runBitGet (getByteString n >> isEmpty)) bs
138+ where n = fromIntegral $ L.length bs
139+
140+prop_isEmptyOfNotConsumedNotEmpty :: L.ByteString -> Int -> Property
141+prop_isEmptyOfNotConsumedNotEmpty bs n =
142+ (fromIntegral n) < L.length bs && not (L.null bs) ==>
143+ not (runGet (runBitGet (getByteString n >> isEmpty)) bs)
144+
145+prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
146+prop_getLazyByteString_equal_to_ByteString bs n =
147+ (fromIntegral n) <= L.length bs ==>
148+ runGet (runBitGet (getLazyByteString (fromIntegral n))) bs ==
149+ (L.fromChunks . (:[]) $ runGet (runBitGet (getByteString n)) bs)
150+
151+prop_getLazyByteString_equal_to_ByteString2 :: L.ByteString -> Int -> Property
152+prop_getLazyByteString_equal_to_ByteString2 bs n =
153+ (L.length bs > 1) && (fromIntegral n) < L.length bs ==>
154+ runGet (runBitGet (getWord8 2 >> getLazyByteString (fromIntegral n))) bs ==
155+ (L.fromChunks . (:[]) $ runGet (runBitGet (getWord8 2 >> getByteString n)) bs)
113156
114157prop_getByteString_negative :: Int -> Property
115158prop_getByteString_negative n =
Data/Binary/Bits/Get.hs+18−6
@@ -79,10 +79,11 @@ module Data.Binary.Bits.Get
7979 , byteString
8080 , Data.Binary.Bits.Get.getByteString
8181 , Data.Binary.Bits.Get.getLazyByteString
82+ , Data.Binary.Bits.Get.isEmpty
8283
8384 ) where
8485
85−import Data.Binary.Get as B ( runGet, Get, getByteString, getLazyByteString )
86+import Data.Binary.Get as B ( runGet, Get, getByteString, getLazyByteString, isEmpty )
8687import Data.Binary.Get.Internal as B ( get, put, ensureN )
8788
8889import Data.ByteString as B
@@ -91,7 +92,6 @@ import Data.ByteString.Unsafe
9192
9293import Data.Bits
9394import Data.Word
94−
9595import Control.Applicative
9696
9797import Prelude as P
@@ -416,11 +416,23 @@ getWord64be n = block (word64be n)
416416getByteString :: Int -> BitGet ByteString
417417getByteString n = block (byteString n)
418418
419+-- | Get @n@ bytes as a lazy ByteString.
419420getLazyByteString :: Int -> BitGet L.ByteString
420−getLazyByteString m = B $ \ (S n bs) -> do
421− putBackState n bs
422− lbs <- B.getLazyByteString (fromIntegral m)
423− return (S B.empty 0, lbs)
421+getLazyByteString n = do
422+ (S _ o) <- getState
423+ case o of
424+ 0 -> B $ \ (S bs o') -> do
425+ putBackState bs o'
426+ lbs <- B.getLazyByteString (fromIntegral n)
427+ return (S B.empty 0, lbs)
428+ _ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
429+
430+-- | Test whether all input has been consumed, i.e. there are no remaining
431+-- undecoded bytes.
432+isEmpty :: BitGet Bool
433+isEmpty = B $ \ (S bs o) -> if B.null bs
434+ then B.isEmpty >>= \e -> return (S bs o, e)
435+ else return (S bs o, False)
424436
425437-- | Read a 1 bit 'Bool'.
426438bool :: Block Bool