summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam T <pxqr.sta@gmail.com>2013-06-06 21:48:30 +0400
committerSam T <pxqr.sta@gmail.com>2013-06-06 21:48:30 +0400
commit99e771564a1433029ce8a8ce4db8282fc217a1c4 (patch)
tree2b6c80c6bec1f46841b7b881e929cfd0119f95d6
parent638ffee180b444c6c8d769fa8c95d151a44b7962 (diff)
+ Add some tests.
-rw-r--r--bittorrent.cabal1
-rw-r--r--src/Data/Bitfield.hs9
-rw-r--r--tests/Encoding.hs2
-rw-r--r--tests/Main.hs30
4 files changed, 36 insertions, 6 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index d911c71f..ebb8b62f 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -119,6 +119,7 @@ test-suite properties
119 , QuickCheck 119 , QuickCheck
120 120
121 , bittorrent 121 , bittorrent
122 , intset
122 123
123 ghc-options: -Wall -fno-warn-orphans 124 ghc-options: -Wall -fno-warn-orphans
124 125
diff --git a/src/Data/Bitfield.hs b/src/Data/Bitfield.hs
index 024f8f71..3424a0c6 100644
--- a/src/Data/Bitfield.hs
+++ b/src/Data/Bitfield.hs
@@ -9,6 +9,7 @@
9-- bitfields. Bitfields are used to keep track indices of complete 9-- bitfields. Bitfields are used to keep track indices of complete
10-- pieces either peer have or client have. 10-- pieces either peer have or client have.
11-- 11--
12{-# LANGUAGE CPP #-}
12{-# LANGUAGE BangPatterns #-} 13{-# LANGUAGE BangPatterns #-}
13{-# LANGUAGE RecordWildCards #-} 14{-# LANGUAGE RecordWildCards #-}
14module Data.Bitfield 15module Data.Bitfield
@@ -32,8 +33,10 @@ module Data.Bitfield
32 , getBitfield, putBitfield 33 , getBitfield, putBitfield
33 , bitfieldByteCount 34 , bitfieldByteCount
34 35
36-- #ifdef TESTING
35 , -- * Debug 37 , -- * Debug
36 mkBitfield 38 mkBitfield
39-- #endif
37 ) where 40 ) where
38 41
39import Control.Monad 42import Control.Monad
@@ -143,11 +146,13 @@ frequencies xs = runST $ do
143 where 146 where
144 size = maximum (map bfSize xs) 147 size = maximum (map bfSize xs)
145 148
149-- TODO it seems like this operation is veeery slow
150
146-- | Find least available piece index. If no piece available return 'Nothing'. 151-- | Find least available piece index. If no piece available return 'Nothing'.
147rarest :: [Bitfield] -> Maybe PieceIx 152rarest :: [Bitfield] -> Maybe PieceIx
148rarest xs 153rarest xs
149 | V.null freqMap = Nothing 154 | V.null freqMap = Nothing
150 | otherwise = Just $ fst $ V.ifoldr minIx (0, freqMap V.! 0) freqMap 155 | otherwise = Just $ fst $ V.ifoldr' minIx (0, freqMap V.! 0) freqMap
151 where 156 where
152 freqMap = frequencies xs 157 freqMap = frequencies xs
153 158
@@ -210,5 +215,5 @@ bitfieldByteCount = error "bitfieldByteCount"
210mkBitfield :: PieceCount -> [PieceIx] -> Bitfield 215mkBitfield :: PieceCount -> [PieceIx] -> Bitfield
211mkBitfield s ixs = Bitfield { 216mkBitfield s ixs = Bitfield {
212 bfSize = s 217 bfSize = s
213 , bfSet = S.splitLT s $ S.fromList ixs 218 , bfSet = S.splitGT (-1) $ S.splitLT s $ S.fromList ixs
214 } \ No newline at end of file 219 } \ No newline at end of file
diff --git a/tests/Encoding.hs b/tests/Encoding.hs
index 0b678a25..8afd3625 100644
--- a/tests/Encoding.hs
+++ b/tests/Encoding.hs
@@ -35,7 +35,7 @@ instance Arbitrary Block where
35 arbitrary = Block <$> positive <*> positive <*> arbitrary 35 arbitrary = Block <$> positive <*> positive <*> arbitrary
36 36
37instance Arbitrary Bitfield where 37instance Arbitrary Bitfield where
38 arbitrary = mkBitfield <$> positive <*> arbitrary 38 arbitrary = mkBitfield <$> (succ <$> positive) <*> arbitrary
39 39
40instance Arbitrary PortNumber where 40instance Arbitrary PortNumber where
41 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) 41 arbitrary = fromIntegral <$> (arbitrary :: Gen Word16)
diff --git a/tests/Main.hs b/tests/Main.hs
index bc3f7809..ef4ab8a7 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -1,14 +1,38 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2module Main (main) where 2module Main (main) where
3 3
4import Control.Applicative
5import Data.IntervalSet
6import Data.List as L
7import Data.Ord
4import Data.Word 8import Data.Word
9
5import Test.Framework (defaultMain) 10import Test.Framework (defaultMain)
6import Test.Framework.Providers.QuickCheck2 (testProperty) 11import Test.Framework.Providers.QuickCheck2 (testProperty)
12import Test.QuickCheck
13
14import Data.Bitfield as BF
15import Network.BitTorrent as BT
7 16
8import Encoding 17import Encoding
9 18
10import Data.Bitfield as BT 19
11import Network.BitTorrent as BT 20
21instance Arbitrary IntSet where
22 arbitrary = fromList <$> arbitrary
23
24prop_completenessRange :: Bitfield -> Bool
25prop_completenessRange bf = 0 <= c && c <= 1
26 where
27 c = completeness bf
28
29prop_rarestInRange :: [Bitfield] -> Bool
30prop_rarestInRange xs = case rarest xs of
31 Just r -> 0 <= r && r < totalCount (maximumBy (comparing totalCount) xs)
32 Nothing -> True
12 33
13main :: IO () 34main :: IO ()
14main = defaultMain [] 35main = defaultMain
36 [ testProperty "completeness range" prop_completenessRange
37 , testProperty "rarest in range" prop_rarestInRange
38 ]