index_graph.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // (C) Copyright 2007-2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #include <iostream>
  7. #include <boost/graph/undirected_graph.hpp>
  8. #include <boost/graph/directed_graph.hpp>
  9. using namespace std;
  10. using namespace boost;
  11. template <typename Graph>
  12. void test()
  13. {
  14. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  15. typedef typename property_map<Graph, vertex_index_t>::type IndexMap;
  16. static const size_t N = 5;
  17. Graph g;
  18. (void)(IndexMap)get(vertex_index, g);
  19. // build up the graph
  20. Vertex v[N];
  21. for(size_t i = 0; i < N; ++i) {
  22. v[i] = add_vertex(g);
  23. }
  24. // after the first build, we should have these conditions
  25. BOOST_ASSERT(max_vertex_index(g) == N);
  26. for(size_t i = 0; i < N; ++i) {
  27. BOOST_ASSERT(get_vertex_index(v[i], g) == i);
  28. }
  29. // Remove all vertices and then re-add them.
  30. for(size_t i = 0; i < N; ++i) remove_vertex(v[i], g);
  31. BOOST_ASSERT(num_vertices(g) == 0);
  32. for(size_t i = 0; i < N; ++i) {
  33. v[i] = add_vertex(g);
  34. }
  35. BOOST_ASSERT(num_vertices(g) == N);
  36. // Before renumbering, our vertices should be off by N. In other words,
  37. // we're not in a good state.
  38. BOOST_ASSERT(max_vertex_index(g) == 10);
  39. for(size_t i = 0; i < N; ++i) {
  40. BOOST_ASSERT(get_vertex_index(v[i], g) == N + i);
  41. }
  42. // Renumber vertices
  43. renumber_vertex_indices(g);
  44. // And we should be back to the initial condition
  45. BOOST_ASSERT(max_vertex_index(g) == N);
  46. for(size_t i = 0; i < N; ++i) {
  47. BOOST_ASSERT(get_vertex_index(v[i], g) == i);
  48. }
  49. }
  50. // Make sure that graphs constructed over n vertices will actually number
  51. // their vertices correctly.
  52. template <typename Graph>
  53. void build()
  54. {
  55. typedef typename graph_traits<Graph>::vertex_iterator Iterator;
  56. typedef typename property_map<Graph, vertex_index_t>::type IndexMap;
  57. static const size_t N = 5;
  58. Graph g(N);
  59. BOOST_ASSERT(max_vertex_index(g) == N);
  60. (void)(IndexMap)get(vertex_index, g);
  61. // Each vertex should be numbered correctly.
  62. Iterator i, end;
  63. boost::tie(i, end) = vertices(g);
  64. for(size_t x = 0; i != end; ++i, ++x) {
  65. BOOST_ASSERT(get_vertex_index(*i, g) == x);
  66. }
  67. }
  68. int main(int, char*[])
  69. {
  70. test< undirected_graph<> >();
  71. test< directed_graph<> >();
  72. build< undirected_graph<> >();
  73. return 0;
  74. }