diff options
author | cpurmessur <70621228+cpurmessur@users.noreply.github.com> | 2021-03-14 11:06:15 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-14 11:06:15 -0400 |
commit | 7e096606c4d985148e315c2a97cd25efc16ef007 (patch) | |
tree | b9a97e4170c5ce71b1f6da5ed5eafb25de6d84d8 | |
parent | e03144e0780898fed4afa976fff86f2710aa5d40 (diff) | |
download | networkx-7e096606c4d985148e315c2a97cd25efc16ef007.tar.gz |
Increase code coverage tournament (#4665)
* update tests for tournament
* updates to test_tournament
* add tests to tournament
* add tests to tournament
* update tests to tournament
* fix black
* fix black 2
* fix tests
* tweak to get github to notice a commit
* update tests
* update index satisfying
Co-authored-by: Dan Schult <dschult@colgate.edu>
-rw-r--r-- | networkx/algorithms/tests/test_tournament.py | 205 | ||||
-rw-r--r-- | networkx/algorithms/tournament.py | 1 |
2 files changed, 115 insertions, 91 deletions
diff --git a/networkx/algorithms/tests/test_tournament.py b/networkx/algorithms/tests/test_tournament.py index 8de6f7c3..42a31f9b 100644 --- a/networkx/algorithms/tests/test_tournament.py +++ b/networkx/algorithms/tests/test_tournament.py @@ -1,132 +1,157 @@ """Unit tests for the :mod:`networkx.algorithms.tournament` module.""" from itertools import combinations - - +import pytest from networkx import DiGraph from networkx.algorithms.tournament import is_reachable from networkx.algorithms.tournament import is_strongly_connected from networkx.algorithms.tournament import is_tournament from networkx.algorithms.tournament import random_tournament from networkx.algorithms.tournament import hamiltonian_path +from networkx.algorithms.tournament import score_sequence +from networkx.algorithms.tournament import tournament_matrix +from networkx.algorithms.tournament import index_satisfying + + +def test_condition_not_satisfied(): + condition = lambda x: x > 0 + iter_in = [0] + assert index_satisfying(iter_in, condition) == 1 + + +def test_empty_iterable(): + condition = lambda x: x > 0 + with pytest.raises(ValueError): + index_satisfying([], condition) -class TestIsTournament: - """Unit tests for the :func:`networkx.tournament.is_tournament` - function. +def test_is_tournament(): + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) + assert is_tournament(G) + + +def test_self_loops(): + """A tournament must have no self-loops.""" + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) + G.add_edge(0, 0) + assert not is_tournament(G) + + +def test_missing_edges(): + """A tournament must not have any pair of nodes without at least + one edge joining the pair. """ + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)]) + assert not is_tournament(G) + - def test_is_tournament(self): - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) +def test_bidirectional_edges(): + """A tournament must not have any pair of nodes with greater + than one edge joining the pair. + + """ + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) + G.add_edge(1, 0) + assert not is_tournament(G) + + +def test_graph_is_tournament(): + for _ in range(10): + G = random_tournament(5) assert is_tournament(G) - def test_self_loops(self): - """A tournament must have no self-loops.""" - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) - G.add_edge(0, 0) - assert not is_tournament(G) - def test_missing_edges(self): - """A tournament must not have any pair of nodes without at least - one edge joining the pair. +def test_graph_is_tournament_seed(): + for _ in range(10): + G = random_tournament(5, seed=1) + assert is_tournament(G) - """ - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3)]) - assert not is_tournament(G) - def test_bidirectional_edges(self): - """A tournament must not have any pair of nodes with greater - than one edge joining the pair. +def test_graph_is_tournament_one_node(): + G = random_tournament(1) + assert is_tournament(G) - """ - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) - G.add_edge(1, 0) - assert not is_tournament(G) +def test_graph_is_tournament_zero_node(): + G = random_tournament(0) + assert is_tournament(G) -class TestRandomTournament: - """Unit tests for the :func:`networkx.tournament.random_tournament` - function. - """ +def test_hamiltonian_empty_graph(): + path = hamiltonian_path(DiGraph()) + assert len(path) == 0 - def test_graph_is_tournament(self): - for n in range(10): - G = random_tournament(5) - assert is_tournament(G) - def test_graph_is_tournament_seed(self): - for n in range(10): - G = random_tournament(5, seed=1) - assert is_tournament(G) +def test_path_is_hamiltonian(): + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) + path = hamiltonian_path(G) + assert len(path) == 4 + assert all(v in G[u] for u, v in zip(path, path[1:])) -class TestHamiltonianPath: - """Unit tests for the :func:`networkx.tournament.hamiltonian_path` - function. +def test_hamiltonian_cycle(): + """Tests that :func:`networkx.tournament.hamiltonian_path` + returns a Hamiltonian cycle when provided a strongly connected + tournament. """ + G = DiGraph() + G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) + path = hamiltonian_path(G) + assert len(path) == 4 + assert all(v in G[u] for u, v in zip(path, path[1:])) + assert path[0] in G[path[-1]] - def test_path_is_hamiltonian(self): - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) - path = hamiltonian_path(G) - assert len(path) == 4 - assert all(v in G[u] for u, v in zip(path, path[1:])) - def test_hamiltonian_cycle(self): - """Tests that :func:`networkx.tournament.hamiltonian_path` - returns a Hamiltonian cycle when provided a strongly connected - tournament. +def test_score_sequence_edge(): + G = DiGraph([(0, 1)]) + assert score_sequence(G) == [0, 1] - """ - G = DiGraph() - G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)]) - path = hamiltonian_path(G) - assert len(path) == 4 - assert all(v in G[u] for u, v in zip(path, path[1:])) - assert path[0] in G[path[-1]] +def test_score_sequence_triangle(): + G = DiGraph([(0, 1), (1, 2), (2, 0)]) + assert score_sequence(G) == [1, 1, 1] -class TestReachability: - """Unit tests for the :func:`networkx.tournament.is_reachable` - function. - """ +def test_tournament_matrix(): + np = pytest.importorskip("numpy") + npt = pytest.importorskip("numpy.testing") + G = DiGraph([(0, 1)]) + m = tournament_matrix(G) + npt.assert_array_equal(m.todense(), np.array([[0, 1], [-1, 0]])) - def test_reachable_pair(self): - """Tests for a reachable pair of nodes.""" - G = DiGraph([(0, 1), (1, 2), (2, 0)]) - assert is_reachable(G, 0, 2) - def test_same_node_is_reachable(self): - """Tests that a node is always reachable from itself.""" - # G is an arbitrary tournament on ten nodes. - G = DiGraph(sorted(p) for p in combinations(range(10), 2)) - assert all(is_reachable(G, v, v) for v in G) +def test_reachable_pair(): + """Tests for a reachable pair of nodes.""" + G = DiGraph([(0, 1), (1, 2), (2, 0)]) + assert is_reachable(G, 0, 2) - def test_unreachable_pair(self): - """Tests for an unreachable pair of nodes.""" - G = DiGraph([(0, 1), (0, 2), (1, 2)]) - assert not is_reachable(G, 1, 0) +def test_same_node_is_reachable(): + """Tests that a node is always reachable from it.""" + # G is an arbitrary tournament on ten nodes. + G = DiGraph(sorted(p) for p in combinations(range(10), 2)) + assert all(is_reachable(G, v, v) for v in G) -class TestStronglyConnected: - """Unit tests for the - :func:`networkx.tournament.is_strongly_connected` function. - """ +def test_unreachable_pair(): + """Tests for an unreachable pair of nodes.""" + G = DiGraph([(0, 1), (0, 2), (1, 2)]) + assert not is_reachable(G, 1, 0) + + +def test_is_strongly_connected(): + """Tests for a strongly connected tournament.""" + G = DiGraph([(0, 1), (1, 2), (2, 0)]) + assert is_strongly_connected(G) - def test_is_strongly_connected(self): - """Tests for a strongly connected tournament.""" - G = DiGraph([(0, 1), (1, 2), (2, 0)]) - assert is_strongly_connected(G) - def test_not_strongly_connected(self): - """Tests for a tournament that is not strongly connected.""" - G = DiGraph([(0, 1), (0, 2), (1, 2)]) - assert not is_strongly_connected(G) +def test_not_strongly_connected(): + """Tests for a tournament that is not strongly connected.""" + G = DiGraph([(0, 1), (0, 2), (1, 2)]) + assert not is_strongly_connected(G) diff --git a/networkx/algorithms/tournament.py b/networkx/algorithms/tournament.py index 2205f555..cedd2bc0 100644 --- a/networkx/algorithms/tournament.py +++ b/networkx/algorithms/tournament.py @@ -276,7 +276,6 @@ def is_reachable(G, s, t): tournaments." *Electronic Colloquium on Computational Complexity*. 2001. <http://eccc.hpi-web.de/report/2001/092/> - """ def two_neighborhood(G, v): |