xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/ctor_exceptions.pass.cpp (revision c5cd1e958c2cea9fe5a6a9087c3481537d598e24)
18ff4d218SNikolas Klauser //===----------------------------------------------------------------------===//
28ff4d218SNikolas Klauser //
38ff4d218SNikolas Klauser // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48ff4d218SNikolas Klauser // See https://llvm.org/LICENSE.txt for license information.
58ff4d218SNikolas Klauser // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68ff4d218SNikolas Klauser //
78ff4d218SNikolas Klauser //===----------------------------------------------------------------------===//
88ff4d218SNikolas Klauser 
98ff4d218SNikolas Klauser // UNSUPPORTED: no-exceptions
108ff4d218SNikolas Klauser 
118ff4d218SNikolas Klauser // (bug report: https://llvm.org/PR58392)
128ff4d218SNikolas Klauser // Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception
138ff4d218SNikolas Klauser 
14886e92c1SCasey Carter #include <cstddef>
15d6cd4257SMark de Wever #include <memory>
16*c5cd1e95SPeng Liu #include <type_traits>
178ff4d218SNikolas Klauser #include <vector>
188ff4d218SNikolas Klauser 
19*c5cd1e95SPeng Liu #include "../vector/common.h"
208ff4d218SNikolas Klauser #include "count_new.h"
218ff4d218SNikolas Klauser #include "test_iterators.h"
228ff4d218SNikolas Klauser 
238ff4d218SNikolas Klauser int main(int, char**) {
24*c5cd1e95SPeng Liu   using AllocVec = std::vector<bool, throwing_allocator<bool> >;
25*c5cd1e95SPeng Liu 
26*c5cd1e95SPeng Liu   try { // Throw in vector() from allocator
27*c5cd1e95SPeng Liu     AllocVec vec;
28*c5cd1e95SPeng Liu   } catch (int) {
29*c5cd1e95SPeng Liu   }
30*c5cd1e95SPeng Liu   check_new_delete_called();
318ff4d218SNikolas Klauser 
328ff4d218SNikolas Klauser #if TEST_STD_VER >= 14
338ff4d218SNikolas Klauser   try { // Throw in vector(size_type, const allocator_type&) from allocator
34*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
358ff4d218SNikolas Klauser     AllocVec get_alloc(0, alloc);
368ff4d218SNikolas Klauser   } catch (int) {
378ff4d218SNikolas Klauser   }
388ff4d218SNikolas Klauser   check_new_delete_called();
398ff4d218SNikolas Klauser #endif // TEST_STD_VER >= 14
408ff4d218SNikolas Klauser 
41*c5cd1e95SPeng Liu   try { // Throw in vector(size_type, const value_type&, const allocator_type&) from allocator
42*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
43*c5cd1e95SPeng Liu     AllocVec get_alloc(0, true, alloc);
44*c5cd1e95SPeng Liu   } catch (int) {
45*c5cd1e95SPeng Liu   }
46*c5cd1e95SPeng Liu   check_new_delete_called();
47*c5cd1e95SPeng Liu 
488ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator) from input iterator
49*c5cd1e95SPeng Liu     std::vector<bool> vec(
50*c5cd1e95SPeng Liu         throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2));
518ff4d218SNikolas Klauser   } catch (int) {
528ff4d218SNikolas Klauser   }
538ff4d218SNikolas Klauser   check_new_delete_called();
548ff4d218SNikolas Klauser 
558ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator) from forward iterator
56*c5cd1e95SPeng Liu     std::vector<bool> vec(
57*c5cd1e95SPeng Liu         throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2));
588ff4d218SNikolas Klauser   } catch (int) {
598ff4d218SNikolas Klauser   }
608ff4d218SNikolas Klauser   check_new_delete_called();
618ff4d218SNikolas Klauser 
628ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator) from allocator
63*c5cd1e95SPeng Liu     bool a[] = {true, true};
64*c5cd1e95SPeng Liu     AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2));
658ff4d218SNikolas Klauser   } catch (int) {
668ff4d218SNikolas Klauser   }
678ff4d218SNikolas Klauser   check_new_delete_called();
688ff4d218SNikolas Klauser 
698ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator
708ff4d218SNikolas Klauser     std::allocator<bool> alloc;
71*c5cd1e95SPeng Liu     std::vector<bool> vec(
72*c5cd1e95SPeng Liu         throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc);
738ff4d218SNikolas Klauser   } catch (int) {
748ff4d218SNikolas Klauser   }
758ff4d218SNikolas Klauser   check_new_delete_called();
768ff4d218SNikolas Klauser 
778ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator
788ff4d218SNikolas Klauser     std::allocator<bool> alloc;
79*c5cd1e95SPeng Liu     std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(),
80*c5cd1e95SPeng Liu                           throwing_iterator<bool, std::forward_iterator_tag>(2),
81*c5cd1e95SPeng Liu                           alloc);
828ff4d218SNikolas Klauser   } catch (int) {
838ff4d218SNikolas Klauser   }
848ff4d218SNikolas Klauser   check_new_delete_called();
858ff4d218SNikolas Klauser 
868ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
878ff4d218SNikolas Klauser     bool a[] = {true, false};
88*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
898ff4d218SNikolas Klauser     AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc);
908ff4d218SNikolas Klauser   } catch (int) {
918ff4d218SNikolas Klauser   }
928ff4d218SNikolas Klauser   check_new_delete_called();
938ff4d218SNikolas Klauser 
948ff4d218SNikolas Klauser   try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator
958ff4d218SNikolas Klauser     bool a[] = {true, false};
96*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
978ff4d218SNikolas Klauser     AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc);
988ff4d218SNikolas Klauser   } catch (int) {
998ff4d218SNikolas Klauser   }
1008ff4d218SNikolas Klauser   check_new_delete_called();
1018ff4d218SNikolas Klauser 
102*c5cd1e95SPeng Liu #if TEST_STD_VER >= 11
103*c5cd1e95SPeng Liu   try { // Throw in vector(const vector&, const allocator_type&) from allocator
104*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
105*c5cd1e95SPeng Liu     AllocVec vec(alloc);
106*c5cd1e95SPeng Liu     vec.push_back(true);
107*c5cd1e95SPeng Liu     alloc.throw_on_copy_ = true;
108*c5cd1e95SPeng Liu     AllocVec vec2(vec, alloc);
109*c5cd1e95SPeng Liu   } catch (int) {
110*c5cd1e95SPeng Liu   }
111*c5cd1e95SPeng Liu   check_new_delete_called();
112*c5cd1e95SPeng Liu 
113*c5cd1e95SPeng Liu   try { // Throw in vector(vector&&, const allocator_type&) from allocator
114*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false);
115*c5cd1e95SPeng Liu     AllocVec vec(alloc);
116*c5cd1e95SPeng Liu     vec.push_back(true);
117*c5cd1e95SPeng Liu     alloc.throw_on_copy_ = true;
118*c5cd1e95SPeng Liu     AllocVec vec2(std::move(vec), alloc);
119*c5cd1e95SPeng Liu   } catch (int) {
120*c5cd1e95SPeng Liu   }
121*c5cd1e95SPeng Liu   check_new_delete_called();
122*c5cd1e95SPeng Liu 
123*c5cd1e95SPeng Liu   try { // Throw in vector(initializer_list<value_type>, const allocator_type&) constructor from allocator
124*c5cd1e95SPeng Liu     throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true);
125*c5cd1e95SPeng Liu     AllocVec vec({true, true}, alloc);
126*c5cd1e95SPeng Liu   } catch (int) {
127*c5cd1e95SPeng Liu   }
128*c5cd1e95SPeng Liu   check_new_delete_called();
129*c5cd1e95SPeng Liu #endif // TEST_STD_VER >= 11
130*c5cd1e95SPeng Liu 
1318ff4d218SNikolas Klauser   return 0;
1328ff4d218SNikolas Klauser }
133