UnitTestFramework.cpp 5.4 KB

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