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>
194 lines
5.8 KiB
C++
194 lines
5.8 KiB
C++
#include <Void/Either/Either.hpp>
|
|
#include <Void/Either/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());
|
|
}
|