1 #!/usr/bin/env python 2 from nose.tools import * 3 import networkx as nx 4 import os,tempfile 5 6 class TestLEDA(object): 7 8 def test_parse_leda(self): 9 data="""#header section \nLEDA.GRAPH \nstring\nint\n-1\n#nodes section\n5 \n|{v1}| \n|{v2}| \n|{v3}| \n|{v4}| \n|{v5}| \n\n#edges section\n7 \n1 2 0 |{4}| \n1 3 0 |{3}| \n2 3 0 |{2}| \n3 4 0 |{3}| \n3 5 0 |{7}| \n4 5 0 |{6}| \n5 1 0 |{foo}|""" 10 G=nx.parse_leda(data) 11 G=nx.parse_leda(data.split('\n')) 12 assert_equal(sorted(G.nodes()), 13 ['v1', 'v2', 'v3', 'v4', 'v5']) 14 assert_equal([e for e in sorted(G.edges(data=True))], 15 [('v1', 'v2', {'label': '4'}), 16 ('v1', 'v3', {'label': '3'}), 17 ('v2', 'v3', {'label': '2'}), 18 ('v3', 'v4', {'label': '3'}), 19 ('v3', 'v5', {'label': '7'}), 20 ('v4', 'v5', {'label': '6'}), 21 ('v5', 'v1', {'label': 'foo'})]) 22 23 24 def test_read_LEDA(self): 25 data="""#header section \nLEDA.GRAPH \nstring\nint\n-1\n#nodes section\n5 \n|{v1}| \n|{v2}| \n|{v3}| \n|{v4}| \n|{v5}| \n\n#edges section\n7 \n1 2 0 |{4}| \n1 3 0 |{3}| \n2 3 0 |{2}| \n3 4 0 |{3}| \n3 5 0 |{7}| \n4 5 0 |{6}| \n5 1 0 |{foo}|""" 26 G=nx.parse_leda(data) 27 (fd,fname)=tempfile.mkstemp() 28 fh=open(fname,'w') 29 b=fh.write(data) 30 fh.close() 31 Gin=nx.read_leda(fname) 32 assert_equal(sorted(G.nodes()),sorted(Gin.nodes())) 33 assert_equal(sorted(G.edges()),sorted(Gin.edges())) 34 os.close(fd) 35 os.unlink(fname) 36