ctkDependencyGraphTest2.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.commontk.org/LICENSE
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =========================================================================*/
  15. // CTK includes
  16. #include "ctkDependencyGraph.h"
  17. // STL includes
  18. #include <cstdlib>
  19. #include <iostream>
  20. // Qt includes
  21. #include <QDebug>
  22. int ctkDependencyGraphTest2(int argc, char * argv [] )
  23. {
  24. Q_UNUSED(argc);
  25. Q_UNUSED(argv);
  26. // check that cycle detection works
  27. {
  28. const int numberOfVertices = 2;
  29. ctkDependencyGraph graph(numberOfVertices);
  30. //
  31. // 1 -> 2
  32. // \ /
  33. // <-
  34. //
  35. graph.insertEdge(1,2);
  36. graph.insertEdge(2,1);
  37. int expectedNumberOfEdge = 2;
  38. int nov = graph.numberOfVertices();
  39. if( nov != numberOfVertices )
  40. {
  41. qCritical() << "Number of vertices does not match (expected" << numberOfVertices << "got" << nov << ")";
  42. return EXIT_FAILURE;
  43. }
  44. int noe = graph.numberOfEdges();
  45. if( noe != expectedNumberOfEdge )
  46. {
  47. qCritical() << "Number of edges does not match (expected" << expectedNumberOfEdge << "got" << noe << ")";
  48. return EXIT_FAILURE;
  49. }
  50. bool cfc = graph.checkForCycle();
  51. if( cfc == false )
  52. {
  53. qCritical() << "Cycle detection failed";
  54. return EXIT_FAILURE;
  55. }
  56. bool cdtd = graph.cycleDetected();
  57. if( cdtd == false )
  58. {
  59. qCritical() << "Cycle detected flag wrong";
  60. return EXIT_FAILURE;
  61. }
  62. int co = graph.cycleOrigin();
  63. int ce = graph.cycleEnd();
  64. if (co != 2)
  65. {
  66. qCritical() << "Wrong cycle origin (expected" << 2 << "got" << co << ")";
  67. return EXIT_FAILURE;
  68. }
  69. if (ce != 1)
  70. {
  71. qCritical() << "Wrong cycle end (expected" << 1 << "got" << ce << ")";
  72. return EXIT_FAILURE;
  73. }
  74. }
  75. {
  76. const int numberOfVertices = 4;
  77. ctkDependencyGraph graph(numberOfVertices);
  78. // -> 3
  79. // /
  80. // 1 -> 2 -> 4
  81. // \ /
  82. // <-
  83. //
  84. graph.insertEdge(1,2);
  85. graph.insertEdge(2,3);
  86. graph.insertEdge(2,4);
  87. graph.insertEdge(4,2);
  88. int expectedNumberOfEdge = 4;
  89. int nov = graph.numberOfVertices();
  90. if( nov != numberOfVertices )
  91. {
  92. qCritical() << "Number of vertices does not match (expected" << numberOfVertices << "got" << nov << ")";
  93. return EXIT_FAILURE;
  94. }
  95. int noe = graph.numberOfEdges();
  96. if( noe != expectedNumberOfEdge )
  97. {
  98. qCritical() << "Number of edges does not match (expected" << expectedNumberOfEdge << "got" << noe << ")";
  99. return EXIT_FAILURE;
  100. }
  101. bool cfc = graph.checkForCycle();
  102. if( cfc == false )
  103. {
  104. qCritical() << "Cycle detection failed";
  105. return EXIT_FAILURE;
  106. }
  107. bool cdtd = graph.cycleDetected();
  108. if( cdtd == false )
  109. {
  110. qCritical() << "Cycle detected flag wrong";
  111. return EXIT_FAILURE;
  112. }
  113. int co = graph.cycleOrigin();
  114. int ce = graph.cycleEnd();
  115. if (co != 2)
  116. {
  117. qCritical() << "Wrong cycle origin (expected" << 2 << "got" << co << ")";
  118. return EXIT_FAILURE;
  119. }
  120. if (ce != 4)
  121. {
  122. qCritical() << "Wrong cycle end (expected" << 4 << "got" << ce << ")";
  123. return EXIT_FAILURE;
  124. }
  125. }
  126. // check that cycle detection works on disconnected graphs
  127. {
  128. const int numberOfVertices = 8;
  129. ctkDependencyGraph graph(numberOfVertices);
  130. /* 1 -> 2 -> 3
  131. * \
  132. * -> 4
  133. *
  134. * -> 7
  135. * /
  136. * 5 -> 6 -> 8
  137. * \ /
  138. * ---<---
  139. */
  140. graph.insertEdge(1,2);
  141. graph.insertEdge(2,3);
  142. graph.insertEdge(2,4);
  143. graph.insertEdge(5,6);
  144. graph.insertEdge(6,7);
  145. graph.insertEdge(6,8);
  146. graph.insertEdge(8,5);
  147. int expectedNumberOfEdge = 7;
  148. int nov = graph.numberOfVertices();
  149. if( nov != numberOfVertices )
  150. {
  151. qCritical() << "Number of vertices does not match (expected" << numberOfVertices << "got" << nov << ")";
  152. return EXIT_FAILURE;
  153. }
  154. int noe = graph.numberOfEdges();
  155. if( noe != expectedNumberOfEdge )
  156. {
  157. qCritical() << "Number of edges does not match (expected" << expectedNumberOfEdge << "got" << noe << ")";
  158. return EXIT_FAILURE;
  159. }
  160. bool cfc = graph.checkForCycle();
  161. if( cfc == false )
  162. {
  163. qCritical() << "Cycle detection failed";
  164. return EXIT_FAILURE;
  165. }
  166. bool cdtd = graph.cycleDetected();
  167. if( cdtd == false )
  168. {
  169. qCritical() << "Cycle detected flag wrong";
  170. return EXIT_FAILURE;
  171. }
  172. }
  173. // check that topological ordering and paths
  174. // work on disconnected graphs
  175. {
  176. const int numberOfVertices = 11;
  177. ctkDependencyGraph graph(numberOfVertices);
  178. /* 1 -> 2 -> 3
  179. * \
  180. * -> 4
  181. *
  182. * -> 7 ->
  183. * / \
  184. * 5 -> 6 -> 8 -> 9
  185. * ^
  186. * |
  187. * 10 -> 11
  188. */
  189. graph.insertEdge(1,2);
  190. graph.insertEdge(2,3);
  191. graph.insertEdge(2,4);
  192. graph.insertEdge(5,6);
  193. graph.insertEdge(6,7);
  194. graph.insertEdge(6,8);
  195. graph.insertEdge(7,9);
  196. graph.insertEdge(8,9);
  197. graph.insertEdge(10,8);
  198. graph.insertEdge(10,11);
  199. int expectedNumberOfEdge = 10;
  200. int nov = graph.numberOfVertices();
  201. if( nov != numberOfVertices )
  202. {
  203. qCritical() << "Number of vertices does not match (expected" << numberOfVertices << "got" << nov << ")";
  204. return EXIT_FAILURE;
  205. }
  206. int noe = graph.numberOfEdges();
  207. if( noe != expectedNumberOfEdge )
  208. {
  209. qCritical() << "Number of edges does not match (expected" << expectedNumberOfEdge << "got" << noe << ")";
  210. return EXIT_FAILURE;
  211. }
  212. bool cfc = graph.checkForCycle();
  213. if( cfc == true )
  214. {
  215. qCritical() << "Cycle detection failed";
  216. return EXIT_FAILURE;
  217. }
  218. bool cdtd = graph.cycleDetected();
  219. if( cdtd == true )
  220. {
  221. qCritical() << "Cycle detected flag wrong";
  222. return EXIT_FAILURE;
  223. }
  224. QList<int> sources;
  225. graph.sourceVertices(sources);
  226. QList<int> expectedSources;
  227. expectedSources << 1 << 5 << 10;
  228. if (sources != expectedSources)
  229. {
  230. qCritical() << "Source vertices do not match (expected" << expectedSources << "got" << sources << ")";
  231. return EXIT_FAILURE;
  232. }
  233. QList<int> globalSort;
  234. graph.topologicalSort(globalSort);
  235. QList<int> expectedGlobalSort;
  236. expectedGlobalSort << 1 << 5 << 10 << 2 << 6 << 11 << 3 << 4 << 7 << 8 << 9;
  237. if (globalSort != expectedGlobalSort)
  238. {
  239. qCritical() << "Topological sort error (expected" << expectedGlobalSort << "got" << globalSort << ")";
  240. return EXIT_FAILURE;
  241. }
  242. QList<int> subSort10;
  243. graph.topologicalSort(subSort10, 10);
  244. QList<int> expectedSubSort10;
  245. expectedSubSort10 << 10 << 8 << 11 << 9;
  246. if (subSort10 != expectedSubSort10)
  247. {
  248. qCritical() << "Topological subgraph sort error (expected" << expectedSubSort10 << "got" << subSort10 << ")";
  249. return EXIT_FAILURE;
  250. }
  251. }
  252. return EXIT_SUCCESS;
  253. }