Headers move, CMake targets merge, C++ namespaces stay unchanged: - Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant - Functional: Pipeline, Either, Result, Retry - Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval - Math: +Matrix, Numeric, Random, Histogram - Text: Str, Format, Tokenizer, Diff, Encoding, Semver - Data: Json, Csv, Ini - Async: Coro, Concurrency, EventBus, Observable - Time: +Bench - Log: Log facade + Logging framework (facade now header-only, breaking the Stacktrace<->Log link cycle the merge would have created) - System: FileSystem + Process - Configuration: +Argparse - Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail Module system fixes: - OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no longer depends on the alphabetical discovery order of src/ subdirs (Testing -> Text would have been silently dropped). - Every header-only module now has a tracked module.cmake; previously the empty src/<Name> marker dirs were untracked, so a fresh clone would have silently skipped those modules. - VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT. Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files prefixed: ResultBasic etc.). Test count verified identical (831) before and after. README/CLAUDE.md module tables updated. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
148 lines
5.1 KiB
C++
148 lines
5.1 KiB
C++
#include <Void/Core/Variant.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <string>
|
|
#include <variant>
|
|
|
|
using namespace Void::Variant;
|
|
using V3 = std::variant<int, double, std::string>;
|
|
|
|
// ── match ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_match_int()
|
|
{
|
|
V3 v = 42;
|
|
const auto s = match(v,
|
|
[](int i) { return std::to_string(i); },
|
|
[](double d) { return std::to_string(d); },
|
|
[](const std::string& s) { return s; });
|
|
Void::Testing::expect_eq(s, std::string{"42"});
|
|
}
|
|
|
|
Void::Testing::Test variant_match_string()
|
|
{
|
|
V3 v = std::string{"hello"};
|
|
bool got_string = false;
|
|
match(v,
|
|
[](int) {},
|
|
[](double) {},
|
|
[&](const std::string&) { got_string = true; });
|
|
Void::Testing::expect(got_string);
|
|
}
|
|
|
|
Void::Testing::Test variant_match_returns_value()
|
|
{
|
|
V3 v = 3.14;
|
|
const int result = match(v,
|
|
[](int i) { return i; },
|
|
[](double d) { return static_cast<int>(d); },
|
|
[](const std::string&) { return 0; });
|
|
Void::Testing::expect_eq(result, 3);
|
|
}
|
|
|
|
// ── overload ──────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_overload_helper()
|
|
{
|
|
V3 v = 99;
|
|
const auto visitor = overload{
|
|
[](int i) { return i * 2; },
|
|
[](double d) { return static_cast<int>(d); },
|
|
[](const std::string&) { return -1; }
|
|
};
|
|
Void::Testing::expect_eq(std::visit(visitor, v), 198);
|
|
}
|
|
|
|
// ── holds ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_holds_correct_type()
|
|
{
|
|
V3 v = 1;
|
|
Void::Testing::expect(holds<int>(v));
|
|
Void::Testing::expect(!holds<double>(v));
|
|
Void::Testing::expect(!holds<std::string>(v));
|
|
}
|
|
|
|
Void::Testing::Test variant_holds_after_assignment()
|
|
{
|
|
V3 v = 1;
|
|
v = 3.14;
|
|
Void::Testing::expect(!holds<int>(v));
|
|
Void::Testing::expect(holds<double>(v));
|
|
}
|
|
|
|
// ── get_or ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_get_or_present()
|
|
{
|
|
V3 v = 7;
|
|
Void::Testing::expect_eq(get_or<int>(v, 0), 7);
|
|
}
|
|
|
|
Void::Testing::Test variant_get_or_absent()
|
|
{
|
|
V3 v = 3.14;
|
|
Void::Testing::expect_eq(get_or<int>(v, -1), -1);
|
|
}
|
|
|
|
// ── try_get ───────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_try_get_present()
|
|
{
|
|
V3 v = std::string{"world"};
|
|
const auto r = try_get<std::string>(v);
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, std::string{"world"});
|
|
}
|
|
|
|
Void::Testing::Test variant_try_get_absent()
|
|
{
|
|
V3 v = 1;
|
|
Void::Testing::expect(!try_get<std::string>(v).has_value());
|
|
}
|
|
|
|
// ── index_of ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_index_of_compile_time()
|
|
{
|
|
static_assert(index_of<int, V3> == 0);
|
|
static_assert(index_of<double, V3> == 1);
|
|
static_assert(index_of<std::string, V3> == 2);
|
|
Void::Testing::expect(true); // compile-time assertion passed
|
|
}
|
|
|
|
// ── apply_to ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_apply_to_matching()
|
|
{
|
|
V3 v = 5;
|
|
const auto r = apply_to<int>(v, [](int i) { return i * 10; });
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, 50);
|
|
}
|
|
|
|
Void::Testing::Test variant_apply_to_non_matching()
|
|
{
|
|
V3 v = 3.14;
|
|
const auto r = apply_to<int>(v, [](int i) { return i; });
|
|
Void::Testing::expect(!r.has_value());
|
|
}
|
|
|
|
// ── narrow ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test variant_narrow_convertible()
|
|
{
|
|
V3 v = 7;
|
|
const auto r = narrow<double>(v); // int is convertible to double
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, 7.0);
|
|
}
|
|
|
|
Void::Testing::Test variant_narrow_not_convertible()
|
|
{
|
|
V3 v = std::string{"x"};
|
|
const auto r = narrow<double>(v);
|
|
Void::Testing::expect(!r.has_value());
|
|
}
|