UnitTestFramework.cpp 4.6 KB

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