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 // UNSUPPORTED: no-exceptions
11 // (bug report: https://llvm.org/PR58392)
12 // Check that vector constructors don't leak memory when an operation inside the constructor throws an exception
14 // XFAIL: FROZEN-CXX03-HEADERS-FIXME
18 #include <type_traits>
21 #include "../common.h"
22 #include "count_new.h"
23 #include "test_allocator.h"
24 #include "test_iterators.h"
26 int main(int, char**) {
27 using AllocVec
= std::vector
<int, throwing_allocator
<int> >;
32 check_new_delete_called();
34 try { // Throw in vector(size_type) from type
35 std::vector
<throwing_t
> get_alloc(1);
38 check_new_delete_called();
40 #if TEST_STD_VER >= 14
41 try { // Throw in vector(size_type, value_type) from type
43 throwing_t
v(throw_after
);
44 std::vector
<throwing_t
> get_alloc(1, v
);
47 check_new_delete_called();
49 try { // Throw in vector(size_type, const allocator_type&) from allocator
50 throwing_allocator
<int> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
51 AllocVec
get_alloc(0, alloc
);
54 check_new_delete_called();
56 try { // Throw in vector(size_type, const allocator_type&) from the type
57 std::vector
<throwing_t
> vec(1, std::allocator
<throwing_t
>());
60 check_new_delete_called();
61 #endif // TEST_STD_VER >= 14
63 try { // Throw in vector(size_type, value_type, const allocator_type&) from the type
65 throwing_t
v(throw_after
);
66 std::vector
<throwing_t
> vec(1, v
, std::allocator
<throwing_t
>());
69 check_new_delete_called();
71 try { // Throw in vector(InputIterator, InputIterator) from input iterator
73 (throwing_iterator
<int, std::input_iterator_tag
>()), throwing_iterator
<int, std::input_iterator_tag
>(2));
76 check_new_delete_called();
78 try { // Throw in vector(InputIterator, InputIterator) from forward iterator
80 (throwing_iterator
<int, std::forward_iterator_tag
>()), throwing_iterator
<int, std::forward_iterator_tag
>(2));
83 check_new_delete_called();
85 try { // Throw in vector(InputIterator, InputIterator) from allocator
87 AllocVec
vec(cpp17_input_iterator
<int*>(a
), cpp17_input_iterator
<int*>(a
+ 2));
90 check_new_delete_called();
92 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
93 std::allocator
<int> alloc
;
95 throwing_iterator
<int, std::input_iterator_tag
>(), throwing_iterator
<int, std::input_iterator_tag
>(2), alloc
);
98 check_new_delete_called();
100 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
101 std::allocator
<int> alloc
;
102 std::vector
<int> vec(throwing_iterator
<int, std::forward_iterator_tag
>(),
103 throwing_iterator
<int, std::forward_iterator_tag
>(2),
107 check_new_delete_called();
109 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
111 throwing_allocator
<int> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
112 AllocVec
vec(cpp17_input_iterator
<int*>(a
), cpp17_input_iterator
<int*>(a
+ 2), alloc
);
115 check_new_delete_called();
117 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
119 throwing_allocator
<int> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
120 AllocVec
vec(forward_iterator
<int*>(a
), forward_iterator
<int*>(a
+ 2), alloc
);
123 check_new_delete_called();
125 try { // Throw in vector(const vector&) from type
126 std::vector
<throwing_t
> vec
;
128 vec
.emplace_back(throw_after
);
132 check_new_delete_called();
134 try { // Throw in vector(const vector&, const allocator_type&) from type
135 std::vector
<throwing_t
> vec
;
137 vec
.emplace_back(throw_after
);
138 std::vector
<throwing_t
> vec2(vec
, std::allocator
<int>());
141 check_new_delete_called();
143 try { // Throw in vector(vector&&, const allocator_type&) from type during element-wise move
144 std::vector
<throwing_t
, test_allocator
<throwing_t
> > vec(test_allocator
<throwing_t
>(1));
145 int throw_after
= 10;
146 throwing_t
v(throw_after
);
147 vec
.insert(vec
.end(), 6, v
);
148 std::vector
<throwing_t
, test_allocator
<throwing_t
> > vec2(std::move(vec
), test_allocator
<throwing_t
>(2));
151 check_new_delete_called();
153 #if TEST_STD_VER >= 11
154 try { // Throw in vector(initializer_list<value_type>) from type
156 std::vector
<throwing_t
> vec({throwing_t(throw_after
)});
159 check_new_delete_called();
161 try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from type
163 std::vector
<throwing_t
> vec({throwing_t(throw_after
)}, std::allocator
<throwing_t
>());
166 check_new_delete_called();
167 #endif // TEST_STD_VER >= 11