]> git.rkrishnan.org Git - functorrent.git/blobdiff - src/FuncTorrent/Bencode.hs
quickcheck: size limit randomly generated Benc structures
[functorrent.git] / src / FuncTorrent / Bencode.hs
index d4eb58c5ac12d0e4f811eb050f95266733b9f262..b55b3fe15509e5e4745a5451fd9509d842eb7c05 100644 (file)
@@ -23,6 +23,7 @@ module FuncTorrent.Bencode
     , bValToInteger
     , bstrToString
     , decode
+    , decodeWithLeftOvers
     , encode
     ) where
 
@@ -58,9 +59,9 @@ instance Arbitrary BVal where
                                , Bstr <$> arbitrary]
                 bval n = oneof [ Bint <$> arbitrary
                                , Bstr <$> arbitrary
-                               , Blist <$> vectorOf n (bval (n `div` 4))
-                               , do keys <- vectorOf n genNonEmptyString
-                                    vals <- vectorOf n (bval (n `div` 4))
+                               , Blist <$> vectorOf (n `div` 2) (bval (n `div` 4))
+                               , do keys <- vectorOf (n `div` 2) genNonEmptyString
+                                    vals <- vectorOf (n `div` 2) (bval (n `div` 4))
                                     return $ Bdict $ fromList $ zip keys vals ]
 
 -- getters
@@ -165,6 +166,10 @@ bencVal = Bstr <$> bencStr <|>
 decode :: ByteString -> Either ParseError BVal
 decode = parse bencVal "BVal"
 
+decodeWithLeftOvers :: ByteString -> Either ParseError (BVal, ByteString)
+decodeWithLeftOvers = parse ((,) <$> bencVal <*> (fmap pack leftOvers)) "BVal with LeftOvers"
+  where leftOvers = manyTill anyToken eof
+
 -- Encode BVal into a bencoded ByteString. Inverse of decode
 
 -- TODO: Use builders and lazy byte string to get O(1) concatenation over O(n)