<s> import random <EOL> from nose . tools import * <EOL> import networkx as nx <EOL> class TestFunction ( object ) : <EOL> def setUp ( self ) : <EOL> self . G = nx . Graph ( { <NUM_LIT:0> : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT:1> : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ] , <NUM_LIT:4> : [ ] } , name = '<STR_LIT>' ) <EOL> self . Gdegree = { <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:1> , <NUM_LIT:4> : <NUM_LIT:0> } <EOL> self . Gnodes = list ( range ( <NUM_LIT:5> ) ) <EOL> self . Gedges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> self . DG = nx . DiGraph ( { <NUM_LIT:0> : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , <NUM_LIT:1> : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ] , <NUM_LIT:4> : [ ] } ) <EOL> self . DGin_degree = { <NUM_LIT:0> : <NUM_LIT:1> , <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:2> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:1> , <NUM_LIT:4> : <NUM_LIT:0> } <EOL> self . DGout_degree = { <NUM_LIT:0> : <NUM_LIT:3> , <NUM_LIT:1> : <NUM_LIT:3> , <NUM_LIT:2> : <NUM_LIT:0> , <NUM_LIT:3> : <NUM_LIT:0> , <NUM_LIT:4> : <NUM_LIT:0> } <EOL> self . DGnodes = list ( range ( <NUM_LIT:5> ) ) <EOL> self . DGedges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> def test_nodes ( self ) : <EOL> assert_equal ( list ( self . G . nodes ( ) ) , list ( nx . nodes ( self . G ) ) ) <EOL> assert_equal ( list ( self . DG . nodes ( ) ) , list ( nx . nodes ( self . DG ) ) ) <EOL> def test_edges ( self ) : <EOL> assert_equal ( list ( self . G . edges ( ) ) , list ( nx . edges ( self . G ) ) ) <EOL> assert_equal ( list ( self . DG . edges ( ) ) , list ( nx . edges ( self . DG ) ) ) <EOL> assert_equal ( list ( self . G . edges ( nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ) ) , list ( nx . edges ( self . G , nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ) ) ) <EOL> assert_equal ( list ( self . DG . edges ( nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ) ) , list ( nx . edges ( self . DG , nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:3> ] ) ) ) <EOL> def test_degree ( self ) : <EOL> assert_equal ( list ( self . G . degree ( ) ) , list ( nx . degree ( self . G ) ) ) <EOL> assert_equal ( list ( self . DG . degree ( ) ) , list ( nx . degree ( self . DG ) ) ) <EOL> assert_equal ( list ( self . G . degree ( nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) , list ( nx . degree ( self . G , nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) ) <EOL> assert_equal ( list ( self . DG . degree ( nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) , list ( nx . degree ( self . DG , nbunch = [ <NUM_LIT:0> , <NUM_LIT:1> ] ) ) ) <EOL> assert_equal ( list ( self . G . degree ( weight = '<STR_LIT>' ) ) , list ( nx . degree ( self . G , weight = '<STR_LIT>' ) ) ) <EOL> assert_equal ( list ( self . DG . degree ( weight = '<STR_LIT>' ) ) , list ( nx . degree ( self . DG , weight = '<STR_LIT>' ) ) ) <EOL> def test_neighbors ( self ) : <EOL> assert_equal ( self . G . neighbors ( <NUM_LIT:1> ) , nx . neighbors ( self . G , <NUM_LIT:1> ) ) <EOL> assert_equal ( self . DG . neighbors ( <NUM_LIT:1> ) , nx . neighbors ( self . DG , <NUM_LIT:1> ) ) <EOL> def test_number_of_nodes ( self ) : <EOL> assert_equal ( self . G . number_of_nodes ( ) , nx . number_of_nodes ( self . G ) ) <EOL> assert_equal ( self . DG . number_of_nodes ( ) , nx . number_of_nodes ( self . DG ) ) <EOL> def test_number_of_edges ( self ) : <EOL> assert_equal ( self . G . number_of_edges ( ) , nx . number_of_edges ( self . G ) ) <EOL> assert_equal ( self . DG . number_of_edges ( ) , nx . number_of_edges ( self . DG ) ) <EOL> def test_is_directed ( self ) : <EOL> assert_equal ( self . G . is_directed ( ) , nx . is_directed ( self . G ) ) <EOL> assert_equal ( self . DG . is_directed ( ) , nx . is_directed ( self . DG ) ) <EOL> def test_add_star ( self ) : <EOL> G = self . G . copy ( ) <EOL> nlist = [ <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> ] <EOL> nx . add_star ( G , nlist ) <EOL> assert_equal ( sorted ( G . edges ( nlist ) ) , [ ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT:12> , <NUM_LIT:15> ) ] ) <EOL> G = self . G . copy ( ) <EOL> nx . add_star ( G , nlist , weight = <NUM_LIT> ) <EOL> assert_equal ( sorted ( G . edges ( nlist , data = True ) ) , <EOL> [ ( <NUM_LIT:12> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT:15> , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> def test_add_path ( self ) : <EOL> G = self . G . copy ( ) <EOL> nlist = [ <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> ] <EOL> nx . add_path ( G , nlist ) <EOL> assert_equal ( sorted ( G . edges ( nlist ) ) , [ ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:15> ) ] ) <EOL> G = self . G . copy ( ) <EOL> nx . add_path ( G , nlist , weight = <NUM_LIT> ) <EOL> assert_equal ( sorted ( G . edges ( nlist , data = True ) ) , <EOL> [ ( <NUM_LIT:12> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT:15> , { '<STR_LIT>' : <NUM_LIT> } ) ] ) <EOL> def test_add_cycle ( self ) : <EOL> G = self . G . copy ( ) <EOL> nlist = [ <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:15> ] <EOL> oklists = [ [ ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT:12> , <NUM_LIT:15> ) , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:15> ) ] , <EOL> [ ( <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT> ) , ( <NUM_LIT> , <NUM_LIT:15> ) , ( <NUM_LIT:15> , <NUM_LIT:12> ) ] ] <EOL> nx . add_cycle ( G , nlist ) <EOL> assert_true ( sorted ( G . edges ( nlist ) ) in oklists ) <EOL> G = self . G . copy ( ) <EOL> oklists = [ [ ( <NUM_LIT:12> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT:12> , <NUM_LIT:15> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT:15> , { '<STR_LIT>' : <NUM_LIT:1.> } ) ] , <EOL> [ ( <NUM_LIT:12> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT> , <NUM_LIT:15> , { '<STR_LIT>' : <NUM_LIT:1.> } ) , <EOL> ( <NUM_LIT:15> , <NUM_LIT:12> , { '<STR_LIT>' : <NUM_LIT:1.> } ) ] ] <EOL> nx . add_cycle ( G , nlist , weight = <NUM_LIT:1.0> ) <EOL> assert_true ( sorted ( G . edges ( nlist , data = True ) ) in oklists ) <EOL> def test_subgraph ( self ) : <EOL> assert_equal ( self . G . subgraph ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] ) . adj , nx . subgraph ( self . G , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] ) . adj ) <EOL> assert_equal ( self . DG . subgraph ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] ) . adj , nx . subgraph ( self . DG , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> ] ) . adj ) <EOL> def test_create_empty_copy ( self ) : <EOL> G = nx . create_empty_copy ( self . G , with_data = False ) <EOL> assert_equal ( list ( G ) , list ( self . G ) ) <EOL> assert_equal ( G . graph , { } ) <EOL> assert_equal ( G . node , { } . fromkeys ( self . G . nodes ( ) , { } ) ) <EOL> assert_equal ( G . edge , { } . fromkeys ( self . G . nodes ( ) , { } ) ) <EOL> G = nx . create_empty_copy ( self . G ) <EOL> assert_equal ( list ( G ) , list ( self . G ) ) <EOL> assert_equal ( G . graph , self . G . graph ) <EOL> assert_equal ( G . node , self . G . node ) <EOL> assert_equal ( G . edge , { } . fromkeys ( self . G . nodes ( ) , { } ) ) <EOL> def test_degree_histogram ( self ) : <EOL> assert_equal ( nx . degree_histogram ( self . G ) , [ <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> def test_density ( self ) : <EOL> assert_equal ( nx . density ( self . G ) , <NUM_LIT:0.5> ) <EOL> assert_equal ( nx . density ( self . DG ) , <NUM_LIT> ) <EOL> G = nx . Graph ( ) <EOL> G . add_node ( <NUM_LIT:1> ) <EOL> assert_equal ( nx . density ( G ) , <NUM_LIT:0.0> ) <EOL> def test_density_selfloop ( self ) : <EOL> G = nx . Graph ( ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:1> ) <EOL> assert_equal ( nx . density ( G ) , <NUM_LIT:0.0> ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert_equal ( nx . density ( G ) , <NUM_LIT> ) <EOL> def test_freeze ( self ) : <EOL> G = nx . freeze ( self . G ) <EOL> assert_equal ( G . frozen , True ) <EOL> assert_raises ( nx . NetworkXError , G . add_node , <NUM_LIT:1> ) <EOL> assert_raises ( nx . NetworkXError , G . add_nodes_from , [ <NUM_LIT:1> ] ) <EOL> assert_raises ( nx . NetworkXError , G . remove_node , <NUM_LIT:1> ) <EOL> assert_raises ( nx . NetworkXError , G . remove_nodes_from , [ <NUM_LIT:1> ] ) <EOL> assert_raises ( nx . NetworkXError , G . add_edge , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert_raises ( nx . NetworkXError , G . add_edges_from , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> assert_raises ( nx . NetworkXError , G . remove_edge , <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> assert_raises ( nx . NetworkXError , G . remove_edges_from , [ ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> assert_raises ( nx . NetworkXError , G . clear ) <EOL> def test_is_frozen ( self ) : <EOL> assert_equal ( nx . is_frozen ( self . G ) , False ) <EOL> G = nx . freeze ( self . G ) <EOL> assert_equal ( G . frozen , nx . is_frozen ( self . G ) ) <EOL> assert_equal ( G . frozen , True ) <EOL> def test_info ( self ) : <EOL> G = nx . path_graph ( <NUM_LIT:5> ) <EOL> info = nx . info ( G ) <EOL> expected_graph_info = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert_equal ( info , expected_graph_info ) <EOL> info = nx . info ( G , n = <NUM_LIT:1> ) <EOL> expected_node_info = '<STR_LIT:\n>' . join ( <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert_equal ( info , expected_node_info ) <EOL> def test_info_digraph ( self ) : <EOL> G = nx . DiGraph ( name = '<STR_LIT>' ) <EOL> nx . add_path ( G , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> info = nx . info ( G ) <EOL> expected_graph_info = '<STR_LIT:\n>' . join ( [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert_equal ( info , expected_graph_info ) <EOL> info = nx . info ( G , n = <NUM_LIT:1> ) <EOL> expected_node_info = '<STR_LIT:\n>' . join ( <EOL> [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert_equal ( info , expected_node_info ) <EOL> assert_raises ( nx . NetworkXError , nx . info , G , n = - <NUM_LIT:1> ) <EOL> def test_neighbors ( self ) : <EOL> graph = nx . complete_graph ( <NUM_LIT:100> ) <EOL> pop = random . sample ( list ( graph ) , <NUM_LIT:1> ) <EOL> nbors = list ( nx . neighbors ( graph , pop [ <NUM_LIT:0> ] ) ) <EOL> assert_equal ( len ( nbors ) , len ( graph ) - <NUM_LIT:1> ) <EOL> graph = nx . path_graph ( <NUM_LIT:100> ) <EOL> node = random . sample ( list ( graph ) , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> nbors = list ( nx . neighbors ( graph , node ) ) <EOL> if node != <NUM_LIT:0> and node != <NUM_LIT> : <EOL> assert_equal ( len ( nbors ) , <NUM_LIT:2> ) <EOL> else : <EOL> assert_equal ( len ( nbors ) , <NUM_LIT:1> ) <EOL> graph = nx . star_graph ( <NUM_LIT> ) <EOL> nbors = list ( nx . neighbors ( graph , <NUM_LIT:0> ) ) <EOL> assert_equal ( len ( nbors ) , <NUM_LIT> ) <EOL> def test_non_neighbors ( self ) : <EOL> graph = nx . complete_graph ( <NUM_LIT:100> ) <EOL> pop = random . sample ( list ( graph ) , <NUM_LIT:1> ) <EOL> nbors = list ( nx . non_neighbors ( graph , pop [ <NUM_LIT:0> ] ) ) <EOL> assert_equal ( len ( nbors ) , <NUM_LIT:0> ) <EOL> graph = nx . path_graph ( <NUM_LIT:100> ) <EOL> node = random . sample ( list ( graph ) , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> nbors = list ( nx . non_neighbors ( graph , node ) ) <EOL> if node != <NUM_LIT:0> and node != <NUM_LIT> : <EOL> assert_equal ( len ( nbors ) , <NUM_LIT> ) <EOL> else : <EOL> assert_equal ( len ( nbors ) , <NUM_LIT> ) <EOL> graph = nx . star_graph ( <NUM_LIT> ) <EOL> nbors = list ( nx . non_neighbors ( graph , <NUM_LIT:0> ) ) <EOL> assert_equal ( len ( nbors ) , <NUM_LIT:0> ) <EOL> graph = nx . Graph ( ) <EOL> graph . add_nodes_from ( range ( <NUM_LIT:10> ) ) <EOL> nbors = list ( nx . non_neighbors ( graph , <NUM_LIT:0> ) ) <EOL> assert_equal ( len ( nbors ) , <NUM_LIT:9> ) <EOL> def test_non_edges ( self ) : <EOL> graph = nx . complete_graph ( <NUM_LIT:5> ) <EOL> nedges = list ( nx . non_edges ( graph ) ) <EOL> assert_equal ( len ( nedges ) , <NUM_LIT:0> ) <EOL> graph = nx . path_graph ( <NUM_LIT:4> ) <EOL> expected = [ ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:0> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) ] <EOL> nedges = list ( nx . non_edges ( graph ) ) <EOL> for ( u , v ) in expected : <EOL> assert_true ( ( u , v ) in nedges or ( v , u ) in nedges ) <EOL> graph = nx . star_graph ( <NUM_LIT:4> ) <EOL> expected = [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:1> , <NUM_LIT:3> ) , ( <NUM_LIT:1> , <NUM_LIT:4> ) , ( <NUM_LIT:2> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) ] <EOL> nedges = list ( nx . non_edges ( graph ) ) <EOL> for ( u , v ) in expected : <EOL> assert_true ( ( u , v ) in nedges or ( v , u ) in nedges ) <EOL> graph = nx . DiGraph ( ) <EOL> graph . add_edges_from ( [ ( <NUM_LIT:0> , <NUM_LIT:2> ) , ( <NUM_LIT:2> , <NUM_LIT:0> ) , ( <NUM_LIT:2> , <NUM_LIT:1> ) ] ) <EOL> expected = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> nedges = list ( nx . non_edges ( graph ) ) <EOL> for e in expected : <EOL> assert_true ( e in nedges ) <EOL> def test_is_weighted ( self ) : <EOL> G = nx . Graph ( ) <EOL> assert_false ( nx . is_weighted ( G ) ) <EOL> G = nx . path_graph ( <NUM_LIT:4> ) <EOL> assert_false ( nx . is_weighted ( G ) ) <EOL> assert_false ( nx . is_weighted ( G , ( <NUM_LIT:2> , <NUM_LIT:3> ) ) ) <EOL> G . add_node ( <NUM_LIT:4> ) <EOL> G . add_edge ( <NUM_LIT:3> , <NUM_LIT:4> , weight = <NUM_LIT:4> ) <EOL> assert_false ( nx . is_weighted ( G ) ) <EOL> assert_true ( nx . is_weighted ( G , ( <NUM_LIT:3> , <NUM_LIT:4> ) ) ) <EOL> G = nx . DiGraph ( ) <EOL> G . add_weighted_edges_from ( [ ( '<STR_LIT:0>' , '<STR_LIT:3>' , <NUM_LIT:3> ) , ( '<STR_LIT:0>' , '<STR_LIT:1>' , - <NUM_LIT:5> ) , ( '<STR_LIT:1>' , '<STR_LIT:0>' , - <NUM_LIT:5> ) , <EOL> ( '<STR_LIT:0>' , '<STR_LIT:2>' , <NUM_LIT:2> ) , ( '<STR_LIT:1>' , '<STR_LIT:2>' , <NUM_LIT:4> ) , <EOL> ( '<STR_LIT:2>' , '<STR_LIT:3>' , <NUM_LIT:1> ) ] ) <EOL> assert_true ( nx . is_weighted ( G ) ) <EOL> assert_true ( nx . is_weighted ( G , ( '<STR_LIT:1>' , '<STR_LIT:0>' ) ) ) <EOL> G = G . to_undirected ( ) <EOL> assert_true ( nx . is_weighted ( G ) ) <EOL> assert_true ( nx . is_weighted ( G , ( '<STR_LIT:1>' , '<STR_LIT:0>' ) ) ) <EOL> assert_raises ( nx . NetworkXError , nx . is_weighted , G , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def test_is_negatively_weighted ( self ) : <EOL> G = nx . Graph ( ) <EOL> assert_false ( nx . is_negatively_weighted ( G ) ) <EOL> G . add_node ( <NUM_LIT:1> ) <EOL> G . add_nodes_from ( [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> assert_false ( nx . is_negatively_weighted ( G ) ) <EOL> G . add_edge ( <NUM_LIT:1> , <NUM_LIT:2> , weight = <NUM_LIT:4> ) <EOL> assert_false ( nx . is_negatively_weighted ( G , ( <NUM_LIT:1> , <NUM_LIT:2> ) ) ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:3> ) , ( <NUM_LIT:2> , <NUM_LIT:4> ) , ( <NUM_LIT:2> , <NUM_LIT:6> ) ] ) <EOL> G [ <NUM_LIT:1> ] [ <NUM_LIT:3> ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> assert_false ( nx . is_negatively_weighted ( G ) ) <EOL> assert_false ( nx . is_negatively_weighted ( G , ( <NUM_LIT:1> , <NUM_LIT:3> ) ) ) <EOL> G [ <NUM_LIT:2> ] [ <NUM_LIT:4> ] [ '<STR_LIT>' ] = - <NUM_LIT:2> <EOL> assert_true ( nx . is_negatively_weighted ( G , ( <NUM_LIT:2> , <NUM_LIT:4> ) ) ) <EOL> assert_true ( nx . is_negatively_weighted ( G ) ) <EOL> G = nx . DiGraph ( ) <EOL> G . add_weighted_edges_from ( [ ( '<STR_LIT:0>' , '<STR_LIT:3>' , <NUM_LIT:3> ) , ( '<STR_LIT:0>' , '<STR_LIT:1>' , - <NUM_LIT:5> ) , ( '<STR_LIT:1>' , '<STR_LIT:0>' , - <NUM_LIT:2> ) , <EOL> ( '<STR_LIT:0>' , '<STR_LIT:2>' , <NUM_LIT:2> ) , ( '<STR_LIT:1>' , '<STR_LIT:2>' , - <NUM_LIT:3> ) , ( '<STR_LIT:2>' , '<STR_LIT:3>' , <NUM_LIT:1> ) ] ) <EOL> assert_true ( nx . is_negatively_weighted ( G ) ) <EOL> assert_false ( nx . is_negatively_weighted ( G , ( '<STR_LIT:0>' , '<STR_LIT:3>' ) ) ) <EOL> assert_true ( nx . is_negatively_weighted ( G , ( '<STR_LIT:1>' , '<STR_LIT:0>' ) ) ) <EOL> assert_raises ( nx . NetworkXError , nx . is_negatively_weighted , G , ( <NUM_LIT:1> , <NUM_LIT:4> ) ) <EOL> class TestCommonNeighbors ( ) : <EOL> def setUp ( self ) : <EOL> self . func = nx . common_neighbors <EOL> def test_func ( G , u , v , expected ) : <EOL> result = sorted ( self . func ( G , u , v ) ) <EOL> assert_equal ( result , expected ) <EOL> self . test = test_func <EOL> def test_K5 ( self ) : <EOL> G = nx . complete_graph ( <NUM_LIT:5> ) <EOL> self . test ( G , <NUM_LIT:0> , <NUM_LIT:1> , [ <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> def test_P3 ( self ) : <EOL> G = nx . path_graph ( <NUM_LIT:3> ) <EOL> self . test ( G , <NUM_LIT:0> , <NUM_LIT:2> , [ <NUM_LIT:1> ] ) <EOL> def test_S4 ( self ) : <EOL> G = nx . star_graph ( <NUM_LIT:4> ) <EOL> self . test ( G , <NUM_LIT:1> , <NUM_LIT:2> , [ <NUM_LIT:0> ] ) <EOL> @ raises ( nx . NetworkXNotImplemented ) <EOL> def test_digraph ( self ) : <EOL> G = nx . DiGraph ( ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] ) <EOL> self . func ( G , <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> def test_nonexistent_nodes ( self ) : <EOL> G = nx . complete_graph ( <NUM_LIT:5> ) <EOL> assert_raises ( nx . NetworkXError , nx . common_neighbors , G , <NUM_LIT:5> , <NUM_LIT:4> ) <EOL> assert_raises ( nx . NetworkXError , nx . common_neighbors , G , <NUM_LIT:4> , <NUM_LIT:5> ) <EOL> assert_raises ( nx . NetworkXError , nx . common_neighbors , G , <NUM_LIT:5> , <NUM_LIT:6> ) <EOL> def test_custom1 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = nx . Graph ( ) <EOL> G . add_nodes_from ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> self . test ( G , <NUM_LIT:0> , <NUM_LIT:1> , [ ] ) <EOL> def test_custom2 ( self ) : <EOL> """<STR_LIT>""" <EOL> G = nx . complete_graph ( <NUM_LIT:4> ) <EOL> self . test ( G , <NUM_LIT:0> , <NUM_LIT:0> , [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> def test_set_node_attributes ( ) : <EOL> graphs = [ nx . Graph ( ) , nx . DiGraph ( ) , nx . MultiGraph ( ) , nx . MultiDiGraph ( ) ] <EOL> for G in graphs : <EOL> G = nx . path_graph ( <NUM_LIT:3> , create_using = G ) <EOL> attr = '<STR_LIT:hello>' <EOL> vals = <NUM_LIT:100> <EOL> nx . set_node_attributes ( G , attr , vals ) <EOL> assert_equal ( G . node [ <NUM_LIT:0> ] [ attr ] , vals ) <EOL> assert_equal ( G . node [ <NUM_LIT:1> ] [ attr ] , vals ) <EOL> assert_equal ( G . node [ <NUM_LIT:2> ] [ attr ] , vals ) <EOL> attr = '<STR_LIT>' <EOL> vals = dict ( zip ( sorted ( G . nodes ( ) ) , range ( len ( G ) ) ) ) <EOL> nx . set_node_attributes ( G , attr , vals ) <EOL> assert_equal ( G . node [ <NUM_LIT:0> ] [ attr ] , <NUM_LIT:0> ) <EOL> assert_equal ( G . node [ <NUM_LIT:1> ] [ attr ] , <NUM_LIT:1> ) <EOL> assert_equal ( G . node [ <NUM_LIT:2> ] [ attr ] , <NUM_LIT:2> ) <EOL> def test_set_edge_attributes ( ) : <EOL> graphs = [ nx . Graph ( ) , nx . DiGraph ( ) ] <EOL> for G in graphs : <EOL> G = nx . path_graph ( <NUM_LIT:3> , create_using = G ) <EOL> attr = '<STR_LIT:hello>' <EOL> vals = <NUM_LIT:3> <EOL> nx . set_edge_attributes ( G , attr , vals ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ attr ] , vals ) <EOL> assert_equal ( G [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ attr ] , vals ) <EOL> attr = '<STR_LIT>' <EOL> edges = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> vals = dict ( zip ( edges , range ( len ( edges ) ) ) ) <EOL> nx . set_edge_attributes ( G , attr , vals ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ attr ] , <NUM_LIT:0> ) <EOL> assert_equal ( G [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ attr ] , <NUM_LIT:1> ) <EOL> def test_set_edge_attributes_multi ( ) : <EOL> graphs = [ nx . MultiGraph ( ) , nx . MultiDiGraph ( ) ] <EOL> for G in graphs : <EOL> G = nx . path_graph ( <NUM_LIT:3> , create_using = G ) <EOL> attr = '<STR_LIT:hello>' <EOL> vals = <NUM_LIT:3> <EOL> nx . set_edge_attributes ( G , attr , vals ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ attr ] , vals ) <EOL> assert_equal ( G [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] [ attr ] , vals ) <EOL> attr = '<STR_LIT>' <EOL> edges = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ] <EOL> vals = dict ( zip ( edges , range ( len ( edges ) ) ) ) <EOL> nx . set_edge_attributes ( G , attr , vals ) <EOL> assert_equal ( G [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] [ attr ] , <NUM_LIT:0> ) <EOL> assert_equal ( G [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] [ <NUM_LIT:0> ] [ attr ] , <NUM_LIT:1> ) <EOL> def test_get_node_attributes ( ) : <EOL> graphs = [ nx . Graph ( ) , nx . DiGraph ( ) , nx . MultiGraph ( ) , nx . MultiDiGraph ( ) ] <EOL> for G in graphs : <EOL> G = nx . path_graph ( <NUM_LIT:3> , create_using = G ) <EOL> attr = '<STR_LIT:hello>' <EOL> vals = <NUM_LIT:100> <EOL> nx . set_node_attributes ( G , attr , vals ) <EOL> attrs = nx . get_node_attributes ( G , attr ) <EOL> assert_equal ( attrs [ <NUM_LIT:0> ] , vals ) <EOL> assert_equal ( attrs [ <NUM_LIT:1> ] , vals ) <EOL> assert_equal ( attrs [ <NUM_LIT:2> ] , vals ) <EOL> def test_get_edge_attributes ( ) : <EOL> graphs = [ nx . Graph ( ) , nx . DiGraph ( ) , nx . MultiGraph ( ) , nx . MultiDiGraph ( ) ] <EOL> for G in graphs : <EOL> G = nx . path_graph ( <NUM_LIT:3> , create_using = G ) <EOL> attr = '<STR_LIT:hello>' <EOL> vals = <NUM_LIT:100> <EOL> nx . set_edge_attributes ( G , attr , vals ) <EOL> attrs = nx . get_edge_attributes ( G , attr ) <EOL> assert_equal ( len ( attrs ) , <NUM_LIT:2> ) <EOL> if G . is_multigraph ( ) : <EOL> keys = [ ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ] <EOL> else : <EOL> keys = [ ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:2> ) ] <EOL> for key in keys : <EOL> assert_equal ( attrs [ key ] , <NUM_LIT:100> ) <EOL> def test_is_empty ( ) : <EOL> graphs = [ nx . Graph ( ) , nx . DiGraph ( ) , nx . MultiGraph ( ) , nx . MultiDiGraph ( ) ] <EOL> for G in graphs : <EOL> assert_true ( nx . is_empty ( G ) ) <EOL> G . add_nodes_from ( range ( <NUM_LIT:5> ) ) <EOL> assert_true ( nx . is_empty ( G ) ) <EOL> G . add_edges_from ( [ ( <NUM_LIT:1> , <NUM_LIT:2> ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) ] ) <EOL> assert_false ( nx . is_empty ( G ) ) </s>
