From 865bae8e30a04bba03796731887fee37afce6fe7 Mon Sep 17 00:00:00 2001 From: Swiftb0y <12380386+Swiftb0y@users.noreply.github.com> Date: Tue, 29 Aug 2023 22:36:49 +0200 Subject: [PATCH] feat: add test for ensuring resolution of steady_clock --- CMakeLists.txt | 1 + src/test/chrono_clock_resolution_test.cpp | 29 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/test/chrono_clock_resolution_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f8841783c5b..a38d409bc4b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1823,6 +1823,7 @@ add_executable(mixxx-test src/test/broadcastsettings_test.cpp src/test/cache_test.cpp src/test/channelhandle_test.cpp + src/test/chrono_clock_resolution_test.cpp src/test/colorconfig_test.cpp src/test/colormapperjsproxy_test.cpp src/test/colorpalette_test.cpp diff --git a/src/test/chrono_clock_resolution_test.cpp b/src/test/chrono_clock_resolution_test.cpp new file mode 100644 index 00000000000..7ab5fabf28b --- /dev/null +++ b/src/test/chrono_clock_resolution_test.cpp @@ -0,0 +1,29 @@ +#include + +#include +#include +#include + +using namespace std::chrono_literals; + +namespace { + +static constexpr auto kMinResolution = 1ms; +static constexpr auto kNumIterations = 1000; + +} // namespace + +TEST(ChronoClockResolutionTest, SteadyClockTicksAreSmallerThan1Ms) { + std::vector ticks; + std::generate_n(std::back_inserter(ticks), kNumIterations, []() { + const auto start = std::chrono::steady_clock::now(); + const auto end = std::chrono::steady_clock::now(); + return end - start; + }); + // Make sure there is no aliasing going on. + ASSERT_TRUE(std::all_of(ticks.begin(), ticks.end(), [](const auto& tick) { + return tick >= 0ns; + })); + // make sure that at least one duration is smaller than the minimum resolution + ASSERT_LT(*std::min_element(ticks.begin(), ticks.end()), kMinResolution); +};