module Test.Ganeti.HTools.Graph (testHTools_Graph) where
import Test.QuickCheck
import Test.HUnit
import Test.Ganeti.TestHelper
import Test.Ganeti.TestCommon
import Ganeti.HTools.Graph
import qualified Data.Graph as Graph
import qualified Data.IntMap as IntMap
data TestableGraph = TestableGraph Graph.Graph deriving (Show)
data TestableClique = TestableClique Graph.Graph deriving (Show)
undirEdges :: Gen (Graph.Bounds, [Graph.Edge])
undirEdges = sized undirEdges'
where
undirEdges' 0 = return ((0, 0), [])
undirEdges' n = do
maxv <- choose (1, n)
edges <- listOf1 $ do
i <- choose (0, maxv)
j <- choose (0, maxv) `suchThat` (/= i)
return [(i, j), (j, i)]
return ((0, maxv), concat edges)
cliqueEdges :: Gen (Graph.Bounds, [Graph.Edge])
cliqueEdges = sized cliqueEdges'
where
cliqueEdges' 0 = return ((0, 0), [])
cliqueEdges' n = do
maxv <- choose (0, n)
let edges = [(x, y) | x <- [0..maxv], y <- [0..maxv], x /= y]
return ((0, maxv), edges)
instance Arbitrary TestableGraph where
arbitrary = do
(mybounds, myedges) <- undirEdges
return . TestableGraph $ Graph.buildG mybounds myedges
instance Arbitrary TestableClique where
arbitrary = do
(mybounds, myedges) <- cliqueEdges
return . TestableClique $ Graph.buildG mybounds myedges
case_emptyVertColorMapNull :: Assertion
case_emptyVertColorMapNull = assertBool "" $ IntMap.null emptyVertColorMap
case_emptyVertColorMapEmpty :: Assertion
case_emptyVertColorMapEmpty =
assertEqual "" 0 $ IntMap.size emptyVertColorMap
anyTwo :: (a -> a -> Bool) -> [a] -> Bool
anyTwo _ [] = True
anyTwo _ [_] = True
anyTwo op (x:y:xs) = (x `op` y) && anyTwo op (y:xs)
prop_verticesByDegreeAscAsc :: TestableGraph -> Bool
prop_verticesByDegreeAscAsc (TestableGraph g) = anyTwo (<=) (degrees asc)
where degrees = map (length . neighbors g)
asc = verticesByDegreeAsc g
prop_verticesByDegreeDescDesc :: TestableGraph -> Bool
prop_verticesByDegreeDescDesc (TestableGraph g) = anyTwo (>=) (degrees desc)
where degrees = map (length . neighbors g)
desc = verticesByDegreeDesc g
prop_isColorableTestableGraph :: TestableGraph -> Bool
prop_isColorableTestableGraph (TestableGraph g) = isColorable g
prop_isColorableTestableClique :: TestableClique -> Bool
prop_isColorableTestableClique (TestableClique g) = isColorable g
prop_colorClique :: (Graph.Graph -> VertColorMap) -> TestableClique -> Property
prop_colorClique alg (TestableClique g) = numvertices ==? numcolors
where numcolors = (IntMap.size . colorVertMap) $ alg g
numvertices = length (Graph.vertices g)
prop_colorLFClique :: TestableClique -> Property
prop_colorLFClique = prop_colorClique colorLF
prop_colorDsaturClique :: TestableClique -> Property
prop_colorDsaturClique = prop_colorClique colorDsatur
prop_colorDcolorClique :: TestableClique -> Property
prop_colorDcolorClique = prop_colorClique colorDcolor
prop_colorAllNodes :: (Graph.Graph -> VertColorMap)
-> TestableGraph
-> Property
prop_colorAllNodes alg (TestableGraph g) = numvertices ==? numcolored
where numcolored = IntMap.fold ((+) . length) 0 vcMap
vcMap = colorVertMap $ alg g
numvertices = length (Graph.vertices g)
prop_colorLFAllNodes :: TestableGraph -> Property
prop_colorLFAllNodes = prop_colorAllNodes colorLF
prop_colorDsaturAllNodes :: TestableGraph -> Property
prop_colorDsaturAllNodes = prop_colorAllNodes colorDsatur
prop_colorDcolorAllNodes :: TestableGraph -> Property
prop_colorDcolorAllNodes = prop_colorAllNodes colorDcolor
prop_colorProper :: (Graph.Graph -> VertColorMap) -> TestableGraph -> Bool
prop_colorProper alg (TestableGraph g) = all isEdgeOk $ Graph.edges g
where isEdgeOk :: Graph.Edge -> Bool
isEdgeOk (v1, v2) = color v1 /= color v2
color v = cMap IntMap.! v
cMap = alg g
prop_colorLFProper :: TestableGraph -> Bool
prop_colorLFProper = prop_colorProper colorLF
prop_colorDsaturProper :: TestableGraph -> Bool
prop_colorDsaturProper = prop_colorProper colorDsatur
prop_colorDcolorProper :: TestableGraph -> Bool
prop_colorDcolorProper = prop_colorProper colorDcolor
testSuite "HTools/Graph"
[ 'case_emptyVertColorMapNull
, 'case_emptyVertColorMapEmpty
, 'prop_verticesByDegreeAscAsc
, 'prop_verticesByDegreeDescDesc
, 'prop_colorLFClique
, 'prop_colorDsaturClique
, 'prop_colorDcolorClique
, 'prop_colorLFAllNodes
, 'prop_colorDsaturAllNodes
, 'prop_colorDcolorAllNodes
, 'prop_colorLFProper
, 'prop_colorDsaturProper
, 'prop_colorDcolorProper
, 'prop_isColorableTestableGraph
, 'prop_isColorableTestableClique
]