test_graph.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // (C) Copyright 2009 Andrew Sutton
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef TEST_GRAPH_HPP
  7. #define TEST_GRAPH_HPP
  8. /** @file test_graph.hpp
  9. * This file houses the generic graph testing framework, which is essentially
  10. * run using the test_graph function. This function is called, passing a
  11. * graph object that be constructed and exercised according to the concepts
  12. * that the graph models. This test is extensively metaprogrammed in order to
  13. * differentiate testable features of graph instances.
  14. */
  15. #include "typestr.hpp"
  16. #include <utility>
  17. #include <vector>
  18. #include <boost/assert.hpp>
  19. #include <boost/concept/assert.hpp>
  20. #include <boost/graph/graph_concepts.hpp>
  21. #include <boost/graph/graph_traits.hpp>
  22. #include <boost/graph/graph_mutability_traits.hpp>
  23. #include <boost/graph/labeled_graph.hpp>
  24. #define BOOST_META_ASSERT(x) BOOST_ASSERT(x::value)
  25. typedef std::pair<std::size_t, std::size_t> Pair;
  26. static const std::size_t N = 6;
  27. static const std::size_t M = 7;
  28. // A helper function that globally defines the graph being constructed. Note
  29. // that this graph shown here: http://en.wikipedia.org/wiki/Graph_theory.
  30. std::pair<Pair*, Pair*> edge_pairs() {
  31. static Pair pairs[] = {
  32. Pair(5, 3), Pair(3, 4), Pair(3, 2), Pair(4, 0), Pair(4, 1),
  33. Pair(2, 1), Pair(1, 0)
  34. };
  35. Pair* f = &pairs[0];
  36. Pair* l = f + M;
  37. return std::make_pair(f, l);
  38. }
  39. // Return true if the vertex v is the target of the edge e.
  40. template <typename Graph, typename Edge, typename Vertex>
  41. bool has_target(Graph const& g, Edge e, Vertex v)
  42. { return boost::target(e, g) == v; }
  43. // Return true if the vertex v is the source of the edge e.
  44. template <typename Graph, typename Edge, typename Vertex>
  45. bool has_source(Graph const& g, Edge e, Vertex v)
  46. { return boost::source(e, g) == v; }
  47. /** @name Property Bundles
  48. * Support testing with bundled properties. Note that the vertex bundle and
  49. * edge bundle MAY NOT be the same if you want to use the property map type
  50. * generator to define property maps.
  51. */
  52. //@{
  53. // This is really just a place holder to make sure that bundled graph
  54. // properties actually work. There are no semantics to this type.
  55. struct GraphBundle {
  56. int value;
  57. };
  58. struct VertexBundle {
  59. VertexBundle() : value() { }
  60. VertexBundle(int n) : value(n) { }
  61. bool operator==(VertexBundle const& x) const { return value == x.value; }
  62. bool operator<(VertexBundle const& x) const { return value < x.value; }
  63. int value;
  64. };
  65. struct EdgeBundle {
  66. EdgeBundle() : value() { }
  67. EdgeBundle(int n) : value(n) { }
  68. bool operator==(EdgeBundle const& x) const { return value == x.value; }
  69. bool operator<(EdgeBundle const& x) const { return value < x.value; }
  70. int value;
  71. };
  72. //@}
  73. #include "test_construction.hpp"
  74. #include "test_destruction.hpp"
  75. #include "test_iteration.hpp"
  76. #include "test_direction.hpp"
  77. #include "test_properties.hpp"
  78. template <typename Graph>
  79. void test_graph(Graph& g) {
  80. using namespace boost;
  81. BOOST_CONCEPT_ASSERT((GraphConcept<Graph>));
  82. std::cout << typestr(g) << "\n";
  83. // Define a bunch of tags for the graph.
  84. typename graph_has_add_vertex<Graph>::type can_add_vertex;
  85. typename graph_has_remove_vertex<Graph>::type can_remove_vertex;
  86. typename is_labeled_graph<Graph>::type is_labeled;
  87. typename is_directed_unidirectional_graph<Graph>::type is_directed;
  88. typename is_directed_bidirectional_graph<Graph>::type is_bidirectional;
  89. typename is_undirected_graph<Graph>::type is_undirected;
  90. // Test constrution and vertex list.
  91. build_graph(g, can_add_vertex, is_labeled);
  92. build_property_graph(g, can_add_vertex, is_labeled);
  93. test_vertex_list_graph(g);
  94. // Collect the vertices for an easy method of "naming" them.
  95. typedef typename graph_traits<Graph>::vertex_descriptor Vertex;
  96. typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;
  97. std::vector<Vertex> verts;
  98. std::pair<VertexIterator, VertexIterator> rng = vertices(g);
  99. for( ; rng.first != rng.second; ++rng.first) {
  100. verts.push_back(*rng.first);
  101. }
  102. // Test connection and edge list
  103. connect_graph(g, verts, is_labeled);
  104. // connect_property_graph(g, verts, is_labeld);
  105. test_edge_list_graph(g);
  106. // Test properties
  107. test_properties(g, verts);
  108. // Test directionality.
  109. test_outdirected_graph(g, verts, is_directed);
  110. test_indirected_graph(g, verts, is_bidirectional);
  111. test_undirected_graph(g, verts, is_undirected);
  112. // Test disconnection
  113. disconnect_graph(g, verts, is_labeled);
  114. destroy_graph(g, verts, can_remove_vertex, is_labeled);
  115. }
  116. #endif