diff options
Diffstat (limited to 'tests/Main.hs')
-rw-r--r-- | tests/Main.hs | 118 |
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 | |||
10 | import Data.Bitfield as BT | 10 | import Data.Bitfield as BT |
11 | import Network.BitTorrent as BT | 11 | import Network.BitTorrent as BT |
12 | 12 | ||
13 | |||
14 | prop_bitfieldDiff0 :: Bitfield -> Bool | ||
15 | prop_bitfieldDiff0 b = (b `difference` empty (8 * bitfieldByteCount b)) == b | ||
16 | |||
17 | prop_bitfieldDiff1 :: Bitfield -> Bool | ||
18 | prop_bitfieldDiff1 b = em `difference` b == em | ||
19 | where | ||
20 | em = empty (8 * bitfieldByteCount b) | ||
21 | |||
22 | prop_bitfieldMaxNothing :: Int -> Bool | ||
23 | prop_bitfieldMaxNothing n = findMax (empty (n `mod` 1024)) == Nothing | ||
24 | |||
25 | prop_bitfieldMinNothing :: Int -> Bool | ||
26 | prop_bitfieldMinNothing n = findMax (empty (n `mod` 1024)) == Nothing | ||
27 | |||
28 | prop_bitfieldMaxJust :: Word -> Bool | ||
29 | prop_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 | |||
36 | prop_bitfieldMinCases :: Bool | ||
37 | prop_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 | |||
44 | prop_bitfieldMaxCases :: Bool | ||
45 | prop_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 | |||
52 | prop_bitfieldMinJust :: Word -> Bool | ||
53 | prop_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 | |||
58 | prop_bitfieldUnionIdentity :: Bitfield -> Bool | ||
59 | prop_bitfieldUnionIdentity b = | ||
60 | ((b `union` empty (8 * bitfieldByteCount b)) == b) | ||
61 | && ((empty (8 * bitfieldByteCount b) `union` b) == b) | ||
62 | |||
63 | prop_bitfieldUnionCommutative :: Bitfield -> Bitfield -> Bool | ||
64 | prop_bitfieldUnionCommutative a b = union a b == union b a | ||
65 | |||
66 | prop_bitfieldUnionAssociative :: Bitfield -> Bitfield -> Bitfield -> Bool | ||
67 | prop_bitfieldUnionAssociative a b c = union a (union b c) == union (union a b) c | ||
68 | |||
69 | prop_bitfieldUnionIdempotent :: Bitfield -> Bitfield -> Bool | ||
70 | prop_bitfieldUnionIdempotent a b = union a b == union a (union a b) | ||
71 | |||
72 | prop_bitfieldIntersectionIdentity :: Bitfield -> Bool | ||
73 | prop_bitfieldIntersectionIdentity b = | ||
74 | ((b `intersection` full (8 * bitfieldByteCount b)) == b) | ||
75 | && ((full (8 * bitfieldByteCount b) `intersection` b) == b) | ||
76 | |||
77 | prop_bitfieldIntersectionCommutative :: Bitfield -> Bitfield -> Bool | ||
78 | prop_bitfieldIntersectionCommutative a b = intersection a b == intersection b a | ||
79 | |||
80 | prop_bitfieldIntersectionAssociative :: Bitfield -> Bitfield -> Bitfield -> Bool | ||
81 | prop_bitfieldIntersectionAssociative a b c = | ||
82 | intersection a (intersection b c) == intersection (intersection a b) c | ||
83 | |||
84 | prop_bitfieldIntersectionIndempotent :: Bitfield -> Bitfield -> Bool | ||
85 | prop_bitfieldIntersectionIndempotent a b = f b == f (f b) | ||
86 | where | ||
87 | f = intersection a | ||
88 | |||
89 | prop_bitfieldHaveCount :: Bitfield -> Bool | ||
90 | prop_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 | |||
97 | prop_bitfieldCompeteness :: Bitfield -> Bool | ||
98 | prop_bitfieldCompeteness b = let (have, total) = completeness b in have <= total | ||
99 | |||
100 | main :: IO () | 13 | main :: IO () |
101 | main = defaultMain $ | 14 | main = 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 | ||