@@ -9,6 +9,7 @@ import Data.Binary.Put ( runPut )
9import Data.Binary.Bits
9import Data.Binary.Bits
10import Data.Binary.Bits.Get
10import Data.Binary.Bits.Get
11import Data.Binary.Bits.Put
11import Data.Binary.Bits.Put
12+import Data.Binary.Bits.BitOrder
13import qualified Data.ByteString as B
14import qualified Data.ByteString as B
14import qualified Data.ByteString.Lazy as L
15import qualified Data.ByteString.Lazy as L
@@ -18,6 +19,8 @@ import Data.Bits
18import Data.Word
19import Data.Word
19import Foreign.Storable
20import Foreign.Storable
20import System.Random
21import System.Random
22+import Data.Traversable (traverse)
23+import Data.Foldable (traverse_)
22import Test.Framework.Providers.QuickCheck2 ( testProperty )
25import Test.Framework.Providers.QuickCheck2 ( testProperty )
23import Test.Framework.Runners.Console ( defaultMain )
26import Test.Framework.Runners.Console ( defaultMain )
@@ -36,7 +39,10 @@ tests =
36 [ testProperty "prop_composite_case" prop_composite_case ]
39 [ testProperty "prop_composite_case" prop_composite_case ]
38 , testGroup "getByteString"
41 , testGroup "getByteString"
39− [ testProperty "prop_getByteString_negative" prop_getByteString_negative ]
42+ [ testProperty "prop_getByteString_negative" prop_getByteString_negative
43+ , testProperty "prop_putByteString_getByteString" (prop_putByteString_getByteString :: BitOrder -> B.ByteString -> Property)
44+ , testProperty "prop_putByteString_getByteString_many" (prop_putByteString_getByteString_many :: BitOrder -> [B.ByteString] -> Property)
41 , testGroup "getLazyByteString"
47 , testGroup "getLazyByteString"
42 [ testProperty "getLazyByteString == getByteString"
48 [ testProperty "getLazyByteString == getByteString"
@@ -84,10 +90,10 @@ tests =
86 , testGroup "prop_put_with_bitreq"
92 , testGroup "prop_put_with_bitreq"
87− [ testProperty "Word8" (prop_putget_with_bitreq :: W Word8 -> Property)
93+ [ testProperty "Word8" (prop_putget_with_bitreq :: BitOrder -> W Word8 -> Property)
88− , testProperty "Word16" (prop_putget_with_bitreq :: W Word16 -> Property)
94+ , testProperty "Word16" (prop_putget_with_bitreq :: BitOrder -> W Word16 -> Property)
89− , testProperty "Word32" (prop_putget_with_bitreq :: W Word32 -> Property)
95+ , testProperty "Word32" (prop_putget_with_bitreq :: BitOrder -> W Word32 -> Property)
90− , testProperty "Word64" (prop_putget_with_bitreq :: W Word64 -> Property)
96+ , testProperty "Word64" (prop_putget_with_bitreq :: BitOrder -> W Word64 -> Property)
93 , testGroup "prop_putget_list_simple"
99 , testGroup "prop_putget_list_simple"
@@ -112,10 +118,10 @@ tests =
112 , testProperty "Word64" (prop_putget_list_with_bitreq :: W [Word64] -> Property)
118 , testProperty "Word64" (prop_putget_list_with_bitreq :: W [Word64] -> Property)
114 , testGroup "prop_bitget_bytestring_interspersed"
120 , testGroup "prop_bitget_bytestring_interspersed"
115− [ testProperty "Word8" (prop_bitget_bytestring_interspersed :: W Word8 -> [B.ByteString] -> Property)
121+ [ testProperty "Word8" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word8 -> [B.ByteString] -> Property)
116− , testProperty "Word16" (prop_bitget_bytestring_interspersed :: W Word16 -> [B.ByteString] -> Property)
122+ , testProperty "Word16" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word16 -> [B.ByteString] -> Property)
117− , testProperty "Word32" (prop_bitget_bytestring_interspersed :: W Word32 -> [B.ByteString] -> Property)
123+ , testProperty "Word32" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word32 -> [B.ByteString] -> Property)
118− , testProperty "Word64" (prop_bitget_bytestring_interspersed :: W Word64 -> [B.ByteString] -> Property)
124+ , testProperty "Word64" (prop_bitget_bytestring_interspersed :: BitOrder -> W Word64 -> [B.ByteString] -> Property)
120 , testGroup "Simulate programs"
126 , testGroup "Simulate programs"
121 [ testProperty "primitive" prop_primitive
127 [ testProperty "primitive" prop_primitive
@@ -158,13 +164,28 @@ prop_getByteString_negative n =
158 n < 1 ==>
164 n < 1 ==>
159 runGet (runBitGet (getByteString n)) L.empty == B.empty
165 runGet (runBitGet (getByteString n)) L.empty == B.empty
167+prop_putByteString_getByteString :: BitOrder -> B.ByteString -> Property
168+prop_putByteString_getByteString bo bs = property $ bs' == bs
171+ w = runPut (runBitPut (withBitOrder bo (putByteString bs)))
172+ bs' = runGet (runBitGet (withBitOrder bo (getByteString n))) w
174+prop_putByteString_getByteString_many :: BitOrder -> [B.ByteString] -> Property
175+prop_putByteString_getByteString_many bo bs = property $ bs' == bs
177+ n = fmap B.length bs
178+ w = runPut (runBitPut (withBitOrder bo (traverse_ putByteString bs)))
179+ bs' = runGet (runBitGet (withBitOrder bo (traverse getByteString n))) w
161−prop_putget_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => W a -> Property
182+prop_putget_with_bitreq :: (BinaryBit a, Num a, Bits a, Ord a) => BitOrder -> W a -> Property
162−prop_putget_with_bitreq (W w) = property $
183+prop_putget_with_bitreq bo (W w) = property $
163 -- write all words with as many bits as it's required
184 -- write all words with as many bits as it's required
164 let p = putBits (bitreq w) w
185 let p = putBits (bitreq w) w
165 g = getBits (bitreq w)
186 g = getBits (bitreq w)
166− lbs = runPut (runBitPut p)
187+ lbs = runPut (runBitPut (withBitOrder bo p))
167− w' = runGet (runBitGet g) lbs
188+ w' = runGet (runBitGet (withBitOrder bo g)) lbs
168 in w == w'
189 in w == w'
170-- | Write a list of items. Each item is written with the maximum amount of
191-- | Write a list of items. Each item is written with the maximum amount of
@@ -226,12 +247,12 @@ prop_bitget_with_put_from_binary (W ws) = property $
226 in ws == ws'
247 in ws == ws'
228-- | Write each 'ByteString' with a variable sized value as a separator.
249-- | Write each 'ByteString' with a variable sized value as a separator.
229−prop_bitget_bytestring_interspersed :: (BinaryBit a, Binary a, Num a, Ord a, Bits a) => W a -> [B.ByteString] -> Property
250+prop_bitget_bytestring_interspersed :: (BinaryBit a, Binary a, Num a, Ord a, Bits a) => BitOrder -> W a -> [B.ByteString] -> Property
230−prop_bitget_bytestring_interspersed (W ws) bss = property $
251+prop_bitget_bytestring_interspersed bo (W ws) bss = property $
231 let p = mapM_ (\bs -> putBits (bitreq ws) ws >> putByteString bs) bss
252 let p = mapM_ (\bs -> putBits (bitreq ws) ws >> putByteString bs) bss
232 g = mapM (\bs -> (,) <$> getBits (bitreq ws) <*> getByteString (B.length bs)) bss
253 g = mapM (\bs -> (,) <$> getBits (bitreq ws) <*> getByteString (B.length bs)) bss
233− lbs = runPut (runBitPut p)
254+ lbs = runPut (runBitPut (withBitOrder bo p))
234− r = runGet (runBitGet g) lbs
255+ r = runGet (runBitGet (withBitOrder bo g)) lbs
235 in map (ws,) bss == r
256 in map (ws,) bss == r
237-- | Test failing.
258-- | Test failing.
@@ -425,6 +446,14 @@ instance Arbitrary Primitive where
425 LBS _ lbs -> let ws = L.unpack lbs in map (\ws' -> LBS (length ws') (L.pack ws')) (shrink ws)
446 LBS _ lbs -> let ws = L.unpack lbs in map (\ws' -> LBS (length ws') (L.pack ws')) (shrink ws)
426 IsEmpty -> []
447 IsEmpty -> []
449+instance Arbitrary BitOrder where
450+ arbitrary = elements [BB, LB, LL, BL]
451+ shrink LL = [BB,LB,BL]
428prop_primitive :: Primitive -> Property
457prop_primitive :: Primitive -> Property
429prop_primitive prim = property $
458prop_primitive prim = property $
430 let p = putPrimitive prim
459 let p = putPrimitive prim