on master help sign in
Add isEmpty
22c8278Alexander Polakov
BitsQC.hs+24
@@ -50,6 +50,13 @@ tests =
50 prop_getLazyByteString_equal_to_ByteString2
50 prop_getLazyByteString_equal_to_ByteString2
51 ]
51 ]
52
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+
53 , testGroup "Fail"
60 , testGroup "Fail"
54 [ testProperty "monadic fail" prop_fail ]
61 [ testProperty "monadic fail" prop_fail ]
55
62
@@ -117,6 +124,23 @@ tests =
117 ]
124 ]
118 ]
125 ]
119
126
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) ==> runGet (runBitGet isEmpty) bs == False
133+
134+prop_isEmptyOfConsumedEmpty :: L.ByteString -> Int -> Property
135+prop_isEmptyOfConsumedEmpty bs n =
136+ (fromIntegral n) == L.length bs && not (L.null bs) ==>
137+ runGet (runBitGet (getByteString n >> isEmpty)) bs == True
138+
139+prop_isEmptyOfNotConsumedNotEmpty :: L.ByteString -> Int -> Property
140+prop_isEmptyOfNotConsumedNotEmpty bs n =
141+ (fromIntegral n) < L.length bs && not (L.null bs) ==>
142+ runGet (runBitGet (getByteString n >> isEmpty)) bs == False
143+
120prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
144prop_getLazyByteString_equal_to_ByteString :: L.ByteString -> Int -> Property
121prop_getLazyByteString_equal_to_ByteString bs n =
145prop_getLazyByteString_equal_to_ByteString bs n =
122 (fromIntegral n) <= L.length bs ==>
146 (fromIntegral n) <= L.length bs ==>
Data/Binary/Bits/Get.hs+7−2
@@ -79,10 +79,11 @@ module Data.Binary.Bits.Get
79 , byteString
79 , byteString
80 , Data.Binary.Bits.Get.getByteString
80 , Data.Binary.Bits.Get.getByteString
81 , Data.Binary.Bits.Get.getLazyByteString
81 , Data.Binary.Bits.Get.getLazyByteString
82+ , Data.Binary.Bits.Get.isEmpty
82
83
83 ) where
84 ) where
84
85
85−import Data.Binary.Get as B ( runGet, Get, getByteString, getLazyByteString )
86+import Data.Binary.Get as B ( runGet, Get, getByteString, getLazyByteString, isEmpty )
86import Data.Binary.Get.Internal as B ( get, put, ensureN )
87import Data.Binary.Get.Internal as B ( get, put, ensureN )
87
88
88import Data.ByteString as B
89import Data.ByteString as B
@@ -91,7 +92,6 @@ import Data.ByteString.Unsafe
91
92
92import Data.Bits
93import Data.Bits
93import Data.Word
94import Data.Word
94−
95import Control.Applicative
95import Control.Applicative
96
96
97import Prelude as P
97import Prelude as P
@@ -427,6 +427,11 @@ getLazyByteString n = do
427 return (S B.empty 0, lbs)
427 return (S B.empty 0, lbs)
428 _ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
428 _ -> L.fromChunks . (:[]) <$> Data.Binary.Bits.Get.getByteString n
429
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) -> B.isEmpty >>= \e -> return (S bs o, e)
434+
430-- | Read a 1 bit 'Bool'.
435-- | Read a 1 bit 'Bool'.
431bool :: Block Bool
436bool :: Block Bool
432bool = Block 1 readBool
437bool = Block 1 readBool