Framework.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include "tests/framework/Framework.h"
  6. #include <iostream>
  7. #include <cstring>
  8. #include <malloc.h>
  9. namespace anki
  10. {
  11. TestSuite::~TestSuite()
  12. {
  13. for(Test* t : tests)
  14. {
  15. delete t;
  16. }
  17. }
  18. void Test::run()
  19. {
  20. std::cout << "========\nRunning " << suite->name << " " << name << "\n========" << std::endl;
  21. #if ANKI_OS == ANKI_OS_LINUX
  22. struct mallinfo a = mallinfo();
  23. #endif
  24. callback(*this);
  25. #if ANKI_OS == ANKI_OS_LINUX
  26. struct mallinfo b = mallinfo();
  27. int diff = b.uordblks - a.uordblks;
  28. if(diff > 0)
  29. {
  30. std::cerr << "Test leaks memory: " << diff << std::endl;
  31. }
  32. #endif
  33. std::cout << std::endl;
  34. }
  35. void Tester::addTest(const char* name, const char* suiteName, TestCallback callback)
  36. {
  37. std::vector<TestSuite*>::iterator it;
  38. for(it = suites.begin(); it != suites.end(); it++)
  39. {
  40. if((*it)->name == suiteName)
  41. {
  42. break;
  43. }
  44. }
  45. // Not found
  46. TestSuite* suite = nullptr;
  47. if(it == suites.end())
  48. {
  49. suite = new TestSuite;
  50. suite->name = suiteName;
  51. suites.push_back(suite);
  52. }
  53. else
  54. {
  55. suite = *it;
  56. }
  57. // Sanity check
  58. std::vector<Test*>::iterator it1;
  59. for(it1 = suite->tests.begin(); it1 != suite->tests.end(); it1++)
  60. {
  61. if((*it)->name == name)
  62. {
  63. std::cerr << "Test already exists: " << name << std::endl;
  64. return;
  65. }
  66. }
  67. // Add the test
  68. Test* test = new Test;
  69. suite->tests.push_back(test);
  70. test->name = name;
  71. test->suite = suite;
  72. test->callback = callback;
  73. }
  74. int Tester::run(int argc, char** argv)
  75. {
  76. // Parse args
  77. //
  78. programName = argv[0];
  79. std::string helpMessage = "Usage: " + programName + R"( [options]
  80. Options:
  81. --help Print this message
  82. --list-tests List all the tests
  83. --suite <name> Run tests only from this suite
  84. --test <name> Run this test. --suite needs to be specified)";
  85. std::string suiteName;
  86. std::string testName;
  87. for(int i = 1; i < argc; i++)
  88. {
  89. const char* arg = argv[i];
  90. if(strcmp(arg, "--list-tests") == 0)
  91. {
  92. listTests();
  93. return 0;
  94. }
  95. else if(strcmp(arg, "--help") == 0)
  96. {
  97. std::cout << helpMessage << std::endl;
  98. return 0;
  99. }
  100. else if(strcmp(arg, "--suite") == 0)
  101. {
  102. ++i;
  103. if(i >= argc)
  104. {
  105. std::cerr << "<name> is missing after --suite" << std::endl;
  106. return 1;
  107. }
  108. suiteName = argv[i];
  109. }
  110. else if(strcmp(arg, "--test") == 0)
  111. {
  112. ++i;
  113. if(i >= argc)
  114. {
  115. std::cerr << "<name> is missing after --test" << std::endl;
  116. return 1;
  117. }
  118. testName = argv[i];
  119. }
  120. }
  121. // Sanity check
  122. if(testName.length() > 0 && suiteName.length() == 0)
  123. {
  124. std::cout << "Specify --suite as well" << std::endl;
  125. return 1;
  126. }
  127. // Run tests
  128. //
  129. int passed = 0;
  130. int run = 0;
  131. if(argc == 1)
  132. {
  133. // Run all
  134. for(TestSuite* suite : suites)
  135. {
  136. for(Test* test : suite->tests)
  137. {
  138. ++run;
  139. test->run();
  140. ++passed;
  141. }
  142. }
  143. }
  144. else
  145. {
  146. for(TestSuite* suite : suites)
  147. {
  148. if(suite->name == suiteName)
  149. {
  150. for(Test* test : suite->tests)
  151. {
  152. if(test->name == testName || testName.length() == 0)
  153. {
  154. ++run;
  155. test->run();
  156. ++passed;
  157. }
  158. }
  159. }
  160. }
  161. }
  162. int failed = run - passed;
  163. std::cout << "========\nRun " << run << " tests, failed " << failed << std::endl;
  164. if(failed == 0)
  165. {
  166. std::cout << "SUCCESS!" << std::endl;
  167. }
  168. else
  169. {
  170. std::cout << "FAILURE" << std::endl;
  171. }
  172. return run - passed;
  173. }
  174. int Tester::listTests()
  175. {
  176. for(TestSuite* suite : suites)
  177. {
  178. for(Test* test : suite->tests)
  179. {
  180. std::cout << programName << " --suite \"" << suite->name << "\" --test \"" << test->name << "\""
  181. << std::endl;
  182. }
  183. }
  184. return 0;
  185. }
  186. static Tester* testerInstance = nullptr;
  187. Tester& getTesterSingleton()
  188. {
  189. return *(testerInstance ? testerInstance : (testerInstance = new Tester));
  190. }
  191. void deleteTesterSingleton()
  192. {
  193. if(testerInstance != nullptr)
  194. {
  195. delete testerInstance;
  196. }
  197. }
  198. } // end namespace anki