1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Ensure that all the elements in the vector are destroyed, especially when reallocating the internal buffer
19 #include "test_macros.h"
21 struct DestroyTracker
{
22 TEST_CONSTEXPR_CXX20
DestroyTracker(std::vector
<bool>& vec
) : vec_(&vec
), index_(vec
.size()) { vec
.push_back(false); }
24 TEST_CONSTEXPR_CXX20
DestroyTracker(const DestroyTracker
& other
) : vec_(other
.vec_
), index_(vec_
->size()) {
25 vec_
->push_back(false);
28 TEST_CONSTEXPR_CXX20 DestroyTracker
& operator=(const DestroyTracker
&) { return *this; }
29 TEST_CONSTEXPR_CXX20
~DestroyTracker() { (*vec_
)[index_
] = true; }
31 std::vector
<bool>* vec_
;
35 template <class Operation
>
36 TEST_CONSTEXPR_CXX20
void test(Operation operation
) {
37 std::vector
<bool> all_destroyed
;
40 std::vector
<DestroyTracker
> v
;
41 for (size_t i
= 0; i
!= 100; ++i
)
42 operation(v
, all_destroyed
);
45 assert(std::all_of(all_destroyed
.begin(), all_destroyed
.end(), [](bool b
) { return b
; }));
48 TEST_CONSTEXPR_CXX20
bool test() {
49 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) { vec
.emplace_back(tracker
); });
50 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) { vec
.push_back(tracker
); });
51 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) { vec
.emplace(vec
.begin(), tracker
); });
52 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) { vec
.insert(vec
.begin(), tracker
); });
53 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) { vec
.resize(vec
.size() + 1, tracker
); });
54 #if TEST_STD_VER >= 23
55 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) {
56 vec
.insert_range(vec
.begin(), std::array
<DestroyTracker
, 2>{tracker
, tracker
});
59 test([](std::vector
<DestroyTracker
>& vec
, std::vector
<bool>& tracker
) {
60 vec
.append_range(std::array
<DestroyTracker
, 2>{tracker
, tracker
});
69 #if TEST_STD_VER >= 20
70 static_assert(test());