1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// Ensure that all the elements in the vector are destroyed, especially when reallocating the internal buffer
10
11// UNSUPPORTED: c++03
12
13#include <algorithm>
14#include <array>
15#include <cassert>
16#include <vector>
17
18#include "test_macros.h"
19
20struct DestroyTracker {
21 TEST_CONSTEXPR_CXX20 DestroyTracker(std::vector<bool>& vec) : vec_(&vec), index_(vec.size()) { vec.push_back(false); }
22
23 TEST_CONSTEXPR_CXX20 DestroyTracker(const DestroyTracker& other) : vec_(other.vec_), index_(vec_->size()) {
24 vec_->push_back(false);
25 }
26
27 TEST_CONSTEXPR_CXX20 DestroyTracker& operator=(const DestroyTracker&) { return *this; }
28 TEST_CONSTEXPR_CXX20 ~DestroyTracker() { (*vec_)[index_] = true; }
29
30 std::vector<bool>* vec_;
31 size_t index_;
32};
33
34template <class Operation>
35TEST_CONSTEXPR_CXX20 void test(Operation operation) {
36 std::vector<bool> all_destroyed;
37
38 {
39 std::vector<DestroyTracker> v;
40 for (size_t i = 0; i != 100; ++i)
41 operation(v, all_destroyed);
42 }
43
44 assert(std::all_of(all_destroyed.begin(), all_destroyed.end(), [](bool b) { return b; }));
45}
46
47TEST_CONSTEXPR_CXX20 bool test() {
48 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { vec.emplace_back(args&: tracker); });
49 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { vec.push_back(tracker); });
50 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { vec.emplace(position: vec.begin(), args&: tracker); });
51 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { vec.insert(vec.begin(), tracker); });
52 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { vec.resize(vec.size() + 1, tracker); });
53#if TEST_STD_VER >= 23
54 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) {
55 vec.insert_range(vec.begin(), std::array<DestroyTracker, 2>{tracker, tracker});
56 });
57
58 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) {
59 vec.append_range(std::array<DestroyTracker, 2>{tracker, tracker});
60 });
61#endif
62
63 return true;
64}
65
66int main() {
67 test();
68#if TEST_STD_VER >= 20
69 static_assert(test());
70#endif
71}
72

source code of libcxx/test/std/containers/sequences/vector/vector.modifiers/destory_elements.pass.cpp