Framework.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // Copyright (C) 2009-2021, 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. {
  15. #if !ANKI_OS_ANDROID
  16. # define ANKI_TEST_LOG(fmt, ...) printf(fmt "\n", __VA_ARGS__)
  17. #else
  18. # define ANKI_TEST_LOG(fmt, ...) __android_log_print(ANDROID_LOG_INFO, "AnKi Tests", fmt, __VA_ARGS__)
  19. #endif
  20. TestSuite::~TestSuite()
  21. {
  22. for(Test* t : tests)
  23. {
  24. delete t;
  25. }
  26. }
  27. void Test::run()
  28. {
  29. ANKI_TEST_LOG("========\nRunning %s %s\n========", suite->name.c_str(), name.c_str());
  30. #if ANKI_OS_LINUX
  31. struct mallinfo a = mallinfo();
  32. #endif
  33. callback(*this);
  34. #if ANKI_OS_LINUX
  35. struct mallinfo b = mallinfo();
  36. int diff = b.uordblks - a.uordblks;
  37. if(diff > 0)
  38. {
  39. ANKI_TEST_LOG("Test leaks memory: %d", diff);
  40. }
  41. #endif
  42. }
  43. void Tester::addTest(const char* name, const char* suiteName, TestCallback callback)
  44. {
  45. std::vector<TestSuite*>::iterator it;
  46. for(it = suites.begin(); it != suites.end(); it++)
  47. {
  48. if((*it)->name == suiteName)
  49. {
  50. break;
  51. }
  52. }
  53. // Not found
  54. TestSuite* suite = nullptr;
  55. if(it == suites.end())
  56. {
  57. suite = new TestSuite;
  58. suite->name = suiteName;
  59. suites.push_back(suite);
  60. }
  61. else
  62. {
  63. suite = *it;
  64. }
  65. // Sanity check
  66. std::vector<Test*>::iterator it1;
  67. for(it1 = suite->tests.begin(); it1 != suite->tests.end(); it1++)
  68. {
  69. if((*it)->name == name)
  70. {
  71. ANKI_TEST_LOG("Test already exists: %s", name);
  72. return;
  73. }
  74. }
  75. // Add the test
  76. Test* test = new Test;
  77. suite->tests.push_back(test);
  78. test->name = name;
  79. test->suite = suite;
  80. test->callback = callback;
  81. }
  82. int Tester::run(int argc, char** argv)
  83. {
  84. // Parse args
  85. //
  86. programName = argv[0];
  87. std::string helpMessage = "Usage: " + programName + R"( [options]
  88. Options:
  89. --help Print this message
  90. --list-tests List all the tests
  91. --suite <name> Run tests only from this suite
  92. --test <name> Run this test. --suite needs to be specified)";
  93. std::string suiteName;
  94. std::string testName;
  95. for(int i = 1; i < argc; i++)
  96. {
  97. const char* arg = argv[i];
  98. if(strcmp(arg, "--list-tests") == 0)
  99. {
  100. listTests();
  101. return 0;
  102. }
  103. else if(strcmp(arg, "--help") == 0)
  104. {
  105. ANKI_TEST_LOG("%s", helpMessage.c_str());
  106. return 0;
  107. }
  108. else if(strcmp(arg, "--suite") == 0)
  109. {
  110. ++i;
  111. if(i >= argc)
  112. {
  113. ANKI_TEST_LOG("%s", "<name> is missing after --suite");
  114. return 1;
  115. }
  116. suiteName = argv[i];
  117. }
  118. else if(strcmp(arg, "--test") == 0)
  119. {
  120. ++i;
  121. if(i >= argc)
  122. {
  123. ANKI_TEST_LOG("%s", "<name> is missing after --test");
  124. return 1;
  125. }
  126. testName = argv[i];
  127. }
  128. }
  129. // Sanity check
  130. if(testName.length() > 0 && suiteName.length() == 0)
  131. {
  132. ANKI_TEST_LOG("%s", "Specify --suite as well");
  133. return 1;
  134. }
  135. // Run tests
  136. //
  137. int passed = 0;
  138. int run = 0;
  139. if(argc == 1)
  140. {
  141. // Run all
  142. for(TestSuite* suite : suites)
  143. {
  144. for(Test* test : suite->tests)
  145. {
  146. ++run;
  147. test->run();
  148. ++passed;
  149. }
  150. }
  151. }
  152. else
  153. {
  154. for(TestSuite* suite : suites)
  155. {
  156. if(suite->name == suiteName)
  157. {
  158. for(Test* test : suite->tests)
  159. {
  160. if(test->name == testName || testName.length() == 0)
  161. {
  162. ++run;
  163. test->run();
  164. ++passed;
  165. }
  166. }
  167. }
  168. }
  169. }
  170. int failed = run - passed;
  171. ANKI_TEST_LOG("========\nRun %d tests, failed %d", run, failed);
  172. if(failed == 0)
  173. {
  174. ANKI_TEST_LOG("%s", "SUCCESS!");
  175. }
  176. else
  177. {
  178. ANKI_TEST_LOG("%s", "FAILURE");
  179. }
  180. return run - passed;
  181. }
  182. int Tester::listTests()
  183. {
  184. for(TestSuite* suite : suites)
  185. {
  186. for(Test* test : suite->tests)
  187. {
  188. ANKI_TEST_LOG("%s --suite %s --test %s", programName.c_str(), suite->name.c_str(), test->name.c_str());
  189. }
  190. }
  191. return 0;
  192. }
  193. static Tester* g_testerInstance = nullptr;
  194. Tester& getTesterSingleton()
  195. {
  196. return *(g_testerInstance ? g_testerInstance : (g_testerInstance = new Tester));
  197. }
  198. void deleteTesterSingleton()
  199. {
  200. delete g_testerInstance;
  201. }
  202. void initConfig(ConfigSet& cfg)
  203. {
  204. cfg.set("width", 1920);
  205. cfg.set("height", 1080);
  206. cfg.set("rsrc_dataPaths", ".:..");
  207. }
  208. NativeWindow* createWindow(ConfigSet& cfg)
  209. {
  210. NativeWindowInitInfo inf;
  211. inf.m_allocCallback = allocAligned;
  212. inf.m_width = cfg.getNumberU32("width");
  213. inf.m_height = cfg.getNumberU32("height");
  214. inf.m_title = "AnKi unit tests";
  215. NativeWindow* win;
  216. const Error err = NativeWindow::newInstance(inf, win);
  217. if(err)
  218. {
  219. return nullptr;
  220. }
  221. cfg.set("width", win->getWidth());
  222. cfg.set("height", win->getHeight());
  223. return win;
  224. }
  225. GrManager* createGrManager(const ConfigSet& cfg, NativeWindow* win)
  226. {
  227. GrManagerInitInfo inf;
  228. inf.m_allocCallback = allocAligned;
  229. StringAuto home(HeapAllocator<U8>(allocAligned, nullptr));
  230. const Error err = getTempDirectory(home);
  231. if(err)
  232. {
  233. return nullptr;
  234. }
  235. inf.m_cacheDirectory = home;
  236. inf.m_config = &cfg;
  237. inf.m_window = win;
  238. GrManager* gr;
  239. ANKI_TEST_EXPECT_NO_ERR(GrManager::newInstance(inf, gr));
  240. return gr;
  241. }
  242. ResourceManager* createResourceManager(const ConfigSet& cfg, GrManager* gr, PhysicsWorld*& physics,
  243. ResourceFilesystem*& resourceFs)
  244. {
  245. HeapAllocator<U8> alloc(allocAligned, nullptr);
  246. physics = new PhysicsWorld();
  247. ANKI_TEST_EXPECT_NO_ERR(physics->init(allocAligned, nullptr));
  248. resourceFs = new ResourceFilesystem(alloc);
  249. ANKI_TEST_EXPECT_NO_ERR(resourceFs->init(cfg, "./"));
  250. ResourceManagerInitInfo rinit;
  251. rinit.m_gr = gr;
  252. rinit.m_physics = physics;
  253. rinit.m_resourceFs = resourceFs;
  254. rinit.m_config = &cfg;
  255. rinit.m_cacheDir = "./";
  256. rinit.m_allocCallback = allocAligned;
  257. rinit.m_allocCallbackData = nullptr;
  258. ResourceManager* resources = new ResourceManager();
  259. ANKI_TEST_EXPECT_NO_ERR(resources->init(rinit));
  260. return resources;
  261. }
  262. } // end namespace anki