Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules: - Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals - Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string - Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces - Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode - Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks - Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation) Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added Detail/Color.hpp shim that delegates to Stacktrace::Color when present or provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler, ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE. Fix Repr.hpp reflect_struct to handle template specializations that lack std::meta::identifier_of (now guarded by has_identifier check). 33/33 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
42268c6c35
commit
a09e000f0f
65 files changed
+4959
-138
No files matched your search
@@ -1,10 +1,12 @@
|
||||
// Cross-module integration tests for the kitchen-sink VoidInternalTests
|
||||
// binary. Exercises Logging + Result + Stacktrace + FileSystem working
|
||||
// binary. Exercises Logging + Result + Stacktrace + FileSystem + Log working
|
||||
// together — the kind of thing the standalone per-module tests can't
|
||||
// reach because they only link one module.
|
||||
|
||||
#include <Void/FileSystem/Path.hpp>
|
||||
#include <Void/FileSystem/TempDir.hpp>
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Log/LoggingAdapter.hpp>
|
||||
#include <Void/Logging/ConsoleLogger.hpp>
|
||||
#include <Void/Logging/FileLogger.hpp>
|
||||
#include <Void/Logging/Level.hpp>
|
||||
@@ -113,3 +115,57 @@ Void::Testing::Test error_chain_logged_to_file_roundtrip()
|
||||
Void::Testing::expect(content.find("socket closed") != std::string::npos);
|
||||
Void::Testing::expect(content.find("caused by:") != std::string::npos);
|
||||
}
|
||||
|
||||
// Void::Log + Void::Logging: make_sink() bridges the two modules so that
|
||||
// internal library events routed through Void::Log::log() surface in a
|
||||
// Void::Logging::ConsoleLogger captured via a stringstream.
|
||||
Void::Testing::Test log_adapter_routes_to_void_logging()
|
||||
{
|
||||
std::ostringstream out;
|
||||
Void::Logging::ConsoleLogger logger{out};
|
||||
logger.set_level(Void::Logging::Level::Trace);
|
||||
|
||||
Void::Log::set_sink(Void::Log::make_sink(logger));
|
||||
|
||||
Void::Log::log(
|
||||
Void::Log::Level::Info, "TestModule", "hello from Void::Log");
|
||||
Void::Log::log(Void::Log::Level::Warning, "TestModule", "a warning");
|
||||
|
||||
Void::Log::set_sink({});
|
||||
|
||||
const auto text = out.str();
|
||||
Void::Testing::expect(text.find("hello from Void::Log") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(text.find("a warning") != std::string::npos);
|
||||
Void::Testing::expect(text.find("[TestModule]") != std::string::npos);
|
||||
}
|
||||
|
||||
// FileSystem operations now emit Void::Log events; verify they reach a sink.
|
||||
Void::Testing::Test filesystem_ops_emit_log_events()
|
||||
{
|
||||
int debug_count = 0;
|
||||
int warning_count = 0;
|
||||
Void::Log::set_sink(
|
||||
[&](Void::Log::Level l, std::string_view, std::string_view) {
|
||||
if (l == Void::Log::Level::Debug) {
|
||||
++debug_count;
|
||||
}
|
||||
if (l == Void::Log::Level::Warning) {
|
||||
++warning_count;
|
||||
}
|
||||
});
|
||||
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto p = tmp / "test.txt";
|
||||
(void)Void::FileSystem::write_file(p, "data");
|
||||
(void)Void::FileSystem::read_file(p);
|
||||
(void)Void::FileSystem::read_file(tmp / "nonexistent.txt");
|
||||
|
||||
Void::Log::set_sink({});
|
||||
|
||||
// TempDir creation + read success + write (no debug from write_file) = at
|
||||
// least 2
|
||||
Void::Testing::expect(debug_count >= 2);
|
||||
// Missing file read = 1 warning
|
||||
Void::Testing::expect(warning_count >= 1);
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
#include <Void/Bits/Bits.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
// ── popcount ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test popcount_zero()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::popcount(0u), 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test popcount_all_ones_u8()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::popcount(std::uint8_t{0xFF}), 8);
|
||||
}
|
||||
|
||||
Void::Testing::Test popcount_u32()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::popcount(std::uint32_t{0x0F0F0F0F}), 16);
|
||||
}
|
||||
|
||||
// ── test/set/clear/toggle ────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test test_bit_set()
|
||||
{
|
||||
Void::Testing::expect(Void::Bits::test_bit(std::uint8_t{0b00000100u}, 2));
|
||||
}
|
||||
|
||||
Void::Testing::Test test_bit_clear()
|
||||
{
|
||||
Void::Testing::expect(!Void::Bits::test_bit(std::uint8_t{0b00000100u}, 0));
|
||||
}
|
||||
|
||||
Void::Testing::Test set_bit_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::set_bit(std::uint8_t{0u}, 3),
|
||||
std::uint8_t{0b00001000u});
|
||||
}
|
||||
|
||||
Void::Testing::Test clear_bit_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::clear_bit(std::uint8_t{0xFF}, 3),
|
||||
std::uint8_t{0b11110111u});
|
||||
}
|
||||
|
||||
Void::Testing::Test toggle_bit_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::toggle_bit(std::uint8_t{0u}, 5),
|
||||
std::uint8_t{0b00100000u});
|
||||
}
|
||||
|
||||
// ── extract / deposit ─────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test extract_bits_works()
|
||||
{
|
||||
// bits 4-7 of 0b10110100 = 0b1011
|
||||
Void::Testing::expect_eq(
|
||||
Void::Bits::extract_bits(std::uint8_t{0b10110100u}, 4, 4),
|
||||
std::uint8_t{0b1011u});
|
||||
}
|
||||
|
||||
Void::Testing::Test deposit_bits_works()
|
||||
{
|
||||
// deposit 0b1010 into bits 4-7 of 0x00
|
||||
Void::Testing::expect_eq(
|
||||
Void::Bits::deposit_bits(std::uint8_t{0u}, std::uint8_t{0b1010u}, 4, 4),
|
||||
std::uint8_t{0b10100000u});
|
||||
}
|
||||
|
||||
// ── rotation ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test rotl_8()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::rotl(std::uint8_t{0b10000001u}, 1),
|
||||
std::uint8_t{0b00000011u});
|
||||
}
|
||||
|
||||
Void::Testing::Test rotr_8()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::rotr(std::uint8_t{0b10000001u}, 1),
|
||||
std::uint8_t{0b11000000u});
|
||||
}
|
||||
|
||||
// ── pow2 helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test has_single_bit()
|
||||
{
|
||||
Void::Testing::expect(Void::Bits::has_single_bit(64u));
|
||||
Void::Testing::expect(!Void::Bits::has_single_bit(63u));
|
||||
}
|
||||
|
||||
Void::Testing::Test ceil_pow2_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::ceil_pow2(5u), 8u);
|
||||
Void::Testing::expect_eq(Void::Bits::ceil_pow2(8u), 8u);
|
||||
}
|
||||
|
||||
Void::Testing::Test floor_pow2_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::floor_pow2(5u), 4u);
|
||||
}
|
||||
|
||||
// ── align ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test align_up_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::align_up(13u, 8u), 16u);
|
||||
Void::Testing::expect_eq(Void::Bits::align_up(8u, 8u), 8u);
|
||||
}
|
||||
|
||||
Void::Testing::Test align_down_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::align_down(13u, 8u), 8u);
|
||||
}
|
||||
|
||||
// ── masks ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test low_mask_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::low_mask<std::uint8_t>(4),
|
||||
std::uint8_t{0x0F});
|
||||
}
|
||||
|
||||
Void::Testing::Test range_mask_works()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::range_mask<std::uint8_t>(2, 5),
|
||||
std::uint8_t{0b00111100u});
|
||||
}
|
||||
|
||||
// ── byteswap / endian ────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test byteswap_u32()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Bits::byteswap(std::uint32_t{0x12345678u}),
|
||||
std::uint32_t{0x78563412u});
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
#include <Void/Concurrency/Channel.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
using Void::Concurrency::Channel;
|
||||
|
||||
Void::Testing::Test capacity_is_preserved()
|
||||
{
|
||||
Channel<int> ch{8};
|
||||
Void::Testing::expect_eq(ch.capacity(), std::size_t{8});
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_and_try_receive_roundtrip()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
Void::Testing::expect(ch.try_send(10));
|
||||
Void::Testing::expect(ch.try_send(20));
|
||||
Void::Testing::expect_eq(ch.size(), std::size_t{2});
|
||||
auto v1 = ch.try_receive();
|
||||
Void::Testing::expect(v1.has_value());
|
||||
Void::Testing::expect_eq(*v1, 10);
|
||||
auto v2 = ch.try_receive();
|
||||
Void::Testing::expect(v2.has_value());
|
||||
Void::Testing::expect_eq(*v2, 20);
|
||||
auto empty = ch.try_receive();
|
||||
Void::Testing::expect(!empty.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_fails_when_full()
|
||||
{
|
||||
Channel<int> ch{2};
|
||||
Void::Testing::expect(ch.try_send(1));
|
||||
Void::Testing::expect(ch.try_send(2));
|
||||
Void::Testing::expect(!ch.try_send(3));
|
||||
}
|
||||
|
||||
Void::Testing::Test close_unblocks_receiver()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
std::thread producer{[&] { ch.close(); }};
|
||||
auto val = ch.receive();
|
||||
producer.join();
|
||||
Void::Testing::expect(!val.has_value());
|
||||
Void::Testing::expect(ch.is_closed());
|
||||
}
|
||||
|
||||
Void::Testing::Test producer_consumer_ordering()
|
||||
{
|
||||
Channel<int> ch{16};
|
||||
constexpr int N = 10;
|
||||
std::thread producer{[&] {
|
||||
for (int i = 0; i < N; ++i) {
|
||||
ch.send(i);
|
||||
}
|
||||
ch.close();
|
||||
}};
|
||||
std::vector<int> received;
|
||||
while (auto v = ch.receive()) {
|
||||
received.push_back(*v);
|
||||
}
|
||||
producer.join();
|
||||
Void::Testing::expect_eq(static_cast<int>(received.size()), N);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
Void::Testing::expect_eq(received[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test try_send_fails_after_close()
|
||||
{
|
||||
Channel<int> ch{4};
|
||||
ch.close();
|
||||
Void::Testing::expect(!ch.try_send(1));
|
||||
}
|
||||
|
||||
Void::Testing::Test send_returns_false_when_closed()
|
||||
{
|
||||
Channel<int> ch{1};
|
||||
ch.close();
|
||||
bool ok = ch.send(42);
|
||||
Void::Testing::expect(!ok);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
#include <Void/Concurrency/ParallelFor.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <vector>
|
||||
|
||||
using namespace Void::Concurrency;
|
||||
|
||||
Void::Testing::Test parallel_sum_matches_sequential()
|
||||
{
|
||||
constexpr int N = 1000;
|
||||
std::atomic<long long> total{0};
|
||||
parallel_for(0, N, [&](int i) { total += i; });
|
||||
Void::Testing::expect_eq(total.load(),
|
||||
static_cast<long long>(N) * (N - 1) / 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test empty_range_does_nothing()
|
||||
{
|
||||
std::atomic<int> calls{0};
|
||||
parallel_for(5, 5, [&](int) { ++calls; });
|
||||
Void::Testing::expect_eq(calls.load(), 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test with_explicit_pool()
|
||||
{
|
||||
ThreadPool pool{2};
|
||||
std::atomic<int> count{0};
|
||||
parallel_for(0, 10, [&](int) { ++count; }, pool);
|
||||
Void::Testing::expect_eq(count.load(), 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test results_written_to_indexed_array()
|
||||
{
|
||||
constexpr int N = 64;
|
||||
std::vector<int> out(N, 0);
|
||||
parallel_for(0, N, [&](int i) { out[i] = i * i; });
|
||||
for (int i = 0; i < N; ++i) {
|
||||
Void::Testing::expect_eq(out[i], i * i);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test exception_propagates()
|
||||
{
|
||||
bool caught = false;
|
||||
try {
|
||||
parallel_for(0, 4, [](int i) {
|
||||
if (i == 2) {
|
||||
throw std::runtime_error("bad index");
|
||||
}
|
||||
});
|
||||
} catch (const std::runtime_error&) {
|
||||
caught = true;
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <Void/Concurrency/ThreadPool.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
|
||||
using Void::Concurrency::ThreadPool;
|
||||
|
||||
Void::Testing::Test thread_count_matches_requested()
|
||||
{
|
||||
ThreadPool pool{4};
|
||||
Void::Testing::expect_eq(pool.thread_count(), std::size_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test defaults_to_hardware_concurrency()
|
||||
{
|
||||
ThreadPool pool;
|
||||
Void::Testing::expect(pool.thread_count() >= 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test submit_returns_correct_value()
|
||||
{
|
||||
ThreadPool pool{2};
|
||||
auto fut = pool.submit([] { return 42; });
|
||||
Void::Testing::expect_eq(fut.get(), 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test tasks_run_concurrently()
|
||||
{
|
||||
constexpr int N = 8;
|
||||
ThreadPool pool{N};
|
||||
std::atomic<int> counter{0};
|
||||
std::vector<std::future<void>> futs;
|
||||
futs.reserve(N);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
futs.push_back(pool.submit([&] {
|
||||
++counter;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
}));
|
||||
}
|
||||
for (auto& f : futs) {
|
||||
f.get();
|
||||
}
|
||||
Void::Testing::expect_eq(counter.load(), N);
|
||||
}
|
||||
|
||||
Void::Testing::Test parallel_sum_is_correct()
|
||||
{
|
||||
ThreadPool pool{4};
|
||||
constexpr int N = 100;
|
||||
std::atomic<long long> total{0};
|
||||
std::vector<std::future<void>> futs;
|
||||
futs.reserve(N);
|
||||
for (int i = 1; i <= N; ++i) {
|
||||
futs.push_back(pool.submit([&, i] { total += i; }));
|
||||
}
|
||||
for (auto& f : futs) {
|
||||
f.get();
|
||||
}
|
||||
Void::Testing::expect_eq(total.load(), 5050LL);
|
||||
}
|
||||
|
||||
Void::Testing::Test exception_propagates_through_future()
|
||||
{
|
||||
ThreadPool pool{1};
|
||||
auto fut = pool.submit([] -> int { throw std::runtime_error("boom"); });
|
||||
bool caught = false;
|
||||
try {
|
||||
fut.get();
|
||||
} catch (const std::runtime_error& e) {
|
||||
caught = (std::string{e.what()} == "boom");
|
||||
}
|
||||
Void::Testing::expect(caught);
|
||||
}
|
||||
|
||||
Void::Testing::Test submit_after_destruction_throws()
|
||||
{
|
||||
std::optional<std::future<int>> fut;
|
||||
{
|
||||
ThreadPool pool{1};
|
||||
fut = pool.submit([] { return 1; });
|
||||
fut->get();
|
||||
}
|
||||
// pool is now destroyed; this just verifies destruction was clean
|
||||
Void::Testing::expect(true);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#include <Void/Configuration/Config.hpp>
|
||||
#include <Void/Configuration/Env.hpp>
|
||||
#include <Void/FileSystem/TempDir.hpp>
|
||||
#include <Void/FileSystem/Path.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <string>
|
||||
|
||||
Void::Testing::Test get_env_finds_set_variable()
|
||||
{
|
||||
::setenv("VOID_TEST_VAR_A", "hello", 1);
|
||||
auto r = Void::Configuration::get_env("VOID_TEST_VAR_A");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::string{"hello"});
|
||||
::unsetenv("VOID_TEST_VAR_A");
|
||||
}
|
||||
|
||||
Void::Testing::Test get_env_returns_error_for_missing()
|
||||
{
|
||||
::unsetenv("VOID_TEST_VAR_MISSING_XYZ");
|
||||
auto r = Void::Configuration::get_env("VOID_TEST_VAR_MISSING_XYZ");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test get_env_or_returns_fallback_when_absent()
|
||||
{
|
||||
::unsetenv("VOID_TEST_VAR_MISSING_XYZ");
|
||||
auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_MISSING_XYZ", "default");
|
||||
Void::Testing::expect_eq(val, std::string{"default"});
|
||||
}
|
||||
|
||||
Void::Testing::Test get_env_or_returns_value_when_present()
|
||||
{
|
||||
::setenv("VOID_TEST_VAR_B", "world", 1);
|
||||
auto val = Void::Configuration::get_env_or("VOID_TEST_VAR_B", "fallback");
|
||||
Void::Testing::expect_eq(val, std::string{"world"});
|
||||
::unsetenv("VOID_TEST_VAR_B");
|
||||
}
|
||||
|
||||
Void::Testing::Test config_set_and_get()
|
||||
{
|
||||
Void::Configuration::Config cfg;
|
||||
cfg.set("key", "value");
|
||||
auto r = cfg.get("key");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(std::string{*r}, std::string{"value"});
|
||||
}
|
||||
|
||||
Void::Testing::Test config_get_missing_key_fails()
|
||||
{
|
||||
Void::Configuration::Config cfg;
|
||||
auto r = cfg.get("nonexistent");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test config_get_or_returns_fallback()
|
||||
{
|
||||
Void::Configuration::Config cfg;
|
||||
auto val = cfg.get_or("missing", "fb");
|
||||
Void::Testing::expect_eq(std::string{val}, std::string{"fb"});
|
||||
}
|
||||
|
||||
Void::Testing::Test config_from_env_reads_listed_keys()
|
||||
{
|
||||
::setenv("VOID_TEST_CFG_X", "42", 1);
|
||||
::unsetenv("VOID_TEST_CFG_Y");
|
||||
|
||||
auto cfg = Void::Configuration::Config::from_env({"VOID_TEST_CFG_X", "VOID_TEST_CFG_Y"});
|
||||
Void::Testing::expect_eq(cfg.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(std::string{cfg.get_or("VOID_TEST_CFG_X")}, std::string{"42"});
|
||||
::unsetenv("VOID_TEST_CFG_X");
|
||||
}
|
||||
|
||||
Void::Testing::Test config_from_file_parses_key_value()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto path = tmp / "cfg.env";
|
||||
(void)Void::FileSystem::write_file(path, "HOST=localhost\nPORT=5432\n");
|
||||
|
||||
auto r = Void::Configuration::Config::from_file(path);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(std::string{r->get_or("HOST")}, std::string{"localhost"});
|
||||
Void::Testing::expect_eq(std::string{r->get_or("PORT")}, std::string{"5432"});
|
||||
}
|
||||
|
||||
Void::Testing::Test config_from_file_skips_comments_and_blank_lines()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto path = tmp / "cfg.env";
|
||||
(void)Void::FileSystem::write_file(path, "# this is a comment\n\nFOO=bar\n");
|
||||
|
||||
auto r = Void::Configuration::Config::from_file(path);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(std::string{r->get_or("FOO")}, std::string{"bar"});
|
||||
}
|
||||
|
||||
Void::Testing::Test config_overlay_later_wins_on_conflict()
|
||||
{
|
||||
Void::Configuration::Config base;
|
||||
base.set("A", "from_base");
|
||||
base.set("B", "only_base");
|
||||
|
||||
Void::Configuration::Config top;
|
||||
top.set("A", "from_top");
|
||||
top.set("C", "only_top");
|
||||
|
||||
base.overlay(top);
|
||||
|
||||
Void::Testing::expect_eq(std::string{base.get_or("A")}, std::string{"from_top"});
|
||||
Void::Testing::expect_eq(std::string{base.get_or("B")}, std::string{"only_base"});
|
||||
Void::Testing::expect_eq(std::string{base.get_or("C")}, std::string{"only_top"});
|
||||
}
|
||||
|
||||
Void::Testing::Test config_from_file_missing_path_fails()
|
||||
{
|
||||
auto r = Void::Configuration::Config::from_file("/no/such/file.env");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
#include <Void/Encoding/Base64.hpp>
|
||||
#include <Void/Encoding/Hex.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
// ── Base64 encode ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test base64_encode_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::Encoding::Base64::encode("").empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_encode_man()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Man"),
|
||||
std::string{"TWFu"});
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_encode_ma()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Ma"),
|
||||
std::string{"TWE="});
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_encode_m()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Base64::encode("M"),
|
||||
std::string{"TQ=="});
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_encode_hello_world()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Base64::encode("Hello, World!"),
|
||||
std::string{"SGVsbG8sIFdvcmxkIQ=="});
|
||||
}
|
||||
|
||||
// ── Base64 decode ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test base64_decode_empty()
|
||||
{
|
||||
const auto r = Void::Encoding::Base64::decode("");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_decode_man()
|
||||
{
|
||||
const auto r = Void::Encoding::Base64::decode("TWFu");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::string{"Man"});
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_decode_with_padding()
|
||||
{
|
||||
const auto r = Void::Encoding::Base64::decode("SGVsbG8sIFdvcmxkIQ==");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::string{"Hello, World!"});
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_decode_invalid_returns_nullopt()
|
||||
{
|
||||
const auto r = Void::Encoding::Base64::decode("!!");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test base64_round_trip()
|
||||
{
|
||||
const std::string original = "The quick brown fox jumps over the lazy dog";
|
||||
const auto encoded = Void::Encoding::Base64::encode(original);
|
||||
const auto decoded = Void::Encoding::Base64::decode(encoded);
|
||||
Void::Testing::expect(decoded.has_value());
|
||||
Void::Testing::expect_eq(*decoded, original);
|
||||
}
|
||||
|
||||
// ── Base64 URL ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test base64url_no_padding()
|
||||
{
|
||||
const auto s = Void::Encoding::Base64::encode_url("Man");
|
||||
Void::Testing::expect_eq(s, std::string{"TWFu"});
|
||||
Void::Testing::expect(s.find('=') == std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test base64url_no_plus_or_slash()
|
||||
{
|
||||
// encode something that would produce + or / in standard base64
|
||||
const std::string tricky = "\xfb\xff\xfe";
|
||||
const auto s = Void::Encoding::Base64::encode_url(tricky);
|
||||
Void::Testing::expect(s.find('+') == std::string::npos);
|
||||
Void::Testing::expect(s.find('/') == std::string::npos);
|
||||
}
|
||||
|
||||
// ── Hex encode ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test hex_encode_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::Encoding::Hex::encode("").empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_encode_hello()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Hex::encode("Hi"),
|
||||
std::string{"4869"});
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_encode_binary()
|
||||
{
|
||||
const std::string bin{'\x00', '\xff', '\xab'};
|
||||
Void::Testing::expect_eq(Void::Encoding::Hex::encode(bin),
|
||||
std::string{"00ffab"});
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_encode_upper()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Encoding::Hex::encode_upper("Hi"),
|
||||
std::string{"4869"});
|
||||
}
|
||||
|
||||
// ── Hex decode ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test hex_decode_empty()
|
||||
{
|
||||
const auto r = Void::Encoding::Hex::decode("");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_decode_hello()
|
||||
{
|
||||
const auto r = Void::Encoding::Hex::decode("4869");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::string{"Hi"});
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_decode_uppercase()
|
||||
{
|
||||
const auto r = Void::Encoding::Hex::decode("4869");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::string{"Hi"});
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_decode_odd_length_fails()
|
||||
{
|
||||
Void::Testing::expect(!Void::Encoding::Hex::decode("abc").has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_decode_invalid_char_fails()
|
||||
{
|
||||
Void::Testing::expect(!Void::Encoding::Hex::decode("zz").has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test hex_round_trip()
|
||||
{
|
||||
const std::string original = "binary\x00\x01\x02\xff";
|
||||
const auto encoded = Void::Encoding::Hex::encode(original);
|
||||
const auto decoded = Void::Encoding::Hex::decode(encoded);
|
||||
Void::Testing::expect(decoded.has_value());
|
||||
Void::Testing::expect_eq(*decoded, original);
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <Void/FileSystem/Path.hpp>
|
||||
#include <Void/FileSystem/Query.hpp>
|
||||
#include <Void/FileSystem/TempDir.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <filesystem>
|
||||
|
||||
Void::Testing::Test exists_returns_true_for_existing_file()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto p = tmp / "f.txt";
|
||||
(void)Void::FileSystem::write_file(p, "x");
|
||||
Void::Testing::expect(Void::FileSystem::exists(p));
|
||||
}
|
||||
|
||||
Void::Testing::Test exists_returns_false_for_missing()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
Void::Testing::expect(!Void::FileSystem::exists(tmp / "no_such.txt"));
|
||||
}
|
||||
|
||||
Void::Testing::Test file_size_returns_correct_byte_count()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto p = tmp / "sized.txt";
|
||||
(void)Void::FileSystem::write_file(p, "hello");
|
||||
auto r = Void::FileSystem::file_size(p);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, std::uintmax_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test file_size_fails_for_missing_path()
|
||||
{
|
||||
auto r = Void::FileSystem::file_size("/no/such/file.txt");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test last_modified_returns_a_time()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto p = tmp / "ts.txt";
|
||||
(void)Void::FileSystem::write_file(p, "data");
|
||||
auto r = Void::FileSystem::last_modified(p);
|
||||
Void::Testing::expect(r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test last_modified_fails_for_missing_path()
|
||||
{
|
||||
auto r = Void::FileSystem::last_modified("/no/such/file.txt");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test list_directory_returns_created_entries()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
(void)Void::FileSystem::write_file(tmp / "a.txt", "");
|
||||
(void)Void::FileSystem::write_file(tmp / "b.txt", "");
|
||||
auto r = Void::FileSystem::list_directory(tmp.path());
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->size() >= 2);
|
||||
const auto has_a = std::any_of(r->begin(), r->end(), [&](const auto& p) {
|
||||
return p.filename() == "a.txt";
|
||||
});
|
||||
const auto has_b = std::any_of(r->begin(), r->end(), [&](const auto& p) {
|
||||
return p.filename() == "b.txt";
|
||||
});
|
||||
Void::Testing::expect(has_a);
|
||||
Void::Testing::expect(has_b);
|
||||
}
|
||||
|
||||
Void::Testing::Test list_directory_fails_for_missing_path()
|
||||
{
|
||||
auto r = Void::FileSystem::list_directory("/no/such/directory");
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test copy_file_creates_duplicate()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto src = tmp / "src.txt";
|
||||
auto dst = tmp / "dst.txt";
|
||||
(void)Void::FileSystem::write_file(src, "contents");
|
||||
auto r = Void::FileSystem::copy_file(src, dst);
|
||||
Void::Testing::expect(r.has_value());
|
||||
auto back = Void::FileSystem::read_file(dst);
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(*back, std::string{"contents"});
|
||||
}
|
||||
|
||||
Void::Testing::Test copy_file_fails_without_overwrite_when_dst_exists()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto src = tmp / "src.txt";
|
||||
auto dst = tmp / "dst.txt";
|
||||
(void)Void::FileSystem::write_file(src, "new");
|
||||
(void)Void::FileSystem::write_file(dst, "old");
|
||||
auto r = Void::FileSystem::copy_file(src, dst, /*overwrite=*/false);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test copy_file_with_overwrite_replaces_dst()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto src = tmp / "src.txt";
|
||||
auto dst = tmp / "dst.txt";
|
||||
(void)Void::FileSystem::write_file(src, "new_content");
|
||||
(void)Void::FileSystem::write_file(dst, "old_content");
|
||||
auto r = Void::FileSystem::copy_file(src, dst, /*overwrite=*/true);
|
||||
Void::Testing::expect(r.has_value());
|
||||
auto back = Void::FileSystem::read_file(dst);
|
||||
Void::Testing::expect_eq(*back, std::string{"new_content"});
|
||||
}
|
||||
|
||||
Void::Testing::Test copy_dir_duplicates_tree()
|
||||
{
|
||||
Void::FileSystem::TempDir tmp;
|
||||
auto src = tmp / "src";
|
||||
auto dst = tmp / "dst";
|
||||
(void)Void::FileSystem::ensure_directory(src);
|
||||
(void)Void::FileSystem::write_file(src / "file.txt", "tree");
|
||||
auto r = Void::FileSystem::copy_dir(src, dst);
|
||||
Void::Testing::expect(r.has_value());
|
||||
auto back = Void::FileSystem::read_file(dst / "file.txt");
|
||||
Void::Testing::expect(back.has_value());
|
||||
Void::Testing::expect_eq(*back, std::string{"tree"});
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#include <Void/Hash/Hash.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace Void::Hash::Literals;
|
||||
|
||||
// ── FNV-1a 32 ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test fnv1a32_empty_string()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::fnv1a32(""), 2166136261u);
|
||||
}
|
||||
|
||||
Void::Testing::Test fnv1a32_hello_world()
|
||||
{
|
||||
const auto a = Void::Hash::fnv1a32("hello");
|
||||
const auto b = Void::Hash::fnv1a32("world");
|
||||
Void::Testing::expect(a != b);
|
||||
Void::Testing::expect(a != 0u);
|
||||
}
|
||||
|
||||
Void::Testing::Test fnv1a32_deterministic()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::fnv1a32("test"),
|
||||
Void::Hash::fnv1a32("test"));
|
||||
}
|
||||
|
||||
Void::Testing::Test fnv1a32_different_strings_differ()
|
||||
{
|
||||
Void::Testing::expect(Void::Hash::fnv1a32("abc") !=
|
||||
Void::Hash::fnv1a32("abd"));
|
||||
}
|
||||
|
||||
// ── FNV-1a 64 ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test fnv1a64_empty_basis()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::fnv1a64(""),
|
||||
14695981039346656037ull);
|
||||
}
|
||||
|
||||
Void::Testing::Test fnv1a64_hello_is_stable()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::fnv1a64("hello"),
|
||||
Void::Hash::fnv1a64("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test fnv1a64_wider_than_32()
|
||||
{
|
||||
// 64-bit should differ from 32-bit value for same input
|
||||
Void::Testing::expect(Void::Hash::fnv1a64("test") !=
|
||||
static_cast<std::uint64_t>(Void::Hash::fnv1a32("test")));
|
||||
}
|
||||
|
||||
// ── CRC-32 ───────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test crc32_known_value()
|
||||
{
|
||||
// CRC-32 of "123456789" is a well-known check value: 0xCBF43926
|
||||
Void::Testing::expect_eq(Void::Hash::crc32("123456789"), std::uint32_t{0xCBF43926u});
|
||||
}
|
||||
|
||||
Void::Testing::Test crc32_empty_is_zero()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::crc32(""), std::uint32_t{0u});
|
||||
}
|
||||
|
||||
Void::Testing::Test crc32_deterministic()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::crc32("void"), Void::Hash::crc32("void"));
|
||||
}
|
||||
|
||||
// ── DJB2 ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test djb2_non_zero_for_nonempty()
|
||||
{
|
||||
Void::Testing::expect(Void::Hash::djb2("hello") != 0u);
|
||||
}
|
||||
|
||||
Void::Testing::Test djb2_differs_per_string()
|
||||
{
|
||||
Void::Testing::expect(Void::Hash::djb2("foo") != Void::Hash::djb2("bar"));
|
||||
}
|
||||
|
||||
// ── MurmurHash3 ──────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test murmur3_32_deterministic()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Hash::murmur3_32("hello", 42u),
|
||||
Void::Hash::murmur3_32("hello", 42u));
|
||||
}
|
||||
|
||||
Void::Testing::Test murmur3_32_seed_affects_output()
|
||||
{
|
||||
Void::Testing::expect(Void::Hash::murmur3_32("hello", 0u) !=
|
||||
Void::Hash::murmur3_32("hello", 1u));
|
||||
}
|
||||
|
||||
Void::Testing::Test murmur3_32_empty_with_zero_seed()
|
||||
{
|
||||
// MurmurHash3("", 0) = 0
|
||||
Void::Testing::expect_eq(Void::Hash::murmur3_32("", 0u), std::uint32_t{0u});
|
||||
}
|
||||
|
||||
// ── UDL literals ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test udl_fnv32_comptime()
|
||||
{
|
||||
constexpr auto h = "hello"_fnv32;
|
||||
static_assert(h != 0u);
|
||||
Void::Testing::expect_eq(h, Void::Hash::fnv1a32("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test udl_fnv64_comptime()
|
||||
{
|
||||
constexpr auto h = "hello"_fnv64;
|
||||
static_assert(h != 0u);
|
||||
Void::Testing::expect_eq(h, Void::Hash::fnv1a64("hello"));
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
#include <Void/Log/Log.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <atomic>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// Reset the global sink between tests via a scoped RAII guard.
|
||||
struct SinkGuard {
|
||||
~SinkGuard() { Void::Log::set_sink({}); }
|
||||
};
|
||||
|
||||
Void::Testing::Test no_sink_is_silent()
|
||||
{
|
||||
SinkGuard guard;
|
||||
bool called = false;
|
||||
// No sink installed — this must not crash or invoke anything.
|
||||
Void::Log::log(Void::Log::Level::Info, "test", "silent message");
|
||||
Void::Testing::expect(!called);
|
||||
}
|
||||
|
||||
Void::Testing::Test sink_receives_all_fields()
|
||||
{
|
||||
SinkGuard guard;
|
||||
Void::Log::Level got_level{};
|
||||
std::string got_module;
|
||||
std::string got_message;
|
||||
|
||||
Void::Log::set_sink(
|
||||
[&](Void::Log::Level l, std::string_view mod, std::string_view msg) {
|
||||
got_level = l;
|
||||
got_module = mod;
|
||||
got_message = msg;
|
||||
});
|
||||
|
||||
Void::Log::log(Void::Log::Level::Warning, "MyModule", "something happened");
|
||||
|
||||
Void::Testing::expect(got_level == Void::Log::Level::Warning);
|
||||
Void::Testing::expect(got_module == "MyModule");
|
||||
Void::Testing::expect(got_message == "something happened");
|
||||
}
|
||||
|
||||
Void::Testing::Test set_sink_replaces_previous()
|
||||
{
|
||||
SinkGuard guard;
|
||||
int first_calls = 0;
|
||||
int second_calls = 0;
|
||||
|
||||
Void::Log::set_sink([&](auto, auto, auto) { ++first_calls; });
|
||||
Void::Log::log(Void::Log::Level::Info, "m", "a");
|
||||
|
||||
Void::Log::set_sink([&](auto, auto, auto) { ++second_calls; });
|
||||
Void::Log::log(Void::Log::Level::Info, "m", "b");
|
||||
|
||||
Void::Testing::expect_eq(first_calls, 1);
|
||||
Void::Testing::expect_eq(second_calls, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test clear_sink_silences_output()
|
||||
{
|
||||
SinkGuard guard;
|
||||
int calls = 0;
|
||||
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
|
||||
Void::Log::log(Void::Log::Level::Info, "m", "before clear");
|
||||
Void::Log::set_sink({});
|
||||
Void::Log::log(Void::Log::Level::Info, "m", "after clear");
|
||||
Void::Testing::expect_eq(calls, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test concurrent_log_calls_are_safe()
|
||||
{
|
||||
SinkGuard guard;
|
||||
std::atomic<int> count{0};
|
||||
Void::Log::set_sink([&](auto, auto, auto) { ++count; });
|
||||
|
||||
constexpr int N = 64;
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(N);
|
||||
for (int i = 0; i < N; ++i) {
|
||||
threads.emplace_back([] {
|
||||
Void::Log::log(Void::Log::Level::Debug, "test", "concurrent");
|
||||
});
|
||||
}
|
||||
for (auto& t : threads) {
|
||||
t.join();
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(count.load(), N);
|
||||
}
|
||||
|
||||
Void::Testing::Test all_levels_are_forwarded()
|
||||
{
|
||||
SinkGuard guard;
|
||||
int calls = 0;
|
||||
Void::Log::set_sink([&](auto, auto, auto) { ++calls; });
|
||||
|
||||
Void::Log::log(Void::Log::Level::Trace, "m", "t");
|
||||
Void::Log::log(Void::Log::Level::Debug, "m", "d");
|
||||
Void::Log::log(Void::Log::Level::Info, "m", "i");
|
||||
Void::Log::log(Void::Log::Level::Warning, "m", "w");
|
||||
Void::Log::log(Void::Log::Level::Error, "m", "e");
|
||||
Void::Log::log(Void::Log::Level::Fatal, "m", "f");
|
||||
|
||||
Void::Testing::expect_eq(calls, 6);
|
||||
}
|
||||
@@ -21,7 +21,7 @@ class ConsoleLoggerTests : public Void::Testing::Fixture
|
||||
const auto content = out.str();
|
||||
Void::Testing::expect(content.find("hello world") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(content.find("INFO") != std::string::npos);
|
||||
Void::Testing::expect(content.find("Info") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test level_filtering()
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
#include <Void/Math/Mat.hpp>
|
||||
#include <Void/Math/Vec.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <cmath>
|
||||
|
||||
static constexpr float EPS = 1e-5f;
|
||||
|
||||
// ── Vec2 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test vec2_add()
|
||||
{
|
||||
constexpr auto v = Void::Math::Vec2f{1, 2} + Void::Math::Vec2f{3, 4};
|
||||
static_assert(v.x == 4.f && v.y == 6.f);
|
||||
Void::Testing::expect_eq(v.x, 4.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_subtract()
|
||||
{
|
||||
constexpr auto v = Void::Math::Vec2f{3, 4} - Void::Math::Vec2f{1, 1};
|
||||
Void::Testing::expect_eq(v.x, 2.f);
|
||||
Void::Testing::expect_eq(v.y, 3.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_scale()
|
||||
{
|
||||
constexpr auto v = Void::Math::Vec2f{2, 3} * 2.f;
|
||||
Void::Testing::expect_eq(v.x, 4.f);
|
||||
Void::Testing::expect_eq(v.y, 6.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_dot()
|
||||
{
|
||||
constexpr auto d = Void::Math::Vec2f{1, 0}.dot({0, 1});
|
||||
static_assert(d == 0.f);
|
||||
Void::Testing::expect_eq(d, 0.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_length()
|
||||
{
|
||||
const Void::Math::Vec2f v{3, 4};
|
||||
Void::Testing::expect(std::abs(v.length() - 5.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_normalized()
|
||||
{
|
||||
const Void::Math::Vec2f v = Void::Math::Vec2f{3, 4}.normalized();
|
||||
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec2_lerp()
|
||||
{
|
||||
const auto v = Void::Math::lerp(Void::Math::Vec2f{0, 0},
|
||||
Void::Math::Vec2f{2, 4}, 0.5f);
|
||||
Void::Testing::expect(std::abs(v.x - 1.f) < EPS);
|
||||
Void::Testing::expect(std::abs(v.y - 2.f) < EPS);
|
||||
}
|
||||
|
||||
// ── Vec3 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test vec3_cross_unit_vectors()
|
||||
{
|
||||
constexpr Void::Math::Vec3f x{1, 0, 0};
|
||||
constexpr Void::Math::Vec3f y{0, 1, 0};
|
||||
constexpr Void::Math::Vec3f z = x.cross(y);
|
||||
static_assert(z.x == 0.f && z.y == 0.f && z.z == 1.f);
|
||||
Void::Testing::expect_eq(z.z, 1.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec3_length()
|
||||
{
|
||||
const Void::Math::Vec3f v{1, 2, 2};
|
||||
Void::Testing::expect(std::abs(v.length() - 3.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec3_normalized_unit_length()
|
||||
{
|
||||
const auto v = Void::Math::Vec3f{2, 3, 6}.normalized();
|
||||
Void::Testing::expect(std::abs(v.length() - 1.f) < EPS);
|
||||
}
|
||||
|
||||
// ── Vec4 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test vec4_from_vec3()
|
||||
{
|
||||
constexpr Void::Math::Vec4f v{Void::Math::Vec3f{1, 2, 3}, 1.f};
|
||||
static_assert(v.w == 1.f);
|
||||
Void::Testing::expect_eq(v.z, 3.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test vec4_xyz()
|
||||
{
|
||||
constexpr Void::Math::Vec4f v{1, 2, 3, 4};
|
||||
constexpr auto xyz = v.xyz();
|
||||
static_assert(xyz.x == 1.f && xyz.y == 2.f && xyz.z == 3.f);
|
||||
}
|
||||
|
||||
// ── Mat4 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test mat4_identity_times_vec()
|
||||
{
|
||||
constexpr auto I = Void::Math::Mat4f::identity();
|
||||
constexpr Void::Math::Vec4f v{1, 2, 3, 1};
|
||||
constexpr auto r = I * v;
|
||||
static_assert(r.x == 1.f && r.y == 2.f && r.z == 3.f && r.w == 1.f);
|
||||
}
|
||||
|
||||
Void::Testing::Test mat4_translation()
|
||||
{
|
||||
const auto T = Void::Math::Mat4f::translation(1, 2, 3);
|
||||
const Void::Math::Vec4f v{0, 0, 0, 1};
|
||||
const auto r = T * v;
|
||||
Void::Testing::expect(std::abs(r.x - 1.f) < EPS);
|
||||
Void::Testing::expect(std::abs(r.y - 2.f) < EPS);
|
||||
Void::Testing::expect(std::abs(r.z - 3.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test mat4_scale()
|
||||
{
|
||||
const auto S = Void::Math::Mat4f::scale(2, 3, 4);
|
||||
const Void::Math::Vec4f v{1, 1, 1, 1};
|
||||
const auto r = S * v;
|
||||
Void::Testing::expect(std::abs(r.x - 2.f) < EPS);
|
||||
Void::Testing::expect(std::abs(r.y - 3.f) < EPS);
|
||||
Void::Testing::expect(std::abs(r.z - 4.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test mat4_rotation_z_90_degrees()
|
||||
{
|
||||
const auto R = Void::Math::Mat4f::rotation_z(
|
||||
static_cast<float>(M_PI) / 2.f);
|
||||
const Void::Math::Vec4f v{1, 0, 0, 0};
|
||||
const auto r = R * v;
|
||||
Void::Testing::expect(std::abs(r.x - 0.f) < EPS);
|
||||
Void::Testing::expect(std::abs(r.y - 1.f) < EPS);
|
||||
}
|
||||
|
||||
Void::Testing::Test mat4_mul_identity_nop()
|
||||
{
|
||||
const auto A = Void::Math::Mat4f::translation(1, 2, 3);
|
||||
const auto I = Void::Math::Mat4f::identity();
|
||||
const auto R = A * I;
|
||||
Void::Testing::expect_eq(R, A);
|
||||
}
|
||||
|
||||
Void::Testing::Test mat4_transpose()
|
||||
{
|
||||
auto M = Void::Math::Mat4f::identity();
|
||||
M.at(0, 1) = 5.f;
|
||||
const auto T = M.transposed();
|
||||
Void::Testing::expect_eq(T.at(1, 0), 5.f);
|
||||
Void::Testing::expect_eq(T.at(0, 1), 0.f);
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
#include <Void/Process/Process.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
Void::Testing::Test shell_echo_captures_stdout()
|
||||
{
|
||||
auto r = Void::Process::shell("echo hello");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 0);
|
||||
Void::Testing::expect(r->out.find("hello") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test shell_exit_code_propagates()
|
||||
{
|
||||
auto r = Void::Process::shell("exit 42");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test shell_captures_stderr_separately()
|
||||
{
|
||||
auto r = Void::Process::shell("echo err_output >&2");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->err.find("err_output") != std::string::npos);
|
||||
Void::Testing::expect(r->out.find("err_output") == std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test run_with_args_builds_correct_argv()
|
||||
{
|
||||
auto r = Void::Process::run("/bin/echo", {"hello", "world"});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 0);
|
||||
Void::Testing::expect(r->out.find("hello world") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test run_stdin_forwarded_to_cat()
|
||||
{
|
||||
auto r = Void::Process::run("/bin/cat", {}, "piped_input");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 0);
|
||||
Void::Testing::expect(r->out.find("piped_input") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test run_missing_executable_returns_code_127()
|
||||
{
|
||||
auto r = Void::Process::run("/this/binary/does/not/exist");
|
||||
// fork succeeds, execvp fails, child _exits(127)
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 127);
|
||||
}
|
||||
|
||||
Void::Testing::Test run_stdout_and_stderr_independent()
|
||||
{
|
||||
auto r = Void::Process::shell(
|
||||
"echo to_stdout; echo to_stderr >&2");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect(r->out.find("to_stdout") != std::string::npos);
|
||||
Void::Testing::expect(r->err.find("to_stderr") != std::string::npos);
|
||||
Void::Testing::expect(r->out.find("to_stderr") == std::string::npos);
|
||||
Void::Testing::expect(r->err.find("to_stdout") == std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test run_large_output_is_captured_fully()
|
||||
{
|
||||
// Generate 10 000 lines; poll-based reader must accumulate them all.
|
||||
auto r = Void::Process::shell("seq 1 10000");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(r->exit_code, 0);
|
||||
// "10000\n" must be present, and total output must be substantial
|
||||
Void::Testing::expect(r->out.find("10000") != std::string::npos);
|
||||
Void::Testing::expect(r->out.size() > 40000);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#include <Void/Random/Random.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
// ── Xoshiro256 ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test xoshiro_produces_output()
|
||||
{
|
||||
Void::Random::Xoshiro256 rng{42};
|
||||
const auto a = rng();
|
||||
const auto b = rng();
|
||||
Void::Testing::expect(a != b);
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_same_seed_same_sequence()
|
||||
{
|
||||
Void::Random::Xoshiro256 r1{12345};
|
||||
Void::Random::Xoshiro256 r2{12345};
|
||||
for (int i = 0; i < 10; ++i) {
|
||||
Void::Testing::expect_eq(r1(), r2());
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_different_seeds_differ()
|
||||
{
|
||||
Void::Random::Xoshiro256 r1{1};
|
||||
Void::Random::Xoshiro256 r2{2};
|
||||
Void::Testing::expect(r1() != r2());
|
||||
}
|
||||
|
||||
// ── uniform_int ──────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test uniform_int_in_range()
|
||||
{
|
||||
for (int i = 0; i < 100; ++i) {
|
||||
const auto v = Void::Random::uniform_int(1, 6);
|
||||
Void::Testing::expect(v >= 1);
|
||||
Void::Testing::expect(v <= 6);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test uniform_int_single_value()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Random::uniform_int(7, 7), 7);
|
||||
}
|
||||
|
||||
// ── uniform_real ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test uniform_real_in_range()
|
||||
{
|
||||
for (int i = 0; i < 50; ++i) {
|
||||
const double v = Void::Random::uniform_real(0.0, 1.0);
|
||||
Void::Testing::expect(v >= 0.0);
|
||||
Void::Testing::expect(v < 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
// ── coin_flip ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test coin_flip_always_true()
|
||||
{
|
||||
Void::Testing::expect(Void::Random::coin_flip(1.0));
|
||||
}
|
||||
|
||||
Void::Testing::Test coin_flip_always_false()
|
||||
{
|
||||
Void::Testing::expect(!Void::Random::coin_flip(0.0));
|
||||
}
|
||||
|
||||
// ── shuffle ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test shuffle_preserves_elements()
|
||||
{
|
||||
std::vector<int> v{1, 2, 3, 4, 5};
|
||||
const auto orig = v;
|
||||
Void::Random::shuffle(v);
|
||||
auto sorted = v;
|
||||
std::sort(sorted.begin(), sorted.end());
|
||||
Void::Testing::expect_eq(sorted, orig);
|
||||
}
|
||||
|
||||
// ── random_string ────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test random_string_correct_length()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Random::random_string(10).size(),
|
||||
std::size_t{10});
|
||||
}
|
||||
|
||||
Void::Testing::Test random_string_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::Random::random_string(0).empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test random_string_varies()
|
||||
{
|
||||
// Extremely unlikely to produce identical 16-char strings twice
|
||||
const auto a = Void::Random::random_string(16);
|
||||
const auto b = Void::Random::random_string(16);
|
||||
Void::Testing::expect(a != b);
|
||||
}
|
||||
|
||||
// ── sample_indices ───────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test sample_indices_correct_count()
|
||||
{
|
||||
const auto idxs = Void::Random::sample_indices(20, 5);
|
||||
Void::Testing::expect_eq(idxs.size(), std::size_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test sample_indices_in_range()
|
||||
{
|
||||
const auto idxs = Void::Random::sample_indices(10, 10);
|
||||
for (auto i : idxs) {
|
||||
Void::Testing::expect(i < std::size_t{10});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
#include <Void/String/String.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
// ── trim ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trim_removes_leading_and_trailing_spaces()
|
||||
{
|
||||
Void::Testing::expect_eq(
|
||||
std::string{Void::String::trim(" hello ")},
|
||||
std::string{"hello"});
|
||||
}
|
||||
|
||||
Void::Testing::Test trim_leaves_interior_whitespace()
|
||||
{
|
||||
Void::Testing::expect_eq(
|
||||
std::string{Void::String::trim(" a b ")},
|
||||
std::string{"a b"});
|
||||
}
|
||||
|
||||
Void::Testing::Test trim_empty_string_stays_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::String::trim("").empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test trim_only_whitespace_returns_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::String::trim(" \t\n ").empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test trim_left_only_removes_prefix()
|
||||
{
|
||||
Void::Testing::expect_eq(
|
||||
std::string{Void::String::trim_left(" hi ")},
|
||||
std::string{"hi "});
|
||||
}
|
||||
|
||||
Void::Testing::Test trim_right_only_removes_suffix()
|
||||
{
|
||||
Void::Testing::expect_eq(
|
||||
std::string{Void::String::trim_right(" hi ")},
|
||||
std::string{" hi"});
|
||||
}
|
||||
|
||||
// ── split ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test split_char_produces_correct_parts()
|
||||
{
|
||||
const auto parts = Void::String::split("a,b,c", ',');
|
||||
Void::Testing::expect_eq(parts.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(std::string{parts[0]}, std::string{"a"});
|
||||
Void::Testing::expect_eq(std::string{parts[1]}, std::string{"b"});
|
||||
Void::Testing::expect_eq(std::string{parts[2]}, std::string{"c"});
|
||||
}
|
||||
|
||||
Void::Testing::Test split_char_preserves_empty_tokens()
|
||||
{
|
||||
const auto parts = Void::String::split("a,,b", ',');
|
||||
Void::Testing::expect_eq(parts.size(), std::size_t{3});
|
||||
Void::Testing::expect(parts[1].empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test split_char_no_delimiter_returns_whole_string()
|
||||
{
|
||||
const auto parts = Void::String::split("hello", ',');
|
||||
Void::Testing::expect_eq(parts.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(std::string{parts[0]}, std::string{"hello"});
|
||||
}
|
||||
|
||||
Void::Testing::Test split_string_delimiter_works()
|
||||
{
|
||||
const auto parts = Void::String::split("a::b::c", "::");
|
||||
Void::Testing::expect_eq(parts.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(std::string{parts[1]}, std::string{"b"});
|
||||
}
|
||||
|
||||
Void::Testing::Test split_empty_delimiter_returns_whole()
|
||||
{
|
||||
const auto parts = Void::String::split("abc", "");
|
||||
Void::Testing::expect_eq(parts.size(), std::size_t{1});
|
||||
}
|
||||
|
||||
// ── join ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test join_vector_of_string_view()
|
||||
{
|
||||
const std::vector<std::string_view> parts{"a", "b", "c"};
|
||||
Void::Testing::expect_eq(Void::String::join(parts, ", "),
|
||||
std::string{"a, b, c"});
|
||||
}
|
||||
|
||||
Void::Testing::Test join_single_element_no_separator()
|
||||
{
|
||||
const std::vector<std::string_view> parts{"only"};
|
||||
Void::Testing::expect_eq(Void::String::join(parts, "-"),
|
||||
std::string{"only"});
|
||||
}
|
||||
|
||||
Void::Testing::Test join_empty_range_returns_empty()
|
||||
{
|
||||
const std::vector<std::string_view> parts;
|
||||
Void::Testing::expect(Void::String::join(parts, ",").empty());
|
||||
}
|
||||
|
||||
// ── case ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test to_lower_converts_ascii()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::to_lower("Hello World"),
|
||||
std::string{"hello world"});
|
||||
}
|
||||
|
||||
Void::Testing::Test to_upper_converts_ascii()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::to_upper("Hello World"),
|
||||
std::string{"HELLO WORLD"});
|
||||
}
|
||||
|
||||
// ── replace_all ───────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test replace_all_replaces_all_occurrences()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::replace_all("aXbXc", "X", "--"),
|
||||
std::string{"a--b--c"});
|
||||
}
|
||||
|
||||
Void::Testing::Test replace_all_with_empty_from_returns_original()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::replace_all("abc", "", "x"),
|
||||
std::string{"abc"});
|
||||
}
|
||||
|
||||
Void::Testing::Test replace_all_no_match_returns_original()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::replace_all("abc", "z", "x"),
|
||||
std::string{"abc"});
|
||||
}
|
||||
|
||||
// ── pad ───────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pad_right_pads_to_width()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::pad_right("hi", 5),
|
||||
std::string{"hi "});
|
||||
}
|
||||
|
||||
Void::Testing::Test pad_right_no_op_when_already_wide()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::pad_right("hello", 3),
|
||||
std::string{"hello"});
|
||||
}
|
||||
|
||||
Void::Testing::Test pad_left_pads_to_width()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::pad_left("hi", 5),
|
||||
std::string{" hi"});
|
||||
}
|
||||
|
||||
// ── misc ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test repeat_produces_correct_output()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::repeat("ab", 3),
|
||||
std::string{"ababab"});
|
||||
}
|
||||
|
||||
Void::Testing::Test repeat_zero_returns_empty()
|
||||
{
|
||||
Void::Testing::expect(Void::String::repeat("ab", 0).empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test count_occurrences_finds_all()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::count_occurrences("abab", "ab"),
|
||||
std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test count_occurrences_no_match_returns_zero()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::String::count_occurrences("abc", "x"),
|
||||
std::size_t{0});
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
#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()
|
||||
{
|
||||
Void::Time::Stopwatch sw;
|
||||
Void::Testing::expect(sw.elapsed().count() >= 0);
|
||||
}
|
||||
|
||||
Void::Testing::Test stopwatch_elapsed_as_milliseconds()
|
||||
{
|
||||
Void::Time::Stopwatch sw;
|
||||
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_reset_restarts_from_zero()
|
||||
{
|
||||
Void::Time::Stopwatch sw;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds{5});
|
||||
sw.reset();
|
||||
const auto ns = sw.elapsed().count();
|
||||
Void::Testing::expect(ns < 5'000'000); // < 5ms since reset
|
||||
}
|
||||
|
||||
Void::Testing::Test stopwatch_split_returns_elapsed_and_resets()
|
||||
{
|
||||
Void::Time::Stopwatch sw;
|
||||
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 reset < before reset
|
||||
}
|
||||
|
||||
// ── 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');
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
#include <Void/Uuid/Uuid.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
// ── v4 ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test v4_not_nil()
|
||||
{
|
||||
Void::Testing::expect(!Void::Uuid::v4().is_nil());
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_version_nibble()
|
||||
{
|
||||
const auto u = Void::Uuid::v4();
|
||||
Void::Testing::expect_eq(u.bytes[6] >> 4, std::uint8_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_variant_bits()
|
||||
{
|
||||
const auto u = Void::Uuid::v4();
|
||||
// top two bits of byte 8 must be 10
|
||||
Void::Testing::expect_eq(u.bytes[8] >> 6, std::uint8_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_unique()
|
||||
{
|
||||
Void::Testing::expect(Void::Uuid::v4() != Void::Uuid::v4());
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_string_length()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Uuid::v4().to_string().size(),
|
||||
std::size_t{36});
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_string_has_dashes()
|
||||
{
|
||||
const auto s = Void::Uuid::v4().to_string();
|
||||
Void::Testing::expect_eq(s[8], '-');
|
||||
Void::Testing::expect_eq(s[13], '-');
|
||||
Void::Testing::expect_eq(s[18], '-');
|
||||
Void::Testing::expect_eq(s[23], '-');
|
||||
}
|
||||
|
||||
// ── v5_lite ───────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test v5_lite_deterministic()
|
||||
{
|
||||
const auto a = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "example.com");
|
||||
const auto b = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "example.com");
|
||||
Void::Testing::expect_eq(a, b);
|
||||
}
|
||||
|
||||
Void::Testing::Test v5_lite_different_names()
|
||||
{
|
||||
const auto a = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "foo.com");
|
||||
const auto b = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "bar.com");
|
||||
Void::Testing::expect(a != b);
|
||||
}
|
||||
|
||||
Void::Testing::Test v5_lite_different_namespaces()
|
||||
{
|
||||
const auto a = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "example");
|
||||
const auto b = Void::Uuid::v5_lite(Void::Uuid::Namespace::Url, "example");
|
||||
Void::Testing::expect(a != b);
|
||||
}
|
||||
|
||||
Void::Testing::Test v5_lite_version_nibble()
|
||||
{
|
||||
const auto u = Void::Uuid::v5_lite(Void::Uuid::Namespace::Dns, "test");
|
||||
Void::Testing::expect_eq(u.bytes[6] >> 4, std::uint8_t{5});
|
||||
}
|
||||
|
||||
// ── parse ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test parse_round_trip()
|
||||
{
|
||||
const auto u = Void::Uuid::v4();
|
||||
const auto s = u.to_string();
|
||||
const auto parsed = Void::Uuid::parse(s);
|
||||
Void::Testing::expect(parsed.has_value());
|
||||
Void::Testing::expect_eq(*parsed, u);
|
||||
}
|
||||
|
||||
Void::Testing::Test parse_uppercase()
|
||||
{
|
||||
const auto opt = Void::Uuid::parse(
|
||||
"550E8400-E29B-41D4-A716-446655440000");
|
||||
Void::Testing::expect(opt.has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test parse_invalid_returns_nullopt()
|
||||
{
|
||||
Void::Testing::expect(!Void::Uuid::parse("not-a-uuid").has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test parse_32_char_hex()
|
||||
{
|
||||
const auto opt = Void::Uuid::parse("550e8400e29b41d4a716446655440000");
|
||||
Void::Testing::expect(opt.has_value());
|
||||
}
|
||||
|
||||
// ── nil ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test nil_uuid_is_nil()
|
||||
{
|
||||
Void::Testing::expect(Void::Uuid::Uuid::nil().is_nil());
|
||||
}
|
||||
|
||||
Void::Testing::Test v4_not_nil_check()
|
||||
{
|
||||
Void::Testing::expect(!Void::Uuid::v4().is_nil());
|
||||
}
|
||||
Reference in new issue
Block a user