]> git.rkrishnan.org Git - functorrent.git/commitdiff
WIP: QuickCheck for BVal type
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 26 Apr 2015 06:10:46 +0000 (11:40 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sun, 26 Apr 2015 06:11:16 +0000 (11:41 +0530)
functorrent.cabal
src/FuncTorrent/Bencode.hs

index 6bfb1c88ae1a2fca59b099fe78d71465fbfe34d5..08fd0c247a7a8e167d0eab360c8ce0117e4f3545 100644 (file)
@@ -39,6 +39,7 @@ library
                        HTTP,
                        network-uri,
                        parsec,
+                       QuickCheck,
                        tasty,
                        tasty-hunit
 
index 3a982ec0336da0f8ca5a31bbfb73ed4a29b2d78e..8b6ff5229909266a42e85bb4a47952f32afe7af8 100644 (file)
@@ -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