]> code.delx.au - offlineimap/blob - testsrc/runtests.hs
Fixed prop_unaryApplyChanges for arbitrary types)
[offlineimap] / testsrc / runtests.hs
1 {-
2 Copyright (C) 2002-2008 John Goerzen <jgoerzen@complete.org>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -}
18
19 module Main where
20 import Test.QuickCheck
21 import Test.QuickCheck.Batch
22 import qualified Test.HUnit as HU
23 import Test.HUnit.Utils
24 import qualified Data.Map as Map
25 import Data.List
26 import Data.Maybe(catMaybes)
27 import System.IO(stderr)
28
29 import Data.Syncable
30 import TestInfrastructure
31
32 prop_empty :: Bool
33 prop_empty =
34 syncBiDir (emptymap::Map.Map Int ()) emptymap emptymap == ([], []) -- ([DeleteItem 5], [], [])
35
36 prop_delAllFromChild :: SyncCollection Int () -> Result
37 prop_delAllFromChild inp =
38 let (resMaster, resChild) = syncBiDir emptymap inp inp
39 expectedResChild = sort . map DeleteItem . Map.keys $ inp
40 in ([], expectedResChild) @=?
41 (resMaster, sort resChild)
42
43 prop_delAllFromMaster :: SyncCollection Int () -> Result
44 prop_delAllFromMaster inp =
45 let (resMaster, resChild) = syncBiDir inp emptymap inp
46 expectedResMaster = sort . map DeleteItem . Map.keys $ inp
47 in (expectedResMaster, []) @=?
48 (sort resMaster, resChild)
49
50 prop_addFromMaster :: SyncCollection Int Float -> Result
51 prop_addFromMaster inp =
52 let (resMaster, resChild) = syncBiDir inp emptymap emptymap
53 expectedResChild = sort . map (\(k, v) -> CopyItem k v) . Map.toList $ inp
54 in ([], expectedResChild) @=?
55 (resMaster, sort resChild)
56
57 -- FIXME: prop_addFromChild
58
59 prop_allChangesToChild :: SyncCollection Int Float -> SyncCollection Int Float -> Result
60 prop_allChangesToChild master child =
61 let (resMaster, resChild) = syncBiDir master child child
62 expectedResChild = sort $
63 (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference master $ child) ++
64 (map DeleteItem . Map.keys . Map.difference child $ master)
65 in ([], expectedResChild) @=?
66 (resMaster, sort resChild)
67
68 prop_allChangesToMaster :: SyncCollection Int Float -> SyncCollection Int Float -> Result
69 prop_allChangesToMaster master child =
70 let (resMaster, resChild) = syncBiDir master child master
71 expectedResMaster = sort $
72 (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference child $ master) ++
73 (map DeleteItem . Map.keys . Map.difference master $ child)
74 in (expectedResMaster, []) @=?
75 (sort resMaster, resChild)
76
77 prop_allChanges :: SyncCollection Int Float -> SyncCollection Int Float -> SyncCollection Int Float -> Result
78 prop_allChanges master child lastchild =
79 let (resMaster, resChild) = syncBiDir master child lastchild
80 expectedResMaster = sort $
81 (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference child $ Map.union master lastchild) ++
82 (map DeleteItem . Map.keys . Map.intersection master $ Map.difference lastchild child)
83 expectedResChild = sort $
84 (map (\(k, v) -> CopyItem k v) . Map.toList . Map.difference master $ Map.union child lastchild) ++
85 (map DeleteItem . Map.keys . Map.intersection child $ Map.difference lastchild master)
86 in (expectedResMaster, expectedResChild) @=?
87 (sort resMaster, sort resChild)
88
89 {- | Basic validation that unaryApplyChanges works -}
90 prop_unaryApplyChanges :: SyncCollection Int Float -> [(Bool, Int, Float)] -> Result
91 prop_unaryApplyChanges collection randcommands =
92 let -- We use nubBy to make sure we don't get input that has reference
93 -- to the same key more than once. We then convert True/False to
94 -- commands.
95 commands = map toCommand . nubBy (\(x1, y1, z1) (x2, y2, z2) -> y1 == y2) $ randcommands
96 toCommand (True, x, v) = CopyItem x v
97 toCommand (False, x, _) = DeleteItem x
98
99 addedItems = catMaybes . map (\x -> case x of CopyItem y v -> Just (y, v); _ -> Nothing) $ commands
100 deletedKeys = catMaybes . map (\x -> case x of DeleteItem y -> Just y; _ -> Nothing) $ commands
101
102 collection' = foldl (flip Map.delete) collection deletedKeys
103 expectedCollection =
104 Map.union collection' (Map.fromList addedItems)
105 in (sort . Map.keys $ expectedCollection) @=?
106 (sort . Map.keys $ unaryApplyChanges collection commands)
107
108 {- | Should validate both that unaryApplyChanges works, and that it is
109 an identify -}
110 prop_unaryApplyChangesId :: SyncCollection Int Float -> SyncCollection Int Float -> Result
111 prop_unaryApplyChangesId master child =
112 let (resMaster, resChild) = syncBiDir master child child
113 newMaster = unaryApplyChanges master resMaster
114 newChild = unaryApplyChanges child resChild
115 newMasterKeys = sort . Map.keys $ newMaster
116 newChildKeys = sort . Map.keys $ newChild
117 in (True, sort (Map.keys master), sort (Map.keys master)) @=?
118 (newMasterKeys == newChildKeys, newMasterKeys, newChildKeys)
119
120 prop_unaryApplyChanges3 :: SyncCollection Int Float -> SyncCollection Int Float -> SyncCollection Int Float -> Result
121 prop_unaryApplyChanges3 master child lastChild =
122 let (resMaster, resChild) = syncBiDir master child lastChild
123 newMaster = unaryApplyChanges master resMaster
124 newChild = unaryApplyChanges child resChild
125 in newMaster @=? newChild
126
127 prop_diffCollection :: SyncCollection Int Float -> SyncCollection Int Float -> Result
128 prop_diffCollection coll1 coll2 =
129 let commands = diffCollection coll1 coll2
130 newcoll2 = unaryApplyChanges coll1 commands
131 in coll2 @=? newcoll2
132
133 allt = [qctest "Empty" prop_empty,
134 qctest "Del all from child" prop_delAllFromChild,
135 qctest "Del all from master" prop_delAllFromMaster,
136 qctest "Add from master" prop_addFromMaster,
137 qctest "All changes to child" prop_allChangesToChild,
138 qctest "All changes to master" prop_allChangesToMaster,
139 qctest "All changes" prop_allChanges,
140 qctest "unaryApplyChanges" prop_unaryApplyChanges,
141 qctest "unaryApplyChangesId" prop_unaryApplyChangesId,
142 qctest "unaryApplyChanges3" prop_unaryApplyChanges3,
143 qctest "diffCollection" prop_diffCollection
144 ]
145
146 testh = HU.runTestTT $ HU.TestList allt
147 testv = runVerbTestText (HU.putTextToHandle stderr True) $ HU.TestList allt
148
149
150 main =
151 do testv
152 return ()
153