-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add test for ensuring resolution of steady_clock
- Loading branch information
Showing
2 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include <algorithm> | ||
#include <chrono> | ||
#include <ranges> | ||
#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); | ||
}; |