UnitTestFramework.cpp 10 KB

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