UnitTestFramework.cpp 6.1 KB

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