Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / classes / tests / historical_tests.py @ 5cef0f13

History | View | Annotate | Download (16.2 KB)

1
#!/usr/bin/env python
2
"""Original NetworkX graph tests"""
3
from nose.tools import *
4
import networkx as nx
5
from networkx import convert_node_labels_to_integers as cnlti
6
from networkx.testing import *
7

    
8

    
9
class HistoricalTests(object):
10

    
11
    def setUp(self):
12
        self.null = nx.null_graph()
13
        self.P1 = cnlti(nx.path_graph(1), first_label=1)
14
        self.P3 = cnlti(nx.path_graph(3), first_label=1)
15
        self.P10 = cnlti(nx.path_graph(10), first_label=1)
16
        self.K1 = cnlti(nx.complete_graph(1), first_label=1)
17
        self.K3 = cnlti(nx.complete_graph(3), first_label=1)
18
        self.K4 = cnlti(nx.complete_graph(4), first_label=1)
19
        self.K5 = cnlti(nx.complete_graph(5), first_label=1)
20
        self.K10 = cnlti(nx.complete_graph(10), first_label=1)
21
        self.G = nx.Graph
22

    
23
    def test_name(self):
24
        G = self.G(name="test")
25
        assert_equal(str(G), 'test')
26
        assert_equal(G.name, 'test')
27
        H = self.G()
28
        assert_equal(H.name, '')
29

    
30
    # Nodes
31

    
32
    def test_add_remove_node(self):
33
        G = self.G()
34
        G.add_node('A')
35
        assert_true(G.has_node('A'))
36
        G.remove_node('A')
37
        assert_false(G.has_node('A'))
38

    
39
    def test_nonhashable_node(self):
40
        # Test if a non-hashable object is in the Graph.  A python dict will
41
        # raise a TypeError, but for a Graph class a simple  False should be
42
        # returned (see Graph __contains__). If it cannot be a node then it is
43
        # not a node.
44
        G = self.G()
45
        assert_false(G.has_node(['A']))
46
        assert_false(G.has_node({'A': 1}))
47

    
48
    def test_add_nodes_from(self):
49
        G = self.G()
50
        G.add_nodes_from(list("ABCDEFGHIJKL"))
51
        assert_true(G.has_node("L"))
52
        G.remove_nodes_from(['H', 'I', 'J', 'K', 'L'])
53
        G.add_nodes_from([1, 2, 3, 4])
54
        assert_equal(sorted(G.nodes(), key=str),
55
                     [1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G'])
56
        # test __iter__
57
        assert_equal(sorted(G, key=str),
58
                     [1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G'])
59

    
60
    def test_contains(self):
61
        G = self.G()
62
        G.add_node('A')
63
        assert_true('A' in G)
64
        assert_false([] in G)  # never raise a Key or TypeError in this test
65
        assert_false({1: 1} in G)
66

    
67
    def test_add_remove(self):
68
        # Test add_node and remove_node acting for various nbunch
69
        G = self.G()
70
        G.add_node('m')
71
        assert_true(G.has_node('m'))
72
        G.add_node('m')   # no complaints
73
        assert_raises(nx.NetworkXError, G.remove_node, 'j')
74
        G.remove_node('m')
75
        assert_equal(list(G), [])
76

    
77
    def test_nbunch_is_list(self):
78
        G = self.G()
79
        G.add_nodes_from(list("ABCD"))
80
        G.add_nodes_from(self.P3)  # add nbunch of nodes (nbunch=Graph)
81
        assert_equal(sorted(G.nodes(), key=str),
82
                     [1, 2, 3, 'A', 'B', 'C', 'D'])
83
        G.remove_nodes_from(self.P3)  # remove nbunch of nodes (nbunch=Graph)
84
        assert_equal(sorted(G.nodes(), key=str),
85
                     ['A', 'B', 'C', 'D'])
86

    
87
    def test_nbunch_is_set(self):
88
        G = self.G()
89
        nbunch = set("ABCDEFGHIJKL")
90
        G.add_nodes_from(nbunch)
91
        assert_true(G.has_node("L"))
92

    
93
    def test_nbunch_dict(self):
94
        # nbunch is a dict with nodes as keys
95
        G = self.G()
96
        nbunch = set("ABCDEFGHIJKL")
97
        G.add_nodes_from(nbunch)
98
        nbunch = {'I': "foo", 'J': 2, 'K': True, 'L': "spam"}
99
        G.remove_nodes_from(nbunch)
100
        assert_true(sorted(G.nodes(), key=str),
101
                    ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
102

    
103
    def test_nbunch_iterator(self):
104
        G = self.G()
105
        G.add_nodes_from(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
106
        n_iter = self.P3.nodes()
107
        G.add_nodes_from(n_iter)
108
        assert_equal(sorted(G.nodes(), key=str),
109
                     [1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
110
        n_iter = self.P3.nodes()  # rebuild same iterator
111
        G.remove_nodes_from(n_iter)  # remove nbunch of nodes (nbunch=iterator)
112
        assert_equal(sorted(G.nodes(), key=str),
113
                     ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
114

    
115
    def test_nbunch_graph(self):
116
        G = self.G()
117
        G.add_nodes_from(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
118
        nbunch = self.K3
119
        G.add_nodes_from(nbunch)
120
        assert_true(sorted(G.nodes(), key=str),
121
                    [1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
122

    
123
    # Edges
124

    
125
    def test_add_edge(self):
126
        G = self.G()
127
        assert_raises(TypeError, G.add_edge, 'A')
128

    
129
        G.add_edge('A', 'B')     # testing add_edge()
130
        G.add_edge('A', 'B')  # should fail silently
131
        assert_true(G.has_edge('A', 'B'))
132
        assert_false(G.has_edge('A', 'C'))
133
        assert_true(G.has_edge(*('A', 'B')))
134
        if G.is_directed():
135
            assert_false(G.has_edge('B', 'A'))
136
        else:
137
            # G is undirected, so B->A is an edge
138
            assert_true(G.has_edge('B', 'A'))
139

    
140
        G.add_edge('A', 'C')  # test directedness
141
        G.add_edge('C', 'A')
142
        G.remove_edge('C', 'A')
143
        if G.is_directed():
144
            assert_true(G.has_edge('A', 'C'))
145
        else:
146
            assert_false(G.has_edge('A', 'C'))
147
        assert_false(G.has_edge('C', 'A'))
148

    
149
    def test_self_loop(self):
150
        G = self.G()
151
        G.add_edge('A', 'A')  # test self loops
152
        assert_true(G.has_edge('A', 'A'))
153
        G.remove_edge('A', 'A')
154
        G.add_edge('X', 'X')
155
        assert_true(G.has_node('X'))
156
        G.remove_node('X')
157
        G.add_edge('A', 'Z')  # should add the node silently
158
        assert_true(G.has_node('Z'))
159

    
160
    def test_add_edges_from(self):
161
        G = self.G()
162
        G.add_edges_from([('B', 'C')])   # test add_edges_from()
163
        assert_true(G.has_edge('B', 'C'))
164
        if G.is_directed():
165
            assert_false(G.has_edge('C', 'B'))
166
        else:
167
            assert_true(G.has_edge('C', 'B'))  # undirected
168

    
169
        G.add_edges_from([('D', 'F'), ('B', 'D')])
170
        assert_true(G.has_edge('D', 'F'))
171
        assert_true(G.has_edge('B', 'D'))
172

    
173
        if G.is_directed():
174
            assert_false(G.has_edge('D', 'B'))
175
        else:
176
            assert_true(G.has_edge('D', 'B'))  # undirected
177

    
178
    def test_add_edges_from2(self):
179
        G = self.G()
180
        # after failing silently, should add 2nd edge
181
        G.add_edges_from([tuple('IJ'), list('KK'), tuple('JK')])
182
        assert_true(G.has_edge(*('I', 'J')))
183
        assert_true(G.has_edge(*('K', 'K')))
184
        assert_true(G.has_edge(*('J', 'K')))
185
        if G.is_directed():
186
            assert_false(G.has_edge(*('K', 'J')))
187
        else:
188
            assert_true(G.has_edge(*('K', 'J')))
189

    
190
    def test_add_edges_from3(self):
191
        G = self.G()
192
        G.add_edges_from(zip(list('ACD'), list('CDE')))
193
        assert_true(G.has_edge('D', 'E'))
194
        assert_false(G.has_edge('E', 'C'))
195

    
196
    def test_remove_edge(self):
197
        G = self.G()
198
        G.add_nodes_from([1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'])
199

    
200
        G.add_edges_from(zip(list('MNOP'), list('NOPM')))
201
        assert_true(G.has_edge('O', 'P'))
202
        assert_true(G.has_edge('P', 'M'))
203
        G.remove_node('P')    # tests remove_node()'s handling of edges.
204
        assert_false(G.has_edge('P', 'M'))
205
        assert_raises(TypeError, G.remove_edge, 'M')
206

    
207
        G.add_edge('N', 'M')
208
        assert_true(G.has_edge('M', 'N'))
209
        G.remove_edge('M', 'N')
210
        assert_false(G.has_edge('M', 'N'))
211

    
212
        # self loop fails silently
213
        G.remove_edges_from([list('HI'), list('DF'),
214
                             tuple('KK'), tuple('JK')])
215
        assert_false(G.has_edge('H', 'I'))
216
        assert_false(G.has_edge('J', 'K'))
217
        G.remove_edges_from([list('IJ'), list('KK'), list('JK')])
218
        assert_false(G.has_edge('I', 'J'))
219
        G.remove_nodes_from(set('ZEFHIMNO'))
220
        G.add_edge('J', 'K')
221

    
222
    def test_edges_nbunch(self):
223
        # Test G.edges(nbunch) with various forms of nbunch
224
        G = self.G()
225
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
226
                          ('C', 'B'), ('C', 'D')])
227
        # node not in nbunch should be quietly ignored
228
        assert_raises(nx.NetworkXError, G.edges, 6)
229
        assert_equals(list(G.edges('Z')), [])  # iterable non-node
230
        # nbunch can be an empty list
231
        assert_equals(list(G.edges([])), [])
232
        if G.is_directed():
233
            elist = [('A', 'B'), ('A', 'C'), ('B', 'D')]
234
        else:
235
            elist = [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')]
236
        # nbunch can be a list
237
        assert_edges_equal(list(G.edges(['A', 'B'])), elist)
238
        # nbunch can be a set
239
        assert_edges_equal(G.edges(set(['A', 'B'])), elist)
240
        # nbunch can be a graph
241
        G1 = self.G()
242
        G1.add_nodes_from('AB')
243
        assert_edges_equal(G.edges(G1), elist)
244
        # nbunch can be a dict with nodes as keys
245
        ndict = {'A': "thing1", 'B': "thing2"}
246
        assert_edges_equal(G.edges(ndict), elist)
247
        # nbunch can be a single node
248
        assert_edges_equal(list(G.edges('A')), [('A', 'B'), ('A', 'C')])
249
        assert_nodes_equal(sorted(G), ['A', 'B', 'C', 'D'])
250

    
251
        # nbunch can be nothing (whole graph)
252
        assert_edges_equal(
253
            list(G.edges()),
254
            [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'B'), ('C', 'D')]
255
        )
256

    
257
    def test_degree(self):
258
        G = self.G()
259
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
260
                          ('C', 'B'), ('C', 'D')])
261
        assert_equal(G.degree('A'), 2)
262

    
263
        # degree of single node in iterable container must return dict
264
        assert_equal(list(G.degree(['A'])), [('A', 2)])
265
        assert_equal(sorted(d for n, d in G.degree(['A', 'B'])), [2, 3])
266
        assert_equal(sorted(d for n, d in G.degree()), [2, 2, 3, 3])
267

    
268
    def test_degree2(self):
269
        H = self.G()
270
        H.add_edges_from([(1, 24), (1, 2)])
271
        assert_equal(sorted(d for n, d in H.degree([1, 24])), [1, 2])
272

    
273
    def test_degree_graph(self):
274
        P3 = nx.path_graph(3)
275
        P5 = nx.path_graph(5)
276
        # silently ignore nodes not in P3
277
        assert_equal(dict(d for n, d in P3.degree(['A', 'B'])), {})
278
        # nbunch can be a graph
279
        assert_equal(sorted(d for n, d in P5.degree(P3)), [1, 2, 2])
280
        # nbunch can be a graph that's way too big
281
        assert_equal(sorted(d for n, d in P3.degree(P5)), [1, 1, 2])
282
        assert_equal(list(P5.degree([])), [])
283
        assert_equal(dict(P5.degree([])), {})
284

    
285
    def test_null(self):
286
        null = nx.null_graph()
287
        assert_equal(list(null.degree()), [])
288
        assert_equal(dict(null.degree()), {})
289

    
290
    def test_order_size(self):
291
        G = self.G()
292
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
293
                          ('C', 'B'), ('C', 'D')])
294
        assert_equal(G.order(), 4)
295
        assert_equal(G.size(), 5)
296
        assert_equal(G.number_of_edges(), 5)
297
        assert_equal(G.number_of_edges('A', 'B'), 1)
298
        assert_equal(G.number_of_edges('A', 'D'), 0)
299

    
300
    def test_copy(self):
301
        G = self.G()
302
        H = G.copy()      # copy
303
        assert_equal(H.adj, G.adj)
304
        assert_equal(H.name, G.name)
305
        assert_not_equal(H, G)
306

    
307
    def test_subgraph(self):
308
        G = self.G()
309
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
310
                          ('C', 'B'), ('C', 'D')])
311
        SG = G.subgraph(['A', 'B', 'D'])
312
        assert_nodes_equal(list(SG), ['A', 'B', 'D'])
313
        assert_edges_equal(list(SG.edges()), [('A', 'B'), ('B', 'D')])
314

    
315
    def test_to_directed(self):
316
        G = self.G()
317
        if not G.is_directed():
318
            G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
319
                              ('C', 'B'), ('C', 'D')])
320

    
321
            DG = G.to_directed()
322
            assert_not_equal(DG, G)  # directed copy or copy
323

    
324
            assert_true(DG.is_directed())
325
            assert_equal(DG.name, G.name)
326
            assert_equal(DG.adj, G.adj)
327
            assert_equal(sorted(DG.out_edges(list('AB'))),
328
                         [('A', 'B'), ('A', 'C'), ('B', 'A'),
329
                          ('B', 'C'), ('B', 'D')])
330
            DG.remove_edge('A', 'B')
331
            assert_true(DG.has_edge('B', 'A'))  # this removes B-A but not  A-B
332
            assert_false(DG.has_edge('A', 'B'))
333

    
334
    def test_to_undirected(self):
335
        G = self.G()
336
        if G.is_directed():
337
            G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
338
                              ('C', 'B'), ('C', 'D')])
339
            UG = G.to_undirected()       # to_undirected
340
            assert_not_equal(UG, G)
341
            assert_false(UG.is_directed())
342
            assert_true(G.is_directed())
343
            assert_equal(UG.name, G.name)
344
            assert_not_equal(UG.adj, G.adj)
345
            assert_equal(sorted(UG.edges(list('AB'))),
346
                         [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')])
347
            assert_equal(sorted(UG.edges(['A', 'B'])),
348
                         [('A', 'B'), ('A', 'C'), ('B', 'C'), ('B', 'D')])
349
            UG.remove_edge('A', 'B')
350
            assert_false(UG.has_edge('B', 'A'))
351
            assert_false(UG.has_edge('A', 'B'))
352

    
353
    def test_neighbors(self):
354
        G = self.G()
355
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
356
                          ('C', 'B'), ('C', 'D')])
357
        G.add_nodes_from('GJK')
358
        assert_equal(sorted(G['A']), ['B', 'C'])
359
        assert_equal(sorted(G.neighbors('A')), ['B', 'C'])
360
        assert_equal(sorted(G.neighbors('A')), ['B', 'C'])
361
        assert_equal(sorted(G.neighbors('G')), [])
362
        assert_raises(nx.NetworkXError, G.neighbors, 'j')
363

    
364
    def test_iterators(self):
365
        G = self.G()
366
        G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'),
367
                          ('C', 'B'), ('C', 'D')])
368
        G.add_nodes_from('GJK')
369
        assert_equal(sorted(G.nodes()),
370
                     ['A', 'B', 'C', 'D', 'G', 'J', 'K'])
371
        assert_edges_equal(G.edges(),
372
                           [('A', 'B'), ('A', 'C'), ('B', 'D'), ('C', 'B'), ('C', 'D')])
373

    
374
        assert_equal(sorted([v for k, v in G.degree()]),
375
                     [0, 0, 0, 2, 2, 3, 3])
376
        assert_equal(sorted(G.degree(), key=str),
377
                     [('A', 2), ('B', 3), ('C', 3), ('D', 2),
378
                      ('G', 0), ('J', 0), ('K', 0)])
379
        assert_equal(sorted(G.neighbors('A')), ['B', 'C'])
380
        assert_raises(nx.NetworkXError, G.neighbors, 'X')
381
        G.clear()
382
        assert_equal(nx.number_of_nodes(G), 0)
383
        assert_equal(nx.number_of_edges(G), 0)
384

    
385
    def test_null_subgraph(self):
386
        # Subgraph of a null graph is a null graph
387
        nullgraph = nx.null_graph()
388
        G = nx.null_graph()
389
        H = G.subgraph([])
390
        assert_true(nx.is_isomorphic(H, nullgraph))
391

    
392
    def test_empty_subgraph(self):
393
        # Subgraph of an empty graph is an empty graph. test 1
394
        nullgraph = nx.null_graph()
395
        E5 = nx.empty_graph(5)
396
        E10 = nx.empty_graph(10)
397
        H = E10.subgraph([])
398
        assert_true(nx.is_isomorphic(H, nullgraph))
399
        H = E10.subgraph([1, 2, 3, 4, 5])
400
        assert_true(nx.is_isomorphic(H, E5))
401

    
402
    def test_complete_subgraph(self):
403
        # Subgraph of a complete graph is a complete graph
404
        K1 = nx.complete_graph(1)
405
        K3 = nx.complete_graph(3)
406
        K5 = nx.complete_graph(5)
407
        H = K5.subgraph([1, 2, 3])
408
        assert_true(nx.is_isomorphic(H, K3))
409

    
410
    def test_subgraph_nbunch(self):
411
        nullgraph = nx.null_graph()
412
        K1 = nx.complete_graph(1)
413
        K3 = nx.complete_graph(3)
414
        K5 = nx.complete_graph(5)
415
        # Test G.subgraph(nbunch), where nbunch is a single node
416
        H = K5.subgraph(1)
417
        assert_true(nx.is_isomorphic(H, K1))
418
        # Test G.subgraph(nbunch), where nbunch is a set
419
        H = K5.subgraph(set([1]))
420
        assert_true(nx.is_isomorphic(H, K1))
421
        # Test G.subgraph(nbunch), where nbunch is an iterator
422
        H = K5.subgraph(iter(K3))
423
        assert_true(nx.is_isomorphic(H, K3))
424
        # Test G.subgraph(nbunch), where nbunch is another graph
425
        H = K5.subgraph(K3)
426
        assert_true(nx.is_isomorphic(H, K3))
427
        H = K5.subgraph([9])
428
        assert_true(nx.is_isomorphic(H, nullgraph))
429

    
430
    def test_node_tuple_issue(self):
431
        H = self.G()
432
        # Test error handling of tuple as a node
433
        assert_raises(nx.NetworkXError, H.remove_node, (1, 2))
434
        H.remove_nodes_from([(1, 2)])  # no error
435
        assert_raises(nx.NetworkXError, H.neighbors, (1, 2))