]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Container static_vector |
2 | // Unit Test | |
3 | ||
4 | // Copyright (c) 2012-2013 Adam Wulkiewicz, Lodz, Poland. | |
5 | // Copyright (c) 2012-2013 Andrew Hundt. | |
6 | ||
7 | // Use, modification and distribution is subject to the Boost Software License, | |
8 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at | |
9 | // http://www.boost.org/LICENSE_1_0.txt) | |
7c673cae FG |
10 | #include <boost/core/lightweight_test.hpp> |
11 | #include <boost/core/no_exceptions_support.hpp> | |
12 | #include <boost/container/vector.hpp> | |
20effc67 | 13 | #include <boost/container/list.hpp> |
7c673cae FG |
14 | #include <boost/container/detail/iterator.hpp> |
15 | #include "../../intrusive/test/iterator_test.hpp" | |
16 | ||
7c673cae FG |
17 | #include "static_vector_test.hpp" |
18 | ||
7c673cae FG |
19 | |
20 | template <typename T, size_t N> | |
21 | void test_ctor_ndc() | |
22 | { | |
23 | static_vector<T, N> s; | |
24 | BOOST_STATIC_ASSERT((static_vector<T, N>::static_capacity) == N); | |
25 | BOOST_TEST_EQ(s.size() , 0u); | |
26 | BOOST_TEST(s.capacity() == N); | |
27 | BOOST_TEST(s.max_size() == N); | |
1e59de90 | 28 | BOOST_TEST_THROWS( (void)s.at(0u), out_of_range_t); |
7c673cae FG |
29 | } |
30 | ||
31 | template <typename T, size_t N> | |
32 | void test_ctor_nc(size_t n) | |
33 | { | |
34 | static_vector<T, N> s(n); | |
35 | BOOST_STATIC_ASSERT((static_vector<T, N>::static_capacity) == N); | |
36 | BOOST_TEST(s.size() == n); | |
37 | BOOST_TEST(s.capacity() == N); | |
38 | BOOST_TEST(s.max_size() == N); | |
1e59de90 | 39 | BOOST_TEST_THROWS( (void)s.at(n), out_of_range_t); |
7c673cae FG |
40 | if ( 1 < n ) |
41 | { | |
20effc67 | 42 | s[0] = 10; |
7c673cae FG |
43 | BOOST_TEST(T(10) == s[0]); |
44 | BOOST_TEST(T(10) == s.at(0)); | |
20effc67 | 45 | s.at(1) = 20; |
7c673cae FG |
46 | BOOST_TEST(T(20) == s[1]); |
47 | BOOST_TEST(T(20) == s.at(1)); | |
48 | } | |
49 | } | |
50 | ||
51 | template <typename T, size_t N> | |
52 | void test_ctor_nd(size_t n, T const& v) | |
53 | { | |
54 | static_vector<T, N> s(n, v); | |
55 | BOOST_STATIC_ASSERT((static_vector<T, N>::static_capacity) == N); | |
56 | BOOST_TEST(s.size() == n); | |
57 | BOOST_TEST(s.capacity() == N); | |
1e59de90 | 58 | BOOST_TEST_THROWS( (void)s.at(n), out_of_range_t); |
7c673cae FG |
59 | if ( 1 < n ) |
60 | { | |
61 | BOOST_TEST(v == s[0]); | |
62 | BOOST_TEST(v == s.at(0)); | |
63 | BOOST_TEST(v == s[1]); | |
64 | BOOST_TEST(v == s.at(1)); | |
65 | s[0] = T(10); | |
66 | BOOST_TEST(T(10) == s[0]); | |
67 | BOOST_TEST(T(10) == s.at(0)); | |
68 | s.at(1) = T(20); | |
69 | BOOST_TEST(T(20) == s[1]); | |
70 | BOOST_TEST(T(20) == s.at(1)); | |
71 | } | |
72 | } | |
73 | ||
74 | void test_support_for_initializer_list() | |
75 | { | |
76 | #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) | |
77 | { | |
20effc67 TL |
78 | typedef static_vector<int, 2> sv_cap_2; |
79 | sv_cap_2 sv = {10, 8}; | |
7c673cae FG |
80 | BOOST_TEST(10 == sv[0]); |
81 | BOOST_TEST(8 == sv[1]); | |
82 | ||
1e59de90 | 83 | BOOST_TEST_THROWS(sv_cap_2({1, 1, 1}), bad_alloc_t); |
7c673cae FG |
84 | } |
85 | ||
86 | { | |
87 | static_vector<int, 2> sv; | |
88 | sv.assign({1, 2}); | |
89 | BOOST_TEST(1 == sv[0]); | |
90 | BOOST_TEST(2 == sv[1]); | |
91 | ||
1e59de90 | 92 | BOOST_TEST_THROWS(sv.assign({1, 2, 3}), bad_alloc_t); |
7c673cae FG |
93 | |
94 | static_vector<int, 3> greaterThanSv = {1, 2, 3}; | |
1e59de90 | 95 | BOOST_TEST_THROWS(sv = greaterThanSv, bad_alloc_t); |
7c673cae FG |
96 | } |
97 | ||
98 | { | |
99 | static_vector<int, 2> sv; | |
100 | sv.insert(sv.begin(), {99, 95}); | |
101 | BOOST_TEST(99 == sv[0]); | |
102 | BOOST_TEST(95 == sv[1]); | |
103 | ||
1e59de90 | 104 | BOOST_TEST_THROWS(sv.insert(sv.begin(), {101, 102, 103}), bad_alloc_t); |
7c673cae FG |
105 | } |
106 | #endif | |
107 | } | |
108 | ||
109 | template <typename T, size_t N> | |
110 | void test_resize_nc(size_t n) | |
111 | { | |
112 | static_vector<T, N> s; | |
113 | ||
114 | s.resize(n); | |
115 | BOOST_TEST(s.size() == n); | |
116 | BOOST_TEST(s.capacity() == N); | |
1e59de90 | 117 | BOOST_TEST_THROWS( (void)s.at(n), out_of_range_t); |
7c673cae FG |
118 | if ( 1 < n ) |
119 | { | |
20effc67 | 120 | s[0] = 10; |
7c673cae FG |
121 | BOOST_TEST(T(10) == s[0]); |
122 | BOOST_TEST(T(10) == s.at(0)); | |
20effc67 | 123 | s.at(1) = 20; |
7c673cae FG |
124 | BOOST_TEST(T(20) == s[1]); |
125 | BOOST_TEST(T(20) == s.at(1)); | |
126 | } | |
127 | } | |
128 | ||
129 | template <typename T, size_t N> | |
130 | void test_resize_nd(size_t n, T const& v) | |
131 | { | |
132 | static_vector<T, N> s; | |
133 | ||
134 | s.resize(n, v); | |
135 | BOOST_TEST(s.size() == n); | |
136 | BOOST_TEST(s.capacity() == N); | |
1e59de90 | 137 | BOOST_TEST_THROWS( (void)s.at(n), out_of_range_t); |
7c673cae FG |
138 | if ( 1 < n ) |
139 | { | |
140 | BOOST_TEST(v == s[0]); | |
141 | BOOST_TEST(v == s.at(0)); | |
142 | BOOST_TEST(v == s[1]); | |
143 | BOOST_TEST(v == s.at(1)); | |
144 | s[0] = T(10); | |
145 | BOOST_TEST(T(10) == s[0]); | |
146 | BOOST_TEST(T(10) == s.at(0)); | |
147 | s.at(1) = T(20); | |
148 | BOOST_TEST(T(20) == s[1]); | |
149 | BOOST_TEST(T(20) == s.at(1)); | |
150 | } | |
151 | } | |
152 | ||
153 | template <typename T, size_t N> | |
154 | void test_push_back_nd() | |
155 | { | |
156 | static_vector<T, N> s; | |
157 | ||
158 | BOOST_TEST(s.size() == 0); | |
1e59de90 | 159 | BOOST_TEST_THROWS( (void)s.at(0), out_of_range_t); |
7c673cae FG |
160 | |
161 | for ( size_t i = 0 ; i < N ; ++i ) | |
162 | { | |
20effc67 | 163 | T t(static_cast<int>(i)); |
7c673cae FG |
164 | s.push_back(t); |
165 | BOOST_TEST(s.size() == i + 1); | |
1e59de90 | 166 | BOOST_TEST_THROWS( (void)s.at(i + 1), out_of_range_t); |
20effc67 TL |
167 | BOOST_TEST(T((int)i) == s.at(i)); |
168 | BOOST_TEST(T((int)i) == s[i]); | |
169 | BOOST_TEST(T((int)i) == s.back()); | |
7c673cae | 170 | BOOST_TEST(T(0) == s.front()); |
20effc67 | 171 | BOOST_TEST(T((int)i) == *(s.data() + i)); |
7c673cae FG |
172 | } |
173 | } | |
174 | ||
175 | template <typename T, size_t N> | |
176 | void test_pop_back_nd() | |
177 | { | |
178 | static_vector<T, N> s; | |
179 | ||
180 | for ( size_t i = 0 ; i < N ; ++i ) | |
181 | { | |
20effc67 | 182 | T t((int)i); |
7c673cae FG |
183 | s.push_back(t); |
184 | } | |
185 | ||
186 | for ( size_t i = N ; i > 1 ; --i ) | |
187 | { | |
188 | s.pop_back(); | |
189 | BOOST_TEST(s.size() == i - 1); | |
1e59de90 | 190 | BOOST_TEST_THROWS( (void)s.at(i - 1), out_of_range_t); |
20effc67 TL |
191 | BOOST_TEST(T((int)i - 2) == s.at(i - 2)); |
192 | BOOST_TEST(T((int)i - 2) == s[i - 2]); | |
193 | BOOST_TEST(T((int)i - 2) == s.back()); | |
7c673cae FG |
194 | BOOST_TEST(T(0) == s.front()); |
195 | } | |
196 | } | |
197 | ||
198 | template <typename It1, typename It2> | |
199 | void test_compare_ranges(It1 first1, It1 last1, It2 first2, It2 last2) | |
200 | { | |
1e59de90 | 201 | BOOST_TEST(boost::container::iterator_udistance(first1, last1) == boost::container::iterator_udistance(first2, last2)); |
7c673cae FG |
202 | for ( ; first1 != last1 && first2 != last2 ; ++first1, ++first2 ) |
203 | BOOST_TEST(*first1 == *first2); | |
204 | } | |
205 | ||
206 | template <typename T, size_t N, typename C> | |
207 | void test_copy_and_assign(C const& c) | |
208 | { | |
209 | { | |
210 | static_vector<T, N> s(c.begin(), c.end()); | |
211 | BOOST_TEST(s.size() == c.size()); | |
212 | test_compare_ranges(s.begin(), s.end(), c.begin(), c.end()); | |
213 | } | |
214 | { | |
215 | static_vector<T, N> s; | |
216 | BOOST_TEST(0 == s.size()); | |
217 | s.assign(c.begin(), c.end()); | |
218 | BOOST_TEST(s.size() == c.size()); | |
219 | test_compare_ranges(s.begin(), s.end(), c.begin(), c.end()); | |
220 | } | |
221 | } | |
222 | ||
223 | template <typename T, size_t N> | |
224 | void test_copy_and_assign_nd(T const& val) | |
225 | { | |
226 | static_vector<T, N> s; | |
20effc67 TL |
227 | vector<T> v; |
228 | list<T> l; | |
7c673cae FG |
229 | |
230 | for ( size_t i = 0 ; i < N ; ++i ) | |
231 | { | |
20effc67 | 232 | T t((int)i); |
7c673cae FG |
233 | s.push_back(t); |
234 | v.push_back(t); | |
235 | l.push_back(t); | |
236 | } | |
237 | // copy ctor | |
238 | { | |
239 | static_vector<T, N> s1(s); | |
240 | BOOST_TEST(s.size() == s1.size()); | |
241 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); | |
242 | } | |
243 | // copy assignment | |
244 | { | |
245 | static_vector<T, N> s1; | |
246 | BOOST_TEST(0 == s1.size()); | |
247 | s1 = s; | |
248 | BOOST_TEST(s.size() == s1.size()); | |
249 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); | |
250 | } | |
251 | ||
252 | // ctor(Iter, Iter) and assign(Iter, Iter) | |
253 | test_copy_and_assign<T, N>(s); | |
254 | test_copy_and_assign<T, N>(v); | |
255 | test_copy_and_assign<T, N>(l); | |
256 | ||
257 | // assign(N, V) | |
258 | { | |
259 | static_vector<T, N> s1(s); | |
260 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); | |
20effc67 | 261 | vector<T> a(N, val); |
7c673cae FG |
262 | s1.assign(N, val); |
263 | test_compare_ranges(a.begin(), a.end(), s1.begin(), s1.end()); | |
264 | } | |
7c673cae FG |
265 | } |
266 | ||
267 | template <typename T, size_t N> | |
268 | void test_iterators_nd() | |
269 | { | |
270 | static_vector<T, N> s; | |
20effc67 | 271 | vector<T> v; |
7c673cae FG |
272 | |
273 | for ( size_t i = 0 ; i < N ; ++i ) | |
274 | { | |
20effc67 TL |
275 | s.push_back(T((int)i)); |
276 | v.push_back(T((int)i)); | |
7c673cae FG |
277 | } |
278 | ||
279 | test_compare_ranges(s.begin(), s.end(), v.begin(), v.end()); | |
280 | test_compare_ranges(s.rbegin(), s.rend(), v.rbegin(), v.rend()); | |
281 | ||
282 | s.assign(v.rbegin(), v.rend()); | |
283 | ||
284 | test_compare_ranges(s.begin(), s.end(), v.rbegin(), v.rend()); | |
285 | test_compare_ranges(s.rbegin(), s.rend(), v.begin(), v.end()); | |
286 | } | |
287 | ||
288 | template <typename T, size_t N> | |
289 | void test_erase_nd() | |
290 | { | |
291 | static_vector<T, N> s; | |
292 | typedef typename static_vector<T, N>::iterator It; | |
1e59de90 | 293 | typedef typename static_vector<T, N>::difference_type dif_t; |
7c673cae FG |
294 | |
295 | for ( size_t i = 0 ; i < N ; ++i ) | |
20effc67 | 296 | s.push_back(T((int)i)); |
7c673cae FG |
297 | |
298 | // erase(pos) | |
299 | { | |
300 | for ( size_t i = 0 ; i < N ; ++i ) | |
301 | { | |
302 | static_vector<T, N> s1(s); | |
1e59de90 TL |
303 | It it = s1.erase(s1.begin() + dif_t(i)); |
304 | BOOST_TEST(s1.begin() + dif_t(i) == it); | |
7c673cae FG |
305 | BOOST_TEST(s1.size() == N - 1); |
306 | for ( size_t j = 0 ; j < i ; ++j ) | |
20effc67 | 307 | BOOST_TEST(s1[j] == T((int)j)); |
7c673cae | 308 | for ( size_t j = i+1 ; j < N ; ++j ) |
20effc67 | 309 | BOOST_TEST(s1[j-1] == T((int)j)); |
7c673cae FG |
310 | } |
311 | } | |
312 | // erase(first, last) | |
313 | { | |
314 | size_t n = N/3; | |
315 | for ( size_t i = 0 ; i <= N ; ++i ) | |
316 | { | |
317 | static_vector<T, N> s1(s); | |
318 | size_t removed = i + n < N ? n : N - i; | |
1e59de90 TL |
319 | It it = s1.erase(s1.begin() + dif_t(i), s1.begin() + dif_t(i + removed)); |
320 | BOOST_TEST(s1.begin() + dif_t(i) == it); | |
7c673cae FG |
321 | BOOST_TEST(s1.size() == N - removed); |
322 | for ( size_t j = 0 ; j < i ; ++j ) | |
20effc67 | 323 | BOOST_TEST(s1[j] == T((int)j)); |
7c673cae | 324 | for ( size_t j = i+n ; j < N ; ++j ) |
20effc67 | 325 | BOOST_TEST(s1[j-n] == T((int)j)); |
7c673cae FG |
326 | } |
327 | } | |
328 | } | |
329 | ||
330 | template <typename T, size_t N, typename SV, typename C> | |
331 | void test_insert(SV const& s, C const& c) | |
332 | { | |
333 | size_t h = N/2; | |
1e59de90 TL |
334 | size_t n = size_t(float(h)/1.5f); |
335 | typedef typename static_vector<T, N>::difference_type dif_t; | |
7c673cae FG |
336 | |
337 | for ( size_t i = 0 ; i <= h ; ++i ) | |
338 | { | |
339 | static_vector<T, N> s1(s); | |
340 | ||
341 | typename C::const_iterator it = c.begin(); | |
1e59de90 | 342 | boost::container::iterator_uadvance(it, n); |
7c673cae | 343 | typename static_vector<T, N>::iterator |
1e59de90 | 344 | it1 = s1.insert(s1.begin() + dif_t(i), c.begin(), it); |
7c673cae | 345 | |
1e59de90 | 346 | BOOST_TEST(s1.begin() + dif_t(i) == it1); |
7c673cae FG |
347 | BOOST_TEST(s1.size() == h+n); |
348 | for ( size_t j = 0 ; j < i ; ++j ) | |
20effc67 | 349 | BOOST_TEST(s1[j] == T((int)j)); |
7c673cae | 350 | for ( size_t j = 0 ; j < n ; ++j ) |
20effc67 | 351 | BOOST_TEST(s1[j+i] == T(100 + (int)j)); |
7c673cae | 352 | for ( size_t j = 0 ; j < h-i ; ++j ) |
20effc67 | 353 | BOOST_TEST(s1[j+i+n] == T((int)j+(int)i)); |
7c673cae FG |
354 | } |
355 | } | |
356 | ||
357 | template <typename T, size_t N> | |
358 | void test_insert_nd(T const& val) | |
359 | { | |
360 | size_t h = N/2; | |
361 | ||
362 | static_vector<T, N> s, ss; | |
20effc67 TL |
363 | vector<T> v; |
364 | list<T> l; | |
7c673cae FG |
365 | |
366 | typedef typename static_vector<T, N>::iterator It; | |
1e59de90 | 367 | typedef typename static_vector<T, N>::difference_type dif_t; |
7c673cae FG |
368 | |
369 | for ( size_t i = 0 ; i < h ; ++i ) | |
370 | { | |
20effc67 TL |
371 | s.push_back(T((int)i)); |
372 | ss.push_back(T(100 + (int)i)); | |
373 | v.push_back(T(100 + (int)i)); | |
374 | l.push_back(T(100 + (int)i)); | |
7c673cae FG |
375 | } |
376 | ||
377 | // insert(pos, val) | |
378 | { | |
379 | for ( size_t i = 0 ; i <= h ; ++i ) | |
380 | { | |
381 | static_vector<T, N> s1(s); | |
1e59de90 TL |
382 | It it = s1.insert(s1.begin() + dif_t(i), val); |
383 | BOOST_TEST(s1.begin() + dif_t(i) == it); | |
7c673cae FG |
384 | BOOST_TEST(s1.size() == h+1); |
385 | for ( size_t j = 0 ; j < i ; ++j ) | |
20effc67 | 386 | BOOST_TEST(s1[j] == T((int)j)); |
7c673cae FG |
387 | BOOST_TEST(s1[i] == val); |
388 | for ( size_t j = 0 ; j < h-i ; ++j ) | |
20effc67 | 389 | BOOST_TEST(s1[j+i+1] == T((int)j+(int)i)); |
7c673cae FG |
390 | } |
391 | } | |
392 | // insert(pos, n, val) | |
393 | { | |
1e59de90 | 394 | size_t n = size_t(float(h)/1.5f); |
7c673cae FG |
395 | for ( size_t i = 0 ; i <= h ; ++i ) |
396 | { | |
397 | static_vector<T, N> s1(s); | |
1e59de90 TL |
398 | It it = s1.insert(s1.begin() + dif_t(i), n, val); |
399 | BOOST_TEST(s1.begin() + dif_t(i) == it); | |
7c673cae FG |
400 | BOOST_TEST(s1.size() == h+n); |
401 | for ( size_t j = 0 ; j < i ; ++j ) | |
20effc67 | 402 | BOOST_TEST(s1[j] == T((int)j)); |
7c673cae FG |
403 | for ( size_t j = 0 ; j < n ; ++j ) |
404 | BOOST_TEST(s1[j+i] == val); | |
405 | for ( size_t j = 0 ; j < h-i ; ++j ) | |
20effc67 | 406 | BOOST_TEST(s1[j+i+n] == T((int)j+(int)i)); |
7c673cae FG |
407 | } |
408 | } | |
409 | // insert(pos, first, last) | |
410 | test_insert<T, N>(s, ss); | |
411 | test_insert<T, N>(s, v); | |
412 | test_insert<T, N>(s, l); | |
7c673cae FG |
413 | } |
414 | ||
415 | template <typename T> | |
416 | void test_capacity_0_nd() | |
417 | { | |
418 | static_vector<T, 10> v(5u, T(0)); | |
419 | ||
20effc67 TL |
420 | typedef static_vector<T, 0> static_vector_0_t; |
421 | static_vector_0_t s; | |
7c673cae FG |
422 | BOOST_TEST(s.size() == 0); |
423 | BOOST_TEST(s.capacity() == 0); | |
1e59de90 TL |
424 | BOOST_TEST_THROWS((void)s.at(0), out_of_range_t); |
425 | BOOST_TEST_THROWS(s.resize(5u, T(0)), bad_alloc_t); | |
426 | BOOST_TEST_THROWS(s.push_back(T(0)), bad_alloc_t); | |
427 | BOOST_TEST_THROWS(s.insert(s.end(), T(0)), bad_alloc_t); | |
428 | BOOST_TEST_THROWS(s.insert(s.end(), v.begin(), v.end()), bad_alloc_t); | |
429 | BOOST_TEST_THROWS(s.assign(v.begin(), v.end()), bad_alloc_t); | |
430 | BOOST_TEST_THROWS(s.assign(5u, T(0)), bad_alloc_t); | |
431 | BOOST_TEST_THROWS(s.assign(5u, T(0)), bad_alloc_t); | |
432 | BOOST_TEST_THROWS(static_vector_0_t s2(v.begin(), v.end()), bad_alloc_t); | |
433 | BOOST_TEST_THROWS(static_vector_0_t s1(5u, T(0)), bad_alloc_t); | |
7c673cae FG |
434 | } |
435 | ||
436 | template <typename T, size_t N> | |
437 | void test_exceptions_nd() | |
438 | { | |
439 | static_vector<T, N> v(N, T(0)); | |
20effc67 TL |
440 | typedef static_vector<T, N/2> static_vector_n_half_t; |
441 | static_vector_n_half_t s(N/2, T(0)); | |
7c673cae | 442 | |
1e59de90 TL |
443 | BOOST_TEST_THROWS(s.resize(N, T(0)), bad_alloc_t); |
444 | BOOST_TEST_THROWS(s.push_back(T(0)), bad_alloc_t); | |
445 | BOOST_TEST_THROWS(s.insert(s.end(), T(0)), bad_alloc_t); | |
446 | BOOST_TEST_THROWS(s.insert(s.end(), 1, T(0)), bad_alloc_t); | |
447 | BOOST_TEST_THROWS(s.insert(s.end(), v.begin(), v.end()), bad_alloc_t); | |
448 | BOOST_TEST_THROWS(s.assign(v.begin(), v.end()), bad_alloc_t); | |
449 | BOOST_TEST_THROWS(s.assign(N, T(0)), bad_alloc_t); | |
450 | BOOST_TEST_THROWS(static_vector_n_half_t s2(v.begin(), v.end()), bad_alloc_t); | |
451 | BOOST_TEST_THROWS(static_vector_n_half_t s1(N/2+1, T(0)), bad_alloc_t); | |
7c673cae FG |
452 | } |
453 | ||
454 | template <typename T, size_t N> | |
455 | void test_swap_and_move_nd() | |
456 | { | |
457 | { | |
458 | static_vector<T, N> v1, v2, v3, v4; | |
459 | static_vector<T, N> s1, s2; | |
460 | static_vector<T, N> s4; | |
461 | ||
462 | for (size_t i = 0 ; i < N ; ++i ) | |
463 | { | |
20effc67 TL |
464 | v1.push_back(T((int)i)); |
465 | v2.push_back(T((int)i)); | |
466 | v3.push_back(T((int)i)); | |
467 | v4.push_back(T((int)i)); | |
7c673cae FG |
468 | } |
469 | for (size_t i = 0 ; i < N/2 ; ++i ) | |
470 | { | |
20effc67 TL |
471 | s1.push_back(T(100 + (int)i)); |
472 | s2.push_back(T(100 + (int)i)); | |
473 | s4.push_back(T(100 + (int)i)); | |
7c673cae FG |
474 | } |
475 | ||
476 | s1.swap(v1); | |
477 | s2 = boost::move(v2); | |
478 | static_vector<T, N> s3(boost::move(v3)); | |
479 | s4.swap(v4); | |
480 | ||
481 | BOOST_TEST(v1.size() == N/2); | |
482 | BOOST_TEST(s1.size() == N); | |
20effc67 | 483 | BOOST_TEST(v2.size() == 0); |
7c673cae | 484 | BOOST_TEST(s2.size() == N); |
20effc67 | 485 | BOOST_TEST(v3.size() == 0); |
7c673cae FG |
486 | BOOST_TEST(s3.size() == N); |
487 | BOOST_TEST(v4.size() == N/2); | |
488 | BOOST_TEST(s4.size() == N); | |
489 | for (size_t i = 0 ; i < N/2 ; ++i ) | |
490 | { | |
20effc67 TL |
491 | BOOST_TEST(v1[i] == T(100 + (int)i)); |
492 | BOOST_TEST(v4[i] == T(100 + (int)i)); | |
7c673cae FG |
493 | } |
494 | for (size_t i = 0 ; i < N ; ++i ) | |
495 | { | |
20effc67 TL |
496 | BOOST_TEST(s1[i] == T((int)i)); |
497 | BOOST_TEST(s2[i] == T((int)i)); | |
498 | BOOST_TEST(s3[i] == T((int)i)); | |
499 | BOOST_TEST(s4[i] == T((int)i)); | |
7c673cae FG |
500 | } |
501 | } | |
502 | { | |
503 | static_vector<T, N> v1, v2, v3; | |
504 | static_vector<T, N/2> s1, s2, s3; | |
505 | ||
506 | for (size_t i = 0 ; i < N/2 ; ++i ) | |
507 | { | |
20effc67 TL |
508 | v1.push_back(T((int)i)); |
509 | v2.push_back(T((int)i)); | |
510 | v3.push_back(T((int)i)); | |
7c673cae FG |
511 | } |
512 | for (size_t i = 0 ; i < N/3 ; ++i ) | |
513 | { | |
20effc67 TL |
514 | s1.push_back(T(100 + (int)i)); |
515 | s2.push_back(T(100 + (int)i)); | |
7c673cae FG |
516 | } |
517 | ||
518 | s1.swap(v1); | |
519 | s3 = v2; | |
520 | s2 = boost::move(v2); | |
521 | static_vector<T, N/2> s4(boost::move(v3)); | |
522 | ||
523 | BOOST_TEST(v1.size() == N/3); | |
524 | BOOST_TEST(s1.size() == N/2); | |
525 | //iG moving does not imply emptying source | |
526 | //BOOST_TEST(v2.size() == 0); | |
527 | BOOST_TEST(s2.size() == N/2); | |
528 | BOOST_TEST(s3.size() == N/2); | |
529 | //iG moving does not imply emptying source | |
530 | //BOOST_TEST(v3.size() == 0); | |
531 | BOOST_TEST(s4.size() == N/2); | |
532 | for (size_t i = 0 ; i < N/3 ; ++i ) | |
20effc67 | 533 | BOOST_TEST(v1[i] == T(100 + (int)i)); |
7c673cae FG |
534 | for (size_t i = 0 ; i < N/2 ; ++i ) |
535 | { | |
20effc67 TL |
536 | BOOST_TEST(s1[i] == T((int)i)); |
537 | BOOST_TEST(s2[i] == T((int)i)); | |
538 | BOOST_TEST(s3[i] == T((int)i)); | |
539 | BOOST_TEST(s4[i] == T((int)i)); | |
7c673cae FG |
540 | } |
541 | } | |
542 | { | |
543 | typedef static_vector<T, N/2> small_vector_t; | |
544 | static_vector<T, N> v(N, T(0)); | |
545 | small_vector_t s(N/2, T(1)); | |
1e59de90 | 546 | BOOST_TEST_THROWS(s.swap(v), bad_alloc_t); |
7c673cae | 547 | v.resize(N, T(0)); |
1e59de90 TL |
548 | BOOST_TEST_THROWS(s = boost::move(v), bad_alloc_t); |
549 | BOOST_TEST_THROWS(s = v, bad_alloc_t); | |
7c673cae | 550 | v.resize(N, T(0)); |
1e59de90 | 551 | BOOST_TEST_THROWS(small_vector_t s2(boost::move(v)), bad_alloc_t); |
7c673cae FG |
552 | } |
553 | } | |
554 | ||
555 | template <typename T, size_t N> | |
556 | void test_emplace_0p() | |
557 | { | |
558 | //emplace_back() | |
559 | { | |
560 | static_vector<T, N> v; | |
561 | ||
562 | for (int i = 0 ; i < int(N) ; ++i ) | |
563 | v.emplace_back(); | |
564 | BOOST_TEST(v.size() == N); | |
1e59de90 | 565 | BOOST_TEST_THROWS(v.emplace_back(), bad_alloc_t); |
7c673cae FG |
566 | } |
567 | } | |
568 | ||
569 | template <typename T, size_t N> | |
570 | void test_emplace_2p() | |
571 | { | |
572 | //emplace_back(pos, int, int) | |
573 | { | |
574 | static_vector<T, N> v; | |
575 | ||
1e59de90 | 576 | for (int i = 0 ; i < (int)N ; ++i ) |
7c673cae FG |
577 | v.emplace_back(i, 100 + i); |
578 | BOOST_TEST(v.size() == N); | |
1e59de90 | 579 | BOOST_TEST_THROWS(v.emplace_back((int)N, 100 + (int)N), bad_alloc_t); |
7c673cae | 580 | BOOST_TEST(v.size() == N); |
1e59de90 TL |
581 | for (int i = 0 ; i < (int)N ; ++i ) |
582 | BOOST_TEST(v[std::size_t(i)] == T(i, 100 + i)); | |
7c673cae FG |
583 | } |
584 | ||
585 | // emplace(pos, int, int) | |
586 | { | |
587 | typedef typename static_vector<T, N>::iterator It; | |
1e59de90 | 588 | typedef typename static_vector<T, N>::difference_type dif_t; |
7c673cae FG |
589 | |
590 | int h = N / 2; | |
591 | ||
592 | static_vector<T, N> v; | |
593 | for ( int i = 0 ; i < h ; ++i ) | |
594 | v.emplace_back(i, 100 + i); | |
595 | ||
596 | for ( int i = 0 ; i <= h ; ++i ) | |
597 | { | |
598 | static_vector<T, N> vv(v); | |
1e59de90 TL |
599 | It it = vv.emplace(vv.begin() + dif_t(i), i+100, i+200); |
600 | BOOST_TEST(vv.begin() + dif_t(i) == it); | |
7c673cae FG |
601 | BOOST_TEST(vv.size() == size_t(h+1)); |
602 | for ( int j = 0 ; j < i ; ++j ) | |
1e59de90 TL |
603 | BOOST_TEST(vv[std::size_t(j)] == T(j, j+100)); |
604 | BOOST_TEST(vv[std::size_t(i)] == T(i+100, i+200)); | |
7c673cae | 605 | for ( int j = 0 ; j < h-i ; ++j ) |
1e59de90 | 606 | BOOST_TEST(vv[std::size_t(j+i+1)] == T(j+i, j+i+100)); |
7c673cae FG |
607 | } |
608 | } | |
609 | } | |
610 | ||
611 | template <typename T, size_t N> | |
612 | void test_sv_elem(T const& t) | |
613 | { | |
614 | typedef static_vector<T, N> V; | |
615 | ||
616 | static_vector<V, N> v; | |
617 | ||
618 | v.push_back(V(N/2, t)); | |
619 | V vvv(N/2, t); | |
620 | v.push_back(boost::move(vvv)); | |
621 | v.insert(v.begin(), V(N/2, t)); | |
622 | v.insert(v.end(), V(N/2, t)); | |
623 | v.emplace_back(N/2, t); | |
624 | } | |
625 | ||
626 | bool default_init_test()//Test for default initialization | |
627 | { | |
7c673cae FG |
628 | const std::size_t Capacity = 100; |
629 | ||
b32b8144 FG |
630 | typedef static_vector<int, Capacity> di_vector_t; |
631 | ||
632 | { | |
633 | di_vector_t v(Capacity, default_init); | |
634 | } | |
7c673cae FG |
635 | { |
636 | di_vector_t v; | |
637 | int *p = v.data(); | |
638 | ||
639 | for(std::size_t i = 0; i != Capacity; ++i, ++p){ | |
640 | *p = static_cast<int>(i); | |
641 | } | |
642 | ||
b32b8144 | 643 | //Destroy the vector, p still pointing to the storage |
7c673cae FG |
644 | v.~di_vector_t(); |
645 | ||
646 | di_vector_t &rv = *::new(&v)di_vector_t(Capacity, default_init); | |
647 | di_vector_t::iterator it = rv.begin(); | |
648 | ||
649 | for(std::size_t i = 0; i != Capacity; ++i, ++it){ | |
650 | if(*it != static_cast<int>(i)) | |
651 | return false; | |
652 | } | |
653 | ||
654 | v.~di_vector_t(); | |
655 | } | |
656 | { | |
657 | di_vector_t v; | |
658 | ||
659 | int *p = v.data(); | |
660 | for(std::size_t i = 0; i != Capacity; ++i, ++p){ | |
661 | *p = static_cast<int>(i+100); | |
662 | } | |
663 | ||
664 | v.resize(Capacity, default_init); | |
665 | ||
666 | di_vector_t::iterator it = v.begin(); | |
667 | for(std::size_t i = 0; i != Capacity; ++i, ++it){ | |
668 | if(*it != static_cast<int>(i+100)) | |
669 | return false; | |
670 | } | |
671 | } | |
672 | ||
673 | return true; | |
674 | } | |
675 | ||
676 | ||
677 | int main(int, char* []) | |
678 | { | |
679 | using boost::container::test::movable_and_copyable_int; | |
680 | using boost::container::test::produce_movable_and_copyable_int; | |
681 | BOOST_TEST(counting_value::count() == 0); | |
682 | ||
683 | test_ctor_ndc<int, 10>(); | |
684 | test_ctor_ndc<value_ndc, 10>(); | |
685 | test_ctor_ndc<counting_value, 10>(); | |
686 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
687 | test_ctor_ndc<movable_and_copyable_int, 10>(); |
688 | ||
689 | test_ctor_nc<int, 10>(5); | |
690 | test_ctor_nc<value_nc, 10>(5); | |
691 | test_ctor_nc<counting_value, 10>(5); | |
692 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
693 | test_ctor_nc<movable_and_copyable_int, 10>(5); |
694 | ||
695 | test_ctor_nd<int, 10>(5, 1); | |
696 | test_ctor_nd<value_nd, 10>(5, value_nd(1)); | |
697 | test_ctor_nd<counting_value, 10>(5, counting_value(1)); | |
698 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
699 | test_ctor_nd<movable_and_copyable_int, 10>(5, produce_movable_and_copyable_int()); |
700 | ||
701 | test_resize_nc<int, 10>(5); | |
702 | test_resize_nc<value_nc, 10>(5); | |
703 | test_resize_nc<counting_value, 10>(5); | |
704 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
705 | test_resize_nc<movable_and_copyable_int, 10>(5); |
706 | ||
707 | test_resize_nd<int, 10>(5, 1); | |
708 | test_resize_nd<value_nd, 10>(5, value_nd(1)); | |
709 | test_resize_nd<counting_value, 10>(5, counting_value(1)); | |
710 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
711 | test_resize_nd<movable_and_copyable_int, 10>(5, produce_movable_and_copyable_int()); |
712 | ||
713 | test_push_back_nd<int, 10>(); | |
714 | test_push_back_nd<value_nd, 10>(); | |
715 | test_push_back_nd<counting_value, 10>(); | |
716 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
717 | test_push_back_nd<movable_and_copyable_int, 10>(); |
718 | ||
719 | test_pop_back_nd<int, 10>(); | |
720 | test_pop_back_nd<value_nd, 10>(); | |
721 | test_pop_back_nd<counting_value, 10>(); | |
722 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
723 | test_pop_back_nd<movable_and_copyable_int, 10>(); |
724 | ||
725 | test_copy_and_assign_nd<int, 10>(1); | |
726 | test_copy_and_assign_nd<value_nd, 10>(value_nd(1)); | |
727 | test_copy_and_assign_nd<counting_value, 10>(counting_value(1)); | |
728 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
729 | test_copy_and_assign_nd<movable_and_copyable_int, 10>(produce_movable_and_copyable_int()); |
730 | ||
731 | test_iterators_nd<int, 10>(); | |
732 | test_iterators_nd<value_nd, 10>(); | |
733 | test_iterators_nd<counting_value, 10>(); | |
734 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
735 | test_iterators_nd<movable_and_copyable_int, 10>(); |
736 | ||
737 | test_erase_nd<int, 10>(); | |
738 | test_erase_nd<value_nd, 10>(); | |
739 | test_erase_nd<counting_value, 10>(); | |
740 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
741 | test_erase_nd<movable_and_copyable_int, 10>(); |
742 | ||
743 | test_insert_nd<int, 10>(50); | |
744 | test_insert_nd<value_nd, 10>(value_nd(50)); | |
745 | test_insert_nd<counting_value, 10>(counting_value(50)); | |
746 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
747 | test_insert_nd<movable_and_copyable_int, 10>(produce_movable_and_copyable_int()); |
748 | ||
749 | test_capacity_0_nd<int>(); | |
750 | test_capacity_0_nd<value_nd>(); | |
751 | test_capacity_0_nd<counting_value>(); | |
752 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
753 | test_capacity_0_nd<movable_and_copyable_int>(); |
754 | ||
755 | test_exceptions_nd<int, 10>(); | |
756 | test_exceptions_nd<value_nd, 10>(); | |
757 | test_exceptions_nd<counting_value, 10>(); | |
758 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
759 | test_exceptions_nd<movable_and_copyable_int, 10>(); |
760 | ||
761 | test_swap_and_move_nd<int, 10>(); | |
762 | test_swap_and_move_nd<value_nd, 10>(); | |
763 | test_swap_and_move_nd<counting_value, 10>(); | |
764 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
765 | test_swap_and_move_nd<movable_and_copyable_int, 10>(); |
766 | ||
767 | test_emplace_0p<counting_value, 10>(); | |
768 | BOOST_TEST(counting_value::count() == 0); | |
769 | ||
770 | test_emplace_2p<counting_value, 10>(); | |
771 | BOOST_TEST(counting_value::count() == 0); | |
772 | ||
773 | test_sv_elem<int, 10>(50); | |
774 | test_sv_elem<value_nd, 10>(value_nd(50)); | |
775 | test_sv_elem<counting_value, 10>(counting_value(50)); | |
776 | BOOST_TEST(counting_value::count() == 0); | |
7c673cae FG |
777 | test_sv_elem<movable_and_copyable_int, 10>(movable_and_copyable_int(50)); |
778 | ||
779 | BOOST_TEST(default_init_test() == true); | |
780 | ||
781 | test_support_for_initializer_list(); | |
782 | ||
783 | //////////////////////////////////// | |
784 | // Iterator testing | |
785 | //////////////////////////////////// | |
786 | { | |
787 | typedef boost::container::static_vector<int, 3> cont_int; | |
788 | cont_int a; a.push_back(0); a.push_back(1); a.push_back(2); | |
789 | boost::intrusive::test::test_iterator_random< cont_int >(a); | |
790 | } | |
791 | ||
792 | return boost::report_errors(); | |
793 | } | |
794 | ||
20effc67 TL |
795 | /* |
796 | ||
797 | #include <boost/container/small_vector.hpp> | |
798 | #include <type_traits> | |
799 | ||
800 | struct S_trivial { | |
801 | int i; | |
802 | }; | |
803 | static_assert(std::is_nothrow_move_constructible<S_trivial>::value, ""); | |
804 | static_assert(std::is_nothrow_move_assignable<S_trivial>::value, ""); | |
805 | ||
806 | struct S1 { | |
807 | int i = 0; | |
808 | }; | |
809 | static_assert(std::is_nothrow_move_constructible<S1>::value, ""); | |
810 | static_assert(std::is_nothrow_move_assignable<S1>::value, ""); | |
811 | ||
812 | struct S2 { | |
813 | int i = 0; | |
814 | S2(S2&&) noexcept; | |
815 | S2& operator=(S2&&) noexcept; | |
816 | }; | |
817 | static_assert(std::is_nothrow_move_constructible<S2>::value, ""); | |
818 | static_assert(std::is_nothrow_move_assignable<S2>::value, ""); | |
819 | ||
820 | // Succeed | |
821 | static_assert(std::is_nothrow_move_constructible<boost::container::small_vector<S_trivial, 1>>::value, ""); | |
822 | static_assert(std::is_nothrow_move_assignable<boost::container::small_vector<S_trivial, 1>>::value, ""); | |
823 | ||
824 | // Fail | |
825 | static_assert(std::is_nothrow_move_constructible<boost::container::small_vector<S1, 1>>::value, ""); | |
826 | static_assert(std::is_nothrow_move_assignable<boost::container::small_vector<S1, 1>>::value, ""); | |
827 | ||
828 | // Fail | |
829 | static_assert(std::is_nothrow_move_constructible<boost::container::small_vector<S2, 1>>::value, ""); | |
830 | static_assert(std::is_nothrow_move_assignable<boost::container::small_vector<S2, 1>>::value, ""); | |
831 | ||
832 | int main() | |
833 | { | |
834 | return 0; | |
835 | } | |
836 | */ |