graph_test.hpp 14 KB

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