File: AnyDBMtest.hs

package info (click to toggle)
missingh 0.10.7.sarge.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,240 kB
  • ctags: 12
  • sloc: haskell: 9,729; makefile: 148
file content (119 lines) | stat: -rw-r--r-- 4,712 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{- arch-tag: AnyDBM tests main file
Copyright (C) 2004-2005 John Goerzen <jgoerzen@complete.org>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-}

module AnyDBMtest(mf, generic_persist_test, generic_test, tests) where
import HUnit
import MissingH.List
import MissingH.IO.HVFS
import MissingH.IO.HVFS.InstanceHelpers
import MissingH.AnyDBM
import MissingH.AnyDBM.StringDBM
import MissingH.AnyDBM.FiniteMapDBM
import System.Directory
import MissingH.IO.HVFS.Utils
import Data.HashTable
import Data.List(sort)
import Control.Exception(finally)

mf :: AnyDBM a => IO b -> (b -> IO a) -> String -> (a -> Assertion) -> Test
mf initfunc openfunc msg code =
    TestLabel msg $ TestCase $ do i <- initfunc
                                  h <- openfunc i
                                  finally (code h) (closeA h)
        
infix 1 @>=?
(@>=?) :: (Eq a, Show a) => a -> IO a -> Assertion
(@>=?) exp res = do r <- res
                    exp @=? r

deleteall h = do k <- keysA h
                 mapM_ (deleteA h) k
                 [] @>=? keysA h

weirdl = sort $ [("", "empty"), 
                 ("foo\nbar", "v1\0v2"),
                 ("v3,v4", ""),
                 ("k\0ey", "\xFF")]

createdir = TestCase $ createDirectory "testtmp"
removedir = TestCase $ recursiveRemove SystemFS "testtmp"

generic_test initfunc openfunc =
    let f = mf initfunc openfunc in
        [
         createdir
        ,f "empty" $ \h -> do [] @>=? keysA h
                              [] @>=? valuesA h
                              [] @>=? toListA h
                              Nothing @>=? lookupA h "foo"
                     
        ,f "basic" $ \h -> do insertA h "key" "value"
                              (Just "value") @>=? lookupA h "key"
                              [("key", "value")] @>=? toListA h
                              insertA h "key" "v2"
                              [("key", "v2")] @>=? toListA h
                              deleteA h "key"
                              [] @>=? toListA h
        ,f "mult" $ \h -> do insertListA h [("1", "2"), ("3", "4"), ("5", "6")]
                             [("1", "2"), ("3", "4"), ("5", "6")] @>=? 
                                (toListA h >>= return . sort)
                             ["1", "3", "5"] @>=? (keysA h >>= return . sort)
                             ["2", "4", "6"] @>=? (valuesA h >>= return . sort)
                             deleteall h
        ,f "weirdchars" $ \h -> do insertListA h weirdl
                                   weirdl @>=? (toListA h >>= return . sort)
                                   deleteall h
        ,removedir
        ]

generic_persist_test initfunc openfunc =
    let f = mf initfunc openfunc in
        [
         createdir
        ,f "empty" deleteall 
        ,f "weirdpop" $ \h -> insertListA h weirdl
        ,f "weirdcheck" $ \h -> do weirdl @>=? (toListA h >>= return . sort)
                                   deleteall h
                                   insertA h "key" "value"
        ,f "step3" $ \h -> do [("key", "value")] @>=? (toListA h >>= return . sort)
                              insertA h "key" "v2"
                              insertA h "z" "y"
        ,f "step4" $ \h -> do [("key", "v2"), ("z", "y")] @>=?
                                 (toListA h >>= return . sort)
        ,f "cleanupdb" deleteall
        ,removedir
        ]

test_hashtable = generic_test (return ())
                  (\_ -> ((new (==) hashString)::IO (HashTable String String)))

test_finitemap = generic_test (return ())
                  (\_ -> newFiniteMapDBM)
test_stringdbm = generic_persist_test (return SystemFS)
                   (\f -> openStringVDBM f "testtmp/StringDBM" ReadWriteMode)
                 ++
                 generic_test (return SystemFS)
                   (\f -> openStringVDBM f "testtmp/StringDBM" ReadWriteMode)

tests = TestList [TestLabel "HashTable" (TestList test_hashtable),
                  TestLabel "StringDBM" (TestList test_stringdbm),
                  TestLabel "FiniteMap" (TestList test_finitemap)
                 ]