From 63e0a33c109e4d73d1ca331ba284f7785fe3d6dc Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Sun, 26 Apr 2015 11:40:46 +0530
Subject: [PATCH] WIP: QuickCheck for BVal type

---
 functorrent.cabal          |  1 +
 src/FuncTorrent/Bencode.hs | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)

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
-- 
2.45.2