feat(Time): add MonotonicClock/CivilClock/ManualClock and bounded Stopwatch (M-TIME-02)
This commit is contained in:
1 parent
7f3395cb74
commit
4227348051
4 files changed
+471
-45
No files matched your search
@@ -0,0 +1,101 @@
|
||||
#include <Void/Time/Clock.hpp>
|
||||
#include <Void/Time/Stopwatch.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <limits>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Void::Testing::Test manual_clock_starts_at_epoch_and_advances()
|
||||
{
|
||||
Void::Time::ManualClock clock;
|
||||
Void::Testing::expect_eq(clock.now().time_since_epoch().count(),
|
||||
std::int64_t{0});
|
||||
|
||||
const auto adv = clock.advance(25ms);
|
||||
Void::Testing::expect(adv.has_value());
|
||||
Void::Testing::expect_eq(clock.now().time_since_epoch(), 25ms);
|
||||
}
|
||||
|
||||
Void::Testing::Test manual_clock_rejects_negative_and_overflow()
|
||||
{
|
||||
Void::Time::ManualClock clock{
|
||||
Void::Time::MonotonicClock::time_point{100ns}};
|
||||
|
||||
const auto neg = clock.advance(-1ns);
|
||||
Void::Testing::expect(!neg.has_value());
|
||||
Void::Testing::expect(neg.error() == Void::Time::ClockError::negative_step);
|
||||
Void::Testing::expect_eq(clock.now().time_since_epoch(), 100ns);
|
||||
|
||||
const auto max_step = std::chrono::nanoseconds{
|
||||
std::numeric_limits<std::int64_t>::max()};
|
||||
const auto ovf = clock.advance(max_step);
|
||||
Void::Testing::expect(!ovf.has_value());
|
||||
Void::Testing::expect(ovf.error() == Void::Time::ClockError::overflow);
|
||||
Void::Testing::expect_eq(clock.now().time_since_epoch(), 100ns);
|
||||
|
||||
const auto back_set = clock.set(Void::Time::MonotonicClock::time_point{50ns});
|
||||
Void::Testing::expect(!back_set.has_value());
|
||||
Void::Testing::expect(back_set.error() ==
|
||||
Void::Time::ClockError::negative_step);
|
||||
}
|
||||
|
||||
Void::Testing::Test manual_civil_clock_supports_backward_set()
|
||||
{
|
||||
Void::Time::ManualCivilClock civil{
|
||||
Void::Time::CivilClock::time_point{1000s}};
|
||||
Void::Testing::expect(civil.advance(5s).has_value());
|
||||
Void::Testing::expect_eq(
|
||||
std::chrono::duration_cast<std::chrono::seconds>(
|
||||
civil.now().time_since_epoch()),
|
||||
1005s);
|
||||
|
||||
civil.set(Void::Time::CivilClock::time_point{900s});
|
||||
Void::Testing::expect_eq(
|
||||
std::chrono::duration_cast<std::chrono::seconds>(
|
||||
civil.now().time_since_epoch()),
|
||||
900s);
|
||||
}
|
||||
|
||||
Void::Testing::Test remaining_until_clamps_expired_deadlines()
|
||||
{
|
||||
Void::Time::ManualClock clock;
|
||||
const auto deadline = clock.now() + 50ms;
|
||||
Void::Testing::expect_eq(Void::Time::remaining_until(clock, deadline),
|
||||
50ms);
|
||||
|
||||
Void::Testing::expect(clock.advance(80ms).has_value());
|
||||
Void::Testing::expect_eq(Void::Time::remaining_until(clock, deadline), 0ns);
|
||||
}
|
||||
|
||||
Void::Testing::Test manual_clock_concurrent_advances()
|
||||
{
|
||||
Void::Time::ManualClock clock;
|
||||
std::vector<std::thread> threads;
|
||||
for (int t = 0; t < 4; ++t) {
|
||||
threads.emplace_back([&clock]() {
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
(void)clock.advance(1ms);
|
||||
}
|
||||
});
|
||||
}
|
||||
for (auto& th : threads) {
|
||||
th.join();
|
||||
}
|
||||
Void::Testing::expect_eq(clock.now().time_since_epoch(), 4000ms);
|
||||
}
|
||||
|
||||
Void::Testing::Test scoped_timer_with_manual_clock()
|
||||
{
|
||||
Void::Time::ManualClock clock;
|
||||
Void::Time::Stopwatch::Duration out{};
|
||||
{
|
||||
Void::Time::ScopedTimer timer{out, clock};
|
||||
Void::Testing::expect(clock.advance(42ms).has_value());
|
||||
}
|
||||
Void::Testing::expect_eq(out, 42ms);
|
||||
}
|
||||
Reference in new issue
Block a user