<s> """<STR_LIT>""" <EOL> import unittest <EOL> import pygraph <EOL> from pygraph . algorithms . generators import generate <EOL> from pygraph . classes . exceptions import AdditionError <EOL> from pygraph . classes . graph import graph <EOL> import testlib <EOL> from copy import copy , deepcopy <EOL> class test_graph ( unittest . TestCase ) : <EOL> def test_raise_exception_on_duplicate_node_addition ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( '<STR_LIT>' ) <EOL> try : <EOL> gr . add_node ( '<STR_LIT>' ) <EOL> except AdditionError : <EOL> pass <EOL> else : <EOL> fail ( ) <EOL> def test_raise_exception_on_duplicate_edge_addition ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( '<STR_LIT>' ) <EOL> gr . add_node ( '<STR_LIT>' ) <EOL> gr . add_edge ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> try : <EOL> gr . add_edge ( ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> except AdditionError : <EOL> pass <EOL> else : <EOL> fail ( ) <EOL> def test_raise_exception_when_edge_added_from_non_existing_node ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> try : <EOL> gr . add_edge ( ( <NUM_LIT:3> , <NUM_LIT:0> ) ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> fail ( ) <EOL> assert gr . node_neighbors == { <NUM_LIT:0> : [ ] , <NUM_LIT:1> : [ ] } <EOL> def test_raise_exception_when_edge_added_to_non_existing_node ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> try : <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:3> ) ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> fail ( ) <EOL> assert gr . node_neighbors == { <NUM_LIT:0> : [ ] , <NUM_LIT:1> : [ ] } <EOL> def test_remove_node ( self ) : <EOL> gr = testlib . new_graph ( ) <EOL> gr . del_node ( <NUM_LIT:0> ) <EOL> self . assertTrue ( <NUM_LIT:0> not in gr ) <EOL> for each , other in gr . edges ( ) : <EOL> self . assertTrue ( each in gr ) <EOL> self . assertTrue ( other in gr ) <EOL> def test_remove_edge_from_node_to_same_node ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( <NUM_LIT:0> ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> gr . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> def test_remove_node_with_edge_to_itself ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( <NUM_LIT:0> ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:0> ) ) <EOL> gr . del_node ( <NUM_LIT:0> ) <EOL> def test_edges_between_different_nodes_should_be_arrows_in_both_ways ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , label = "<STR_LIT:label>" , attrs = [ ( '<STR_LIT:key>' , '<STR_LIT:value>' ) ] ) <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:1> ) in gr . edges ( ) <EOL> assert ( <NUM_LIT:1> , <NUM_LIT:0> ) in gr . edges ( ) <EOL> assert len ( gr . edges ( ) ) == <NUM_LIT:2> <EOL> assert gr . neighbors ( <NUM_LIT:0> ) == [ <NUM_LIT:1> ] <EOL> assert gr . neighbors ( <NUM_LIT:1> ) == [ <NUM_LIT:0> ] <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:1> ) in gr . edge_properties . keys ( ) <EOL> assert ( <NUM_LIT:1> , <NUM_LIT:0> ) in gr . edge_properties . keys ( ) <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:1> ) in gr . edge_attr . keys ( ) <EOL> assert ( <NUM_LIT:1> , <NUM_LIT:0> ) in gr . edge_attr . keys ( ) <EOL> def test_edges_between_different_nodes_should_be_a_single_arrow ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( <NUM_LIT:0> ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:0> ) , label = "<STR_LIT:label>" , attrs = [ ( '<STR_LIT:key>' , '<STR_LIT:value>' ) ] ) <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:0> ) in gr . edges ( ) <EOL> assert len ( gr . edges ( ) ) == <NUM_LIT:1> <EOL> assert gr . neighbors ( <NUM_LIT:0> ) == [ <NUM_LIT:0> ] <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:0> ) in gr . edge_properties . keys ( ) <EOL> assert ( <NUM_LIT:0> , <NUM_LIT:0> ) in gr . edge_attr . keys ( ) <EOL> assert len ( gr . edge_attr [ ( <NUM_LIT:0> , <NUM_LIT:0> ) ] ) == <NUM_LIT:1> <EOL> def test_invert_graph ( self ) : <EOL> gr = testlib . new_graph ( ) <EOL> inv = gr . inverse ( ) <EOL> for each in gr . edges ( ) : <EOL> self . assertTrue ( each not in inv . edges ( ) ) <EOL> for each in inv . edges ( ) : <EOL> self . assertTrue ( each not in gr . edges ( ) ) <EOL> def test_invert_empty_graph ( self ) : <EOL> gr = graph ( ) <EOL> inv = gr . inverse ( ) <EOL> self . assertTrue ( gr . nodes ( ) == [ ] ) <EOL> self . assertTrue ( gr . edges ( ) == [ ] ) <EOL> def test_complete_graph ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_nodes ( range ( <NUM_LIT:10> ) ) <EOL> gr . complete ( ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> for j in range ( <NUM_LIT:10> ) : <EOL> self . assertTrue ( ( i , j ) in gr . edges ( ) or i == j ) <EOL> def test_complete_empty_graph ( self ) : <EOL> gr = graph ( ) <EOL> gr . complete ( ) <EOL> self . assertTrue ( gr . nodes ( ) == [ ] ) <EOL> self . assertTrue ( gr . edges ( ) == [ ] ) <EOL> def test_complete_graph_with_one_node ( self ) : <EOL> gr = graph ( ) <EOL> gr . add_node ( <NUM_LIT:0> ) <EOL> gr . complete ( ) <EOL> self . assertTrue ( gr . nodes ( ) == [ <NUM_LIT:0> ] ) <EOL> self . assertTrue ( gr . edges ( ) == [ ] ) <EOL> def test_add_graph ( self ) : <EOL> gr1 = testlib . new_graph ( ) <EOL> gr2 = testlib . new_graph ( ) <EOL> gr1 . add_graph ( gr2 ) <EOL> for each in gr2 . nodes ( ) : <EOL> self . assertTrue ( each in gr1 ) <EOL> for each in gr2 . edges ( ) : <EOL> self . assertTrue ( each in gr1 . edges ( ) ) <EOL> def test_add_empty_graph ( self ) : <EOL> gr1 = testlib . new_graph ( ) <EOL> gr1c = copy ( gr1 ) <EOL> gr2 = graph ( ) <EOL> gr1 . add_graph ( gr2 ) <EOL> self . assertTrue ( gr1 . nodes ( ) == gr1c . nodes ( ) ) <EOL> self . assertTrue ( gr1 . edges ( ) == gr1c . edges ( ) ) <EOL> def test_add_spanning_tree ( self ) : <EOL> gr = graph ( ) <EOL> st = { <NUM_LIT:0> : None , <NUM_LIT:1> : <NUM_LIT:0> , <NUM_LIT:2> : <NUM_LIT:0> , <NUM_LIT:3> : <NUM_LIT:1> , <NUM_LIT:4> : <NUM_LIT:2> , <NUM_LIT:5> : <NUM_LIT:3> } <EOL> gr . add_spanning_tree ( st ) <EOL> for each in st : <EOL> self . assertTrue ( ( each , st [ each ] ) in gr . edges ( ) or ( each , st [ each ] ) == ( <NUM_LIT:0> , None ) ) <EOL> self . assertTrue ( ( st [ each ] , each ) in gr . edges ( ) or ( each , st [ each ] ) == ( <NUM_LIT:0> , None ) ) <EOL> def test_add_empty_spanning_tree ( self ) : <EOL> gr = graph ( ) <EOL> st = { } <EOL> gr . add_spanning_tree ( st ) <EOL> self . assertTrue ( gr . nodes ( ) == [ ] ) <EOL> self . assertTrue ( gr . edges ( ) == [ ] ) <EOL> def test_repr ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = testlib . new_graph ( ) <EOL> gr_repr = repr ( gr ) <EOL> assert isinstance ( gr_repr , str ) <EOL> assert gr . __class__ . __name__ in gr_repr <EOL> def test_order_len_equivlance ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = testlib . new_graph ( ) <EOL> assert len ( gr ) == gr . order ( ) <EOL> assert gr . order ( ) == len ( gr . node_neighbors ) <EOL> def test_graph_equality_nodes ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] ) <EOL> gr2 = deepcopy ( gr ) <EOL> gr3 = deepcopy ( gr ) <EOL> gr3 . del_node ( <NUM_LIT:5> ) <EOL> gr4 = deepcopy ( gr ) <EOL> gr4 . add_node ( <NUM_LIT:6> ) <EOL> gr4 . del_node ( <NUM_LIT:0> ) <EOL> assert gr == gr2 <EOL> assert gr2 == gr <EOL> assert gr != gr3 <EOL> assert gr3 != gr <EOL> assert gr != gr4 <EOL> assert gr4 != gr <EOL> def test_graph_equality_edges ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , wt = <NUM_LIT:1> ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , wt = <NUM_LIT:2> ) <EOL> gr . add_edge ( ( <NUM_LIT:1> , <NUM_LIT:2> ) , wt = <NUM_LIT:3> ) <EOL> gr . add_edge ( ( <NUM_LIT:3> , <NUM_LIT:4> ) , wt = <NUM_LIT:4> ) <EOL> gr2 = deepcopy ( gr ) <EOL> gr3 = deepcopy ( gr ) <EOL> gr3 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> gr4 = deepcopy ( gr ) <EOL> gr4 . add_edge ( ( <NUM_LIT:2> , <NUM_LIT:4> ) ) <EOL> gr5 = deepcopy ( gr ) <EOL> gr5 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> gr5 . add_edge ( ( <NUM_LIT:2> , <NUM_LIT:4> ) ) <EOL> gr6 = deepcopy ( gr ) <EOL> gr6 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:2> ) ) <EOL> gr6 . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:2> ) , wt = <NUM_LIT:10> ) <EOL> assert gr == gr2 <EOL> assert gr2 == gr <EOL> assert gr != gr3 <EOL> assert gr3 != gr <EOL> assert gr != gr4 <EOL> assert gr4 != gr <EOL> assert gr != gr5 <EOL> assert gr5 != gr <EOL> assert gr != gr6 <EOL> assert gr6 != gr <EOL> def test_graph_equality_labels ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , label = "<STR_LIT>" ) <EOL> gr . add_edge ( ( <NUM_LIT:1> , <NUM_LIT:2> ) , label = "<STR_LIT>" ) <EOL> gr2 = deepcopy ( gr ) <EOL> gr3 = deepcopy ( gr ) <EOL> gr3 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr3 . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr4 = deepcopy ( gr ) <EOL> gr4 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr4 . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , label = "<STR_LIT>" ) <EOL> assert gr == gr2 <EOL> assert gr2 == gr <EOL> assert gr != gr3 <EOL> assert gr3 != gr <EOL> assert gr != gr4 <EOL> assert gr4 != gr <EOL> def test_graph_equality_attributes ( self ) : <EOL> """<STR_LIT>""" <EOL> gr = graph ( ) <EOL> gr . add_nodes ( [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:2> ] ) <EOL> gr . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr . add_node_attribute ( <NUM_LIT:1> , ( '<STR_LIT:a>' , '<STR_LIT:x>' ) ) <EOL> gr . add_node_attribute ( <NUM_LIT:2> , ( '<STR_LIT:b>' , '<STR_LIT:y>' ) ) <EOL> gr . add_edge_attribute ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( '<STR_LIT:c>' , '<STR_LIT:z>' ) ) <EOL> gr2 = deepcopy ( gr ) <EOL> gr3 = deepcopy ( gr ) <EOL> gr3 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr3 . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr4 = deepcopy ( gr ) <EOL> gr4 . del_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr4 . add_edge ( ( <NUM_LIT:0> , <NUM_LIT:1> ) ) <EOL> gr4 . add_edge_attribute ( ( <NUM_LIT:0> , <NUM_LIT:1> ) , ( '<STR_LIT:d>' , '<STR_LIT:k>' ) ) <EOL> gr5 = deepcopy ( gr ) <EOL> gr5 . del_node ( <NUM_LIT:2> ) <EOL> gr5 . add_node ( <NUM_LIT:2> ) <EOL> gr5 . add_node_attribute ( <NUM_LIT:0> , ( '<STR_LIT:d>' , '<STR_LIT:k>' ) ) <EOL> assert gr == gr2 <EOL> assert gr2 == gr <EOL> assert gr != gr3 <EOL> assert gr3 != gr <EOL> assert gr != gr4 <EOL> assert gr4 != gr <EOL> assert gr != gr5 <EOL> assert gr5 != gr <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
