File: test_color.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (51 lines) | stat: -rw-r--r-- 1,498 bytes parent folder | download | duplicates (9)
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
from rpython.tool.algo.color import DependencyGraph


def graph1():
    dg = DependencyGraph()
    dg.add_node('a')
    dg.add_node('b')
    dg.add_node('c')
    dg.add_node('d')
    dg.add_node('e')
    dg.add_edge('a', 'b')       #       d---e
    dg.add_edge('a', 'd')       #      / \ / \
    dg.add_edge('d', 'b')       #     a---b---c
    dg.add_edge('d', 'e')
    dg.add_edge('b', 'c')
    dg.add_edge('b', 'e')
    dg.add_edge('e', 'c')
    return dg

def test_lexicographic_order():
    dg = graph1()
    order = list(dg.lexicographic_order())
    assert len(order) == 5
    # there are many orders that are correct answers, but check that we get
    # the following one, which is somehow the 'first' answer in the order
    # of insertion of nodes.
    assert ''.join(order) == 'abdec'

def test_lexicographic_order_empty():
    dg = DependencyGraph()
    order = list(dg.lexicographic_order())
    assert order == []

def test_size_of_largest_clique():
    dg = graph1()
    assert dg.size_of_largest_clique() == 3

def test_find_node_coloring():
    dg = graph1()
    coloring = dg.find_node_coloring()
    assert len(coloring) == 5
    assert sorted(coloring.keys()) == list('abcde')
    assert set(coloring.values()) == set([0, 1, 2])
    for v1, v2list in dg.neighbours.items():
        for v2 in v2list:
            assert coloring[v1] != coloring[v2]

def test_find_node_coloring_empty():
    dg = DependencyGraph()
    coloring = dg.find_node_coloring()
    assert coloring == {}