Completes the in-progress merges (Optional->Either/Opt, Curry+Memo->Pipeline,
ScopeGuard->Scope, String->Str):
- Pipeline/Memo.hpp now carries the full Memo API (memoize with recursion
support, TupleHash, SimpleCache) alongside memoize_fn; old Memo module
and its test dir removed.
- Duplicate Channel module removed; Concurrency::Channel gains empty() and
the unique test cases (drain-after-close, multi-producer, string payloads).
- StopWatch module merged into Time::Stopwatch: one class with explicit
start/stop/laps plus a started() factory for the auto-start RAII style;
ScopedTimer moved along.
- Testing optional dep retargeted String->Str (pad_right lives in Str now).
- Stale empty dirs removed: src/{ScopeGuard,String,Core}, include/Void/{Core,
Filesystem,Memo,Channel,StopWatch}.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
152 lines
4.4 KiB
C++
152 lines
4.4 KiB
C++
#include <Void/Pipeline/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());
|
|
}
|