195 lines
5.7 KiB
C++
195 lines
5.7 KiB
C++
#include <Void/Time/Stopwatch.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <thread>
|
|
|
|
using namespace Void::Time;
|
|
using Duration = Void::Time::Stopwatch::Duration;
|
|
|
|
// ── Stopwatch ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test stopwatch_not_running_initially()
|
|
{
|
|
Stopwatch sw;
|
|
Void::Testing::expect(!sw.is_running());
|
|
Void::Testing::expect_eq(sw.elapsed(), Duration::zero());
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_start_sets_running()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
Void::Testing::expect(sw.is_running());
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_elapsed_increases_while_running()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
const auto first = sw.elapsed();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
const auto second = sw.elapsed();
|
|
Void::Testing::expect(second > first);
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_stop_returns_elapsed()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
const Duration d = sw.stop();
|
|
Void::Testing::expect(!sw.is_running());
|
|
Void::Testing::expect(d.count() > 0);
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_elapsed_ms()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
sw.stop();
|
|
Void::Testing::expect(sw.elapsed_ms() >= 4.0);
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_lap_count()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
sw.lap();
|
|
sw.lap();
|
|
sw.lap();
|
|
Void::Testing::expect_eq(sw.lap_count(), std::size_t{3});
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_lap_durations_sum_to_elapsed()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
sw.lap();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{2});
|
|
sw.lap();
|
|
const Duration total = sw.stop();
|
|
|
|
Duration lap_sum{};
|
|
for (const auto& l : sw.laps()) lap_sum += l;
|
|
// Lap sum should be close to total (within 1ms tolerance)
|
|
const auto diff = total - lap_sum;
|
|
Void::Testing::expect(diff.count() < 1'000'000); // < 1 ms
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_reset_clears_state()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
sw.lap();
|
|
sw.stop();
|
|
sw.reset();
|
|
Void::Testing::expect(!sw.is_running());
|
|
Void::Testing::expect_eq(sw.elapsed(), Duration::zero());
|
|
Void::Testing::expect_eq(sw.lap_count(), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_restart_clears_laps()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
sw.lap();
|
|
sw.stop();
|
|
sw.start(); // restart
|
|
Void::Testing::expect_eq(sw.lap_count(), std::size_t{0});
|
|
}
|
|
|
|
Void::Testing::Test stopwatch_stopped_elapsed_does_not_grow()
|
|
{
|
|
Stopwatch sw;
|
|
sw.start();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
sw.stop();
|
|
const Duration d1 = sw.elapsed();
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
const Duration d2 = sw.elapsed();
|
|
Void::Testing::expect_eq(d1, d2);
|
|
}
|
|
|
|
// ── ScopedTimer ───────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test scoped_timer_records_elapsed()
|
|
{
|
|
Duration d{};
|
|
{
|
|
ScopedTimer t{d};
|
|
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
|
}
|
|
Void::Testing::expect(d.count() > 0);
|
|
}
|
|
|
|
Void::Testing::Test scoped_timer_written_on_destruction()
|
|
{
|
|
Duration d{};
|
|
d = Duration::zero();
|
|
{
|
|
ScopedTimer t{d};
|
|
// do nothing
|
|
}
|
|
Void::Testing::expect(d != Duration::zero() || d == Duration::zero()); // always true: just verifies it compiles
|
|
(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});
|
|
}
|