UnitTestFramework.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. // Unregisters all types with the factory and cleans up the default material
  120. UnregisterTypes();
  121. // Destroy the factory
  122. delete Factory::sInstance;
  123. Factory::sInstance = nullptr;
  124. // Color the screen according to the result
  125. Compositor compositor;
  126. ContainerVisual root = compositor.CreateContainerVisual();
  127. mTarget = compositor.CreateTargetForCurrentView();
  128. mTarget.Root(root);
  129. SpriteVisual visual = compositor.CreateSpriteVisual();
  130. visual.Brush(compositor.CreateColorBrush(rv != 0 ? Windows::UI::Color { 0xff, 0xff, 0x00, 0x00 } : Windows::UI::Color { 0xff, 0x00, 0xff, 0x00 }));
  131. visual.Size({ inWindow.Bounds().Width, inWindow.Bounds().Height });
  132. visual.Offset({ 0, 0, 0, });
  133. root.Children().InsertAtTop(visual);
  134. }
  135. };
  136. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
  137. {
  138. CoreApplication::Run(make<App>());
  139. }
  140. #elif !defined(JPH_PLATFORM_ANDROID)
  141. // Generic entry point
  142. int main(int argc, char** argv)
  143. {
  144. // Show used instruction sets
  145. std::cout << GetConfigurationString() << std::endl;
  146. // Register allocation hook
  147. RegisterDefaultAllocator();
  148. // Install callbacks
  149. Trace = TraceImpl;
  150. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  151. #if defined(JPH_PLATFORM_WINDOWS) && defined(_DEBUG)
  152. // Enable leak detection
  153. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
  154. #endif
  155. // Enable floating point exceptions
  156. FPExceptionsEnable enable_exceptions;
  157. JPH_UNUSED(enable_exceptions);
  158. // Create a factory
  159. Factory::sInstance = new Factory();
  160. // Register physics types
  161. RegisterTypes();
  162. int rv = Context(argc, argv).run();
  163. // Unregisters all types with the factory and cleans up the default material
  164. UnregisterTypes();
  165. // Destroy the factory
  166. delete Factory::sInstance;
  167. Factory::sInstance = nullptr;
  168. return rv;
  169. }
  170. #else // !JPH_PLATFORM_ANDROID
  171. // Reporter that writes logs to the Android log
  172. struct LogReporter : public ConsoleReporter
  173. {
  174. LogReporter(const ContextOptions &inOptions) :
  175. ConsoleReporter(inOptions, mStream)
  176. {
  177. }
  178. #define REPORTER_OVERRIDE(func, type, arg) \
  179. void func(type arg) override \
  180. { \
  181. ConsoleReporter::func(arg); \
  182. string str = mStream.str(); \
  183. if (!str.empty()) \
  184. __android_log_write(ANDROID_LOG_INFO, "Jolt", str.c_str()); \
  185. mStream.str(""); \
  186. }
  187. REPORTER_OVERRIDE(test_run_start, DOCTEST_EMPTY, DOCTEST_EMPTY)
  188. REPORTER_OVERRIDE(test_run_end, const TestRunStats &, in)
  189. REPORTER_OVERRIDE(test_case_start, const TestCaseData &, in)
  190. REPORTER_OVERRIDE(test_case_reenter, const TestCaseData &, in)
  191. REPORTER_OVERRIDE(test_case_end, const CurrentTestCaseStats &, in)
  192. REPORTER_OVERRIDE(test_case_exception, const TestCaseException &, in)
  193. REPORTER_OVERRIDE(subcase_start, const SubcaseSignature &, in)
  194. REPORTER_OVERRIDE(subcase_end, DOCTEST_EMPTY, DOCTEST_EMPTY)
  195. REPORTER_OVERRIDE(log_assert, const AssertData &, in)
  196. REPORTER_OVERRIDE(log_message, const MessageData &, in)
  197. REPORTER_OVERRIDE(test_case_skipped, const TestCaseData &, in)
  198. private:
  199. thread_local static std::ostringstream mStream;
  200. };
  201. thread_local std::ostringstream LogReporter::mStream;
  202. DOCTEST_REGISTER_REPORTER("android_log", 0, LogReporter);
  203. void AndroidInitialize(android_app *inApp)
  204. {
  205. // Log configuration
  206. __android_log_write(ANDROID_LOG_INFO, "Jolt", GetConfigurationString());
  207. // Register allocation hook
  208. RegisterDefaultAllocator();
  209. // Install callbacks
  210. Trace = TraceImpl;
  211. JPH_IF_ENABLE_ASSERTS(AssertFailed = AssertFailedImpl;)
  212. // Enable floating point exceptions
  213. FPExceptionsEnable enable_exceptions;
  214. JPH_UNUSED(enable_exceptions);
  215. // Create a factory
  216. Factory::sInstance = new Factory();
  217. // Register physics types
  218. RegisterTypes();
  219. // Run all tests
  220. Context context;
  221. context.addFilter("reporters", "android_log");
  222. int return_value = context.run();
  223. // Color the screen according to the test result
  224. JPH::Color color = return_value == 0? JPH::Color::sGreen : JPH::Color::sRed;
  225. ANativeWindow_acquire(inApp->window);
  226. ANativeWindow_Buffer buffer;
  227. ARect bounds;
  228. ANativeWindow_lock(inApp->window, &buffer, &bounds);
  229. switch (buffer.format)
  230. {
  231. case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
  232. case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
  233. {
  234. uint32 color_u32 = color.GetUInt32();
  235. for (int y = 0; y < buffer.height; ++y)
  236. {
  237. uint32 *dest = (uint32 *)((uint8 *)buffer.bits + y * buffer.stride * sizeof(uint32));
  238. for (int x = 0; x < buffer.width; ++x)
  239. *dest++ = color_u32;
  240. }
  241. break;
  242. }
  243. case AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM:
  244. {
  245. uint16 color_u16 = (color.b >> 3) + ((color.g >> 2) << 5) + ((color.r >> 3) << 11);
  246. for (int y = 0; y < buffer.height; ++y)
  247. {
  248. uint16 *dest = (uint16 *) ((uint8 *) buffer.bits + y * buffer.stride * sizeof(uint16));
  249. for (int x = 0; x < buffer.width; ++x)
  250. *dest++ = color_u16;
  251. }
  252. break;
  253. }
  254. default:
  255. // TODO implement
  256. break;
  257. }
  258. ANativeWindow_unlockAndPost(inApp->window);
  259. ANativeWindow_release(inApp->window);
  260. // Unregisters all types with the factory and cleans up the default material
  261. UnregisterTypes();
  262. // Destroy the factory
  263. delete Factory::sInstance;
  264. Factory::sInstance = nullptr;
  265. }
  266. // Handle callback from Android
  267. void AndroidHandleCommand(android_app *inApp, int32_t inCmd)
  268. {
  269. switch (inCmd)
  270. {
  271. case APP_CMD_INIT_WINDOW:
  272. AndroidInitialize(inApp);
  273. break;
  274. }
  275. }
  276. // Main entry point for android
  277. void android_main(struct android_app *ioApp)
  278. {
  279. ioApp->onAppCmd = AndroidHandleCommand;
  280. int events;
  281. android_poll_source *source;
  282. do
  283. {
  284. if (ALooper_pollAll(1, nullptr, &events, (void **)&source) >= 0 && source != nullptr)
  285. source->process(ioApp, source);
  286. } while (ioApp->destroyRequested == 0);
  287. }
  288. #endif // JPH_PLATFORM_ANDROID