Restructure into umbrella modules (~55 -> 16 targets)
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>
This commit is contained in:
1 parent
da2e93c405
commit
28b94883fd
156 files changed
+278
-220
No files matched your search
@@ -0,0 +1,198 @@
|
||||
#include <Void/Containers/Bitset.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
using namespace Void::Bitset;
|
||||
|
||||
// ── basic operations ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_default_all_clear()
|
||||
{
|
||||
const Bitset<8> b;
|
||||
Void::Testing::expect(b.none());
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{0});
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_set_and_test()
|
||||
{
|
||||
Bitset<16> b;
|
||||
b.set(0);
|
||||
b.set(7);
|
||||
b.set(15);
|
||||
Void::Testing::expect(b.test(0));
|
||||
Void::Testing::expect(b.test(7));
|
||||
Void::Testing::expect(b.test(15));
|
||||
Void::Testing::expect(!b.test(1));
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{3});
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_reset_clears_bit()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(3);
|
||||
Void::Testing::expect(b.test(3));
|
||||
b.reset(3);
|
||||
Void::Testing::expect(!b.test(3));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_flip()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(5);
|
||||
b.flip(5);
|
||||
Void::Testing::expect(!b.test(5));
|
||||
b.flip(5);
|
||||
Void::Testing::expect(b.test(5));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_set_all_and_reset_all()
|
||||
{
|
||||
Bitset<64> b;
|
||||
b.set_all();
|
||||
Void::Testing::expect(b.all());
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{64});
|
||||
b.reset_all();
|
||||
Void::Testing::expect(b.none());
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_flip_all()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(0); b.set(2); b.set(4);
|
||||
b.flip_all();
|
||||
Void::Testing::expect(!b.test(0));
|
||||
Void::Testing::expect(b.test(1));
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_out_of_range_throws()
|
||||
{
|
||||
Bitset<8> b;
|
||||
bool threw = false;
|
||||
try { b.set(8); } catch (...) { threw = true; }
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
// ── multi-word (N > 64) ───────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_large_set_and_test()
|
||||
{
|
||||
Bitset<128> b;
|
||||
b.set(63);
|
||||
b.set(64);
|
||||
b.set(127);
|
||||
Void::Testing::expect(b.test(63));
|
||||
Void::Testing::expect(b.test(64));
|
||||
Void::Testing::expect(b.test(127));
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{3});
|
||||
}
|
||||
|
||||
// ── bitwise ops ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_and()
|
||||
{
|
||||
Bitset<8> a, b;
|
||||
a.set(0); a.set(1);
|
||||
b.set(1); b.set(2);
|
||||
const auto c = a & b;
|
||||
Void::Testing::expect(!c.test(0));
|
||||
Void::Testing::expect(c.test(1));
|
||||
Void::Testing::expect(!c.test(2));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_or()
|
||||
{
|
||||
Bitset<8> a, b;
|
||||
a.set(0); b.set(1);
|
||||
const auto c = a | b;
|
||||
Void::Testing::expect(c.test(0));
|
||||
Void::Testing::expect(c.test(1));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_xor()
|
||||
{
|
||||
Bitset<8> a, b;
|
||||
a.set(0); a.set(1);
|
||||
b.set(1); b.set(2);
|
||||
const auto c = a ^ b;
|
||||
Void::Testing::expect(c.test(0));
|
||||
Void::Testing::expect(!c.test(1));
|
||||
Void::Testing::expect(c.test(2));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_not()
|
||||
{
|
||||
Bitset<8> a;
|
||||
a.set(0);
|
||||
const auto b = ~a;
|
||||
Void::Testing::expect(!b.test(0));
|
||||
Void::Testing::expect(b.test(1));
|
||||
Void::Testing::expect_eq(b.count(), std::size_t{7});
|
||||
}
|
||||
|
||||
// ── shift ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_shift_left()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(0);
|
||||
const auto s = b << 3;
|
||||
Void::Testing::expect(!s.test(0));
|
||||
Void::Testing::expect(s.test(3));
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_shift_right()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(5);
|
||||
const auto s = b >> 2;
|
||||
Void::Testing::expect(!s.test(5));
|
||||
Void::Testing::expect(s.test(3));
|
||||
}
|
||||
|
||||
// ── to_string ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_to_string()
|
||||
{
|
||||
Bitset<8> b;
|
||||
b.set(0); // LSB → rightmost in string
|
||||
const auto s = b.to_string();
|
||||
Void::Testing::expect_eq(s.size(), std::size_t{8});
|
||||
Void::Testing::expect_eq(s[7], '1'); // bit 0 is rightmost
|
||||
Void::Testing::expect_eq(s[0], '0');
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_to_string_all_set()
|
||||
{
|
||||
Bitset<4> b;
|
||||
b.set_all();
|
||||
Void::Testing::expect_eq(b.to_string(), std::string{"1111"});
|
||||
}
|
||||
|
||||
// ── find_first ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_find_first_set()
|
||||
{
|
||||
Bitset<64> b;
|
||||
b.set(5);
|
||||
b.set(10);
|
||||
Void::Testing::expect_eq(b.find_first(), std::size_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test bitset_find_first_none()
|
||||
{
|
||||
const Bitset<64> b;
|
||||
Void::Testing::expect_eq(b.find_first(), std::size_t{64}); // sentinel
|
||||
}
|
||||
|
||||
// ── equality ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bitset_equality()
|
||||
{
|
||||
Bitset<8> a, b, c;
|
||||
a.set(3); b.set(3); c.set(4);
|
||||
Void::Testing::expect(a == b);
|
||||
Void::Testing::expect(a != c);
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
#include <Void/Containers/DisjointSet.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
using namespace Void::DisjointSet;
|
||||
|
||||
Void::Testing::Test disjoint_initial_state()
|
||||
{
|
||||
DisjointSet ds{5};
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{5});
|
||||
Void::Testing::expect_eq(ds.size(), std::size_t{5});
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_each_element_own_component()
|
||||
{
|
||||
DisjointSet ds{4};
|
||||
for (std::size_t i = 0; i < 4; ++i)
|
||||
for (std::size_t j = i + 1; j < 4; ++j)
|
||||
Void::Testing::expect(!ds.connected(i, j));
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_unite_reduces_count()
|
||||
{
|
||||
DisjointSet ds{5};
|
||||
ds.unite(0, 1);
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{4});
|
||||
ds.unite(1, 2);
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{3});
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_unite_already_connected_noop()
|
||||
{
|
||||
DisjointSet ds{3};
|
||||
ds.unite(0, 1);
|
||||
const bool merged = ds.unite(0, 1);
|
||||
Void::Testing::expect(!merged);
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_transitivity()
|
||||
{
|
||||
DisjointSet ds{5};
|
||||
ds.unite(0, 1);
|
||||
ds.unite(1, 2);
|
||||
Void::Testing::expect(ds.connected(0, 2));
|
||||
Void::Testing::expect(!ds.connected(0, 3));
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_unite_returns_true_first_time()
|
||||
{
|
||||
DisjointSet ds{4};
|
||||
Void::Testing::expect(ds.unite(2, 3));
|
||||
Void::Testing::expect(!ds.unite(2, 3));
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_full_merge_one_component()
|
||||
{
|
||||
DisjointSet ds{5};
|
||||
for (std::size_t i = 1; i < 5; ++i) ds.unite(0, i);
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{1});
|
||||
Void::Testing::expect(ds.connected(0, 4));
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_component_size()
|
||||
{
|
||||
DisjointSet ds{6};
|
||||
ds.unite(0, 1);
|
||||
ds.unite(1, 2);
|
||||
Void::Testing::expect_eq(ds.component_size(0), std::size_t{3});
|
||||
Void::Testing::expect_eq(ds.component_size(3), std::size_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_out_of_range_throws()
|
||||
{
|
||||
DisjointSet ds{3};
|
||||
bool threw = false;
|
||||
try { [[maybe_unused]] auto r = ds.find(10); } catch (...) { threw = true; }
|
||||
Void::Testing::expect(threw);
|
||||
}
|
||||
|
||||
Void::Testing::Test disjoint_graph_connectivity()
|
||||
{
|
||||
// Graph: 0-1, 2-3, 3-4 → two components {0,1} and {2,3,4}
|
||||
DisjointSet ds{5};
|
||||
ds.unite(0, 1);
|
||||
ds.unite(2, 3);
|
||||
ds.unite(3, 4);
|
||||
Void::Testing::expect_eq(ds.count(), std::size_t{2});
|
||||
Void::Testing::expect(ds.connected(2, 4));
|
||||
Void::Testing::expect(!ds.connected(1, 4));
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
#include <Void/Containers/Graph.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using G = Void::Graph::DiGraph<int>;
|
||||
|
||||
// ── Vertices ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test graph_add_vertex()
|
||||
{
|
||||
G g;
|
||||
g.add_vertex(1);
|
||||
Void::Testing::expect(g.has_vertex(1));
|
||||
Void::Testing::expect_eq(g.vertex_count(), std::size_t{1});
|
||||
}
|
||||
|
||||
Void::Testing::Test graph_add_edge_creates_vertices()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
Void::Testing::expect(g.has_vertex(1));
|
||||
Void::Testing::expect(g.has_vertex(2));
|
||||
}
|
||||
|
||||
Void::Testing::Test graph_has_edge()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
Void::Testing::expect(g.has_edge(1, 2));
|
||||
Void::Testing::expect(!g.has_edge(2, 1));
|
||||
}
|
||||
|
||||
Void::Testing::Test graph_edge_count()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(2, 3);
|
||||
Void::Testing::expect_eq(g.edge_count(), std::size_t{2});
|
||||
}
|
||||
|
||||
// ── BFS ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test bfs_visits_all_reachable()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(1, 3);
|
||||
g.add_edge(2, 4);
|
||||
auto order = g.bfs_order(1);
|
||||
Void::Testing::expect_eq(order.size(), std::size_t{4});
|
||||
Void::Testing::expect_eq(order[0], 1);
|
||||
}
|
||||
|
||||
Void::Testing::Test bfs_unreachable_vertex_not_visited()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_vertex(5);
|
||||
auto order = g.bfs_order(1);
|
||||
Void::Testing::expect_eq(order.size(), std::size_t{2});
|
||||
}
|
||||
|
||||
// ── DFS ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test dfs_visits_all_reachable()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(2, 3);
|
||||
auto order = g.dfs_order(1);
|
||||
Void::Testing::expect_eq(order.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(order[0], 1);
|
||||
}
|
||||
|
||||
// ── Topological sort ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test topo_sort_dag()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(1, 3);
|
||||
g.add_edge(2, 4);
|
||||
g.add_edge(3, 4);
|
||||
const auto order = g.topological_sort();
|
||||
Void::Testing::expect(order.has_value());
|
||||
Void::Testing::expect_eq(order->size(), std::size_t{4});
|
||||
// 1 must come before 2, 3; 2 and 3 before 4
|
||||
Void::Testing::expect_eq(order->front(), 1);
|
||||
Void::Testing::expect_eq(order->back(), 4);
|
||||
}
|
||||
|
||||
Void::Testing::Test topo_sort_cycle_returns_nullopt()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(2, 3);
|
||||
g.add_edge(3, 1); // cycle
|
||||
Void::Testing::expect(!g.topological_sort().has_value());
|
||||
}
|
||||
|
||||
// ── Reachability ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test can_reach_direct()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
Void::Testing::expect(g.can_reach(1, 2));
|
||||
}
|
||||
|
||||
Void::Testing::Test can_reach_indirect()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(2, 3);
|
||||
Void::Testing::expect(g.can_reach(1, 3));
|
||||
}
|
||||
|
||||
Void::Testing::Test cannot_reach_reverse()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
Void::Testing::expect(!g.can_reach(2, 1));
|
||||
}
|
||||
|
||||
// ── Shortest path ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test shortest_path_direct()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
const auto p = g.shortest_path(1, 2);
|
||||
Void::Testing::expect(p.has_value());
|
||||
Void::Testing::expect_eq(p->size(), std::size_t{2});
|
||||
Void::Testing::expect_eq((*p)[0], 1);
|
||||
Void::Testing::expect_eq((*p)[1], 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test shortest_path_longer()
|
||||
{
|
||||
G g;
|
||||
g.add_edge(1, 2);
|
||||
g.add_edge(2, 3);
|
||||
g.add_edge(1, 3); // shortcut
|
||||
const auto p = g.shortest_path(1, 3);
|
||||
Void::Testing::expect(p.has_value());
|
||||
// BFS finds shortest (length 2: 1→3)
|
||||
Void::Testing::expect_eq(p->size(), std::size_t{2});
|
||||
}
|
||||
|
||||
Void::Testing::Test shortest_path_no_path()
|
||||
{
|
||||
G g;
|
||||
g.add_vertex(1);
|
||||
g.add_vertex(2);
|
||||
Void::Testing::expect(!g.shortest_path(1, 2).has_value());
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#include <Void/Containers/Interval.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
using namespace Void::Interval;
|
||||
|
||||
// ── contains ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test interval_contains_midpoint()
|
||||
{
|
||||
const auto iv = make_interval(0, 10);
|
||||
Void::Testing::expect(iv.contains(5));
|
||||
Void::Testing::expect(iv.contains(0));
|
||||
Void::Testing::expect(!iv.contains(10));
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_contains_closed()
|
||||
{
|
||||
const auto iv = make_interval(0, 10);
|
||||
Void::Testing::expect(iv.contains_closed(10));
|
||||
Void::Testing::expect(!iv.contains_closed(11));
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_empty()
|
||||
{
|
||||
const auto iv = make_interval(5, 5);
|
||||
Void::Testing::expect(iv.empty());
|
||||
Void::Testing::expect(!make_interval(1, 2).empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_length()
|
||||
{
|
||||
const auto iv = make_interval(3, 8);
|
||||
Void::Testing::expect_eq(iv.length(), 5);
|
||||
}
|
||||
|
||||
// ── set operations ────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test interval_overlaps()
|
||||
{
|
||||
const auto a = make_interval(0, 5);
|
||||
const auto b = make_interval(3, 8);
|
||||
const auto c = make_interval(5, 9);
|
||||
Void::Testing::expect(a.overlaps(b));
|
||||
Void::Testing::expect(!a.overlaps(c));
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_adjacent()
|
||||
{
|
||||
const auto a = make_interval(0, 5);
|
||||
const auto b = make_interval(5, 10);
|
||||
Void::Testing::expect(a.adjacent(b));
|
||||
Void::Testing::expect(!a.adjacent(make_interval(6, 10)));
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_hull()
|
||||
{
|
||||
const auto a = make_interval(0, 5);
|
||||
const auto b = make_interval(3, 10);
|
||||
const auto h = a.hull(b);
|
||||
Void::Testing::expect_eq(h.lo, 0);
|
||||
Void::Testing::expect_eq(h.hi, 10);
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_intersection_overlap()
|
||||
{
|
||||
const auto a = make_interval(0, 8);
|
||||
const auto b = make_interval(4, 12);
|
||||
const auto i = a.intersection(b);
|
||||
Void::Testing::expect_eq(i.lo, 4);
|
||||
Void::Testing::expect_eq(i.hi, 8);
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_intersection_disjoint()
|
||||
{
|
||||
const auto a = make_interval(0, 3);
|
||||
const auto b = make_interval(5, 9);
|
||||
const auto i = a.intersection(b);
|
||||
Void::Testing::expect(i.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test interval_clamp_value()
|
||||
{
|
||||
const auto iv = make_interval(0, 10);
|
||||
Void::Testing::expect_eq(iv.clamp(-5), 0);
|
||||
Void::Testing::expect_eq(iv.clamp(5), 5);
|
||||
Void::Testing::expect_eq(iv.clamp(15), 10);
|
||||
}
|
||||
|
||||
// ── closed helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test interval_closed_helper()
|
||||
{
|
||||
const auto iv = closed(1, 5);
|
||||
Void::Testing::expect(iv.contains(1));
|
||||
Void::Testing::expect(iv.contains(5));
|
||||
Void::Testing::expect(!iv.contains(6));
|
||||
}
|
||||
|
||||
// ── equality ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test interval_equality()
|
||||
{
|
||||
const auto a = make_interval(0, 5);
|
||||
const auto b = make_interval(0, 5);
|
||||
const auto c = make_interval(0, 6);
|
||||
Void::Testing::expect(a == b);
|
||||
Void::Testing::expect(!(a == c));
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#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());
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#include <Void/Containers/Pool.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
|
||||
// ── ObjectPool ────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test object_pool_acquire_returns_valid_ptr()
|
||||
{
|
||||
Void::Pool::ObjectPool<int> pool;
|
||||
auto p = pool.acquire(42);
|
||||
Void::Testing::expect(p != nullptr);
|
||||
Void::Testing::expect_eq(*p, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test object_pool_release_on_ptr_destruction()
|
||||
{
|
||||
Void::Pool::ObjectPool<int, 4> pool;
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{0});
|
||||
{
|
||||
auto p = pool.acquire(1);
|
||||
// capacity grew to 4, 3 slots free
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
||||
}
|
||||
// Released back to pool
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test object_pool_reuses_memory()
|
||||
{
|
||||
Void::Pool::ObjectPool<int, 4> pool;
|
||||
int* raw1 = nullptr;
|
||||
{
|
||||
auto p = pool.acquire(1);
|
||||
raw1 = p.get();
|
||||
}
|
||||
auto p2 = pool.acquire(2);
|
||||
// After release the same slot should be reused (LIFO from free list)
|
||||
Void::Testing::expect(p2.get() == raw1);
|
||||
}
|
||||
|
||||
Void::Testing::Test object_pool_string_construction()
|
||||
{
|
||||
Void::Pool::ObjectPool<std::string> pool;
|
||||
auto p = pool.acquire("hello");
|
||||
Void::Testing::expect_eq(*p, std::string{"hello"});
|
||||
}
|
||||
|
||||
Void::Testing::Test object_pool_capacity_grows()
|
||||
{
|
||||
Void::Pool::ObjectPool<int, 4> pool;
|
||||
Void::Testing::expect_eq(pool.capacity(), std::size_t{0});
|
||||
auto p = pool.acquire(1);
|
||||
Void::Testing::expect_eq(pool.capacity(), std::size_t{4});
|
||||
}
|
||||
|
||||
// ── FixedPool ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test fixed_pool_capacity()
|
||||
{
|
||||
Void::Pool::FixedPool<int, 8> pool;
|
||||
static_assert(pool.capacity() == 8);
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{8});
|
||||
}
|
||||
|
||||
Void::Testing::Test fixed_pool_acquire_decrements_available()
|
||||
{
|
||||
Void::Pool::FixedPool<int, 4> pool;
|
||||
auto p = pool.acquire(99);
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
||||
Void::Testing::expect_eq(*p, 99);
|
||||
}
|
||||
|
||||
Void::Testing::Test fixed_pool_returns_null_when_full()
|
||||
{
|
||||
Void::Pool::FixedPool<int, 2> pool;
|
||||
auto p1 = pool.acquire(1);
|
||||
auto p2 = pool.acquire(2);
|
||||
auto p3 = pool.acquire(3); // should be nullptr
|
||||
Void::Testing::expect(p3 == nullptr);
|
||||
}
|
||||
|
||||
Void::Testing::Test fixed_pool_release_restores_available()
|
||||
{
|
||||
Void::Pool::FixedPool<int, 4> pool;
|
||||
{
|
||||
auto p = pool.acquire(1);
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{3});
|
||||
}
|
||||
Void::Testing::expect_eq(pool.available(), std::size_t{4});
|
||||
}
|
||||
|
||||
Void::Testing::Test fixed_pool_reuse_after_release()
|
||||
{
|
||||
Void::Pool::FixedPool<int, 4> pool;
|
||||
auto p1 = pool.acquire(1);
|
||||
auto p2 = pool.acquire(2);
|
||||
p1.reset();
|
||||
auto p3 = pool.acquire(3);
|
||||
Void::Testing::expect(p3 != nullptr);
|
||||
Void::Testing::expect_eq(*p3, 3);
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
#include <Void/Containers/PriorityQueue.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace Void::PriorityQueue;
|
||||
|
||||
// ── basic operations ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_push_pop_min_order()
|
||||
{
|
||||
PriorityQueue<std::string, int> pq;
|
||||
pq.push("c", 30);
|
||||
pq.push("a", 10);
|
||||
pq.push("b", 20);
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"a"});
|
||||
Void::Testing::expect_eq(pq.top_priority(), 10);
|
||||
Void::Testing::expect_eq(pq.pop(), std::string{"a"});
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"b"});
|
||||
Void::Testing::expect_eq(pq.pop(), std::string{"b"});
|
||||
Void::Testing::expect_eq(pq.pop(), std::string{"c"});
|
||||
Void::Testing::expect(pq.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_size_and_empty()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
Void::Testing::expect(pq.empty());
|
||||
Void::Testing::expect_eq(pq.size(), std::size_t{0});
|
||||
pq.push(1, 10);
|
||||
pq.push(2, 20);
|
||||
Void::Testing::expect_eq(pq.size(), std::size_t{2});
|
||||
Void::Testing::expect(!pq.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_contains()
|
||||
{
|
||||
PriorityQueue<std::string, int> pq;
|
||||
pq.push("x", 1);
|
||||
Void::Testing::expect(pq.contains("x"));
|
||||
Void::Testing::expect(!pq.contains("y"));
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_priority_of()
|
||||
{
|
||||
PriorityQueue<int, double> pq;
|
||||
pq.push(1, 3.14);
|
||||
const auto p = pq.priority_of(1);
|
||||
Void::Testing::expect(p.has_value());
|
||||
Void::Testing::expect(*p > 3.1 && *p < 3.2);
|
||||
Void::Testing::expect(!pq.priority_of(99).has_value());
|
||||
}
|
||||
|
||||
// ── update ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_update_decreases_priority()
|
||||
{
|
||||
PriorityQueue<std::string, int> pq;
|
||||
pq.push("a", 10);
|
||||
pq.push("b", 5);
|
||||
// Update "a" to have priority 1 (now minimum)
|
||||
pq.update("a", 1);
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"a"});
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_update_increases_priority()
|
||||
{
|
||||
PriorityQueue<std::string, int> pq;
|
||||
pq.push("a", 1);
|
||||
pq.push("b", 5);
|
||||
pq.update("a", 10); // "a" no longer minimum
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"b"});
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_push_existing_key_updates()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
pq.push(1, 10);
|
||||
pq.push(1, 2); // update key 1 to priority 2
|
||||
Void::Testing::expect_eq(pq.size(), std::size_t{1});
|
||||
Void::Testing::expect_eq(*pq.priority_of(1), 2);
|
||||
}
|
||||
|
||||
// ── erase ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_erase_top()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
pq.push(1, 5); pq.push(2, 10); pq.push(3, 15);
|
||||
pq.erase(1);
|
||||
Void::Testing::expect(!pq.contains(1));
|
||||
Void::Testing::expect_eq(pq.top_key(), 2);
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_erase_middle()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
pq.push(1, 5); pq.push(2, 10); pq.push(3, 15);
|
||||
pq.erase(2);
|
||||
Void::Testing::expect_eq(pq.size(), std::size_t{2});
|
||||
Void::Testing::expect(!pq.contains(2));
|
||||
}
|
||||
|
||||
Void::Testing::Test pq_erase_nonexistent_is_noop()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
pq.push(1, 5);
|
||||
pq.erase(99);
|
||||
Void::Testing::expect_eq(pq.size(), std::size_t{1});
|
||||
}
|
||||
|
||||
// ── clear ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_clear()
|
||||
{
|
||||
PriorityQueue<int, int> pq;
|
||||
pq.push(1, 1); pq.push(2, 2);
|
||||
pq.clear();
|
||||
Void::Testing::expect(pq.empty());
|
||||
}
|
||||
|
||||
// ── max-heap variant ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_max_heap()
|
||||
{
|
||||
MaxPriorityQueue<std::string, int> pq;
|
||||
pq.push("a", 1);
|
||||
pq.push("b", 100);
|
||||
pq.push("c", 50);
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"b"});
|
||||
Void::Testing::expect_eq(pq.pop(), std::string{"b"});
|
||||
Void::Testing::expect_eq(pq.top_key(), std::string{"c"});
|
||||
}
|
||||
|
||||
// ── dijkstra-style usage ──────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test pq_dijkstra_pattern()
|
||||
{
|
||||
// Shortest path from 0 in a simple graph: 0-1 weight 4, 0-2 weight 1, 2-1 weight 2
|
||||
PriorityQueue<int, int> pq;
|
||||
std::vector<int> dist(3, std::numeric_limits<int>::max());
|
||||
dist[0] = 0;
|
||||
pq.push(0, 0);
|
||||
|
||||
while (!pq.empty()) {
|
||||
const int u = pq.pop();
|
||||
if (u == 0) {
|
||||
if (4 < dist[1]) { dist[1] = 4; pq.push(1, 4); }
|
||||
if (1 < dist[2]) { dist[2] = 1; pq.push(2, 1); }
|
||||
} else if (u == 2) {
|
||||
if (dist[2] + 2 < dist[1]) { dist[1] = dist[2] + 2; pq.push(1, dist[1]); }
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::expect_eq(dist[1], 3); // via node 2: 1+2
|
||||
Void::Testing::expect_eq(dist[2], 1);
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
#include <Void/Containers/Ring.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// ── SpscRing ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test spsc_empty_on_construction()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 8> ring;
|
||||
Void::Testing::expect(ring.empty());
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_push_pop_single()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 8> ring;
|
||||
Void::Testing::expect(ring.push(42));
|
||||
const auto v = ring.pop();
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_pop_empty_returns_nullopt()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 8> ring;
|
||||
Void::Testing::expect(!ring.pop().has_value());
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_full_push_returns_false()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 4> ring; // capacity = 3
|
||||
Void::Testing::expect(ring.push(1));
|
||||
Void::Testing::expect(ring.push(2));
|
||||
Void::Testing::expect(ring.push(3));
|
||||
Void::Testing::expect(!ring.push(4)); // full
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_fifo_order()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 8> ring;
|
||||
for (int i = 0; i < 5; ++i) ring.push(i);
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
const auto v = ring.pop();
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, i);
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_capacity_is_n_minus_one()
|
||||
{
|
||||
Void::Testing::expect_eq(Void::Ring::SpscRing<int, 16>::capacity(),
|
||||
std::size_t{15});
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_full_flag()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 4> ring;
|
||||
ring.push(1); ring.push(2); ring.push(3);
|
||||
Void::Testing::expect(ring.full());
|
||||
}
|
||||
|
||||
Void::Testing::Test spsc_string_values()
|
||||
{
|
||||
Void::Ring::SpscRing<std::string, 4> ring;
|
||||
ring.push(std::string{"hello"});
|
||||
const auto v = ring.pop();
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, std::string{"hello"});
|
||||
}
|
||||
|
||||
// @tag threading
|
||||
Void::Testing::Test spsc_producer_consumer_threads()
|
||||
{
|
||||
Void::Ring::SpscRing<int, 64> ring;
|
||||
constexpr int N = 50;
|
||||
std::vector<int> received;
|
||||
received.reserve(N);
|
||||
|
||||
std::thread producer([&] {
|
||||
for (int i = 0; i < N; ++i) {
|
||||
while (!ring.push(i)) {}
|
||||
}
|
||||
});
|
||||
|
||||
std::thread consumer([&] {
|
||||
int got = 0;
|
||||
while (got < N) {
|
||||
auto v = ring.pop();
|
||||
if (v) { received.push_back(*v); ++got; }
|
||||
}
|
||||
});
|
||||
|
||||
producer.join();
|
||||
consumer.join();
|
||||
|
||||
Void::Testing::expect_eq(received.size(), std::size_t{N});
|
||||
for (int i = 0; i < N; ++i) {
|
||||
Void::Testing::expect_eq(received[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
// ── MpscRing ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test mpsc_push_pop_single()
|
||||
{
|
||||
Void::Ring::MpscRing<int, 8> ring;
|
||||
Void::Testing::expect(ring.push(99));
|
||||
const auto v = ring.pop();
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, 99);
|
||||
}
|
||||
|
||||
Void::Testing::Test mpsc_empty_on_construction()
|
||||
{
|
||||
Void::Ring::MpscRing<int, 8> ring;
|
||||
Void::Testing::expect(ring.empty());
|
||||
}
|
||||
|
||||
// @tag threading
|
||||
Void::Testing::Test mpsc_two_producers_one_consumer()
|
||||
{
|
||||
Void::Ring::MpscRing<int, 128> ring;
|
||||
constexpr int N = 40;
|
||||
std::atomic<int> total{0};
|
||||
|
||||
std::thread p1([&] {
|
||||
for (int i = 0; i < N; ++i) while (!ring.push(1)) {}
|
||||
});
|
||||
std::thread p2([&] {
|
||||
for (int i = 0; i < N; ++i) while (!ring.push(2)) {}
|
||||
});
|
||||
|
||||
std::thread consumer([&] {
|
||||
int got = 0;
|
||||
while (got < N * 2) {
|
||||
auto v = ring.pop();
|
||||
if (v) { total.fetch_add(*v, std::memory_order_relaxed); ++got; }
|
||||
}
|
||||
});
|
||||
|
||||
p1.join(); p2.join(); consumer.join();
|
||||
Void::Testing::expect_eq(total.load(), N * 3); // N×1 + N×2 = 3N
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
#include <Void/Containers/Trie.hpp>
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Test.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using BoolTrie = Void::Trie::Trie<bool>;
|
||||
using IntTrie = Void::Trie::Trie<int>;
|
||||
|
||||
// ── insert / contains ─────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_insert_and_contains()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(t.contains("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_does_not_contain_prefix_only()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(!t.contains("hell"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_does_not_contain_extension()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hell");
|
||||
Void::Testing::expect(!t.contains("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_insert_empty_key()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("");
|
||||
Void::Testing::expect(t.contains(""));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_multiple_words()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("car");
|
||||
t.insert("card");
|
||||
t.insert("care");
|
||||
Void::Testing::expect(t.contains("car"));
|
||||
Void::Testing::expect(t.contains("card"));
|
||||
Void::Testing::expect(t.contains("care"));
|
||||
Void::Testing::expect(!t.contains("ca"));
|
||||
}
|
||||
|
||||
// ── get ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_get_value()
|
||||
{
|
||||
IntTrie t;
|
||||
t.insert("key", 42);
|
||||
const auto v = t.get("key");
|
||||
Void::Testing::expect(v.has_value());
|
||||
Void::Testing::expect_eq(*v, 42);
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_get_missing_returns_nullopt()
|
||||
{
|
||||
IntTrie t;
|
||||
Void::Testing::expect(!t.get("nope").has_value());
|
||||
}
|
||||
|
||||
// ── starts_with ───────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_starts_with_true()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(t.starts_with("hel"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_starts_with_false()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(!t.starts_with("world"));
|
||||
}
|
||||
|
||||
// ── erase ────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_erase_removes_word()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(t.erase("hello"));
|
||||
Void::Testing::expect(!t.contains("hello"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_erase_prefix_word_keeps_extension()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("car");
|
||||
t.insert("card");
|
||||
t.erase("car");
|
||||
Void::Testing::expect(!t.contains("car"));
|
||||
Void::Testing::expect(t.contains("card"));
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_erase_missing_returns_false()
|
||||
{
|
||||
BoolTrie t;
|
||||
Void::Testing::expect(!t.erase("nope"));
|
||||
}
|
||||
|
||||
// ── keys_with_prefix ──────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_keys_with_prefix()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("car");
|
||||
t.insert("card");
|
||||
t.insert("care");
|
||||
t.insert("dog");
|
||||
auto keys = t.keys_with_prefix("car");
|
||||
std::sort(keys.begin(), keys.end());
|
||||
Void::Testing::expect_eq(keys.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(keys[0], std::string{"car"});
|
||||
Void::Testing::expect_eq(keys[1], std::string{"card"});
|
||||
Void::Testing::expect_eq(keys[2], std::string{"care"});
|
||||
}
|
||||
|
||||
Void::Testing::Test trie_keys_with_prefix_no_match()
|
||||
{
|
||||
BoolTrie t;
|
||||
t.insert("hello");
|
||||
Void::Testing::expect(t.keys_with_prefix("xyz").empty());
|
||||
}
|
||||
|
||||
// ── size ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
Void::Testing::Test trie_size_tracks_inserts()
|
||||
{
|
||||
BoolTrie t;
|
||||
Void::Testing::expect_eq(t.size(), std::size_t{0});
|
||||
t.insert("a");
|
||||
t.insert("b");
|
||||
Void::Testing::expect_eq(t.size(), std::size_t{2});
|
||||
}
|
||||
Reference in new issue
Block a user