feat(Time): add MonotonicClock/CivilClock/ManualClock and bounded Stopwatch (M-TIME-02)

This commit is contained in:
faumaray committed 2026-11-24 02:46:25 +03:00
1 parent 7f3395cb74
commit 4227348051
4 files changed
+471 -45

No files matched your search

+55
View File
@@ -137,3 +137,58 @@ Void::Testing::Test scoped_timer_written_on_destruction()
(void)d;
Void::Testing::expect(true); // ScopedTimer destructor ran without error
}
Void::Testing::Test deterministic_stopwatch_start_stop_resume_split()
{
using namespace std::chrono_literals;
ManualClock clock;
auto sw = Stopwatch::started(clock);
Void::Testing::expect(clock.advance(10ms).has_value());
Void::Testing::expect_eq(sw.elapsed(), 10ms);
const auto lap1 = sw.try_lap();
Void::Testing::expect(lap1.has_value() && *lap1 == 10ms);
const auto stopped = sw.stop();
Void::Testing::expect_eq(stopped, 10ms);
// Advancing while stopped must not add to elapsed time.
Void::Testing::expect(clock.advance(100ms).has_value());
Void::Testing::expect_eq(sw.elapsed(), 10ms);
sw.resume();
Void::Testing::expect(clock.advance(5ms).has_value());
Void::Testing::expect_eq(sw.elapsed(), 15ms);
Void::Testing::expect_eq(sw.lap_count(), std::size_t{1});
const auto split_total = sw.split();
Void::Testing::expect_eq(split_total, 15ms);
Void::Testing::expect_eq(sw.lap_count(), std::size_t{0});
Void::Testing::expect_eq(sw.elapsed(), Duration::zero());
Void::Testing::expect(clock.advance(7ms).has_value());
Void::Testing::expect_eq(sw.elapsed(), 7ms);
}
Void::Testing::Test stopwatch_enforces_max_laps_and_not_running()
{
using namespace std::chrono_literals;
ManualClock clock;
Stopwatch sw{clock, 3};
const auto not_running = sw.try_lap();
Void::Testing::expect(!not_running.has_value());
Void::Testing::expect(not_running.error() == StopwatchError::not_running);
sw.start();
for (int i = 0; i < 3; ++i) {
Void::Testing::expect(clock.advance(1ms).has_value());
Void::Testing::expect(sw.try_lap().has_value());
}
Void::Testing::expect(clock.advance(1ms).has_value());
const auto full = sw.try_lap();
Void::Testing::expect(!full.has_value());
Void::Testing::expect(full.error() == StopwatchError::lap_limit_reached);
Void::Testing::expect_eq(sw.lap_count(), std::size_t{3});
}