|
12 | 12 | // Check that vector<bool> constructors don't leak memory when an operation inside the constructor throws an exception |
13 | 13 |
|
14 | 14 | #include <cstddef> |
15 | | -#include <type_traits> |
16 | 15 | #include <memory> |
| 16 | +#include <type_traits> |
17 | 17 | #include <vector> |
18 | 18 |
|
| 19 | +#include "../vector/common.h" |
19 | 20 | #include "count_new.h" |
20 | 21 | #include "test_iterators.h" |
21 | 22 |
|
22 | | -template <class T> |
23 | | -struct Allocator { |
24 | | - using value_type = T; |
25 | | - using is_always_equal = std::false_type; |
26 | | - |
27 | | - template <class U> |
28 | | - Allocator(const Allocator<U>&) {} |
29 | | - |
30 | | - Allocator(bool should_throw = true) { |
31 | | - if (should_throw) |
32 | | - throw 0; |
33 | | - } |
34 | | - |
35 | | - T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); } |
36 | | - void deallocate(T* ptr, std::size_t n) { std::allocator<T>().deallocate(ptr, n); } |
37 | | - |
38 | | - template <class U> |
39 | | - friend bool operator==(const Allocator&, const Allocator<U>&) { return true; } |
40 | | -}; |
41 | | - |
42 | | -template <class IterCat> |
43 | | -struct Iterator { |
44 | | - using iterator_category = IterCat; |
45 | | - using difference_type = std::ptrdiff_t; |
46 | | - using value_type = bool; |
47 | | - using reference = bool&; |
48 | | - using pointer = bool*; |
49 | | - |
50 | | - int i_; |
51 | | - bool b_ = true; |
52 | | - Iterator(int i = 0) : i_(i) {} |
53 | | - bool& operator*() { |
54 | | - if (i_ == 1) |
55 | | - throw 1; |
56 | | - return b_; |
57 | | - } |
58 | | - |
59 | | - friend bool operator==(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ == rhs.i_; } |
60 | | - |
61 | | - friend bool operator!=(const Iterator& lhs, const Iterator& rhs) { return lhs.i_ != rhs.i_; } |
62 | | - |
63 | | - Iterator& operator++() { |
64 | | - ++i_; |
65 | | - return *this; |
66 | | - } |
| 23 | +int main(int, char**) { |
| 24 | + using AllocVec = std::vector<bool, throwing_allocator<bool> >; |
67 | 25 |
|
68 | | - Iterator operator++(int) { |
69 | | - auto tmp = *this; |
70 | | - ++i_; |
71 | | - return tmp; |
| 26 | + try { // Throw in vector() from allocator |
| 27 | + AllocVec vec; |
| 28 | + } catch (int) { |
72 | 29 | } |
73 | | -}; |
74 | | - |
75 | | -void check_new_delete_called() { |
76 | | - assert(globalMemCounter.new_called == globalMemCounter.delete_called); |
77 | | - assert(globalMemCounter.new_array_called == globalMemCounter.delete_array_called); |
78 | | - assert(globalMemCounter.aligned_new_called == globalMemCounter.aligned_delete_called); |
79 | | - assert(globalMemCounter.aligned_new_array_called == globalMemCounter.aligned_delete_array_called); |
80 | | -} |
81 | | - |
82 | | -int main(int, char**) { |
83 | | - using AllocVec = std::vector<bool, Allocator<bool> >; |
| 30 | + check_new_delete_called(); |
84 | 31 |
|
85 | 32 | #if TEST_STD_VER >= 14 |
86 | | - try { // Throw in vector(size_type, const allocator_type&) from allocator |
87 | | - Allocator<bool> alloc(false); |
| 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); |
88 | 35 | AllocVec get_alloc(0, alloc); |
89 | 36 | } catch (int) { |
90 | 37 | } |
91 | 38 | check_new_delete_called(); |
92 | | -#endif // TEST_STD_VER >= 14 |
| 39 | +#endif // TEST_STD_VER >= 14 |
| 40 | + |
| 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); |
| 44 | + } catch (int) { |
| 45 | + } |
| 46 | + check_new_delete_called(); |
93 | 47 |
|
94 | 48 | try { // Throw in vector(InputIterator, InputIterator) from input iterator |
95 | | - std::vector<bool> vec((Iterator<std::input_iterator_tag>()), Iterator<std::input_iterator_tag>(2)); |
| 49 | + std::vector<bool> vec( |
| 50 | + throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2)); |
96 | 51 | } catch (int) { |
97 | 52 | } |
98 | 53 | check_new_delete_called(); |
99 | 54 |
|
100 | 55 | try { // Throw in vector(InputIterator, InputIterator) from forward iterator |
101 | | - std::vector<bool> vec((Iterator<std::forward_iterator_tag>()), Iterator<std::forward_iterator_tag>(2)); |
| 56 | + std::vector<bool> vec( |
| 57 | + throwing_iterator<bool, std::forward_iterator_tag>(), throwing_iterator<bool, std::forward_iterator_tag>(2)); |
102 | 58 | } catch (int) { |
103 | 59 | } |
104 | 60 | check_new_delete_called(); |
105 | 61 |
|
106 | 62 | try { // Throw in vector(InputIterator, InputIterator) from allocator |
107 | | - int a[] = {1, 2}; |
108 | | - AllocVec vec(cpp17_input_iterator<int*>(a), cpp17_input_iterator<int*>(a + 2)); |
| 63 | + bool a[] = {true, true}; |
| 64 | + AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2)); |
109 | 65 | } catch (int) { |
110 | 66 | } |
111 | 67 | check_new_delete_called(); |
112 | 68 |
|
113 | 69 | try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from input iterator |
114 | 70 | std::allocator<bool> alloc; |
115 | | - std::vector<bool> vec(Iterator<std::input_iterator_tag>(), Iterator<std::input_iterator_tag>(2), alloc); |
| 71 | + std::vector<bool> vec( |
| 72 | + throwing_iterator<bool, std::input_iterator_tag>(), throwing_iterator<bool, std::input_iterator_tag>(2), alloc); |
116 | 73 | } catch (int) { |
117 | 74 | } |
118 | 75 | check_new_delete_called(); |
119 | 76 |
|
120 | 77 | try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from forward iterator |
121 | 78 | std::allocator<bool> alloc; |
122 | | - std::vector<bool> vec(Iterator<std::forward_iterator_tag>(), Iterator<std::forward_iterator_tag>(2), alloc); |
| 79 | + std::vector<bool> vec(throwing_iterator<bool, std::forward_iterator_tag>(), |
| 80 | + throwing_iterator<bool, std::forward_iterator_tag>(2), |
| 81 | + alloc); |
123 | 82 | } catch (int) { |
124 | 83 | } |
125 | 84 | check_new_delete_called(); |
126 | 85 |
|
127 | 86 | try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator |
128 | | - bool a[] = {true, false}; |
129 | | - Allocator<bool> alloc(false); |
| 87 | + bool a[] = {true, false}; |
| 88 | + throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true); |
130 | 89 | AllocVec vec(cpp17_input_iterator<bool*>(a), cpp17_input_iterator<bool*>(a + 2), alloc); |
131 | 90 | } catch (int) { |
132 | 91 | } |
133 | 92 | check_new_delete_called(); |
134 | 93 |
|
135 | 94 | try { // Throw in vector(InputIterator, InputIterator, const allocator_type&) from allocator |
136 | | - bool a[] = {true, false}; |
137 | | - Allocator<bool> alloc(false); |
| 95 | + bool a[] = {true, false}; |
| 96 | + throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ true); |
138 | 97 | AllocVec vec(forward_iterator<bool*>(a), forward_iterator<bool*>(a + 2), alloc); |
139 | 98 | } catch (int) { |
140 | 99 | } |
141 | 100 | check_new_delete_called(); |
142 | 101 |
|
| 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); |
| 105 | + AllocVec vec(alloc); |
| 106 | + vec.push_back(true); |
| 107 | + alloc.throw_on_copy_ = true; |
| 108 | + AllocVec vec2(vec, alloc); |
| 109 | + } catch (int) { |
| 110 | + } |
| 111 | + check_new_delete_called(); |
| 112 | + |
| 113 | + try { // Throw in vector(vector&&, const allocator_type&) from allocator |
| 114 | + throwing_allocator<bool> alloc(/*throw_on_ctor = */ false, /*throw_on_copy = */ false); |
| 115 | + AllocVec vec(alloc); |
| 116 | + vec.push_back(true); |
| 117 | + alloc.throw_on_copy_ = true; |
| 118 | + AllocVec vec2(std::move(vec), alloc); |
| 119 | + } catch (int) { |
| 120 | + } |
| 121 | + check_new_delete_called(); |
| 122 | + |
| 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); |
| 126 | + } catch (int) { |
| 127 | + } |
| 128 | + check_new_delete_called(); |
| 129 | +#endif // TEST_STD_VER >= 11 |
| 130 | + |
143 | 131 | return 0; |
144 | 132 | } |
0 commit comments