Skip to content

Commit

Permalink
feat: add test for ensuring resolution of steady_clock
Browse files Browse the repository at this point in the history
  • Loading branch information
Swiftb0y committed Sep 6, 2023
1 parent 1c4408a commit 5523d9b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/test/chrono_clock_resolution_test.cpp
@@ -0,0 +1,30 @@
#include <gtest/gtest.h>

#include <algorithm>
#include <chrono>
#include <ranges>

Check failure on line 5 in src/test/chrono_clock_resolution_test.cpp

View workflow job for this annotation

GitHub Actions / macOS 11 x64

'ranges' file not found

Check failure on line 5 in src/test/chrono_clock_resolution_test.cpp

View workflow job for this annotation

GitHub Actions / macOS 11 arm64

'ranges' file not found
#include <vector>

using namespace std::chrono_literals;

namespace {

static constexpr auto kMinResolution = 1ms;
static constexpr auto kNumIterations = 1000;

} // namespace

TEST(ChronoClockResolutionTest, SteadyClockTicksAreSmallerThan1Ms) {
std::vector<std::chrono::nanoseconds> 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::ranges::all_of(ticks, [](const auto& tick) {
return tick >= 0ns;
}));
// make sure that at least one duration is smaller than the minimum resolution
ASSERT_LT(*std::ranges::min_element(ticks), kMinResolution);
};

0 comments on commit 5523d9b

Please sign in to comment.