diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-03 16:15:32 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-12-03 16:15:32 +0400 |
commit | d6a0442a56d7b977d5f1d1d162517c9086c413eb (patch) | |
tree | 83a1de6acdd77c7bc1ae60c7418a6f43927251c6 /tests/System/Torrent/FileMapSpec.hs | |
parent | 5570963d8b22713d4f6ed9c0e2c7f686d5bc75da (diff) |
New storage
Diffstat (limited to 'tests/System/Torrent/FileMapSpec.hs')
-rw-r--r-- | tests/System/Torrent/FileMapSpec.hs | 81 |
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! | ||
2 | module System.Torrent.FileMapSpec (spec) where | ||
3 | |||
4 | import Control.Monad.Loops | ||
5 | import Data.List as L | ||
6 | import Data.ByteString.Lazy as BL | ||
7 | import System.Directory | ||
8 | import System.FilePath | ||
9 | import System.IO.Unsafe | ||
10 | import Test.Hspec | ||
11 | |||
12 | import Data.Torrent.Layout | ||
13 | import System.Torrent.FileMap as FM | ||
14 | |||
15 | |||
16 | layout :: FileLayout FileSize | ||
17 | layout = | ||
18 | [ (dir </> "a", 2) | ||
19 | , (dir </> "b", 3) | ||
20 | , (dir </> "c", 2) | ||
21 | ] | ||
22 | where | ||
23 | dir = unsafePerformIO $ getTemporaryDirectory | ||
24 | |||
25 | spec :: Spec | ||
26 | spec = 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 | ||