Debian Codegraph @

rpython/tool/algo/test/test_color.py

raw symbols SCIP
1 from rpython.tool.algo.color import DependencyGraph
2
3
4 def graph1():
5 dg = DependencyGraph()
6 dg.add_node('a')
7 dg.add_node('b')
8 dg.add_node('c')
9 dg.add_node('d')
10 dg.add_node('e')
11 dg.add_edge('a', 'b') # d---e
12 dg.add_edge('a', 'd') # / \ / \
13 dg.add_edge('d', 'b') # a---b---c
14 dg.add_edge('d', 'e')
15 dg.add_edge('b', 'c')
16 dg.add_edge('b', 'e')
17 dg.add_edge('e', 'c')
18 return dg
19
20 def test_lexicographic_order():
21 dg = graph1()
22 order = list(dg.lexicographic_order())
23 assert len(order) == 5
24 # there are many orders that are correct answers, but check that we get
25 # the following one, which is somehow the 'first' answer in the order
26 # of insertion of nodes.
27 assert ''.join(order) == 'abdec'
28
29 def test_lexicographic_order_empty():
30 dg = DependencyGraph()
31 order = list(dg.lexicographic_order())
32 assert order == []
33
34 def test_size_of_largest_clique():
35 dg = graph1()
36 assert dg.size_of_largest_clique() == 3
37
38 def test_find_node_coloring():
39 dg = graph1()
40 coloring = dg.find_node_coloring()
41 assert len(coloring) == 5
42 assert sorted(coloring.keys()) == list('abcde')
43 assert set(coloring.values()) == set([0, 1, 2])
44 for v1, v2list in dg.neighbours.items():
45 for v2 in v2list:
46 assert coloring[v1] != coloring[v2]
47
48 def test_find_node_coloring_empty():
49 dg = DependencyGraph()
50 coloring = dg.find_node_coloring()
51 assert coloring == {}