test_semiconnected.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from itertools import chain
  2. import pytest
  3. import networkx as nx
  4. class TestIsSemiconnected:
  5. def test_undirected(self):
  6. pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.Graph())
  7. pytest.raises(nx.NetworkXNotImplemented, nx.is_semiconnected, nx.MultiGraph())
  8. def test_empty(self):
  9. pytest.raises(nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.DiGraph())
  10. pytest.raises(
  11. nx.NetworkXPointlessConcept, nx.is_semiconnected, nx.MultiDiGraph()
  12. )
  13. def test_single_node_graph(self):
  14. G = nx.DiGraph()
  15. G.add_node(0)
  16. assert nx.is_semiconnected(G)
  17. def test_path(self):
  18. G = nx.path_graph(100, create_using=nx.DiGraph())
  19. assert nx.is_semiconnected(G)
  20. G.add_edge(100, 99)
  21. assert not nx.is_semiconnected(G)
  22. def test_cycle(self):
  23. G = nx.cycle_graph(100, create_using=nx.DiGraph())
  24. assert nx.is_semiconnected(G)
  25. G = nx.path_graph(100, create_using=nx.DiGraph())
  26. G.add_edge(0, 99)
  27. assert nx.is_semiconnected(G)
  28. def test_tree(self):
  29. G = nx.DiGraph()
  30. G.add_edges_from(
  31. chain.from_iterable([(i, 2 * i + 1), (i, 2 * i + 2)] for i in range(100))
  32. )
  33. assert not nx.is_semiconnected(G)
  34. def test_dumbbell(self):
  35. G = nx.cycle_graph(100, create_using=nx.DiGraph())
  36. G.add_edges_from((i + 100, (i + 1) % 100 + 100) for i in range(100))
  37. assert not nx.is_semiconnected(G) # G is disconnected.
  38. G.add_edge(100, 99)
  39. assert nx.is_semiconnected(G)
  40. def test_alternating_path(self):
  41. G = nx.DiGraph(
  42. chain.from_iterable([(i, i - 1), (i, i + 1)] for i in range(0, 100, 2))
  43. )
  44. assert not nx.is_semiconnected(G)