UnitTestFramework.cpp 9.9 KB

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