test_interval_graph.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Unit tests for the :mod:`networkx.generators.interval_graph` module.
  2. """
  3. import math
  4. import pytest
  5. import networkx as nx
  6. from networkx.generators.interval_graph import interval_graph
  7. from networkx.utils import edges_equal
  8. class TestIntervalGraph:
  9. """Unit tests for :func:`networkx.generators.interval_graph.interval_graph`"""
  10. def test_empty(self):
  11. """Tests for trivial case of empty input"""
  12. assert len(interval_graph([])) == 0
  13. def test_interval_graph_check_invalid(self):
  14. """Tests for conditions that raise Exceptions"""
  15. invalids_having_none = [None, (1, 2)]
  16. with pytest.raises(TypeError):
  17. interval_graph(invalids_having_none)
  18. invalids_having_set = [{1, 2}]
  19. with pytest.raises(TypeError):
  20. interval_graph(invalids_having_set)
  21. invalids_having_seq_but_not_length2 = [(1, 2, 3)]
  22. with pytest.raises(TypeError):
  23. interval_graph(invalids_having_seq_but_not_length2)
  24. invalids_interval = [[3, 2]]
  25. with pytest.raises(ValueError):
  26. interval_graph(invalids_interval)
  27. def test_interval_graph_0(self):
  28. intervals = [(1, 2), (1, 3)]
  29. expected_graph = nx.Graph()
  30. expected_graph.add_edge(*intervals)
  31. actual_g = interval_graph(intervals)
  32. assert set(actual_g.nodes) == set(expected_graph.nodes)
  33. assert edges_equal(expected_graph, actual_g)
  34. def test_interval_graph_1(self):
  35. intervals = [(1, 2), (2, 3), (3, 4), (1, 4)]
  36. expected_graph = nx.Graph()
  37. expected_graph.add_nodes_from(intervals)
  38. e1 = ((1, 4), (1, 2))
  39. e2 = ((1, 4), (2, 3))
  40. e3 = ((1, 4), (3, 4))
  41. e4 = ((3, 4), (2, 3))
  42. e5 = ((1, 2), (2, 3))
  43. expected_graph.add_edges_from([e1, e2, e3, e4, e5])
  44. actual_g = interval_graph(intervals)
  45. assert set(actual_g.nodes) == set(expected_graph.nodes)
  46. assert edges_equal(expected_graph, actual_g)
  47. def test_interval_graph_2(self):
  48. intervals = [(1, 2), [3, 5], [6, 8], (9, 10)]
  49. expected_graph = nx.Graph()
  50. expected_graph.add_nodes_from([(1, 2), (3, 5), (6, 8), (9, 10)])
  51. actual_g = interval_graph(intervals)
  52. assert set(actual_g.nodes) == set(expected_graph.nodes)
  53. assert edges_equal(expected_graph, actual_g)
  54. def test_interval_graph_3(self):
  55. intervals = [(1, 4), [3, 5], [2.5, 4]]
  56. expected_graph = nx.Graph()
  57. expected_graph.add_nodes_from([(1, 4), (3, 5), (2.5, 4)])
  58. e1 = ((1, 4), (3, 5))
  59. e2 = ((1, 4), (2.5, 4))
  60. e3 = ((3, 5), (2.5, 4))
  61. expected_graph.add_edges_from([e1, e2, e3])
  62. actual_g = interval_graph(intervals)
  63. assert set(actual_g.nodes) == set(expected_graph.nodes)
  64. assert edges_equal(expected_graph, actual_g)
  65. def test_interval_graph_4(self):
  66. """test all possible overlaps"""
  67. intervals = [
  68. (0, 2),
  69. (-2, -1),
  70. (-2, 0),
  71. (-2, 1),
  72. (-2, 2),
  73. (-2, 3),
  74. (0, 1),
  75. (0, 2),
  76. (0, 3),
  77. (1, 2),
  78. (1, 3),
  79. (2, 3),
  80. (3, 4),
  81. ]
  82. expected_graph = nx.Graph()
  83. expected_graph.add_nodes_from(intervals)
  84. expected_nbrs = {
  85. (-2, 0),
  86. (-2, 1),
  87. (-2, 2),
  88. (-2, 3),
  89. (0, 1),
  90. (0, 2),
  91. (0, 3),
  92. (1, 2),
  93. (1, 3),
  94. (2, 3),
  95. }
  96. actual_g = nx.interval_graph(intervals)
  97. actual_nbrs = nx.neighbors(actual_g, (0, 2))
  98. assert set(actual_nbrs) == expected_nbrs
  99. def test_interval_graph_5(self):
  100. """this test is to see that an interval supports infinite number"""
  101. intervals = {(-math.inf, 0), (-1, -1), (0.5, 0.5), (1, 1), (1, math.inf)}
  102. expected_graph = nx.Graph()
  103. expected_graph.add_nodes_from(intervals)
  104. e1 = ((-math.inf, 0), (-1, -1))
  105. e2 = ((1, 1), (1, math.inf))
  106. expected_graph.add_edges_from([e1, e2])
  107. actual_g = interval_graph(intervals)
  108. assert set(actual_g.nodes) == set(expected_graph.nodes)
  109. assert edges_equal(expected_graph, actual_g)