### comparison env/lib/python3.9/site-packages/networkx/algorithms/tests/test_tournament.py @ 0:4f3585e2f14bdraftdefaulttip

author shellac Mon, 22 Mar 2021 18:12:50 +0000
comparison
equal inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Unit tests for the :mod:`networkx.algorithms.tournament` module."""
2 from itertools import combinations
3
4
5 from networkx import DiGraph
6 from networkx.algorithms.tournament import is_reachable
7 from networkx.algorithms.tournament import is_strongly_connected
8 from networkx.algorithms.tournament import is_tournament
9 from networkx.algorithms.tournament import random_tournament
10 from networkx.algorithms.tournament import hamiltonian_path
11
12
13 class TestIsTournament:
14 """Unit tests for the :func:`networkx.tournament.is_tournament`
15 function.
16
17 """
18
19 def test_is_tournament(self):
20 G = DiGraph()
21 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
22 assert is_tournament(G)
23
24 def test_self_loops(self):
25 """A tournament must have no self-loops."""
26 G = DiGraph()
27 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
29 assert not is_tournament(G)
30
31 def test_missing_edges(self):
32 """A tournament must not have any pair of nodes without at least
33 one edge joining the pair.
34
35 """
36 G = DiGraph()
37 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)])
38 assert not is_tournament(G)
39
40 def test_bidirectional_edges(self):
41 """A tournament must not have any pair of nodes with greater
42 than one edge joining the pair.
43
44 """
45 G = DiGraph()
46 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
48 assert not is_tournament(G)
49
50
51 class TestRandomTournament:
52 """Unit tests for the :func:`networkx.tournament.random_tournament`
53 function.
54
55 """
56
57 def test_graph_is_tournament(self):
58 for n in range(10):
59 G = random_tournament(5)
60 assert is_tournament(G)
61
62 def test_graph_is_tournament_seed(self):
63 for n in range(10):
64 G = random_tournament(5, seed=1)
65 assert is_tournament(G)
66
67
68 class TestHamiltonianPath:
69 """Unit tests for the :func:`networkx.tournament.hamiltonian_path`
70 function.
71
72 """
73
74 def test_path_is_hamiltonian(self):
75 G = DiGraph()
76 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
77 path = hamiltonian_path(G)
78 assert len(path) == 4
79 assert all(v in G[u] for u, v in zip(path, path[1:]))
80
81 def test_hamiltonian_cycle(self):
82 """Tests that :func:`networkx.tournament.hamiltonian_path`
83 returns a Hamiltonian cycle when provided a strongly connected
84 tournament.
85
86 """
87 G = DiGraph()
88 G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
89 path = hamiltonian_path(G)
90 assert len(path) == 4
91 assert all(v in G[u] for u, v in zip(path, path[1:]))
92 assert path in G[path[-1]]
93
94
95 class TestReachability:
96 """Unit tests for the :func:`networkx.tournament.is_reachable`
97 function.
98
99 """
100
101 def test_reachable_pair(self):
102 """Tests for a reachable pair of nodes."""
103 G = DiGraph([(0, 1), (1, 2), (2, 0)])
104 assert is_reachable(G, 0, 2)
105
106 def test_same_node_is_reachable(self):
107 """Tests that a node is always reachable from itself."""
108 # G is an arbitrary tournament on ten nodes.
109 G = DiGraph(sorted(p) for p in combinations(range(10), 2))
110 assert all(is_reachable(G, v, v) for v in G)
111
112 def test_unreachable_pair(self):
113 """Tests for an unreachable pair of nodes."""
114 G = DiGraph([(0, 1), (0, 2), (1, 2)])
115 assert not is_reachable(G, 1, 0)
116
117
118 class TestStronglyConnected:
119 """Unit tests for the
120 :func:`networkx.tournament.is_strongly_connected` function.
121
122 """
123
124 def test_is_strongly_connected(self):
125 """Tests for a strongly connected tournament."""
126 G = DiGraph([(0, 1), (1, 2), (2, 0)])
127 assert is_strongly_connected(G)
128
129 def test_not_strongly_connected(self):
130 """Tests for a tournament that is not strongly connected."""
131 G = DiGraph([(0, 1), (0, 2), (1, 2)])
132 assert not is_strongly_connected(G)