Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.32 KB)

1
from io import BytesIO
2
import tempfile
3
from unittest import TestCase
4

    
5
from nose.tools import assert_equal
6
from nose.tools import assert_true
7

    
8
import networkx as nx
9
from networkx.testing.utils import assert_edges_equal
10
from networkx.testing.utils import assert_nodes_equal
11

    
12

    
13
class TestSparseGraph6(object):
14

    
15
    def test_from_sparse6_bytes(self):
16
        data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM'
17
        G = nx.from_sparse6_bytes(data)
18
        assert_nodes_equal(sorted(G.nodes()),
19
                           [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
20
                            10, 11, 12, 13, 14, 15, 16, 17])
21
        assert_edges_equal(G.edges(),
22
                           [(0, 1), (0, 2), (0, 3), (1, 12), (1, 14), (2, 13),
23
                            (2, 15), (3, 16), (3, 17), (4, 7), (4, 9), (4, 11),
24
                            (5, 6), (5, 8), (5, 9), (6, 10), (6, 11), (7, 8),
25
                            (7, 10), (8, 12), (9, 15), (10, 14), (11, 13),
26
                            (12, 16), (13, 17), (14, 17), (15, 16)])
27

    
28
    def test_from_bytes_multigraph_graph(self):
29
        graph_data = b':An'
30
        G = nx.from_sparse6_bytes(graph_data)
31
        assert_true(type(G), nx.Graph)
32
        multigraph_data = b':Ab'
33
        M = nx.from_sparse6_bytes(multigraph_data)
34
        assert_true(type(M), nx.MultiGraph)
35

    
36
    def test_read_sparse6(self):
37
        data = b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM'
38
        G = nx.from_sparse6_bytes(data)
39
        fh = BytesIO(data)
40
        Gin = nx.read_sparse6(fh)
41
        assert_nodes_equal(G.nodes(), Gin.nodes())
42
        assert_edges_equal(G.edges(), Gin.edges())
43

    
44
    def test_read_many_graph6(self):
45
        # Read many graphs into list
46
        data = (b':Q___eDcdFcDeFcE`GaJ`IaHbKNbLM\n'
47
                b':Q___dCfDEdcEgcbEGbFIaJ`JaHN`IM')
48
        fh = BytesIO(data)
49
        glist = nx.read_sparse6(fh)
50
        assert_equal(len(glist), 2)
51
        for G in glist:
52
            assert_nodes_equal(G.nodes(),
53
                               [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
54
                                10, 11, 12, 13, 14, 15, 16, 17])
55

    
56

    
57
class TestWriteSparse6(TestCase):
58
    """Unit tests for writing graphs in the sparse6 format.
59

60
    Most of the test cases were checked against the sparse6 encoder in Sage.
61

62
    """
63

    
64
    def test_null_graph(self):
65
        G = nx.null_graph()
66
        result = BytesIO()
67
        nx.write_sparse6(G, result)
68
        self.assertEqual(result.getvalue(), b'>>sparse6<<:?\n')
69

    
70
    def test_trivial_graph(self):
71
        G = nx.trivial_graph()
72
        result = BytesIO()
73
        nx.write_sparse6(G, result)
74
        self.assertEqual(result.getvalue(), b'>>sparse6<<:@\n')
75

    
76
    def test_empty_graph(self):
77
        G = nx.empty_graph(5)
78
        result = BytesIO()
79
        nx.write_sparse6(G, result)
80
        self.assertEqual(result.getvalue(), b'>>sparse6<<:D\n')
81

    
82
    def test_large_empty_graph(self):
83
        G = nx.empty_graph(68)
84
        result = BytesIO()
85
        nx.write_sparse6(G, result)
86
        self.assertEqual(result.getvalue(), b'>>sparse6<<:~?@C\n')
87

    
88
    def test_very_large_empty_graph(self):
89
        G = nx.empty_graph(258049)
90
        result = BytesIO()
91
        nx.write_sparse6(G, result)
92
        self.assertEqual(result.getvalue(), b'>>sparse6<<:~~???~?@\n')
93

    
94
    def test_complete_graph(self):
95
        G = nx.complete_graph(4)
96
        result = BytesIO()
97
        nx.write_sparse6(G, result)
98
        self.assertEqual(result.getvalue(), b'>>sparse6<<:CcKI\n')
99

    
100
    def test_no_header(self):
101
        G = nx.complete_graph(4)
102
        result = BytesIO()
103
        nx.write_sparse6(G, result, header=False)
104
        self.assertEqual(result.getvalue(), b':CcKI\n')
105

    
106
    def test_padding(self):
107
        codes = (b':Cdv', b':DaYn', b':EaYnN', b':FaYnL', b':GaYnLz')
108
        for n, code in enumerate(codes, start=4):
109
            G = nx.path_graph(n)
110
            result = BytesIO()
111
            nx.write_sparse6(G, result, header=False)
112
            self.assertEqual(result.getvalue(), code + b'\n')
113

    
114
    def test_complete_bipartite(self):
115
        G = nx.complete_bipartite_graph(6, 9)
116
        result = BytesIO()
117
        nx.write_sparse6(G, result)
118
        # Compared with sage
119
        expected = b'>>sparse6<<:Nk' + b'?G`cJ' * 9 + b'\n'
120
        assert_equal(result.getvalue(), expected)
121

    
122
    def test_read_write_inverse(self):
123
        for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
124
            m = min(2 * i, i * i // 2)
125
            g = nx.random_graphs.gnm_random_graph(i, m, seed=i)
126
            gstr = BytesIO()
127
            nx.write_sparse6(g, gstr, header=False)
128
            # Strip the trailing newline.
129
            gstr = gstr.getvalue().rstrip()
130
            g2 = nx.from_sparse6_bytes(gstr)
131
            assert_equal(g2.order(), g.order())
132
            assert_edges_equal(g2.edges(), g.edges())
133

    
134
    def no_directed_graphs(self):
135
        with self.assertRaises(nx.NetworkXNotImplemented):
136
            nx.write_sparse6(nx.DiGraph(), BytesIO())
137

    
138
    def test_write_path(self):
139
        # On Windows, we can't reopen a file that is open
140
        # So, for test we get a valid name from tempfile but close it.
141
        with tempfile.NamedTemporaryFile() as f:
142
            fullfilename = f.name
143
        # file should be closed now, so write_sparse6 can open it
144
        nx.write_sparse6(nx.null_graph(), fullfilename)
145
        fh = open(fullfilename, mode='rb')
146
        self.assertEqual(fh.read(), b'>>sparse6<<:?\n')
147
        fh.close()
148
        import os
149
        os.remove(fullfilename)