#include #include #include #include #include #include #include using namespace Void::Either; using E = Either; // ── 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(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 ──────────────────────────────────────────────────────────────────── 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(); 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 { 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().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().ok_or(std::string{"missing"}); Void::Testing::expect(err.is_left()); } // ── free optional utilities ─────────────────────────────────────────────────── Void::Testing::Test opt_transform_present() { const std::optional 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 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 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 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{1}, std::optional{"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{1}, std::optional{}); Void::Testing::expect(!r.has_value()); } Void::Testing::Test opt_flatten_nested() { const std::optional> o{std::optional{7}}; Void::Testing::expect_eq(*opt_flatten(o), 7); Void::Testing::expect(!opt_flatten(std::optional>{std::optional{}}).has_value()); }