1 #!/usr/bin/env python 2 from nose.tools import * 3 import networkx 4 from test_graph import BaseGraphTester, BaseAttrGraphTester, TestGraph 5 6 class BaseDiGraphTester(BaseGraphTester): 7 def test_has_successor(self): 8 G=self.K3 9 assert_equal(G.has_successor(0,1),True) 10 assert_equal(G.has_successor(0,-1),False) 11 12 def test_successors(self): 13 G=self.K3 14 assert_equal(sorted(G.successors(0)),[1,2]) 15 assert_raises((KeyError,networkx.NetworkXError), G.successors,-1) 16 17 def test_successors_iter(self): 18 G=self.K3 19 assert_equal(sorted(G.successors_iter(0)),[1,2]) 20 assert_raises((KeyError,networkx.NetworkXError), G.successors_iter,-1) 21 22 def test_has_predecessor(self): 23 G=self.K3 24 assert_equal(G.has_predecessor(0,1),True) 25 assert_equal(G.has_predecessor(0,-1),False) 26 27 def test_predecessors(self): 28 G=self.K3 29 assert_equal(sorted(G.predecessors(0)),[1,2]) 30 assert_raises((KeyError,networkx.NetworkXError), G.predecessors,-1) 31 32 def test_predecessors_iter(self): 33 G=self.K3 34 assert_equal(sorted(G.predecessors_iter(0)),[1,2]) 35 assert_raises((KeyError,networkx.NetworkXError), G.predecessors_iter,-1) 36 37 def test_edges(self): 38 G=self.K3 39 assert_equal(sorted(G.edges()),[(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]) 40 assert_equal(sorted(G.edges(0)),[(0,1),(0,2)]) 41 assert_raises((KeyError,networkx.NetworkXError), G.edges,-1) 42 43 def test_edges_iter(self): 44 G=self.K3 45 assert_equal(sorted(G.edges_iter()), 46 [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]) 47 assert_equal(sorted(G.edges_iter(0)),[(0,1),(0,2)]) 48 49 def test_edges_data(self): 50 G=self.K3 51 assert_equal(sorted(G.edges(data=True)), 52 [(0,1,{}),(0,2,{}),(1,0,{}),(1,2,{}),(2,0,{}),(2,1,{})]) 53 assert_equal(sorted(G.edges(0,data=True)),[(0,1,{}),(0,2,{})]) 54 assert_raises((KeyError,networkx.NetworkXError), G.edges,-1) 55 56 def test_out_edges(self): 57 G=self.K3 58 assert_equal(sorted(G.out_edges()), 59 [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]) 60 assert_equal(sorted(G.out_edges(0)),[(0,1),(0,2)]) 61 assert_raises((KeyError,networkx.NetworkXError), G.out_edges,-1) 62 63 def test_out_edges_iter(self): 64 G=self.K3 65 assert_equal(sorted(G.out_edges_iter()), 66 [(0,1),(0,2),(1,0),(1,2),(2,0),(2,1)]) 67 assert_equal(sorted(G.edges_iter(0)),[(0,1),(0,2)]) 68 69 def test_out_edges_dir(self): 70 G=self.P3 71 assert_equal(sorted(G.out_edges()),[(0, 1), (1, 2)]) 72 assert_equal(sorted(G.out_edges(0)),[(0, 1)]) 73 assert_equal(sorted(G.out_edges(2)),[]) 74 75 def test_out_edges_iter_dir(self): 76 G=self.P3 77 assert_equal(sorted(G.out_edges_iter()),[(0, 1), (1, 2)]) 78 assert_equal(sorted(G.out_edges_iter(0)),[(0, 1)]) 79 assert_equal(sorted(G.out_edges_iter(2)),[]) 80 81 def test_in_edges_dir(self): 82 G=self.P3 83 assert_equal(sorted(G.in_edges()),[(0, 1), (1, 2)]) 84 assert_equal(sorted(G.in_edges(0)),[]) 85 assert_equal(sorted(G.in_edges(2)),[(1,2)]) 86 87 def test_in_edges_iter_dir(self): 88 G=self.P3 89 assert_equal(sorted(G.in_edges_iter()),[(0, 1), (1, 2)]) 90 assert_equal(sorted(G.in_edges_iter(0)),[]) 91 assert_equal(sorted(G.in_edges_iter(2)),[(1,2)]) 92 93 def test_degree(self): 94 G=self.K3 95 assert_equal(list(G.degree().values()),[4,4,4]) 96 assert_equal(G.degree(),{0:4,1:4,2:4}) 97 assert_equal(G.degree(0),4) 98 assert_equal(G.degree([0]),{0:4}) 99 assert_raises((KeyError,networkx.NetworkXError), G.degree,-1) 100 101 def test_degree_iter(self): 102 G=self.K3 103 assert_equal(list(G.degree_iter()),[(0,4),(1,4),(2,4)]) 104 assert_equal(dict(G.degree_iter()),{0:4,1:4,2:4}) 105 assert_equal(list(G.degree_iter(0)),[(0,4)]) 106 107 def test_in_degree(self): 108 G=self.K3 109 assert_equal(list(G.in_degree().values()),[2,2,2]) 110 assert_equal(G.in_degree(),{0:2,1:2,2:2}) 111 assert_equal(G.in_degree(0),2) 112 assert_equal(G.in_degree([0]),{0:2}) 113 assert_raises((KeyError,networkx.NetworkXError), G.in_degree,-1) 114 115 def test_in_degree_iter(self): 116 G=self.K3 117 assert_equal(list(G.in_degree_iter()),[(0,2),(1,2),(2,2)]) 118 assert_equal(dict(G.in_degree_iter()),{0:2,1:2,2:2}) 119 assert_equal(list(G.in_degree_iter(0)),[(0,2)]) 120 121 def test_in_degree_iter_weighted(self): 122 G=self.K3 123 G.add_edge(0,1,weight=0.3,other=1.2) 124 assert_equal(list(G.in_degree_iter(weight='weight')),[(0,2),(1,1.3),(2,2)]) 125 assert_equal(dict(G.in_degree_iter(weight='weight')),{0:2,1:1.3,2:2}) 126 assert_equal(list(G.in_degree_iter(1,weight='weight')),[(1,1.3)]) 127 assert_equal(list(G.in_degree_iter(weight='other')),[(0,2),(1,2.2),(2,2)]) 128 assert_equal(dict(G.in_degree_iter(weight='other')),{0:2,1:2.2,2:2}) 129 assert_equal(list(G.in_degree_iter(1,weight='other')),[(1,2.2)]) 130 131 def test_out_degree(self): 132 G=self.K3 133 assert_equal(list(G.out_degree().values()),[2,2,2]) 134 assert_equal(G.out_degree(),{0:2,1:2,2:2}) 135 assert_equal(G.out_degree(0),2) 136 assert_equal(G.out_degree([0]),{0:2}) 137 assert_raises((KeyError,networkx.NetworkXError), G.out_degree,-1) 138 139 def test_out_degree_iter_weighted(self): 140 G=self.K3 141 G.add_edge(0,1,weight=0.3,other=1.2) 142 assert_equal(list(G.out_degree_iter(weight='weight')),[(0,1.3),(1,2),(2,2)]) 143 assert_equal(dict(G.out_degree_iter(weight='weight')),{0:1.3,1:2,2:2}) 144 assert_equal(list(G.out_degree_iter(0,weight='weight')),[(0,1.3)]) 145 assert_equal(list(G.out_degree_iter(weight='other')),[(0,2.2),(1,2),(2,2)]) 146 assert_equal(dict(G.out_degree_iter(weight='other')),{0:2.2,1:2,2:2}) 147 assert_equal(list(G.out_degree_iter(0,weight='other')),[(0,2.2)]) 148 149 def test_out_degree_iter(self): 150 G=self.K3 151 assert_equal(list(G.out_degree_iter()),[(0,2),(1,2),(2,2)]) 152 assert_equal(dict(G.out_degree_iter()),{0:2,1:2,2:2}) 153 assert_equal(list(G.out_degree_iter(0)),[(0,2)]) 154 155 def test_size(self): 156 G=self.K3 157 assert_equal(G.size(),6) 158 assert_equal(G.number_of_edges(),6) 159 160 def test_to_undirected_reciprocal(self): 161 G=self.Graph() 162 G.add_edge(1,2) 163 assert_true(G.to_undirected().has_edge(1,2)) 164 assert_false(G.to_undirected(reciprocal=True).has_edge(1,2)) 165 G.add_edge(2,1) 166 assert_true(G.to_undirected(reciprocal=True).has_edge(1,2)) 167 168 def test_reverse_copy(self): 169 G=networkx.DiGraph([(0,1),(1,2)]) 170 R=G.reverse() 171 assert_equal(sorted(R.edges()),[(1,0),(2,1)]) 172 R.remove_edge(1,0) 173 assert_equal(sorted(R.edges()),[(2,1)]) 174 assert_equal(sorted(G.edges()),[(0,1),(1,2)]) 175 176 def test_reverse_nocopy(self): 177 G=networkx.DiGraph([(0,1),(1,2)]) 178 R=G.reverse(copy=False) 179 assert_equal(sorted(R.edges()),[(1,0),(2,1)]) 180 R.remove_edge(1,0) 181 assert_equal(sorted(R.edges()),[(2,1)]) 182 assert_equal(sorted(G.edges()),[(2,1)]) 183 184 class BaseAttrDiGraphTester(BaseDiGraphTester,BaseAttrGraphTester): 185 pass 186 187 188 class TestDiGraph(BaseAttrDiGraphTester,TestGraph): 189 """Tests specific to dict-of-dict-of-dict digraph data structure""" 190 def setUp(self): 191 self.Graph=networkx.DiGraph 192 # build dict-of-dict-of-dict K3 193 ed1,ed2,ed3,ed4,ed5,ed6 = ({},{},{},{},{},{}) 194 self.k3adj={0: {1: ed1, 2: ed2}, 1: {0: ed3, 2: ed4}, 2: {0: ed5, 1:ed6}} 195 self.k3edges=[(0, 1), (0, 2), (1, 2)] 196 self.k3nodes=[0, 1, 2] 197 self.K3=self.Graph() 198 self.K3.adj = self.K3.succ = self.K3.edge = self.k3adj 199 self.K3.pred={0: {1: ed3, 2: ed5}, 1: {0: ed1, 2: ed6}, 2: {0: ed2, 1:ed4}} 200 201 ed1,ed2 = ({},{}) 202 self.P3=self.Graph() 203 self.P3.adj={0: {1: ed1}, 1: {2: ed2}, 2: {}} 204 self.P3.succ=self.P3.adj 205 self.P3.pred={0: {}, 1: {0: ed1}, 2: {1: ed2}} 206 self.K3.node={} 207 self.K3.node[0]={} 208 self.K3.node[1]={} 209 self.K3.node[2]={} 210 self.P3.node={} 211 self.P3.node[0]={} 212 self.P3.node[1]={} 213 self.P3.node[2]={} 214 215 def test_data_input(self): 216 G=self.Graph(data={1:[2],2:[1]}, name="test") 217 assert_equal(G.name,"test") 218 assert_equal(sorted(G.adj.items()),[(1, {2: {}}), (2, {1: {}})]) 219 assert_equal(sorted(G.succ.items()),[(1, {2: {}}), (2, {1: {}})]) 220 assert_equal(sorted(G.pred.items()),[(1, {2: {}}), (2, {1: {}})]) 221 222 def test_add_edge(self): 223 G=self.Graph() 224 G.add_edge(0,1) 225 assert_equal(G.adj,{0: {1: {}}, 1: {}}) 226 assert_equal(G.succ,{0: {1: {}}, 1: {}}) 227 assert_equal(G.pred,{0: {}, 1: {0:{}}}) 228 G=self.Graph() 229 G.add_edge(*(0,1)) 230 assert_equal(G.adj,{0: {1: {}}, 1: {}}) 231 assert_equal(G.succ,{0: {1: {}}, 1: {}}) 232 assert_equal(G.pred,{0: {}, 1: {0:{}}}) 233 234 def test_add_edges_from(self): 235 G=self.Graph() 236 G.add_edges_from([(0,1),(0,2,{'data':3})],data=2) 237 assert_equal(G.adj,{0: {1: {'data':2}, 2: {'data':3}}, 1: {}, 2: {}}) 238 assert_equal(G.succ,{0: {1: {'data':2}, 2: {'data':3}}, 1: {}, 2: {}}) 239 assert_equal(G.pred,{0: {}, 1: {0: {'data':2}}, 2: {0: {'data':3}}}) 240 241 assert_raises(networkx.NetworkXError, G.add_edges_from,[(0,)]) # too few in tuple 242 assert_raises(networkx.NetworkXError, G.add_edges_from,[(0,1,2,3)]) # too many in tuple 243 assert_raises(TypeError, G.add_edges_from,[0]) # not a tuple 244 245 def test_remove_edge(self): 246 G=self.K3 247 G.remove_edge(0,1) 248 assert_equal(G.succ,{0:{2:{}},1:{0:{},2:{}},2:{0:{},1:{}}}) 249 assert_equal(G.pred,{0:{1:{}, 2:{}}, 1:{2:{}}, 2:{0:{},1:{}}}) 250 assert_raises((KeyError,networkx.NetworkXError), G.remove_edge,-1,0) 251 252 def test_remove_edges_from(self): 253 G=self.K3 254 G.remove_edges_from([(0,1)]) 255 assert_equal(G.succ,{0:{2:{}},1:{0:{},2:{}},2:{0:{},1:{}}}) 256 assert_equal(G.pred,{0:{1:{}, 2:{}}, 1:{2:{}}, 2:{0:{},1: {}}}) 257 G.remove_edges_from([(0,0)]) # silent fail 258