test_adjlist.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. """
  2. Unit tests for adjlist.
  3. """
  4. import io
  5. import os
  6. import tempfile
  7. import pytest
  8. import networkx as nx
  9. from networkx.utils import edges_equal, graphs_equal, nodes_equal
  10. class TestAdjlist:
  11. @classmethod
  12. def setup_class(cls):
  13. cls.G = nx.Graph(name="test")
  14. e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
  15. cls.G.add_edges_from(e)
  16. cls.G.add_node("g")
  17. cls.DG = nx.DiGraph(cls.G)
  18. cls.XG = nx.MultiGraph()
  19. cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
  20. cls.XDG = nx.MultiDiGraph(cls.XG)
  21. def test_read_multiline_adjlist_1(self):
  22. # Unit test for https://networkx.lanl.gov/trac/ticket/252
  23. s = b"""# comment line
  24. 1 2
  25. # comment line
  26. 2
  27. 3
  28. """
  29. bytesIO = io.BytesIO(s)
  30. G = nx.read_multiline_adjlist(bytesIO)
  31. adj = {"1": {"3": {}, "2": {}}, "3": {"1": {}}, "2": {"1": {}}}
  32. assert graphs_equal(G, nx.Graph(adj))
  33. def test_unicode(self):
  34. G = nx.Graph()
  35. name1 = chr(2344) + chr(123) + chr(6543)
  36. name2 = chr(5543) + chr(1543) + chr(324)
  37. G.add_edge(name1, "Radiohead", **{name2: 3})
  38. fd, fname = tempfile.mkstemp()
  39. nx.write_multiline_adjlist(G, fname)
  40. H = nx.read_multiline_adjlist(fname)
  41. assert graphs_equal(G, H)
  42. os.close(fd)
  43. os.unlink(fname)
  44. def test_latin1_err(self):
  45. G = nx.Graph()
  46. name1 = chr(2344) + chr(123) + chr(6543)
  47. name2 = chr(5543) + chr(1543) + chr(324)
  48. G.add_edge(name1, "Radiohead", **{name2: 3})
  49. fd, fname = tempfile.mkstemp()
  50. pytest.raises(
  51. UnicodeEncodeError, nx.write_multiline_adjlist, G, fname, encoding="latin-1"
  52. )
  53. os.close(fd)
  54. os.unlink(fname)
  55. def test_latin1(self):
  56. G = nx.Graph()
  57. name1 = "Bj" + chr(246) + "rk"
  58. name2 = chr(220) + "ber"
  59. G.add_edge(name1, "Radiohead", **{name2: 3})
  60. fd, fname = tempfile.mkstemp()
  61. nx.write_multiline_adjlist(G, fname, encoding="latin-1")
  62. H = nx.read_multiline_adjlist(fname, encoding="latin-1")
  63. assert graphs_equal(G, H)
  64. os.close(fd)
  65. os.unlink(fname)
  66. def test_parse_adjlist(self):
  67. lines = ["1 2 5", "2 3 4", "3 5", "4", "5"]
  68. nx.parse_adjlist(lines, nodetype=int) # smoke test
  69. with pytest.raises(TypeError):
  70. nx.parse_adjlist(lines, nodetype="int")
  71. lines = ["1 2 5", "2 b", "c"]
  72. with pytest.raises(TypeError):
  73. nx.parse_adjlist(lines, nodetype=int)
  74. def test_adjlist_graph(self):
  75. G = self.G
  76. (fd, fname) = tempfile.mkstemp()
  77. nx.write_adjlist(G, fname)
  78. H = nx.read_adjlist(fname)
  79. H2 = nx.read_adjlist(fname)
  80. assert H is not H2 # they should be different graphs
  81. assert nodes_equal(list(H), list(G))
  82. assert edges_equal(list(H.edges()), list(G.edges()))
  83. os.close(fd)
  84. os.unlink(fname)
  85. def test_adjlist_digraph(self):
  86. G = self.DG
  87. (fd, fname) = tempfile.mkstemp()
  88. nx.write_adjlist(G, fname)
  89. H = nx.read_adjlist(fname, create_using=nx.DiGraph())
  90. H2 = nx.read_adjlist(fname, create_using=nx.DiGraph())
  91. assert H is not H2 # they should be different graphs
  92. assert nodes_equal(list(H), list(G))
  93. assert edges_equal(list(H.edges()), list(G.edges()))
  94. os.close(fd)
  95. os.unlink(fname)
  96. def test_adjlist_integers(self):
  97. (fd, fname) = tempfile.mkstemp()
  98. G = nx.convert_node_labels_to_integers(self.G)
  99. nx.write_adjlist(G, fname)
  100. H = nx.read_adjlist(fname, nodetype=int)
  101. H2 = nx.read_adjlist(fname, nodetype=int)
  102. assert H is not H2 # they should be different graphs
  103. assert nodes_equal(list(H), list(G))
  104. assert edges_equal(list(H.edges()), list(G.edges()))
  105. os.close(fd)
  106. os.unlink(fname)
  107. def test_adjlist_multigraph(self):
  108. G = self.XG
  109. (fd, fname) = tempfile.mkstemp()
  110. nx.write_adjlist(G, fname)
  111. H = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
  112. H2 = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
  113. assert H is not H2 # they should be different graphs
  114. assert nodes_equal(list(H), list(G))
  115. assert edges_equal(list(H.edges()), list(G.edges()))
  116. os.close(fd)
  117. os.unlink(fname)
  118. def test_adjlist_multidigraph(self):
  119. G = self.XDG
  120. (fd, fname) = tempfile.mkstemp()
  121. nx.write_adjlist(G, fname)
  122. H = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiDiGraph())
  123. H2 = nx.read_adjlist(fname, nodetype=int, create_using=nx.MultiDiGraph())
  124. assert H is not H2 # they should be different graphs
  125. assert nodes_equal(list(H), list(G))
  126. assert edges_equal(list(H.edges()), list(G.edges()))
  127. os.close(fd)
  128. os.unlink(fname)
  129. def test_adjlist_delimiter(self):
  130. fh = io.BytesIO()
  131. G = nx.path_graph(3)
  132. nx.write_adjlist(G, fh, delimiter=":")
  133. fh.seek(0)
  134. H = nx.read_adjlist(fh, nodetype=int, delimiter=":")
  135. assert nodes_equal(list(H), list(G))
  136. assert edges_equal(list(H.edges()), list(G.edges()))
  137. class TestMultilineAdjlist:
  138. @classmethod
  139. def setup_class(cls):
  140. cls.G = nx.Graph(name="test")
  141. e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
  142. cls.G.add_edges_from(e)
  143. cls.G.add_node("g")
  144. cls.DG = nx.DiGraph(cls.G)
  145. cls.DG.remove_edge("b", "a")
  146. cls.DG.remove_edge("b", "c")
  147. cls.XG = nx.MultiGraph()
  148. cls.XG.add_weighted_edges_from([(1, 2, 5), (1, 2, 5), (1, 2, 1), (3, 3, 42)])
  149. cls.XDG = nx.MultiDiGraph(cls.XG)
  150. def test_parse_multiline_adjlist(self):
  151. lines = [
  152. "1 2",
  153. "b {'weight':3, 'name': 'Frodo'}",
  154. "c {}",
  155. "d 1",
  156. "e {'weight':6, 'name': 'Saruman'}",
  157. ]
  158. nx.parse_multiline_adjlist(iter(lines)) # smoke test
  159. with pytest.raises(TypeError):
  160. nx.parse_multiline_adjlist(iter(lines), nodetype=int)
  161. nx.parse_multiline_adjlist(iter(lines), edgetype=str) # smoke test
  162. with pytest.raises(TypeError):
  163. nx.parse_multiline_adjlist(iter(lines), nodetype=int)
  164. lines = ["1 a"]
  165. with pytest.raises(TypeError):
  166. nx.parse_multiline_adjlist(iter(lines))
  167. lines = ["a 2"]
  168. with pytest.raises(TypeError):
  169. nx.parse_multiline_adjlist(iter(lines), nodetype=int)
  170. lines = ["1 2"]
  171. with pytest.raises(TypeError):
  172. nx.parse_multiline_adjlist(iter(lines))
  173. lines = ["1 2", "2 {}"]
  174. with pytest.raises(TypeError):
  175. nx.parse_multiline_adjlist(iter(lines))
  176. def test_multiline_adjlist_graph(self):
  177. G = self.G
  178. (fd, fname) = tempfile.mkstemp()
  179. nx.write_multiline_adjlist(G, fname)
  180. H = nx.read_multiline_adjlist(fname)
  181. H2 = nx.read_multiline_adjlist(fname)
  182. assert H is not H2 # they should be different graphs
  183. assert nodes_equal(list(H), list(G))
  184. assert edges_equal(list(H.edges()), list(G.edges()))
  185. os.close(fd)
  186. os.unlink(fname)
  187. def test_multiline_adjlist_digraph(self):
  188. G = self.DG
  189. (fd, fname) = tempfile.mkstemp()
  190. nx.write_multiline_adjlist(G, fname)
  191. H = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
  192. H2 = nx.read_multiline_adjlist(fname, create_using=nx.DiGraph())
  193. assert H is not H2 # they should be different graphs
  194. assert nodes_equal(list(H), list(G))
  195. assert edges_equal(list(H.edges()), list(G.edges()))
  196. os.close(fd)
  197. os.unlink(fname)
  198. def test_multiline_adjlist_integers(self):
  199. (fd, fname) = tempfile.mkstemp()
  200. G = nx.convert_node_labels_to_integers(self.G)
  201. nx.write_multiline_adjlist(G, fname)
  202. H = nx.read_multiline_adjlist(fname, nodetype=int)
  203. H2 = nx.read_multiline_adjlist(fname, nodetype=int)
  204. assert H is not H2 # they should be different graphs
  205. assert nodes_equal(list(H), list(G))
  206. assert edges_equal(list(H.edges()), list(G.edges()))
  207. os.close(fd)
  208. os.unlink(fname)
  209. def test_multiline_adjlist_multigraph(self):
  210. G = self.XG
  211. (fd, fname) = tempfile.mkstemp()
  212. nx.write_multiline_adjlist(G, fname)
  213. H = nx.read_multiline_adjlist(fname, nodetype=int, create_using=nx.MultiGraph())
  214. H2 = nx.read_multiline_adjlist(
  215. fname, nodetype=int, create_using=nx.MultiGraph()
  216. )
  217. assert H is not H2 # they should be different graphs
  218. assert nodes_equal(list(H), list(G))
  219. assert edges_equal(list(H.edges()), list(G.edges()))
  220. os.close(fd)
  221. os.unlink(fname)
  222. def test_multiline_adjlist_multidigraph(self):
  223. G = self.XDG
  224. (fd, fname) = tempfile.mkstemp()
  225. nx.write_multiline_adjlist(G, fname)
  226. H = nx.read_multiline_adjlist(
  227. fname, nodetype=int, create_using=nx.MultiDiGraph()
  228. )
  229. H2 = nx.read_multiline_adjlist(
  230. fname, nodetype=int, create_using=nx.MultiDiGraph()
  231. )
  232. assert H is not H2 # they should be different graphs
  233. assert nodes_equal(list(H), list(G))
  234. assert edges_equal(list(H.edges()), list(G.edges()))
  235. os.close(fd)
  236. os.unlink(fname)
  237. def test_multiline_adjlist_delimiter(self):
  238. fh = io.BytesIO()
  239. G = nx.path_graph(3)
  240. nx.write_multiline_adjlist(G, fh, delimiter=":")
  241. fh.seek(0)
  242. H = nx.read_multiline_adjlist(fh, nodetype=int, delimiter=":")
  243. assert nodes_equal(list(H), list(G))
  244. assert edges_equal(list(H.edges()), list(G.edges()))