ctkDependencyGraphTest1.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // CTK includes
  15. #include "ctkDependencyGraph.h"
  16. #include "ctkDependencyGraphTestHelper.h"
  17. // STL includes
  18. #include <cstdlib>
  19. #include <iostream>
  20. //-----------------------------------------------------------------------------
  21. int ctkDependencyGraphTest1(int argc, char * argv [] )
  22. {
  23. if (argc > 1)
  24. {
  25. std::cerr << argv[0] << " expects zero arguments" << std::endl;
  26. }
  27. const int numberOfVertices = 14;
  28. ctkDependencyGraph graph(numberOfVertices);
  29. graph.setVerbose(true);
  30. graph.setVerbose(false);
  31. //graph.setVerbose(true);
  32. //
  33. // 6 - 8
  34. // |
  35. // 7 - 5 - 1 - 2 -
  36. // | | | - 9
  37. // | 4 - 3 - |
  38. // | |
  39. // 10 - 11 - 12 - 13 - 14
  40. //
  41. graph.insertEdge(3,4);
  42. graph.insertEdge(4,1);
  43. graph.insertEdge(1,5);
  44. graph.insertEdge(5,7);
  45. graph.insertEdge(2,1);
  46. graph.insertEdge(8,6);
  47. graph.insertEdge(6,7);
  48. graph.insertEdge(9,2);
  49. graph.insertEdge(9,3);
  50. graph.insertEdge(14,9);
  51. graph.insertEdge(14,13);
  52. graph.insertEdge(13,12);
  53. graph.insertEdge(12,11);
  54. graph.insertEdge(11,10);
  55. graph.insertEdge(10,7);
  56. int expectedNumberOfEdge = 15;
  57. graph.printAdditionalInfo();
  58. // graph.printGraph(); // printAdditionalInfo also prints graph.
  59. int nov = graph.numberOfVertices();
  60. if( nov != numberOfVertices )
  61. {
  62. return EXIT_FAILURE;
  63. }
  64. int noe = graph.numberOfEdges();
  65. if( noe != expectedNumberOfEdge )
  66. {
  67. return EXIT_FAILURE;
  68. }
  69. bool cfc = graph.checkForCycle();
  70. if( cfc == true )
  71. {
  72. return EXIT_FAILURE;
  73. }
  74. bool cdtd = graph.cycleDetected();
  75. if( cdtd == true )
  76. {
  77. return EXIT_FAILURE;
  78. }
  79. //int corigin = graph.cycleOrigin();
  80. //int cend = graph.cycleEnd();
  81. std::list<int> path;
  82. std::list<int> expectedPath;
  83. graph.findPath( 8, 7, path );
  84. expectedPath.push_back(8);
  85. expectedPath.push_back(6);
  86. expectedPath.push_back(7);
  87. if (path != expectedPath)
  88. {
  89. std::cerr << "Problem with findPath()" << std::endl;
  90. printIntegerList("current:", path);
  91. printIntegerList("expected:", expectedPath);
  92. return EXIT_FAILURE;
  93. }
  94. path.clear();
  95. expectedPath.clear();
  96. graph.findPath( 1, 7, path );
  97. expectedPath.push_back(1);
  98. expectedPath.push_back(5);
  99. expectedPath.push_back(7);
  100. if (path != expectedPath)
  101. {
  102. std::cerr << "Problem with findPath()" << std::endl;
  103. printIntegerList("current:", path);
  104. printIntegerList("expected:", expectedPath);
  105. return EXIT_FAILURE;
  106. }
  107. path.clear();
  108. expectedPath.clear();
  109. graph.findPath( 3, 7, path );
  110. expectedPath.push_back(3);
  111. expectedPath.push_back(4);
  112. expectedPath.push_back(1);
  113. expectedPath.push_back(5);
  114. expectedPath.push_back(7);
  115. if (path != expectedPath)
  116. {
  117. std::cerr << "Problem with findPath()" << std::endl;
  118. printIntegerList("current:", path);
  119. printIntegerList("expected:", expectedPath);
  120. return EXIT_FAILURE;
  121. }
  122. path.clear();
  123. expectedPath.clear();
  124. graph.findPath( 2, 5, path );
  125. expectedPath.push_back(2);
  126. expectedPath.push_back(1);
  127. expectedPath.push_back(5);
  128. if (path != expectedPath)
  129. {
  130. std::cerr << "Problem with findPath()" << std::endl;
  131. printIntegerList("current:", path);
  132. printIntegerList("expected:", expectedPath);
  133. return EXIT_FAILURE;
  134. }
  135. path.clear();
  136. expectedPath.clear();
  137. std::list<std::list<int>* > paths;
  138. std::list<int> expectedPath1;
  139. std::list<int> expectedPath2;
  140. std::list<int> expectedPath3;
  141. graph.findPaths(14, 5, paths);
  142. expectedPath1.push_back(14);
  143. expectedPath1.push_back(9);
  144. expectedPath1.push_back(3);
  145. expectedPath1.push_back(4);
  146. expectedPath1.push_back(1);
  147. expectedPath1.push_back(5);
  148. expectedPath2.push_back(14);
  149. expectedPath2.push_back(9);
  150. expectedPath2.push_back(2);
  151. expectedPath2.push_back(1);
  152. expectedPath2.push_back(5);
  153. std::list<std::list<int>* >::const_iterator pathsIterator;
  154. for(pathsIterator = paths.begin(); pathsIterator != paths.end(); pathsIterator++)
  155. {
  156. if (*(*pathsIterator) != expectedPath1 && *(*pathsIterator) != expectedPath2)
  157. {
  158. printIntegerList("current:", *(*pathsIterator));
  159. printIntegerList("expected:", expectedPath1, false);
  160. printIntegerList(" or ", expectedPath2);
  161. return EXIT_FAILURE;
  162. }
  163. }
  164. expectedPath1.clear();
  165. expectedPath2.clear();
  166. graph.findPaths(14, 7, paths);
  167. expectedPath1.push_back(14);
  168. expectedPath1.push_back(9);
  169. expectedPath1.push_back(3);
  170. expectedPath1.push_back(4);
  171. expectedPath1.push_back(1);
  172. expectedPath1.push_back(5);
  173. expectedPath1.push_back(7);
  174. expectedPath2.push_back(14);
  175. expectedPath2.push_back(9);
  176. expectedPath2.push_back(2);
  177. expectedPath2.push_back(1);
  178. expectedPath2.push_back(5);
  179. expectedPath2.push_back(7);
  180. expectedPath3.push_back(14);
  181. expectedPath3.push_back(13);
  182. expectedPath3.push_back(12);
  183. expectedPath3.push_back(11);
  184. expectedPath3.push_back(10);
  185. expectedPath3.push_back(7);
  186. for(pathsIterator = paths.begin(); pathsIterator != paths.end(); pathsIterator++)
  187. {
  188. if (*(*pathsIterator) != expectedPath1 && *(*pathsIterator) != expectedPath2 && *(*pathsIterator) != expectedPath3)
  189. {
  190. printIntegerList("current:", *(*pathsIterator));
  191. printIntegerList("expected:", expectedPath1, false);
  192. printIntegerList(" or ", expectedPath2, false);
  193. printIntegerList(" or ", expectedPath3);
  194. return EXIT_FAILURE;
  195. }
  196. }
  197. return EXIT_SUCCESS;
  198. }