Files
Library/tests/standalone/Core/Counted.cpp
T
faumarayandClaude Fable 5 28b94883fd 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>
2026-06-12 18:50:02 +03:00

150 lines
4.4 KiB
C++

#include <Void/Core/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
}