diff options
Diffstat (limited to 'tests/Main.hs')
-rw-r--r-- | tests/Main.hs | 58 |
1 files changed, 50 insertions, 8 deletions
diff --git a/tests/Main.hs b/tests/Main.hs index 45f92813..bcadc97e 100644 --- a/tests/Main.hs +++ b/tests/Main.hs | |||
@@ -3,7 +3,7 @@ | |||
3 | {-# LANGUAGE OverloadedStrings #-} | 3 | {-# LANGUAGE OverloadedStrings #-} |
4 | module Main (main) where | 4 | module Main (main) where |
5 | 5 | ||
6 | import Control.Applicative | 6 | import Control.Applicative hiding (empty) |
7 | import Data.ByteString (ByteString) | 7 | import Data.ByteString (ByteString) |
8 | import qualified Data.ByteString as B | 8 | import qualified Data.ByteString as B |
9 | import qualified Data.ByteString.Lazy as Lazy | 9 | import qualified Data.ByteString.Lazy as Lazy |
@@ -16,10 +16,13 @@ import Data.Text as T | |||
16 | 16 | ||
17 | import Network | 17 | import Network |
18 | import Network.URI | 18 | import Network.URI |
19 | import System.Directory | ||
19 | 20 | ||
20 | import Test.Framework (Test, defaultMain) | 21 | import Test.QuickCheck as QC |
22 | import Test.HUnit as HU | ||
23 | import Test.Framework as Framework (Test, defaultMain) | ||
21 | import Test.Framework.Providers.QuickCheck2 (testProperty) | 24 | import Test.Framework.Providers.QuickCheck2 (testProperty) |
22 | import Test.QuickCheck | 25 | import Test.Framework.Providers.HUnit (testCase) |
23 | 26 | ||
24 | import Data.BEncode as BE | 27 | import Data.BEncode as BE |
25 | import Data.Bitfield as BF | 28 | import Data.Bitfield as BF |
@@ -28,6 +31,8 @@ import Network.BitTorrent as BT | |||
28 | import Network.BitTorrent.Exchange.Protocol | 31 | import Network.BitTorrent.Exchange.Protocol |
29 | import Network.BitTorrent.Tracker | 32 | import Network.BitTorrent.Tracker |
30 | import Network.BitTorrent.Peer | 33 | import Network.BitTorrent.Peer |
34 | import System.IO.MMap.Fixed hiding (empty, interval) | ||
35 | import qualified System.IO.MMap.Fixed as Fixed | ||
31 | 36 | ||
32 | -- import Debug.Trace | 37 | -- import Debug.Trace |
33 | 38 | ||
@@ -128,7 +133,7 @@ prop_cerealEncoding _ msgs = S.decode (S.encode msgs) == Right msgs | |||
128 | -- chunk uri by parts) Moreover in practice there should be no | 133 | -- chunk uri by parts) Moreover in practice there should be no |
129 | -- difference. (I think so) | 134 | -- difference. (I think so) |
130 | -- | 135 | -- |
131 | test_scrape_url :: [Test] | 136 | test_scrape_url :: [Framework.Test] |
132 | test_scrape_url = L.zipWith mkTest [1 :: Int ..] (check `L.map` tests) | 137 | test_scrape_url = L.zipWith mkTest [1 :: Int ..] (check `L.map` tests) |
133 | where | 138 | where |
134 | check (iu, ou) = (parseURI iu >>= (`scrapeURL` []) | 139 | check (iu, ou) = (parseURI iu >>= (`scrapeURL` []) |
@@ -202,18 +207,43 @@ prop_messageEncoding msg | |||
202 | = S.decode (S.encode msg) == Right msg | 207 | = S.decode (S.encode msg) == Right msg |
203 | 208 | ||
204 | {----------------------------------------------------------------------- | 209 | {----------------------------------------------------------------------- |
210 | MemMap | ||
211 | -----------------------------------------------------------------------} | ||
212 | |||
213 | boundaryTest = do | ||
214 | f <- mallocTo (Fixed.interval 0 1) Fixed.empty | ||
215 | f <- mallocTo (Fixed.interval 1 2) f | ||
216 | writeElem f 0 (1 :: Word8) | ||
217 | writeElem f 1 (2 :: Word8) | ||
218 | bs <- readBytes (Fixed.interval 0 2) f | ||
219 | "\x1\x2" @=? bs | ||
220 | |||
221 | mmapSingle = do | ||
222 | f <- mmapTo "single.test" (10, 5) 5 Fixed.empty | ||
223 | writeBytes (Fixed.interval 5 5) "abcde" f | ||
224 | bs <- readBytes (Fixed.interval 5 5) f | ||
225 | "abcde" @=? bs | ||
226 | |||
227 | coalesceTest = do | ||
228 | f <- mmapTo "a.test" (0, 1) 10 Fixed.empty | ||
229 | f <- mmapTo "bc.test" (0, 2) 12 f | ||
230 | f <- mmapTo "c.test" (0, 1) 13 f | ||
231 | writeBytes (Fixed.interval 10 4) "abcd" f | ||
232 | bs <- readBytes (Fixed.interval 10 4) f | ||
233 | "abcd" @=? bs | ||
234 | |||
235 | {----------------------------------------------------------------------- | ||
205 | Main | 236 | Main |
206 | -----------------------------------------------------------------------} | 237 | -----------------------------------------------------------------------} |
207 | 238 | ||
208 | main :: IO () | 239 | allTests :: [Framework.Test] |
209 | main = defaultMain $ | 240 | allTests = |
210 | [ -- bitfield module | 241 | [ -- bitfield module |
211 | testProperty "completeness range" prop_completenessRange | 242 | testProperty "completeness range" prop_completenessRange |
212 | , testProperty "rarest in range" prop_rarestInRange | 243 | , testProperty "rarest in range" prop_rarestInRange |
213 | , testProperty "min less that max" prop_minMax | 244 | , testProperty "min less that max" prop_minMax |
214 | , testProperty "difference de morgan" prop_differenceDeMorgan | 245 | , testProperty "difference de morgan" prop_differenceDeMorgan |
215 | 246 | -- torrent module | |
216 | -- torrent module | ||
217 | , testProperty "file info encoding" $ | 247 | , testProperty "file info encoding" $ |
218 | prop_properBEncode (T :: T FileInfo) | 248 | prop_properBEncode (T :: T FileInfo) |
219 | , testProperty "content info encoding" $ | 249 | , testProperty "content info encoding" $ |
@@ -226,4 +256,16 @@ main = defaultMain $ | |||
226 | prop_cerealEncoding (T :: T Handshake) | 256 | prop_cerealEncoding (T :: T Handshake) |
227 | , testProperty "message encoding" prop_messageEncoding | 257 | , testProperty "message encoding" prop_messageEncoding |
228 | 258 | ||
259 | -- mem map | ||
260 | , testCase "boudary" boundaryTest | ||
261 | , testCase "single" mmapSingle | ||
262 | , testCase "coalesce" coalesceTest | ||
229 | ] ++ test_scrape_url | 263 | ] ++ test_scrape_url |
264 | |||
265 | main :: IO () | ||
266 | main = do | ||
267 | let tmpdir = "tmp" -- for mem map test cases | ||
268 | createDirectoryIfMissing True tmpdir | ||
269 | setCurrentDirectory tmpdir | ||
270 | |||
271 | defaultMain allTests | ||