test_construction.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_CONSTRUCTION_HPP
  7. #define TEST_CONSTRUCTION_HPP
  8. #include <boost/concept/assert.hpp>
  9. #include <utility>
  10. /** @name Build Graph
  11. * Build the standard graph structure used in the remaining tests. Depending
  12. * on the mutability traits of the graph G, this may or may not add N vertices
  13. * to graph. The Add and Label type parameters are used to dispatch a build
  14. * method for normal or labeled graphs.
  15. */
  16. //@{
  17. // This will basically catch adjacency matrices, which don't get built.
  18. template <typename Graph, typename Add, typename Label>
  19. void build_graph(Graph& g, Add, Label)
  20. { }
  21. // This matches MutableGraph, so just add some vertices.
  22. template <typename Graph>
  23. void build_graph(Graph& g, boost::mpl::true_, boost::mpl::false_) {
  24. using namespace boost;
  25. BOOST_CONCEPT_ASSERT((VertexListGraphConcept<Graph>));
  26. BOOST_CONCEPT_ASSERT((VertexMutableGraphConcept<Graph>));
  27. std::cout << "...build_normal\n";
  28. for(std::size_t i = 0; i < N; ++i) {
  29. add_vertex(g);
  30. }
  31. BOOST_ASSERT(num_vertices(g) == N);
  32. }
  33. // This will match labeled graphs.
  34. template <typename Graph>
  35. void build_graph(Graph& g, boost::mpl::false_, boost::mpl::true_) {
  36. using namespace boost;
  37. BOOST_CONCEPT_ASSERT((VertexListGraphConcept<Graph>));
  38. // BOOST_CONCEPT_ASSERT((VertexMutableGraphConcept<Graph>));
  39. std::cout << "...build_labeled\n";
  40. // Add each vertex labeled with the number i.
  41. for(std::size_t i = 0; i < N; ++i) {
  42. add_vertex(i, g);
  43. }
  44. BOOST_ASSERT(num_vertices(g) == N);
  45. }
  46. //@}
  47. /** @name Build Mutable
  48. * For mutable property graphs, try to add a vertex with a property. This test
  49. * actually builds a new graph - or at least tries to. We're not testing for
  50. * labeled graphs since that's actually done in build_graph above.
  51. */
  52. //@{
  53. template <typename Graph, typename Add, typename Label>
  54. void build_property_graph(Graph const& g, Add, Label)
  55. { }
  56. template <typename Graph>
  57. void build_property_graph(Graph const&, boost::mpl::true_, boost::mpl::false_) {
  58. using namespace boost;
  59. BOOST_CONCEPT_ASSERT((VertexMutablePropertyGraphConcept<Graph>));
  60. typedef typename vertex_property_type<Graph>::type VertexProp;
  61. std::cout << "...build mutable\n";
  62. // Start with clean graph. Nothing really to assert. Just make sure it
  63. // copmpiles.
  64. Graph h;
  65. add_vertex(VertexProp(), h);
  66. }
  67. //@}
  68. /** @name Connect Graph
  69. * Given a constructed graph, connect the edges to create a the standard
  70. * testing graph. To facilitate ease of use, we pass a vector of vertices
  71. * along with the graph such that v[0] -> *vertices(g).first, etc. The
  72. * Labeled type parameter is used to dispatch connection techniques for
  73. * normal or labled graphs.
  74. */
  75. //@{
  76. template <typename Graph, typename VertexSet>
  77. void connect_graph(Graph& g, VertexSet const& verts, boost::mpl::false_) {
  78. using namespace boost;
  79. BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept<Graph>));
  80. BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept<Graph>));
  81. std::cout << "...connect_normal\n";
  82. Pair *f, *l;
  83. for(boost::tie(f, l) = edge_pairs(); f != l; ++f) {
  84. Pair const& e = *f;
  85. add_edge(verts[e.first], verts[e.second], g);
  86. }
  87. // Is the lollipop connected? Is the lollipop not connected to the roof?
  88. BOOST_ASSERT(edge(verts[5], verts[3], g).second == true);
  89. BOOST_ASSERT(edge(verts[5], verts[0], g).second == false);
  90. }
  91. template <typename Graph, typename VertexSet>
  92. void connect_graph(Graph& g, VertexSet const& verts, boost::mpl::true_) {
  93. using namespace boost;
  94. BOOST_CONCEPT_ASSERT((AdjacencyMatrixConcept<Graph>));
  95. // BOOST_CONCEPT_ASSERT((EdgeMutableGraphConcept<Graph>));
  96. std::cout << "...connect_labeled\n";
  97. // With labeled graphs, we want to operate directly on the edge numbers
  98. // rather than looking up the correct vertex index. This is because the
  99. // vertices are already mapped to indices.
  100. Pair* p = edge_pairs().first;
  101. for(std::size_t i = 0; i < M; ++i) {
  102. Pair const& e = p[i];
  103. add_edge_by_label(e.first, e.second, g);
  104. }
  105. // Is the lollipop connected?
  106. BOOST_ASSERT(edge_by_label(5, 3, g).second == true);
  107. BOOST_ASSERT(edge_by_label(5, 0, g).second == false);
  108. }
  109. //@}
  110. #endif