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>
134 lines
3.8 KiB
C++
134 lines
3.8 KiB
C++
#include <Void/Containers/LRU.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <stdexcept>
|
|
#include <string>
|
|
|
|
using Cache = Void::LRU::LruCache<int, std::string>;
|
|
|
|
// ── Basic ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test lru_zero_capacity_throws()
|
|
{
|
|
bool threw = false;
|
|
try { Void::LRU::LruCache<int,int>{0}; }
|
|
catch (const std::invalid_argument&) { threw = true; }
|
|
Void::Testing::expect(threw);
|
|
}
|
|
|
|
Void::Testing::Test lru_put_and_get()
|
|
{
|
|
Cache c{4};
|
|
c.put(1, "one");
|
|
const auto v = c.get(1);
|
|
Void::Testing::expect(v.has_value());
|
|
Void::Testing::expect_eq(*v, std::string{"one"});
|
|
}
|
|
|
|
Void::Testing::Test lru_get_miss_returns_nullopt()
|
|
{
|
|
Cache c{4};
|
|
Void::Testing::expect(!c.get(99).has_value());
|
|
}
|
|
|
|
Void::Testing::Test lru_contains()
|
|
{
|
|
Cache c{4};
|
|
c.put(1, "a");
|
|
Void::Testing::expect(c.contains(1));
|
|
Void::Testing::expect(!c.contains(2));
|
|
}
|
|
|
|
Void::Testing::Test lru_size_and_empty()
|
|
{
|
|
Cache c{4};
|
|
Void::Testing::expect(c.empty());
|
|
c.put(1, "a");
|
|
Void::Testing::expect_eq(c.size(), std::size_t{1});
|
|
}
|
|
|
|
// ── Eviction ─────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test lru_evicts_lru_when_full()
|
|
{
|
|
Cache c{3};
|
|
c.put(1, "a");
|
|
c.put(2, "b");
|
|
c.put(3, "c");
|
|
c.put(4, "d"); // evicts 1 (LRU)
|
|
Void::Testing::expect(!c.contains(1));
|
|
Void::Testing::expect(c.contains(4));
|
|
}
|
|
|
|
Void::Testing::Test lru_get_promotes_entry()
|
|
{
|
|
Cache c{3};
|
|
c.put(1, "a");
|
|
c.put(2, "b");
|
|
c.put(3, "c");
|
|
c.get(1); // promote 1 to MRU
|
|
c.put(4, "d"); // evicts 2 (now LRU)
|
|
Void::Testing::expect(c.contains(1));
|
|
Void::Testing::expect(!c.contains(2));
|
|
}
|
|
|
|
Void::Testing::Test lru_update_existing_key()
|
|
{
|
|
Cache c{4};
|
|
c.put(1, "old");
|
|
c.put(1, "new");
|
|
Void::Testing::expect_eq(c.size(), std::size_t{1});
|
|
Void::Testing::expect_eq(*c.get(1), std::string{"new"});
|
|
}
|
|
|
|
// ── Erase ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test lru_erase_existing()
|
|
{
|
|
Cache c{4};
|
|
c.put(1, "a");
|
|
Void::Testing::expect(c.erase(1));
|
|
Void::Testing::expect(!c.contains(1));
|
|
}
|
|
|
|
Void::Testing::Test lru_erase_missing_returns_false()
|
|
{
|
|
Cache c{4};
|
|
Void::Testing::expect(!c.erase(99));
|
|
}
|
|
|
|
// ── Peek ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test lru_peek_does_not_promote()
|
|
{
|
|
Cache c{3};
|
|
c.put(1, "a");
|
|
c.put(2, "b");
|
|
c.put(3, "c");
|
|
c.peek(1); // peek 1 but do NOT promote
|
|
c.put(4, "d"); // evicts 1 (still LRU despite peek)
|
|
Void::Testing::expect(!c.contains(1));
|
|
}
|
|
|
|
// ── Clear ────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test lru_clear()
|
|
{
|
|
Cache c{4};
|
|
c.put(1, "a");
|
|
c.put(2, "b");
|
|
c.clear();
|
|
Void::Testing::expect(c.empty());
|
|
Void::Testing::expect(!c.contains(1));
|
|
}
|
|
|
|
Void::Testing::Test lru_full_flag()
|
|
{
|
|
Cache c{2};
|
|
c.put(1, "a");
|
|
Void::Testing::expect(!c.full());
|
|
c.put(2, "b");
|
|
Void::Testing::expect(c.full());
|
|
}
|