annotate DEPENDENCIES/generic/include/boost/graph/graph_test.hpp @ 125:34e428693f5d vext

Vext -> Repoint
author Chris Cannam
date Thu, 14 Jun 2018 11:15:39 +0100
parents 2665513ce2d3
children
rev   line source
Chris@16 1 //=======================================================================
Chris@16 2 // Copyright 2002 Indiana University.
Chris@16 3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
Chris@16 4 //
Chris@16 5 // Distributed under the Boost Software License, Version 1.0. (See
Chris@16 6 // accompanying file LICENSE_1_0.txt or copy at
Chris@16 7 // http://www.boost.org/LICENSE_1_0.txt)
Chris@16 8 //=======================================================================
Chris@16 9
Chris@16 10 #ifndef BOOST_GRAPH_TEST_HPP
Chris@16 11 #define BOOST_GRAPH_TEST_HPP
Chris@16 12
Chris@16 13 #include <vector>
Chris@16 14 #include <boost/test/minimal.hpp>
Chris@16 15 #include <boost/graph/filtered_graph.hpp>
Chris@16 16 #include <boost/graph/iteration_macros.hpp>
Chris@16 17 #include <boost/graph/isomorphism.hpp>
Chris@16 18 #include <boost/graph/copy.hpp>
Chris@16 19 #include <boost/graph/graph_utility.hpp> // for connects
Chris@16 20 #include <boost/range.hpp>
Chris@16 21 #include <boost/range/algorithm/find_if.hpp>
Chris@16 22
Chris@16 23
Chris@16 24 // UNDER CONSTRUCTION
Chris@16 25
Chris@16 26 namespace boost {
Chris@16 27
Chris@16 28 template <typename Graph>
Chris@16 29 struct graph_test
Chris@16 30 {
Chris@16 31
Chris@16 32 typedef typename graph_traits<Graph>::vertex_descriptor vertex_t;
Chris@16 33 typedef typename graph_traits<Graph>::edge_descriptor edge_t;
Chris@16 34 typedef typename graph_traits<Graph>::vertices_size_type v_size_t;
Chris@16 35 typedef typename graph_traits<Graph>::degree_size_type deg_size_t;
Chris@16 36 typedef typename graph_traits<Graph>::edges_size_type e_size_t;
Chris@16 37 typedef typename graph_traits<Graph>::out_edge_iterator out_edge_iter;
Chris@16 38 typedef typename property_map<Graph, vertex_index_t>::type index_map_t;
Chris@16 39 typedef iterator_property_map<typename std::vector<vertex_t>::iterator,
Chris@16 40 index_map_t,vertex_t,vertex_t&> IsoMap;
Chris@16 41
Chris@16 42 struct ignore_vertex {
Chris@16 43 ignore_vertex() { }
Chris@16 44 ignore_vertex(vertex_t v) : v(v) { }
Chris@16 45 bool operator()(vertex_t x) const { return x != v; }
Chris@16 46 vertex_t v;
Chris@16 47 };
Chris@16 48 struct ignore_edge {
Chris@16 49 ignore_edge() { }
Chris@16 50 ignore_edge(edge_t e) : e(e) { }
Chris@16 51 bool operator()(edge_t x) const { return x != e; }
Chris@16 52 edge_t e;
Chris@16 53 };
Chris@16 54 struct ignore_edges {
Chris@16 55 ignore_edges(vertex_t s, vertex_t t, const Graph& g)
Chris@16 56 : s(s), t(t), g(g) { }
Chris@16 57 bool operator()(edge_t x) const {
Chris@16 58 return !(source(x, g) == s && target(x, g) == t);
Chris@16 59 }
Chris@16 60 vertex_t s; vertex_t t; const Graph& g;
Chris@16 61 };
Chris@16 62
Chris@16 63 //=========================================================================
Chris@16 64 // Traversal Operations
Chris@16 65
Chris@16 66 void test_incidence_graph
Chris@16 67 (const std::vector<vertex_t>& vertex_set,
Chris@16 68 const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
Chris@16 69 const Graph& g)
Chris@16 70 {
Chris@16 71 typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
Chris@16 72 typedef typename std::vector< std::pair<vertex_t, vertex_t> >
Chris@16 73 ::const_iterator edge_iter;
Chris@16 74 typedef typename graph_traits<Graph>::out_edge_iterator out_edge_iter;
Chris@16 75
Chris@16 76 for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) {
Chris@16 77 vertex_t u = *ui;
Chris@16 78 std::vector<vertex_t> adj;
Chris@16 79 for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
Chris@16 80 if (e->first == u)
Chris@16 81 adj.push_back(e->second);
Chris@16 82
Chris@16 83 std::pair<out_edge_iter, out_edge_iter> p = out_edges(u, g);
Chris@16 84 BOOST_CHECK(out_degree(u, g) == adj.size());
Chris@16 85 BOOST_CHECK(deg_size_t(std::distance(p.first, p.second))
Chris@16 86 == out_degree(u, g));
Chris@16 87 for (; p.first != p.second; ++p.first) {
Chris@16 88 edge_t e = *p.first;
Chris@16 89 BOOST_CHECK(source(e, g) == u);
Chris@16 90 BOOST_CHECK(container_contains(adj, target(e, g)) == true);
Chris@16 91 }
Chris@16 92 }
Chris@16 93 }
Chris@16 94
Chris@16 95 void test_bidirectional_graph
Chris@16 96 (const std::vector<vertex_t>& vertex_set,
Chris@16 97 const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
Chris@16 98 const Graph& g)
Chris@16 99 {
Chris@16 100 typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
Chris@16 101 typedef typename std::vector< std::pair<vertex_t, vertex_t> >
Chris@16 102 ::const_iterator edge_iter;
Chris@16 103 typedef typename graph_traits<Graph>::in_edge_iterator in_edge_iter;
Chris@16 104
Chris@16 105 for (vertex_iter vi = vertex_set.begin(); vi != vertex_set.end(); ++vi) {
Chris@16 106 vertex_t v = *vi;
Chris@16 107 std::vector<vertex_t> inv_adj;
Chris@16 108 for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
Chris@16 109 if (e->second == v)
Chris@16 110 inv_adj.push_back(e->first);
Chris@16 111
Chris@16 112 std::pair<in_edge_iter, in_edge_iter> p = in_edges(v, g);
Chris@16 113 BOOST_CHECK(in_degree(v, g) == inv_adj.size());
Chris@16 114 BOOST_CHECK(deg_size_t(std::distance(p.first, p.second))
Chris@16 115 == in_degree(v, g));
Chris@16 116 for (; p.first != p.second; ++p.first) {
Chris@16 117 edge_t e = *p.first;
Chris@16 118 BOOST_CHECK(target(e, g) == v);
Chris@16 119 BOOST_CHECK(container_contains(inv_adj, source(e, g)) == true);
Chris@16 120 }
Chris@16 121 }
Chris@16 122 }
Chris@16 123
Chris@16 124 void test_adjacency_graph
Chris@16 125 (const std::vector<vertex_t>& vertex_set,
Chris@16 126 const std::vector< std::pair<vertex_t,vertex_t> >& edge_set,
Chris@16 127 const Graph& g)
Chris@16 128 {
Chris@16 129 typedef typename std::vector<vertex_t>::const_iterator vertex_iter;
Chris@16 130 typedef typename std::vector<std::pair<vertex_t,vertex_t> >
Chris@16 131 ::const_iterator edge_iter;
Chris@16 132 typedef typename graph_traits<Graph>::adjacency_iterator adj_iter;
Chris@16 133
Chris@16 134 for (vertex_iter ui = vertex_set.begin(); ui != vertex_set.end(); ++ui) {
Chris@16 135 vertex_t u = *ui;
Chris@16 136 std::vector<vertex_t> adj;
Chris@16 137 for (edge_iter e = edge_set.begin(); e != edge_set.end(); ++e)
Chris@16 138 if (e->first == u)
Chris@16 139 adj.push_back(e->second);
Chris@16 140
Chris@16 141 std::pair<adj_iter, adj_iter> p = adjacent_vertices(u, g);
Chris@16 142 BOOST_CHECK(deg_size_t(std::distance(p.first, p.second)) == adj.size());
Chris@16 143 for (; p.first != p.second; ++p.first) {
Chris@16 144 vertex_t v = *p.first;
Chris@16 145 BOOST_CHECK(container_contains(adj, v) == true);
Chris@16 146 }
Chris@16 147 }
Chris@16 148 }
Chris@16 149
Chris@16 150 void test_vertex_list_graph
Chris@16 151 (const std::vector<vertex_t>& vertex_set, const Graph& g)
Chris@16 152 {
Chris@16 153 typedef typename graph_traits<Graph>::vertex_iterator v_iter;
Chris@16 154 std::pair<v_iter, v_iter> p = vertices(g);
Chris@16 155 BOOST_CHECK(num_vertices(g) == vertex_set.size());
Chris@16 156 v_size_t n = (size_t)std::distance(p.first, p.second);
Chris@16 157 BOOST_CHECK(n == num_vertices(g));
Chris@16 158 for (; p.first != p.second; ++p.first) {
Chris@16 159 vertex_t v = *p.first;
Chris@16 160 BOOST_CHECK(container_contains(vertex_set, v) == true);
Chris@16 161 }
Chris@16 162 }
Chris@16 163
Chris@16 164 void test_edge_list_graph
Chris@16 165 (const std::vector<vertex_t>& vertex_set,
Chris@16 166 const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
Chris@16 167 const Graph& g)
Chris@16 168 {
Chris@16 169 typedef typename graph_traits<Graph>::edge_iterator e_iter;
Chris@16 170 std::pair<e_iter, e_iter> p = edges(g);
Chris@16 171 BOOST_CHECK(num_edges(g) == edge_set.size());
Chris@16 172 e_size_t m = std::distance(p.first, p.second);
Chris@16 173 BOOST_CHECK(m == num_edges(g));
Chris@16 174 for (; p.first != p.second; ++p.first) {
Chris@16 175 edge_t e = *p.first;
Chris@16 176 BOOST_CHECK(find_if(edge_set, connects(source(e, g), target(e, g), g)) != boost::end(edge_set));
Chris@16 177 BOOST_CHECK(container_contains(vertex_set, source(e, g)) == true);
Chris@16 178 BOOST_CHECK(container_contains(vertex_set, target(e, g)) == true);
Chris@16 179 }
Chris@16 180 }
Chris@16 181
Chris@16 182 void test_adjacency_matrix
Chris@16 183 (const std::vector<vertex_t>& vertex_set,
Chris@16 184 const std::vector< std::pair<vertex_t, vertex_t> >& edge_set,
Chris@16 185 const Graph& g)
Chris@16 186 {
Chris@16 187 std::pair<edge_t, bool> p;
Chris@16 188 for (typename std::vector<std::pair<vertex_t, vertex_t> >
Chris@16 189 ::const_iterator i = edge_set.begin();
Chris@16 190 i != edge_set.end(); ++i) {
Chris@16 191 p = edge(i->first, i->second, g);
Chris@16 192 BOOST_CHECK(p.second == true);
Chris@16 193 BOOST_CHECK(source(p.first, g) == i->first);
Chris@16 194 BOOST_CHECK(target(p.first, g) == i->second);
Chris@16 195 }
Chris@16 196 typename std::vector<vertex_t>::const_iterator j, k;
Chris@16 197 for (j = vertex_set.begin(); j != vertex_set.end(); ++j)
Chris@16 198 for (k = vertex_set.begin(); k != vertex_set.end(); ++k) {
Chris@16 199 p = edge(*j, *k, g);
Chris@16 200 if (p.second == true)
Chris@16 201 BOOST_CHECK(find_if(edge_set,
Chris@16 202 connects(source(p.first, g), target(p.first, g), g)) != boost::end(edge_set));
Chris@16 203 }
Chris@16 204 }
Chris@16 205
Chris@16 206 //=========================================================================
Chris@16 207 // Mutating Operations
Chris@16 208
Chris@16 209 void test_add_vertex(Graph& g)
Chris@16 210 {
Chris@16 211 Graph cpy;
Chris@16 212 std::vector<vertex_t> iso_vec(num_vertices(g));
Chris@16 213 IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
Chris@16 214 copy_graph(g, cpy, orig_to_copy(iso_map));
Chris@16 215
Chris@16 216 BOOST_CHECK((verify_isomorphism(g, cpy, iso_map)));
Chris@16 217
Chris@16 218 vertex_t v = add_vertex(g);
Chris@16 219
Chris@16 220 BOOST_CHECK(num_vertices(g) == num_vertices(cpy) + 1);
Chris@16 221
Chris@16 222 BOOST_CHECK(out_degree(v, g) == 0);
Chris@16 223
Chris@16 224 // Make sure the rest of the graph stayed the same
Chris@16 225 BOOST_CHECK((verify_isomorphism
Chris@16 226 (make_filtered_graph(g, keep_all(), ignore_vertex(v)), cpy,
Chris@16 227 iso_map)));
Chris@16 228 }
Chris@16 229
Chris@16 230 void test_add_edge(vertex_t u, vertex_t v, Graph& g)
Chris@16 231 {
Chris@16 232 Graph cpy;
Chris@16 233 std::vector<vertex_t> iso_vec(num_vertices(g));
Chris@16 234 IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
Chris@16 235 copy_graph(g, cpy, orig_to_copy(iso_map));
Chris@16 236
Chris@16 237 bool parallel_edge_exists = container_contains(adjacent_vertices(u, g), v);
Chris@16 238
Chris@16 239 std::pair<edge_t, bool> p = add_edge(u, v, g);
Chris@16 240 edge_t e = p.first;
Chris@16 241 bool added = p.second;
Chris@16 242
Chris@16 243 if (is_undirected(g) && u == v) // self edge
Chris@16 244 BOOST_CHECK(added == false);
Chris@16 245 else if (parallel_edge_exists)
Chris@16 246 BOOST_CHECK(allows_parallel_edges(g) && added == true
Chris@16 247 || !allows_parallel_edges(g) && added == false);
Chris@16 248 else
Chris@16 249 BOOST_CHECK(added == true);
Chris@16 250
Chris@16 251 if (p.second == true) { // edge added
Chris@16 252 BOOST_CHECK(num_edges(g) == num_edges(cpy) + 1);
Chris@16 253
Chris@16 254 BOOST_CHECK(container_contains(out_edges(u, g), e) == true);
Chris@16 255
Chris@16 256 BOOST_CHECK((verify_isomorphism
Chris@16 257 (make_filtered_graph(g, ignore_edge(e)), cpy, iso_map)));
Chris@16 258 }
Chris@16 259 else { // edge not added
Chris@16 260 if (! (is_undirected(g) && u == v)) {
Chris@16 261 // e should be a parallel edge
Chris@16 262 BOOST_CHECK(source(e, g) == u);
Chris@16 263 BOOST_CHECK(target(e, g) == v);
Chris@16 264 }
Chris@16 265 // The graph should not be changed.
Chris@16 266 BOOST_CHECK((verify_isomorphism(g, cpy, iso_map)));
Chris@16 267 }
Chris@16 268 } // test_add_edge()
Chris@16 269
Chris@16 270
Chris@16 271 void test_remove_edge(vertex_t u, vertex_t v, Graph& g)
Chris@16 272 {
Chris@16 273 Graph cpy;
Chris@16 274 std::vector<vertex_t> iso_vec(num_vertices(g));
Chris@16 275 IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
Chris@16 276 copy_graph(g, cpy, orig_to_copy(iso_map));
Chris@16 277
Chris@16 278 deg_size_t occurances = count(adjacent_vertices(u, g), v);
Chris@16 279
Chris@16 280 remove_edge(u, v, g);
Chris@16 281
Chris@16 282 BOOST_CHECK(num_edges(g) + occurances == num_edges(cpy));
Chris@16 283 BOOST_CHECK((verify_isomorphism
Chris@16 284 (g, make_filtered_graph(cpy, ignore_edges(u,v,cpy)),
Chris@16 285 iso_map)));
Chris@16 286 }
Chris@16 287
Chris@16 288 void test_remove_edge(edge_t e, Graph& g)
Chris@16 289 {
Chris@16 290 Graph cpy;
Chris@16 291 std::vector<vertex_t> iso_vec(num_vertices(g));
Chris@16 292 IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
Chris@16 293 copy_graph(g, cpy, orig_to_copy(iso_map));
Chris@16 294
Chris@16 295 vertex_t u = source(e, g), v = target(e, g);
Chris@16 296 deg_size_t occurances = count(adjacent_vertices(u, g), v);
Chris@16 297
Chris@16 298 remove_edge(e, g);
Chris@16 299
Chris@16 300 BOOST_CHECK(num_edges(g) + 1 == num_edges(cpy));
Chris@16 301 BOOST_CHECK(count(adjacent_vertices(u, g), v) + 1 == occurances);
Chris@16 302 BOOST_CHECK((verify_isomorphism
Chris@16 303 (g, make_filtered_graph(cpy, ignore_edge(e)),
Chris@16 304 iso_map)));
Chris@16 305 }
Chris@16 306
Chris@16 307 void test_clear_vertex(vertex_t v, Graph& g)
Chris@16 308 {
Chris@16 309 Graph cpy;
Chris@16 310 std::vector<vertex_t> iso_vec(num_vertices(g));
Chris@16 311 IsoMap iso_map(iso_vec.begin(), get(vertex_index, g));
Chris@16 312 copy_graph(g, cpy, orig_to_copy(iso_map));
Chris@16 313
Chris@16 314 clear_vertex(v, g);
Chris@16 315
Chris@16 316 BOOST_CHECK(out_degree(v, g) == 0);
Chris@16 317 BOOST_CHECK(num_vertices(g) == num_vertices(cpy));
Chris@16 318 BOOST_CHECK((verify_isomorphism
Chris@16 319 (g, make_filtered_graph(cpy, keep_all(), ignore_vertex(v)),
Chris@16 320 iso_map)));
Chris@16 321 }
Chris@16 322
Chris@16 323 //=========================================================================
Chris@16 324 // Property Map
Chris@16 325
Chris@16 326 template <typename PropVal, typename PropertyTag>
Chris@16 327 void test_readable_vertex_property_graph
Chris@16 328 (const std::vector<PropVal>& vertex_prop, PropertyTag tag, const Graph& g)
Chris@16 329 {
Chris@16 330 typedef typename property_map<Graph, PropertyTag>::const_type const_Map;
Chris@16 331 const_Map pmap = get(tag, g);
Chris@16 332 typename std::vector<PropVal>::const_iterator i = vertex_prop.begin();
Chris@16 333
Chris@16 334 for (typename boost::graph_traits<Graph>::vertex_iterator
Chris@16 335 bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second;
Chris@16 336 bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9)
Chris@16 337 for (typename boost::graph_traits<Graph>::vertex_descriptor v;
Chris@16 338 bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false;
Chris@16 339 ++bgl_first_9) {
Chris@16 340 //BGL_FORALL_VERTICES_T(v, g, Graph) {
Chris@16 341 typename property_traits<const_Map>::value_type
Chris@16 342 pval1 = get(pmap, v), pval2 = get(tag, g, v);
Chris@16 343 BOOST_CHECK(pval1 == pval2);
Chris@16 344 BOOST_CHECK(pval1 == *i++);
Chris@16 345 }
Chris@16 346 }
Chris@16 347
Chris@16 348 template <typename PropVal, typename PropertyTag>
Chris@16 349 void test_vertex_property_graph
Chris@16 350 (const std::vector<PropVal>& vertex_prop, PropertyTag tag, Graph& g)
Chris@16 351 {
Chris@16 352 typedef typename property_map<Graph, PropertyTag>::type PMap;
Chris@16 353 PMap pmap = get(tag, g);
Chris@16 354 typename std::vector<PropVal>::const_iterator i = vertex_prop.begin();
Chris@16 355 for (typename boost::graph_traits<Graph>::vertex_iterator
Chris@16 356 bgl_first_9 = vertices(g).first, bgl_last_9 = vertices(g).second;
Chris@16 357 bgl_first_9 != bgl_last_9; bgl_first_9 = bgl_last_9)
Chris@16 358 for (typename boost::graph_traits<Graph>::vertex_descriptor v;
Chris@16 359 bgl_first_9 != bgl_last_9 ? (v = *bgl_first_9, true) : false;
Chris@16 360 ++bgl_first_9)
Chris@16 361 // BGL_FORALL_VERTICES_T(v, g, Graph)
Chris@16 362 put(pmap, v, *i++);
Chris@16 363
Chris@16 364 test_readable_vertex_property_graph(vertex_prop, tag, g);
Chris@16 365
Chris@16 366 BGL_FORALL_VERTICES_T(v, g, Graph)
Chris@16 367 put(pmap, v, vertex_prop[0]);
Chris@16 368
Chris@16 369 typename std::vector<PropVal>::const_iterator j = vertex_prop.begin();
Chris@16 370 BGL_FORALL_VERTICES_T(v, g, Graph)
Chris@16 371 put(tag, g, v, *j++);
Chris@16 372
Chris@16 373 test_readable_vertex_property_graph(vertex_prop, tag, g);
Chris@16 374 }
Chris@16 375
Chris@16 376
Chris@16 377 };
Chris@16 378
Chris@16 379
Chris@16 380 } // namespace boost
Chris@16 381
Chris@16 382 #include <boost/graph/iteration_macros_undef.hpp>
Chris@16 383
Chris@16 384 #endif // BOOST_GRAPH_TEST_HPP