test_direction.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_DIRECTION_HPP
  7. #define TEST_DIRECTION_HPP
  8. #include <algorithm>
  9. #include <boost/range.hpp>
  10. #include <boost/concept/assert.hpp>
  11. /** @name Test Out-Directed Graph
  12. * Test all graphs that have directed out edges.
  13. */
  14. //@{
  15. template <typename Graph, typename VertexSet>
  16. void test_outdirected_graph(Graph const& g, VertexSet const& verts, boost::mpl::true_) {
  17. using namespace boost;
  18. BOOST_CONCEPT_ASSERT((IncidenceGraphConcept<Graph>));
  19. std::cout << "...test_outdirected_graph\n";
  20. typedef typename graph_traits<Graph>::out_edge_iterator OutIter;
  21. typedef std::pair<OutIter, OutIter> OutRange;
  22. typedef std::vector<OutRange> OutSet;
  23. // Collect all of the out edge ranges from the graph.
  24. OutSet outs(verts.size());
  25. for(size_t i = 0; i < verts.size(); ++i) {
  26. outs[i] = out_edges(verts[i], g);
  27. }
  28. BOOST_ASSERT(distance(outs[0]) == 0);
  29. BOOST_ASSERT(distance(outs[1]) == 1);
  30. BOOST_ASSERT(distance(outs[2]) == 1);
  31. BOOST_ASSERT(distance(outs[3]) == 2);
  32. BOOST_ASSERT(distance(outs[4]) == 2);
  33. BOOST_ASSERT(distance(outs[5]) == 1);
  34. // Verify that the edges are actually correct.
  35. // TODO: Find a better way of testing connectivity with multiple edges.
  36. BOOST_ASSERT(has_target(g, *outs[1].first, verts[0]));
  37. BOOST_ASSERT(has_target(g, *outs[2].first, verts[1]));
  38. // BOOST_ASSERT(has_target(g, *outs[3].first++, verts[4]));
  39. // BOOST_ASSERT(has_target(g, *outs[3].first, verts[2]));
  40. // BOOST_ASSERT(has_target(g, *outs[3].first++, verts[4]));
  41. // BOOST_ASSERT(has_target(g, *outs[3].first, verts[2]));
  42. BOOST_ASSERT(has_target(g, *outs[5].first, verts[3]));
  43. }
  44. template <typename Graph, typename VertexSet>
  45. void test_outdirected_graph(Graph const&, VertexSet const&, boost::mpl::false_)
  46. { }
  47. //@}
  48. /** @name Test In-Directed Graph
  49. * Test all graphs that support in-directed edges.
  50. */
  51. //@{
  52. template <typename Graph, typename VertexSet>
  53. void test_indirected_graph(Graph const& g, VertexSet const& verts, boost::mpl::true_) {
  54. using namespace boost;
  55. BOOST_CONCEPT_ASSERT((BidirectionalGraphConcept<Graph>));
  56. std::cout << "...test_indirected_graph\n";
  57. typedef typename graph_traits<Graph>::in_edge_iterator InIter;
  58. typedef std::pair<InIter, InIter> InRange;
  59. typedef std::vector<InRange> InSet;
  60. // Collect all of the in edges from the graph.
  61. InSet ins(verts.size());
  62. for(size_t i = 0; i < verts.size(); ++i) {
  63. ins[i] = in_edges(verts[i], g);
  64. }
  65. BOOST_ASSERT(distance(ins[0]) == 2);
  66. BOOST_ASSERT(distance(ins[1]) == 2);
  67. BOOST_ASSERT(distance(ins[2]) == 1);
  68. BOOST_ASSERT(distance(ins[3]) == 1);
  69. BOOST_ASSERT(distance(ins[4]) == 1);
  70. BOOST_ASSERT(distance(ins[5]) == 0);
  71. // Verify that the connected edges are correct.
  72. // TODO: Find a better way of testing connectivity with multiple edges.
  73. BOOST_ASSERT(has_source(g, *ins[2].first, verts[3]));
  74. BOOST_ASSERT(has_source(g, *ins[3].first, verts[5]));
  75. BOOST_ASSERT(has_source(g, *ins[4].first, verts[3]));
  76. }
  77. template <typename Graph, typename VertexSet>
  78. void test_indirected_graph(Graph const&, VertexSet const&, boost::mpl::false_)
  79. { }
  80. //@}
  81. /** @name Undirected Graphs
  82. * Test all graphs that have undirected edges.
  83. */
  84. template <typename Graph, typename VertexSet>
  85. void test_undirected_graph(Graph const& g, VertexSet const& verts, boost::mpl::true_) {
  86. using namespace boost;
  87. BOOST_CONCEPT_ASSERT((IncidenceGraphConcept<Graph>));
  88. std::cout << "...test_undirected_graph\n";
  89. typedef typename graph_traits<Graph>::out_edge_iterator OutIter;
  90. typedef std::pair<OutIter, OutIter> OutRange;
  91. typedef std::vector<OutRange> OutSet;
  92. // The set of out edges is the same as the set of incident edges.
  93. OutSet outs(verts.size());
  94. for(size_t i = 0; i < verts.size(); ++i) {
  95. outs[i] = out_edges(verts[i], g);
  96. }
  97. // TODO: Actually test the end connections to ensure that these are
  98. // definitely correct.
  99. BOOST_ASSERT(distance(outs[0]) == 2);
  100. BOOST_ASSERT(distance(outs[1]) == 3);
  101. BOOST_ASSERT(distance(outs[2]) == 2);
  102. BOOST_ASSERT(distance(outs[3]) == 3);
  103. BOOST_ASSERT(distance(outs[4]) == 3);
  104. BOOST_ASSERT(distance(outs[5]) == 1);
  105. }
  106. template <typename Graph, typename VertexSet>
  107. void test_undirected_graph(Graph const&, VertexSet const&, boost::mpl::false_)
  108. { }
  109. //@}
  110. #endif