[clang] Fix crashes when passing VLA to va_arg (#119563)
[llvm-project.git] / libcxx / test / std / containers / sequences / vector / vector.modifiers / destroy_elements.pass.cpp
bloba245131c7869da7595926639b72eb14050ca1080
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 //===----------------------------------------------------------------------===//
9 // Ensure that all the elements in the vector are destroyed, especially when reallocating the internal buffer
11 // UNSUPPORTED: c++03
13 #include <algorithm>
14 #include <array>
15 #include <cassert>
16 #include <cstddef>
17 #include <vector>
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_;
32 size_t index_;
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});
57 });
59 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) {
60 vec.append_range(std::array<DestroyTracker, 2>{tracker, tracker});
61 });
62 #endif
64 return true;
67 int main() {
68 test();
69 #if TEST_STD_VER >= 20
70 static_assert(test());
71 #endif