summaryrefslogtreecommitdiff
path: root/tests/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Main.hs')
-rw-r--r--tests/Main.hs58
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 #-}
4module Main (main) where 4module Main (main) where
5 5
6import Control.Applicative 6import Control.Applicative hiding (empty)
7import Data.ByteString (ByteString) 7import Data.ByteString (ByteString)
8import qualified Data.ByteString as B 8import qualified Data.ByteString as B
9import qualified Data.ByteString.Lazy as Lazy 9import qualified Data.ByteString.Lazy as Lazy
@@ -16,10 +16,13 @@ import Data.Text as T
16 16
17import Network 17import Network
18import Network.URI 18import Network.URI
19import System.Directory
19 20
20import Test.Framework (Test, defaultMain) 21import Test.QuickCheck as QC
22import Test.HUnit as HU
23import Test.Framework as Framework (Test, defaultMain)
21import Test.Framework.Providers.QuickCheck2 (testProperty) 24import Test.Framework.Providers.QuickCheck2 (testProperty)
22import Test.QuickCheck 25import Test.Framework.Providers.HUnit (testCase)
23 26
24import Data.BEncode as BE 27import Data.BEncode as BE
25import Data.Bitfield as BF 28import Data.Bitfield as BF
@@ -28,6 +31,8 @@ import Network.BitTorrent as BT
28import Network.BitTorrent.Exchange.Protocol 31import Network.BitTorrent.Exchange.Protocol
29import Network.BitTorrent.Tracker 32import Network.BitTorrent.Tracker
30import Network.BitTorrent.Peer 33import Network.BitTorrent.Peer
34import System.IO.MMap.Fixed hiding (empty, interval)
35import 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--
131test_scrape_url :: [Test] 136test_scrape_url :: [Framework.Test]
132test_scrape_url = L.zipWith mkTest [1 :: Int ..] (check `L.map` tests) 137test_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
213boundaryTest = 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
221mmapSingle = 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
227coalesceTest = 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
208main :: IO () 239allTests :: [Framework.Test]
209main = defaultMain $ 240allTests =
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
265main :: IO ()
266main = do
267 let tmpdir = "tmp" -- for mem map test cases
268 createDirectoryIfMissing True tmpdir
269 setCurrentDirectory tmpdir
270
271 defaultMain allTests