1# unit tests for the cache module
2# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
3# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
4#
5# This file is part of logilab-common.
6#
7# logilab-common is free software: you can redistribute it and/or modify it under
8# the terms of the GNU Lesser General Public License as published by the Free
9# Software Foundation, either version 2.1 of the License, or (at your option) any
10# later version.
11#
12# logilab-common is distributed in the hope that it will be useful, but WITHOUT
13# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
15# details.
16#
17# You should have received a copy of the GNU Lesser General Public License along
18# with logilab-common.  If not, see <http://www.gnu.org/licenses/>.
19
20from logilab.common.testlib import TestCase, unittest_main
21from logilab.common.graph import get_cycles, has_path, ordered_nodes, UnorderableGraph
22
23
24class getCyclesTC(TestCase):
25    def test_known0(self):
26        self.assertEqual(get_cycles({1: [2], 2: [3], 3: [1]}), [[1, 2, 3]])
27
28    def test_known1(self):
29        self.assertEqual(get_cycles({1: [2], 2: [3], 3: [1, 4], 4: [3]}), [[1, 2, 3], [3, 4]])
30
31    def test_known2(self):
32        self.assertEqual(get_cycles({1: [2], 2: [3], 3: [0], 0: []}), [])
33
34
35class hasPathTC(TestCase):
36    def test_direct_connection(self):
37        self.assertEqual(has_path({"A": ["B"], "B": ["A"]}, "A", "B"), ["B"])
38
39    def test_indirect_connection(self):
40        self.assertEqual(has_path({"A": ["B"], "B": ["A", "C"], "C": ["B"]}, "A", "C"), ["B", "C"])
41
42    def test_no_connection(self):
43        self.assertEqual(has_path({"A": ["B"], "B": ["A"]}, "A", "C"), None)
44
45    def test_cycle(self):
46        self.assertEqual(has_path({"A": ["A"]}, "A", "B"), None)
47
48
49class ordered_nodesTC(TestCase):
50    def test_one_item(self):
51        graph = {"a": []}
52        ordered = ordered_nodes(graph)
53        self.assertEqual(ordered, ("a",))
54
55    def test_single_dependency(self):
56        graph = {"a": ["b"], "b": []}
57        ordered = ordered_nodes(graph)
58        self.assertEqual(ordered, ("a", "b"))
59        graph = {"a": [], "b": ["a"]}
60        ordered = ordered_nodes(graph)
61        self.assertEqual(ordered, ("b", "a"))
62
63    def test_two_items_no_dependency(self):
64        graph = {"a": [], "b": []}
65        ordered = ordered_nodes(graph)
66        self.assertEqual(ordered, ("a", "b"))
67
68    def test_three_items_no_dependency(self):
69        graph = {"a": [], "b": [], "c": []}
70        ordered = ordered_nodes(graph)
71        self.assertEqual(ordered, ("a", "b", "c"))
72
73    def test_three_items_one_dependency(self):
74        graph = {"a": ["c"], "b": [], "c": []}
75        ordered = ordered_nodes(graph)
76        self.assertEqual(ordered, ("a", "b", "c"))
77
78    def test_three_items_two_dependencies(self):
79        graph = {"a": ["b"], "b": ["c"], "c": []}
80        ordered = ordered_nodes(graph)
81        self.assertEqual(ordered, ("a", "b", "c"))
82
83    def test_bad_graph(self):
84        graph = {"a": ["b"]}
85        self.assertRaises(UnorderableGraph, ordered_nodes, graph)
86
87
88if __name__ == "__main__":
89    unittest_main()
90