-----------------------------------------------------------------------------
-- |
-- Module      :  BEncode.Tests
-- Copyright   :  (c) Lemmih 2005
-- License     :  BSD-like
--
-- Maintainer  :  lemmih@gmail.com
-- Stability   :  stable
-- Portability :  portable
--
-----------------------------------------------------------------------------
module BEncode.Tests where

import qualified Data.Map as Map
import Test.QuickCheck

import BEncode.BEncode
import Control.Monad

import qualified Data.FastPackedString as FS
import Data.FastPackedString (FastString)

arbitraryFStr :: Int -> Gen FastString
arbitraryFStr len
    = fmap FS.pack (replicateM len (elements ['\0' .. '\255']))

instance Arbitrary FS.FastString where
    arbitrary = sized $ \n -> arbitraryFStr =<< choose (1,n)
    coarbitrary = undefined

instance Arbitrary Char where
  arbitrary = elements "abcdefghijklmopqstuvwzyx"
  coarbitrary = undefined

instance (Arbitrary k, Ord k
         ,Arbitrary a ) => Arbitrary (Map.Map k a) where
    arbitrary = sized $ \n ->
                do x <- arbitrary
                   xs <- resize (n `div` 2) arbitrary
                   return (Map.fromList (x:xs))
    coarbitrary = undefined

instance Arbitrary BEncode where
    coarbitrary = undefined
    arbitrary = sized $ \n ->
                oneof
                [ liftM BInt arbitrary
                , liftM BString arbitrary
                , liftM BList (resize (n `div` 2) arbitrary)
                , liftM BDict arbitrary ]

prop_identity :: BEncode -> Bool
prop_identity bencode
    = case bRead (FS.pack (bShow bencode "")) of
        Nothing -> False
        Just bencode' -> bencode' == bencode

{-
data BEncode = BInt Int
	     | BString FastString
	     | BList [BEncode]
             | BDict (Map String BEncode)
	       deriving (Eq, Ord, Show)
-}
