forked from pmatiello/python-graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunittests-hypergraph.py
More file actions
339 lines (280 loc) · 8.92 KB
/
unittests-hypergraph.py
File metadata and controls
339 lines (280 loc) · 8.92 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright (c) Pedro Matiello <[email protected]>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
"""
Unittests for graph.classes.hypergraph
"""
import unittest
import pygraph
from pygraph.algorithms.generators import generate
from pygraph.classes.exceptions import AdditionError
from pygraph.classes.hypergraph import hypergraph
from . import testlib
from copy import copy, deepcopy
class test_hypergraph(unittest.TestCase):
# Add/Remove nodes and edges
def test_raise_exception_on_duplicate_node_addition(self):
gr = hypergraph()
gr.add_node('a_node')
try:
gr.add_node('a_node')
except AdditionError:
pass
else:
fail()
def test_raise_exception_on_duplicate_edge_link(self):
gr = hypergraph()
gr.add_node('a node')
gr.add_hyperedge('an edge')
gr.link('a node', 'an edge')
try:
gr.link('a node', 'an edge')
except AdditionError:
pass
else:
fail()
def test_raise_exception_on_non_existing_link_removal(self):
gr = hypergraph()
gr.add_node(0)
gr.add_hyperedge(1)
try:
gr.unlink(0, 1)
except ValueError:
pass
else:
fail()
def test_raise_exception_when_edge_added_from_non_existing_node(self):
gr = hypergraph()
gr.add_nodes([0,1])
try:
gr.link(3,0)
except KeyError:
pass
else:
fail()
assert gr.neighbors(0) == []
def test_raise_exception_when_edge_added_to_non_existing_node(self):
gr = hypergraph()
gr.add_nodes([0,1])
try:
gr.link(0,3)
except KeyError:
pass
else:
fail()
assert gr.neighbors(0) == []
def test_remove_node(self):
gr = testlib.new_hypergraph()
gr.del_node(0)
self.assertTrue(0 not in gr.nodes())
for e in gr.hyperedges():
for n in gr.links(e):
self.assertTrue(n in gr.nodes())
def test_remove_edge(self):
h = hypergraph()
h.add_nodes([1,2])
h.add_edges(['a', 'b'])
h.link(1,'a')
h.link(2,'a')
h.link(1,'b')
h.link(2,'b')
# Delete an edge
h.del_edge('a')
assert 1 == len(h.hyperedges())
gr = testlib.new_hypergraph()
edge_no = len(gr.nodes())+1
gr.del_hyperedge(edge_no)
self.assertTrue(edge_no not in gr.hyperedges())
def test_remove_link_from_node_to_same_node(self):
gr = hypergraph()
gr.add_node(0)
gr.add_hyperedge(0)
gr.link(0, 0)
gr.unlink(0, 0)
def test_remove_node_with_edge_to_itself(self):
gr = hypergraph()
gr.add_node(0)
gr.add_hyperedge(0)
gr.link(0, 0)
gr.del_node(0)
def test_check_add_node_s(self):
gr = hypergraph()
nodes = [1,2,3]
gr.add_nodes(nodes)
gr.add_node(0)
for n in [0] + nodes:
assert n in gr
assert gr.has_node(n)
def test_rank(self):
# Uniform case
gr = testlib.new_uniform_hypergraph(3)
assert 3 == gr.rank()
# Non-uniform case
gr = testlib.new_hypergraph()
num = max([len(gr.links(e)) for e in gr.hyperedges()])
assert num == gr.rank()
def test_repr(self):
"""
Validate the repr string
"""
gr = testlib.new_hypergraph()
gr_repr = repr(gr)
assert isinstance(gr_repr, str )
assert gr.__class__.__name__ in gr_repr
def test_order_len_equivlance(self):
"""
Verify the behavior of G.order()
"""
gr = testlib.new_hypergraph()
assert len(gr) == gr.order()
assert gr.order() == len( gr.node_links )
def test_hypergraph_equality_nodes(self):
"""
Hyperaph equality test. This one checks node equality.
"""
gr = hypergraph()
gr.add_nodes([0,1,2,3,4,5])
gr2 = deepcopy(gr)
gr3 = deepcopy(gr)
gr3.del_node(5)
gr4 = deepcopy(gr)
gr4.add_node(6)
gr4.del_node(0)
assert gr == gr2
assert gr2 == gr
assert gr != gr3
assert gr3 != gr
assert gr != gr4
assert gr4 != gr
def test_hypergraph_equality_edges(self):
"""
Hyperaph equality test. This one checks edge equality.
"""
gr = hypergraph()
gr.add_nodes([0,1,2,3])
gr.add_edge('e1')
gr.add_edge('e2')
gr.link(0, 'e1')
gr.link(1, 'e1')
gr.link(1, 'e2')
gr.link(2, 'e2')
gr2 = deepcopy(gr)
gr3 = deepcopy(gr)
gr3.del_edge('e2')
gr4 = deepcopy(gr)
gr4.unlink(1, 'e2')
assert gr == gr2
assert gr2 == gr
assert gr != gr3
assert gr3 != gr
assert gr != gr4
assert gr4 != gr
def test_hypergraph_equality_labels(self):
"""
Hyperaph equality test. This one checks edge equality.
"""
gr = hypergraph()
gr.add_nodes([0,1,2,3])
gr.add_edge('e1')
gr.add_edge('e2')
gr.add_edge('e3')
gr.set_edge_label('e1', 'l1')
gr.set_edge_label('e2', 'l2')
gr2 = deepcopy(gr)
gr3 = deepcopy(gr)
gr3.set_edge_label('e3', 'l3')
gr4 = deepcopy(gr)
gr4.set_edge_label('e1', 'lx')
gr5 = deepcopy(gr)
gr5.del_edge('e1')
gr5.add_edge('e1')
assert gr == gr2
assert gr2 == gr
assert gr != gr3
assert gr3 != gr
assert gr != gr4
assert gr4 != gr
assert gr != gr5
assert gr5 != gr
def test_hypergraph_equality_attributes(self):
"""
Hyperaph equality test. This one checks edge equality.
"""
gr = hypergraph()
gr.add_nodes([0,1])
gr.add_edge('e1')
gr.add_edge('e2')
gr.add_node_attribute(0, ('a',0))
gr.add_edge_attribute('e1', ('b',1))
gr2 = deepcopy(gr)
gr3 = deepcopy(gr)
gr3.add_node_attribute(0, ('x','y'))
gr4 = deepcopy(gr)
gr4.add_edge_attribute('e1', ('u','v'))
gr5 = deepcopy(gr)
gr5.del_edge('e1')
gr5.add_edge('e1')
gr6 = deepcopy(gr)
gr6.del_node(0)
gr6.add_node(0)
assert gr == gr2
assert gr2 == gr
assert gr != gr3
assert gr3 != gr
assert gr != gr4
assert gr4 != gr
assert gr != gr5
assert gr5 != gr
assert gr != gr6
assert gr6 != gr
def test_hypergraph_equality_weight(self):
"""
Hyperaph equality test. This one checks edge equality.
"""
gr = hypergraph()
gr.add_nodes([0,1,2,3])
gr.add_edge('e1')
gr.add_edge('e2')
gr.add_edge('e3')
gr.set_edge_weight('e1', 2)
gr2 = deepcopy(gr)
gr3 = deepcopy(gr)
gr3.set_edge_weight('e3', 2)
gr4 = deepcopy(gr)
gr4.set_edge_weight('e1', 1)
assert gr == gr2
assert gr2 == gr
assert gr != gr3
assert gr3 != gr
assert gr != gr4
assert gr4 != gr
def test_hypergraph_link_unlink_link(self):
"""
Hypergraph link-unlink-link test. It makes sure that unlink cleans
everything properly. No AdditionError should occur.
"""
h = hypergraph()
h.add_nodes([1,2])
h.add_edges(['e1'])
h.link(1, 'e1')
h.unlink(1, 'e1')
h.link(1,'e1')
if __name__ == "__main__":
unittest.main()