comparison env/lib/python3.9/site-packages/networkx/generators/tests/test_interval_graph.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.generators.interval_graph` module.
2
3 """
4 import math
5 import pytest
6
7 import networkx as nx
8 from networkx.generators.interval_graph import interval_graph
9 from networkx.testing import assert_edges_equal
10
11
12 class TestIntervalGraph:
13 """Unit tests for :func:`networkx.generators.interval_graph.interval_graph`"""
14
15 def test_empty(self):
16 """ Tests for trivial case of empty input"""
17 assert len(interval_graph([])) == 0
18
19 def test_interval_graph_check_invalid(self):
20 """ Tests for conditions that raise Exceptions """
21
22 invalids_having_none = [None, (1, 2)]
23 with pytest.raises(TypeError):
24 interval_graph(invalids_having_none)
25
26 invalids_having_set = [{1, 2}]
27 with pytest.raises(TypeError):
28 interval_graph(invalids_having_set)
29
30 invalids_having_seq_but_not_length2 = [(1, 2, 3)]
31 with pytest.raises(TypeError):
32 interval_graph(invalids_having_seq_but_not_length2)
33
34 invalids_interval = [[3, 2]]
35 with pytest.raises(ValueError):
36 interval_graph(invalids_interval)
37
38 def test_interval_graph_0(self):
39 intervals = [(1, 2), (1, 3)]
40
41 expected_graph = nx.Graph()
42 expected_graph.add_edge(*intervals)
43
44 actual_g = interval_graph(intervals)
45
46 assert set(actual_g.nodes) == set(expected_graph.nodes)
47 assert_edges_equal(expected_graph, actual_g)
48
49 def test_interval_graph_1(self):
50 intervals = [(1, 2), (2, 3), (3, 4), (1, 4)]
51
52 expected_graph = nx.Graph()
53 expected_graph.add_nodes_from(intervals)
54 e1 = ((1, 4), (1, 2))
55 e2 = ((1, 4), (2, 3))
56 e3 = ((1, 4), (3, 4))
57 e4 = ((3, 4), (2, 3))
58 e5 = ((1, 2), (2, 3))
59
60 expected_graph.add_edges_from([e1, e2, e3, e4, e5])
61
62 actual_g = interval_graph(intervals)
63
64 assert set(actual_g.nodes) == set(expected_graph.nodes)
65 assert_edges_equal(expected_graph, actual_g)
66
67 def test_interval_graph_2(self):
68 intervals = [(1, 2), [3, 5], [6, 8], (9, 10)]
69
70 expected_graph = nx.Graph()
71 expected_graph.add_nodes_from([(1, 2), (3, 5), (6, 8), (9, 10)])
72
73 actual_g = interval_graph(intervals)
74
75 assert set(actual_g.nodes) == set(expected_graph.nodes)
76 assert_edges_equal(expected_graph, actual_g)
77
78 def test_interval_graph_3(self):
79 intervals = [(1, 4), [3, 5], [2.5, 4]]
80
81 expected_graph = nx.Graph()
82 expected_graph.add_nodes_from([(1, 4), (3, 5), (2.5, 4)])
83 e1 = ((1, 4), (3, 5))
84 e2 = ((1, 4), (2.5, 4))
85 e3 = ((3, 5), (2.5, 4))
86
87 expected_graph.add_edges_from([e1, e2, e3])
88
89 actual_g = interval_graph(intervals)
90
91 assert set(actual_g.nodes) == set(expected_graph.nodes)
92 assert_edges_equal(expected_graph, actual_g)
93
94 def test_interval_graph_4(self):
95 """ test all possible overlaps """
96 intervals = [
97 (0, 2),
98 (-2, -1),
99 (-2, 0),
100 (-2, 1),
101 (-2, 2),
102 (-2, 3),
103 (0, 1),
104 (0, 2),
105 (0, 3),
106 (1, 2),
107 (1, 3),
108 (2, 3),
109 (3, 4),
110 ]
111
112 expected_graph = nx.Graph()
113 expected_graph.add_nodes_from(intervals)
114 expected_nbrs = {
115 (-2, 0),
116 (-2, 1),
117 (-2, 2),
118 (-2, 3),
119 (0, 1),
120 (0, 2),
121 (0, 3),
122 (1, 2),
123 (1, 3),
124 (2, 3),
125 }
126 actual_g = nx.interval_graph(intervals)
127 actual_nbrs = nx.neighbors(actual_g, (0, 2))
128
129 assert set(actual_nbrs) == expected_nbrs
130
131 def test_interval_graph_5(self):
132 """ this test is to see that an interval supports infinite number"""
133 intervals = {(-math.inf, 0), (-1, -1), (0.5, 0.5), (1, 1), (1, math.inf)}
134
135 expected_graph = nx.Graph()
136 expected_graph.add_nodes_from(intervals)
137 e1 = ((-math.inf, 0), (-1, -1))
138 e2 = ((1, 1), (1, math.inf))
139
140 expected_graph.add_edges_from([e1, e2])
141 actual_g = interval_graph(intervals)
142
143 assert set(actual_g.nodes) == set(expected_graph.nodes)
144 assert_edges_equal(expected_graph, actual_g)