summaryrefslogtreecommitdiffstats
path: root/python/samba/tests/kcc/graph_utils.py
blob: 3eaa1c706dac22619ff44375bfae6a833f20de48 (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Unix SMB/CIFS implementation. Tests for kcc.graph_utils routines
# Copyright (C) Andrew Bartlett 2015
#
# Written by Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Tests for samba.kcc.graph_utils"""

import samba
import samba.tests
from samba.kcc.graph_utils import GraphError
from samba.kcc.graph_utils import (verify_graph_complete,
                                   verify_graph_connected,
                                   verify_graph_connected_under_edge_failures,
                                   verify_graph_forest,
                                   verify_graph_connected_under_vertex_failures,
                                   verify_graph_no_lonely_vertices)

import itertools


def make_tree(vertices):
    if len(vertices) < 2:
        return ()
    remaining = set(vertices)
    used = set()
    edges = set()
    used.add(remaining.pop())
    used.add(remaining.pop())
    edges.add(tuple(used))
    while remaining:
        v = remaining.pop()
        w = used.pop()
        e = (w, v)
        edges.add(e)
        used.update(e)
    return tuple(edges)

# TODO: test directed graphs


class UndirectedGraphTests(samba.tests.TestCase):

    def setUp(self):
        super().setUp()
        vertices = tuple('abcdefgh')
        vertices2 = tuple('ijk')
        edges = tuple(itertools.combinations(vertices, 2))
        edges2 = tuple(itertools.combinations(vertices2, 2))
        line_edges = list(zip(vertices[1:], vertices[:-1]))
        ring_edges = line_edges + [(vertices[0], vertices[-1])]

        tree = make_tree(vertices)
        tree2 = make_tree(vertices2)

        self.complete_graph = [edges, vertices, vertices]

        self.disconnected_clusters = [edges + edges2,
                                      vertices + vertices2,
                                      vertices + vertices2]

        self.graph_with_unreachables = [edges,
                                        vertices + vertices2,
                                        vertices]

        self.ring = [ring_edges, vertices, vertices]
        self.line = [line_edges, vertices, vertices]

        self.tree = [tree, vertices, vertices]
        self.forest = [tree + tree2,
                       vertices + vertices2,
                       vertices + vertices2]

        self.unconnected_graph = ((), vertices, ())

    def assertGraphError(self, fn, *args):
        return self.assertRaises(GraphError, fn, *args)

    def test_graph_complete(self):
        fn = verify_graph_complete

        self.assertGraphError(fn, *self.disconnected_clusters)
        self.assertGraphError(fn, *self.graph_with_unreachables)
        self.assertGraphError(fn, *self.ring)
        self.assertGraphError(fn, *self.tree)

        self.assertIsNone(fn(*self.complete_graph))

    def test_graph_connected(self):
        fn = verify_graph_connected

        self.assertGraphError(fn, *self.disconnected_clusters)
        self.assertGraphError(fn, *self.graph_with_unreachables)
        self.assertGraphError(fn, *self.forest)
        self.assertGraphError(fn, *self.unconnected_graph)

        self.assertIsNone(fn(*self.line))
        self.assertIsNone(fn(*self.ring))
        self.assertIsNone(fn(*self.complete_graph))
        self.assertIsNone(fn(*self.tree))

    def test_graph_forest(self):
        fn = verify_graph_forest

        self.assertGraphError(fn, *self.disconnected_clusters)
        self.assertGraphError(fn, *self.graph_with_unreachables)
        self.assertGraphError(fn, *self.ring)

        self.assertIsNone(fn(*self.line))
        self.assertIsNone(fn(*self.tree))
        self.assertIsNone(fn(*self.forest))
        self.assertIsNone(fn(*self.unconnected_graph))

    def test_graph_connected_under_edge_failures(self):
        fn = verify_graph_connected_under_edge_failures

        self.assertGraphError(fn, *self.line)
        self.assertGraphError(fn, *self.tree)
        self.assertGraphError(fn, *self.forest)
        self.assertGraphError(fn, *self.disconnected_clusters)

        self.assertIsNone(fn(*self.ring))
        self.assertIsNone(fn(*self.complete_graph))

    def test_graph_connected_under_vertex_failures(self):
        # XXX no tests to distinguish this from the edge_failures case
        fn = verify_graph_connected_under_vertex_failures

        self.assertGraphError(fn, *self.line)
        self.assertGraphError(fn, *self.tree)
        self.assertGraphError(fn, *self.forest)
        self.assertGraphError(fn, *self.disconnected_clusters)

        self.assertIsNone(fn(*self.ring))
        self.assertIsNone(fn(*self.complete_graph))

    def test_graph_multi_edge_forest(self):
        pass

    def test_graph_no_lonely_vertices(self):
        fn = verify_graph_no_lonely_vertices
        self.assertGraphError(fn, *self.unconnected_graph)
        self.assertGraphError(fn, *self.graph_with_unreachables)

        self.assertIsNone(fn(*self.ring))
        self.assertIsNone(fn(*self.complete_graph))
        self.assertIsNone(fn(*self.line))
        self.assertIsNone(fn(*self.tree))
        self.assertIsNone(fn(*self.forest))

    def test_graph_no_unknown_vertices(self):
        pass