Framework.cpp 4.3 KB

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