UnitTestFramework.cpp 6.0 KB

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