test_voterank.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. Unit tests for VoteRank.
  3. """
  4. import networkx as nx
  5. class TestVoteRankCentrality:
  6. # Example Graph present in reference paper
  7. def test_voterank_centrality_1(self):
  8. G = nx.Graph()
  9. G.add_edges_from(
  10. [
  11. (7, 8),
  12. (7, 5),
  13. (7, 9),
  14. (5, 0),
  15. (0, 1),
  16. (0, 2),
  17. (0, 3),
  18. (0, 4),
  19. (1, 6),
  20. (2, 6),
  21. (3, 6),
  22. (4, 6),
  23. ]
  24. )
  25. assert [0, 7, 6] == nx.voterank(G)
  26. def test_voterank_emptygraph(self):
  27. G = nx.Graph()
  28. assert [] == nx.voterank(G)
  29. # Graph unit test
  30. def test_voterank_centrality_2(self):
  31. G = nx.florentine_families_graph()
  32. d = nx.voterank(G, 4)
  33. exact = ["Medici", "Strozzi", "Guadagni", "Castellani"]
  34. assert exact == d
  35. # DiGraph unit test
  36. def test_voterank_centrality_3(self):
  37. G = nx.gnc_graph(10, seed=7)
  38. d = nx.voterank(G, 4)
  39. exact = [3, 6, 8]
  40. assert exact == d
  41. # MultiGraph unit test
  42. def test_voterank_centrality_4(self):
  43. G = nx.MultiGraph()
  44. G.add_edges_from(
  45. [(0, 1), (0, 1), (1, 2), (2, 5), (2, 5), (5, 6), (5, 6), (2, 4), (4, 3)]
  46. )
  47. exact = [2, 1, 5, 4]
  48. assert exact == nx.voterank(G)
  49. # MultiDiGraph unit test
  50. def test_voterank_centrality_5(self):
  51. G = nx.MultiDiGraph()
  52. G.add_edges_from(
  53. [(0, 1), (0, 1), (1, 2), (2, 5), (2, 5), (5, 6), (5, 6), (2, 4), (4, 3)]
  54. )
  55. exact = [2, 0, 5, 4]
  56. assert exact == nx.voterank(G)