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>
150 lines
4.4 KiB
C++
150 lines
4.4 KiB
C++
#include <Void/Counted/Counted.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
using namespace Void::Counted;
|
|
|
|
// ── InstanceCounter ───────────────────────────────────────────────────────────
|
|
|
|
struct Widget : InstanceCounter<Widget> {
|
|
int value;
|
|
explicit Widget(int v) : value{v} {}
|
|
};
|
|
|
|
struct Gadget : InstanceCounter<Gadget> {};
|
|
|
|
Void::Testing::Test instance_counter_starts_at_zero()
|
|
{
|
|
// Run in isolation — Widget count may have been affected by other tests.
|
|
// Check relative behavior instead.
|
|
const std::size_t before = Widget::instance_count();
|
|
{
|
|
Widget w{1};
|
|
Void::Testing::expect_eq(Widget::instance_count(), before + 1);
|
|
}
|
|
Void::Testing::expect_eq(Widget::instance_count(), before);
|
|
}
|
|
|
|
Void::Testing::Test instance_counter_multiple_objects()
|
|
{
|
|
const std::size_t before = Widget::instance_count();
|
|
{
|
|
Widget a{1}, b{2}, c{3};
|
|
Void::Testing::expect_eq(Widget::instance_count(), before + 3);
|
|
}
|
|
Void::Testing::expect_eq(Widget::instance_count(), before);
|
|
}
|
|
|
|
Void::Testing::Test instance_counter_copy_increments()
|
|
{
|
|
const std::size_t before = Widget::instance_count();
|
|
{
|
|
Widget a{1};
|
|
Widget b = a; // copy
|
|
Void::Testing::expect_eq(Widget::instance_count(), before + 2);
|
|
}
|
|
Void::Testing::expect_eq(Widget::instance_count(), before);
|
|
}
|
|
|
|
Void::Testing::Test instance_counter_separate_per_type()
|
|
{
|
|
const std::size_t widgets_before = Widget::instance_count();
|
|
const std::size_t gadgets_before = Gadget::instance_count();
|
|
{
|
|
Widget w{42};
|
|
Gadget g;
|
|
Void::Testing::expect_eq(Widget::instance_count(), widgets_before + 1);
|
|
Void::Testing::expect_eq(Gadget::instance_count(), gadgets_before + 1);
|
|
}
|
|
Void::Testing::expect_eq(Gadget::instance_count(), gadgets_before);
|
|
}
|
|
|
|
// ── AtomicCounter ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test atomic_counter_initial_value()
|
|
{
|
|
AtomicCounter c;
|
|
Void::Testing::expect_eq(c.get(), std::size_t{0});
|
|
AtomicCounter c2{10};
|
|
Void::Testing::expect_eq(c2.get(), std::size_t{10});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_increment()
|
|
{
|
|
AtomicCounter c;
|
|
c.increment();
|
|
c.increment(5);
|
|
Void::Testing::expect_eq(c.get(), std::size_t{6});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_decrement()
|
|
{
|
|
AtomicCounter c{10};
|
|
c.decrement(3);
|
|
Void::Testing::expect_eq(c.get(), std::size_t{7});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_reset()
|
|
{
|
|
AtomicCounter c{100};
|
|
c.reset();
|
|
Void::Testing::expect_eq(c.get(), std::size_t{0});
|
|
c.reset(42);
|
|
Void::Testing::expect_eq(c.get(), std::size_t{42});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_prefix_operators()
|
|
{
|
|
AtomicCounter c{5};
|
|
++c;
|
|
Void::Testing::expect_eq(c.get(), std::size_t{6});
|
|
--c;
|
|
Void::Testing::expect_eq(c.get(), std::size_t{5});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_postfix_returns_old()
|
|
{
|
|
AtomicCounter c{3};
|
|
const auto old_inc = c++;
|
|
Void::Testing::expect_eq(old_inc, std::size_t{3});
|
|
Void::Testing::expect_eq(c.get(), std::size_t{4});
|
|
}
|
|
|
|
Void::Testing::Test atomic_counter_thread_safe_increment()
|
|
{
|
|
AtomicCounter c;
|
|
constexpr int threads = 4;
|
|
constexpr int per_thread = 1000;
|
|
|
|
std::vector<std::thread> ts;
|
|
ts.reserve(threads);
|
|
for (int i = 0; i < threads; ++i)
|
|
ts.emplace_back([&] { for (int j = 0; j < per_thread; ++j) c.increment(); });
|
|
for (auto& t : ts) t.join();
|
|
|
|
Void::Testing::expect_eq(c.get(), std::size_t{threads * per_thread});
|
|
}
|
|
|
|
// ── SeqId ─────────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test seq_id_monotonic()
|
|
{
|
|
struct MyTag {};
|
|
SeqId<MyTag> a, b, c;
|
|
Void::Testing::expect(a.id() < b.id());
|
|
Void::Testing::expect(b.id() < c.id());
|
|
}
|
|
|
|
Void::Testing::Test seq_id_unique_per_tag()
|
|
{
|
|
struct TagA {};
|
|
struct TagB {};
|
|
SeqId<TagA> a1, a2;
|
|
SeqId<TagB> b1;
|
|
// TagA and TagB sequences are independent
|
|
Void::Testing::expect(a1.id() != b1.id() || a2.id() != b1.id()); // at least one differs
|
|
}
|