From 56c5515c98d1d2e01b62e97e8d9718bd447011e5 Mon Sep 17 00:00:00 2001 From: Ross Barnowski Date: Tue, 11 Oct 2022 09:50:31 -0700 Subject: Minor updates to expanders generator tests (#6027) * Split MGG test into two based on dependencies. * Parametrize tests on prime numbers. * Use fns from nx namespace, rm explicit imports. * Parametrize exception test and check message. --- networkx/generators/tests/test_expanders.py | 93 ++++++++++++++--------------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/networkx/generators/tests/test_expanders.py b/networkx/generators/tests/test_expanders.py index 822eeee0..58f6b1b8 100644 --- a/networkx/generators/tests/test_expanders.py +++ b/networkx/generators/tests/test_expanders.py @@ -5,69 +5,68 @@ import pytest import networkx as nx -from networkx import adjacency_matrix, number_of_nodes -from networkx.generators.expanders import ( - chordal_cycle_graph, - margulis_gabber_galil_graph, - paley_graph, -) -def test_margulis_gabber_galil_graph(): - for n in 2, 3, 5, 6, 10: - g = margulis_gabber_galil_graph(n) - assert number_of_nodes(g) == n * n - for node in g: - assert g.degree(node) == 8 - assert len(node) == 2 - for i in node: - assert int(i) == i - assert 0 <= i < n +@pytest.mark.parametrize("n", (2, 3, 5, 6, 10)) +def test_margulis_gabber_galil_graph_properties(n): + g = nx.margulis_gabber_galil_graph(n) + assert g.number_of_nodes() == n * n + for node in g: + assert g.degree(node) == 8 + assert len(node) == 2 + for i in node: + assert int(i) == i + assert 0 <= i < n + +@pytest.mark.parametrize("n", (2, 3, 5, 6, 10)) +def test_margulis_gabber_galil_graph_eigvals(n): np = pytest.importorskip("numpy") sp = pytest.importorskip("scipy") import scipy.linalg + g = nx.margulis_gabber_galil_graph(n) # Eigenvalues are already sorted using the scipy eigvalsh, # but the implementation in numpy does not guarantee order. - w = sorted(sp.linalg.eigvalsh(adjacency_matrix(g).toarray())) + w = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(g).toarray())) assert w[-2] < 5 * np.sqrt(2) -def test_chordal_cycle_graph(): +@pytest.mark.parametrize("p", (3, 5, 7, 11)) # Primes +def test_chordal_cycle_graph(p): """Test for the :func:`networkx.chordal_cycle_graph` function.""" - primes = [3, 5, 7, 11] - for p in primes: - G = chordal_cycle_graph(p) - assert len(G) == p - # TODO The second largest eigenvalue should be smaller than a constant, - # independent of the number of nodes in the graph: - # - # eigs = sorted(sp.linalg.eigvalsh(adjacency_matrix(G).A)) - # assert_less(eigs[-2], ...) - # + G = nx.chordal_cycle_graph(p) + assert len(G) == p + # TODO The second largest eigenvalue should be smaller than a constant, + # independent of the number of nodes in the graph: + # + # eigs = sorted(sp.linalg.eigvalsh(nx.adjacency_matrix(G).toarray())) + # assert_less(eigs[-2], ...) + # -def test_paley_graph(): +@pytest.mark.parametrize("p", (3, 5, 7, 11, 13)) # Primes +def test_paley_graph(p): """Test for the :func:`networkx.paley_graph` function.""" - primes = [3, 5, 7, 11, 13] - for p in primes: - G = paley_graph(p) - # G has p nodes - assert len(G) == p - # G is (p-1)/2-regular - in_degrees = {G.in_degree(node) for node in G.nodes} - out_degrees = {G.out_degree(node) for node in G.nodes} - assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2 - assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2 + G = nx.paley_graph(p) + # G has p nodes + assert len(G) == p + # G is (p-1)/2-regular + in_degrees = {G.in_degree(node) for node in G.nodes} + out_degrees = {G.out_degree(node) for node in G.nodes} + assert len(in_degrees) == 1 and in_degrees.pop() == (p - 1) // 2 + assert len(out_degrees) == 1 and out_degrees.pop() == (p - 1) // 2 - # If p = 1 mod 4, -1 is a square mod 4 and therefore the - # edge in the Paley graph are symmetric. - if p % 4 == 1: - for (u, v) in G.edges: - assert (v, u) in G.edges + # If p = 1 mod 4, -1 is a square mod 4 and therefore the + # edge in the Paley graph are symmetric. + if p % 4 == 1: + for (u, v) in G.edges: + assert (v, u) in G.edges -def test_margulis_gabber_galil_graph_badinput(): - pytest.raises(nx.NetworkXError, margulis_gabber_galil_graph, 3, nx.DiGraph()) - pytest.raises(nx.NetworkXError, margulis_gabber_galil_graph, 3, nx.Graph()) +@pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph)) +def test_margulis_gabber_galil_graph_badinput(graph_type): + with pytest.raises( + nx.NetworkXError, match="`create_using` must be an undirected multigraph" + ): + nx.margulis_gabber_galil_graph(3, create_using=graph_type) -- cgit v1.2.1