// (C) Copyright Andrew Sutton 2009 // // Use, modification and distribution are subject to the // Boost Software License, Version 1.0 (See accompanying file // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) /*********************************************************** IMPORTANT: this file should not be tested - it creates invalid graphs/sequences which *do* crash at runtime - this seems to be the intent, but it's not clear why or whether the file should be retained. ***********************************************************/ #include #include #include #include "typestr.hpp" using namespace std; using namespace boost; // The purpose of this test is simply to provide a testing ground for the // invalidation of iterators and descriptors. template void make_graph(Graph& g) { // Build a simple (barbell) graph. typedef typename graph_traits::vertex_descriptor Vertex; Vertex u = add_vertex(10, g); Vertex v = add_vertex(20, g); add_edge(u, v, 100, g); } // Invalid iterators and descriptors will cause a segfault. template void test(Graph& g, Iterator i, Descriptor d, string const& str) { int x; cout << "... " << str << " iter" << endl; x = g[*i]; // cout << "... " << x << endl; cout << "... " << str << " desc" << endl; x = g[d]; // cout << "... " << x << endl; } template void invalidate_edges() { typedef typename graph_traits::edge_descriptor Edge; typedef typename graph_traits::edge_iterator EdgeIterator; Graph g; make_graph(g); // The actual test. These are valid here. EdgeIterator i = edges(g).first; Edge e = *i; // Add a vertex, see what breaks. add_vertex(g); test(g, i, e, "edges"); }; template void invalidate_vertices() { typedef typename graph_traits::vertex_descriptor Vertex; typedef typename graph_traits::vertex_iterator VertexIterator; Graph g; make_graph(g); // The actual test. These are valid here. VertexIterator i = vertices(g).first; Vertex v = *i; // Add a vertex, see what breaks. add_vertex(g); test(g, i, v, "vertices"); } template void invalidate_out_edges() { typedef typename graph_traits::edge_descriptor Edge; typedef typename graph_traits::out_edge_iterator OutIterator; Graph g; make_graph(g); // The actual test. These are valid here. OutIterator i = out_edges(*vertices(g).first, g).first; Edge e = *i; // Add a vertex, see what breaks. add_vertex(g); test(g, i, e, "out edges"); } template void invalidate_adj_verts() { typedef typename graph_traits::vertex_descriptor Vertex; typedef typename graph_traits::adjacency_iterator AdjIterator; Graph g; make_graph(g); // The actual test. These are valid here. AdjIterator i = adjacent_vertices(*vertices(g).first, g).first; Vertex v = *i; // Add a vertex, see what breaks. add_vertex(g); test(g, i, v, "adjacent vertices"); } int main() { typedef adjacency_list VVU; cout << "vecS vecS undirectedS" << endl; invalidate_vertices(); invalidate_edges(); invalidate_out_edges(); invalidate_adj_verts(); typedef adjacency_list VVB; cout << "vecS vecS bidirectionals" << endl; invalidate_vertices(); invalidate_edges(); invalidate_out_edges(); invalidate_adj_verts(); // If you comment out the tests before this, then adj_verts test will // run without segfaulting - at least under gcc-4.3. Not really sure why, // but I'm guessing it's still not generating valid results, and shouldn't // be taken as an indicator of stability. typedef adjacency_list VVD; cout << "vecS vecS directedS" << endl; invalidate_vertices(); // invalidate_edges(); // invalidate_out_edges(); // invalidate_adj_verts(); typedef adjacency_list LVD; cout << "listS vecS directedS" << endl; invalidate_vertices(); // invalidate_edges(); // invalidate_out_edges(); // invalidate_adj_verts(); }