diff options
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/small_vector/small_vector_test.cpp | 26 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/small_vector.h | 1 |
2 files changed, 27 insertions, 0 deletions
diff --git a/vespalib/src/tests/small_vector/small_vector_test.cpp b/vespalib/src/tests/small_vector/small_vector_test.cpp index 2118a3c492f..4b432799672 100644 --- a/vespalib/src/tests/small_vector/small_vector_test.cpp +++ b/vespalib/src/tests/small_vector/small_vector_test.cpp @@ -7,6 +7,14 @@ using namespace vespalib; +struct Alive { + uint32_t &cnt; + uint32_t id; + Alive(uint32_t &cnt_in, uint32_t id_in) noexcept + : cnt(cnt_in), id(id_in) { ++cnt; } + ~Alive() { --cnt; } +}; + template <typename T, size_t N> void verify(const SmallVector<T,N> &vec, std::vector<uint32_t> expect, size_t expect_capacity = 0) { if (expect_capacity == 0) { @@ -263,4 +271,22 @@ TEST(SmallVectorTest, check_back_method) { EXPECT_EQ(&vec.back(), vec.end() - 1); } +TEST(SmallVectorTest, pop_back) { + uint32_t my_cnt = 0; + { + SmallVector<Alive> vec; + vec.emplace_back(my_cnt, 1); + vec.emplace_back(my_cnt, 2); + vec.emplace_back(my_cnt, 3); + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(my_cnt, 3); + vec.pop_back(); + EXPECT_EQ(my_cnt, 2); + ASSERT_EQ(vec.size(), 2); + EXPECT_EQ(vec[0].id, 1); + EXPECT_EQ(vec[1].id, 2); + } + EXPECT_EQ(my_cnt, 0); +} + GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/small_vector.h b/vespalib/src/vespa/vespalib/util/small_vector.h index ba166362d33..0d204897328 100644 --- a/vespalib/src/vespa/vespalib/util/small_vector.h +++ b/vespalib/src/vespa/vespalib/util/small_vector.h @@ -212,6 +212,7 @@ public: } void push_back(const T &obj) { emplace_back(obj); } void push_back(T &&obj) { emplace_back(std::move(obj)); } + void pop_back() { small_vector::destroy_objects(_data + --_size, 1); } }; template <typename T, size_t N, size_t M> |