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 4, 2023
1 parent 1c4408a commit 90825e3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <gtest/gtest.h>

#include <algorithm>
#include <chrono>
#include <vector>

namespace {

static constexpr auto kMinResolution = std::chrono::milliseconds(1);
static constexpr auto kNumIterations = 1000;

} // namespace

using namespace std::chrono_literals;

TEST(ChronoClockResolutionTest, SteadyClockTicksAreSmallerThan1Ms) {
std::vector<std::chrono::nanoseconds> ticks;
ticks.reserve(kNumIterations);
for (int i = 0; i < kNumIterations; ++i) {
const auto start = std::chrono::steady_clock::now();
const auto end = std::chrono::steady_clock::now();
ticks.push_back(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);
};

0 comments on commit 90825e3

Please sign in to comment.