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 <cstddef> 17 #include <vector> 18 19 #include "test_macros.h" 20 21 struct DestroyTracker { 22 TEST_CONSTEXPR_CXX20 DestroyTracker(std::vector<bool>& vec) : vec_(&vec), index_(vec.size()) { vec.push_back(false); } 23 24 TEST_CONSTEXPR_CXX20 DestroyTracker(const DestroyTracker& other) : vec_(other.vec_), index_(vec_->size()) { 25 vec_->push_back(false); 26 } 27 28 TEST_CONSTEXPR_CXX20 DestroyTracker& operator=(const DestroyTracker&) { return *this; } 29 TEST_CONSTEXPR_CXX20 ~DestroyTracker() { (*vec_)[index_] = true; } 30 31 std::vector<bool>* vec_; 32 size_t index_; 33 }; 34 35 template <class Operation> 36 TEST_CONSTEXPR_CXX20 void test(Operation operation) { 37 std::vector<bool> all_destroyed; 38 39 { 40 std::vector<DestroyTracker> v; 41 for (size_t i = 0; i != 100; ++i) 42 operation(v, all_destroyed); 43 } 44 45 assert(std::all_of(all_destroyed.begin(), all_destroyed.end(), [](bool b) { return b; })); 46 } 47 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 }); 58 59 test([](std::vector<DestroyTracker>& vec, std::vector<bool>& tracker) { 60 vec.append_range(std::array<DestroyTracker, 2>{tracker, tracker}); 61 }); 62 #endif 63 64 return true; 65 } 66 67 int main() { 68 test(); 69 #if TEST_STD_VER >= 20 70 static_assert(test()); 71 #endif 72 } 73