4
Unit tests for XGraph Class in xbase.py
5
---------------------------------------
7
>>> from networkx import *
8
>>> from networkx.isomorph import graph_could_be_isomorphic
9
>>> is_isomorphic=graph_could_be_isomorphic
10
>>> from networkx.operators import convert_node_labels_to_integers as cnlti
16
>>> P1=cnlti(path_graph(1),first_label=1)
17
>>> P3=cnlti(path_graph(3),first_label=1)
18
>>> P10=cnlti(path_graph(10),first_label=1)
19
>>> K1=cnlti(complete_graph(1),first_label=1)
20
>>> K3=cnlti(complete_graph(3),first_label=1)
21
>>> K4=cnlti(complete_graph(4),first_label=1)
22
>>> K5=cnlti(complete_graph(5),first_label=1)
23
>>> K10=cnlti(complete_graph(10),first_label=1)
25
Same small graphs but as XGraphs
26
--------------------------------
30
>>> P1X.add_nodes_from(P1)
31
>>> P1X.add_edges_from(P1.edges())
33
>>> P3X.add_edges_from(P3.edges())
35
>>> P10X.add_edges_from(P10.edges())
37
>>> K1X.add_nodes_from(K1)
38
>>> K1X.add_edges_from(K1.edges())
40
>>> K3X.add_edges_from(K3.edges())
42
>>> K4X.add_edges_from(K4.edges())
44
>>> K5X.add_edges_from(K5.edges())
49
>>> G = XGraph(name="test")
50
>>> print G # test of __str__
67
Test if a non-hashable object is in the Graph. A python dict will
68
raise a TypeError, but for a Graph class a simple False should be
69
returned (see Graph __contains__). If it cannot be a node then it is
74
>>> G.has_node({'A':1})
77
>>> G.delete_node('A')
80
>>> G.add_nodes_from(list("ABCDEFGHIJKL"))
83
>>> G.delete_nodes_from(['H','I','J','K','L'])
84
>>> G.add_nodes_from([1,2,3,4])
86
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
87
>>> sorted(G) # test __iter__
88
[1, 2, 3, 4, 'A', 'B', 'C', 'D', 'E', 'F', 'G']
96
>>> [] in G # never raise a Key or TypeError in this test
106
test node portion of clear()
113
Test add_node and delete_node acting for various nbunch
118
>>> G.add_node('m') # no complaints
119
>>> G.delete_node('j') # NetworkXError
120
Traceback (most recent call last):
122
NetworkXError: node j not in graph
123
>>> G.delete_node('m')
129
>>> G.add_nodes_from(list("ABCD"))
130
>>> G.add_nodes_from(P3) # add nbunch of nodes (nbunch=Graph)
131
>>> sorted(G.nodes())
132
[1, 2, 3, 'A', 'B', 'C', 'D']
133
>>> G.delete_nodes_from(P3) # delete nbunch of nodes (nbunch=Graph)
134
>>> sorted(G.nodes())
139
>>> nbunch=set("ABCDEFGHIJKL")
140
>>> G.add_nodes_from(nbunch)
144
nbunch is a dict with nodes as keys
146
>>> nbunch={'I':"foo",'J':2,'K':True,'L':"spam"}
147
>>> G.delete_nodes_from(nbunch)
148
>>> sorted(G.nodes())
149
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
151
nbunch is an iterator
153
>>> n_iter=P3.nodes_iter()
154
>>> G.add_nodes_from(n_iter)
155
>>> sorted(G.nodes())
156
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
157
>>> n_iter=P3.nodes_iter() # rebuild same iterator
158
>>> G.delete_nodes_from(n_iter) # delete nbunch of nodes (nbunch=iterator)
159
>>> sorted(G.nodes())
160
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
165
>>> G.add_nodes_from(nbunch)
166
>>> sorted(G.nodes())
167
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
172
>>> G.add_nodes_from(nbunch)
173
>>> sorted(G.nodes())
174
[1, 2, 3, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
182
Traceback (most recent call last):
184
ValueError: need more than 1 value to unpack
186
>>> G.add_edge('A','B') # testing add_edge()
187
>>> G.add_edge('A','B') # should fail silently
188
>>> G.has_edge('A','B') # testing has_edge()
190
>>> G.has_edge('A','C')
192
>>> G.has_edge( ('A','B') )
194
>>> G.has_edge('B','A') # G is undirected, so B->A is an edge
196
>>> G.has_neighbor('A','C') # same as has_edge
198
>>> G.has_neighbor('A','B')
201
>>> G.add_edge('A','C') # test directedness
202
>>> G.add_edge('C','A')
203
>>> G.delete_edge('C','A')
204
>>> G.has_edge('A','C') # G is undirected
206
>>> G.has_edge('C','A')
209
>>> G.add_edge('A','A') # test self loops
210
>>> G.has_edge('A','A')
213
>>> G.add_edge('X','X')
214
>>> G.has_node('X') # added node but not self loop
216
>>> G.delete_node('X')
219
>>> G.add_edge('A','Z') # test that missing node 'Z' is added silently
223
>>> G.add_edges_from([('B','C')]) # test add_edges_from()
224
>>> G.has_edge('B','C')
226
>>> G.has_edge('C','B') # undirected
228
>>> G.add_edges_from([('D','F'),('B','D')])
229
>>> G.has_edge('D','F')
231
>>> G.has_edge('B','D')
233
>>> G.has_edge('D','B') # undirected
235
>>> G.add_edges_from([tuple('IJ'),list('KK'),tuple('JK')]) # after failing silently, should add 2nd edge
236
>>> G.has_edge(('I','J'))
238
>>> G.has_edge(('K','K'))
240
>>> G.has_edge(('J','K'))
242
>>> G.has_edge(('K','J')) # undirected
245
>>> G.add_path(list('ACDE')) # test add_path() and add_cycle()
246
>>> G.has_edge('D','E')
248
>>> G.has_edge('E','C')
250
>>> G.add_cycle(list('MNOP'))
251
>>> G.has_edge('O','P')
253
>>> G.has_edge('P','M')
255
>>> G.delete_node('P') # tests delete_node()'s handling of edges.
256
>>> G.has_edge('P','M')
260
>>> G.delete_edge('M') # test delete_edge()
261
Traceback (most recent call last):
263
ValueError: need more than 1 value to unpack
265
>>> G.add_edge('N','M')
266
>>> G.has_edge('M','N')
268
>>> G.delete_edge('M','N')
269
>>> G.has_edge('M','N')
271
>>> G.has_edge('N','M') # undirected
273
>>> G.delete_edges_from([list('HI'),list('DF'),tuple('KK'),tuple('JK')]) # self loop fails silently
274
>>> G.has_edge('H','I')
276
>>> G.has_edge('J','K')
278
>>> G.delete_edges_from([list('IJ'),list('KK'),list('JK')])
279
>>> G.has_edge('I','J')
281
>>> G.delete_nodes_from(set('ZEFHIMNO'))
282
>>> sorted(G.nodes())
283
[1, 2, 3, 'A', 'B', 'C', 'D', 'G', 'J', 'K']
284
>>> G.delete_nodes_from([1,2,3])
285
>>> sorted(G.nodes())
286
['A', 'B', 'C', 'D', 'G', 'J', 'K']
287
>>> sorted(G.edges())
288
[('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
290
Test G.edges(nbunch) with various forms of nbunch
292
node not in nbunch should be quietly ignored
294
>>> sorted(G.edges(6)) # non-iterable non-node
295
Traceback (most recent call last):
297
NetworkXError: nbunch is not a node or a sequence of nodes.
299
>>> sorted(G.edges('Z')) # iterable non-node
302
nbunch can be an empty list
304
>>> sorted(G.edges([]))
309
>>> sorted(G.edges(['A','B']))
310
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
314
>>> sorted(G.edges(set(['A','B'])))
315
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
317
nbunch can be a graph
320
>>> G1.add_nodes_from('AB')
321
>>> sorted(G.edges(G1)) # nbunch is a graph
322
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
324
nbunch can be an XGraph
327
>>> G1.add_nodes_from('AB')
328
>>> sorted(G.edges(G1)) # nbunch is an XGraph
329
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
332
nbunch can be a dict with nodes as keys
334
>>> ndict={'A': "thing1", 'B': "thing2"}
335
>>> sorted(G.edges(ndict))
336
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
338
nbunch can be a single node
340
>>> sorted(G.edges('A'))
341
[('A', 'B', None), ('A', 'C', None)]
344
Test G.edges_iter(nbunch) with various forms of nbunch
346
node not in nbunch should be quietly ignored
348
>>> sorted(G.edges_iter('Z'))
351
nbunch can be an empty list
353
>>> sorted(G.edges_iter([]))
358
>>> sorted(G.edges_iter(['A','B']))
359
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
363
>>> sorted(G.edges_iter(set(['A','B'])))
364
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
366
nbunch can be a graph
369
>>> G1.add_nodes_from(['A','B'])
370
>>> sorted(G.edges_iter(G1)) # nbunch is a graph
371
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
373
nbunch can be an XGraph
376
>>> G1.add_nodes_from(['A','B'])
377
>>> sorted(G.edges_iter(G1)) # nbunch is a graph
378
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
381
nbunch can be a dict with nodes as keys
383
>>> ndict={'A': "thing1", 'B': "thing2"}
384
>>> sorted(G.edges_iter(ndict))
385
[('A', 'B', None), ('A', 'C', None), ('B', 'C', None), ('B', 'D', None)]
387
nbunch can be a single node
389
>>> sorted(G.edges_iter('A'))
390
[('A', 'B', None), ('A', 'C', None)]
392
nbunch can be nothing (whole graph)
394
>>> sorted(G.edges_iter())
395
[('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
396
>>> sorted(G.nodes_iter())
397
['A', 'B', 'C', 'D', 'G', 'J', 'K']
402
degree() of an empty graph must return []
406
>>> nullX.degree(with_labels=True)
408
>>> list(nullX.degree_iter())
410
>>> dict(nullX.degree_iter(with_labels=True))
414
degree of single node must return single int
419
degree of single node in iterable container must return list
424
with_labels=True always return a dict
426
>>> G.degree('A',with_labels=True)
429
>>> G.degree(['A','B'])
431
>>> G.degree(['A','B'],with_labels=True)
434
>>> sorted(G.degree())
435
[0, 0, 0, 2, 2, 3, 3]
436
>>> sorted(list(G.degree_iter()))
437
[0, 0, 0, 2, 2, 3, 3]
439
>>> P3X.degree(['A','B']) # silently ignore nodes not in P3
441
>>> sorted(P10X.degree(P3X)) # nbunch can be a graph
443
>>> sorted(P3X.degree(P10X)) # nbunch can be a graph thats way to big
447
>>> list(P10X.degree_iter([]))
449
>>> dict( P10X.degree_iter([],with_labels=True) )
451
>>> dict(P10X.degree_iter([],with_labels=True))
454
>>> nullX.degree(with_labels=True)
456
>>> list(nullX.degree_iter())
458
>>> dict(nullX.degree_iter(with_labels=True))
473
>>> H=G.copy() # copy
481
>>> SG=G.subgraph(['A','B','D']) # subgraph
482
>>> sorted(SG.nodes())
484
>>> sorted(SG.edges())
485
[('A', 'B', None), ('B', 'D', None)]
487
>>> DG=G.to_directed()
496
>>> sorted(DG.out_edges(list('A')))
497
[('A', 'B', None), ('A', 'C', None)]
498
>>> sorted(DG.in_edges(list('A')))
499
[('B', 'A', None), ('C', 'A', None)]
500
>>> DG.delete_edge('A','B')
501
>>> DG.has_edge('B','A') # this deletes B-A but not A-B
503
>>> DG.has_edge('A','B')
514
>>> sorted(G.neighbors('A'))
516
>>> sorted(G.neighbors_iter('A'))
519
>>> sorted(G.neighbors('G'))
521
>>> sorted(G.neighbors('j'))
522
Traceback (most recent call last):
524
NetworkXError: node j not in graph
531
['A', 'B', 'C', 'D', 'G', 'J', 'K']
532
>>> sorted(nodes_iter(G))
533
['A', 'B', 'C', 'D', 'G', 'J', 'K']
535
[('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
536
>>> sorted(edges_iter(G))
537
[('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
538
>>> sorted(degree(G))
539
[0, 0, 0, 2, 2, 3, 3]
540
>>> sorted(neighbors(G,'A'))
542
>>> number_of_nodes(G)
544
>>> number_of_edges(G)
546
>>> density(G)==5/(7*(7-1)*0.5)
548
>>> degree_histogram(G)
554
>>> sorted(G.nodes_iter())
555
['A', 'B', 'C', 'D', 'G', 'J', 'K']
556
>>> sorted(G.edges_iter())
557
[('A', 'B', None), ('A', 'C', None), ('B', 'D', None), ('C', 'B', None), ('C', 'D', None)]
558
>>> sorted(G.degree_iter())
559
[0, 0, 0, 2, 2, 3, 3]
560
>>> sorted(G.degree_iter(with_labels=True))
561
[('A', 2), ('B', 3), ('C', 3), ('D', 2), ('G', 0), ('J', 0), ('K', 0)]
562
>>> sorted(G.neighbors_iter('A'))
564
>>> sorted(G.neighbors_iter('X'))
565
Traceback (most recent call last):
567
NetworkXError: node X not in graph
570
>>> number_of_nodes(G)
572
>>> number_of_edges(G)
579
Subgraph of a null graph is a null graph
583
>>> is_isomorphic(H,nullX)
586
Subgraph of an empty graph is an empty graph. test 1
588
>>> E5X=empty_graph(5,create_using=XGraph())
589
>>> E10X=empty_graph(10,create_using=XGraph())
590
>>> H=E10X.subgraph([])
591
>>> is_isomorphic(H,nullX)
594
Subgraph of an empty graph is an empty graph. test 2
596
>>> H=E10X.subgraph([1,2,3,4,5])
597
>>> is_isomorphic(H,E5X)
600
Subgraph of a complete graph is a complete graph
602
>>> H=K5X.subgraph([1,2,3])
603
>>> is_isomorphic(H,K3X)
606
Test G.subgraph(nbunch), where nbunch is a single node
608
>>> H=K5X.subgraph(1)
609
>>> is_isomorphic(H,K1X)
612
>>> H=J5.subgraph(1,inplace=True)
613
>>> is_isomorphic(H,K1X)
615
>>> is_isomorphic(J5,K1X)
618
Test G.subgraph(nbunch), where nbunch is a set
620
>>> H=K5X.subgraph(set([1]))
621
>>> is_isomorphic(H,K1X)
624
>>> H=J5.subgraph(set([1]),inplace=True)
625
>>> is_isomorphic(H,K1X)
627
>>> is_isomorphic(J5,K1X)
630
Test G.subgraph(nbunch), where nbunch is an iterator
632
>>> H=K5X.subgraph(iter(K3X))
633
>>> is_isomorphic(H,K3X)
636
>>> H=J5.subgraph(iter(K3X),inplace=True)
637
>>> is_isomorphic(H,K3X)
639
>>> is_isomorphic(J5,K3X)
642
Test G.subgraph(nbunch), where nbunch is another graph
644
>>> H=K5X.subgraph(K3X)
645
>>> is_isomorphic(H,K3X)
648
>>> H=J5.subgraph(K3X,inplace=True)
649
>>> is_isomorphic(H,K3X)
651
>>> is_isomorphic(J5,K3X)
655
Test for no error when nbunch has node not in G.nodes()
657
>>> H=K5X.subgraph([9])
658
>>> is_isomorphic(H,nullX)