UnitTestFramework.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt/Jolt.h>
  4. #include <Jolt/Core/FPException.h>
  5. #include <Jolt/Core/Factory.h>
  6. #include <Jolt/RegisterTypes.h>
  7. #ifdef JPH_PLATFORM_WINDOWS
  8. #include <crtdbg.h>
  9. #endif // JPH_PLATFORM_WINDOWS
  10. #ifdef JPH_PLATFORM_ANDROID
  11. #include <Jolt/Core/Color.h>
  12. #include <android/log.h>
  13. #include <android_native_app_glue.h>
  14. #endif // JPH_PLATFORM_ANDROID
  15. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  16. #include <cstdarg>
  17. JPH_SUPPRESS_WARNINGS_STD_END
  18. using namespace JPH;
  19. // Emit everything needed for the main function
  20. #define DOCTEST_CONFIG_IMPLEMENT
  21. #define DOCTEST_CONFIG_NO_WINDOWS_SEH
  22. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  23. #include "doctest.h"
  24. JPH_SUPPRESS_WARNINGS_STD_END
  25. using namespace doctest;
  26. // Disable common warnings triggered by Jolt
  27. JPH_SUPPRESS_WARNINGS
  28. // Callback for traces
  29. static void TraceImpl(const char *inFMT, ...)
  30. {
  31. // Format the message
  32. va_list list;
  33. va_start(list, inFMT);
  34. char buffer[1024];
  35. vsnprintf(buffer, sizeof(buffer), inFMT, list);
  36. va_end(list);
  37. // Forward to doctest
  38. MESSAGE(buffer);
  39. }
  40. #ifdef JPH_ENABLE_ASSERTS
  41. // Callback for asserts
  42. static bool AssertFailedImpl(const char *inExpression, const char *inMessage, const char *inFile, uint inLine)
  43. {
  44. // Format message
  45. char buffer[1024];
  46. snprintf(buffer, sizeof(buffer), "%s:%u: (%s) %s", inFile, inLine, inExpression, inMessage != nullptr? inMessage : "");
  47. // Forward to doctest
  48. FAIL_CHECK(buffer);
  49. // No breakpoint
  50. return false;
  51. }
  52. #endif // JPH_ENABLE_ASSERTS
  53. #ifdef JPH_PLATFORM_WINDOWS_UWP
  54. JPH_SUPPRESS_WARNING_PUSH
  55. JPH_MSVC_SUPPRESS_WARNING(4265) // warning C4265: 'winrt::impl::implements_delegate<winrt::Windows::UI::Core::DispatchedHandler,H>': class has virtual functions, but its non-trivial destructor is not virtual; instances of this class may not be destructed correctly
  56. JPH_MSVC_SUPPRESS_WARNING(4668) // warning C4668: '_MANAGED' is not defined as a preprocessor macro, replacing with '0' for '#if/#elif'
  57. JPH_MSVC_SUPPRESS_WARNING(4946) // warning C4946: reinterpret_cast used between related classes: 'winrt::impl::abi<winrt::Windows::ApplicationModel::Core::IFrameworkViewSource,void>::type' and 'winrt::impl::abi<winrt::Windows::Foundation::IUnknown,void>::type'
  58. JPH_MSVC_SUPPRESS_WARNING(5039) // winbase.h(13179): warning C5039: 'TpSetCallbackCleanupGroup': pointer or reference to potentially throwing function passed to 'extern "C"' function under -EHc. Undefined behavior may occur if this function throws an exception.
  59. JPH_MSVC_SUPPRESS_WARNING(5204) // warning C5204: 'winrt::impl::produce_base<D,winrt::Windows::ApplicationModel::Core::IFrameworkViewSource,void>': class has virtual functions, but its trivial destructor is not virtual; instances of objects derived from this class may not be destructed correctly
  60. JPH_MSVC_SUPPRESS_WARNING(5246) // warning C5246: '_Elems': the initialization of a subobject should be wrapped in braces
  61. #include <winrt/Windows.Foundation.h>
  62. #include <winrt/Windows.Foundation.Collections.h>
  63. #include <winrt/Windows.ApplicationModel.Core.h>
  64. #include <winrt/Windows.UI.Core.h>
  65. #include <winrt/Windows.UI.Composition.h>
  66. #include <winrt/Windows.UI.Input.h>
  67. JPH_SUPPRESS_WARNING_POP
  68. using namespace winrt;
  69. using namespace Windows;
  70. using namespace Windows::ApplicationModel::Core;
  71. using namespace Windows::UI;
  72. using namespace Windows::UI::Core;
  73. using namespace Windows::UI::Composition;
  74. struct App : implements<App, IFrameworkViewSource, IFrameworkView>
  75. {
  76. CompositionTarget mTarget { nullptr };
  77. IFrameworkView CreateView()
  78. {
  79. return *this;
  80. }
  81. void Initialize(CoreApplicationView const&)
  82. {
  83. }
  84. void Load(hstring const&)
  85. {
  86. }
  87. void Uninitialize()
  88. {
  89. }
  90. void Run()
  91. {
  92. CoreWindow window = CoreWindow::GetForCurrentThread();
  93. window.Activate();
  94. CoreDispatcher dispatcher = window.Dispatcher();
  95. dispatcher.ProcessEvents(CoreProcessEventsOption::ProcessUntilQuit);
  96. }
  97. void SetWindow(CoreWindow const& inWindow)
  98. {
  99. // Register allocation hook
  100. RegisterDefaultAllocator();
  101. // Install callbacks
  102. Trace = TraceImpl;
  103. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  104. #ifdef _DEBUG
  105. // Enable leak detection
  106. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  107. #endif
  108. // Enable floating point exceptions
  109. FPExceptionsEnable enable_exceptions;
  110. JPH_UNUSED(enable_exceptions);
  111. // Create a factory
  112. Factory::sInstance = new Factory();
  113. // Register physics types
  114. RegisterTypes();
  115. // Run the tests
  116. int rv = Context().run();
  117. // Destroy the factory
  118. delete Factory::sInstance;
  119. Factory::sInstance = nullptr;
  120. // Color the screen according to the result
  121. Compositor compositor;
  122. ContainerVisual root = compositor.CreateContainerVisual();
  123. mTarget = compositor.CreateTargetForCurrentView();
  124. mTarget.Root(root);
  125. SpriteVisual visual = compositor.CreateSpriteVisual();
  126. visual.Brush(compositor.CreateColorBrush(rv != 0 ? Windows::UI::Color { 0xff, 0xff, 0x00, 0x00 } : Windows::UI::Color { 0xff, 0x00, 0xff, 0x00 }));
  127. visual.Size({ inWindow.Bounds().Width, inWindow.Bounds().Height });
  128. visual.Offset({ 0, 0, 0, });
  129. root.Children().InsertAtTop(visual);
  130. }
  131. };
  132. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  133. {
  134. CoreApplication::Run(make<App>());
  135. }
  136. #elif !defined(JPH_PLATFORM_ANDROID)
  137. // Generic entry point
  138. int main(int argc, char** argv)
  139. {
  140. // Register allocation hook
  141. RegisterDefaultAllocator();
  142. // Install callbacks
  143. Trace = TraceImpl;
  144. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  145. #if defined(JPH_PLATFORM_WINDOWS) && defined(_DEBUG)
  146. // Enable leak detection
  147. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  148. #endif
  149. // Enable floating point exceptions
  150. FPExceptionsEnable enable_exceptions;
  151. JPH_UNUSED(enable_exceptions);
  152. // Create a factory
  153. Factory::sInstance = new Factory();
  154. // Register physics types
  155. RegisterTypes();
  156. int rv = Context(argc, argv).run();
  157. // Destroy the factory
  158. delete Factory::sInstance;
  159. Factory::sInstance = nullptr;
  160. return rv;
  161. }
  162. #else // !JPH_PLATFORM_ANDROID
  163. // Reporter that writes logs to the Android log
  164. struct LogReporter : public ConsoleReporter
  165. {
  166. LogReporter(const ContextOptions &inOptions) :
  167. ConsoleReporter(inOptions, mStream)
  168. {
  169. }
  170. #define REPORTER_OVERRIDE(func, type, arg) \
  171. void func(type arg) override \
  172. { \
  173. ConsoleReporter::func(arg); \
  174. string str = mStream.str(); \
  175. if (!str.empty()) \
  176. __android_log_write(ANDROID_LOG_INFO, "Jolt", str.c_str()); \
  177. mStream.str(""); \
  178. }
  179. REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  180. REPORTER_OVERRIDE(test_run_end, const TestRunStats &, in)
  181. REPORTER_OVERRIDE(test_case_start, const TestCaseData &, in)
  182. REPORTER_OVERRIDE(test_case_reenter, const TestCaseData &, in)
  183. REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats &, in)
  184. REPORTER_OVERRIDE(test_case_exception, const TestCaseException &, in)
  185. REPORTER_OVERRIDE(subcase_start, const SubcaseSignature &, in)
  186. REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  187. REPORTER_OVERRIDE(log_assert, const AssertData &, in)
  188. REPORTER_OVERRIDE(log_message, const MessageData &, in)
  189. REPORTER_OVERRIDE(test_case_skipped, const TestCaseData &, in)
  190. private:
  191. thread_local static std::ostringstream mStream;
  192. };
  193. thread_local std::ostringstream LogReporter::mStream;
  194. DOCTEST_REGISTER_REPORTER("android_log", 0, LogReporter);
  195. void AndroidInitialize(android_app *inApp)
  196. {
  197. // Register allocation hook
  198. RegisterDefaultAllocator();
  199. // Install callbacks
  200. Trace = TraceImpl;
  201. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  202. // Enable floating point exceptions
  203. FPExceptionsEnable enable_exceptions;
  204. JPH_UNUSED(enable_exceptions);
  205. // Create a factory
  206. Factory::sInstance = new Factory();
  207. // Register physics types
  208. RegisterTypes();
  209. // Run all tests
  210. Context context;
  211. context.addFilter("reporters", "android_log");
  212. int return_value = context.run();
  213. // Color the screen according to the test result
  214. JPH::Color color = return_value == 0? JPH::Color::sGreen : JPH::Color::sRed;
  215. ANativeWindow_acquire(inApp->window);
  216. ANativeWindow_Buffer buffer;
  217. ARect bounds;
  218. ANativeWindow_lock(inApp->window, &buffer, &bounds);
  219. switch (buffer.format)
  220. {
  221. case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
  222. case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
  223. {
  224. uint32 color_u32 = color.GetUInt32();
  225. for (int y = 0; y < buffer.height; ++y)
  226. {
  227. uint32 *dest = (uint32 *)((uint8 *)buffer.bits + y * buffer.stride * sizeof(uint32));
  228. for (int x = 0; x < buffer.width; ++x)
  229. *dest++ = color_u32;
  230. }
  231. break;
  232. }
  233. default:
  234. // TODO implement
  235. break;
  236. }
  237. ANativeWindow_unlockAndPost(inApp->window);
  238. ANativeWindow_release(inApp->window);
  239. // Destroy the factory
  240. delete Factory::sInstance;
  241. Factory::sInstance = nullptr;
  242. }
  243. // Handle callback from Android
  244. void AndroidHandleCommand(android_app *inApp, int32_t inCmd)
  245. {
  246. switch (inCmd)
  247. {
  248. case APP_CMD_INIT_WINDOW:
  249. AndroidInitialize(inApp);
  250. break;
  251. }
  252. }
  253. // Main entry point for android
  254. void android_main(struct android_app *ioApp)
  255. {
  256. ioApp->onAppCmd = AndroidHandleCommand;
  257. int events;
  258. android_poll_source *source;
  259. do
  260. {
  261. if (ALooper_pollAll(1, nullptr, &events, (void **)&source) >= 0 && source != nullptr)
  262. source->process(ioApp, source);
  263. } while (ioApp->destroyRequested == 0);
  264. }
  265. #endif // JPH_PLATFORM_ANDROID