DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (1aeaa33a64f9)

VCS Links

Line Code
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
# -*- coding: utf-8 -*-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

from __future__ import absolute_import, print_function, unicode_literals

import unittest

from taskgraph.graph import Graph
from mozunit import main


class TestGraph(unittest.TestCase):

    tree = Graph(set(['a', 'b', 'c', 'd', 'e', 'f', 'g']), {
        ('a', 'b', 'L'),
        ('a', 'c', 'L'),
        ('b', 'd', 'K'),
        ('b', 'e', 'K'),
        ('c', 'f', 'N'),
        ('c', 'g', 'N'),
    })

    linear = Graph(set(['1', '2', '3', '4']), {
        ('1', '2', 'L'),
        ('2', '3', 'L'),
        ('3', '4', 'L'),
    })

    diamonds = Graph(set(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']),
                     set(tuple(x) for x in
                         'AFL ADL BDL BEL CEL CHL DFL DGL EGL EHL FIL GIL GJL HJL'.split()
                         ))

    multi_edges = Graph(set(['1', '2', '3', '4']), {
        ('2', '1', 'red'),
        ('2', '1', 'blue'),
        ('3', '1', 'red'),
        ('3', '2', 'blue'),
        ('3', '2', 'green'),
        ('4', '3', 'green'),
    })

    disjoint = Graph(set(['1', '2', '3', '4', 'α', 'β', 'γ']), {
        ('2', '1', 'red'),
        ('3', '1', 'red'),
        ('3', '2', 'green'),
        ('4', '3', 'green'),
        ('α', 'β', 'πράσινο'),
        ('β', 'γ', 'κόκκινο'),
        ('α', 'γ', 'μπλε'),
    })

    def test_transitive_closure_empty(self):
        "transitive closure of an empty set is an empty graph"
        g = Graph(set(['a', 'b', 'c']), {('a', 'b', 'L'), ('a', 'c', 'L')})
        self.assertEqual(g.transitive_closure(set()),
                         Graph(set(), set()))

    def test_transitive_closure_disjoint(self):
        "transitive closure of a disjoint set is a subset"
        g = Graph(set(['a', 'b', 'c']), set())
        self.assertEqual(g.transitive_closure(set(['a', 'c'])),
                         Graph(set(['a', 'c']), set()))

    def test_transitive_closure_trees(self):
        "transitive closure of a tree, at two non-root nodes, is the two subtrees"
        self.assertEqual(self.tree.transitive_closure(set(['b', 'c'])),
                         Graph(set(['b', 'c', 'd', 'e', 'f', 'g']), {
                             ('b', 'd', 'K'),
                             ('b', 'e', 'K'),
                             ('c', 'f', 'N'),
                             ('c', 'g', 'N'),
                         }))

    def test_transitive_closure_multi_edges(self):
        "transitive closure of a tree with multiple edges between nodes keeps those edges"
        self.assertEqual(self.multi_edges.transitive_closure(set(['3'])),
                         Graph(set(['1', '2', '3']), {
                             ('2', '1', 'red'),
                             ('2', '1', 'blue'),
                             ('3', '1', 'red'),
                             ('3', '2', 'blue'),
                             ('3', '2', 'green'),
                         }))

    def test_transitive_closure_disjoint_edges(self):
        "transitive closure of a disjoint graph keeps those edges"
        self.assertEqual(self.disjoint.transitive_closure(set(['3', 'β'])),
                         Graph(set(['1', '2', '3', 'β', 'γ']), {
                             ('2', '1', 'red'),
                             ('3', '1', 'red'),
                             ('3', '2', 'green'),
                             ('β', 'γ', 'κόκκινο'),
                         }))

    def test_transitive_closure_linear(self):
        "transitive closure of a linear graph includes all nodes in the line"
        self.assertEqual(self.linear.transitive_closure(set(['1'])), self.linear)

    def test_visit_postorder_empty(self):
        "postorder visit of an empty graph is empty"
        self.assertEqual(list(Graph(set(), set()).visit_postorder()), [])

    def assert_postorder(self, seq, all_nodes):
        seen = set()
        for e in seq:
            for l, r, n in self.tree.edges:
                if l == e:
                    self.assertTrue(r in seen)
            seen.add(e)
        self.assertEqual(seen, all_nodes)

    def test_visit_postorder_tree(self):
        "postorder visit of a tree satisfies invariant"
        self.assert_postorder(self.tree.visit_postorder(), self.tree.nodes)

    def test_visit_postorder_diamonds(self):
        "postorder visit of a graph full of diamonds satisfies invariant"
        self.assert_postorder(self.diamonds.visit_postorder(), self.diamonds.nodes)

    def test_visit_postorder_multi_edges(self):
        "postorder visit of a graph with duplicate edges satisfies invariant"
        self.assert_postorder(self.multi_edges.visit_postorder(), self.multi_edges.nodes)

    def test_visit_postorder_disjoint(self):
        "postorder visit of a disjoint graph satisfies invariant"
        self.assert_postorder(self.disjoint.visit_postorder(), self.disjoint.nodes)

    def assert_preorder(self, seq, all_nodes):
        seen = set()
        for e in seq:
            for l, r, n in self.tree.edges:
                if r == e:
                    self.assertTrue(l in seen)
            seen.add(e)
        self.assertEqual(seen, all_nodes)

    def test_visit_preorder_tree(self):
        "preorder visit of a tree satisfies invariant"
        self.assert_preorder(self.tree.visit_preorder(), self.tree.nodes)

    def test_visit_preorder_diamonds(self):
        "preorder visit of a graph full of diamonds satisfies invariant"
        self.assert_preorder(self.diamonds.visit_preorder(), self.diamonds.nodes)

    def test_visit_preorder_multi_edges(self):
        "preorder visit of a graph with duplicate edges satisfies invariant"
        self.assert_preorder(self.multi_edges.visit_preorder(), self.multi_edges.nodes)

    def test_visit_preorder_disjoint(self):
        "preorder visit of a disjoint graph satisfies invariant"
        self.assert_preorder(self.disjoint.visit_preorder(), self.disjoint.nodes)

    def test_links_dict(self):
        "link dict for a graph with multiple edges is correct"
        self.assertEqual(self.multi_edges.links_dict(), {
            '2': set(['1']),
            '3': set(['1', '2']),
            '4': set(['3']),
        })

    def test_named_links_dict(self):
        "named link dict for a graph with multiple edges is correct"
        self.assertEqual(self.multi_edges.named_links_dict(), {
            '2': dict(red='1', blue='1'),
            '3': dict(red='1', blue='2', green='2'),
            '4': dict(green='3'),
        })

    def test_reverse_links_dict(self):
        "reverse link dict for a graph with multiple edges is correct"
        self.assertEqual(self.multi_edges.reverse_links_dict(), {
            '1': set(['2', '3']),
            '2': set(['3']),
            '3': set(['4']),
        })


if __name__ == '__main__':
    main()