Skip to content

Commit 4936ff1

Browse files
committed
Added test for reusing block when not guaranteed
1 parent b88e8ed commit 4936ff1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

tests/runtime_tests.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3494,6 +3494,49 @@ TEST_CASE("observer from this maybe no block reset to new", "[observer_from_this
34943494
REQUIRE(mem_track.double_del() == 0u);
34953495
}
34963496

3497+
TEST_CASE("observer from this maybe no block reset to new after release", "[observer_from_this]") {
3498+
memory_tracker mem_track;
3499+
3500+
{
3501+
auto* raw_ptr = new test_object_observer_from_this_maybe_no_block_unique;
3502+
3503+
test_ptr_from_this_maybe_no_block ptr{raw_ptr};
3504+
const test_ptr_from_this_maybe_no_block& cptr = ptr;
3505+
3506+
test_optr_from_this_maybe_no_block_unique optr_from_this = ptr->observer_from_this();
3507+
test_optr_from_this_const_maybe_no_block_unique optr_from_this_const =
3508+
cptr->observer_from_this();
3509+
3510+
ptr.release();
3511+
3512+
REQUIRE(instances == 1);
3513+
REQUIRE(optr_from_this.expired() == false);
3514+
REQUIRE(optr_from_this_const.expired() == false);
3515+
REQUIRE(optr_from_this.get() == raw_ptr);
3516+
REQUIRE(optr_from_this_const.get() == raw_ptr);
3517+
3518+
ptr.reset(raw_ptr);
3519+
3520+
REQUIRE(instances == 1);
3521+
REQUIRE(optr_from_this.expired() == false);
3522+
REQUIRE(optr_from_this_const.expired() == false);
3523+
REQUIRE(optr_from_this.get() == ptr.get());
3524+
REQUIRE(optr_from_this_const.get() == ptr.get());
3525+
3526+
ptr.reset();
3527+
3528+
REQUIRE(instances == 0);
3529+
REQUIRE(optr_from_this.expired() == true);
3530+
REQUIRE(optr_from_this_const.expired() == true);
3531+
REQUIRE(optr_from_this.get() == nullptr);
3532+
REQUIRE(optr_from_this_const.get() == nullptr);
3533+
}
3534+
3535+
REQUIRE(instances == 0);
3536+
REQUIRE(mem_track.leaks() == 0u);
3537+
REQUIRE(mem_track.double_del() == 0u);
3538+
}
3539+
34973540
TEST_CASE("observer from this maybe no block reset to new bad alloc", "[observer_from_this]") {
34983541
memory_tracker mem_track;
34993542

0 commit comments

Comments
 (0)