UnitTestFramework.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Core/FPException.h>
  5. #include <Jolt/Core/Factory.h>
  6. #include <Jolt/RegisterTypes.h>
  7. #ifdef JPH_PLATFORM_WINDOWS
  8. #include <crtdbg.h>
  9. #endif // JPH_PLATFORM_WINDOWS
  10. #ifdef JPH_PLATFORM_ANDROID
  11. #include <Jolt/Core/Color.h>
  12. #include <android/log.h>
  13. #include <android_native_app_glue.h>
  14. #endif // JPH_PLATFORM_ANDROID
  15. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  16. #include <cstdarg>
  17. JPH_SUPPRESS_WARNINGS_STD_END
  18. using namespace JPH;
  19. // Emit everything needed for the main function
  20. #define DOCTEST_CONFIG_IMPLEMENT
  21. #define DOCTEST_CONFIG_NO_WINDOWS_SEH
  22. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  23. #include "doctest.h"
  24. JPH_SUPPRESS_WARNINGS_STD_END
  25. using namespace doctest;
  26. // Disable common warnings triggered by Jolt
  27. JPH_SUPPRESS_WARNINGS
  28. // Callback for traces
  29. static void TraceImpl(const char *inFMT, ...)
  30. {
  31. // Format the message
  32. va_list list;
  33. va_start(list, inFMT);
  34. char buffer[1024];
  35. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  36. va_end(list);
  37. // Forward to doctest
  38. MESSAGE(buffer);
  39. }
  40. #ifdef JPH_ENABLE_ASSERTS
  41. // Callback for asserts
  42. static bool AssertFailedImpl(const char *inExpression, const char *inMessage, const char *inFile, uint inLine)
  43. {
  44. // Format message
  45. char buffer[1024];
  46. snprintf(buffer, sizeof(buffer), "%s:%u: (%s) %s", inFile, inLine, inExpression, inMessage != nullptr? inMessage : "");
  47. // Forward to doctest
  48. FAIL_CHECK(buffer);
  49. // No breakpoint
  50. return false;
  51. }
  52. #endif // JPH_ENABLE_ASSERTS
  53. #ifdef JPH_PLATFORM_WINDOWS_UWP
  54. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  55. {
  56. // Register allocation hook
  57. RegisterDefaultAllocator();
  58. // Install callbacks
  59. Trace = TraceImpl;
  60. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  61. #ifdef _DEBUG
  62. // Enable leak detection
  63. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  64. #endif
  65. // Enable floating point exceptions
  66. FPExceptionsEnable enable_exceptions;
  67. JPH_UNUSED(enable_exceptions);
  68. // Create a factory
  69. Factory::sInstance = new Factory();
  70. // Register physics types
  71. RegisterTypes();
  72. int rv = Context().run();
  73. // Destroy the factory
  74. delete Factory::sInstance;
  75. Factory::sInstance = nullptr;
  76. return rv;
  77. }
  78. #elif !defined(JPH_PLATFORM_ANDROID)
  79. // Generic entry point
  80. int main(int argc, char** argv)
  81. {
  82. // Register allocation hook
  83. RegisterDefaultAllocator();
  84. // Install callbacks
  85. Trace = TraceImpl;
  86. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  87. #if defined(JPH_PLATFORM_WINDOWS) && defined(_DEBUG)
  88. // Enable leak detection
  89. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  90. #endif
  91. // Enable floating point exceptions
  92. FPExceptionsEnable enable_exceptions;
  93. JPH_UNUSED(enable_exceptions);
  94. // Create a factory
  95. Factory::sInstance = new Factory();
  96. // Register physics types
  97. RegisterTypes();
  98. int rv = Context(argc, argv).run();
  99. // Destroy the factory
  100. delete Factory::sInstance;
  101. Factory::sInstance = nullptr;
  102. return rv;
  103. }
  104. #else // !JPH_PLATFORM_ANDROID
  105. // Reporter that writes logs to the Android log
  106. struct LogReporter : public ConsoleReporter
  107. {
  108. LogReporter(const ContextOptions &inOptions) :
  109. ConsoleReporter(inOptions, mStream)
  110. {
  111. }
  112. #define REPORTER_OVERRIDE(func, type, arg) \
  113. void func(type arg) override \
  114. { \
  115. ConsoleReporter::func(arg); \
  116. string str = mStream.str(); \
  117. if (!str.empty()) \
  118. __android_log_write(ANDROID_LOG_INFO, "Jolt", str.c_str()); \
  119. mStream.str(""); \
  120. }
  121. REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  122. REPORTER_OVERRIDE(test_run_end, const TestRunStats &, in)
  123. REPORTER_OVERRIDE(test_case_start, const TestCaseData &, in)
  124. REPORTER_OVERRIDE(test_case_reenter, const TestCaseData &, in)
  125. REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats &, in)
  126. REPORTER_OVERRIDE(test_case_exception, const TestCaseException &, in)
  127. REPORTER_OVERRIDE(subcase_start, const SubcaseSignature &, in)
  128. REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  129. REPORTER_OVERRIDE(log_assert, const AssertData &, in)
  130. REPORTER_OVERRIDE(log_message, const MessageData &, in)
  131. REPORTER_OVERRIDE(test_case_skipped, const TestCaseData &, in)
  132. private:
  133. thread_local static std::ostringstream mStream;
  134. };
  135. thread_local std::ostringstream LogReporter::mStream;
  136. DOCTEST_REGISTER_REPORTER("android_log", 0, LogReporter);
  137. void AndroidInitialize(android_app *inApp)
  138. {
  139. // Register allocation hook
  140. RegisterDefaultAllocator();
  141. // Install callbacks
  142. Trace = TraceImpl;
  143. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  144. // Enable floating point exceptions
  145. FPExceptionsEnable enable_exceptions;
  146. JPH_UNUSED(enable_exceptions);
  147. // Create a factory
  148. Factory::sInstance = new Factory();
  149. // Register physics types
  150. RegisterTypes();
  151. // Run all tests
  152. Context context;
  153. context.addFilter("reporters", "android_log");
  154. int return_value = context.run();
  155. // Color the screen according to the test result
  156. JPH::Color color = return_value == 0? JPH::Color::sGreen : JPH::Color::sRed;
  157. ANativeWindow_acquire(inApp->window);
  158. ANativeWindow_Buffer buffer;
  159. ARect bounds;
  160. ANativeWindow_lock(inApp->window, &buffer, &bounds);
  161. switch (buffer.format)
  162. {
  163. case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
  164. case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
  165. {
  166. uint32 color_u32 = color.GetUInt32();
  167. for (int y = 0; y < buffer.height; ++y)
  168. {
  169. uint32 *dest = (uint32 *)((uint8 *)buffer.bits + y * buffer.stride * sizeof(uint32));
  170. for (int x = 0; x < buffer.width; ++x)
  171. *dest++ = color_u32;
  172. }
  173. break;
  174. }
  175. default:
  176. // TODO implement
  177. break;
  178. }
  179. ANativeWindow_unlockAndPost(inApp->window);
  180. ANativeWindow_release(inApp->window);
  181. // Destroy the factory
  182. delete Factory::sInstance;
  183. Factory::sInstance = nullptr;
  184. }
  185. // Handle callback from Android
  186. void AndroidHandleCommand(android_app *inApp, int32_t inCmd)
  187. {
  188. switch (inCmd)
  189. {
  190. case APP_CMD_INIT_WINDOW:
  191. AndroidInitialize(inApp);
  192. break;
  193. }
  194. }
  195. // Main entry point for android
  196. void android_main(struct android_app *ioApp)
  197. {
  198. ioApp->onAppCmd = AndroidHandleCommand;
  199. int events;
  200. android_poll_source *source;
  201. do
  202. {
  203. if (ALooper_pollAll(1, nullptr, &events, (void **)&source) >= 0 && source != nullptr)
  204. source->process(ioApp, source);
  205. } while (ioApp->destroyRequested == 0);
  206. }
  207. #endif // JPH_PLATFORM_ANDROID