graph/test/adj_list_invalidation.cpp
Andrew Sutton a6daa83472 Added tests for out edge and adjacency components under add_vertex.
For some reason, adjacency_iterators seem to remain valid if no operations
on other graph types are executed prior to their use, which is completely
at odds with what I would expect.


[SVN r50231]
2008-12-10 15:46:05 +00:00

141 lines
3.6 KiB
C++

#include <iostream>
#include <string>
#include <boost/graph/adjacency_list.hpp>
#include "../../../../../gpld/common/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 <typename Graph>
void make_graph(Graph& g)
{
// Build a simple (barbell) graph.
typedef typename graph_traits<Graph>::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 <typename Graph, typename Iterator, typename Descriptor>
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 <typename Graph>
void invalidate_edges()
{
typedef typename graph_traits<Graph>::edge_descriptor Edge;
typedef typename graph_traits<Graph>::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 <typename Graph>
void invalidate_vertices()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::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 <typename Graph>
void invalidate_out_edges()
{
typedef typename graph_traits<Graph>::edge_descriptor Edge;
typedef typename graph_traits<Graph>::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 <typename Graph>
void invalidate_adj_verts()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::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<vecS, vecS, undirectedS, int, int> VVU;
cout << "vecS vecS undirectedS" << endl;
invalidate_vertices<VVU>();
invalidate_edges<VVU>();
invalidate_out_edges<VVU>();
invalidate_adj_verts<VVU>();
typedef adjacency_list<vecS, vecS, bidirectionalS, int, int> VVB;
cout << "vecS vecS bidirectionals" << endl;
invalidate_vertices<VVB>();
invalidate_edges<VVB>();
invalidate_out_edges<VVB>();
invalidate_adj_verts<VVB>();
typedef adjacency_list<vecS, vecS, directedS, int, int> VVD;
cout << "vecS vecS directedS" << endl;
invalidate_vertices<VVD>();
// invalidate_edges<VVD>();
// invalidate_out_edges<VVD>();
// invalidate_adj_verts<VVD>();
typedef adjacency_list<vecS, listS, directedS, int, int> VLD;
cout << "vecS listS directedS" << endl;
invalidate_vertices<VLD>();
invalidate_edges<VLD>();
invalidate_out_edges<VLD>();
invalidate_adj_verts<VLD>();
}