From: Ramakrishnan Muthukrishnan Date: Mon, 27 Apr 2015 12:49:43 +0000 (+0530) Subject: Revert "Move Arbitrary instances into another module" X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/nxhtml.html?a=commitdiff_plain;h=aa1436e9f7c2284067eb2b0a9ba7dbeba1cabe29;p=functorrent.git Revert "Move Arbitrary instances into another module" This reverts commit 3d5c1507b312afb8e4219dcb048db0ae2847986e. --- diff --git a/functorrent.cabal b/functorrent.cabal index 0251a3c..05ed4b3 100644 --- a/functorrent.cabal +++ b/functorrent.cabal @@ -39,15 +39,15 @@ library HTTP, network-uri, parsec, + QuickCheck, tasty, tasty-hunit executable functorrent main-is: Main.hs - other-modules: TestInstances other-extensions: OverloadedStrings hs-source-dirs: src - ghc-options: -Wall -fwarn-incomplete-patterns -fno-warn-orphans -optc-Os + ghc-options: -Wall -fwarn-incomplete-patterns -optc-Os default-language: Haskell2010 build-depends: base, base16-bytestring, diff --git a/src/FuncTorrent/Bencode.hs b/src/FuncTorrent/Bencode.hs index 3a982ec..62d4767 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 (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 diff --git a/src/TestInstances.hs b/src/TestInstances.hs deleted file mode 100644 index 4d9082c..0000000 --- a/src/TestInstances.hs +++ /dev/null @@ -1,22 +0,0 @@ -module FuncTorrent.TestInstances where - -import qualified Data.ByteString as B -import Test.QuickCheck - -import Bencode - -instance Arbitrary B.ByteString where - arbitrary = B.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 ]