Debian Codegraph @

test/hs/Test/Ganeti/HTools/Graph.hs

raw
1 {-# LANGUAGE TemplateHaskell #-}
2 {-# OPTIONS_GHC -fno-warn-orphans #-}
3
4 {-| Unittests for Ganeti.Htools.Graph
5
6 -}
7
8 {-
9
10 Copyright (C) 2009, 2010, 2011, 2012 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.HTools.Graph (testHTools_Graph) where
39
40 import Test.QuickCheck
41 import Test.HUnit
42
43 import Test.Ganeti.TestHelper
44 import Test.Ganeti.TestCommon
45
46 import Ganeti.HTools.Graph
47
48 import qualified Data.Graph as Graph
49 import qualified Data.IntMap as IntMap
50
51 {-# ANN module "HLint: ignore Use camelCase" #-}
52
53 data TestableGraph = TestableGraph Graph.Graph deriving (Show)
54 data TestableClique = TestableClique Graph.Graph deriving (Show)
55
56 -- | Generate node bounds and edges for an undirected graph.
57 -- A graph is undirected if for every (a, b) edge there is a
58 -- corresponding (b, a) one.
59 undirEdges :: Gen (Graph.Bounds, [Graph.Edge])
60 undirEdges = sized undirEdges'
61 where
62 undirEdges' 0 = return ((0, 0), [])
63 undirEdges' n = do
64 maxv <- choose (1, n)
65 edges <- listOf1 $ do
66 i <- choose (0, maxv)
67 j <- choose (0, maxv) `suchThat` (/= i)
68 return [(i, j), (j, i)]
69 return ((0, maxv), concat edges)
70
71 -- | Generate node bounds and edges for a clique.
72 -- In a clique all nodes are directly connected to each other.
73 cliqueEdges :: Gen (Graph.Bounds, [Graph.Edge])
74 cliqueEdges = sized cliqueEdges'
75 where
76 cliqueEdges' 0 = return ((0, 0), [])
77 cliqueEdges' n = do
78 maxv <- choose (0, n)
79 let edges = [(x, y) | x <- [0..maxv], y <- [0..maxv], x /= y]
80 return ((0, maxv), edges)
81
82 instance Arbitrary TestableGraph where
83 arbitrary = do
84 (mybounds, myedges) <- undirEdges
85 return . TestableGraph $ Graph.buildG mybounds myedges
86
87 instance Arbitrary TestableClique where
88 arbitrary = do
89 (mybounds, myedges) <- cliqueEdges
90 return . TestableClique $ Graph.buildG mybounds myedges
91
92 -- | Check that the empty vertex color map is empty.
93 case_emptyVertColorMapNull :: Assertion
94 case_emptyVertColorMapNull = assertBool "" $ IntMap.null emptyVertColorMap
95
96 -- | Check that the empty vertex color map is zero in size.
97 case_emptyVertColorMapEmpty :: Assertion
98 case_emptyVertColorMapEmpty =
99 assertEqual "" 0 $ IntMap.size emptyVertColorMap
100
101 -- | Check if each two consecutive elements on a list
102 -- respect a given condition.
103 anyTwo :: (a -> a -> Bool) -> [a] -> Bool
104 anyTwo _ [] = True
105 anyTwo _ [_] = True
106 anyTwo op (x:y:xs) = (x `op` y) && anyTwo op (y:xs)
107
108 -- | Check order of vertices returned by verticesByDegreeAsc.
109 prop_verticesByDegreeAscAsc :: TestableGraph -> Bool
110 prop_verticesByDegreeAscAsc (TestableGraph g) = anyTwo (<=) (degrees asc)
111 where degrees = map (length . neighbors g)
112 asc = verticesByDegreeAsc g
113
114 -- | Check order of vertices returned by verticesByDegreeDesc.
115 prop_verticesByDegreeDescDesc :: TestableGraph -> Bool
116 prop_verticesByDegreeDescDesc (TestableGraph g) = anyTwo (>=) (degrees desc)
117 where degrees = map (length . neighbors g)
118 desc = verticesByDegreeDesc g
119
120 -- | Check that our generated graphs are colorable
121 prop_isColorableTestableGraph :: TestableGraph -> Bool
122 prop_isColorableTestableGraph (TestableGraph g) = isColorable g
123
124 -- | Check that our generated graphs are colorable
125 prop_isColorableTestableClique :: TestableClique -> Bool
126 prop_isColorableTestableClique (TestableClique g) = isColorable g
127
128 -- | Check that the given algorithm colors a clique with the same number of
129 -- colors as the vertices number.
130 prop_colorClique :: (Graph.Graph -> VertColorMap) -> TestableClique -> Property
131 prop_colorClique alg (TestableClique g) = numvertices ==? numcolors
132 where numcolors = (IntMap.size . colorVertMap) $ alg g
133 numvertices = length (Graph.vertices g)
134
135 -- | Specific check for the LF algorithm.
136 prop_colorLFClique :: TestableClique -> Property
137 prop_colorLFClique = prop_colorClique colorLF
138
139 -- | Specific check for the Dsatur algorithm.
140 prop_colorDsaturClique :: TestableClique -> Property
141 prop_colorDsaturClique = prop_colorClique colorDsatur
142
143 -- | Specific check for the Dcolor algorithm.
144 prop_colorDcolorClique :: TestableClique -> Property
145 prop_colorDcolorClique = prop_colorClique colorDcolor
146
147 -- Check that all nodes are colored.
148 prop_colorAllNodes :: (Graph.Graph -> VertColorMap)
149 -> TestableGraph
150 -> Property
151 prop_colorAllNodes alg (TestableGraph g) = numvertices ==? numcolored
152 where numcolored = IntMap.foldr ((+) . length) 0 vcMap
153 vcMap = colorVertMap $ alg g
154 numvertices = length (Graph.vertices g)
155
156 -- | Specific check for the LF algorithm.
157 prop_colorLFAllNodes :: TestableGraph -> Property
158 prop_colorLFAllNodes = prop_colorAllNodes colorLF
159
160 -- | Specific check for the Dsatur algorithm.
161 prop_colorDsaturAllNodes :: TestableGraph -> Property
162 prop_colorDsaturAllNodes = prop_colorAllNodes colorDsatur
163
164 -- | Specific check for the Dcolor algorithm.
165 prop_colorDcolorAllNodes :: TestableGraph -> Property
166 prop_colorDcolorAllNodes = prop_colorAllNodes colorDcolor
167
168 -- | Check that no two vertices sharing the same edge have the same color.
169 prop_colorProper :: (Graph.Graph -> VertColorMap) -> TestableGraph -> Bool
170 prop_colorProper alg (TestableGraph g) = all isEdgeOk $ Graph.edges g
171 where isEdgeOk :: Graph.Edge -> Bool
172 isEdgeOk (v1, v2) = color v1 /= color v2
173 color v = cMap IntMap.! v
174 cMap = alg g
175
176 -- | Specific check for the LF algorithm.
177 prop_colorLFProper :: TestableGraph -> Bool
178 prop_colorLFProper = prop_colorProper colorLF
179
180 -- | Specific check for the Dsatur algorithm.
181 prop_colorDsaturProper :: TestableGraph -> Bool
182 prop_colorDsaturProper = prop_colorProper colorDsatur
183
184 -- | Specific check for the Dcolor algorithm.
185 prop_colorDcolorProper :: TestableGraph -> Bool
186 prop_colorDcolorProper = prop_colorProper colorDcolor
187
188 -- | List of tests for the Graph module.
189 testSuite "HTools/Graph"
190 [ 'case_emptyVertColorMapNull
191 , 'case_emptyVertColorMapEmpty
192 , 'prop_verticesByDegreeAscAsc
193 , 'prop_verticesByDegreeDescDesc
194 , 'prop_colorLFClique
195 , 'prop_colorDsaturClique
196 , 'prop_colorDcolorClique
197 , 'prop_colorLFAllNodes
198 , 'prop_colorDsaturAllNodes
199 , 'prop_colorDcolorAllNodes
200 , 'prop_colorLFProper
201 , 'prop_colorDsaturProper
202 , 'prop_colorDcolorProper
203 , 'prop_isColorableTestableGraph
204 , 'prop_isColorableTestableClique
205 ]