packer 0.1.8 → 0.1.9
raw patch · 3 files changed
+50/−1 lines, 3 files
Files
- Data/Packer.hs +14/−0
- Tests/Tests.hs +35/−0
- packer.cabal +1/−1
Data/Packer.hs view
@@ -50,6 +50,8 @@ , getFloat64LE , getFloat64BE , isolate+ , endOfInput+ , countRemaining -- * Packing functions , packGetPosition , putWord8@@ -244,6 +246,18 @@ -- this function will raises an exception isolate :: Int -> Unpacking a -> Unpacking a isolate n subUnpacker = unpackIsolate n subUnpacker++-- | Return True if there are no more bytes to be unpacked.+--+-- No input is consumed.+endOfInput :: Unpacking Bool+endOfInput = (== 0) <$> unpackGetNbRemaining++-- | Return the number of bytes remaining in the current Unpacking.+--+-- No input is consumed.+countRemaining :: Unpacking Int+countRemaining = unpackGetNbRemaining -- | Put a Word8 putWord8 :: Word8 -> Packing ()
Tests/Tests.hs view
@@ -5,6 +5,7 @@ import Test.Tasty.QuickCheck import Test.Tasty.HUnit+import Test.Tasty (TestTree) import Control.Applicative ((<$>)) import Control.Monad import Control.Exception@@ -26,6 +27,7 @@ , ("64BE", putWord64BE . fromIntegral, (fromIntegral <$> getWord64BE), B.pack [8,7,6,5,4,3,2,1], 0x0807060504030201) ] +toEndianCase :: (Eq a, Show a) => (String, a -> Packing (), Unpacking a, B.ByteString, a) -> [TestTree] toEndianCase (n, pAct, gAct, bs, v) = [ testCase ("put" ++ n) (runPacking 8 (pAct v) @=? bs) , testCase ("get" ++ n) (runUnpacking gAct bs @=? v)@@ -200,6 +202,36 @@ assertException msg filterE act = handleJust filterE (\_ -> return ()) (evaluate act >> assertFailure (msg ++ " didn't raise the proper exception")) ++endOfInputCase :: [TestTree]+endOfInputCase =+ [ testCase "endOfInput False" (getEndOfInput getWord8 "as" @=? False)+ , testCase "endOfInput True" (getEndOfInput getWord16 "as" @=? True)+ ]+ where+ getEndOfInput :: Unpacking a -> B.ByteString -> Bool+ getEndOfInput getter bs =+ let inner = do+ _ <- getter+ endOfInput+ in runUnpacking inner bs+++countRemainingCase :: [TestTree]+countRemainingCase =+ [ testCase "countRemaining 3" (getRemainingCount getWord8 "abcd" @=? 3)+ , testCase "countRemaining 2" (getRemainingCount getWord16 "abcd" @=? 2)+ , testCase "countRemaining 0" (getRemainingCount getWord32 "abcd" @=? 0)+ ]+ where+ getRemainingCount :: Unpacking a -> B.ByteString -> Int+ getRemainingCount getter bs =+ let inner = do+ _ <- getter+ countRemaining+ in runUnpacking inner bs++ main :: IO () main = defaultMain $ testGroup "packer" [ testGroup "serialization"@@ -221,7 +253,10 @@ , testCase "unpacking isolate not consumed" $ assertException "unpacking isolate" (\(IsolationNotFullyConsumed _ _) -> Just ()) (runUnpacking (isolate 3 (getBytes 2)) (B.pack [1,2,3]))+ ]+ , testGroup "endOfInput" endOfInputCase+ , testGroup "countRemaining" countRemainingCase , testGroup "endianness cases" $ concatMap toEndianCase endiannessCases , testProperty "unpacking.packing=id" (\ds -> unpackDataStream ds (packDataStream ds) == ds) , testProperty "holes" test_property_hole
packer.cabal view
@@ -1,5 +1,5 @@ Name: packer-Version: 0.1.8+Version: 0.1.9 Description: Fast byte serializer and unserializer License: BSD3 License-file: LICENSE