Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (4.4 KB)

1
from __future__ import division
2

    
3
from io import BytesIO
4
import tempfile
5
from unittest import TestCase
6

    
7
from nose.tools import assert_equal
8

    
9
import networkx as nx
10
import networkx.readwrite.graph6 as g6
11
from networkx.testing.utils import assert_edges_equal
12
from networkx.testing.utils import assert_graphs_equal
13
from networkx.testing.utils import assert_nodes_equal
14

    
15

    
16
class TestGraph6Utils(object):
17

    
18
    def test_n_data_n_conversion(self):
19
        for i in [0, 1, 42, 62, 63, 64, 258047, 258048, 7744773, 68719476735]:
20
            assert_equal(g6.data_to_n(g6.n_to_data(i))[0], i)
21
            assert_equal(g6.data_to_n(g6.n_to_data(i))[1], [])
22
            assert_equal(g6.data_to_n(g6.n_to_data(i) + [42, 43])[1],
23
                         [42, 43])
24

    
25

    
26
class TestFromGraph6Bytes(TestCase):
27

    
28
    def test_from_graph6_bytes(self):
29
        data = b'DF{'
30
        G = nx.from_graph6_bytes(data)
31
        assert_nodes_equal(G.nodes(), [0, 1, 2, 3, 4])
32
        assert_edges_equal(G.edges(),
33
                           [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)])
34

    
35
    def test_read_equals_from_bytes(self):
36
        data = b'DF{'
37
        G = nx.from_graph6_bytes(data)
38
        fh = BytesIO(data)
39
        Gin = nx.read_graph6(fh)
40
        assert_nodes_equal(G.nodes(), Gin.nodes())
41
        assert_edges_equal(G.edges(), Gin.edges())
42

    
43

    
44
class TestReadGraph6(TestCase):
45

    
46
    def test_read_many_graph6(self):
47
        """Test for reading many graphs from a file into a list."""
48
        data = b'DF{\nD`{\nDqK\nD~{\n'
49
        fh = BytesIO(data)
50
        glist = nx.read_graph6(fh)
51
        assert_equal(len(glist), 4)
52
        for G in glist:
53
            assert_equal(sorted(G), list(range(5)))
54

    
55

    
56
class TestWriteGraph6(TestCase):
57
    """Unit tests for writing a graph to a file in graph6 format."""
58

    
59
    def test_null_graph(self):
60
        result = BytesIO()
61
        nx.write_graph6(nx.null_graph(), result)
62
        self.assertEqual(result.getvalue(), b'>>graph6<<?\n')
63

    
64
    def test_trivial_graph(self):
65
        result = BytesIO()
66
        nx.write_graph6(nx.trivial_graph(), result)
67
        self.assertEqual(result.getvalue(), b'>>graph6<<@\n')
68

    
69
    def test_complete_graph(self):
70
        result = BytesIO()
71
        nx.write_graph6(nx.complete_graph(4), result)
72
        self.assertEqual(result.getvalue(), b'>>graph6<<C~\n')
73

    
74
    def test_large_complete_graph(self):
75
        result = BytesIO()
76
        nx.write_graph6(nx.complete_graph(67), result, header=False)
77
        self.assertEqual(result.getvalue(), b'~?@B' + b'~' * 368 + b'w\n')
78

    
79
    def test_no_header(self):
80
        result = BytesIO()
81
        nx.write_graph6(nx.complete_graph(4), result, header=False)
82
        self.assertEqual(result.getvalue(), b'C~\n')
83

    
84
    def test_complete_bipartite_graph(self):
85
        result = BytesIO()
86
        G = nx.complete_bipartite_graph(6, 9)
87
        nx.write_graph6(G, result, header=False)
88
        # The expected encoding here was verified by Sage.
89
        self.assertEqual(result.getvalue(), b'N??F~z{~Fw^_~?~?^_?\n')
90

    
91
    def no_directed_graphs(self):
92
        with self.assertRaises(nx.NetworkXNotImplemented):
93
            nx.write_graph6(nx.DiGraph(), BytesIO())
94

    
95
    def test_length(self):
96
        for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
97
            g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
98
            gstr = BytesIO()
99
            nx.write_graph6(g, gstr, header=False)
100
            # Strip the trailing newline.
101
            gstr = gstr.getvalue().rstrip()
102
            assert_equal(len(gstr),
103
                         ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4))
104

    
105
    def test_roundtrip(self):
106
        for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
107
            G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
108
            f = BytesIO()
109
            nx.write_graph6(G, f)
110
            f.seek(0)
111
            H = nx.read_graph6(f)
112
            assert_nodes_equal(G.nodes(), H.nodes())
113
            assert_edges_equal(G.edges(), H.edges())
114

    
115
    def test_write_path(self):
116
        with tempfile.NamedTemporaryFile() as f:
117
            g6.write_graph6_file(nx.null_graph(), f)
118
            f.seek(0)
119
            self.assertEqual(f.read(), b'>>graph6<<?\n')
120

    
121
    def test_relabeling(self):
122
        G = nx.Graph([(0, 1)])
123
        assert_equal(g6.to_graph6_bytes(G), b'>>graph6<<A_\n')
124
        G = nx.Graph([(1, 2)])
125
        assert_equal(g6.to_graph6_bytes(G), b'>>graph6<<A_\n')
126
        G = nx.Graph([(1, 42)])
127
        assert_equal(g6.to_graph6_bytes(G), b'>>graph6<<A_\n')