|
| 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 | +// <vector> |
| 10 | + |
| 11 | +// Check that vector assignments don't change the rhs vector when an operation throws an exception during rallocations triggered by assignments |
| 12 | + |
| 13 | +#include <cassert> |
| 14 | +#include <ranges> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +#include "allocators.h" |
| 18 | +#include "exception_test_helpers.h" |
| 19 | +#include "test_allocator.h" |
| 20 | +#include "test_macros.h" |
| 21 | + |
| 22 | +void test_allocation_exception() { |
| 23 | + { |
| 24 | + limited_alloc_wrapper<int> alloc1 = limited_allocator<int, 100>(); |
| 25 | + limited_alloc_wrapper<int> alloc2 = limited_allocator<int, 200>(); |
| 26 | + std::vector<int, limited_alloc_wrapper<int>> v(100, alloc1); |
| 27 | + std::vector<int, limited_alloc_wrapper<int>> in(200, alloc2); |
| 28 | + try { // Throw in copy-assignment operator during allocation |
| 29 | + v = in; |
| 30 | + } catch (const std::exception&) { |
| 31 | + } |
| 32 | + assert(v.size() == 100); |
| 33 | + } |
| 34 | + |
| 35 | + { |
| 36 | + limited_alloc_wrapper<int> alloc1 = limited_allocator<int, 100>(); |
| 37 | + limited_alloc_wrapper<int> alloc2 = limited_allocator<int, 200>(); |
| 38 | + std::vector<int, limited_alloc_wrapper<int>> v(100, alloc1); |
| 39 | + std::vector<int, limited_alloc_wrapper<int>> in(200, alloc2); |
| 40 | + try { // Throw in move-assignment operator during allocation |
| 41 | + v = std::move(in); |
| 42 | + } catch (const std::exception&) { |
| 43 | + } |
| 44 | + assert(v.size() == 100); |
| 45 | + } |
| 46 | + |
| 47 | + { |
| 48 | + std::vector<int, limited_allocator<int, 5>> v(5); |
| 49 | + std::initializer_list<int> in{1, 2, 3, 4, 5, 6}; |
| 50 | + try { // Throw in operator=(initializer_list<value_type>) during allocation |
| 51 | + v = in; |
| 52 | + } catch (const std::exception&) { |
| 53 | + } |
| 54 | + assert(v.size() == 5); |
| 55 | + } |
| 56 | + |
| 57 | + { |
| 58 | + std::vector<int, limited_allocator<int, 100>> v(100); |
| 59 | + std::vector<int> in(101, 1); |
| 60 | + try { // Throw in assign(_ForwardIterator, _ForwardIterator) during allocation |
| 61 | + v.assign(in.begin(), in.end()); |
| 62 | + } catch (const std::exception&) { |
| 63 | + } |
| 64 | + assert(v.size() == 100); |
| 65 | + } |
| 66 | + |
| 67 | + { |
| 68 | + std::vector<int, limited_allocator<int, 100>> v(100); |
| 69 | + try { // Throw in assign(size_type, const_reference) during allocation |
| 70 | + v.assign(101, 1); |
| 71 | + } catch (const std::exception&) { |
| 72 | + } |
| 73 | + assert(v.size() == 100); |
| 74 | + } |
| 75 | + |
| 76 | + { |
| 77 | + std::vector<int, limited_allocator<int, 5>> v(5); |
| 78 | + std::initializer_list<int> in{1, 2, 3, 4, 5, 6}; |
| 79 | + try { // Throw in assign(initializer_list<value_type>) during allocation |
| 80 | + v.assign(in); |
| 81 | + } catch (const std::exception&) { |
| 82 | + } |
| 83 | + assert(v.size() == 5); |
| 84 | + } |
| 85 | + |
| 86 | +#if TEST_STD_VER >= 23 |
| 87 | + { |
| 88 | + std::vector<int, limited_allocator<int, 100>> v(100); |
| 89 | + std::vector<int> in(101, 1); |
| 90 | + try { // Throw in assign(_ForwardIterator, _ForwardIterator) during allocation |
| 91 | + v.assign_range(in); |
| 92 | + } catch (const std::exception&) { |
| 93 | + } |
| 94 | + assert(v.size() == 100); |
| 95 | + } |
| 96 | +#endif |
| 97 | +} |
| 98 | + |
| 99 | +void test_construction_exception() { |
| 100 | + { |
| 101 | + int throw_after = 10; |
| 102 | + throwing_t t = throw_after; |
| 103 | + std::vector<throwing_t> in(6, t); |
| 104 | + std::vector<throwing_t> v(3, t); |
| 105 | + try { // Throw in copy-assignment operator from element type during construction |
| 106 | + v = in; |
| 107 | + } catch (int) { |
| 108 | + } |
| 109 | + assert(v.size() == 3); |
| 110 | + } |
| 111 | + |
| 112 | + { |
| 113 | + int throw_after = 10; |
| 114 | + throwing_t t = throw_after; |
| 115 | + NONPOCMAAllocator<throwing_t> alloc1(1); |
| 116 | + NONPOCMAAllocator<throwing_t> alloc2(2); |
| 117 | + std::vector<throwing_t, NONPOCMAAllocator<throwing_t>> in(6, t, alloc1); |
| 118 | + std::vector<throwing_t, NONPOCMAAllocator<throwing_t>> v(3, t, alloc2); |
| 119 | + try { // Throw in move-assignment operator from element type during construction |
| 120 | + v = std::move(in); |
| 121 | + } catch (int) { |
| 122 | + } |
| 123 | + assert(v.size() == 3); |
| 124 | + } |
| 125 | + |
| 126 | + { |
| 127 | + int throw_after = 10; |
| 128 | + throwing_t t = throw_after; |
| 129 | + std::initializer_list<throwing_t> in{t, t, t, t, t, t}; |
| 130 | + std::vector<throwing_t> v(3, t); |
| 131 | + try { // Throw in operator=(initializer_list<value_type>) from element type during construction |
| 132 | + v = in; |
| 133 | + } catch (int) { |
| 134 | + } |
| 135 | + assert(v.size() == 3); |
| 136 | + } |
| 137 | + |
| 138 | + { |
| 139 | + std::vector<int> v(3); |
| 140 | + try { // Throw in assign(_ForwardIterator, _ForwardIterator) from forward iterator during construction |
| 141 | + v.assign(throwing_iterator<int, std::forward_iterator_tag>(), |
| 142 | + throwing_iterator<int, std::forward_iterator_tag>(6)); |
| 143 | + } catch (int) { |
| 144 | + } |
| 145 | + assert(v.size() == 3); |
| 146 | + } |
| 147 | + |
| 148 | + { |
| 149 | + int throw_after = 10; |
| 150 | + throwing_t t = throw_after; |
| 151 | + std::vector<throwing_t> in(6, t); |
| 152 | + std::vector<throwing_t> v(3, t); |
| 153 | + try { // Throw in assign(_ForwardIterator, _ForwardIterator) from element type during construction |
| 154 | + v.assign(in.begin(), in.end()); |
| 155 | + } catch (int) { |
| 156 | + } |
| 157 | + assert(v.size() == 3); |
| 158 | + } |
| 159 | +#if TEST_STD_VER >= 23 |
| 160 | + { |
| 161 | + int throw_after = 10; |
| 162 | + throwing_t t = throw_after; |
| 163 | + std::vector<throwing_t> in(6, t); |
| 164 | + std::vector<throwing_t> v(3, t); |
| 165 | + try { // Throw in assign_range(_Range&&) from element type during construction |
| 166 | + v.assign_range(in); |
| 167 | + } catch (int) { |
| 168 | + } |
| 169 | + assert(v.size() == 3); |
| 170 | + } |
| 171 | +#endif |
| 172 | + { |
| 173 | + int throw_after = 4; |
| 174 | + throwing_t t = throw_after; |
| 175 | + std::vector<throwing_t> v(3, t); |
| 176 | + try { // Throw in assign(size_type, const_reference) from element type during construction |
| 177 | + v.assign(6, t); |
| 178 | + } catch (int) { |
| 179 | + } |
| 180 | + assert(v.size() == 3); |
| 181 | + } |
| 182 | + |
| 183 | + { |
| 184 | + int throw_after = 10; |
| 185 | + throwing_t t = throw_after; |
| 186 | + std::initializer_list<throwing_t> in{t, t, t, t, t, t}; |
| 187 | + std::vector<throwing_t> v(3, t); |
| 188 | + try { // Throw in assign(initializer_list<value_type>) from element type during construction |
| 189 | + v.assign(in); |
| 190 | + } catch (int) { |
| 191 | + } |
| 192 | + assert(v.size() == 3); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +int main() { |
| 197 | + test_allocation_exception(); |
| 198 | + test_construction_exception(); |
| 199 | +} |
0 commit comments