graph/test/adj_list_loops.cpp
Andrew Sutton 9ad0c6a23c Fixed #1622. A viable solution relies on the fact that incident edges in a
loop are stored adjacently in the out edge list of the vertex. A simple
modification of the global edge erasing loop for undirected graphs will
skip the next iterator if both the current and next contain the same iterator.


[SVN r50206]
2008-12-08 19:03:20 +00:00

120 lines
3.0 KiB
C++

#if __GNUC__ == 4 && __GNUC_MINOR__ >= 3
# define BOOST_NO_HASH
#endif
#include "typestr.hpp"
#include <iostream>
#include <boost/assert.hpp>
#include <boost/graph/adjacency_list.hpp>
using namespace boost;
// TODO: Integrate this into a larger adj_list test suite.
template <typename Graph>
void test_graph_nonloop()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
// Build a graph with 1 edge and turn it into a loop.
Graph g(5);
Vertex u = *vertices(g).first;
Vertex v = *next(vertices(g).first, 2);
add_edge(u, v, g);
BOOST_ASSERT(num_vertices(g) == 5);
BOOST_ASSERT(num_edges(g) == 1);
remove_edge(u, v, g);
BOOST_ASSERT(num_edges(g) == 0);
}
template <typename Graph>
void test_multigraph_nonloop()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
// Build a graph with 1 edge and turn it into a loop.
Graph g(5);
Vertex u = *vertices(g).first;
Vertex v = *next(vertices(g).first, 2);
add_edge(u, v, g);
add_edge(u, v, g);
BOOST_ASSERT(num_vertices(g) == 5);
BOOST_ASSERT(num_edges(g) == 2);
remove_edge(u, v, g);
BOOST_ASSERT(num_edges(g) == 0);
}
template <typename Graph>
void test_graph_loop()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
Graph g(5);
Vertex v = *next(vertices(g).first, 2);
add_edge(v, v, g);
BOOST_ASSERT(num_vertices(g) == 5);
BOOST_ASSERT(num_edges(g) == 1);
remove_edge(v, v, g);
BOOST_ASSERT(num_edges(g) == 0);
}
template <typename Graph>
void test_multigraph_loop()
{
typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
typedef typename graph_traits<Graph>::edge_descriptor Edge;
Graph g(5);
Vertex v = *next(vertices(g).first, 2);
add_edge(v, v, g);
add_edge(v, v, g);
BOOST_ASSERT(num_vertices(g) == 5);
BOOST_ASSERT(num_edges(g) == 2);
remove_edge(v, v, g);
BOOST_ASSERT(num_edges(g) == 0);
}
template <typename Kind>
void test()
{
typedef no_property na;
typedef adjacency_list<vecS, vecS, Kind, na, na, na, listS> VVL;
typedef adjacency_list<listS, vecS, Kind, na, na, na, listS> LVL;
typedef adjacency_list<setS, vecS, Kind, na, na, na, listS> SVL;
typedef adjacency_list<multisetS, vecS, Kind, na, na, na, listS> MVL;
test_graph_nonloop<VVL>();
test_graph_nonloop<LVL>();
test_graph_nonloop<SVL>();
test_graph_nonloop<MVL>();
test_multigraph_nonloop<VVL>();
test_multigraph_nonloop<LVL>();
test_multigraph_nonloop<MVL>();
test_graph_loop<VVL>();
test_graph_loop<LVL>();
test_graph_loop<SVL>();
test_graph_loop<MVL>();
test_multigraph_loop<VVL>();
test_multigraph_loop<LVL>();
test_multigraph_loop<MVL>();
}
int main()
{
test<undirectedS>();
test<directedS>();
test<bidirectionalS>();
return 0;
}