UnitTestFramework.cpp 4.9 KB

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