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<bool> constructors don't leak memory when an operation inside the constructor throws an exception
16 #include <type_traits>
19 #include "../vector/common.h"
20 #include "count_new.h"
21 #include "test_iterators.h"
23 int main(int, char**) {
24 using AllocVec
= std::vector
<bool, throwing_allocator
<bool> >;
26 try { // Throw in vector() from allocator
30 check_new_delete_called();
32 #if TEST_STD_VER >= 14
33 try { // Throw in vector(size_type, const allocator_type&) from allocator
34 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
35 AllocVec
get_alloc(0, alloc
);
38 check_new_delete_called();
39 #endif // TEST_STD_VER >= 14
41 try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
42 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
43 AllocVec
get_alloc(0, true, alloc
);
46 check_new_delete_called();
48 try { // Throw in vector(InputIterator, InputIterator) from input iterator
49 std::vector
<bool> vec(
50 throwing_iterator
<bool, std::input_iterator_tag
>(), throwing_iterator
<bool, std::input_iterator_tag
>(2));
53 check_new_delete_called();
55 try { // Throw in vector(InputIterator, InputIterator) from forward iterator
56 std::vector
<bool> vec(
57 throwing_iterator
<bool, std::forward_iterator_tag
>(), throwing_iterator
<bool, std::forward_iterator_tag
>(2));
60 check_new_delete_called();
62 try { // Throw in vector(InputIterator, InputIterator) from allocator
63 bool a
[] = {true, true};
64 AllocVec
vec(cpp17_input_iterator
<bool*>(a
), cpp17_input_iterator
<bool*>(a
+ 2));
67 check_new_delete_called();
69 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
70 std::allocator
<bool> alloc
;
71 std::vector
<bool> vec(
72 throwing_iterator
<bool, std::input_iterator_tag
>(), throwing_iterator
<bool, std::input_iterator_tag
>(2), alloc
);
75 check_new_delete_called();
77 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
78 std::allocator
<bool> alloc
;
79 std::vector
<bool> vec(throwing_iterator
<bool, std::forward_iterator_tag
>(),
80 throwing_iterator
<bool, std::forward_iterator_tag
>(2),
84 check_new_delete_called();
86 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
87 bool a
[] = {true, false};
88 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
89 AllocVec
vec(cpp17_input_iterator
<bool*>(a
), cpp17_input_iterator
<bool*>(a
+ 2), alloc
);
92 check_new_delete_called();
94 try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
95 bool a
[] = {true, false};
96 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
97 AllocVec
vec(forward_iterator
<bool*>(a
), forward_iterator
<bool*>(a
+ 2), alloc
);
100 check_new_delete_called();
102 #if TEST_STD_VER >= 11
103 try { // Throw in vector(const vector&, const allocator_type&) from allocator
104 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
107 alloc
.throw_on_copy_
= true;
108 AllocVec
vec2(vec
, alloc
);
111 check_new_delete_called();
113 try { // Throw in vector(vector&&, const allocator_type&) from allocator
114 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
117 alloc
.throw_on_copy_
= true;
118 AllocVec
vec2(std::move(vec
), alloc
);
121 check_new_delete_called();
123 try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
124 throwing_allocator
<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
125 AllocVec
vec({true, true}, alloc
);
128 check_new_delete_called();
129 #endif // TEST_STD_VER >= 11