diff options
-rw-r--r-- | src/Data/Bitfield.hs | 5 | ||||
-rw-r--r-- | tests/Encoding.hs | 3 | ||||
-rw-r--r-- | tests/Main.hs | 39 |
3 files changed, 39 insertions, 8 deletions
diff --git a/src/Data/Bitfield.hs b/src/Data/Bitfield.hs index 3424a0c6..a1033ff2 100644 --- a/src/Data/Bitfield.hs +++ b/src/Data/Bitfield.hs | |||
@@ -19,6 +19,7 @@ module Data.Bitfield | |||
19 | , haveAll, haveNone, have | 19 | , haveAll, haveNone, have |
20 | 20 | ||
21 | -- * Query | 21 | -- * Query |
22 | , Data.Bitfield.null | ||
22 | , haveCount, totalCount, completeness | 23 | , haveCount, totalCount, completeness |
23 | , findMin, findMax | 24 | , findMin, findMax |
24 | 25 | ||
@@ -100,6 +101,10 @@ have ix Bitfield {..} | |||
100 | Query | 101 | Query |
101 | -----------------------------------------------------------------------} | 102 | -----------------------------------------------------------------------} |
102 | 103 | ||
104 | -- | Test if bitifield have no one index: peer do not have anything. | ||
105 | null :: Bitfield -> Bool | ||
106 | null Bitfield {..} = S.null bfSet | ||
107 | |||
103 | -- | Count of peer have pieces. | 108 | -- | Count of peer have pieces. |
104 | haveCount :: Bitfield -> PieceCount | 109 | haveCount :: Bitfield -> PieceCount |
105 | haveCount = S.size . bfSet | 110 | haveCount = S.size . bfSet |
diff --git a/tests/Encoding.hs b/tests/Encoding.hs index 8afd3625..a599cd39 100644 --- a/tests/Encoding.hs +++ b/tests/Encoding.hs | |||
@@ -35,7 +35,8 @@ 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 <$> (succ <$> positive) <*> arbitrary | 38 | arbitrary = mkBitfield <$> (succ . min 1000 <$> positive) |
39 | <*> arbitrary | ||
39 | 40 | ||
40 | instance Arbitrary PortNumber where | 41 | instance Arbitrary PortNumber where |
41 | arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) | 42 | arbitrary = fromIntegral <$> (arbitrary :: Gen Word16) |
diff --git a/tests/Main.hs b/tests/Main.hs index ef4ab8a7..0aa6423f 100644 --- a/tests/Main.hs +++ b/tests/Main.hs | |||
@@ -16,23 +16,48 @@ import Network.BitTorrent as BT | |||
16 | 16 | ||
17 | import Encoding | 17 | import Encoding |
18 | 18 | ||
19 | 19 | {----------------------------------------------------------------------- | |
20 | 20 | Bitfield | |
21 | instance Arbitrary IntSet where | 21 | -----------------------------------------------------------------------} |
22 | arbitrary = fromList <$> arbitrary | 22 | -- other properties are tested in IntervalSet |
23 | 23 | ||
24 | prop_completenessRange :: Bitfield -> Bool | 24 | prop_completenessRange :: Bitfield -> Bool |
25 | prop_completenessRange bf = 0 <= c && c <= 1 | 25 | prop_completenessRange bf = 0 <= c && c <= 1 |
26 | where | 26 | where |
27 | c = completeness bf | 27 | c = completeness bf |
28 | 28 | ||
29 | prop_minMax :: Bitfield -> Bool | ||
30 | prop_minMax bf | ||
31 | | BF.null bf = True | ||
32 | | otherwise = BF.findMin bf <= BF.findMax bf | ||
33 | |||
29 | prop_rarestInRange :: [Bitfield] -> Bool | 34 | prop_rarestInRange :: [Bitfield] -> Bool |
30 | prop_rarestInRange xs = case rarest xs of | 35 | prop_rarestInRange xs = case rarest xs of |
31 | Just r -> 0 <= r && r < totalCount (maximumBy (comparing totalCount) xs) | 36 | Just r -> 0 <= r |
37 | && r < totalCount (maximumBy (comparing totalCount) xs) | ||
32 | Nothing -> True | 38 | Nothing -> True |
33 | 39 | ||
40 | {- this one should give pretty good coverage -} | ||
41 | prop_differenceDeMorgan :: Bitfield -> Bitfield -> Bitfield -> Bool | ||
42 | prop_differenceDeMorgan a b c = | ||
43 | (a `BF.difference` (b `BF.intersection` c)) | ||
44 | == ((a `BF.difference` b) `BF.union` (a `BF.difference` c)) | ||
45 | && | ||
46 | (a `BF.difference` (b `BF.union` c)) | ||
47 | == ((a `BF.difference` b) `BF.intersection` (a `BF.difference` c)) | ||
48 | |||
49 | |||
50 | {----------------------------------------------------------------------- | ||
51 | Torrent | ||
52 | -----------------------------------------------------------------------} | ||
53 | |||
54 | -- TODO tests for torrent: encoding <-> decoding | ||
55 | |||
56 | |||
34 | main :: IO () | 57 | main :: IO () |
35 | main = defaultMain | 58 | main = defaultMain |
36 | [ testProperty "completeness range" prop_completenessRange | 59 | [ testProperty "completeness range" prop_completenessRange |
37 | , testProperty "rarest in range" prop_rarestInRange | 60 | , testProperty "rarest in range" prop_rarestInRange |
61 | , testProperty "min less that max" prop_minMax | ||
62 | , testProperty "difference de morgan" prop_differenceDeMorgan | ||
38 | ] | 63 | ] |