ctkDependencyGraphTest2.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 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.apache.org/licenses/LICENSE-2.0.txt
  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. #include "ctkDependencyGraphTestHelper.h"
  18. // STL includes
  19. #include <cstdlib>
  20. #include <iostream>
  21. int ctkDependencyGraphTest2(int argc, char * argv [] )
  22. {
  23. if (argc > 1)
  24. {
  25. std::cerr << argv[0] << " expects zero arguments" << std::endl;
  26. }
  27. // check that cycle detection works
  28. {
  29. const int numberOfVertices = 2;
  30. ctkDependencyGraph graph(numberOfVertices);
  31. //
  32. // 1 -> 2
  33. // \ /
  34. // <-
  35. //
  36. graph.insertEdge(1,2);
  37. graph.insertEdge(2,1);
  38. int expectedNumberOfEdge = 2;
  39. int nov = graph.numberOfVertices();
  40. if( nov != numberOfVertices )
  41. {
  42. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  43. return EXIT_FAILURE;
  44. }
  45. int noe = graph.numberOfEdges();
  46. if( noe != expectedNumberOfEdge )
  47. {
  48. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  49. return EXIT_FAILURE;
  50. }
  51. bool cfc = graph.checkForCycle();
  52. if( cfc == false )
  53. {
  54. std::cerr << "Cycle detection failed" << std::endl;
  55. return EXIT_FAILURE;
  56. }
  57. bool cdtd = graph.cycleDetected();
  58. if( cdtd == false )
  59. {
  60. std::cerr << "Cycle detected flag wrong" << std::endl;
  61. return EXIT_FAILURE;
  62. }
  63. int co = graph.cycleOrigin();
  64. int ce = graph.cycleEnd();
  65. if (co != 2)
  66. {
  67. std::cerr << "Wrong cycle origin (expected" << 2 << " got " << co << " )" << std::endl;
  68. return EXIT_FAILURE;
  69. }
  70. if (ce != 1)
  71. {
  72. std::cerr << "Wrong cycle end (expected" << 1 << " got " << ce << " )" << std::endl;
  73. return EXIT_FAILURE;
  74. }
  75. }
  76. {
  77. const int numberOfVertices = 4;
  78. ctkDependencyGraph graph(numberOfVertices);
  79. // -> 3
  80. // /
  81. // 1 -> 2 -> 4
  82. // \ /
  83. // <-
  84. //
  85. graph.insertEdge(1,2);
  86. graph.insertEdge(2,3);
  87. graph.insertEdge(2,4);
  88. graph.insertEdge(4,2);
  89. int expectedNumberOfEdge = 4;
  90. int nov = graph.numberOfVertices();
  91. if( nov != numberOfVertices )
  92. {
  93. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  94. return EXIT_FAILURE;
  95. }
  96. int noe = graph.numberOfEdges();
  97. if( noe != expectedNumberOfEdge )
  98. {
  99. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  100. return EXIT_FAILURE;
  101. }
  102. bool cfc = graph.checkForCycle();
  103. if( cfc == false )
  104. {
  105. std::cerr << "Cycle detection failed" << std::endl;
  106. return EXIT_FAILURE;
  107. }
  108. bool cdtd = graph.cycleDetected();
  109. if( cdtd == false )
  110. {
  111. std::cerr << "Cycle detected flag wrong" << std::endl;
  112. return EXIT_FAILURE;
  113. }
  114. int co = graph.cycleOrigin();
  115. int ce = graph.cycleEnd();
  116. if (co != 2)
  117. {
  118. std::cerr << "Wrong cycle origin (expected" << 2 << " got " << co << ")" << std::endl;
  119. return EXIT_FAILURE;
  120. }
  121. if (ce != 4)
  122. {
  123. std::cerr << "Wrong cycle end (expected" << 4 << " got " << ce << ")" << std::endl;
  124. return EXIT_FAILURE;
  125. }
  126. }
  127. // check that cycle detection works on disconnected graphs
  128. {
  129. const int numberOfVertices = 8;
  130. ctkDependencyGraph graph(numberOfVertices);
  131. /* 1 -> 2 -> 3
  132. * \
  133. * -> 4
  134. *
  135. * -> 7
  136. * /
  137. * 5 -> 6 -> 8
  138. * \ /
  139. * ---<---
  140. */
  141. graph.insertEdge(1,2);
  142. graph.insertEdge(2,3);
  143. graph.insertEdge(2,4);
  144. graph.insertEdge(5,6);
  145. graph.insertEdge(6,7);
  146. graph.insertEdge(6,8);
  147. graph.insertEdge(8,5);
  148. int expectedNumberOfEdge = 7;
  149. int nov = graph.numberOfVertices();
  150. if( nov != numberOfVertices )
  151. {
  152. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  153. return EXIT_FAILURE;
  154. }
  155. int noe = graph.numberOfEdges();
  156. if( noe != expectedNumberOfEdge )
  157. {
  158. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  159. return EXIT_FAILURE;
  160. }
  161. bool cfc = graph.checkForCycle();
  162. if( cfc == false )
  163. {
  164. std::cerr << "Cycle detection failed" << std::endl;
  165. return EXIT_FAILURE;
  166. }
  167. bool cdtd = graph.cycleDetected();
  168. if( cdtd == false )
  169. {
  170. std::cerr << "Cycle detected flag wrong" << std::endl;
  171. return EXIT_FAILURE;
  172. }
  173. }
  174. // check that topological ordering and paths
  175. // work on disconnected graphs
  176. {
  177. const int numberOfVertices = 11;
  178. ctkDependencyGraph graph(numberOfVertices);
  179. /* 1 -> 2 -> 3
  180. * \
  181. * -> 4
  182. *
  183. * -> 7 ->
  184. * / \
  185. * 5 -> 6 -> 8 -> 9
  186. * ^
  187. * |
  188. * 10 -> 11
  189. */
  190. graph.insertEdge(1,2);
  191. graph.insertEdge(2,3);
  192. graph.insertEdge(2,4);
  193. graph.insertEdge(5,6);
  194. graph.insertEdge(6,7);
  195. graph.insertEdge(6,8);
  196. graph.insertEdge(7,9);
  197. graph.insertEdge(8,9);
  198. graph.insertEdge(10,8);
  199. graph.insertEdge(10,11);
  200. int expectedNumberOfEdge = 10;
  201. int nov = graph.numberOfVertices();
  202. if( nov != numberOfVertices )
  203. {
  204. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  205. return EXIT_FAILURE;
  206. }
  207. int noe = graph.numberOfEdges();
  208. if( noe != expectedNumberOfEdge )
  209. {
  210. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  211. return EXIT_FAILURE;
  212. }
  213. bool cfc = graph.checkForCycle();
  214. if( cfc == true )
  215. {
  216. std::cerr << "Cycle detection failed" << std::endl;
  217. return EXIT_FAILURE;
  218. }
  219. bool cdtd = graph.cycleDetected();
  220. if( cdtd == true )
  221. {
  222. std::cerr << "Cycle detected flag wrong" << std::endl;
  223. return EXIT_FAILURE;
  224. }
  225. std::list<int> sources;
  226. graph.sourceVertices(sources);
  227. std::list<int> expectedSources;
  228. expectedSources.push_back(1);
  229. expectedSources.push_back(5);
  230. expectedSources.push_back(10);
  231. if (sources != expectedSources)
  232. {
  233. std::cerr << "Problem with sourceVertices()" << std::endl;
  234. printIntegerList("sources:", sources);
  235. printIntegerList("expectedSources:", expectedSources);
  236. return EXIT_FAILURE;
  237. }
  238. std::list<int> globalSort;
  239. graph.topologicalSort(globalSort);
  240. std::list<int> expectedGlobalSort;
  241. expectedGlobalSort.push_back(1);
  242. expectedGlobalSort.push_back(5);
  243. expectedGlobalSort.push_back(10);
  244. expectedGlobalSort.push_back(2);
  245. expectedGlobalSort.push_back(6);
  246. expectedGlobalSort.push_back(11);
  247. expectedGlobalSort.push_back(3);
  248. expectedGlobalSort.push_back(4);
  249. expectedGlobalSort.push_back(7);
  250. expectedGlobalSort.push_back(8);
  251. expectedGlobalSort.push_back(9);
  252. if (globalSort != expectedGlobalSort)
  253. {
  254. std::cerr << "Problem with topologicalSort(globalSort)" << std::endl;
  255. printIntegerList("globalSort:", globalSort);
  256. printIntegerList("expectedGlobalSort:", expectedGlobalSort);
  257. return EXIT_FAILURE;
  258. }
  259. std::list<int> subSort10;
  260. graph.topologicalSort(subSort10, 10);
  261. std::list<int> expectedSubSort10;
  262. expectedSubSort10.push_back(10);
  263. expectedSubSort10.push_back(8);
  264. expectedSubSort10.push_back(11);
  265. expectedSubSort10.push_back(9);
  266. if (subSort10 != expectedSubSort10)
  267. {
  268. std::cerr << "Problem with topologicalSort(subSort10, 10)" << std::endl;
  269. printIntegerList("subSort10:", subSort10);
  270. printIntegerList("expectedSubSort10:", expectedSubSort10);
  271. return EXIT_FAILURE;
  272. }
  273. }
  274. // check that topological ordering and paths
  275. // work on disconnected graphs and isolated vertices
  276. {
  277. const int numberOfVertices = 13;
  278. ctkDependencyGraph graph(numberOfVertices);
  279. /* 1 -> 2 -> 3
  280. * \
  281. * -> 4
  282. *
  283. * -> 7 ->
  284. * / \
  285. * 5 -> 6 -> 8 -> 9
  286. * ^
  287. * |
  288. * 10 -> 11
  289. *
  290. * 12 13
  291. */
  292. graph.insertEdge(1,2);
  293. graph.insertEdge(2,3);
  294. graph.insertEdge(2,4);
  295. graph.insertEdge(5,6);
  296. graph.insertEdge(6,7);
  297. graph.insertEdge(6,8);
  298. graph.insertEdge(7,9);
  299. graph.insertEdge(8,9);
  300. graph.insertEdge(10,8);
  301. graph.insertEdge(10,11);
  302. int expectedNumberOfEdge = 10;
  303. int nov = graph.numberOfVertices();
  304. if( nov != numberOfVertices )
  305. {
  306. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  307. return EXIT_FAILURE;
  308. }
  309. int noe = graph.numberOfEdges();
  310. if( noe != expectedNumberOfEdge )
  311. {
  312. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  313. return EXIT_FAILURE;
  314. }
  315. bool cfc = graph.checkForCycle();
  316. if( cfc == true )
  317. {
  318. std::cerr << "Cycle detection failed" << std::endl;
  319. return EXIT_FAILURE;
  320. }
  321. bool cdtd = graph.cycleDetected();
  322. if( cdtd == true )
  323. {
  324. std::cerr << "Cycle detected flag wrong" << std::endl;
  325. return EXIT_FAILURE;
  326. }
  327. std::list<int> sources;
  328. graph.sourceVertices(sources);
  329. std::list<int> expectedSources;
  330. expectedSources.push_back(1);
  331. expectedSources.push_back(5);
  332. expectedSources.push_back(10);
  333. expectedSources.push_back(12);
  334. expectedSources.push_back(13);
  335. if (sources != expectedSources)
  336. {
  337. std::cerr << "Problem with sourceVertices(sources)" << std::endl;
  338. printIntegerList("sources:", sources);
  339. printIntegerList("expectedSources:", expectedSources);
  340. return EXIT_FAILURE;
  341. }
  342. std::list<int> globalSort;
  343. graph.topologicalSort(globalSort);
  344. std::list<int> expectedGlobalSort;
  345. expectedGlobalSort.push_back(1);
  346. expectedGlobalSort.push_back(5);
  347. expectedGlobalSort.push_back(10);
  348. expectedGlobalSort.push_back(12);
  349. expectedGlobalSort.push_back(13);
  350. expectedGlobalSort.push_back(2);
  351. expectedGlobalSort.push_back(6);
  352. expectedGlobalSort.push_back(11);
  353. expectedGlobalSort.push_back(3);
  354. expectedGlobalSort.push_back(4);
  355. expectedGlobalSort.push_back(7);
  356. expectedGlobalSort.push_back(8);
  357. expectedGlobalSort.push_back(9);
  358. if (globalSort != expectedGlobalSort)
  359. {
  360. std::cerr << "Problem with topologicalSort(globalSort)" << std::endl;
  361. printIntegerList("globalSort:", globalSort);
  362. printIntegerList("expectedGlobalSort:", expectedGlobalSort);
  363. return EXIT_FAILURE;
  364. }
  365. std::list<int> subSort10;
  366. graph.topologicalSort(subSort10, 10);
  367. std::list<int> expectedSubSort10;
  368. expectedSubSort10.push_back(10);
  369. expectedSubSort10.push_back(8);
  370. expectedSubSort10.push_back(11);
  371. expectedSubSort10.push_back(9);
  372. if (subSort10 != expectedSubSort10)
  373. {
  374. std::cerr << "Problem with topologicalSort(subSort10, 10)" << std::endl;
  375. printIntegerList("subSort10:", subSort10);
  376. printIntegerList("expectedSubSort10:", expectedSubSort10);
  377. return EXIT_FAILURE;
  378. }
  379. std::list<int> subSort12;
  380. graph.topologicalSort(subSort12, 12);
  381. std::list<int> expectedSubSort12;
  382. expectedSubSort12.push_back(12);
  383. if (subSort12 != expectedSubSort12)
  384. {
  385. std::cerr << "Problem with topologicalSort(subSort12, 12)" << std::endl;
  386. printIntegerList("subSort12:", subSort12);
  387. printIntegerList("expectedSubSort12:", expectedSubSort12);
  388. return EXIT_FAILURE;
  389. }
  390. }
  391. // check that topological ordering and paths
  392. // work on a null graph
  393. {
  394. const int numberOfVertices = 3;
  395. ctkDependencyGraph graph(numberOfVertices);
  396. /*
  397. * 1 2 3
  398. */
  399. // a null graph has no edges
  400. int expectedNumberOfEdge = 0;
  401. int nov = graph.numberOfVertices();
  402. if( nov != numberOfVertices )
  403. {
  404. std::cerr << "Number of vertices does not match (expected" << numberOfVertices << " got " << nov << ")" << std::endl;
  405. return EXIT_FAILURE;
  406. }
  407. int noe = graph.numberOfEdges();
  408. if( noe != expectedNumberOfEdge )
  409. {
  410. std::cerr << "Number of edges does not match (expected" << expectedNumberOfEdge << " got " << noe << ")" << std::endl;
  411. return EXIT_FAILURE;
  412. }
  413. bool cfc = graph.checkForCycle();
  414. if( cfc == true )
  415. {
  416. std::cerr << "Cycle detection failed" << std::endl;
  417. return EXIT_FAILURE;
  418. }
  419. bool cdtd = graph.cycleDetected();
  420. if( cdtd == true )
  421. {
  422. std::cerr << "Cycle detected flag wrong" << std::endl;
  423. return EXIT_FAILURE;
  424. }
  425. std::list<int> sources;
  426. graph.sourceVertices(sources);
  427. std::list<int> expectedSources;
  428. expectedSources.push_back(1);
  429. expectedSources.push_back(2);
  430. expectedSources.push_back(3);
  431. if (sources != expectedSources)
  432. {
  433. std::cerr << "Problem with sourceVertices(sources)" << std::endl;
  434. printIntegerList("sources:", sources);
  435. printIntegerList("expectedSources:", expectedSources);
  436. return EXIT_FAILURE;
  437. }
  438. std::list<int> globalSort;
  439. graph.topologicalSort(globalSort);
  440. std::list<int> expectedGlobalSort;
  441. expectedGlobalSort.push_back(1);
  442. expectedGlobalSort.push_back(2);
  443. expectedGlobalSort.push_back(3);
  444. if (globalSort != expectedGlobalSort)
  445. {
  446. std::cerr << "Problem with topologicalSort(globalSort)" << std::endl;
  447. printIntegerList("globalSort:", globalSort);
  448. printIntegerList("expectedGlobalSort:", expectedGlobalSort);
  449. return EXIT_FAILURE;
  450. }
  451. std::list<int> subSort2;
  452. graph.topologicalSort(subSort2, 2);
  453. std::list<int> expectedSubSort2;
  454. expectedSubSort2.push_back(2);
  455. if (subSort2 != expectedSubSort2)
  456. {
  457. std::cerr << "Problem with topologicalSort(subSort2, 2)" << std::endl;
  458. printIntegerList("subSort2:", subSort2);
  459. printIntegerList("expectedSubSort2:", expectedSubSort2);
  460. return EXIT_FAILURE;
  461. }
  462. }
  463. return EXIT_SUCCESS;
  464. }