Skip to content

Commit

Permalink
Fix resize() so that it respects max_size_max
Browse files Browse the repository at this point in the history
  • Loading branch information
skeetsaz committed Nov 10, 2023
1 parent e5181ac commit b67f2f2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
14 changes: 14 additions & 0 deletions cetlvast/suites/unittest/test_variable_length_array_bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ TYPED_TEST(VLABoolTests, TestBoolResizeOneBit)
ASSERT_EQ(0, array[4]);
}

TYPED_TEST(VLABoolTestsVLAOnly, TestBoolResizeExceedingMaxSizeMax)
{
std::size_t max_size_max = 1ul;
auto array = TypeParam::make_bool_container(1ul);

ASSERT_EQ(0, array.size());
#ifdef __cpp_exceptions
ASSERT_THROW(array.resize(2 * max_size_max), std::length_error);
#else
array.resize(2 * max_size_max);
ASSERT_EQ(max_size_max, array.size());
#endif // __cpp_exceptions
}

TYPED_TEST(VLABoolTests, TestBoolFront)
{
auto array = TypeParam::make_bool_container(std::initializer_list<bool>{true, false, true});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,27 @@ TYPED_TEST(VLATestsGeneralAllocation, TestResizeWithCopy)

// +-------------------------------------------------------------------------------------------------------------------+

TYPED_TEST(VLATestsGeneralAllocation, TestResizeExceedingMaxSizeMax)
{
if (!std::is_same<typename TypeParam::container_type, cetlvast::CETLTag>::value)
{
GTEST_SKIP() << "Skipping test that only works for CETL VLA.";
}

std::size_t max_size_max = 1ul;
typename TestFixture::SubjectType subject{max_size_max, TestFixture::make_allocator()};

ASSERT_EQ(0, subject.size());
#ifdef __cpp_exceptions
ASSERT_THROW(subject.resize(2 * max_size_max), std::length_error);
#else
subject.resize(2 * max_size_max);
ASSERT_EQ(max_size_max, subject.size());
#endif // __cpp_exceptions
}

// +-------------------------------------------------------------------------------------------------------------------+

TYPED_TEST(VLATestsGeneralAllocation, TestFrontAndBack)
{
using const_ref_value_type =
Expand Down
7 changes: 6 additions & 1 deletion include/cetl/variable_length_array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ class VariableLengthArrayBase
}

template <typename... Args>
constexpr void resize(const size_type new_size, const size_type max_size, Args&&... args)
constexpr void resize(size_type new_size, const size_type max_size, Args&&... args)
{
if (new_size == size_)
{
Expand All @@ -659,6 +659,11 @@ class VariableLengthArrayBase
if (new_size > capacity_)
{
reserve(new_size, max_size);
#if !defined(__cpp_exceptions)
if (capacity_ != new_size) {
new_size = capacity_;
}
#endif
}
for (std::size_t i = size_; i < new_size; ++i)
{
Expand Down

0 comments on commit b67f2f2

Please sign in to comment.