test_p2g.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import io
  2. import networkx as nx
  3. from networkx.readwrite.p2g import read_p2g, write_p2g
  4. from networkx.utils import edges_equal
  5. class TestP2G:
  6. @classmethod
  7. def setup_class(cls):
  8. cls.G = nx.Graph(name="test")
  9. e = [("a", "b"), ("b", "c"), ("c", "d"), ("d", "e"), ("e", "f"), ("a", "f")]
  10. cls.G.add_edges_from(e)
  11. cls.G.add_node("g")
  12. cls.DG = nx.DiGraph(cls.G)
  13. def test_read_p2g(self):
  14. s = b"""\
  15. name
  16. 3 4
  17. a
  18. 1 2
  19. b
  20. c
  21. 0 2
  22. """
  23. bytesIO = io.BytesIO(s)
  24. DG = read_p2g(bytesIO)
  25. assert DG.name == "name"
  26. assert sorted(DG) == ["a", "b", "c"]
  27. assert edges_equal(
  28. DG.edges(), [("a", "c"), ("a", "b"), ("c", "a"), ("c", "c")], directed=True
  29. )
  30. def test_write_p2g(self):
  31. s = b"""foo
  32. 3 2
  33. 1
  34. 1
  35. 2
  36. 2
  37. 3
  38. """
  39. fh = io.BytesIO()
  40. G = nx.DiGraph()
  41. G.name = "foo"
  42. G.add_edges_from([(1, 2), (2, 3)])
  43. write_p2g(G, fh)
  44. fh.seek(0)
  45. r = fh.read()
  46. assert r == s
  47. def test_write_read_p2g(self):
  48. fh = io.BytesIO()
  49. G = nx.DiGraph()
  50. G.name = "foo"
  51. G.add_edges_from([("a", "b"), ("b", "c")])
  52. write_p2g(G, fh)
  53. fh.seek(0)
  54. H = read_p2g(fh)
  55. assert edges_equal(G.edges(), H.edges(), directed=True)