1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
//=======================================================================
// Copyright 2002 Indiana University.
// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//=======================================================================
#include <boost/config.hpp>
#include <iostream>
#include <vector>
#include <set>
#include <utility>
#include <algorithm>
#define VERBOSE 0
#include <boost/utility.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/graph/random.hpp>
#include <boost/pending/indirect_cmp.hpp>
#include <boost/random/mersenne_twister.hpp>
enum vertex_id_t
{
vertex_id = 500
};
enum edge_id_t
{
edge_id = 501
};
namespace boost
{
BOOST_INSTALL_PROPERTY(vertex, id);
BOOST_INSTALL_PROPERTY(edge, id);
}
#include "graph_type.hpp" // this provides a typedef for Graph
using namespace boost;
/*
This program tests models of the MutableGraph concept.
*/
using std::cerr;
using std::cout;
using std::endl;
using std::find;
template < class Graph, class Vertex, class ID >
bool check_vertex_cleared(Graph& g, Vertex v, ID id)
{
typename graph_traits< Graph >::vertex_iterator vi, viend;
for (boost::tie(vi, viend) = vertices(g); vi != viend; ++vi)
{
typename graph_traits< Graph >::adjacency_iterator ai, aiend, found;
boost::tie(ai, aiend) = adjacent_vertices(*vi, g);
boost::indirect_cmp< ID, std::equal_to< std::size_t > > cmp(id);
#if (defined(BOOST_MSVC) && BOOST_MSVC <= 1300) && defined(__SGI_STL_PORT)
// seeing internal compiler errors when using std::find_if()
found = aiend;
for (; ai != aiend; ++ai)
if (cmp(*ai, v))
{
found = ai;
break;
}
#elif defined(BOOST_NO_CXX98_BINDERS)
found
= std::find_if(ai, aiend, std::bind(cmp, v, std::placeholders::_1));
#else
found = std::find_if(ai, aiend, std::bind1st(cmp, v));
#endif
if (found != aiend)
{
#if VERBOSE
std::cerr << "should not have found vertex " << id[*found]
<< std::endl;
#endif
return false;
}
}
return true;
}
template < class Graph, class Edge, class EdgeID >
bool check_edge_added(Graph& g, Edge e,
typename graph_traits< Graph >::vertex_descriptor a,
typename graph_traits< Graph >::vertex_descriptor b, EdgeID edge_id,
std::size_t correct_id, bool inserted)
{
if (!(source(e, g) == a))
{
#if VERBOSE
cerr << " Failed, vertex a not source of e." << endl;
#endif
return false;
}
else if (!(target(e, g) == b))
{
#if VERBOSE
cerr << " Failed, vertex b not source of e." << endl;
#endif
return false;
}
else if (!is_adjacent(g, a, b))
{
#if VERBOSE
cerr << " Failed, not adj." << endl;
#endif
return false;
}
else if (!in_edge_set(g, e))
{
#if VERBOSE
cerr << " Failed, not in edge set." << endl;
#endif
return false;
}
else if (inserted && edge_id[e] != correct_id)
{
#if VERBOSE
cerr << " Failed, invalid edge property." << endl;
#endif
return false;
}
else if (!inserted && edge_id[e] != edge_id[edge(a, b, g).first])
{
#if VERBOSE
cerr << " Failed, invalid edge property." << endl;
#endif
return false;
}
else if (num_edges(g) != count_edges(g))
{
#if VERBOSE
cerr << " Failed, invalid number of edges." << endl;
#endif
return false;
}
return true;
}
template < class Graph > std::size_t count_edges(Graph& g)
{
std::size_t e = 0;
typename boost::graph_traits< Graph >::edge_iterator ei, ei_end;
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei)
++e;
return e;
}
int main(int, char*[])
{
int ret = 0;
std::size_t N = 5, E = 0;
std::size_t old_N;
typedef ::Graph Graph;
Graph g;
typedef boost::graph_traits< Graph >::vertex_descriptor Vertex;
typedef boost::graph_traits< Graph >::edge_descriptor Edge;
int i, j;
std::size_t current_vertex_id = 0;
std::size_t current_edge_id = 0;
bool is_failed = false;
property_map< Graph, vertex_id_t >::type vertex_id_map = get(vertex_id, g);
property_map< Graph, edge_id_t >::type edge_id_map = get(edge_id, g);
for (std::size_t k = 0; k < N; ++k)
add_vertex(current_vertex_id++, g);
// also need to test EdgeIterator graph constructor -JGS
mt19937 gen;
for (j = 0; j < 10; ++j)
{
// add_edge
#if VERBOSE
cerr << "Testing add_edge ..." << endl;
#endif
for (i = 0; i < 6; ++i)
{
Vertex a, b;
a = random_vertex(g, gen);
do
{
b = random_vertex(g, gen);
} while (a == b); // don't do self edges
#if VERBOSE
cerr << "add_edge(" << vertex_id_map[a] << "," << vertex_id_map[b]
<< ")" << endl;
#endif
Edge e;
bool inserted;
boost::tie(e, inserted) = add_edge(a, b, current_edge_id++, g);
#if VERBOSE
std::cout << "inserted: " << inserted << std::endl;
std::cout << "source(e,g)" << source(e, g) << endl;
std::cout << "target(e,g)" << target(e, g) << endl;
std::cout << "edge_id[e] = " << edge_id_map[e] << std::endl;
print_edges2(g, vertex_id_map, edge_id_map);
print_graph(g, vertex_id_map);
std::cout << "finished printing" << std::endl;
// print_in_edges(g, vertex_id_map);
#endif
if (!check_edge_added(
g, e, a, b, edge_id_map, current_edge_id - 1, inserted))
{
ret = -1;
break;
}
++E;
}
// remove_edge(u, v, g)
#if VERBOSE
cerr << "Testing remove_edge(u, v, g) ..." << endl;
is_failed = false;
#endif
for (i = 0; i < 2; ++i)
{
#if VERBOSE
print_edges(g, vertex_id_map);
#endif
Vertex a, b;
Edge e = random_edge(g, gen);
boost::tie(a, b) = boost::incident(e, g);
--E;
#if VERBOSE
cerr << "remove_edge(" << vertex_id_map[a] << ","
<< vertex_id_map[b] << ")" << endl;
#endif
remove_edge(a, b, g);
#if VERBOSE
print_graph(g, vertex_id_map);
// print_in_edges(g, vertex_id_map);
print_edges(g, vertex_id_map);
#endif
is_failed = is_failed || is_adjacent(g, a, b)
|| in_edge_set(g, a, b) || num_edges(g) != count_edges(g);
if (is_failed)
break;
}
if (is_failed)
{
ret = -1;
#if VERBOSE
cerr << " Failed." << endl;
#endif
}
else
{
#if VERBOSE
cerr << " Passed." << endl;
#endif
}
// remove_edge(e, g)
#if VERBOSE
cerr << "Testing remove_edge(e, g) ..." << endl;
is_failed = false;
#endif
for (i = 0; i < 2; ++i)
{
#if VERBOSE
print_edges(g, vertex_id_map);
#endif
Vertex a, b;
Edge e = random_edge(g, gen);
boost::tie(a, b) = boost::incident(e, g);
--E;
#if VERBOSE
cerr << "remove_edge(" << vertex_id_map[a] << ","
<< vertex_id_map[b] << ")" << endl;
#endif
graph_traits< Graph >::edges_size_type old_E = num_edges(g);
remove_edge(e, g);
#if VERBOSE
print_graph(g, vertex_id_map);
// print_in_edges(g, vertex_id_map);
print_edges(g, vertex_id_map);
#endif
is_failed = is_failed || old_E != num_edges(g) + 1
|| num_edges(g) != count_edges(g);
if (is_failed)
break;
}
if (is_failed)
{
ret = -1;
#if VERBOSE
cerr << " Failed." << endl;
#endif
}
else
{
#if VERBOSE
cerr << " Passed." << endl;
#endif
}
// add_vertex
#if VERBOSE
cerr << "Testing add_vertex ..." << endl;
is_failed = false;
#endif
old_N = num_vertices(g);
graph_traits< Graph >::vertex_descriptor vid = add_vertex(g),
vidp1 = add_vertex(g);
vertex_id_map[vid] = current_vertex_id++;
vertex_id_map[vidp1] = current_vertex_id++;
#if VERBOSE
print_vertices(g, vertex_id_map);
print_graph(g, vertex_id_map);
// print_in_edges(g,vertex_id_map);
print_edges(g, vertex_id_map);
#endif
// make sure the two added vertices are in the graph's vertex set
{
if (!in_vertex_set(g, vid))
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vid]
<< " not in vertices(g)" << endl;
#endif
ret = -1;
break;
}
if (!in_vertex_set(g, vidp1))
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vidp1]
<< " not in vertices(g)" << endl;
#endif
ret = -1;
break;
}
}
// make sure the vertices do not have any out edges yet
{
graph_traits< Graph >::out_edge_iterator e, e_end;
boost::tie(e, e_end) = out_edges(vid, g);
if (e != e_end)
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vid]
<< " should not have any out-edges yet" << endl;
#endif
ret = -1;
break;
}
boost::tie(e, e_end) = out_edges(vidp1, g);
if (e != e_end)
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vidp1]
<< " should not have any out-edges yet" << endl;
#endif
ret = -1;
break;
}
}
// make sure the vertices do not yet appear in any of the edges
{
graph_traits< Graph >::edge_iterator e, e_end;
for (boost::tie(e, e_end) = edges(g); e != e_end; ++e)
{
if (source(*e, g) == vid || target(*e, g) == vid)
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vid]
<< " should not have any edges" << endl;
#endif
ret = -1;
break;
}
if (source(*e, g) == vidp1 || target(*e, g) == vidp1)
{
#if VERBOSE
cerr << " Failed, " << vertex_id_map[vidp1]
<< " should not have any edges" << endl;
#endif
ret = -1;
break;
}
}
}
// Make sure num_vertices(g) has been updated
N = num_vertices(g);
if ((N - 2) != old_N)
{
ret = -1;
#if VERBOSE
cerr << " Failed. N = " << N << " but should be " << old_N + 2
<< endl;
#endif
break;
}
else
{
#if VERBOSE
cerr << " Passed." << endl;
#endif
}
// add_edge again
#if VERBOSE
cerr << "Testing add_edge after add_vertex ..." << endl;
is_failed = false;
#endif
for (i = 0; i < 2; ++i)
{
Vertex a = random_vertex(g, gen), b = random_vertex(g, gen);
while (a == vid)
a = random_vertex(g, gen);
while (b == vidp1)
b = random_vertex(g, gen);
Edge e;
bool inserted;
#if VERBOSE
cerr << "add_edge(" << vertex_id_map[vid] << "," << vertex_id_map[a]
<< ")" << endl;
#endif
boost::tie(e, inserted)
= add_edge(vid, a, EdgeID(current_edge_id++), g);
if (!check_edge_added(
g, e, vid, a, edge_id_map, current_edge_id - 1, inserted))
{
ret = -1;
break;
}
#if VERBOSE
cerr << "add_edge(" << vertex_id_map[b] << ","
<< vertex_id_map[vidp1] << ")" << endl;
#endif
// add_edge without plugin
boost::tie(e, inserted) = add_edge(b, vidp1, g);
if (inserted)
edge_id_map[e] = current_edge_id;
++current_edge_id;
if (!check_edge_added(
g, e, b, vidp1, edge_id_map, current_edge_id - 1, inserted))
{
ret = -1;
break;
}
}
// clear_vertex
Vertex c = random_vertex(g, gen);
#if VERBOSE
cerr << "Testing clear vertex ..." << endl;
is_failed = false;
print_graph(g, vertex_id_map);
// print_in_edges(g, vertex_id_map);
cerr << "clearing vertex " << vertex_id_map[c] << endl;
#endif
clear_vertex(c, g);
#if VERBOSE
print_graph(g, vertex_id_map);
// print_in_edges(g, vertex_id_map);
print_edges(g, vertex_id_map);
#endif
if (check_vertex_cleared(g, c, vertex_id_map)
&& num_edges(g) == count_edges(g))
{
#if VERBOSE
cerr << " Passed." << endl;
#endif
}
else
{
#if VERBOSE
cerr << "**** Failed" << endl;
#endif
ret = -1;
break;
}
#if VERBOSE
cerr << "Testing remove vertex ..." << endl;
is_failed = false;
cerr << "removing vertex " << vertex_id_map[c] << endl;
#endif
old_N = num_vertices(g);
remove_vertex(c, g);
#if VERBOSE
print_graph(g, vertex_id_map);
// print_in_edges(g,vertex_id_map);
print_edges(g, vertex_id_map);
#endif
// can't check in_vertex_set here because the vertex_descriptor c
// is no longer valid, we'll just make sure the vertex set has
// one fewer vertex
{
graph_traits< Graph >::vertex_iterator v, v_end;
boost::tie(v, v_end) = vertices(g);
for (N = 0; v != v_end; ++v)
++N; // N = std::distance(v, v_end);
if (N != old_N - 1)
{
ret = -1;
#if VERBOSE
cerr << " Failed. N = " << N << " but should be "
<< old_N - 1 << endl;
#endif
}
}
N = num_vertices(g);
if (N != old_N - 1)
{
ret = -1;
#if VERBOSE
cerr << " Failed. N = " << N << " but should be " << old_N - 1
<< endl;
#endif
}
else
{
#if VERBOSE
cerr << " Passed." << endl;
#endif
}
}
if (ret == 0)
std::cout << "tests passed" << std::endl;
return ret;
}
|