Skip to content

Commit

Permalink
SmallVector fixing when we call destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jatinchowdhury18 committed Mar 28, 2024
1 parent 0454ded commit c6a1841
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,9 @@ class SmallVector
{
if (usingArray)
{
for (size_t i = 0; i < internal_array_size_used; ++i)
internal_array[i].~T();
internal_array_size_used = 0;
for (auto& elem : internal_array)
elem.~T();
}
internal_vector.clear();
}
Expand Down Expand Up @@ -447,7 +447,6 @@ class SmallVector
{
if (internal_array_size_used + 1 <= head_size)
{
internal_array[internal_array_size_used].~T();
new (&internal_array[internal_array_size_used]) T (args...);
internal_array_size_used++;
return internal_array[internal_array_size_used - 1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,30 @@ TEST_CASE ("Small Vector Test", "[common][data-structures]")
REQUIRE (vec.empty());
}

SECTION ("Clear w/ Destruction Test")
{
chowdsp::SmallVector<std::shared_ptr<int>, 4> vec {
std::make_shared<int> (1),
std::make_shared<int> (2),
std::make_shared<int> (3),
};
vec.resize (3);
REQUIRE (*vec[2] == 3);

vec.clear();
REQUIRE (vec.empty());

vec.emplace_back (std::make_shared<int> (0));
vec.emplace_back (std::make_shared<int> (1));
vec.emplace_back (std::make_shared<int> (2));
vec.emplace_back (std::make_shared<int> (3));
vec.emplace_back (std::make_shared<int> (4));
REQUIRE (vec.size() == 5);

vec.clear();
REQUIRE (vec.empty());
}

SECTION ("Insert/Emplace Single Value Test")
{
chowdsp::SmallVector<double, 4> vec { 1.0, 2.0 };
Expand Down Expand Up @@ -409,7 +433,6 @@ TEST_CASE ("Small Vector Test", "[common][data-structures]")
vec.erase (vec.begin() + 1, vec.begin() + 3);
REQUIRE (vec.size() == 3);
REQUIRE (vec[0] == 1);
;
REQUIRE (vec[1] == 4);
REQUIRE (vec[2] == 5);

Expand Down

0 comments on commit c6a1841

Please sign in to comment.