Files
Library/tests/standalone/Optional/Optional.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

169 lines
4.7 KiB
C++

#include <Void/Optional/Optional.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <optional>
#include <string>
#include <utility>
using namespace Void::Optional;
// ── free functions ────────────────────────────────────────────────────────────
Void::Testing::Test opt_transform_present()
{
const std::optional<int> o{5};
const auto r = 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 = transform(o, [](int x) { return x * 2; });
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test opt_and_then_chain()
{
const std::optional<int> o{4};
const auto r = and_then(o, [](int x) -> std::optional<std::string> {
if (x > 0) return std::to_string(x);
return std::nullopt;
});
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, std::string{"4"});
}
Void::Testing::Test opt_and_then_short_circuits()
{
const std::optional<int> o;
bool called = false;
const auto r = and_then(o, [&](int) -> std::optional<int> { called = true; return 1; });
Void::Testing::expect(!called);
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test opt_filter_passes()
{
const std::optional<int> o{10};
const auto r = filter(o, [](int x) { return x > 5; });
Void::Testing::expect(r.has_value());
}
Void::Testing::Test opt_filter_blocks()
{
const std::optional<int> o{3};
const auto r = filter(o, [](int x) { return x > 5; });
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test opt_expect_present()
{
const std::optional<int> o{42};
Void::Testing::expect_eq(expect(o), 42);
}
Void::Testing::Test opt_expect_throws_on_empty()
{
const std::optional<int> o;
bool threw = false;
try { [[maybe_unused]] auto v = expect(o, "must have value"); } catch (...) { threw = true; }
Void::Testing::expect(threw);
}
Void::Testing::Test opt_zip_both_present()
{
const std::optional<int> a{1};
const std::optional<std::string> b{"hello"};
const auto r = zip(a, b);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(r->first, 1);
Void::Testing::expect_eq(r->second, std::string{"hello"});
}
Void::Testing::Test opt_zip_one_empty()
{
const std::optional<int> a{1};
const std::optional<int> b;
const auto r = zip(a, b);
Void::Testing::expect(!r.has_value());
}
Void::Testing::Test opt_flatten()
{
const std::optional<std::optional<int>> o{std::optional<int>{7}};
const auto r = flatten(o);
Void::Testing::expect(r.has_value());
Void::Testing::expect_eq(*r, 7);
}
Void::Testing::Test opt_flatten_inner_empty()
{
const std::optional<std::optional<int>> o{std::optional<int>{}};
Void::Testing::expect(!flatten(o).has_value());
}
Void::Testing::Test opt_or_else_calls_on_empty()
{
const std::optional<int> o;
const auto r = or_else(o, [] { return std::optional<int>{99}; });
Void::Testing::expect_eq(*r, 99);
}
Void::Testing::Test opt_or_else_skips_on_present()
{
const std::optional<int> o{5};
bool called = false;
const auto r = or_else(o, [&] { called = true; return std::optional<int>{99}; });
Void::Testing::expect(!called);
Void::Testing::expect_eq(*r, 5);
}
// ── Opt<T> fluent wrapper ─────────────────────────────────────────────────────
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()
{
const auto r = some(10).filter([](int x) { return x > 5; });
Void::Testing::expect(r.has_value());
const auto r2 = some(2).filter([](int x) { return x > 5; });
Void::Testing::expect(!r2.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);
}