UnitTestFramework.cpp 9.9 KB

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