Completes the in-progress merges (Optional->Either/Opt, Curry+Memo->Pipeline,
ScopeGuard->Scope, String->Str):
- Pipeline/Memo.hpp now carries the full Memo API (memoize with recursion
support, TupleHash, SimpleCache) alongside memoize_fn; old Memo module
and its test dir removed.
- Duplicate Channel module removed; Concurrency::Channel gains empty() and
the unique test cases (drain-after-close, multi-producer, string payloads).
- StopWatch module merged into Time::Stopwatch: one class with explicit
start/stop/laps plus a started() factory for the auto-start RAII style;
ScopedTimer moved along.
- Testing optional dep retargeted String->Str (pad_right lives in Str now).
- Stale empty dirs removed: src/{ScopeGuard,String,Core}, include/Void/{Core,
Filesystem,Memo,Channel,StopWatch}.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
140 lines
3.8 KiB
C++
140 lines
3.8 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
|
|
}
|