diff --git a/cetlvast/suites/unittest/test_variable_length_array_bool.cpp b/cetlvast/suites/unittest/test_variable_length_array_bool.cpp index 64a7fb79..a2ce506e 100644 --- a/cetlvast/suites/unittest/test_variable_length_array_bool.cpp +++ b/cetlvast/suites/unittest/test_variable_length_array_bool.cpp @@ -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{true, false, true}); diff --git a/cetlvast/suites/unittest/test_variable_length_array_general_allocation.cpp b/cetlvast/suites/unittest/test_variable_length_array_general_allocation.cpp index aa928916..52d043f7 100644 --- a/cetlvast/suites/unittest/test_variable_length_array_general_allocation.cpp +++ b/cetlvast/suites/unittest/test_variable_length_array_general_allocation.cpp @@ -784,6 +784,27 @@ TYPED_TEST(VLATestsGeneralAllocation, TestResizeWithCopy) // +-------------------------------------------------------------------------------------------------------------------+ +TYPED_TEST(VLATestsGeneralAllocation, TestResizeExceedingMaxSizeMax) +{ + if (!std::is_same::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 = diff --git a/include/cetl/variable_length_array.hpp b/include/cetl/variable_length_array.hpp index fc0ba395..d3ffd78a 100644 --- a/include/cetl/variable_length_array.hpp +++ b/include/cetl/variable_length_array.hpp @@ -646,7 +646,7 @@ class VariableLengthArrayBase } template - 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_) { @@ -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) {