+{-
+ - Copyright (C) 2015-2016 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
+ -
+ - This file is part of FuncTorrent.
+ -
+ - FuncTorrent is free software; you can redistribute it and/or modify
+ - it under the terms of the GNU General Public License as published by
+ - the Free Software Foundation; either version 3 of the License, or
+ - (at your option) any later version.
+ -
+ - FuncTorrent is distributed in the hope that it will be useful,
+ - but WITHOUT ANY WARRANTY; without even the implied warranty of
+ - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ - GNU General Public License for more details.
+ -
+ - You should have received a copy of the GNU General Public License
+ - along with FuncTorrent; if not, see <http://www.gnu.org/licenses/>
+ -}
+
{-# LANGUAGE OverloadedStrings #-}
module FuncTorrent.Bencode
(BVal(..)
import Prelude hiding (length, concat)
-import Control.Applicative ((<*))
import Data.ByteString (ByteString, length, concat)
import Data.ByteString.Char8 (unpack, pack)
-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
| 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 (bval (n `div` 4))
+ , do keys <- vectorOf n arbitrary
+ vals <- vectorOf n (bval (n `div` 4))
+ return $ Bdict $ fromList $ zip keys vals ]
+
-- getters
bValToInteger :: BVal -> Maybe Integer
bValToInteger (Bint x) = Just x