From: Ramakrishnan Muthukrishnan Date: Sun, 26 Apr 2015 06:10:46 +0000 (+0530) Subject: WIP: QuickCheck for BVal type X-Git-Url: https://git.rkrishnan.org/vdrive/%5B/%5D%20/uri//%22%22?a=commitdiff_plain;h=63e0a33c109e4d73d1ca331ba284f7785fe3d6dc;p=functorrent.git WIP: QuickCheck for BVal type --- diff --git a/functorrent.cabal b/functorrent.cabal index 6bfb1c8..08fd0c2 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -39,6 +39,7 @@ library HTTP, network-uri, parsec, + QuickCheck, tasty, tasty-hunit diff --git a/src/FuncTorrent/Bencode.hs b/src/FuncTorrent/Bencode.hs index 3a982ec..8b6ff52 100644 --- a/src/FuncTorrent/Bencode.hs +++ b/src/FuncTorrent/Bencode.hs @@ -19,6 +19,7 @@ import Data.Functor ((<$>)) import Data.Map.Strict (Map, fromList, toList) import Text.ParserCombinators.Parsec import qualified Text.Parsec.ByteString as ParsecBS +import Test.QuickCheck data BVal = Bint Integer | Bstr ByteString @@ -26,6 +27,22 @@ data BVal = Bint Integer | Bdict (Map String BVal) deriving (Ord, Eq, Show) +instance Arbitrary ByteString where + arbitrary = pack <$> arbitrary + +instance Arbitrary BVal where + arbitrary = sized bval + where + bval :: Int -> Gen BVal + bval 0 = oneof [ Bint <$> arbitrary + , Bstr <$> arbitrary] + bval n = oneof [ Bint <$> arbitrary + , Bstr <$> arbitrary + , Blist <$> vectorOf n arbitrary + , do keys <- vectorOf n arbitrary + vals <- vectorOf n arbitrary + return $ Bdict $ fromList $ zip keys vals ] + -- getters bValToInteger :: BVal -> Maybe Integer bValToInteger (Bint x) = Just x