summaryrefslogtreecommitdiff
path: root/tests/System/Torrent/FileMapSpec.hs
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-12-03 16:15:32 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-12-03 16:15:32 +0400
commitd6a0442a56d7b977d5f1d1d162517c9086c413eb (patch)
tree83a1de6acdd77c7bc1ae60c7418a6f43927251c6 /tests/System/Torrent/FileMapSpec.hs
parent5570963d8b22713d4f6ed9c0e2c7f686d5bc75da (diff)
New storage
Diffstat (limited to 'tests/System/Torrent/FileMapSpec.hs')
-rw-r--r--tests/System/Torrent/FileMapSpec.hs81
1 files changed, 81 insertions, 0 deletions
diff --git a/tests/System/Torrent/FileMapSpec.hs b/tests/System/Torrent/FileMapSpec.hs
new file mode 100644
index 00000000..90ba3ab9
--- /dev/null
+++ b/tests/System/Torrent/FileMapSpec.hs
@@ -0,0 +1,81 @@
1-- this is test string used in the 'spec' --- don't touch me!
2module System.Torrent.FileMapSpec (spec) where
3
4import Control.Monad.Loops
5import Data.List as L
6import Data.ByteString.Lazy as BL
7import System.Directory
8import System.FilePath
9import System.IO.Unsafe
10import Test.Hspec
11
12import Data.Torrent.Layout
13import System.Torrent.FileMap as FM
14
15
16layout :: FileLayout FileSize
17layout =
18 [ (dir </> "a", 2)
19 , (dir </> "b", 3)
20 , (dir </> "c", 2)
21 ]
22 where
23 dir = unsafePerformIO $ getTemporaryDirectory
24
25spec :: Spec
26spec = do
27 describe "FileMap" $ do
28 it "creates new files" $ do
29 m <- mmapFiles ReadWriteEx layout
30 unmapFiles m
31
32 (doesFileExist . fst) `allM` layout
33 `shouldReturn` True
34
35 it "have specified size" $ do
36 m <- mmapFiles ReadOnly layout
37 FM.size m `shouldBe` L.sum (L.map snd layout)
38 unmapFiles m
39
40 it "read from files" $ do
41 let thisFile = [("tests/System/Torrent/FileMapSpec.hs", 15)]
42 m <- mmapFiles ReadOnly thisFile
43 readBytes 3 15 m `shouldReturn` "this is test"
44 unmapFiles m
45
46 it "writes to files" $ do
47 m <- mmapFiles ReadWriteEx layout
48 writeBytes 0 "a" m
49 readBytes 0 1 m `shouldReturn` "a"
50 writeBytes 1 "ab" m
51 readBytes 1 2 m `shouldReturn` "ab"
52 writeBytes 3 "b" m
53 readBytes 3 1 m `shouldReturn` "b"
54 writeBytes 4 "bc" m
55 readBytes 4 2 m `shouldReturn` "bc"
56 writeBytes 6 "c" m
57 readBytes 6 1 m `shouldReturn` "c"
58 readBytes 0 7 m `shouldReturn` "aabbbcc"
59 unmapFiles m
60
61 BL.readFile (fst (layout !! 0)) `shouldReturn` "aa"
62 BL.readFile (fst (layout !! 1)) `shouldReturn` "bbb"
63 BL.readFile (fst (layout !! 2)) `shouldReturn` "cc"
64
65 it "no buffer underflow errors" $ do
66 m <- mmapFiles ReadOnly layout
67 readBytes (-1) 1 m `shouldReturn` ""
68 readBytes (-5) 12 m `shouldReturn` ""
69 unmapFiles m
70
71 it "no buffer overflow errors" $ do
72 m <- mmapFiles ReadOnly layout
73 writeBytes 5 "ddddddddd" m -- cause segfault
74 unmapFiles m
75
76 BL.readFile (fst (layout !! 2)) `shouldReturn` "dd"
77
78 it "isomorphic to lazy bytestring" $ do
79 m <- mmapFiles ReadOnly layout
80 fromLazyByteString (toLazyByteString m) `shouldBe` m
81 unmapFiles m \ No newline at end of file