1 #!/usr/bin/env python 2 import random 3 from nose.tools import * 4 import networkx 5 import networkx as nx 6 7 class TestFunction(object): 8 def setUp(self): 9 self.G=networkx.Graph({0:[1,2,3], 1:[1,2,0], 4:[]}, name='Test') 10 self.Gdegree={0:3, 1:2, 2:2, 3:1, 4:0} 11 self.Gnodes=list(range(5)) 12 self.Gedges=[(0,1),(0,2),(0,3),(1,0),(1,1),(1,2)] 13 self.DG=networkx.DiGraph({0:[1,2,3], 1:[1,2,0], 4:[]}) 14 self.DGin_degree={0:1, 1:2, 2:2, 3:1, 4:0} 15 self.DGout_degree={0:3, 1:3, 2:0, 3:0, 4:0} 16 self.DGnodes=list(range(5)) 17 self.DGedges=[(0,1),(0,2),(0,3),(1,0),(1,1),(1,2)] 18 19 def test_nodes(self): 20 assert_equal(self.G.nodes(),networkx.nodes(self.G)) 21 assert_equal(self.DG.nodes(),networkx.nodes(self.DG)) 22 def test_edges(self): 23 assert_equal(self.G.edges(),networkx.edges(self.G)) 24 assert_equal(self.DG.edges(),networkx.edges(self.DG)) 25 assert_equal(self.G.edges(nbunch=[0,1,3]),networkx.edges(self.G,nbunch=[0,1,3])) 26 assert_equal(self.DG.edges(nbunch=[0,1,3]),networkx.edges(self.DG,nbunch=[0,1,3])) 27 def test_nodes_iter(self): 28 assert_equal(list(self.G.nodes_iter()),list(networkx.nodes_iter(self.G))) 29 assert_equal(list(self.DG.nodes_iter()),list(networkx.nodes_iter(self.DG))) 30 def test_edges_iter(self): 31 assert_equal(list(self.G.edges_iter()),list(networkx.edges_iter(self.G))) 32 assert_equal(list(self.DG.edges_iter()),list(networkx.edges_iter(self.DG))) 33 assert_equal(list(self.G.edges_iter(nbunch=[0,1,3])),list(networkx.edges_iter(self.G,nbunch=[0,1,3]))) 34 assert_equal(list(self.DG.edges_iter(nbunch=[0,1,3])),list(networkx.edges_iter(self.DG,nbunch=[0,1,3]))) 35 def test_degree(self): 36 assert_equal(self.G.degree(),networkx.degree(self.G)) 37 assert_equal(self.DG.degree(),networkx.degree(self.DG)) 38 assert_equal(self.G.degree(nbunch=[0,1]),networkx.degree(self.G,nbunch=[0,1])) 39 assert_equal(self.DG.degree(nbunch=[0,1]),networkx.degree(self.DG,nbunch=[0,1])) 40 assert_equal(self.G.degree(weight='weight'),networkx.degree(self.G,weight='weight')) 41 assert_equal(self.DG.degree(weight='weight'),networkx.degree(self.DG,weight='weight')) 42 def test_neighbors(self): 43 assert_equal(self.G.neighbors(1),networkx.neighbors(self.G,1)) 44 assert_equal(self.DG.neighbors(1),networkx.neighbors(self.DG,1)) 45 def test_number_of_nodes(self): 46 assert_equal(self.G.number_of_nodes(),networkx.number_of_nodes(self.G)) 47 assert_equal(self.DG.number_of_nodes(),networkx.number_of_nodes(self.DG)) 48 def test_number_of_edges(self): 49 assert_equal(self.G.number_of_edges(),networkx.number_of_edges(self.G)) 50 assert_equal(self.DG.number_of_edges(),networkx.number_of_edges(self.DG)) 51 def test_is_directed(self): 52 assert_equal(self.G.is_directed(),networkx.is_directed(self.G)) 53 assert_equal(self.DG.is_directed(),networkx.is_directed(self.DG)) 54 def test_subgraph(self): 55 assert_equal(self.G.subgraph([0,1,2,4]).adj,networkx.subgraph(self.G,[0,1,2,4]).adj) 56 assert_equal(self.DG.subgraph([0,1,2,4]).adj,networkx.subgraph(self.DG,[0,1,2,4]).adj) 57 58 def test_create_empty_copy(self): 59 G=networkx.create_empty_copy(self.G, with_nodes=False) 60 assert_equal(G.nodes(),[]) 61 assert_equal(G.graph,{}) 62 assert_equal(G.node,{}) 63 assert_equal(G.edge,{}) 64 G=networkx.create_empty_copy(self.G) 65 assert_equal(G.nodes(),self.G.nodes()) 66 assert_equal(G.graph,{}) 67 assert_equal(G.node,{}.fromkeys(self.G.nodes(),{})) 68 assert_equal(G.edge,{}.fromkeys(self.G.nodes(),{})) 69 70 def test_degree_histogram(self): 71 assert_equal(networkx.degree_histogram(self.G), [1,1,1,1,1]) 72 73 def test_density(self): 74 assert_equal(networkx.density(self.G), 0.5) 75 assert_equal(networkx.density(self.DG), 0.3) 76 G=networkx.Graph() 77 G.add_node(1) 78 assert_equal(networkx.density(G), 0.0) 79 80 def test_density_selfloop(self): 81 G = nx.Graph() 82 G.add_edge(1,1) 83 assert_equal(networkx.density(G), 0.0) 84 G.add_edge(1,2) 85 assert_equal(networkx.density(G), 2.0) 86 87 def test_freeze(self): 88 G=networkx.freeze(self.G) 89 assert_equal(G.frozen,True) 90 assert_raises(networkx.NetworkXError, G.add_node, 1) 91 assert_raises(networkx.NetworkXError, G.add_nodes_from, [1]) 92 assert_raises(networkx.NetworkXError, G.remove_node, 1) 93 assert_raises(networkx.NetworkXError, G.remove_nodes_from, [1]) 94 assert_raises(networkx.NetworkXError, G.add_edge, 1,2) 95 assert_raises(networkx.NetworkXError, G.add_edges_from, [(1,2)]) 96 assert_raises(networkx.NetworkXError, G.remove_edge, 1,2) 97 assert_raises(networkx.NetworkXError, G.remove_edges_from, [(1,2)]) 98 assert_raises(networkx.NetworkXError, G.clear) 99 100 def test_is_frozen(self): 101 assert_equal(networkx.is_frozen(self.G), False) 102 G=networkx.freeze(self.G) 103 assert_equal(G.frozen, networkx.is_frozen(self.G)) 104 assert_equal(G.frozen,True) 105 106 def test_info(self): 107 G=networkx.path_graph(5) 108 info=networkx.info(G) 109 expected_graph_info='\n'.join(['Name: path_graph(5)', 110 'Type: Graph', 111 'Number of nodes: 5', 112 'Number of edges: 4', 113 'Average degree: 1.6000']) 114 assert_equal(info,expected_graph_info) 115 116 info=networkx.info(G,n=1) 117 expected_node_info='\n'.join( 118 ['Node 1 has the following properties:', 119 'Degree: 2', 120 'Neighbors: 0 2']) 121 assert_equal(info,expected_node_info) 122 123 def test_info_digraph(self): 124 G=networkx.DiGraph(name='path_graph(5)') 125 G.add_path([0,1,2,3,4]) 126 info=networkx.info(G) 127 expected_graph_info='\n'.join(['Name: path_graph(5)', 128 'Type: DiGraph', 129 'Number of nodes: 5', 130 'Number of edges: 4', 131 'Average in degree: 0.8000', 132 'Average out degree: 0.8000']) 133 assert_equal(info,expected_graph_info) 134 135 info=networkx.info(G,n=1) 136 expected_node_info='\n'.join( 137 ['Node 1 has the following properties:', 138 'Degree: 2', 139 'Neighbors: 2']) 140 assert_equal(info,expected_node_info) 141 142 assert_raises(networkx.NetworkXError,networkx.info,G,n=-1) 143 144 def test_neighbors(self): 145 graph = nx.complete_graph(100) 146 pop = random.sample(graph.nodes(), 1) 147 nbors = list(nx.neighbors(graph, pop[0])) 148 # should be all the other vertices in the graph 149 assert_equal(len(nbors), len(graph) - 1) 150 151 graph = nx.path_graph(100) 152 node = random.sample(graph.nodes(), 1)[0] 153 nbors = list(nx.neighbors(graph, node)) 154 # should be all the other vertices in the graph 155 if node != 0 and node != 99: 156 assert_equal(len(nbors), 2) 157 else: 158 assert_equal(len(nbors), 1) 159 160 # create a star graph with 99 outer nodes 161 graph = nx.star_graph(99) 162 nbors = list(nx.neighbors(graph, 0)) 163 assert_equal(len(nbors), 99) 164 165 def test_non_neighbors(self): 166 graph = nx.complete_graph(100) 167 pop = random.sample(graph.nodes(), 1) 168 nbors = list(nx.non_neighbors(graph, pop[0])) 169 # should be all the other vertices in the graph 170 assert_equal(len(nbors), 0) 171 172 graph = nx.path_graph(100) 173 node = random.sample(graph.nodes(), 1)[0] 174 nbors = list(nx.non_neighbors(graph, node)) 175 # should be all the other vertices in the graph 176 if node != 0 and node != 99: 177 assert_equal(len(nbors), 97) 178 else: 179 assert_equal(len(nbors), 98) 180 181 # create a star graph with 99 outer nodes 182 graph = nx.star_graph(99) 183 nbors = list(nx.non_neighbors(graph, 0)) 184 assert_equal(len(nbors), 0) 185 186 # disconnected graph 187 graph = nx.Graph() 188 graph.add_nodes_from(range(10)) 189 nbors = list(nx.non_neighbors(graph, 0)) 190 assert_equal(len(nbors), 9) 191