subgraph_add.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /* This file is a boost.test unit test and provides tests the internal dependency graph
  2. *
  3. * Created on: 06.10.2015
  4. * Author: Stefan Hammer <s.hammer@univie.ac.at>
  5. * License: 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. *
  10. */
  11. #define BOOST_TEST_MODULE subgraph_add
  12. // std lib includes
  13. #include <iostream>
  14. // include boost components
  15. #include <boost/test/unit_test.hpp>
  16. #include <boost/graph/adjacency_list.hpp>
  17. #include <boost/graph/iteration_macros.hpp>
  18. // include header
  19. #include <boost/graph/subgraph.hpp>
  20. using namespace boost;
  21. BOOST_AUTO_TEST_CASE(simpleGraph) {
  22. BOOST_TEST_MESSAGE("simple subgraph");
  23. typedef subgraph< adjacency_list< vecS, vecS, directedS,
  24. no_property, property< edge_index_t, int > > > Graph;
  25. const int N = 6;
  26. Graph G0(N);
  27. enum { A, B, C, D, E, F}; // for conveniently refering to vertices in G0
  28. Graph& G1 = G0.create_subgraph();
  29. Graph& G2 = G1.create_subgraph();
  30. BOOST_CHECK(&G1.parent() == &G0);
  31. BOOST_CHECK(&G2.parent() == &G1);
  32. enum { A1, B1, C1 }; // for conveniently refering to vertices in G1
  33. enum { A2, B2, C2 }; // for conveniently refering to vertices in G2
  34. add_vertex(C, G1); // global vertex C becomes local A1 for G1
  35. add_vertex(E, G1); // global vertex E becomes local B1 for G1
  36. add_vertex(F, G1); // global vertex F becomes local C1 for G1
  37. add_vertex(C, G2); // global vertex C becomes local A2 for G2
  38. add_vertex(E, G2); // global vertex E becomes local B2 for G2
  39. BOOST_CHECK(num_vertices(G0) == 6);
  40. BOOST_CHECK(num_vertices(G1) == 3);
  41. std::cerr << num_vertices(G1) << std::endl;
  42. BOOST_CHECK(num_vertices(G2) == 2);
  43. // add edges to root graph
  44. add_edge(A, B, G0);
  45. add_edge(B, C, G0);
  46. add_edge(B, D, G0);
  47. add_edge(E, F, G0);
  48. BOOST_CHECK(num_edges(G0) == 4);
  49. BOOST_CHECK(num_edges(G1) == 1);
  50. BOOST_CHECK(num_edges(G2) == 0);
  51. // add edges to G1
  52. add_edge(A1, B1, G1);
  53. BOOST_CHECK(num_edges(G0) == 5);
  54. BOOST_CHECK(num_edges(G1) == 2);
  55. BOOST_CHECK(num_edges(G2) == 1);
  56. // num_vertices stays the same
  57. BOOST_CHECK(num_vertices(G0) == 6);
  58. BOOST_CHECK(num_vertices(G1) == 3);
  59. BOOST_CHECK(num_vertices(G2) == 2);
  60. }
  61. BOOST_AUTO_TEST_CASE(addVertices) {
  62. BOOST_TEST_MESSAGE("subgraph add edges");
  63. typedef subgraph< adjacency_list< vecS, vecS, directedS,
  64. no_property, property< edge_index_t, int > > > Graph;
  65. typedef Graph::vertex_descriptor Vertex;
  66. const int N = 3;
  67. Graph G0(N);
  68. Graph& G1 = G0.create_subgraph();
  69. Graph& G2 = G1.create_subgraph();
  70. BOOST_CHECK(&G1.parent() == &G0);
  71. BOOST_CHECK(&G2.parent() == &G1);
  72. // add vertices to G2
  73. Vertex n1 = add_vertex(0, G2);
  74. Vertex n2 = add_vertex(1, G2);
  75. // check if the global vertex 2 is equal to the returned local vertex
  76. if (G2.find_vertex(0).second) {
  77. BOOST_CHECK(G2.find_vertex(0).first == n1);
  78. } else {
  79. BOOST_ERROR( "vertex not found!" );
  80. }
  81. if (G2.find_vertex(1).second) {
  82. BOOST_CHECK(G2.find_vertex(1).first == n2);
  83. } else {
  84. BOOST_ERROR( "vertex not found!" );
  85. }
  86. // and check if this vertex is also present in G1
  87. if (G1.find_vertex(0).second) {
  88. BOOST_CHECK(G1.local_to_global(G1.find_vertex(0).first) == 0);
  89. } else {
  90. BOOST_ERROR( "vertex not found!" );
  91. }
  92. if (G1.find_vertex(0).second) {
  93. BOOST_CHECK(G1.local_to_global(G1.find_vertex(1).first) == 1);
  94. } else {
  95. BOOST_ERROR( "vertex not found!" );
  96. }
  97. // num_vertices stays the same
  98. BOOST_CHECK(num_vertices(G0) == 3);
  99. BOOST_CHECK(num_vertices(G1) == 2);
  100. BOOST_CHECK(num_vertices(G2) == 2);
  101. // add vertices to G1
  102. Vertex n3 = add_vertex(2, G1);
  103. // check if the global vertex 2 is equal to the returned local vertex
  104. if (G1.find_vertex(2).second) {
  105. BOOST_CHECK(G1.find_vertex(2).first == n3);
  106. } else {
  107. BOOST_ERROR( "vertex not found!" );
  108. }
  109. // num_vertices stays the same
  110. BOOST_CHECK(num_vertices(G0) == 3);
  111. BOOST_CHECK(num_vertices(G1) == 3);
  112. BOOST_CHECK(num_vertices(G2) == 2);
  113. // add vertices to G1
  114. Vertex n4 = add_vertex(G1);
  115. // check if the new local vertex is also in the global graph
  116. BOOST_CHECK(G0.find_vertex(G1.local_to_global(n4)).second);
  117. // check if the new local vertex is not in the subgraphs
  118. BOOST_CHECK(!G2.find_vertex(n4).second);
  119. // num_vertices stays the same
  120. BOOST_CHECK(num_vertices(G0) == 4);
  121. BOOST_CHECK(num_vertices(G1) == 4);
  122. BOOST_CHECK(num_vertices(G2) == 2);
  123. // add vertices to G0
  124. Vertex n5 = add_vertex(G0);
  125. // check if the new local vertex is not in the subgraphs
  126. BOOST_CHECK(!G1.find_vertex(n5).second);
  127. BOOST_CHECK(!G2.find_vertex(n5).second);
  128. // num_vertices stays the same
  129. BOOST_CHECK(num_vertices(G0) == 5);
  130. BOOST_CHECK(num_vertices(G1) == 4);
  131. BOOST_CHECK(num_vertices(G2) == 2);
  132. typedef std::map<graph_traits<Graph::graph_type>::vertex_descriptor, graph_traits<Graph::graph_type>::vertex_descriptor>::iterator v_itr;
  133. std::cerr << "All G0 vertices: " << std::endl;
  134. for(v_itr v = G0.m_local_vertex.begin(); v != G0.m_local_vertex.end(); ++v) {
  135. std::cerr << G0.local_to_global(v->first) << std::endl;
  136. }
  137. std::cerr << "All G1 vertices: " << std::endl;
  138. for(v_itr v = G1.m_local_vertex.begin(); v != G1.m_local_vertex.end(); ++v) {
  139. std::cerr << G1.local_to_global(v->first) << std::endl;
  140. }
  141. std::cerr << "All G2 vertices: " << std::endl;
  142. for(v_itr v = G2.m_local_vertex.begin(); v != G2.m_local_vertex.end(); ++v) {
  143. std::cerr << G2.local_to_global(v->first) << std::endl;
  144. }
  145. }
  146. BOOST_AUTO_TEST_CASE(addEdge) {
  147. BOOST_TEST_MESSAGE("subgraph add edges");
  148. typedef subgraph< adjacency_list< vecS, vecS, directedS,
  149. no_property, property< edge_index_t, int > > > Graph;
  150. typedef Graph::vertex_descriptor Vertex;
  151. const int N = 3;
  152. Graph G0(N);
  153. Graph& G1 = G0.create_subgraph();
  154. Graph& G2 = G1.create_subgraph();
  155. BOOST_CHECK(&G1.parent() == &G0);
  156. BOOST_CHECK(&G2.parent() == &G1);
  157. // add vertices
  158. add_vertex(0, G2);
  159. add_vertex(1, G2);
  160. BOOST_CHECK(num_vertices(G1) == 2);
  161. BOOST_CHECK(num_vertices(G2) == 2);
  162. // add edge to G0 which needs propagation
  163. add_edge(0, 1, G0);
  164. BOOST_CHECK(num_edges(G0) == 1);
  165. BOOST_CHECK(num_edges(G1) == 1);
  166. BOOST_CHECK(num_edges(G2) == 1);
  167. // num_vertices stays the same
  168. BOOST_CHECK(num_vertices(G0) == 3);
  169. BOOST_CHECK(num_vertices(G1) == 2);
  170. BOOST_CHECK(num_vertices(G2) == 2);
  171. // add edge to G0 without propagation
  172. add_edge(1, 2, G0);
  173. BOOST_CHECK(num_edges(G0) == 2);
  174. BOOST_CHECK(num_edges(G1) == 1);
  175. BOOST_CHECK(num_edges(G2) == 1);
  176. // num_vertices stays the same
  177. BOOST_CHECK(num_vertices(G0) == 3);
  178. BOOST_CHECK(num_vertices(G1) == 2);
  179. BOOST_CHECK(num_vertices(G2) == 2);
  180. // add vertex 2 to G2/G1 with edge propagation
  181. Vertex n = add_vertex(2, G2);
  182. BOOST_CHECK(G2.local_to_global(n) == 2);
  183. BOOST_CHECK(num_edges(G0) == 2);
  184. BOOST_CHECK(num_edges(G1) == 2);
  185. BOOST_CHECK(num_edges(G2) == 2);
  186. // num_vertices stays the same
  187. BOOST_CHECK(num_vertices(G0) == 3);
  188. BOOST_CHECK(num_vertices(G1) == 3);
  189. BOOST_CHECK(num_vertices(G2) == 3);
  190. // add edge to G2 with propagation upwards
  191. add_edge(0, 2, G2);
  192. BOOST_CHECK(num_edges(G0) == 3);
  193. BOOST_CHECK(num_edges(G1) == 3);
  194. BOOST_CHECK(num_edges(G2) == 3);
  195. // num_vertices stays the same
  196. BOOST_CHECK(num_vertices(G0) == 3);
  197. BOOST_CHECK(num_vertices(G1) == 3);
  198. BOOST_CHECK(num_vertices(G2) == 3);
  199. typedef std::map<graph_traits<Graph::graph_type>::vertex_descriptor, graph_traits<Graph::graph_type>::vertex_descriptor>::iterator v_itr;
  200. std::cerr << "All G0 vertices: " << std::endl;
  201. for(v_itr v = G0.m_local_vertex.begin(); v != G0.m_local_vertex.end(); ++v) {
  202. std::cerr << G0.local_to_global(v->first) << std::endl;
  203. }
  204. std::cerr << "All G1 vertices: " << std::endl;
  205. for(v_itr v = G1.m_local_vertex.begin(); v != G1.m_local_vertex.end(); ++v) {
  206. std::cerr << G1.local_to_global(v->first) << std::endl;
  207. }
  208. std::cerr << "All G2 vertices: " << std::endl;
  209. for(v_itr v = G2.m_local_vertex.begin(); v != G2.m_local_vertex.end(); ++v) {
  210. std::cerr << G2.local_to_global(v->first) << std::endl;
  211. }
  212. std::cerr << "All G0 edges: " << std::endl;
  213. BGL_FORALL_EDGES(e, G0, Graph) {
  214. std::cerr << source(e, G0) << "->" << target(e, G0) << std::endl;
  215. }
  216. std::cerr << "All G1 edges: " << std::endl;
  217. BGL_FORALL_EDGES(e, G1, Graph) {
  218. std::cerr << source(e, G1) << "->" << target(e, G1) << std::endl;
  219. }
  220. std::cerr << "All G2 edges: " << std::endl;
  221. BGL_FORALL_EDGES(e, G2, Graph) {
  222. std::cerr << source(e, G2) << "->" << target(e, G2) << std::endl;
  223. }
  224. }