| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import pytest |
| 18 |
|
| 19 |
from pyocd.utility.graph import GraphNode |
| 20 |
|
| 21 |
class BaseNode(GraphNode): |
| 22 |
def __init__(self, value): |
| 23 |
super(BaseNode, self).__init__() |
| 24 |
self.value = value |
| 25 |
|
| 26 |
def __repr__(self): |
| 27 |
return "<{}@{:#010x} {}".format(self.__class__.__name__, id(self), self.value) |
| 28 |
|
| 29 |
class NodeA(BaseNode): |
| 30 |
pass |
| 31 |
|
| 32 |
class NodeB(BaseNode): |
| 33 |
pass |
| 34 |
|
| 35 |
@pytest.fixture(scope='function') |
| 36 |
def a(): |
| 37 |
return NodeA(23) |
| 38 |
|
| 39 |
@pytest.fixture(scope='function') |
| 40 |
def b(): |
| 41 |
return NodeB(1) |
| 42 |
|
| 43 |
@pytest.fixture(scope='function') |
| 44 |
def c(): |
| 45 |
return NodeB(2) |
| 46 |
|
| 47 |
@pytest.fixture(scope='function') |
| 48 |
def graph(a, b, c): |
| 49 |
p = GraphNode() |
| 50 |
p.add_child(a) |
| 51 |
a.add_child(b) |
| 52 |
p.add_child(c) |
| 53 |
return p |
| 54 |
|
| 55 |
class TestGraph: |
| 56 |
def test_new(self): |
| 57 |
n = GraphNode() |
| 58 |
assert len(n.children) == 0 |
| 59 |
assert n.parent is None |
| 60 |
|
| 61 |
def test_add_child(self): |
| 62 |
p = GraphNode() |
| 63 |
a = GraphNode() |
| 64 |
p.add_child(a) |
| 65 |
assert p.children == [a] |
| 66 |
assert p.parent is None |
| 67 |
assert a.parent is p |
| 68 |
assert a.children == [] |
| 69 |
|
| 70 |
def test_multiple_child(self): |
| 71 |
p = GraphNode() |
| 72 |
a = GraphNode() |
| 73 |
b = GraphNode() |
| 74 |
c = GraphNode() |
| 75 |
p.add_child(a) |
| 76 |
p.add_child(b) |
| 77 |
p.add_child(c) |
| 78 |
assert p.children == [a, b, c] |
| 79 |
assert p.parent is None |
| 80 |
assert a.parent is p |
| 81 |
assert b.parent is p |
| 82 |
assert c.parent is p |
| 83 |
assert a.children == [] |
| 84 |
assert b.children == [] |
| 85 |
assert c.children == [] |
| 86 |
|
| 87 |
def test_multilevel(self, graph, a, b, c): |
| 88 |
assert len(graph.children) == 2 |
| 89 |
assert graph.children == [a, c] |
| 90 |
assert len(a.children) == 1 |
| 91 |
assert a.children == [b] |
| 92 |
assert graph.parent is None |
| 93 |
assert a.parent is graph |
| 94 |
assert b.parent is a |
| 95 |
assert c.parent is graph |
| 96 |
assert b.children == [] |
| 97 |
assert c.children == [] |
| 98 |
|
| 99 |
def test_find_breadth(self, graph, a, b, c): |
| 100 |
assert graph.find_children(lambda n: n.value == 1) == [b] |
| 101 |
assert graph.find_children(lambda n: n.value == 1 or n.value == 2) == [c, b] |
| 102 |
|
| 103 |
def test_find_depth(self, graph, a, b, c): |
| 104 |
assert graph.find_children(lambda n: n.value == 1, breadth_first=False) == [b] |
| 105 |
assert graph.find_children(lambda n: n.value == 1 or n.value == 2, breadth_first=False) == [b, c] |
| 106 |
|
| 107 |
def test_first(self, graph, a, b, c): |
| 108 |
assert graph.get_first_child_of_type(NodeA) == a |
| 109 |
assert graph.get_first_child_of_type(NodeB) == c |
| 110 |
assert a.get_first_child_of_type(NodeB) == b |
| 111 |
|