Skip to content

Commit a4d40e1

Browse files
committed
Add tests for custom deleters, same-state assignment, and move-only args
Covers custom deleter invocation, assignment between observers sharing the same state, and make_proxy with move-only constructor arguments (validates perfect forwarding).
1 parent 0c22732 commit a4d40e1

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

test/test.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,50 @@ TEST_CASE("proxy_factory creates proxies") {
383383
CHECK(p.alive());
384384
CHECK(*p.get() == 99);
385385
}
386+
387+
// ── Custom deleters ─────────────────────────────────────────────────────────
388+
389+
static bool g_custom_deleter_called = false;
390+
struct TestDeleter {
391+
void operator()(int* p) {
392+
g_custom_deleter_called = true;
393+
delete p;
394+
}
395+
};
396+
397+
TEST_CASE("custom deleter is called on proxy_delete") {
398+
g_custom_deleter_called = false;
399+
auto owner = proxy::proxy_owner_ptr<int>(new int(42), TestDeleter{});
400+
CHECK(owner.alive());
401+
CHECK_FALSE(g_custom_deleter_called);
402+
403+
owner.proxy_delete();
404+
CHECK(g_custom_deleter_called);
405+
CHECK(owner.expired());
406+
}
407+
408+
// ── Same-state assignment ───────────────────────────────────────────────────
409+
410+
TEST_CASE("assign between observers of same state is safe") {
411+
auto owner = proxy::make_proxy<int>(5);
412+
proxy::proxy_ptr<int> a = owner;
413+
proxy::proxy_ptr<int> b = owner;
414+
415+
// a and b share state — assignment should be a no-op
416+
a = b;
417+
CHECK(a.alive());
418+
CHECK(b.alive());
419+
CHECK(a.get() == b.get());
420+
}
421+
422+
// ── Move-only constructor args ──────────────────────────────────────────────
423+
424+
TEST_CASE("make_proxy supports move-only constructor args") {
425+
struct MoveOnly {
426+
std::unique_ptr<int> val;
427+
MoveOnly(std::unique_ptr<int> v) : val(std::move(v)) {}
428+
};
429+
auto owner = proxy::make_proxy<MoveOnly>(std::make_unique<int>(42));
430+
CHECK(owner.alive());
431+
CHECK(*owner->val == 42);
432+
}

0 commit comments

Comments
 (0)