Skip to content
This repository has been archived by the owner on Sep 22, 2023. It is now read-only.

Test failure path for allocated objects #127

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions polymorphic_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class control_block_deleter {
constexpr void operator()(T* t) const noexcept {
if (t != nullptr) {
t->destroy();
t = nullptr;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed here?

}
}
};
Expand Down
39 changes: 37 additions & 2 deletions polymorphic_value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,43 @@ TEST_CASE("Copying object with allocator allocates") {
CHECK(deallocs == 4);
}

TEST_CASE("Allocator used to construct with make_polymorphic") {
GIVEN("an alloator which tracks allocations") {
unsigned allocs = 0;
unsigned deallocs = 0;

tracking_allocator<int> alloc(&allocs, &deallocs);
WHEN("Constructing a type from the allocator") {
unsigned const value = 99;
auto p = make_polymorphic_value<DerivedType>(std::allocator_arg_t{},
alloc, value);
THEN("Expect the allocation to be tracked") {
CHECK(allocs == 2);
CHECK(deallocs == 0);
}
AND_THEN("Expect the deallocation to be tracked") {
p.~polymorphic_value();
CHECK(allocs == 2);
CHECK(deallocs == 2);
}
}
WHEN("Constructing a type that throws on construction from the allocator") {
struct ThrowOnConstruction : DerivedType {
ThrowOnConstruction() { throw "I throw in my default constructor"; }
};

CHECK_THROWS(make_polymorphic_value<ThrowOnConstruction>(
std::allocator_arg_t{}, alloc));
THEN(
"Expect allocation and subsequent deallocation of the intial T to be "
"tracked after the throw (but not the control block's allocation)") {
CHECK(allocs == 1);
CHECK(deallocs == 1);
}
}
}
}

TEST_CASE("Allocator used to construct with allocate_polymorphic_value") {
unsigned allocs = 0;
unsigned deallocs = 0;
Expand All @@ -875,6 +912,4 @@ TEST_CASE("Allocator used to construct with allocate_polymorphic_value") {
CHECK(allocs == 2);
CHECK(deallocs == 0);
}
CHECK(allocs == 2);
CHECK(deallocs == 2);
}