summaryrefslogtreecommitdiff
path: root/tests/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Main.hs')
-rw-r--r--tests/Main.hs118
1 files changed, 1 insertions, 117 deletions
diff --git a/tests/Main.hs b/tests/Main.hs
index 9b0d58e4..bc3f7809 100644
--- a/tests/Main.hs
+++ b/tests/Main.hs
@@ -10,121 +10,5 @@ import Encoding
10import Data.Bitfield as BT 10import Data.Bitfield as BT
11import Network.BitTorrent as BT 11import Network.BitTorrent as BT
12 12
13
14prop_bitfieldDiff0 :: Bitfield -> Bool
15prop_bitfieldDiff0 b = (b `difference` empty (8 * bitfieldByteCount b)) == b
16
17prop_bitfieldDiff1 :: Bitfield -> Bool
18prop_bitfieldDiff1 b = em `difference` b == em
19 where
20 em = empty (8 * bitfieldByteCount b)
21
22prop_bitfieldMaxNothing :: Int -> Bool
23prop_bitfieldMaxNothing n = findMax (empty (n `mod` 1024)) == Nothing
24
25prop_bitfieldMinNothing :: Int -> Bool
26prop_bitfieldMinNothing n = findMax (empty (n `mod` 1024)) == Nothing
27
28prop_bitfieldMaxJust :: Word -> Bool
29prop_bitfieldMaxJust n =
30 let m = findMax (full (8 * s)) in
31 if n == 0 then m == Nothing
32 else m == Just ((s * 8) - 1)
33 where
34 s = fromIntegral n `mod` 1024
35
36prop_bitfieldMinCases :: Bool
37prop_bitfieldMinCases = all mkTestCase
38 [ ("\x0\x3", Just 8)
39 , ("\x0\x127", Just 8)
40 ]
41 where
42 mkTestCase (bs, res) = findMin (MkBitfield bs) == res
43
44prop_bitfieldMaxCases :: Bool
45prop_bitfieldMaxCases = all mkTestCase
46 [ ("\x0\x3", Just 9)
47 , ("\x0\x127", Just 13)
48 ]
49 where
50 mkTestCase (bs, res) = findMax (MkBitfield bs) == res
51
52prop_bitfieldMinJust :: Word -> Bool
53prop_bitfieldMinJust n =
54 let m = findMin (full (fromIntegral n `mod` 1024)) in
55 if n == 0 then m == Nothing
56 else m == Just 0
57
58prop_bitfieldUnionIdentity :: Bitfield -> Bool
59prop_bitfieldUnionIdentity b =
60 ((b `union` empty (8 * bitfieldByteCount b)) == b)
61 && ((empty (8 * bitfieldByteCount b) `union` b) == b)
62
63prop_bitfieldUnionCommutative :: Bitfield -> Bitfield -> Bool
64prop_bitfieldUnionCommutative a b = union a b == union b a
65
66prop_bitfieldUnionAssociative :: Bitfield -> Bitfield -> Bitfield -> Bool
67prop_bitfieldUnionAssociative a b c = union a (union b c) == union (union a b) c
68
69prop_bitfieldUnionIdempotent :: Bitfield -> Bitfield -> Bool
70prop_bitfieldUnionIdempotent a b = union a b == union a (union a b)
71
72prop_bitfieldIntersectionIdentity :: Bitfield -> Bool
73prop_bitfieldIntersectionIdentity b =
74 ((b `intersection` full (8 * bitfieldByteCount b)) == b)
75 && ((full (8 * bitfieldByteCount b) `intersection` b) == b)
76
77prop_bitfieldIntersectionCommutative :: Bitfield -> Bitfield -> Bool
78prop_bitfieldIntersectionCommutative a b = intersection a b == intersection b a
79
80prop_bitfieldIntersectionAssociative :: Bitfield -> Bitfield -> Bitfield -> Bool
81prop_bitfieldIntersectionAssociative a b c =
82 intersection a (intersection b c) == intersection (intersection a b) c
83
84prop_bitfieldIntersectionIndempotent :: Bitfield -> Bitfield -> Bool
85prop_bitfieldIntersectionIndempotent a b = f b == f (f b)
86 where
87 f = intersection a
88
89prop_bitfieldHaveCount :: Bitfield -> Bool
90prop_bitfieldHaveCount b = listHaveCount (toList b) == haveCount b
91 where
92 listHaveCount = foldr f 0
93
94 f :: Bool -> Int -> Int
95 f byte count = fromEnum byte + count
96
97prop_bitfieldCompeteness :: Bitfield -> Bool
98prop_bitfieldCompeteness b = let (have, total) = completeness b in have <= total
99
100main :: IO () 13main :: IO ()
101main = defaultMain $ 14main = defaultMain []
102 [ testProperty "Message encode <-> decode" $ prop_encoding (T :: T Message)
103 , testProperty "PeerID encode <-> decode" $ prop_encoding (T :: T PeerID)
104 , testProperty "Handshake encode <-> decode" $ prop_encoding (T :: T Handshake)
105 ]
106 ++ test_scrape_url ++
107 [
108 testProperty "bitfield `difference` empty bitfield" prop_bitfieldDiff0
109 , testProperty "empty bitfield `difference` bitfield" prop_bitfieldDiff1
110
111 , testProperty "prop_bitfieldMinNothing" prop_bitfieldMinNothing
112 , testProperty "prop_bitfieldMaxNothing" prop_bitfieldMaxNothing
113 , testProperty "prop_bitfieldMaxJust" prop_bitfieldMaxJust
114 , testProperty "prop_bitfieldMinJust" prop_bitfieldMinJust
115 , testProperty "prop_bitfieldMinCases" prop_bitfieldMinCases
116 , testProperty "prop_bitfieldMaxCases" prop_bitfieldMaxCases
117
118 , testProperty "prop_bitfieldUnionIdentity" prop_bitfieldUnionIdentity
119 , testProperty "prop_bitfieldUnionCommutative" prop_bitfieldUnionCommutative
120 , testProperty "prop_bitfieldUnionAssociative" prop_bitfieldUnionAssociative
121 , testProperty "prop_bitfieldUnionIdempotent" prop_bitfieldUnionIdempotent
122
123 , testProperty "prop_bitfieldIntersectionIdentity" prop_bitfieldIntersectionIdentity
124 , testProperty "prop_bitfieldIntersectionCommutative" prop_bitfieldIntersectionCommutative
125 , testProperty "prop_bitfieldIntersectionAssociative" prop_bitfieldIntersectionAssociative
126 , testProperty "prop_bitfieldIntersectionIndempotent" prop_bitfieldIntersectionIndempotent
127
128 , testProperty "prop_bitfieldHaveCount" prop_bitfieldHaveCount
129 , testProperty "prop_bitfieldCompeteness" prop_bitfieldCompeteness
130 ] \ No newline at end of file