2
0

UnitTestFramework.cpp 10.0 KB

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