summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Data/Bitfield.hs5
-rw-r--r--tests/Encoding.hs3
-rw-r--r--tests/Main.hs39
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.
105null :: Bitfield -> Bool
106null Bitfield {..} = S.null bfSet
107
103-- | Count of peer have pieces. 108-- | Count of peer have pieces.
104haveCount :: Bitfield -> PieceCount 109haveCount :: Bitfield -> PieceCount
105haveCount = S.size . bfSet 110haveCount = 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
37instance Arbitrary Bitfield where 37instance Arbitrary Bitfield where
38 arbitrary = mkBitfield <$> (succ <$> positive) <*> arbitrary 38 arbitrary = mkBitfield <$> (succ . min 1000 <$> positive)
39 <*> arbitrary
39 40
40instance Arbitrary PortNumber where 41instance 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
17import Encoding 17import Encoding
18 18
19 19{-----------------------------------------------------------------------
20 20 Bitfield
21instance Arbitrary IntSet where 21-----------------------------------------------------------------------}
22 arbitrary = fromList <$> arbitrary 22-- other properties are tested in IntervalSet
23 23
24prop_completenessRange :: Bitfield -> Bool 24prop_completenessRange :: Bitfield -> Bool
25prop_completenessRange bf = 0 <= c && c <= 1 25prop_completenessRange bf = 0 <= c && c <= 1
26 where 26 where
27 c = completeness bf 27 c = completeness bf
28 28
29prop_minMax :: Bitfield -> Bool
30prop_minMax bf
31 | BF.null bf = True
32 | otherwise = BF.findMin bf <= BF.findMax bf
33
29prop_rarestInRange :: [Bitfield] -> Bool 34prop_rarestInRange :: [Bitfield] -> Bool
30prop_rarestInRange xs = case rarest xs of 35prop_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 -}
41prop_differenceDeMorgan :: Bitfield -> Bitfield -> Bitfield -> Bool
42prop_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
34main :: IO () 57main :: IO ()
35main = defaultMain 58main = 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 ]