test_atlas.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from itertools import groupby
  2. import pytest
  3. import networkx as nx
  4. from networkx import graph_atlas, graph_atlas_g
  5. from networkx.generators.atlas import NUM_GRAPHS
  6. from networkx.utils import edges_equal, nodes_equal, pairwise
  7. class TestAtlasGraph:
  8. """Unit tests for the :func:`~networkx.graph_atlas` function."""
  9. def test_index_too_small(self):
  10. with pytest.raises(ValueError):
  11. graph_atlas(-1)
  12. def test_index_too_large(self):
  13. with pytest.raises(ValueError):
  14. graph_atlas(NUM_GRAPHS)
  15. def test_graph(self):
  16. G = graph_atlas(6)
  17. assert nodes_equal(G.nodes(), range(3))
  18. assert edges_equal(G.edges(), [(0, 1), (0, 2)])
  19. class TestAtlasGraphG:
  20. """Unit tests for the :func:`~networkx.graph_atlas_g` function."""
  21. @classmethod
  22. def setup_class(cls):
  23. cls.GAG = graph_atlas_g()
  24. def test_sizes(self):
  25. G = self.GAG[0]
  26. assert G.number_of_nodes() == 0
  27. assert G.number_of_edges() == 0
  28. G = self.GAG[7]
  29. assert G.number_of_nodes() == 3
  30. assert G.number_of_edges() == 3
  31. def test_names(self):
  32. for i, G in enumerate(self.GAG):
  33. assert int(G.name[1:]) == i
  34. def test_nondecreasing_nodes(self):
  35. # check for nondecreasing number of nodes
  36. for n1, n2 in pairwise(map(len, self.GAG)):
  37. assert n2 <= n1 + 1
  38. def test_nondecreasing_edges(self):
  39. # check for nondecreasing number of edges (for fixed number of
  40. # nodes)
  41. for n, group in groupby(self.GAG, key=nx.number_of_nodes):
  42. for m1, m2 in pairwise(map(nx.number_of_edges, group)):
  43. assert m2 <= m1 + 1
  44. def test_nondecreasing_degree_sequence(self):
  45. # Check for lexicographically nondecreasing degree sequences
  46. # (for fixed number of nodes and edges).
  47. #
  48. # There are three exceptions to this rule in the order given in
  49. # the "Atlas of Graphs" book, so we need to manually exclude
  50. # those.
  51. exceptions = [("G55", "G56"), ("G1007", "G1008"), ("G1012", "G1013")]
  52. for n, group in groupby(self.GAG, key=nx.number_of_nodes):
  53. for m, group in groupby(group, key=nx.number_of_edges):
  54. for G1, G2 in pairwise(group):
  55. if (G1.name, G2.name) in exceptions:
  56. continue
  57. d1 = sorted(d for v, d in G1.degree())
  58. d2 = sorted(d for v, d in G2.degree())
  59. assert d1 <= d2