Files
Library/tests/standalone/Either/Either.cpp
T
faumarayandClaude Sonnet 4.6 58ff7b38a0 Add 20+ new modules with tests: Pipeline, Matrix, Tokenizer, ScopeGuard, Bitset, Optional, Counted, Curry, TypeMap, Bench, DisjointSet, Histogram, StopWatch, and more
Also fixes Diff/Memo/PriorityQueue from previous session: LineDiff is now
an alias for Edit<string> so count_op/apply work on line diffs; Memoized
uses std::apply instead of type-based get to handle duplicate arg types.

874/877 tests pass (3 are platform-gated plugin tests).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 23:49:09 +03:00

99 lines
3.3 KiB
C++

#include <Void/Either/Either.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
using namespace Void::Either;
using E = Either<std::string, int>;
// ── 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);
}
// ── map ───────────────────────────────────────────────────────────────────────
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_map_left_skips_right()
{
const auto e = E::right(7);
const auto m = e.map_left([](const std::string& s) { return s.size(); });
Void::Testing::expect(m.is_right());
Void::Testing::expect_eq(m.right_value(), 7);
}
// ── fold ──────────────────────────────────────────────────────────────────────
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);
}
// ── equality ──────────────────────────────────────────────────────────────────
Void::Testing::Test either_equality_right()
{
const auto a = E::right(1);
const auto b = E::right(1);
const auto c = E::right(2);
Void::Testing::expect(a == b);
Void::Testing::expect(!(a == c));
}
Void::Testing::Test either_equality_left_vs_right()
{
const auto a = E::left("x");
const auto b = E::right(0);
Void::Testing::expect(!(a == b));
}