from io import BytesIO import tempfile import pytest import networkx as nx import networkx.readwrite.graph6 as g6 from networkx.testing.utils import assert_edges_equal from networkx.testing.utils import assert_nodes_equal class TestGraph6Utils: def test_n_data_n_conversion(self): for i in [0, 1, 42, 62, 63, 64, 258047, 258048, 7744773, 68719476735]: assert g6.data_to_n(g6.n_to_data(i))[0] == i assert g6.data_to_n(g6.n_to_data(i))[1] == [] assert g6.data_to_n(g6.n_to_data(i) + [42, 43])[1] == [42, 43] class TestFromGraph6Bytes: def test_from_graph6_bytes(self): data = b"DF{" G = nx.from_graph6_bytes(data) assert_nodes_equal(G.nodes(), [0, 1, 2, 3, 4]) assert_edges_equal( G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)] ) def test_read_equals_from_bytes(self): data = b"DF{" G = nx.from_graph6_bytes(data) fh = BytesIO(data) Gin = nx.read_graph6(fh) assert_nodes_equal(G.nodes(), Gin.nodes()) assert_edges_equal(G.edges(), Gin.edges()) class TestReadGraph6: def test_read_many_graph6(self): """Test for reading many graphs from a file into a list.""" data = b"DF{\nD`{\nDqK\nD~{\n" fh = BytesIO(data) glist = nx.read_graph6(fh) assert len(glist) == 4 for G in glist: assert sorted(G) == list(range(5)) class TestWriteGraph6: """Unit tests for writing a graph to a file in graph6 format.""" def test_null_graph(self): result = BytesIO() nx.write_graph6(nx.null_graph(), result) assert result.getvalue() == b">>graph6<>graph6<<@\n" def test_complete_graph(self): result = BytesIO() nx.write_graph6(nx.complete_graph(4), result) assert result.getvalue() == b">>graph6<>graph6<>graph6<>graph6<>graph6<