Files
Library/tests/standalone/Time/Time.cpp
T
faumarayandClaude Fable 5 da2e93c405 Finish module consolidation: fold Memo into Pipeline, dedupe Channel/Stopwatch
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>
2026-06-12 18:41:24 +03:00

122 lines
3.8 KiB
C++

#include <Void/Time/Format.hpp>
#include <Void/Time/Stopwatch.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <chrono>
#include <string>
#include <thread>
// ── Stopwatch ─────────────────────────────────────────────────────────────
Void::Testing::Test stopwatch_elapsed_is_non_negative()
{
auto sw = Void::Time::Stopwatch::started();
Void::Testing::expect(sw.elapsed().count() >= 0);
}
Void::Testing::Test stopwatch_elapsed_as_milliseconds()
{
auto sw = Void::Time::Stopwatch::started();
std::this_thread::sleep_for(std::chrono::milliseconds{5});
const auto ms = sw.elapsed_as<std::chrono::milliseconds>().count();
Void::Testing::expect(ms >= 4);
Void::Testing::expect(ms < 500);
}
Void::Testing::Test stopwatch_restart_starts_from_zero()
{
auto sw = Void::Time::Stopwatch::started();
std::this_thread::sleep_for(std::chrono::milliseconds{5});
sw.start();
const auto ns = sw.elapsed().count();
Void::Testing::expect(ns < 5'000'000); // < 5ms since restart
}
Void::Testing::Test stopwatch_split_returns_elapsed_and_restarts()
{
auto sw = Void::Time::Stopwatch::started();
std::this_thread::sleep_for(std::chrono::milliseconds{5});
const auto d = sw.split();
const auto after = sw.elapsed().count();
Void::Testing::expect(d.count() >= 0);
Void::Testing::expect(after < d.count()); // after restart < before
}
// ── format_duration ───────────────────────────────────────────────────────
Void::Testing::Test format_duration_nanoseconds()
{
using namespace std::chrono_literals;
Void::Testing::expect_eq(Void::Time::format_duration(500ns),
std::string{"500ns"});
}
Void::Testing::Test format_duration_microseconds()
{
using namespace std::chrono_literals;
Void::Testing::expect(
Void::Time::format_duration(4500ns).find("\xc2\xb5s") !=
std::string::npos);
}
Void::Testing::Test format_duration_milliseconds()
{
using namespace std::chrono_literals;
Void::Testing::expect(
Void::Time::format_duration(2'300'000ns).find("ms") !=
std::string::npos);
}
Void::Testing::Test format_duration_seconds()
{
using namespace std::chrono_literals;
Void::Testing::expect(
Void::Time::format_duration(1'500'000'000ns).find('s') !=
std::string::npos);
}
Void::Testing::Test format_duration_minutes()
{
Void::Testing::expect(
Void::Time::format_duration(std::chrono::seconds{90}).find('m') !=
std::string::npos);
}
Void::Testing::Test format_duration_hours()
{
Void::Testing::expect(
Void::Time::format_duration(std::chrono::seconds{3700}).find('h') !=
std::string::npos);
}
Void::Testing::Test format_duration_works_with_chrono_milliseconds()
{
using namespace std::chrono_literals;
const auto s = Void::Time::format_duration(15ms);
Void::Testing::expect(s.find("ms") != std::string::npos);
}
// ── utc timestamps ────────────────────────────────────────────────────────
Void::Testing::Test utc_now_iso8601_has_correct_format()
{
const auto s = Void::Time::utc_now_iso8601();
// "2026-05-14T10:30:00Z"
Void::Testing::expect_eq(s.size(), std::size_t{20});
Void::Testing::expect_eq(s[4], '-');
Void::Testing::expect_eq(s[7], '-');
Void::Testing::expect_eq(s[10], 'T');
Void::Testing::expect_eq(s.back(), 'Z');
}
Void::Testing::Test utc_now_iso8601_ms_has_correct_format()
{
const auto s = Void::Time::utc_now_iso8601_ms();
// "2026-05-14T10:30:00.123Z"
Void::Testing::expect_eq(s.size(), std::size_t{24});
Void::Testing::expect_eq(s[10], 'T');
Void::Testing::expect_eq(s[19], '.');
Void::Testing::expect_eq(s.back(), 'Z');
}