Framework.cpp 4.3 KB

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