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