UnitTestFramework.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // Install callbacks
  49. Trace = TraceImpl;
  50. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  51. #ifdef _DEBUG
  52. // Enable leak detection
  53. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  54. #endif
  55. // Enable floating point exceptions
  56. FPExceptionsEnable enable_exceptions;
  57. JPH_UNUSED(enable_exceptions);
  58. // Create a factory
  59. Factory::sInstance = new Factory();
  60. // Register physics types
  61. RegisterTypes();
  62. int rv = Context().run();
  63. // Destroy the factory
  64. delete Factory::sInstance;
  65. Factory::sInstance = nullptr;
  66. return rv;
  67. }
  68. #elif !defined(JPH_PLATFORM_ANDROID)
  69. // Generic entry point
  70. int main(int argc, char** argv)
  71. {
  72. // Install callbacks
  73. Trace = TraceImpl;
  74. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  75. #if defined(JPH_PLATFORM_WINDOWS) && defined(_DEBUG)
  76. // Enable leak detection
  77. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  78. #endif
  79. // Enable floating point exceptions
  80. FPExceptionsEnable enable_exceptions;
  81. JPH_UNUSED(enable_exceptions);
  82. // Create a factory
  83. Factory::sInstance = new Factory();
  84. // Register physics types
  85. RegisterTypes();
  86. int rv = Context(argc, argv).run();
  87. // Destroy the factory
  88. delete Factory::sInstance;
  89. Factory::sInstance = nullptr;
  90. return rv;
  91. }
  92. #else // !JPH_PLATFORM_ANDROID
  93. // Reporter that writes logs to the Android log
  94. struct LogReporter : public ConsoleReporter
  95. {
  96. LogReporter(const ContextOptions &inOptions) :
  97. ConsoleReporter(inOptions, mStream)
  98. {
  99. }
  100. #define REPORTER_OVERRIDE(func, type, arg) \
  101. void func(type arg) override \
  102. { \
  103. ConsoleReporter::func(arg); \
  104. const char *str = mStream.str().c_str(); \
  105. if (str[0] != 0) \
  106. __android_log_write(ANDROID_LOG_INFO, "Jolt", str); \
  107. mStream.str(""); \
  108. }
  109. REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  110. REPORTER_OVERRIDE(test_run_end, const TestRunStats &, in)
  111. REPORTER_OVERRIDE(test_case_start, const TestCaseData &, in)
  112. REPORTER_OVERRIDE(test_case_reenter, const TestCaseData &, in)
  113. REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats &, in)
  114. REPORTER_OVERRIDE(test_case_exception, const TestCaseException &, in)
  115. REPORTER_OVERRIDE(subcase_start, const SubcaseSignature &, in)
  116. REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  117. REPORTER_OVERRIDE(log_assert, const AssertData &, in)
  118. REPORTER_OVERRIDE(log_message, const MessageData &, in)
  119. REPORTER_OVERRIDE(test_case_skipped, const TestCaseData &, in)
  120. private:
  121. thread_local static std::ostringstream mStream;
  122. };
  123. thread_local std::ostringstream LogReporter::mStream;
  124. DOCTEST_REGISTER_REPORTER("android_log", 0, LogReporter);
  125. void AndroidInitialize(android_app *inApp)
  126. {
  127. // Install callbacks
  128. Trace = TraceImpl;
  129. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  130. // Enable floating point exceptions
  131. FPExceptionsEnable enable_exceptions;
  132. JPH_UNUSED(enable_exceptions);
  133. // Create a factory
  134. Factory::sInstance = new Factory();
  135. // Register physics types
  136. RegisterTypes();
  137. // Run all tests
  138. Context context;
  139. context.addFilter("reporters", "android_log");
  140. int return_value = context.run();
  141. // Color the screen according to the test result
  142. JPH::Color color = return_value == 0? JPH::Color::sGreen : JPH::Color::sRed;
  143. ANativeWindow_acquire(inApp->window);
  144. ANativeWindow_Buffer buffer;
  145. ARect bounds;
  146. ANativeWindow_lock(inApp->window, &buffer, &bounds);
  147. switch (buffer.format)
  148. {
  149. case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
  150. case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
  151. {
  152. uint32 color_u32 = color.GetUInt32();
  153. for (int y = 0; y < buffer.height; ++y)
  154. {
  155. uint32 *dest = (uint32 *)((uint8 *)buffer.bits + y * buffer.stride * sizeof(uint32));
  156. for (int x = 0; x < buffer.width; ++x)
  157. *dest++ = color_u32;
  158. }
  159. break;
  160. }
  161. default:
  162. // TODO implement
  163. break;
  164. }
  165. ANativeWindow_unlockAndPost(inApp->window);
  166. ANativeWindow_release(inApp->window);
  167. // Destroy the factory
  168. delete Factory::sInstance;
  169. Factory::sInstance = nullptr;
  170. }
  171. // Handle callback from Android
  172. void AndroidHandleCommand(android_app *inApp, int32_t inCmd)
  173. {
  174. switch (inCmd)
  175. {
  176. case APP_CMD_INIT_WINDOW:
  177. AndroidInitialize(inApp);
  178. break;
  179. }
  180. }
  181. // Main entry point for android
  182. void android_main(struct android_app *ioApp)
  183. {
  184. ioApp->onAppCmd = AndroidHandleCommand;
  185. int events;
  186. android_poll_source *source;
  187. do
  188. {
  189. if (ALooper_pollAll(1, nullptr, &events, (void **)&source) >= 0 && source != nullptr)
  190. source->process(ioApp, source);
  191. } while (ioApp->destroyRequested == 0);
  192. }
  193. #endif // JPH_PLATFORM_ANDROID