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 | // template <class Iter> |
12 | // iterator insert(const_iterator position, Iter first, Iter last); |
13 | |
14 | #include <vector> |
15 | #include <cassert> |
16 | #include <cstddef> |
17 | |
18 | #include "test_macros.h" |
19 | #include "test_allocator.h" |
20 | #include "test_iterators.h" |
21 | #include "min_allocator.h" |
22 | #include "asan_testing.h" |
23 | |
24 | namespace adl { |
25 | struct S {}; |
26 | void make_move_iterator(S*) {} |
27 | } |
28 | |
29 | TEST_CONSTEXPR_CXX20 bool tests() |
30 | { |
31 | { |
32 | typedef std::vector<int> V; |
33 | V v(100); |
34 | int a[] = {1, 2, 3, 4, 5}; |
35 | const int N = sizeof(a)/sizeof(a[0]); |
36 | V::iterator i = v.insert(v.cbegin() + 10, cpp17_input_iterator<const int*>(a), |
37 | cpp17_input_iterator<const int*>(a+N)); |
38 | assert(v.size() == 100 + N); |
39 | assert(is_contiguous_container_asan_correct(v)); |
40 | assert(i == v.begin() + 10); |
41 | int j; |
42 | for (j = 0; j < 10; ++j) |
43 | assert(v[j] == 0); |
44 | for (std::size_t k = 0; k < N; ++j, ++k) |
45 | assert(v[j] == a[k]); |
46 | for (; j < 105; ++j) |
47 | assert(v[j] == 0); |
48 | } |
49 | { |
50 | typedef std::vector<int> V; |
51 | V v(100); |
52 | int a[] = {1, 2, 3, 4, 5}; |
53 | const int N = sizeof(a)/sizeof(a[0]); |
54 | V::iterator i = v.insert(v.cbegin() + 10, forward_iterator<const int*>(a), |
55 | forward_iterator<const int*>(a+N)); |
56 | assert(v.size() == 100 + N); |
57 | assert(is_contiguous_container_asan_correct(v)); |
58 | assert(i == v.begin() + 10); |
59 | int j; |
60 | for (j = 0; j < 10; ++j) |
61 | assert(v[j] == 0); |
62 | for (std::size_t k = 0; k < N; ++j, ++k) |
63 | assert(v[j] == a[k]); |
64 | for (; j < 105; ++j) |
65 | assert(v[j] == 0); |
66 | } |
67 | { |
68 | typedef std::vector<int> V; |
69 | V v(100); |
70 | while(v.size() < v.capacity()) v.push_back(x: 0); // force reallocation |
71 | std::size_t sz = v.size(); |
72 | int a[] = {1, 2, 3, 4, 5}; |
73 | const unsigned N = sizeof(a)/sizeof(a[0]); |
74 | V::iterator i = v.insert(v.cbegin() + 10, forward_iterator<const int*>(a), |
75 | forward_iterator<const int*>(a+N)); |
76 | assert(v.size() == sz + N); |
77 | assert(i == v.begin() + 10); |
78 | std::size_t j; |
79 | for (j = 0; j < 10; ++j) |
80 | assert(v[j] == 0); |
81 | for (std::size_t k = 0; k < N; ++j, ++k) |
82 | assert(v[j] == a[k]); |
83 | for (; j < v.size(); ++j) |
84 | assert(v[j] == 0); |
85 | } |
86 | { |
87 | typedef std::vector<int> V; |
88 | V v(100); |
89 | v.reserve(n: 128); // force no reallocation |
90 | std::size_t sz = v.size(); |
91 | int a[] = {1, 2, 3, 4, 5}; |
92 | const unsigned N = sizeof(a)/sizeof(a[0]); |
93 | V::iterator i = v.insert(v.cbegin() + 10, forward_iterator<const int*>(a), |
94 | forward_iterator<const int*>(a+N)); |
95 | assert(v.size() == sz + N); |
96 | assert(i == v.begin() + 10); |
97 | std::size_t j; |
98 | for (j = 0; j < 10; ++j) |
99 | assert(v[j] == 0); |
100 | for (std::size_t k = 0; k < N; ++j, ++k) |
101 | assert(v[j] == a[k]); |
102 | for (; j < v.size(); ++j) |
103 | assert(v[j] == 0); |
104 | } |
105 | { |
106 | typedef std::vector<int, limited_allocator<int, 308> > V; |
107 | V v(100); |
108 | int a[] = {1, 2, 3, 4, 5}; |
109 | const int N = sizeof(a)/sizeof(a[0]); |
110 | V::iterator i = v.insert(v.cbegin() + 10, cpp17_input_iterator<const int*>(a), |
111 | cpp17_input_iterator<const int*>(a+N)); |
112 | assert(v.size() == 100 + N); |
113 | assert(is_contiguous_container_asan_correct(v)); |
114 | assert(i == v.begin() + 10); |
115 | int j; |
116 | for (j = 0; j < 10; ++j) |
117 | assert(v[j] == 0); |
118 | for (std::size_t k = 0; k < N; ++j, ++k) |
119 | assert(v[j] == a[k]); |
120 | for (; j < 105; ++j) |
121 | assert(v[j] == 0); |
122 | } |
123 | { |
124 | typedef std::vector<int, limited_allocator<int, 300> > V; |
125 | V v(100); |
126 | int a[] = {1, 2, 3, 4, 5}; |
127 | const int N = sizeof(a)/sizeof(a[0]); |
128 | V::iterator i = v.insert(v.cbegin() + 10, forward_iterator<const int*>(a), |
129 | forward_iterator<const int*>(a+N)); |
130 | assert(v.size() == 100 + N); |
131 | assert(is_contiguous_container_asan_correct(v)); |
132 | assert(i == v.begin() + 10); |
133 | int j; |
134 | for (j = 0; j < 10; ++j) |
135 | assert(v[j] == 0); |
136 | for (std::size_t k = 0; k < N; ++j, ++k) |
137 | assert(v[j] == a[k]); |
138 | for (; j < 105; ++j) |
139 | assert(v[j] == 0); |
140 | } |
141 | #if TEST_STD_VER >= 11 |
142 | { |
143 | typedef std::vector<int, min_allocator<int> > V; |
144 | V v(100); |
145 | int a[] = {1, 2, 3, 4, 5}; |
146 | const int N = sizeof(a)/sizeof(a[0]); |
147 | V::iterator i = v.insert(v.cbegin() + 10, cpp17_input_iterator<const int*>(a), |
148 | cpp17_input_iterator<const int*>(a+N)); |
149 | assert(v.size() == 100 + N); |
150 | assert(is_contiguous_container_asan_correct(v)); |
151 | assert(i == v.begin() + 10); |
152 | int j; |
153 | for (j = 0; j < 10; ++j) |
154 | assert(v[j] == 0); |
155 | for (std::size_t k = 0; k < N; ++j, ++k) |
156 | assert(v[j] == a[k]); |
157 | for (; j < 105; ++j) |
158 | assert(v[j] == 0); |
159 | } |
160 | { |
161 | typedef std::vector<int, min_allocator<int> > V; |
162 | V v(100); |
163 | int a[] = {1, 2, 3, 4, 5}; |
164 | const int N = sizeof(a)/sizeof(a[0]); |
165 | V::iterator i = v.insert(v.cbegin() + 10, forward_iterator<const int*>(a), |
166 | forward_iterator<const int*>(a+N)); |
167 | assert(v.size() == 100 + N); |
168 | assert(is_contiguous_container_asan_correct(v)); |
169 | assert(i == v.begin() + 10); |
170 | int j; |
171 | for (j = 0; j < 10; ++j) |
172 | assert(v[j] == 0); |
173 | for (std::size_t k = 0; k < N; ++j, ++k) |
174 | assert(v[j] == a[k]); |
175 | for (; j < 105; ++j) |
176 | assert(v[j] == 0); |
177 | } |
178 | #endif |
179 | |
180 | { |
181 | std::vector<adl::S> s; |
182 | s.insert(s.end(), cpp17_input_iterator<adl::S*>(nullptr), cpp17_input_iterator<adl::S*>(nullptr)); |
183 | } |
184 | |
185 | return true; |
186 | } |
187 | |
188 | int main(int, char**) |
189 | { |
190 | tests(); |
191 | #if TEST_STD_VER > 17 |
192 | static_assert(tests()); |
193 | #endif |
194 | return 0; |
195 | } |
196 | |