grid_graph_test.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //=======================================================================
  2. // Copyright 2009 Trustees of Indiana University.
  3. // Authors: Michael Hansen, Andrew Lumsdaine
  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. #include <fstream>
  10. #include <iostream>
  11. #include <set>
  12. #include <boost/foreach.hpp>
  13. #include <boost/lexical_cast.hpp>
  14. #include <boost/graph/grid_graph.hpp>
  15. #include <boost/random.hpp>
  16. #include <boost/test/minimal.hpp>
  17. using namespace boost;
  18. // Function that prints a vertex to std::cout
  19. template <typename Vertex>
  20. void print_vertex(Vertex vertex_to_print) {
  21. std::cout << "(";
  22. for (std::size_t dimension_index = 0;
  23. dimension_index < vertex_to_print.size();
  24. ++dimension_index) {
  25. std::cout << vertex_to_print[dimension_index];
  26. if (dimension_index != (vertex_to_print.size() - 1)) {
  27. std::cout << ", ";
  28. }
  29. }
  30. std::cout << ")";
  31. }
  32. template <unsigned int Dims>
  33. void do_test(minstd_rand& generator) {
  34. typedef grid_graph<Dims> Graph;
  35. typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
  36. typedef typename graph_traits<Graph>::edges_size_type edges_size_type;
  37. typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
  38. typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
  39. std::cout << "Dimensions: " << Dims << ", lengths: ";
  40. // Randomly generate the dimension lengths (3-10) and wrapping
  41. boost::array<vertices_size_type, Dims> lengths;
  42. boost::array<bool, Dims> wrapped;
  43. for (unsigned int dimension_index = 0;
  44. dimension_index < Dims;
  45. ++dimension_index) {
  46. lengths[dimension_index] = 3 + (generator() % 8);
  47. wrapped[dimension_index] = ((generator() % 2) == 0);
  48. std::cout << lengths[dimension_index] <<
  49. (wrapped[dimension_index] ? " [W]" : " [U]") << ", ";
  50. }
  51. std::cout << std::endl;
  52. Graph graph(lengths, wrapped);
  53. // Verify dimension lengths and wrapping
  54. for (unsigned int dimension_index = 0;
  55. dimension_index < Dims;
  56. ++dimension_index) {
  57. BOOST_REQUIRE(graph.length(dimension_index) == lengths[dimension_index]);
  58. BOOST_REQUIRE(graph.wrapped(dimension_index) == wrapped[dimension_index]);
  59. }
  60. // Verify matching indices
  61. for (vertices_size_type vertex_index = 0;
  62. vertex_index < num_vertices(graph);
  63. ++vertex_index) {
  64. BOOST_REQUIRE(get(boost::vertex_index, graph, vertex(vertex_index, graph)) == vertex_index);
  65. }
  66. for (edges_size_type edge_index = 0;
  67. edge_index < num_edges(graph);
  68. ++edge_index) {
  69. edge_descriptor current_edge = edge_at(edge_index, graph);
  70. BOOST_REQUIRE(get(boost::edge_index, graph, current_edge) == edge_index);
  71. }
  72. // Verify all vertices are within bounds
  73. vertices_size_type vertex_count = 0;
  74. BOOST_FOREACH(vertex_descriptor current_vertex, vertices(graph)) {
  75. vertices_size_type current_index =
  76. get(boost::vertex_index, graph, current_vertex);
  77. for (unsigned int dimension_index = 0;
  78. dimension_index < Dims;
  79. ++dimension_index) {
  80. BOOST_REQUIRE(/*(current_vertex[dimension_index] >= 0) && */ // Always true
  81. (current_vertex[dimension_index] < lengths[dimension_index]));
  82. }
  83. // Verify out-edges of this vertex
  84. edges_size_type out_edge_count = 0;
  85. std::set<vertices_size_type> target_vertices;
  86. BOOST_FOREACH(edge_descriptor out_edge,
  87. out_edges(current_vertex, graph)) {
  88. target_vertices.insert
  89. (get(boost::vertex_index, graph, target(out_edge, graph)));
  90. ++out_edge_count;
  91. }
  92. BOOST_REQUIRE(out_edge_count == out_degree(current_vertex, graph));
  93. // Verify in-edges of this vertex
  94. edges_size_type in_edge_count = 0;
  95. BOOST_FOREACH(edge_descriptor in_edge,
  96. in_edges(current_vertex, graph)) {
  97. BOOST_REQUIRE(target_vertices.count
  98. (get(boost::vertex_index, graph, source(in_edge, graph))) > 0);
  99. ++in_edge_count;
  100. }
  101. BOOST_REQUIRE(in_edge_count == in_degree(current_vertex, graph));
  102. // The number of out-edges and in-edges should be the same
  103. BOOST_REQUIRE(degree(current_vertex, graph) ==
  104. out_degree(current_vertex, graph) +
  105. in_degree(current_vertex, graph));
  106. // Verify adjacent vertices to this vertex
  107. vertices_size_type adjacent_count = 0;
  108. BOOST_FOREACH(vertex_descriptor adjacent_vertex,
  109. adjacent_vertices(current_vertex, graph)) {
  110. BOOST_REQUIRE(target_vertices.count
  111. (get(boost::vertex_index, graph, adjacent_vertex)) > 0);
  112. ++adjacent_count;
  113. }
  114. BOOST_REQUIRE(adjacent_count == out_degree(current_vertex, graph));
  115. // Verify that this vertex is not listed as connected to any
  116. // vertices outside of its adjacent vertices.
  117. BOOST_FOREACH(vertex_descriptor unconnected_vertex, vertices(graph)) {
  118. vertices_size_type unconnected_index =
  119. get(boost::vertex_index, graph, unconnected_vertex);
  120. if ((unconnected_index == current_index) ||
  121. (target_vertices.count(unconnected_index) > 0)) {
  122. continue;
  123. }
  124. BOOST_REQUIRE(!edge(current_vertex, unconnected_vertex, graph).second);
  125. BOOST_REQUIRE(!edge(unconnected_vertex, current_vertex, graph).second);
  126. }
  127. ++vertex_count;
  128. }
  129. BOOST_REQUIRE(vertex_count == num_vertices(graph));
  130. // Verify all edges are within bounds
  131. edges_size_type edge_count = 0;
  132. BOOST_FOREACH(edge_descriptor current_edge, edges(graph)) {
  133. vertices_size_type source_index =
  134. get(boost::vertex_index, graph, source(current_edge, graph));
  135. vertices_size_type target_index =
  136. get(boost::vertex_index, graph, target(current_edge, graph));
  137. BOOST_REQUIRE(source_index != target_index);
  138. BOOST_REQUIRE(/* (source_index >= 0) : always true && */ (source_index < num_vertices(graph)));
  139. BOOST_REQUIRE(/* (target_index >= 0) : always true && */ (target_index < num_vertices(graph)));
  140. // Verify that the edge is listed as existing in both directions
  141. BOOST_REQUIRE(edge(source(current_edge, graph), target(current_edge, graph), graph).second);
  142. BOOST_REQUIRE(edge(target(current_edge, graph), source(current_edge, graph), graph).second);
  143. ++edge_count;
  144. }
  145. BOOST_REQUIRE(edge_count == num_edges(graph));
  146. }
  147. int test_main(int argc, char* argv[]) {
  148. std::size_t random_seed = time(0);
  149. if (argc > 1) {
  150. random_seed = lexical_cast<std::size_t>(argv[1]);
  151. }
  152. minstd_rand generator(random_seed);
  153. do_test<0>(generator);
  154. do_test<1>(generator);
  155. do_test<2>(generator);
  156. do_test<3>(generator);
  157. do_test<4>(generator);
  158. return (0);
  159. }