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>
152 lines
4.4 KiB
C++
152 lines
4.4 KiB
C++
#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());
|
|
}
|