strong_components_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //=======================================================================
  2. // Copyright 2014 Alexander Lauser.
  3. // Authors: Alexander Lauser
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See
  6. // accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //=======================================================================
  9. // This test is an adapted version of the MWE for Bug #10231 (showing
  10. // incorrect root_map computation).
  11. /* Output should be:
  12. The example graph:
  13. a --> b
  14. b --> a c
  15. c --> b
  16. Vertex a is in component 0 and has root 0
  17. Vertex b is in component 0 and has root 0
  18. Vertex c is in component 0 and has root 0
  19. */
  20. #include <boost/config.hpp>
  21. #include <iostream>
  22. #include <vector>
  23. #include <boost/graph/strong_components.hpp>
  24. #include <boost/graph/adjacency_list.hpp>
  25. #include <boost/graph/graph_utility.hpp>
  26. int main(int, char*[])
  27. {
  28. using namespace boost;
  29. adjacency_list<vecS, vecS, directedS> G;
  30. typedef graph_traits<adjacency_list<vecS, vecS, directedS> >::vertex_descriptor Vertex;
  31. Vertex a = add_vertex(G);
  32. Vertex b = add_vertex(G);
  33. Vertex c = add_vertex(G);
  34. add_edge(a, b, G);
  35. add_edge(b, a, G);
  36. add_edge(c, b, G);
  37. add_edge(b, c, G);
  38. #if VERBOSE
  39. std::cout << "The example graph:" << std::endl;
  40. const char* name = "abc";
  41. print_graph(G, name);
  42. std::cout << std::endl;
  43. #endif
  44. std::vector<int> component(num_vertices(G)), discover_time(num_vertices(G));
  45. std::vector<default_color_type> color(num_vertices(G));
  46. std::vector<Vertex> root(num_vertices(G));
  47. strong_components(G, make_iterator_property_map(component.begin(), get(vertex_index, G)),
  48. root_map(make_iterator_property_map(root.begin(), get(vertex_index, G))).
  49. color_map(make_iterator_property_map(color.begin(), get(vertex_index, G))).
  50. discover_time_map(make_iterator_property_map(discover_time.begin(), get(vertex_index, G))));
  51. #if VERBOSE
  52. for (std::vector<int>::size_type i = 0; i != component.size(); ++i)
  53. std::cout << "Vertex " << name[i]
  54. << " is in component " << component[i]
  55. << " and has root " << root[i] << std::endl;
  56. #endif
  57. #if VERBOSE
  58. bool test_failed;
  59. #endif
  60. int ret = 0;
  61. //////////
  62. #if VERBOSE
  63. test_failed = false;
  64. std::cerr << "Testing component-computation of strong_components ..." << std::endl;
  65. #endif
  66. for (std::vector<int>::size_type i = 0; i != component.size(); ++i) {
  67. if(component[i] != 0) {
  68. #if VERBOSE
  69. test_failed = true;
  70. #endif
  71. ret = -1;
  72. break;
  73. }
  74. }
  75. #if VERBOSE
  76. std::cerr << (test_failed ? " **** Failed." : " Passed.") << std::endl;
  77. #endif
  78. //////////
  79. //////////
  80. #if VERBOSE
  81. test_failed = false;
  82. std::cerr << "Testing root_map-computation of strong_components ..." << std::endl;
  83. #endif
  84. for (std::vector<int>::size_type i = 0; i != component.size(); ++i) {
  85. if(root[i] != 0) {
  86. #if VERBOSE
  87. test_failed = true;
  88. #endif
  89. ret = -1;
  90. break;
  91. }
  92. }
  93. #if VERBOSE
  94. std::cerr << (test_failed ? " **** Failed." : " Passed.") << std::endl;
  95. #endif
  96. //////////
  97. if (ret == 0)
  98. std::cout << "tests passed" << std::endl;
  99. return ret;
  100. }