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>
181 lines
6.2 KiB
C++
181 lines
6.2 KiB
C++
#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);
|
|
}
|