summaryrefslogtreecommitdiff
path: root/lib/testresources/tests/test_resource_graph.py
blob: ec3930011d777eed1df5d265723e9f9aea499097 (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#
#  testresources: extensions to python unittest to allow declaritive use
#  of resources by test cases.
#
#  Copyright (c) 2005-2010 Testresources Contributors
#  
#  Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
#  license at the users choice. A copy of both licenses are available in the
#  project source as Apache-2.0 and BSD. You may not use this file except in
#  compliance with one of these two licences.
#  
#  Unless required by applicable law or agreed to in writing, software distributed
#  under these licenses is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
#  CONDITIONS OF ANY KIND, either express or implied.  See the license you chose
#  for the specific language governing permissions and limitations under that
#  license.
#

"""Test _resource_graph(resource_sets)."""

import testtools
import testresources
from testresources import split_by_resources, _resource_graph
from testresources.tests import ResultWithResourceExtensions
import unittest


def test_suite():
    from testresources.tests import TestUtil
    loader = TestUtil.TestLoader()
    result = loader.loadTestsFromName(__name__)
    return result


class TestResourceGraph(testtools.TestCase):

    def test_empty(self):
        no_resources = frozenset()
        resource_sets = [no_resources]
        self.assertEqual({no_resources:set([])}, _resource_graph(resource_sets))

    def test_discrete(self):
        resset1 = frozenset([testresources.TestResourceManager()])
        resset2 = frozenset([testresources.TestResourceManager()])
        resource_sets = [resset1, resset2]
        result = _resource_graph(resource_sets)
        self.assertEqual({resset1:set([]), resset2:set([])}, result)

    def test_overlapping(self):
        res1 = testresources.TestResourceManager()
        res2 = testresources.TestResourceManager()
        resset1 = frozenset([res1])
        resset2 = frozenset([res2])
        resset3 = frozenset([res1, res2])
        resource_sets = [resset1, resset2, resset3]
        result = _resource_graph(resource_sets)
        self.assertEqual(
            {resset1:set([resset3]),
             resset2:set([resset3]),
             resset3:set([resset1, resset2])},
            result)


class TestDigraphToGraph(testtools.TestCase):

    def test_wikipedia_example(self):
        """Converting a digraph mirrors it in the XZ axis (matrix view).

        See http://en.wikipedia.org/wiki/Travelling_salesman_problem \
        #Solving_by_conversion_to_Symmetric_TSP
        """
        #       A   B   C
        #   A       1   2
        #   B   6       3
        #   C   5   4   
        A = "A"
        Ap = "A'"
        B = "B"
        Bp = "B'"
        C = "C"
        Cp = "C'"
        digraph = {A:{     B:1, C:2},
                   B:{A:6,      C:3},
                   C:{A:5, B:4     }}
        # and the output
        #       A   B   C   A'  B'  C'
        #   A               0   6   5
        #   B               1   0   4
        #   C               2   3   0
        #   A'  0   1   2           
        #   B'  6   0   3           
        #   C'  5   4   0
        expected = {
            A :{              Ap:0, Bp:6, Cp:5},
            B :{              Ap:1, Bp:0, Cp:4},
            C :{              Ap:2, Bp:3, Cp:0},
            Ap:{A:0, B:1, C:2                 },
            Bp:{A:6, B:0, C:3                 },
            Cp:{A:5, B:4, C:0                 }}
        self.assertEqual(expected,
            testresources._digraph_to_graph(digraph, {A:Ap, B:Bp, C:Cp}))


class TestKruskalsMST(testtools.TestCase):

    def test_wikipedia_example(self):
        """Performing KruskalsMST on a graph returns a spanning tree.

        See http://en.wikipedia.org/wiki/Kruskal%27s_algorithm.
        """
        A = "A"
        B = "B"
        C = "C"
        D = "D"
        E = "E"
        F = "F"
        G = "G"
        graph = {
            A:{     B:7,      D:5},
            B:{A:7,      C:8, D:9,  E:7},
            C:{     B:8,            E:5},
            D:{A:5, B:9,            E:15, F:6},
            E:{     B:7, C:5, D:15,       F:8, G:9},
            F:{               D:6,  E:8,       G:11},
            G:{                     E:9, F:11}}
        expected = {
            A:{     B:7,      D:5},
            B:{A:7,                 E:7},
            C:{                     E:5},
            D:{A:5,                      F:6},
            E:{     B:7, C:5,                  G:9},
            F:{               D:6},
            G:{                     E:9}}
        result = testresources._kruskals_graph_MST(graph)
        e_weight = sum(sum(row.itervalues()) for row in expected.itervalues())
        r_weight = sum(sum(row.itervalues()) for row in result.itervalues())
        self.assertEqual(e_weight, r_weight)
        self.assertEqual(expected,
            testresources._kruskals_graph_MST(graph))