-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_graph.py
223 lines (176 loc) · 4.96 KB
/
test_graph.py
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# tests for a simple graph
from graph import SimpleGraph
import pytest
def test_add_nodes():
"""nodes are added"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
assert 'a' in g.nodes()
assert 'b' in g.nodes()
assert 'c' in g.nodes()
def test_del_nodes():
"""nodes are deleted"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
g.del_node('a')
assert 'a' not in g.nodes()
assert g.has_node('a') is False
def test_del_error():
"""error raised when non-existant value deleted"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
with pytest.raises(KeyError):
g.del_node('p')
def test_del_edge():
"""other edges preserved when one deleted"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
g.del_edge('a', 'c')
assert g.neighbors('a') == {'b': 0}
def test_add_edge():
"""edge added, new node added"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
g.add_edge('q', 'a')
assert g.has_node('q')
assert g.neighbors('q') == {'a': 0}
def test_edge_error():
"""delete non-existant edge"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
with pytest.raises(KeyError):
g.del_edge('a', 'z')
def test_has_node():
"""edge added, new node added"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
assert g.has_node('b')
def test_adjacent():
"""adjacent correctly identify edges, new edge add node"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'd')
g.add_edge('a', 'f')
assert 'f' in g.nodes()
assert g.adjacent('a', 'd')
assert not g.adjacent('a', 'b')
def test_adjacent_error():
"""error raised when nodes non-existant"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
with pytest.raises(KeyError):
g.adjacent('l', 'm')
def test_edges():
"""edges prints correct edges as tuples"""
g = SimpleGraph()
g.add_node('a')
g.add_node('b')
g.add_node('c')
g.add_edge('a', 'c')
g.add_edge('a', 'b')
assert ('a', 'c') in g.edges()
assert ('a', 'b') in g.edges()
def test_breadth():
g = SimpleGraph()
g.add_edge('a', 'b')
g.add_edge('a', 'c')
g.add_edge('b', 'd')
g.add_edge('b', 'e')
g.add_edge('e', 'f')
g.add_edge('f', 'g')
assert g.breadth_first_traversal('a') == ['a', 'c', 'b', 'e', 'd', 'f', 'g']
def test_depth():
g = SimpleGraph()
g.add_edge('a', 'b')
g.add_edge('a', 'c')
g.add_edge('b', 'd')
g.add_edge('b', 'e')
g.add_edge('e', 'f')
g.add_edge('f', 'g')
assert g.depth_first_traversal('a') == ['a', 'c', 'b', 'e', 'f', 'g', 'd']
def test_one_breadth():
g = SimpleGraph()
g.add_node('a')
assert g.breadth_first_traversal('a') == ['a']
def test_one_depth():
g = SimpleGraph()
g.add_node('a')
assert g.depth_first_traversal('a') == ['a']
def test_one_loop_breadth():
g = SimpleGraph()
g.add_edge('a', 'a')
assert g.breadth_first_traversal('a') == ['a']
def test_one_loop_depth():
g = SimpleGraph()
g.add_edge('a', 'a')
assert g.depth_first_traversal('a') == ['a']
def fully_connected_graph(nodes):
g = SimpleGraph()
for node in nodes:
g.add_node(node)
for other_node in nodes:
if other_node is not node:
g.add_edge(node, other_node)
return g
def test_fully_connected():
g = fully_connected_graph(['a', 'b', 'c', 'd'])
assert g.breadth_first_traversal('a') == ['a', 'c', 'b', 'd']
def test_fully_connected_again():
g = fully_connected_graph(['a', 'b', 'c', 'd'])
assert g.depth_first_traversal('a') == ['a', 'c', 'b', 'd']
def test_weighted_edges():
g = SimpleGraph()
g.add_edge('a', 'b', 5)
g.add_edge('a', 'c', 2)
g.add_edge('b', 'c', 1)
assert g.dict_graph['a'] == {'b': 5, 'c': 2}
def test_weighted_edges_with_edge_delete():
g = SimpleGraph()
g.add_edge('a', 'b', 5)
g.add_edge('a', 'c', 2)
g.del_edge('a', 'c')
assert g.dict_graph['a'] == {'b': 5}
def test_weighted_edges_with_node_delete():
g = SimpleGraph()
g.add_edge('a', 'b', 5)
g.add_edge('a', 'c', 2)
g.del_node('c')
assert g.dict_graph['a'] == {'b': 5}
def test_dijkstra():
graph = SimpleGraph([('a', 'b', 6), ('b', 'a', 5),
('a', 'g', 1), ('g', 'b', 1)])
assert graph.dijkstra('a') == {'a': 0, 'b': 2, 'g': 1}