Restructure into umbrella modules (~55 -> 16 targets)
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>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,193 @@
|
||||
#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());
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
#include <Void/Functional/Memo.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
using namespace Void::Pipeline;
|
||||
|
||||
// ── Memoized ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test memo_fibonacci_correctness()
|
||||
{
|
||||
auto fib = memoize([](auto& self, int n) -> int {
|
||||
if (n < 2) return n;
|
||||
return self(n - 1) + self(n - 2);
|
||||
});
|
||||
Void::Testing::expect_eq(fib(0), 0);
|
||||
Void::Testing::expect_eq(fib(1), 1);
|
||||
Void::Testing::expect_eq(fib(10), 55);
|
||||
Void::Testing::expect_eq(fib(20), 6765);
|
||||
}
|
||||
|
||||
Void::Testing::Test memo_caches_result()
|
||||
{
|
||||
int call_count = 0;
|
||||
auto fn = memoize([&call_count](auto& /*self*/, int n) -> int {
|
||||
++call_count;
|
||||
return n * 2;
|
||||
});
|
||||
fn(5);
|
||||
fn(5);
|
||||
fn(5);
|
||||
Void::Testing::expect_eq(call_count, 1);
|
||||
fn(6);
|
||||
Void::Testing::expect_eq(call_count, 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test memo_different_args_separate_cache()
|
||||
{
|
||||
int call_count = 0;
|
||||
auto fn = memoize([&call_count](auto& /*self*/, int n) -> int {
|
||||
++call_count;
|
||||
return n * n;
|
||||
});
|
||||
fn(3);
|
||||
fn(4);
|
||||
fn(3);
|
||||
Void::Testing::expect_eq(call_count, 2);
|
||||
Void::Testing::expect_eq(fn(3), 9);
|
||||
Void::Testing::expect_eq(fn(4), 16);
|
||||
}
|
||||
|
||||
Void::Testing::Test memo_multi_arg()
|
||||
{
|
||||
auto add = memoize([](auto& /*self*/, int a, int b) -> int {
|
||||
return a + b;
|
||||
});
|
||||
Void::Testing::expect_eq(add(1, 2), 3);
|
||||
Void::Testing::expect_eq(add(10, 20), 30);
|
||||
Void::Testing::expect_eq(add(1, 2), 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test memo_string_key()
|
||||
{
|
||||
auto upper = memoize([](auto& /*self*/, std::string s) -> std::string {
|
||||
for (auto& c : s) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
|
||||
return s;
|
||||
});
|
||||
Void::Testing::expect_eq(upper(std::string{"hello"}), std::string{"HELLO"});
|
||||
Void::Testing::expect_eq(upper(std::string{"world"}), std::string{"WORLD"});
|
||||
}
|
||||
|
||||
Void::Testing::Test memo_clear_recomputes()
|
||||
{
|
||||
int call_count = 0;
|
||||
auto fn = memoize([&call_count](auto& /*self*/, int n) -> int {
|
||||
++call_count;
|
||||
return n;
|
||||
});
|
||||
fn(7);
|
||||
Void::Testing::expect_eq(call_count, 1);
|
||||
fn.clear<int>();
|
||||
fn(7);
|
||||
Void::Testing::expect_eq(call_count, 2);
|
||||
}
|
||||
|
||||
// ── SimpleCache ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test simple_cache_get_or_computes_once()
|
||||
{
|
||||
SimpleCache<int, int> cache;
|
||||
int call_count = 0;
|
||||
std::ignore = cache.get_or(1, [&](int k) { ++call_count; return k * 10; });
|
||||
std::ignore = cache.get_or(1, [&](int k) { ++call_count; return k * 10; });
|
||||
Void::Testing::expect_eq(call_count, 1);
|
||||
Void::Testing::expect_eq(*cache.get(1), 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_get_missing_returns_nullopt()
|
||||
{
|
||||
SimpleCache<std::string, int> cache;
|
||||
Void::Testing::expect(!cache.get("missing").has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_put_and_get()
|
||||
{
|
||||
SimpleCache<int, std::string> cache;
|
||||
cache.put(42, std::string{"answer"});
|
||||
const auto v = cache.get(42);
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, std::string{"answer"});
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_put_overwrites()
|
||||
{
|
||||
SimpleCache<int, int> cache;
|
||||
cache.put(1, 10);
|
||||
cache.put(1, 20);
|
||||
Void::Testing::expect_eq(*cache.get(1), 20);
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_erase()
|
||||
{
|
||||
SimpleCache<int, int> cache;
|
||||
cache.put(1, 100);
|
||||
const bool removed = cache.erase(1);
|
||||
Void::Testing::expect(removed);
|
||||
Void::Testing::expect(!cache.get(1).has_value());
|
||||
Void::Testing::expect(!cache.erase(1)); // second erase is false
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_size_and_empty()
|
||||
{
|
||||
SimpleCache<int, int> cache;
|
||||
Void::Testing::expect(cache.empty());
|
||||
Void::Testing::expect_eq(cache.size(), std::size_t{0});
|
||||
cache.put(1, 1);
|
||||
cache.put(2, 2);
|
||||
Void::Testing::expect_eq(cache.size(), std::size_t{2});
|
||||
Void::Testing::expect(!cache.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test simple_cache_clear()
|
||||
{
|
||||
SimpleCache<int, int> cache;
|
||||
cache.put(1, 1);
|
||||
cache.put(2, 2);
|
||||
cache.clear();
|
||||
Void::Testing::expect(cache.empty());
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
#include <Void/Functional/Curry.hpp>
|
||||
#include <Void/Functional/Memo.hpp>
|
||||
#include <Void/Functional/Pipeline.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace Void::Pipeline;
|
||||
|
||||
// ── pipe ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pipe_single_step()
|
||||
{
|
||||
const int result = pipe(5) | [](int x) { return x * 2; };
|
||||
Void::Testing::expect_eq(result, 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test pipe_multi_step()
|
||||
{
|
||||
const int result = pipe(3)
|
||||
| [](int x) { return x + 1; } // 4
|
||||
| [](int x) { return x * 3; } // 12
|
||||
| [](int x) { return x - 2; }; // 10
|
||||
Void::Testing::expect_eq(result, 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test pipe_type_change()
|
||||
{
|
||||
const std::string result = pipe(42)
|
||||
| [](int x) { return std::to_string(x); }
|
||||
| [](std::string s) { return "[" + s + "]"; };
|
||||
Void::Testing::expect_eq(result, std::string{"[42]"});
|
||||
}
|
||||
|
||||
Void::Testing::Test pipe_implicit_conversion()
|
||||
{
|
||||
int v = pipe(7) | [](int x) { return x + 1; };
|
||||
Void::Testing::expect_eq(v, 8);
|
||||
}
|
||||
|
||||
// ── compose ───────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test compose_two()
|
||||
{
|
||||
auto fn = compose([](int x) { return x + 1; },
|
||||
[](int x) { return x * 2; });
|
||||
Void::Testing::expect_eq(fn(3), 8); // (3+1)*2
|
||||
}
|
||||
|
||||
Void::Testing::Test compose_three()
|
||||
{
|
||||
auto fn = compose([](int x) { return x + 1; },
|
||||
[](int x) { return x * 2; },
|
||||
[](int x) { return x - 1; });
|
||||
Void::Testing::expect_eq(fn(3), 7); // ((3+1)*2)-1
|
||||
}
|
||||
|
||||
Void::Testing::Test compose_type_change()
|
||||
{
|
||||
auto fn = compose([](int x) { return std::to_string(x); },
|
||||
[](const std::string& s) { return s.size(); });
|
||||
Void::Testing::expect_eq(fn(12345), std::size_t{5});
|
||||
}
|
||||
|
||||
// ── partial ───────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test partial_one_arg()
|
||||
{
|
||||
auto add = [](int a, int b) { return a + b; };
|
||||
auto add5 = partial(add, 5);
|
||||
Void::Testing::expect_eq(add5(3), 8);
|
||||
Void::Testing::expect_eq(add5(10), 15);
|
||||
}
|
||||
|
||||
Void::Testing::Test partial_two_args()
|
||||
{
|
||||
auto sum3 = [](int a, int b, int c) { return a + b + c; };
|
||||
auto fixed = partial(sum3, 1, 2);
|
||||
Void::Testing::expect_eq(fixed(10), 13);
|
||||
}
|
||||
|
||||
// ── flip ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test flip_reverses_args()
|
||||
{
|
||||
auto sub = [](int a, int b) { return a - b; };
|
||||
auto flipped = flip(sub);
|
||||
Void::Testing::expect_eq(flipped(3, 10), 7); // sub(10, 3)
|
||||
}
|
||||
|
||||
// ── identity / constant ───────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test identity_returns_value()
|
||||
{
|
||||
Void::Testing::expect_eq(identity(42), 42);
|
||||
Void::Testing::expect_eq(identity(std::string{"x"}), std::string{"x"});
|
||||
}
|
||||
|
||||
Void::Testing::Test constant_ignores_args()
|
||||
{
|
||||
auto always5 = constant(5);
|
||||
Void::Testing::expect_eq(always5(), 5);
|
||||
Void::Testing::expect_eq(always5(1, 2, 3), 5);
|
||||
}
|
||||
|
||||
// ── negate ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test negate_predicate()
|
||||
{
|
||||
auto is_positive = [](int x) { return x > 0; };
|
||||
auto is_non_positive = negate(is_positive);
|
||||
Void::Testing::expect(!is_non_positive(5));
|
||||
Void::Testing::expect(is_non_positive(-3));
|
||||
Void::Testing::expect(is_non_positive(0));
|
||||
}
|
||||
|
||||
// ── curry ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test curry_binary_full_apply()
|
||||
{
|
||||
auto add = curry([](int a, int b) { return a + b; });
|
||||
Void::Testing::expect_eq(add(3)(4), 7);
|
||||
}
|
||||
|
||||
Void::Testing::Test curry_binary_partial_then_complete()
|
||||
{
|
||||
auto add = curry([](int a, int b) { return a + b; });
|
||||
auto add10 = add(10);
|
||||
Void::Testing::expect_eq(add10(5), 15);
|
||||
Void::Testing::expect_eq(add10(20), 30);
|
||||
}
|
||||
|
||||
Void::Testing::Test curry_ternary()
|
||||
{
|
||||
auto sum3 = curry([](int a, int b, int c) { return a + b + c; });
|
||||
Void::Testing::expect_eq(sum3(1)(2)(3), 6);
|
||||
}
|
||||
|
||||
Void::Testing::Test curry_type_change()
|
||||
{
|
||||
auto prefix = curry([](std::string p, std::string s) { return p + s; });
|
||||
const auto add_hello = prefix(std::string{"hello "});
|
||||
Void::Testing::expect_eq(add_hello(std::string{"world"}), std::string{"hello world"});
|
||||
}
|
||||
|
||||
Void::Testing::Test curry_unary_invokes_immediately()
|
||||
{
|
||||
auto square = curry([](int x) { return x * x; });
|
||||
Void::Testing::expect_eq(square(5), 25);
|
||||
}
|
||||
|
||||
Void::Testing::Test curry_reuse_partial()
|
||||
{
|
||||
auto mul = curry([](int a, int b) { return a * b; });
|
||||
const auto triple = mul(3);
|
||||
Void::Testing::expect_eq(triple(4), 12);
|
||||
Void::Testing::expect_eq(triple(5), 15);
|
||||
}
|
||||
|
||||
// ── memoize_fn ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test memoize_fn_caches_result()
|
||||
{
|
||||
int call_count = 0;
|
||||
auto fn = memoize_fn<int>([&](int x) { ++call_count; return x * x; });
|
||||
fn(5);
|
||||
fn(5);
|
||||
fn(5);
|
||||
Void::Testing::expect_eq(call_count, 1);
|
||||
fn(6);
|
||||
Void::Testing::expect_eq(call_count, 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test memoize_fn_correct_result()
|
||||
{
|
||||
auto sq = memoize_fn<int>([](int x) { return x * x; });
|
||||
Void::Testing::expect_eq(sq(4), 16);
|
||||
Void::Testing::expect_eq(sq(7), 49);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
Void::Result::Result<int> divide(int n, int d)
|
||||
{
|
||||
if (d == 0) {
|
||||
return Void::Result::fail("division by zero");
|
||||
}
|
||||
return Void::Result::ok(n / d);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test ok_constructs_success()
|
||||
{
|
||||
auto r = divide(10, 2);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 5);
|
||||
}
|
||||
|
||||
Void::Testing::Test fail_constructs_failure()
|
||||
{
|
||||
auto r = divide(10, 0);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(r.error().message(),
|
||||
std::string{"division by zero"});
|
||||
}
|
||||
|
||||
Void::Testing::Test value_or_returns_fallback_on_error()
|
||||
{
|
||||
auto r = divide(10, 0);
|
||||
Void::Testing::expect_eq(r.value_or(-1), -1);
|
||||
}
|
||||
|
||||
Void::Testing::Test bool_conversion()
|
||||
{
|
||||
Void::Testing::expect(static_cast<bool>(divide(10, 2)));
|
||||
Void::Testing::expect(!static_cast<bool>(divide(10, 0)));
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
Void::Result::Result<int> read_file()
|
||||
{
|
||||
return Void::Result::fail("disk read error");
|
||||
}
|
||||
|
||||
Void::Result::Result<int> parse_config()
|
||||
{
|
||||
return Void::Result::with_context(read_file(), "parsing config.toml");
|
||||
}
|
||||
|
||||
Void::Result::Result<int> load()
|
||||
{
|
||||
return Void::Result::with_context(parse_config(), "starting up");
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test context_chain_preserves_root_message()
|
||||
{
|
||||
auto r = load();
|
||||
Void::Testing::expect(!r.has_value());
|
||||
const auto& e = r.error();
|
||||
Void::Testing::expect_eq(e.message(), std::string{"starting up"});
|
||||
Void::Testing::expect(e.cause() != nullptr);
|
||||
Void::Testing::expect_eq(e.cause()->message(),
|
||||
std::string{"parsing config.toml"});
|
||||
Void::Testing::expect_eq(e.cause()->cause()->message(),
|
||||
std::string{"disk read error"});
|
||||
}
|
||||
|
||||
Void::Testing::Test format_renders_full_chain()
|
||||
{
|
||||
auto r = load();
|
||||
const auto rendered = r.error().format();
|
||||
Void::Testing::expect(rendered.find("starting up") != std::string::npos);
|
||||
Void::Testing::expect(rendered.find("parsing config.toml") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(rendered.find("disk read error") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(rendered.find("caused by:") != std::string::npos);
|
||||
}
|
||||
|
||||
Void::Testing::Test with_context_no_op_on_success()
|
||||
{
|
||||
auto r = Void::Result::with_context(Void::Result::ok(42), "unused context");
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 42);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#include <Void/Functional/Error.hpp>
|
||||
#include <Void/Functional/Result.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
Void::Result::Result<int> parse_int(const std::string& s)
|
||||
{
|
||||
try {
|
||||
return Void::Result::ok(std::stoi(s));
|
||||
} catch (...) {
|
||||
return Void::Result::fail("not a number");
|
||||
}
|
||||
}
|
||||
|
||||
Void::Result::Result<int> doubled(int n) { return Void::Result::ok(n * 2); }
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test and_then_chains_on_success()
|
||||
{
|
||||
auto r = parse_int("21").and_then(doubled);
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test and_then_short_circuits_on_failure()
|
||||
{
|
||||
auto r = parse_int("xyz").and_then(doubled);
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(r.error().message(), std::string{"not a number"});
|
||||
}
|
||||
|
||||
Void::Testing::Test transform_maps_value()
|
||||
{
|
||||
auto r = parse_int("3").transform([](int n) { return n + 100; });
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 103);
|
||||
}
|
||||
|
||||
Void::Testing::Test or_else_recovers_on_failure()
|
||||
{
|
||||
auto r = parse_int("xyz").or_else(
|
||||
[](const Void::Result::Error&) { return Void::Result::ok(0); });
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 0);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <Void/Functional/Detail/Repr.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace
|
||||
{
|
||||
struct PlainStruct {
|
||||
int code;
|
||||
std::string label;
|
||||
};
|
||||
|
||||
struct DiagInfo {
|
||||
std::string file;
|
||||
int line;
|
||||
std::string detail;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test repr_uses_std_format_when_available()
|
||||
{
|
||||
const auto out = Void::Result::Detail::repr(42);
|
||||
Void::Testing::expect_eq(out, std::string{"42"});
|
||||
}
|
||||
|
||||
Void::Testing::Test repr_uses_std_format_for_string()
|
||||
{
|
||||
const auto out = Void::Result::Detail::repr(std::string{"hello"});
|
||||
Void::Testing::expect_eq(out, std::string{"hello"});
|
||||
}
|
||||
|
||||
#ifdef VOID_RESULT_HAS_REFLECTION
|
||||
// @tag reflection
|
||||
Void::Testing::Test repr_walks_struct_fields_via_reflection()
|
||||
{
|
||||
PlainStruct s{42, "ok"};
|
||||
const auto out = Void::Result::Detail::repr(s);
|
||||
Void::Testing::expect(out.find("PlainStruct") != std::string::npos);
|
||||
Void::Testing::expect(out.find("code: 42") != std::string::npos);
|
||||
Void::Testing::expect(out.find("label: ok") != std::string::npos);
|
||||
}
|
||||
|
||||
// @tag reflection
|
||||
Void::Testing::Test repr_handles_multiple_fields()
|
||||
{
|
||||
DiagInfo d{"foo.cpp", 17, "bad token"};
|
||||
const auto out = Void::Result::Detail::repr(d);
|
||||
Void::Testing::expect(out.find("DiagInfo") != std::string::npos);
|
||||
Void::Testing::expect(out.find("file: foo.cpp") != std::string::npos);
|
||||
Void::Testing::expect(out.find("line: 17") != std::string::npos);
|
||||
Void::Testing::expect(out.find("detail: bad token") != std::string::npos);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,152 @@
|
||||
#include <Void/Functional/Retry.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
|
||||
using namespace Void::Retry;
|
||||
|
||||
// ── retry ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test retry_succeeds_on_first_attempt()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 3};
|
||||
int calls = 0;
|
||||
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
|
||||
++calls;
|
||||
return 42;
|
||||
});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 42);
|
||||
Void::Testing::expect_eq(calls, 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test retry_succeeds_on_second_attempt()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 3};
|
||||
int calls = 0;
|
||||
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
|
||||
if (++calls < 2) return std::unexpected{"not yet"};
|
||||
return 7;
|
||||
});
|
||||
Void::Testing::expect(r.has_value());
|
||||
Void::Testing::expect_eq(*r, 7);
|
||||
Void::Testing::expect_eq(calls, 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test retry_exhausts_all_attempts()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 4};
|
||||
int calls = 0;
|
||||
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
|
||||
++calls;
|
||||
return std::unexpected{"always fails"};
|
||||
});
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(calls, 4);
|
||||
}
|
||||
|
||||
Void::Testing::Test retry_returns_last_error()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 3};
|
||||
int n = 0;
|
||||
const auto r = retry(p, [&]() -> std::expected<int, std::string> {
|
||||
return std::unexpected{std::to_string(++n)};
|
||||
});
|
||||
Void::Testing::expect(!r.has_value());
|
||||
Void::Testing::expect_eq(r.error(), std::string{"3"});
|
||||
}
|
||||
|
||||
// ── retry_or_throw ────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test retry_or_throw_returns_value()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 2};
|
||||
const auto v = retry_or_throw(p, []() -> std::expected<int, std::string> {
|
||||
return 99;
|
||||
});
|
||||
Void::Testing::expect_eq(v, 99);
|
||||
}
|
||||
|
||||
Void::Testing::Test retry_or_throw_throws_on_failure()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 2};
|
||||
bool threw = false;
|
||||
try {
|
||||
retry_or_throw(p, []() -> std::expected<int, std::string> {
|
||||
return std::unexpected{"err"};
|
||||
});
|
||||
} catch (const std::runtime_error&) {
|
||||
threw = true;
|
||||
}
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
// ── retry_while ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test retry_while_stops_on_success_code()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 5};
|
||||
int calls = 0;
|
||||
const auto r = retry_while(
|
||||
p,
|
||||
[&]() -> int { return ++calls < 3 ? 503 : 200; },
|
||||
[](int code) { return code != 200; });
|
||||
Void::Testing::expect_eq(r, 200);
|
||||
Void::Testing::expect_eq(calls, 3);
|
||||
}
|
||||
|
||||
Void::Testing::Test retry_while_exhausted_returns_last()
|
||||
{
|
||||
const Policy<NoDelay> p{.max_attempts = 3};
|
||||
const auto r = retry_while(
|
||||
p,
|
||||
[]() -> int { return 503; },
|
||||
[](int code) { return code != 200; });
|
||||
Void::Testing::expect_eq(r, 503);
|
||||
}
|
||||
|
||||
// ── backoff policies (logic, no actual sleep) ─────────────────────────────────
|
||||
|
||||
Void::Testing::Test nodelay_always_zero()
|
||||
{
|
||||
const NoDelay b;
|
||||
Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{0});
|
||||
Void::Testing::expect_eq(b.delay(10).count(), std::int64_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test constant_delay_fixed()
|
||||
{
|
||||
const ConstantDelay b{std::chrono::milliseconds{250}};
|
||||
Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{250});
|
||||
Void::Testing::expect_eq(b.delay(5).count(), std::int64_t{250});
|
||||
}
|
||||
|
||||
Void::Testing::Test linear_backoff_grows()
|
||||
{
|
||||
const LinearBackoff b{std::chrono::milliseconds{100}, std::chrono::milliseconds{10'000}};
|
||||
Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{100});
|
||||
Void::Testing::expect_eq(b.delay(1).count(), std::int64_t{200});
|
||||
Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{300});
|
||||
}
|
||||
|
||||
Void::Testing::Test linear_backoff_clamped()
|
||||
{
|
||||
const LinearBackoff b{std::chrono::milliseconds{4000}, std::chrono::milliseconds{5000}};
|
||||
Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{5000});
|
||||
}
|
||||
|
||||
Void::Testing::Test exponential_backoff_grows()
|
||||
{
|
||||
const ExponentialBackoff b{std::chrono::milliseconds{100}, std::chrono::milliseconds{30'000}, 2.0};
|
||||
Void::Testing::expect_eq(b.delay(0).count(), std::int64_t{100});
|
||||
Void::Testing::expect_eq(b.delay(1).count(), std::int64_t{200});
|
||||
Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{400});
|
||||
}
|
||||
|
||||
Void::Testing::Test exponential_backoff_clamped()
|
||||
{
|
||||
const ExponentialBackoff b{std::chrono::milliseconds{10'000}, std::chrono::milliseconds{15'000}, 2.0};
|
||||
Void::Testing::expect_eq(b.delay(2).count(), std::int64_t{15000});
|
||||
}
|
||||
Reference in new issue
Block a user