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>
194 lines
5.8 KiB
C++
194 lines
5.8 KiB
C++
#include <Void/Functional/Either.hpp>
|
|
#include <Void/Functional/Opt.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <optional>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
using namespace Void::Either;
|
|
using E = Either<std::string, int>;
|
|
|
|
// ── Either construction & query ───────────────────────────────────────────────
|
|
|
|
Void::Testing::Test either_left_is_left()
|
|
{
|
|
const auto e = E::left("error");
|
|
Void::Testing::expect(e.is_left());
|
|
Void::Testing::expect(!e.is_right());
|
|
Void::Testing::expect_eq(e.left_value(), std::string{"error"});
|
|
}
|
|
|
|
Void::Testing::Test either_right_is_right()
|
|
{
|
|
const auto e = E::right(42);
|
|
Void::Testing::expect(e.is_right());
|
|
Void::Testing::expect(!e.is_left());
|
|
Void::Testing::expect_eq(e.right_value(), 42);
|
|
}
|
|
|
|
Void::Testing::Test either_map_transforms_right()
|
|
{
|
|
const auto e = E::right(5);
|
|
const auto m = e.map([](int x) { return x * 2; });
|
|
Void::Testing::expect(m.is_right());
|
|
Void::Testing::expect_eq(m.right_value(), 10);
|
|
}
|
|
|
|
Void::Testing::Test either_map_skips_left()
|
|
{
|
|
const auto e = E::left("fail");
|
|
const auto m = e.map([](int x) { return x * 2; });
|
|
Void::Testing::expect(m.is_left());
|
|
Void::Testing::expect_eq(m.left_value(), std::string{"fail"});
|
|
}
|
|
|
|
Void::Testing::Test either_map_left_transforms_left()
|
|
{
|
|
const auto e = E::left("err");
|
|
const auto m = e.map_left([](const std::string& s) { return s.size(); });
|
|
Void::Testing::expect(m.is_left());
|
|
Void::Testing::expect_eq(m.left_value(), std::size_t{3});
|
|
}
|
|
|
|
Void::Testing::Test either_fold_right()
|
|
{
|
|
const auto e = E::right(10);
|
|
const auto v = e.fold(
|
|
[](const std::string&) { return -1; },
|
|
[](int x) { return x + 1; });
|
|
Void::Testing::expect_eq(v, 11);
|
|
}
|
|
|
|
Void::Testing::Test either_fold_left()
|
|
{
|
|
const auto e = E::left("oops");
|
|
const auto v = e.fold(
|
|
[](const std::string& s) { return static_cast<int>(s.size()); },
|
|
[](int x) { return x; });
|
|
Void::Testing::expect_eq(v, 4);
|
|
}
|
|
|
|
Void::Testing::Test either_swap()
|
|
{
|
|
const auto e = E::right(5);
|
|
const auto s = e.swap();
|
|
Void::Testing::expect(s.is_left());
|
|
Void::Testing::expect_eq(s.left_value(), 5);
|
|
}
|
|
|
|
Void::Testing::Test either_equality()
|
|
{
|
|
Void::Testing::expect(E::right(1) == E::right(1));
|
|
Void::Testing::expect(!(E::right(1) == E::right(2)));
|
|
Void::Testing::expect(!(E::left("x") == E::right(0)));
|
|
}
|
|
|
|
// ── Opt<T> ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test opt_some_has_value()
|
|
{
|
|
const auto o = some(42);
|
|
Void::Testing::expect(o.has_value());
|
|
Void::Testing::expect_eq(*o, 42);
|
|
}
|
|
|
|
Void::Testing::Test opt_none_empty()
|
|
{
|
|
const auto o = none<int>();
|
|
Void::Testing::expect(!o.has_value());
|
|
}
|
|
|
|
Void::Testing::Test opt_map_chain()
|
|
{
|
|
const auto r = some(3)
|
|
.map([](int x) { return x + 1; })
|
|
.map([](int x) { return x * 2; });
|
|
Void::Testing::expect_eq(*r, 8);
|
|
}
|
|
|
|
Void::Testing::Test opt_filter_fluent()
|
|
{
|
|
Void::Testing::expect(some(10).filter([](int x) { return x > 5; }).has_value());
|
|
Void::Testing::expect(!some(2).filter([](int x) { return x > 5; }).has_value());
|
|
}
|
|
|
|
Void::Testing::Test opt_flat_map_fluent()
|
|
{
|
|
const auto r = some(5).flat_map([](int x) -> Opt<std::string> {
|
|
return some(std::to_string(x));
|
|
});
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, std::string{"5"});
|
|
}
|
|
|
|
Void::Testing::Test opt_value_or()
|
|
{
|
|
Void::Testing::expect_eq(some(7).value_or(0), 7);
|
|
Void::Testing::expect_eq(none<int>().value_or(99), 99);
|
|
}
|
|
|
|
Void::Testing::Test opt_ok_or_converts_to_either()
|
|
{
|
|
const auto r = some(42).ok_or(std::string{"missing"});
|
|
Void::Testing::expect(r.is_right());
|
|
Void::Testing::expect_eq(r.right_value(), 42);
|
|
|
|
const auto err = none<int>().ok_or(std::string{"missing"});
|
|
Void::Testing::expect(err.is_left());
|
|
}
|
|
|
|
// ── free optional utilities ───────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test opt_transform_present()
|
|
{
|
|
const std::optional<int> o{5};
|
|
const auto r = opt_transform(o, [](int x) { return x * 2; });
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(*r, 10);
|
|
}
|
|
|
|
Void::Testing::Test opt_transform_empty()
|
|
{
|
|
const std::optional<int> o;
|
|
const auto r = opt_transform(o, [](int x) { return x * 2; });
|
|
Void::Testing::expect(!r.has_value());
|
|
}
|
|
|
|
Void::Testing::Test opt_filter_free()
|
|
{
|
|
const std::optional<int> o{10};
|
|
Void::Testing::expect(opt_filter(o, [](int x) { return x > 5; }).has_value());
|
|
Void::Testing::expect(!opt_filter(o, [](int x) { return x > 15; }).has_value());
|
|
}
|
|
|
|
Void::Testing::Test opt_expect_throws_on_empty()
|
|
{
|
|
const std::optional<int> o;
|
|
bool threw = false;
|
|
try { [[maybe_unused]] auto v = opt_expect(o, "empty"); } catch (...) { threw = true; }
|
|
Void::Testing::expect(threw);
|
|
}
|
|
|
|
Void::Testing::Test opt_zip_both_present()
|
|
{
|
|
const auto r = opt_zip(std::optional<int>{1}, std::optional<std::string>{"hi"});
|
|
Void::Testing::expect(r.has_value());
|
|
Void::Testing::expect_eq(r->first, 1);
|
|
Void::Testing::expect_eq(r->second, std::string{"hi"});
|
|
}
|
|
|
|
Void::Testing::Test opt_zip_one_empty()
|
|
{
|
|
const auto r = opt_zip(std::optional<int>{1}, std::optional<int>{});
|
|
Void::Testing::expect(!r.has_value());
|
|
}
|
|
|
|
Void::Testing::Test opt_flatten_nested()
|
|
{
|
|
const std::optional<std::optional<int>> o{std::optional<int>{7}};
|
|
Void::Testing::expect_eq(*opt_flatten(o), 7);
|
|
Void::Testing::expect(!opt_flatten(std::optional<std::optional<int>>{std::optional<int>{}}).has_value());
|
|
}
|