adj_list_invalidation.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // (C) Copyright Andrew Sutton 2009
  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. /***********************************************************
  7. IMPORTANT: this file should not be tested - it creates invalid graphs/sequences
  8. which *do* crash at runtime - this seems to be the intent, but it's not
  9. clear why or whether the file should be retained.
  10. ***********************************************************/
  11. #include <iostream>
  12. #include <string>
  13. #include <boost/graph/adjacency_list.hpp>
  14. #include "typestr.hpp"
  15. using namespace std;
  16. using namespace boost;
  17. // The purpose of this test is simply to provide a testing ground for the
  18. // invalidation of iterators and descriptors.
  19. template <typename Graph>
  20. void make_graph(Graph& g)
  21. {
  22. // Build a simple (barbell) graph.
  23. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  24. Vertex u = add_vertex(10, g);
  25. Vertex v = add_vertex(20, g);
  26. add_edge(u, v, 100, g);
  27. }
  28. // Invalid iterators and descriptors will cause a segfault.
  29. template <typename Graph, typename Iterator, typename Descriptor>
  30. void test(Graph& g, Iterator i, Descriptor d, string const& str)
  31. {
  32. int x;
  33. cout << "... " << str << " iter" << endl;
  34. x = g[*i];
  35. // cout << "... " << x << endl;
  36. cout << "... " << str << " desc" << endl;
  37. x = g[d];
  38. // cout << "... " << x << endl;
  39. }
  40. template <typename Graph>
  41. void invalidate_edges()
  42. {
  43. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  44. typedef typename graph_traits<Graph>::edge_iterator EdgeIterator;
  45. Graph g;
  46. make_graph(g);
  47. // The actual test. These are valid here.
  48. EdgeIterator i = edges(g).first;
  49. Edge e = *i;
  50. // Add a vertex, see what breaks.
  51. add_vertex(g);
  52. test(g, i, e, "edges");
  53. };
  54. template <typename Graph>
  55. void invalidate_vertices()
  56. {
  57. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  58. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  59. Graph g;
  60. make_graph(g);
  61. // The actual test. These are valid here.
  62. VertexIterator i = vertices(g).first;
  63. Vertex v = *i;
  64. // Add a vertex, see what breaks.
  65. add_vertex(g);
  66. test(g, i, v, "vertices");
  67. }
  68. template <typename Graph>
  69. void invalidate_out_edges()
  70. {
  71. typedef typename graph_traits<Graph>::edge_descriptor Edge;
  72. typedef typename graph_traits<Graph>::out_edge_iterator OutIterator;
  73. Graph g;
  74. make_graph(g);
  75. // The actual test. These are valid here.
  76. OutIterator i = out_edges(*vertices(g).first, g).first;
  77. Edge e = *i;
  78. // Add a vertex, see what breaks.
  79. add_vertex(g);
  80. test(g, i, e, "out edges");
  81. }
  82. template <typename Graph>
  83. void invalidate_adj_verts()
  84. {
  85. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  86. typedef typename graph_traits<Graph>::adjacency_iterator AdjIterator;
  87. Graph g;
  88. make_graph(g);
  89. // The actual test. These are valid here.
  90. AdjIterator i = adjacent_vertices(*vertices(g).first, g).first;
  91. Vertex v = *i;
  92. // Add a vertex, see what breaks.
  93. add_vertex(g);
  94. test(g, i, v, "adjacent vertices");
  95. }
  96. int main()
  97. {
  98. typedef adjacency_list<vecS, vecS, undirectedS, int, int> VVU;
  99. cout << "vecS vecS undirectedS" << endl;
  100. invalidate_vertices<VVU>();
  101. invalidate_edges<VVU>();
  102. invalidate_out_edges<VVU>();
  103. invalidate_adj_verts<VVU>();
  104. typedef adjacency_list<vecS, vecS, bidirectionalS, int, int> VVB;
  105. cout << "vecS vecS bidirectionals" << endl;
  106. invalidate_vertices<VVB>();
  107. invalidate_edges<VVB>();
  108. invalidate_out_edges<VVB>();
  109. invalidate_adj_verts<VVB>();
  110. // If you comment out the tests before this, then adj_verts test will
  111. // run without segfaulting - at least under gcc-4.3. Not really sure why,
  112. // but I'm guessing it's still not generating valid results, and shouldn't
  113. // be taken as an indicator of stability.
  114. typedef adjacency_list<vecS, vecS, directedS, int, int> VVD;
  115. cout << "vecS vecS directedS" << endl;
  116. invalidate_vertices<VVD>();
  117. // invalidate_edges<VVD>();
  118. // invalidate_out_edges<VVD>();
  119. // invalidate_adj_verts<VVD>();
  120. typedef adjacency_list<listS, vecS, directedS, int, int> LVD;
  121. cout << "listS vecS directedS" << endl;
  122. invalidate_vertices<LVD>();
  123. // invalidate_edges<LVD>();
  124. // invalidate_out_edges<LVD>();
  125. // invalidate_adj_verts<LVD>();
  126. }