UnitTestFramework.cpp 6.1 KB

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