Framework.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright (C) 2009-present, 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 <AnKi/Util/Filesystem.h>
  7. #include <iostream>
  8. #include <cstring>
  9. #include <malloc.h>
  10. #if ANKI_OS_ANDROID
  11. # include <android/log.h>
  12. #endif
  13. namespace anki {
  14. #if !ANKI_OS_ANDROID
  15. # define ANKI_TEST_LOG(fmt, ...) printf(fmt "\n", __VA_ARGS__)
  16. #else
  17. # define ANKI_TEST_LOG(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "AnKi Tests", fmt, __VA_ARGS__)
  18. #endif
  19. TestSuite::~TestSuite()
  20. {
  21. for(Test* t : tests)
  22. {
  23. delete t;
  24. }
  25. }
  26. void Test::run()
  27. {
  28. ANKI_TEST_LOG("========\nRunning %s %s\n========", suite->name.c_str(), name.c_str());
  29. #if ANKI_COMPILER_GCC_COMPATIBLE
  30. # pragma GCC diagnostic push
  31. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  32. #endif
  33. #if ANKI_OS_LINUX
  34. struct mallinfo a = mallinfo();
  35. #endif
  36. callback(*this);
  37. #if ANKI_OS_LINUX
  38. struct mallinfo b = mallinfo();
  39. int diff = b.uordblks - a.uordblks;
  40. if(diff > 0)
  41. {
  42. ANKI_TEST_LOG("Test leaks memory: %d", diff);
  43. }
  44. #endif
  45. #if ANKI_COMPILER_GCC_COMPATIBLE
  46. # pragma GCC diagnostic pop
  47. #endif
  48. }
  49. void Tester::addTest(const char* name, const char* suiteName, TestCallback callback)
  50. {
  51. std::vector<TestSuite*>::iterator it;
  52. for(it = suites.begin(); it != suites.end(); it++)
  53. {
  54. if((*it)->name == suiteName)
  55. {
  56. break;
  57. }
  58. }
  59. // Not found
  60. TestSuite* suite = nullptr;
  61. if(it == suites.end())
  62. {
  63. suite = new TestSuite;
  64. suite->name = suiteName;
  65. suites.push_back(suite);
  66. }
  67. else
  68. {
  69. suite = *it;
  70. }
  71. // Sanity check
  72. std::vector<Test*>::iterator it1;
  73. for(it1 = suite->tests.begin(); it1 != suite->tests.end(); it1++)
  74. {
  75. if((*it)->name == name)
  76. {
  77. ANKI_TEST_LOG("Test already exists: %s", name);
  78. return;
  79. }
  80. }
  81. // Add the test
  82. Test* test = new Test;
  83. suite->tests.push_back(test);
  84. test->name = name;
  85. test->suite = suite;
  86. test->callback = callback;
  87. }
  88. int Tester::run(int argc, char** argv)
  89. {
  90. // Parse args
  91. //
  92. programName = argv[0];
  93. std::string helpMessage = "Usage: " + programName + R"( [options]
  94. Options:
  95. --help Print this message
  96. --list-tests List all the tests
  97. --suite <name> Run tests only from this suite
  98. --test <name> Run this test. --suite needs to be specified)";
  99. std::string suiteName;
  100. std::string testName;
  101. for(int i = 1; i < argc; i++)
  102. {
  103. const char* arg = argv[i];
  104. if(strcmp(arg, "--list-tests") == 0)
  105. {
  106. listTests();
  107. return 0;
  108. }
  109. else if(strcmp(arg, "--help") == 0)
  110. {
  111. ANKI_TEST_LOG("%s", helpMessage.c_str());
  112. return 0;
  113. }
  114. else if(strcmp(arg, "--suite") == 0)
  115. {
  116. ++i;
  117. if(i >= argc)
  118. {
  119. ANKI_TEST_LOG("%s", "<name> is missing after --suite");
  120. return 1;
  121. }
  122. suiteName = argv[i];
  123. }
  124. else if(strcmp(arg, "--test") == 0)
  125. {
  126. ++i;
  127. if(i >= argc)
  128. {
  129. ANKI_TEST_LOG("%s", "<name> is missing after --test");
  130. return 1;
  131. }
  132. testName = argv[i];
  133. }
  134. else
  135. {
  136. break;
  137. }
  138. }
  139. // Sanity check
  140. if(testName.length() > 0 && suiteName.length() == 0)
  141. {
  142. ANKI_TEST_LOG("%s", "Specify --suite as well");
  143. return 1;
  144. }
  145. // Run tests
  146. //
  147. int passed = 0;
  148. int run = 0;
  149. if(argc == 1)
  150. {
  151. // Run all
  152. for(TestSuite* suite : suites)
  153. {
  154. for(Test* test : suite->tests)
  155. {
  156. ++run;
  157. test->run();
  158. ++passed;
  159. }
  160. }
  161. }
  162. else
  163. {
  164. for(TestSuite* suite : suites)
  165. {
  166. if(suite->name == suiteName)
  167. {
  168. for(Test* test : suite->tests)
  169. {
  170. if(test->name == testName || testName.length() == 0)
  171. {
  172. ++run;
  173. test->run();
  174. ++passed;
  175. }
  176. }
  177. }
  178. }
  179. }
  180. int failed = run - passed;
  181. ANKI_TEST_LOG("========\nRun %d tests, failed %d", run, failed);
  182. if(failed == 0)
  183. {
  184. ANKI_TEST_LOG("%s", "SUCCESS!");
  185. }
  186. else
  187. {
  188. ANKI_TEST_LOG("%s", "FAILURE");
  189. }
  190. return run - passed;
  191. }
  192. int Tester::listTests()
  193. {
  194. for(TestSuite* suite : suites)
  195. {
  196. for(Test* test : suite->tests)
  197. {
  198. ANKI_TEST_LOG("%s --suite %s --test %s", programName.c_str(), suite->name.c_str(), test->name.c_str());
  199. }
  200. }
  201. return 0;
  202. }
  203. static Tester* g_testerInstance = nullptr;
  204. Tester& getTesterSingleton()
  205. {
  206. return *(g_testerInstance ? g_testerInstance : (g_testerInstance = new Tester));
  207. }
  208. void deleteTesterSingleton()
  209. {
  210. delete g_testerInstance;
  211. }
  212. void initWindow()
  213. {
  214. NativeWindowInitInfo inf;
  215. inf.m_width = g_windowWidthCVar;
  216. inf.m_height = g_windowHeightCVar;
  217. inf.m_title = "AnKi unit tests";
  218. NativeWindow::allocateSingleton();
  219. ANKI_TEST_EXPECT_NO_ERR(NativeWindow::getSingleton().init(inf));
  220. }
  221. void initGrManager()
  222. {
  223. GrManagerInitInfo inf;
  224. inf.m_allocCallback = allocAligned;
  225. String home;
  226. ANKI_TEST_EXPECT_NO_ERR(getTempDirectory(home));
  227. inf.m_cacheDirectory = home;
  228. GrManager::allocateSingleton();
  229. ANKI_TEST_EXPECT_NO_ERR(GrManager::getSingleton().init(inf));
  230. }
  231. } // end namespace anki