summaryrefslogtreecommitdiff
path: root/tests
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
parent5570963d8b22713d4f6ed9c0e2c7f686d5bc75da (diff)
New storage
Diffstat (limited to 'tests')
-rw-r--r--tests/Data/Torrent/LayoutSpec.hs30
-rw-r--r--tests/Data/Torrent/MetainfoSpec.hs14
-rw-r--r--tests/System/Torrent/FileMapSpec.hs81
3 files changed, 112 insertions, 13 deletions
diff --git a/tests/Data/Torrent/LayoutSpec.hs b/tests/Data/Torrent/LayoutSpec.hs
new file mode 100644
index 00000000..d3966b3f
--- /dev/null
+++ b/tests/Data/Torrent/LayoutSpec.hs
@@ -0,0 +1,30 @@
1{-# LANGUAGE GeneralizedNewtypeDeriving #-}
2{-# LANGUAGE StandaloneDeriving #-}
3module Data.Torrent.LayoutSpec (spec) where
4
5import Control.Applicative
6import Test.Hspec
7import Test.QuickCheck
8import System.Posix.Types
9
10import Data.Torrent.Layout
11
12
13instance Arbitrary COff where
14 arbitrary = fromIntegral <$> (arbitrary :: Gen Int)
15
16instance Arbitrary a => Arbitrary (FileInfo a) where
17 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
18
19instance Arbitrary LayoutInfo where
20 arbitrary = oneof
21 [ SingleFile <$> arbitrary
22 , MultiFile <$> arbitrary <*> arbitrary
23 ]
24
25spec :: Spec
26spec = do
27 describe "accumPosition" $ do
28 it "" $ property $ \ p1 p2 p3 s1 s2 s3 ->
29 accumPositions [(p1, s1), (p2, s2), (p3, s3)]
30 `shouldBe` [(p1, (0, s1)), (p2, (s1, s2)), (p3, (s1 + s2, s3))] \ No newline at end of file
diff --git a/tests/Data/Torrent/MetainfoSpec.hs b/tests/Data/Torrent/MetainfoSpec.hs
index 636bb6b1..b5716e07 100644
--- a/tests/Data/Torrent/MetainfoSpec.hs
+++ b/tests/Data/Torrent/MetainfoSpec.hs
@@ -16,7 +16,7 @@ import Test.QuickCheck.Instances ()
16import Data.Torrent.Piece 16import Data.Torrent.Piece
17import Data.Torrent.Layout 17import Data.Torrent.Layout
18import Data.Torrent 18import Data.Torrent
19 19import Data.Torrent.LayoutSpec ()
20 20
21{----------------------------------------------------------------------- 21{-----------------------------------------------------------------------
22-- Common 22-- Common
@@ -38,18 +38,6 @@ instance Arbitrary URI where
38-- Instances 38-- Instances
39-----------------------------------------------------------------------} 39-----------------------------------------------------------------------}
40 40
41instance Arbitrary FileSize where
42 arbitrary = fromIntegral <$> (arbitrary :: Gen Int)
43
44instance Arbitrary a => Arbitrary (FileInfo a) where
45 arbitrary = FileInfo <$> arbitrary <*> arbitrary <*> arbitrary
46
47instance Arbitrary LayoutInfo where
48 arbitrary = oneof
49 [ SingleFile <$> arbitrary
50 , MultiFile <$> arbitrary <*> arbitrary
51 ]
52
53instance Arbitrary HashArray where 41instance Arbitrary HashArray where
54 arbitrary = HashArray <$> arbitrary 42 arbitrary = HashArray <$> arbitrary
55 43
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