test_graph6.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import tempfile
  2. from io import BytesIO
  3. import pytest
  4. import networkx as nx
  5. import networkx.readwrite.graph6 as g6
  6. from networkx.utils import edges_equal, nodes_equal
  7. class TestGraph6Utils:
  8. def test_n_data_n_conversion(self):
  9. for i in [0, 1, 42, 62, 63, 64, 258047, 258048, 7744773, 68719476735]:
  10. assert g6.data_to_n(g6.n_to_data(i))[0] == i
  11. assert g6.data_to_n(g6.n_to_data(i))[1] == []
  12. assert g6.data_to_n(g6.n_to_data(i) + [42, 43])[1] == [42, 43]
  13. class TestFromGraph6Bytes:
  14. def test_from_graph6_bytes(self):
  15. data = b"DF{"
  16. G = nx.from_graph6_bytes(data)
  17. assert nodes_equal(G.nodes(), [0, 1, 2, 3, 4])
  18. assert edges_equal(
  19. G.edges(), [(0, 3), (0, 4), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
  20. )
  21. def test_read_equals_from_bytes(self):
  22. data = b"DF{"
  23. G = nx.from_graph6_bytes(data)
  24. fh = BytesIO(data)
  25. Gin = nx.read_graph6(fh)
  26. assert nodes_equal(G.nodes(), Gin.nodes())
  27. assert edges_equal(G.edges(), Gin.edges())
  28. class TestReadGraph6:
  29. def test_read_many_graph6(self):
  30. """Test for reading many graphs from a file into a list."""
  31. data = b"DF{\nD`{\nDqK\nD~{\n"
  32. fh = BytesIO(data)
  33. glist = nx.read_graph6(fh)
  34. assert len(glist) == 4
  35. for G in glist:
  36. assert sorted(G) == list(range(5))
  37. class TestWriteGraph6:
  38. """Unit tests for writing a graph to a file in graph6 format."""
  39. def test_null_graph(self):
  40. result = BytesIO()
  41. nx.write_graph6(nx.null_graph(), result)
  42. assert result.getvalue() == b">>graph6<<?\n"
  43. def test_trivial_graph(self):
  44. result = BytesIO()
  45. nx.write_graph6(nx.trivial_graph(), result)
  46. assert result.getvalue() == b">>graph6<<@\n"
  47. def test_complete_graph(self):
  48. result = BytesIO()
  49. nx.write_graph6(nx.complete_graph(4), result)
  50. assert result.getvalue() == b">>graph6<<C~\n"
  51. def test_large_complete_graph(self):
  52. result = BytesIO()
  53. nx.write_graph6(nx.complete_graph(67), result, header=False)
  54. assert result.getvalue() == b"~?@B" + b"~" * 368 + b"w\n"
  55. def test_no_header(self):
  56. result = BytesIO()
  57. nx.write_graph6(nx.complete_graph(4), result, header=False)
  58. assert result.getvalue() == b"C~\n"
  59. def test_complete_bipartite_graph(self):
  60. result = BytesIO()
  61. G = nx.complete_bipartite_graph(6, 9)
  62. nx.write_graph6(G, result, header=False)
  63. # The expected encoding here was verified by Sage.
  64. assert result.getvalue() == b"N??F~z{~Fw^_~?~?^_?\n"
  65. @pytest.mark.parametrize("G", (nx.MultiGraph(), nx.DiGraph()))
  66. def test_no_directed_or_multi_graphs(self, G):
  67. with pytest.raises(nx.NetworkXNotImplemented):
  68. nx.write_graph6(G, BytesIO())
  69. def test_length(self):
  70. for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
  71. g = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
  72. gstr = BytesIO()
  73. nx.write_graph6(g, gstr, header=False)
  74. # Strip the trailing newline.
  75. gstr = gstr.getvalue().rstrip()
  76. assert len(gstr) == ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4)
  77. def test_roundtrip(self):
  78. for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
  79. G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
  80. f = BytesIO()
  81. nx.write_graph6(G, f)
  82. f.seek(0)
  83. H = nx.read_graph6(f)
  84. assert nodes_equal(G.nodes(), H.nodes())
  85. assert edges_equal(G.edges(), H.edges())
  86. def test_write_path(self):
  87. with tempfile.NamedTemporaryFile() as f:
  88. g6.write_graph6_file(nx.null_graph(), f)
  89. f.seek(0)
  90. assert f.read() == b">>graph6<<?\n"
  91. @pytest.mark.parametrize("edge", ((0, 1), (1, 2), (1, 42)))
  92. def test_relabeling(self, edge):
  93. G = nx.Graph([edge])
  94. f = BytesIO()
  95. nx.write_graph6(G, f)
  96. f.seek(0)
  97. assert f.read() == b">>graph6<<A_\n"
  98. class TestToGraph6Bytes:
  99. def test_null_graph(self):
  100. G = nx.null_graph()
  101. assert g6.to_graph6_bytes(G) == b">>graph6<<?\n"
  102. def test_trivial_graph(self):
  103. G = nx.trivial_graph()
  104. assert g6.to_graph6_bytes(G) == b">>graph6<<@\n"
  105. def test_complete_graph(self):
  106. assert g6.to_graph6_bytes(nx.complete_graph(4)) == b">>graph6<<C~\n"
  107. def test_large_complete_graph(self):
  108. G = nx.complete_graph(67)
  109. assert g6.to_graph6_bytes(G, header=False) == b"~?@B" + b"~" * 368 + b"w\n"
  110. def test_no_header(self):
  111. G = nx.complete_graph(4)
  112. assert g6.to_graph6_bytes(G, header=False) == b"C~\n"
  113. def test_complete_bipartite_graph(self):
  114. G = nx.complete_bipartite_graph(6, 9)
  115. assert g6.to_graph6_bytes(G, header=False) == b"N??F~z{~Fw^_~?~?^_?\n"
  116. @pytest.mark.parametrize("G", (nx.MultiGraph(), nx.DiGraph()))
  117. def test_no_directed_or_multi_graphs(self, G):
  118. with pytest.raises(nx.NetworkXNotImplemented):
  119. g6.to_graph6_bytes(G)
  120. def test_length(self):
  121. for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
  122. G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
  123. # Strip the trailing newline.
  124. gstr = g6.to_graph6_bytes(G, header=False).rstrip()
  125. assert len(gstr) == ((i - 1) * i // 2 + 5) // 6 + (1 if i < 63 else 4)
  126. def test_roundtrip(self):
  127. for i in list(range(13)) + [31, 47, 62, 63, 64, 72]:
  128. G = nx.random_graphs.gnm_random_graph(i, i * i // 4, seed=i)
  129. data = g6.to_graph6_bytes(G)
  130. H = nx.from_graph6_bytes(data.rstrip())
  131. assert nodes_equal(G.nodes(), H.nodes())
  132. assert edges_equal(G.edges(), H.edges())
  133. @pytest.mark.parametrize("edge", ((0, 1), (1, 2), (1, 42)))
  134. def test_relabeling(self, edge):
  135. G = nx.Graph([edge])
  136. assert g6.to_graph6_bytes(G) == b">>graph6<<A_\n"