Statistics
| Branch: | Revision:

iof-tools / networkxMiCe / networkx-master / networkx / readwrite / tests / test_leda.py @ 5cef0f13

History | View | Annotate | Download (1.52 KB)

1
#!/usr/bin/env python
2
from nose.tools import *
3
import networkx as nx
4
import io
5
import os
6
import tempfile
7

    
8

    
9
class TestLEDA(object):
10

    
11
    def test_parse_leda(self):
12
        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}|"""
13
        G = nx.parse_leda(data)
14
        G = nx.parse_leda(data.split('\n'))
15
        assert_equal(sorted(G.nodes()),
16
                     ['v1', 'v2', 'v3', 'v4', 'v5'])
17
        assert_equal(sorted(G.edges(data=True)),
18
                     [('v1', 'v2', {'label': '4'}),
19
                      ('v1', 'v3', {'label': '3'}),
20
                      ('v2', 'v3', {'label': '2'}),
21
                      ('v3', 'v4', {'label': '3'}),
22
                      ('v3', 'v5', {'label': '7'}),
23
                      ('v4', 'v5', {'label': '6'}),
24
                      ('v5', 'v1', {'label': 'foo'})])
25

    
26
    def test_read_LEDA(self):
27
        fh = io.BytesIO()
28
        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}|"""
29
        G = nx.parse_leda(data)
30
        fh.write(data.encode('UTF-8'))
31
        fh.seek(0)
32
        Gin = nx.read_leda(fh)
33
        assert_equal(sorted(G.nodes()), sorted(Gin.nodes()))
34
        assert_equal(sorted(G.edges()), sorted(Gin.edges()))