Universal additions (all generic, header-only, tested; full suite 1127 pass): - Void::Encoding::Url — RFC 3986 percent encode/decode + query_encode. - Void::Text::html_unescape — numeric + common named HTML entities -> UTF-8. - Void::Time::parse_iso8601_interval — parse PT#H#M#S / P#DT#H#M#S durations (distinct from the unit-suffix parse_duration). - Void::FileSystem::TempFile — RAII unique temp file (alongside TempDir), with keep() to persist. - Void::Random::random_string(n, charset) overload. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
165 lines
5.4 KiB
C++
165 lines
5.4 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 ─────────────────────────────────────────────────────────────
|
|
|
|
// ── ISO 8601 interval parsing ─────────────────────────────────────────────
|
|
|
|
Void::Testing::Test iso8601_interval_hours_minutes()
|
|
{
|
|
const auto r = Void::Time::parse_iso8601_interval("PT1H30M");
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(r->count(), static_cast<long long>(5400));
|
|
}
|
|
|
|
Void::Testing::Test iso8601_interval_days_and_time()
|
|
{
|
|
const auto r = Void::Time::parse_iso8601_interval("P1DT2H");
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(r->count(), static_cast<long long>(93600));
|
|
}
|
|
|
|
Void::Testing::Test iso8601_interval_seconds_only()
|
|
{
|
|
const auto r = Void::Time::parse_iso8601_interval("PT45S");
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(r->count(), static_cast<long long>(45));
|
|
}
|
|
|
|
Void::Testing::Test iso8601_interval_zero_and_negative()
|
|
{
|
|
const auto z = Void::Time::parse_iso8601_interval("PT0S");
|
|
Void::Testing::expect(z.has_value());
|
|
Void::Testing::expect_eq(z->count(), static_cast<long long>(0));
|
|
const auto n = Void::Time::parse_iso8601_interval("-PT1M");
|
|
Void::Testing::expect(n.has_value());
|
|
Void::Testing::expect_eq(n->count(), static_cast<long long>(-60));
|
|
}
|
|
|
|
Void::Testing::Test iso8601_interval_malformed()
|
|
{
|
|
Void::Testing::expect(!Void::Time::parse_iso8601_interval("").has_value());
|
|
Void::Testing::expect(
|
|
!Void::Time::parse_iso8601_interval("1H30M").has_value());
|
|
Void::Testing::expect(!Void::Time::parse_iso8601_interval("PT").has_value());
|
|
Void::Testing::expect(
|
|
!Void::Time::parse_iso8601_interval("PT5X").has_value());
|
|
}
|
|
|
|
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');
|
|
}
|