1 #!/usr/bin/env python 2 """Original NetworkX graph tests""" 3 from nose.tools import * 4 import networkx 5 import networkx as nx 6 7 from historical_tests import HistoricalTests 8 9 class TestDiGraphHistorical(HistoricalTests): 10 11 def setUp(self): 12 HistoricalTests.setUp(self) 13 self.G=nx.DiGraph 14 15 16 def test_in_degree(self): 17 G=self.G() 18 G.add_nodes_from('GJK') 19 G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), 20 ('B', 'C'), ('C', 'D')]) 21 22 assert_equal(sorted(G.in_degree().values()),[0, 0, 0, 0, 1, 2, 2]) 23 assert_equal(G.in_degree(), 24 {'A': 0, 'C': 2, 'B': 1, 'D': 2, 'G': 0, 'K': 0, 'J': 0}) 25 assert_equal(sorted([v for k,v in G.in_degree_iter()]), 26 [0, 0, 0, 0, 1, 2, 2]) 27 assert_equal(dict(G.in_degree_iter()), 28 {'A': 0, 'C': 2, 'B': 1, 'D': 2, 'G': 0, 'K': 0, 'J': 0}) 29 30 31 def test_out_degree(self): 32 G=self.G() 33 G.add_nodes_from('GJK') 34 G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), 35 ('B', 'C'), ('C', 'D')]) 36 assert_equal(sorted(G.out_degree().values()),[0, 0, 0, 0, 1, 2, 2]) 37 assert_equal(G.out_degree(), 38 {'A': 2, 'C': 1, 'B': 2, 'D': 0, 'G': 0, 'K': 0, 'J': 0}) 39 assert_equal(sorted([v for k,v in G.in_degree_iter()]), 40 [0, 0, 0, 0, 1, 2, 2]) 41 assert_equal(dict(G.out_degree_iter()), 42 {'A': 2, 'C': 1, 'B': 2, 'D': 0, 'G': 0, 'K': 0, 'J': 0}) 43 44 45 def test_degree_digraph(self): 46 H=nx.DiGraph() 47 H.add_edges_from([(1,24),(1,2)]) 48 assert_equal(sorted(H.in_degree([1,24]).values()),[0, 1]) 49 assert_equal(sorted(H.out_degree([1,24]).values()),[0, 2]) 50 assert_equal(sorted(H.degree([1,24]).values()),[1, 2]) 51 52 53 def test_neighbors(self): 54 G=self.G() 55 G.add_nodes_from('GJK') 56 G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), 57 ('B', 'C'), ('C', 'D')]) 58 59 assert_equal(sorted(G.neighbors('C')),['D']) 60 assert_equal(sorted(G['C']),['D']) 61 assert_equal(sorted(G.neighbors('A')),['B', 'C']) 62 assert_equal(sorted(G.neighbors_iter('A')),['B', 'C']) 63 assert_equal(sorted(G.neighbors_iter('C')),['D']) 64 assert_equal(sorted(G.neighbors('A')),['B', 'C']) 65 assert_raises(nx.NetworkXError,G.neighbors,'j') 66 assert_raises(nx.NetworkXError,G.neighbors_iter,'j') 67 68 def test_successors(self): 69 G=self.G() 70 G.add_nodes_from('GJK') 71 G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), 72 ('B', 'C'), ('C', 'D')]) 73 assert_equal(sorted(G.successors('A')),['B', 'C']) 74 assert_equal(sorted(G.successors_iter('A')),['B', 'C']) 75 assert_equal(sorted(G.successors('G')),[]) 76 assert_equal(sorted(G.successors('D')),[]) 77 assert_equal(sorted(G.successors_iter('G')),[]) 78 assert_raises(nx.NetworkXError,G.successors,'j') 79 assert_raises(nx.NetworkXError,G.successors_iter,'j') 80 81 82 def test_predecessors(self): 83 G=self.G() 84 G.add_nodes_from('GJK') 85 G.add_edges_from([('A', 'B'), ('A', 'C'), ('B', 'D'), 86 ('B', 'C'), ('C', 'D')]) 87 assert_equal(sorted(G.predecessors('C')),['A', 'B']) 88 assert_equal(sorted(G.predecessors_iter('C')),['A', 'B']) 89 assert_equal(sorted(G.predecessors('G')),[]) 90 assert_equal(sorted(G.predecessors('A')),[]) 91 assert_equal(sorted(G.predecessors_iter('G')),[]) 92 assert_equal(sorted(G.predecessors_iter('A')),[]) 93 assert_equal(sorted(G.successors_iter('D')),[]) 94 95 assert_raises(nx.NetworkXError,G.predecessors,'j') 96 assert_raises(nx.NetworkXError,G.predecessors,'j') 97 98 99 100 def test_reverse(self): 101 G=nx.complete_graph(10) 102 H=G.to_directed() 103 HR=H.reverse() 104 assert_true(nx.is_isomorphic(H,HR)) 105 assert_equal(sorted(H.edges()),sorted(HR.edges())) 106 107 def test_reverse2(self): 108 H=nx.DiGraph() 109 foo=[H.add_edge(u,u+1) for u in range(0,5)] 110 HR=H.reverse() 111 for u in range(0,5): 112 assert_true(HR.has_edge(u+1,u)) 113 114 def test_reverse3(self): 115 H=nx.DiGraph() 116 H.add_nodes_from([1,2,3,4]) 117 HR=H.reverse() 118 assert_equal(sorted(HR.nodes()),[1, 2, 3, 4]) 119 120