Debian Codegraph @

test/hs/Test/Ganeti/Utils/MultiMap.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for mutli-maps
5
6 -}
7
8 {-
9
10 Copyright (C) 2014 Google Inc.
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are
15 met:
16
17 1. Redistributions of source code must retain the above copyright notice,
18 this list of conditions and the following disclaimer.
19
20 2. Redistributions in binary form must reproduce the above copyright
21 notice, this list of conditions and the following disclaimer in the
22 documentation and/or other materials provided with the distribution.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
26 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
28 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 -}
37
38 module Test.Ganeti.Utils.MultiMap
39 ( testUtils_MultiMap
40 ) where
41
42 import qualified Data.Set as S
43 import qualified Data.Map as M
44
45 import Test.QuickCheck
46
47 import Test.Ganeti.TestHelper
48 import Test.Ganeti.TestCommon
49
50 import Ganeti.Utils.MultiMap as MM
51
52 instance (Arbitrary k, Ord k, Arbitrary v, Ord v)
53 => Arbitrary (MultiMap k v) where
54 arbitrary = frequency
55 [ (1, (multiMap . M.fromList)
56 <$> listOf ((,) <$> arbitrary
57 <*> (S.fromList <$> listOf arbitrary)))
58 , (4, MM.insert <$> arbitrary <*> arbitrary <*> arbitrary)
59 , (1, MM.fromList <$> listOf ((,) <$> arbitrary <*> arbitrary))
60 , (3, MM.delete <$> arbitrary <*> arbitrary <*> arbitrary)
61 , (1, MM.deleteAll <$> arbitrary <*> arbitrary)
62 ]
63
64 -- | A data type for testing extensional equality.
65 data Three = One | Two | Three
66 deriving (Eq, Ord, Show, Enum, Bounded)
67
68 instance Arbitrary Three where
69 arbitrary = elements [minBound..maxBound]
70
71 -- | Tests the extensional equality of multi-maps.
72 prop_MultiMap_equality
73 :: MultiMap Three Three -> MultiMap Three Three -> Property
74 prop_MultiMap_equality m1 m2 =
75 let testKey k = MM.lookup k m1 == MM.lookup k m2
76 in counterexample ("Extensional equality of '" ++ show m1
77 ++ "' and '" ++ show m2 ++ " doesn't match '=='.")
78 $ all testKey [minBound..maxBound] ==? (m1 == m2)
79
80 prop_MultiMap_serialisation :: MultiMap Int Int -> Property
81 prop_MultiMap_serialisation = testSerialisation
82
83 testSuite "Utils/MultiMap"
84 [ 'prop_MultiMap_equality
85 , 'prop_MultiMap_serialisation
86 ]