-
-
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 <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); | ||
}; |