UnitTestFramework.cpp 10 KB

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