diff options
author | Sam T <pxqr.sta@gmail.com> | 2013-06-06 21:48:30 +0400 |
---|---|---|
committer | Sam T <pxqr.sta@gmail.com> | 2013-06-06 21:48:30 +0400 |
commit | 99e771564a1433029ce8a8ce4db8282fc217a1c4 (patch) | |
tree | 2b6c80c6bec1f46841b7b881e929cfd0119f95d6 | |
parent | 638ffee180b444c6c8d769fa8c95d151a44b7962 (diff) |
+ Add some tests.
-rw-r--r-- | bittorrent.cabal | 1 | ||||
-rw-r--r-- | src/Data/Bitfield.hs | 9 | ||||
-rw-r--r-- | tests/Encoding.hs | 2 | ||||
-rw-r--r-- | tests/Main.hs | 30 |
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 #-} |
14 | module Data.Bitfield | 15 | module 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 | ||
39 | import Control.Monad | 42 | import 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'. |
147 | rarest :: [Bitfield] -> Maybe PieceIx | 152 | rarest :: [Bitfield] -> Maybe PieceIx |
148 | rarest xs | 153 | rarest 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" | |||
210 | mkBitfield :: PieceCount -> [PieceIx] -> Bitfield | 215 | mkBitfield :: PieceCount -> [PieceIx] -> Bitfield |
211 | mkBitfield s ixs = Bitfield { | 216 | mkBitfield 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 | ||
37 | instance Arbitrary Bitfield where | 37 | instance Arbitrary Bitfield where |
38 | arbitrary = mkBitfield <$> positive <*> arbitrary | 38 | arbitrary = mkBitfield <$> (succ <$> positive) <*> arbitrary |
39 | 39 | ||
40 | instance Arbitrary PortNumber where | 40 | instance 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 #-} |
2 | module Main (main) where | 2 | module Main (main) where |
3 | 3 | ||
4 | import Control.Applicative | ||
5 | import Data.IntervalSet | ||
6 | import Data.List as L | ||
7 | import Data.Ord | ||
4 | import Data.Word | 8 | import Data.Word |
9 | |||
5 | import Test.Framework (defaultMain) | 10 | import Test.Framework (defaultMain) |
6 | import Test.Framework.Providers.QuickCheck2 (testProperty) | 11 | import Test.Framework.Providers.QuickCheck2 (testProperty) |
12 | import Test.QuickCheck | ||
13 | |||
14 | import Data.Bitfield as BF | ||
15 | import Network.BitTorrent as BT | ||
7 | 16 | ||
8 | import Encoding | 17 | import Encoding |
9 | 18 | ||
10 | import Data.Bitfield as BT | 19 | |
11 | import Network.BitTorrent as BT | 20 | |
21 | instance Arbitrary IntSet where | ||
22 | arbitrary = fromList <$> arbitrary | ||
23 | |||
24 | prop_completenessRange :: Bitfield -> Bool | ||
25 | prop_completenessRange bf = 0 <= c && c <= 1 | ||
26 | where | ||
27 | c = completeness bf | ||
28 | |||
29 | prop_rarestInRange :: [Bitfield] -> Bool | ||
30 | prop_rarestInRange xs = case rarest xs of | ||
31 | Just r -> 0 <= r && r < totalCount (maximumBy (comparing totalCount) xs) | ||
32 | Nothing -> True | ||
12 | 33 | ||
13 | main :: IO () | 34 | main :: IO () |
14 | main = defaultMain [] | 35 | main = defaultMain |
36 | [ testProperty "completeness range" prop_completenessRange | ||
37 | , testProperty "rarest in range" prop_rarestInRange | ||
38 | ] | ||