[nfc][mlir][scf]: Define scf.for lower/upper bounds can be also negative or zero...
[llvm-project.git] / libcxx / test / std / containers / sequences / deque / deque.modifiers / insert_iter_iter.pass.cpp
blobb99c2b87cc5fe9158c8555e176935cedb8da732e
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 //===----------------------------------------------------------------------===//
9 // REQUIRES: long_tests
10 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 // This test is super slow, in particular with msan or tsan. In order to avoid timeouts and to
13 // spend less time waiting for this particular test to complete we compile with optimizations.
14 // ADDITIONAL_COMPILE_FLAGS(msan): -O1
15 // ADDITIONAL_COMPILE_FLAGS(tsan): -O1
17 // <deque>
19 // template <class InputIterator>
20 // iterator insert (const_iterator p, InputIterator f, InputIterator l);
22 #include "asan_testing.h"
23 #include <deque>
24 #include <cassert>
25 #include <cstddef>
27 #include "test_macros.h"
28 #include "test_iterators.h"
29 #include "MoveOnly.h"
30 #include "test_allocator.h"
31 #include "min_allocator.h"
33 template <class C>
35 make(int size, int start = 0 )
37 const int b = 4096 / sizeof(int);
38 int init = 0;
39 if (start > 0)
41 init = (start+1) / b + ((start+1) % b != 0);
42 init *= b;
43 --init;
45 C c(init, 0);
46 for (int i = 0; i < init-start; ++i)
47 c.pop_back();
48 for (int i = 0; i < size; ++i)
49 c.push_back(i);
50 for (int i = 0; i < start; ++i)
51 c.pop_front();
52 return c;
55 template <class C>
56 void
57 test(int P, const C& c0, const C& c2)
60 typedef typename C::const_iterator CI;
61 typedef cpp17_input_iterator<CI> BCI;
62 C c1 = c0;
63 std::size_t c1_osize = c1.size();
64 CI i = c1.insert(c1.begin() + P, BCI(c2.begin()), BCI(c2.end()));
65 assert(i == c1.begin() + P);
66 assert(c1.size() == c1_osize + c2.size());
67 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
68 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
69 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
70 i = c1.begin();
71 for (int j = 0; j < P; ++j, ++i)
72 assert(*i == j);
73 for (int j = 0; static_cast<std::size_t>(j) < c2.size(); ++j, ++i)
74 assert(*i == j);
75 for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
76 assert(*i == j);
79 typedef typename C::const_iterator CI;
80 typedef forward_iterator<CI> BCI;
81 C c1 = c0;
82 std::size_t c1_osize = c1.size();
83 CI i = c1.insert(c1.begin() + P, BCI(c2.begin()), BCI(c2.end()));
84 assert(i == c1.begin() + P);
85 assert(c1.size() == c1_osize + c2.size());
86 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
87 i = c1.begin();
88 for (int j = 0; j < P; ++j, ++i)
89 assert(*i == j);
90 for (int j = 0; static_cast<std::size_t>(j) < c2.size(); ++j, ++i)
91 assert(*i == j);
92 for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
93 assert(*i == j);
96 typedef typename C::const_iterator CI;
97 typedef bidirectional_iterator<CI> BCI;
98 C c1 = c0;
99 std::size_t c1_osize = c1.size();
100 CI i = c1.insert(c1.begin() + P, BCI(c2.begin()), BCI(c2.end()));
101 assert(i == c1.begin() + P);
102 assert(c1.size() == c1_osize + c2.size());
103 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
104 i = c1.begin();
105 for (int j = 0; j < P; ++j, ++i)
106 assert(*i == j);
107 for (int j = 0; static_cast<std::size_t>(j) < c2.size(); ++j, ++i)
108 assert(*i == j);
109 for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
110 assert(*i == j);
114 template <class C>
115 void
116 testN(int start, int N, int M)
118 for (int i = 0; i <= 3; ++i)
120 if (0 <= i && i <= N)
122 C c1 = make<C>(N, start);
123 C c2 = make<C>(M);
124 test(i, c1, c2);
127 for (int i = M-1; i <= M+1; ++i)
129 if (0 <= i && i <= N)
131 C c1 = make<C>(N, start);
132 C c2 = make<C>(M);
133 test(i, c1, c2);
136 for (int i = N/2-1; i <= N/2+1; ++i)
138 if (0 <= i && i <= N)
140 C c1 = make<C>(N, start);
141 C c2 = make<C>(M);
142 test(i, c1, c2);
145 for (int i = N - M - 1; i <= N - M + 1; ++i)
147 if (0 <= i && i <= N)
149 C c1 = make<C>(N, start);
150 C c2 = make<C>(M);
151 test(i, c1, c2);
154 for (int i = N - M - 1; i <= N - M + 1; ++i)
156 if (0 <= i && i <= N)
158 C c1 = make<C>(N, start);
159 C c2 = make<C>(M);
160 test(i, c1, c2);
163 for (int i = N - 3; i <= N; ++i)
165 if (0 <= i && i <= N)
167 C c1 = make<C>(N, start);
168 C c2 = make<C>(M);
169 test(i, c1, c2);
174 template <class C>
175 void
176 testI(int P, C& c1, const C& c2)
178 typedef typename C::const_iterator CI;
179 typedef cpp17_input_iterator<CI> ICI;
180 std::size_t c1_osize = c1.size();
181 CI i = c1.insert(c1.begin() + P, ICI(c2.begin()), ICI(c2.end()));
182 assert(i == c1.begin() + P);
183 assert(c1.size() == c1_osize + c2.size());
184 assert(static_cast<std::size_t>(std::distance(c1.begin(), c1.end())) == c1.size());
185 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));
186 LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c2));
187 i = c1.begin();
188 for (int j = 0; j < P; ++j, ++i)
189 assert(*i == j);
190 for (int j = 0; static_cast<std::size_t>(j) < c2.size(); ++j, ++i)
191 assert(*i == j);
192 for (int j = P; static_cast<std::size_t>(j) < c1_osize; ++j, ++i)
193 assert(*i == j);
196 template <class C>
197 void
198 testNI(int start, int N, int M)
200 for (int i = 0; i <= 3; ++i)
202 if (0 <= i && i <= N)
204 C c1 = make<C>(N, start);
205 C c2 = make<C>(M);
206 testI(i, c1, c2);
209 for (int i = M-1; i <= M+1; ++i)
211 if (0 <= i && i <= N)
213 C c1 = make<C>(N, start);
214 C c2 = make<C>(M);
215 testI(i, c1, c2);
218 for (int i = N/2-1; i <= N/2+1; ++i)
220 if (0 <= i && i <= N)
222 C c1 = make<C>(N, start);
223 C c2 = make<C>(M);
224 testI(i, c1, c2);
227 for (int i = N - M - 1; i <= N - M + 1; ++i)
229 if (0 <= i && i <= N)
231 C c1 = make<C>(N, start);
232 C c2 = make<C>(M);
233 testI(i, c1, c2);
236 for (int i = N - 3; i <= N; ++i)
238 if (0 <= i && i <= N)
240 C c1 = make<C>(N, start);
241 C c2 = make<C>(M);
242 testI(i, c1, c2);
247 template <class C>
248 void
249 test_move()
251 #if TEST_STD_VER >= 11
252 C c;
253 typedef typename C::const_iterator CI;
255 MoveOnly mo(0);
256 typedef MoveOnly* I;
257 c.insert(c.end(), std::move_iterator<I>(&mo), std::move_iterator<I>(&mo+1));
259 int j = 0;
260 for (CI i = c.begin(); i != c.end(); ++i, (void) ++j)
261 assert(*i == MoveOnly(j));
263 MoveOnly mo(1);
264 typedef cpp17_input_iterator<MoveOnly*> I;
265 c.insert(c.end(), std::move_iterator<I>(I(&mo)), std::move_iterator<I>(I(&mo+1)));
267 j = 0;
268 for (CI i = c.begin(); i != c.end(); ++i, (void) ++j)
269 assert(*i == MoveOnly(j));
270 #endif
273 int main(int, char**)
276 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
277 const int N = sizeof(rng)/sizeof(rng[0]);
278 for (int i = 0; i < N; ++i)
279 for (int j = 0; j < N; ++j)
280 for (int k = 0; k < N; ++k)
281 testN<std::deque<int> >(rng[i], rng[j], rng[k]);
282 testNI<std::deque<int> >(1500, 2000, 1000);
283 #if TEST_STD_VER >= 11
284 test_move<std::deque<MoveOnly, limited_allocator<MoveOnly, 2000> > >();
285 #endif
287 #if TEST_STD_VER >= 11
289 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
290 const int N = sizeof(rng)/sizeof(rng[0]);
291 for (int i = 0; i < N; ++i)
292 for (int j = 0; j < N; ++j)
293 for (int k = 0; k < N; ++k)
294 testN<std::deque<int, min_allocator<int>> >(rng[i], rng[j], rng[k]);
295 testNI<std::deque<int> >(1500, 2000, 1000);
296 test_move<std::deque<MoveOnly, min_allocator<MoveOnly> > >();
299 int rng[] = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};
300 const int N = sizeof(rng)/sizeof(rng[0]);
301 for (int i = 0; i < N; ++i)
302 for (int j = 0; j < N; ++j)
303 for (int k = 0; k < N; ++k)
304 testN<std::deque<int, safe_allocator<int>> >(rng[i], rng[j], rng[k]);
305 testNI<std::deque<int> >(1500, 2000, 1000);
306 test_move<std::deque<MoveOnly, safe_allocator<MoveOnly> > >();
308 #endif
310 return 0;