Chris@16: //======================================================================= Chris@16: // Copyright 2002 Indiana University. Chris@16: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: Chris@16: #ifndef BOOST_GRAPH_TEST_HPP Chris@16: #define BOOST_GRAPH_TEST_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include // for connects Chris@16: #include Chris@16: #include Chris@16: Chris@16: Chris@16: // UNDER CONSTRUCTION Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: template Chris@16: struct graph_test Chris@16: { Chris@16: Chris@16: typedef typename graph_traits::vertex_descriptor vertex_t; Chris@16: typedef typename graph_traits::edge_descriptor edge_t; Chris@16: typedef typename graph_traits::vertices_size_type v_size_t; Chris@16: typedef typename graph_traits::degree_size_type deg_size_t; Chris@16: typedef typename graph_traits::edges_size_type e_size_t; Chris@16: typedef typename graph_traits::out_edge_iterator out_edge_iter; Chris@16: typedef typename property_map::type index_map_t; Chris@16: typedef iterator_property_map::iterator, Chris@16: index_map_t,vertex_t,vertex_t&> IsoMap; Chris@16: Chris@16: struct ignore_vertex { Chris@16: ignore_vertex() { } Chris@16: ignore_vertex(vertex_t v) : v(v) { } Chris@16: bool operator()(vertex_t x) const { return x != v; } Chris@16: vertex_t v; Chris@16: }; Chris@16: struct ignore_edge { Chris@16: ignore_edge() { } Chris@16: ignore_edge(edge_t e) : e(e) { } Chris@16: bool operator()(edge_t x) const { return x != e; } Chris@16: edge_t e; Chris@16: }; Chris@16: struct ignore_edges { Chris@16: ignore_edges(vertex_t s, vertex_t t, const Graph& g) Chris@16: : s(s), t(t), g(g) { } Chris@16: bool operator()(edge_t x) const { Chris@16: return !(source(x, g) == s && target(x, g) == t); Chris@16: } Chris@16: vertex_t s; vertex_t t; const Graph& g; Chris@16: }; Chris@16: Chris@16: //========================================================================= Chris@16: // Traversal Operations Chris@16: Chris@16: void test_incidence_graph Chris@16: (const std::vector& vertex_set, Chris@16: const std::vector< std::pair >& edge_set, Chris@16: const Graph& g) Chris@16: { Chris@16: typedef typename std::vector::const_iterator vertex_iter; Chris@16: typedef typename std::vector< std::pair > Chris@16: ::const_iterator edge_iter; Chris@16: typedef typename graph_traits::out_edge_iterator out_edge_iter; Chris@16: Chris@16: for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) { Chris@16: vertex_t u = *ui; Chris@16: std::vector adj; Chris@16: for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e) Chris@16: if (e->first == u) Chris@16: adj.push_back(e->second); Chris@16: Chris@16: std::pair p = out_edges(u, g); Chris@16: BOOST_CHECK(out_degree(u, g) == adj.size()); Chris@16: BOOST_CHECK(deg_size_t(std::distance(p.first, p.second)) Chris@16: == out_degree(u, g)); Chris@16: for (; p.first != p.second; ++p.first) { Chris@16: edge_t e = *p.first; Chris@16: BOOST_CHECK(source(e, g) == u); Chris@16: BOOST_CHECK(container_contains(adj, target(e, g)) == true); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: void test_bidirectional_graph Chris@16: (const std::vector& vertex_set, Chris@16: const std::vector< std::pair >& edge_set, Chris@16: const Graph& g) Chris@16: { Chris@16: typedef typename std::vector::const_iterator vertex_iter; Chris@16: typedef typename std::vector< std::pair > Chris@16: ::const_iterator edge_iter; Chris@16: typedef typename graph_traits::in_edge_iterator in_edge_iter; Chris@16: Chris@16: for (vertex_iter vi = vertex_set.begin(); vi != vertex_set.end(); ++vi) { Chris@16: vertex_t v = *vi; Chris@16: std::vector inv_adj; Chris@16: for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e) Chris@16: if (e->second == v) Chris@16: inv_adj.push_back(e->first); Chris@16: Chris@16: std::pair p = in_edges(v, g); Chris@16: BOOST_CHECK(in_degree(v, g) == inv_adj.size()); Chris@16: BOOST_CHECK(deg_size_t(std::distance(p.first, p.second)) Chris@16: == in_degree(v, g)); Chris@16: for (; p.first != p.second; ++p.first) { Chris@16: edge_t e = *p.first; Chris@16: BOOST_CHECK(target(e, g) == v); Chris@16: BOOST_CHECK(container_contains(inv_adj, source(e, g)) == true); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: void test_adjacency_graph Chris@16: (const std::vector& vertex_set, Chris@16: const std::vector< std::pair >& edge_set, Chris@16: const Graph& g) Chris@16: { Chris@16: typedef typename std::vector::const_iterator vertex_iter; Chris@16: typedef typename std::vector > Chris@16: ::const_iterator edge_iter; Chris@16: typedef typename graph_traits::adjacency_iterator adj_iter; Chris@16: Chris@16: for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) { Chris@16: vertex_t u = *ui; Chris@16: std::vector adj; Chris@16: for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e) Chris@16: if (e->first == u) Chris@16: adj.push_back(e->second); Chris@16: Chris@16: std::pair p = adjacent_vertices(u, g); Chris@16: BOOST_CHECK(deg_size_t(std::distance(p.first, p.second)) == adj.size()); Chris@16: for (; p.first != p.second; ++p.first) { Chris@16: vertex_t v = *p.first; Chris@16: BOOST_CHECK(container_contains(adj, v) == true); Chris@16: } Chris@16: } Chris@16: } Chris@16: Chris@16: void test_vertex_list_graph Chris@16: (const std::vector& vertex_set, const Graph& g) Chris@16: { Chris@16: typedef typename graph_traits::vertex_iterator v_iter; Chris@16: std::pair p = vertices(g); Chris@16: BOOST_CHECK(num_vertices(g) == vertex_set.size()); Chris@16: v_size_t n = (size_t)std::distance(p.first, p.second); Chris@16: BOOST_CHECK(n == num_vertices(g)); Chris@16: for (; p.first != p.second; ++p.first) { Chris@16: vertex_t v = *p.first; Chris@16: BOOST_CHECK(container_contains(vertex_set, v) == true); Chris@16: } Chris@16: } Chris@16: Chris@16: void test_edge_list_graph Chris@16: (const std::vector& vertex_set, Chris@16: const std::vector< std::pair >& edge_set, Chris@16: const Graph& g) Chris@16: { Chris@16: typedef typename graph_traits::edge_iterator e_iter; Chris@16: std::pair p = edges(g); Chris@16: BOOST_CHECK(num_edges(g) == edge_set.size()); Chris@16: e_size_t m = std::distance(p.first, p.second); Chris@16: BOOST_CHECK(m == num_edges(g)); Chris@16: for (; p.first != p.second; ++p.first) { Chris@16: edge_t e = *p.first; Chris@16: BOOST_CHECK(find_if(edge_set, connects(source(e, g), target(e, g), g)) != boost::end(edge_set)); Chris@16: BOOST_CHECK(container_contains(vertex_set, source(e, g)) == true); Chris@16: BOOST_CHECK(container_contains(vertex_set, target(e, g)) == true); Chris@16: } Chris@16: } Chris@16: Chris@16: void test_adjacency_matrix Chris@16: (const std::vector& vertex_set, Chris@16: const std::vector< std::pair >& edge_set, Chris@16: const Graph& g) Chris@16: { Chris@16: std::pair p; Chris@16: for (typename std::vector > Chris@16: ::const_iterator i = edge_set.begin(); Chris@16: i != edge_set.end(); ++i) { Chris@16: p = edge(i->first, i->second, g); Chris@16: BOOST_CHECK(p.second == true); Chris@16: BOOST_CHECK(source(p.first, g) == i->first); Chris@16: BOOST_CHECK(target(p.first, g) == i->second); Chris@16: } Chris@16: typename std::vector::const_iterator j, k; Chris@16: for (j = vertex_set.begin(); j != vertex_set.end(); ++j) Chris@16: for (k = vertex_set.begin(); k != vertex_set.end(); ++k) { Chris@16: p = edge(*j, *k, g); Chris@16: if (p.second == true) Chris@16: BOOST_CHECK(find_if(edge_set, Chris@16: connects(source(p.first, g), target(p.first, g), g)) != boost::end(edge_set)); Chris@16: } Chris@16: } Chris@16: Chris@16: //========================================================================= Chris@16: // Mutating Operations Chris@16: Chris@16: void test_add_vertex(Graph& g) Chris@16: { Chris@16: Graph cpy; Chris@16: std::vector iso_vec(num_vertices(g)); Chris@16: IsoMap iso_map(iso_vec.begin(), get(vertex_index, g)); Chris@16: copy_graph(g, cpy, orig_to_copy(iso_map)); Chris@16: Chris@16: BOOST_CHECK((verify_isomorphism(g, cpy, iso_map))); Chris@16: Chris@16: vertex_t v = add_vertex(g); Chris@16: Chris@16: BOOST_CHECK(num_vertices(g) == num_vertices(cpy) + 1); Chris@16: Chris@16: BOOST_CHECK(out_degree(v, g) == 0); Chris@16: Chris@16: // Make sure the rest of the graph stayed the same Chris@16: BOOST_CHECK((verify_isomorphism Chris@16: (make_filtered_graph(g, keep_all(), ignore_vertex(v)), cpy, Chris@16: iso_map))); Chris@16: } Chris@16: Chris@16: void test_add_edge(vertex_t u, vertex_t v, Graph& g) Chris@16: { Chris@16: Graph cpy; Chris@16: std::vector iso_vec(num_vertices(g)); Chris@16: IsoMap iso_map(iso_vec.begin(), get(vertex_index, g)); Chris@16: copy_graph(g, cpy, orig_to_copy(iso_map)); Chris@16: Chris@16: bool parallel_edge_exists = container_contains(adjacent_vertices(u, g), v); Chris@16: Chris@16: std::pair p = add_edge(u, v, g); Chris@16: edge_t e = p.first; Chris@16: bool added = p.second; Chris@16: Chris@16: if (is_undirected(g) && u == v) // self edge Chris@16: BOOST_CHECK(added == false); Chris@16: else if (parallel_edge_exists) Chris@16: BOOST_CHECK(allows_parallel_edges(g) && added == true Chris@16: || !allows_parallel_edges(g) && added == false); Chris@16: else Chris@16: BOOST_CHECK(added == true); Chris@16: Chris@16: if (p.second == true) { // edge added Chris@16: BOOST_CHECK(num_edges(g) == num_edges(cpy) + 1); Chris@16: Chris@16: BOOST_CHECK(container_contains(out_edges(u, g), e) == true); Chris@16: Chris@16: BOOST_CHECK((verify_isomorphism Chris@16: (make_filtered_graph(g, ignore_edge(e)), cpy, iso_map))); Chris@16: } Chris@16: else { // edge not added Chris@16: if (! (is_undirected(g) && u == v)) { Chris@16: // e should be a parallel edge Chris@16: BOOST_CHECK(source(e, g) == u); Chris@16: BOOST_CHECK(target(e, g) == v); Chris@16: } Chris@16: // The graph should not be changed. Chris@16: BOOST_CHECK((verify_isomorphism(g, cpy, iso_map))); Chris@16: } Chris@16: } // test_add_edge() Chris@16: Chris@16: Chris@16: void test_remove_edge(vertex_t u, vertex_t v, Graph& g) Chris@16: { Chris@16: Graph cpy; Chris@16: std::vector iso_vec(num_vertices(g)); Chris@16: IsoMap iso_map(iso_vec.begin(), get(vertex_index, g)); Chris@16: copy_graph(g, cpy, orig_to_copy(iso_map)); Chris@16: Chris@16: deg_size_t occurances = count(adjacent_vertices(u, g), v); Chris@16: Chris@16: remove_edge(u, v, g); Chris@16: Chris@16: BOOST_CHECK(num_edges(g) + occurances == num_edges(cpy)); Chris@16: BOOST_CHECK((verify_isomorphism Chris@16: (g, make_filtered_graph(cpy, ignore_edges(u,v,cpy)), Chris@16: iso_map))); Chris@16: } Chris@16: Chris@16: void test_remove_edge(edge_t e, Graph& g) Chris@16: { Chris@16: Graph cpy; Chris@16: std::vector iso_vec(num_vertices(g)); Chris@16: IsoMap iso_map(iso_vec.begin(), get(vertex_index, g)); Chris@16: copy_graph(g, cpy, orig_to_copy(iso_map)); Chris@16: Chris@16: vertex_t u = source(e, g), v = target(e, g); Chris@16: deg_size_t occurances = count(adjacent_vertices(u, g), v); Chris@16: Chris@16: remove_edge(e, g); Chris@16: Chris@16: BOOST_CHECK(num_edges(g) + 1 == num_edges(cpy)); Chris@16: BOOST_CHECK(count(adjacent_vertices(u, g), v) + 1 == occurances); Chris@16: BOOST_CHECK((verify_isomorphism Chris@16: (g, make_filtered_graph(cpy, ignore_edge(e)), Chris@16: iso_map))); Chris@16: } Chris@16: Chris@16: void test_clear_vertex(vertex_t v, Graph& g) Chris@16: { Chris@16: Graph cpy; Chris@16: std::vector iso_vec(num_vertices(g)); Chris@16: IsoMap iso_map(iso_vec.begin(), get(vertex_index, g)); Chris@16: copy_graph(g, cpy, orig_to_copy(iso_map)); Chris@16: Chris@16: clear_vertex(v, g); Chris@16: Chris@16: BOOST_CHECK(out_degree(v, g) == 0); Chris@16: BOOST_CHECK(num_vertices(g) == num_vertices(cpy)); Chris@16: BOOST_CHECK((verify_isomorphism Chris@16: (g, make_filtered_graph(cpy, keep_all(), ignore_vertex(v)), Chris@16: iso_map))); Chris@16: } Chris@16: Chris@16: //========================================================================= Chris@16: // Property Map Chris@16: Chris@16: template Chris@16: void test_readable_vertex_property_graph Chris@16: (const std::vector& vertex_prop, PropertyTag tag, const Graph& g) Chris@16: { Chris@16: typedef typename property_map::const_type const_Map; Chris@16: const_Map pmap = get(tag, g); Chris@16: typename std::vector::const_iterator i = vertex_prop.begin(); Chris@16: Chris@16: for (typename boost::graph_traits::vertex_iterator Chris@16: bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second; Chris@16: bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9) Chris@16: for (typename boost::graph_traits::vertex_descriptor v; Chris@16: bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false; Chris@16: ++bgl_first_9) { Chris@16: //BGL_FORALL_VERTICES_T(v, g, Graph) { Chris@16: typename property_traits::value_type Chris@16: pval1 = get(pmap, v), pval2 = get(tag, g, v); Chris@16: BOOST_CHECK(pval1 == pval2); Chris@16: BOOST_CHECK(pval1 == *i++); Chris@16: } Chris@16: } Chris@16: Chris@16: template Chris@16: void test_vertex_property_graph Chris@16: (const std::vector& vertex_prop, PropertyTag tag, Graph& g) Chris@16: { Chris@16: typedef typename property_map::type PMap; Chris@16: PMap pmap = get(tag, g); Chris@16: typename std::vector::const_iterator i = vertex_prop.begin(); Chris@16: for (typename boost::graph_traits::vertex_iterator Chris@16: bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second; Chris@16: bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9) Chris@16: for (typename boost::graph_traits::vertex_descriptor v; Chris@16: bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false; Chris@16: ++bgl_first_9) Chris@16: // BGL_FORALL_VERTICES_T(v, g, Graph) Chris@16: put(pmap, v, *i++); Chris@16: Chris@16: test_readable_vertex_property_graph(vertex_prop, tag, g); Chris@16: Chris@16: BGL_FORALL_VERTICES_T(v, g, Graph) Chris@16: put(pmap, v, vertex_prop[0]); Chris@16: Chris@16: typename std::vector::const_iterator j = vertex_prop.begin(); Chris@16: BGL_FORALL_VERTICES_T(v, g, Graph) Chris@16: put(tag, g, v, *j++); Chris@16: Chris@16: test_readable_vertex_property_graph(vertex_prop, tag, g); Chris@16: } Chris@16: Chris@16: Chris@16: }; Chris@16: Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_GRAPH_TEST_HPP