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

152 lines
4.3 KiB
C++

#include <Void/Memo/Memo.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <tuple>
using namespace Void::Memo;
// ── 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());
}