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>
92 lines
2.4 KiB
C++
92 lines
2.4 KiB
C++
#include <Void/DisjointSet/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));
|
|
}
|