1 | // Boost.Geometry.Index varray |
2 | // Unit Test |
3 | |
4 | // Copyright (c) 2012-2014 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) |
10 | |
11 | #include <boost/test/included/test_exec_monitor.hpp> |
12 | #include <boost/test/impl/execution_monitor.ipp> |
13 | |
14 | // TODO: Disable parts of the unit test that should not run when BOOST_NO_EXCEPTIONS |
15 | // if exceptions are enabled there must be a user defined throw_exception function |
16 | #ifdef BOOST_NO_EXCEPTIONS |
17 | namespace boost { |
18 | void throw_exception(std::exception const & e){}; // user defined |
19 | } // namespace boost |
20 | #endif // BOOST_NO_EXCEPTIONS |
21 | |
22 | #include <vector> |
23 | #include <list> |
24 | |
25 | #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
26 | #include <boost/container/vector.hpp> |
27 | #include <boost/container/stable_vector.hpp> |
28 | using namespace boost::container; |
29 | #endif |
30 | |
31 | #include "varray_test.hpp" |
32 | |
33 | using namespace boost::geometry::index::detail; |
34 | |
35 | template <typename T, size_t N> |
36 | void test_ctor_ndc() |
37 | { |
38 | varray<T, N> s; |
39 | BOOST_CHECK_EQUAL(s.size(), 0u); |
40 | BOOST_CHECK(s.capacity() == N); |
41 | #ifndef BOOST_NO_EXCEPTIONS |
42 | BOOST_CHECK_THROW( s.at(0), std::out_of_range ); |
43 | #endif // BOOST_NO_EXCEPTIONS |
44 | } |
45 | |
46 | template <typename T, size_t N> |
47 | void test_ctor_nc(size_t n) |
48 | { |
49 | varray<T, N> s(n); |
50 | BOOST_CHECK(s.size() == n); |
51 | BOOST_CHECK(s.capacity() == N); |
52 | #ifndef BOOST_NO_EXCEPTIONS |
53 | BOOST_CHECK_THROW( s.at(n), std::out_of_range ); |
54 | #endif // BOOST_NO_EXCEPTIONS |
55 | if ( 1 < n ) |
56 | { |
57 | T val10(10); |
58 | s[0] = val10; |
59 | BOOST_CHECK(T(10) == s[0]); |
60 | BOOST_CHECK(T(10) == s.at(0)); |
61 | T val20(20); |
62 | s.at(1) = val20; |
63 | BOOST_CHECK(T(20) == s[1]); |
64 | BOOST_CHECK(T(20) == s.at(1)); |
65 | } |
66 | } |
67 | |
68 | template <typename T, size_t N> |
69 | void test_ctor_nd(size_t n, T const& v) |
70 | { |
71 | varray<T, N> s(n, v); |
72 | BOOST_CHECK(s.size() == n); |
73 | BOOST_CHECK(s.capacity() == N); |
74 | #ifndef BOOST_NO_EXCEPTIONS |
75 | BOOST_CHECK_THROW( s.at(n), std::out_of_range ); |
76 | #endif // BOOST_NO_EXCEPTIONS |
77 | if ( 1 < n ) |
78 | { |
79 | BOOST_CHECK(v == s[0]); |
80 | BOOST_CHECK(v == s.at(0)); |
81 | BOOST_CHECK(v == s[1]); |
82 | BOOST_CHECK(v == s.at(1)); |
83 | s[0] = T(10); |
84 | BOOST_CHECK(T(10) == s[0]); |
85 | BOOST_CHECK(T(10) == s.at(0)); |
86 | s.at(1) = T(20); |
87 | BOOST_CHECK(T(20) == s[1]); |
88 | BOOST_CHECK(T(20) == s.at(1)); |
89 | } |
90 | } |
91 | |
92 | template <typename T, size_t N> |
93 | void test_resize_nc(size_t n) |
94 | { |
95 | varray<T, N> s; |
96 | |
97 | s.resize(n); |
98 | BOOST_CHECK(s.size() == n); |
99 | BOOST_CHECK(s.capacity() == N); |
100 | #ifndef BOOST_NO_EXCEPTIONS |
101 | BOOST_CHECK_THROW( s.at(n), std::out_of_range ); |
102 | #endif // BOOST_NO_EXCEPTIONS |
103 | if ( 1 < n ) |
104 | { |
105 | T val10(10); |
106 | s[0] = val10; |
107 | BOOST_CHECK(T(10) == s[0]); |
108 | BOOST_CHECK(T(10) == s.at(0)); |
109 | T val20(20); |
110 | s.at(1) = val20; |
111 | BOOST_CHECK(T(20) == s[1]); |
112 | BOOST_CHECK(T(20) == s.at(1)); |
113 | } |
114 | } |
115 | |
116 | template <typename T, size_t N> |
117 | void test_resize_nd(size_t n, T const& v) |
118 | { |
119 | varray<T, N> s; |
120 | |
121 | s.resize(n, v); |
122 | BOOST_CHECK(s.size() == n); |
123 | BOOST_CHECK(s.capacity() == N); |
124 | #ifndef BOOST_NO_EXCEPTIONS |
125 | BOOST_CHECK_THROW( s.at(n), std::out_of_range ); |
126 | #endif // BOOST_NO_EXCEPTIONS |
127 | if ( 1 < n ) |
128 | { |
129 | BOOST_CHECK(v == s[0]); |
130 | BOOST_CHECK(v == s.at(0)); |
131 | BOOST_CHECK(v == s[1]); |
132 | BOOST_CHECK(v == s.at(1)); |
133 | s[0] = T(10); |
134 | BOOST_CHECK(T(10) == s[0]); |
135 | BOOST_CHECK(T(10) == s.at(0)); |
136 | s.at(1) = T(20); |
137 | BOOST_CHECK(T(20) == s[1]); |
138 | BOOST_CHECK(T(20) == s.at(1)); |
139 | } |
140 | } |
141 | |
142 | template <typename T, size_t N> |
143 | void test_push_back_nd() |
144 | { |
145 | varray<T, N> s; |
146 | |
147 | BOOST_CHECK(s.size() == 0); |
148 | #ifndef BOOST_NO_EXCEPTIONS |
149 | BOOST_CHECK_THROW( s.at(0), std::out_of_range ); |
150 | #endif // BOOST_NO_EXCEPTIONS |
151 | |
152 | for ( size_t i = 0 ; i < N ; ++i ) |
153 | { |
154 | T t(i); |
155 | s.push_back(t); |
156 | BOOST_CHECK(s.size() == i + 1); |
157 | #ifndef BOOST_NO_EXCEPTIONS |
158 | BOOST_CHECK_THROW( s.at(i + 1), std::out_of_range ); |
159 | #endif // BOOST_NO_EXCEPTIONS |
160 | BOOST_CHECK(T(i) == s.at(i)); |
161 | BOOST_CHECK(T(i) == s[i]); |
162 | BOOST_CHECK(T(i) == s.back()); |
163 | BOOST_CHECK(T(0) == s.front()); |
164 | BOOST_CHECK(T(i) == *(s.data() + i)); |
165 | } |
166 | } |
167 | |
168 | template <typename T, size_t N> |
169 | void test_pop_back_nd() |
170 | { |
171 | varray<T, N> s; |
172 | |
173 | for ( size_t i = 0 ; i < N ; ++i ) |
174 | { |
175 | T t(i); |
176 | s.push_back(t); |
177 | } |
178 | |
179 | for ( size_t i = N ; i > 1 ; --i ) |
180 | { |
181 | s.pop_back(); |
182 | BOOST_CHECK(s.size() == i - 1); |
183 | #ifndef BOOST_NO_EXCEPTIONS |
184 | BOOST_CHECK_THROW( s.at(i - 1), std::out_of_range ); |
185 | #endif // BOOST_NO_EXCEPTIONS |
186 | BOOST_CHECK(T(i - 2) == s.at(i - 2)); |
187 | BOOST_CHECK(T(i - 2) == s[i - 2]); |
188 | BOOST_CHECK(T(i - 2) == s.back()); |
189 | BOOST_CHECK(T(0) == s.front()); |
190 | } |
191 | } |
192 | |
193 | template <typename It1, typename It2> |
194 | void test_compare_ranges(It1 first1, It1 last1, It2 first2, It2 last2) |
195 | { |
196 | BOOST_CHECK(std::distance(first1, last1) == std::distance(first2, last2)); |
197 | for ( ; first1 != last1 && first2 != last2 ; ++first1, ++first2 ) |
198 | BOOST_CHECK(*first1 == *first2); |
199 | } |
200 | |
201 | template <typename T, size_t N, typename C> |
202 | void test_copy_and_assign(C const& c) |
203 | { |
204 | { |
205 | varray<T, N> s(c.begin(), c.end()); |
206 | BOOST_CHECK(s.size() == c.size()); |
207 | test_compare_ranges(s.begin(), s.end(), c.begin(), c.end()); |
208 | } |
209 | { |
210 | varray<T, N> s; |
211 | BOOST_CHECK(0 == s.size()); |
212 | s.assign(c.begin(), c.end()); |
213 | BOOST_CHECK(s.size() == c.size()); |
214 | test_compare_ranges(s.begin(), s.end(), c.begin(), c.end()); |
215 | } |
216 | } |
217 | |
218 | template <typename T, size_t N> |
219 | void test_copy_and_assign_nd(T const& val) |
220 | { |
221 | varray<T, N> s; |
222 | std::vector<T> v; |
223 | std::list<T> l; |
224 | |
225 | for ( size_t i = 0 ; i < N ; ++i ) |
226 | { |
227 | T t(i); |
228 | s.push_back(t); |
229 | v.push_back(t); |
230 | l.push_back(t); |
231 | } |
232 | // copy ctor |
233 | { |
234 | varray<T, N> s1(s); |
235 | BOOST_CHECK(s.size() == s1.size()); |
236 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); |
237 | } |
238 | // copy assignment |
239 | { |
240 | varray<T, N> s1; |
241 | BOOST_CHECK(0 == s1.size()); |
242 | s1 = s; |
243 | BOOST_CHECK(s.size() == s1.size()); |
244 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); |
245 | } |
246 | |
247 | // ctor(Iter, Iter) and assign(Iter, Iter) |
248 | test_copy_and_assign<T, N>(s); |
249 | test_copy_and_assign<T, N>(v); |
250 | test_copy_and_assign<T, N>(l); |
251 | |
252 | // assign(N, V) |
253 | { |
254 | varray<T, N> s1(s); |
255 | test_compare_ranges(s.begin(), s.end(), s1.begin(), s1.end()); |
256 | std::vector<T> a(N, val); |
257 | s1.assign(N, val); |
258 | test_compare_ranges(a.begin(), a.end(), s1.begin(), s1.end()); |
259 | } |
260 | |
261 | #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
262 | stable_vector<T> bsv(s.begin(), s.end()); |
263 | vector<T> bv(s.begin(), s.end()); |
264 | test_copy_and_assign<T, N>(bsv); |
265 | test_copy_and_assign<T, N>(bv); |
266 | #endif |
267 | } |
268 | |
269 | template <typename T, size_t N> |
270 | void test_iterators_nd() |
271 | { |
272 | varray<T, N> s; |
273 | std::vector<T> v; |
274 | |
275 | for ( size_t i = 0 ; i < N ; ++i ) |
276 | { |
277 | s.push_back(T(i)); |
278 | v.push_back(T(i)); |
279 | } |
280 | |
281 | test_compare_ranges(s.begin(), s.end(), v.begin(), v.end()); |
282 | test_compare_ranges(s.rbegin(), s.rend(), v.rbegin(), v.rend()); |
283 | |
284 | s.assign(v.rbegin(), v.rend()); |
285 | |
286 | test_compare_ranges(s.cbegin(), s.cend(), v.rbegin(), v.rend()); |
287 | test_compare_ranges(s.crbegin(), s.crend(), v.begin(), v.end()); |
288 | |
289 | varray<T, N> const& cs = s; |
290 | std::vector<T> const& cv = v; |
291 | s.assign(cv.rbegin(), cv.rend()); |
292 | |
293 | test_compare_ranges(cs.begin(), cs.end(), cv.rbegin(), cv.rend()); |
294 | test_compare_ranges(cs.rbegin(), cs.rend(), cv.begin(), cv.end()); |
295 | } |
296 | |
297 | template <typename T, size_t N> |
298 | void test_erase_nd() |
299 | { |
300 | varray<T, N> s; |
301 | typedef typename varray<T, N>::iterator It; |
302 | |
303 | for ( size_t i = 0 ; i < N ; ++i ) |
304 | s.push_back(T(i)); |
305 | |
306 | // erase(pos) |
307 | { |
308 | for ( size_t i = 0 ; i < N ; ++i ) |
309 | { |
310 | varray<T, N> s1(s); |
311 | It it = s1.erase(s1.begin() + i); |
312 | BOOST_CHECK(s1.begin() + i == it); |
313 | BOOST_CHECK(s1.size() == N - 1); |
314 | for ( size_t j = 0 ; j < i ; ++j ) |
315 | BOOST_CHECK(s1[j] == T(j)); |
316 | for ( size_t j = i+1 ; j < N ; ++j ) |
317 | BOOST_CHECK(s1[j-1] == T(j)); |
318 | } |
319 | } |
320 | // erase(first, last) |
321 | { |
322 | size_t n = N/3; |
323 | for ( size_t i = 0 ; i <= N ; ++i ) |
324 | { |
325 | varray<T, N> s1(s); |
326 | size_t removed = i + n < N ? n : N - i; |
327 | It it = s1.erase(s1.begin() + i, s1.begin() + i + removed); |
328 | BOOST_CHECK(s1.begin() + i == it); |
329 | BOOST_CHECK(s1.size() == N - removed); |
330 | for ( size_t j = 0 ; j < i ; ++j ) |
331 | BOOST_CHECK(s1[j] == T(j)); |
332 | for ( size_t j = i+n ; j < N ; ++j ) |
333 | BOOST_CHECK(s1[j-n] == T(j)); |
334 | } |
335 | } |
336 | } |
337 | |
338 | template <typename T, size_t N, typename SV, typename C> |
339 | void test_insert(SV const& s, C const& c) |
340 | { |
341 | size_t h = N/2; |
342 | size_t n = size_t(h/1.5f); |
343 | |
344 | for ( size_t i = 0 ; i <= h ; ++i ) |
345 | { |
346 | varray<T, N> s1(s); |
347 | |
348 | typename C::const_iterator it = c.begin(); |
349 | std::advance(it, n); |
350 | typename varray<T, N>::iterator |
351 | it1 = s1.insert(s1.begin() + i, c.begin(), it); |
352 | |
353 | BOOST_CHECK(s1.begin() + i == it1); |
354 | BOOST_CHECK(s1.size() == h+n); |
355 | for ( size_t j = 0 ; j < i ; ++j ) |
356 | BOOST_CHECK(s1[j] == T(j)); |
357 | for ( size_t j = 0 ; j < n ; ++j ) |
358 | BOOST_CHECK(s1[j+i] == T(100 + j)); |
359 | for ( size_t j = 0 ; j < h-i ; ++j ) |
360 | BOOST_CHECK(s1[j+i+n] == T(j+i)); |
361 | } |
362 | } |
363 | |
364 | template <typename T, size_t N> |
365 | void test_insert_nd(T const& val) |
366 | { |
367 | size_t h = N/2; |
368 | |
369 | varray<T, N> s, ss; |
370 | std::vector<T> v; |
371 | std::list<T> l; |
372 | |
373 | typedef typename varray<T, N>::iterator It; |
374 | |
375 | for ( size_t i = 0 ; i < h ; ++i ) |
376 | { |
377 | s.push_back(T(i)); |
378 | ss.push_back(T(100 + i)); |
379 | v.push_back(T(100 + i)); |
380 | l.push_back(T(100 + i)); |
381 | } |
382 | |
383 | // insert(pos, val) |
384 | { |
385 | for ( size_t i = 0 ; i <= h ; ++i ) |
386 | { |
387 | varray<T, N> s1(s); |
388 | It it = s1.insert(s1.begin() + i, val); |
389 | BOOST_CHECK(s1.begin() + i == it); |
390 | BOOST_CHECK(s1.size() == h+1); |
391 | for ( size_t j = 0 ; j < i ; ++j ) |
392 | BOOST_CHECK(s1[j] == T(j)); |
393 | BOOST_CHECK(s1[i] == val); |
394 | for ( size_t j = 0 ; j < h-i ; ++j ) |
395 | BOOST_CHECK(s1[j+i+1] == T(j+i)); |
396 | } |
397 | } |
398 | // insert(pos, n, val) |
399 | { |
400 | size_t n = size_t(h/1.5f); |
401 | for ( size_t i = 0 ; i <= h ; ++i ) |
402 | { |
403 | varray<T, N> s1(s); |
404 | It it = s1.insert(s1.begin() + i, n, val); |
405 | BOOST_CHECK(s1.begin() + i == it); |
406 | BOOST_CHECK(s1.size() == h+n); |
407 | for ( size_t j = 0 ; j < i ; ++j ) |
408 | BOOST_CHECK(s1[j] == T(j)); |
409 | for ( size_t j = 0 ; j < n ; ++j ) |
410 | BOOST_CHECK(s1[j+i] == val); |
411 | for ( size_t j = 0 ; j < h-i ; ++j ) |
412 | BOOST_CHECK(s1[j+i+n] == T(j+i)); |
413 | } |
414 | } |
415 | // insert(pos, first, last) |
416 | test_insert<T, N>(s, ss); |
417 | test_insert<T, N>(s, v); |
418 | test_insert<T, N>(s, l); |
419 | |
420 | #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) |
421 | stable_vector<T> bsv(ss.begin(), ss.end()); |
422 | vector<T> bv(ss.begin(), ss.end()); |
423 | test_insert<T, N>(s, bv); |
424 | test_insert<T, N>(s, bsv); |
425 | #endif |
426 | } |
427 | |
428 | template <typename T> |
429 | void test_capacity_0_nd() |
430 | { |
431 | varray<T, 10> v(5u, T(0)); |
432 | |
433 | //varray<T, 0, bad_alloc_strategy<T> > s; |
434 | varray<T, 0> s; |
435 | BOOST_CHECK(s.size() == 0); |
436 | BOOST_CHECK(s.capacity() == 0); |
437 | #ifndef BOOST_NO_EXCEPTIONS |
438 | BOOST_CHECK_THROW(s.at(0), std::out_of_range); |
439 | //BOOST_CHECK_THROW(s.resize(5u, T(0)), std::bad_alloc); |
440 | //BOOST_CHECK_THROW(s.push_back(T(0)), std::bad_alloc); |
441 | //BOOST_CHECK_THROW(s.insert(s.end(), T(0)), std::bad_alloc); |
442 | //BOOST_CHECK_THROW(s.insert(s.end(), 5u, T(0)), std::bad_alloc); |
443 | //BOOST_CHECK_THROW(s.insert(s.end(), v.begin(), v.end()), std::bad_alloc); |
444 | //BOOST_CHECK_THROW(s.assign(v.begin(), v.end()), std::bad_alloc); |
445 | //BOOST_CHECK_THROW(s.assign(5u, T(0)), std::bad_alloc); |
446 | //try{ |
447 | // varray<T, 0, bad_alloc_strategy<T> > s2(v.begin(), v.end()); |
448 | // BOOST_CHECK(false); |
449 | //}catch(std::bad_alloc &){} |
450 | //try{ |
451 | // varray<T, 0, bad_alloc_strategy<T> > s1(5u, T(0)); |
452 | // BOOST_CHECK(false); |
453 | //}catch(std::bad_alloc &){} |
454 | #endif // BOOST_NO_EXCEPTIONS |
455 | } |
456 | |
457 | template <typename T, size_t N> |
458 | void test_exceptions_nd() |
459 | { |
460 | varray<T, N> v(N, T(0)); |
461 | //varray<T, N/2, bad_alloc_strategy<T> > s(N/2, T(0)); |
462 | varray<T, N/2> s(N/2, T(0)); |
463 | |
464 | #ifndef BOOST_NO_EXCEPTIONS |
465 | /*BOOST_CHECK_THROW(s.resize(N, T(0)), std::bad_alloc); |
466 | BOOST_CHECK_THROW(s.push_back(T(0)), std::bad_alloc); |
467 | BOOST_CHECK_THROW(s.insert(s.end(), T(0)), std::bad_alloc); |
468 | BOOST_CHECK_THROW(s.insert(s.end(), N, T(0)), std::bad_alloc); |
469 | BOOST_CHECK_THROW(s.insert(s.end(), v.begin(), v.end()), std::bad_alloc); |
470 | BOOST_CHECK_THROW(s.assign(v.begin(), v.end()), std::bad_alloc); |
471 | BOOST_CHECK_THROW(s.assign(N, T(0)), std::bad_alloc); |
472 | try{ |
473 | container_detail::varray<T, N/2, bad_alloc_strategy<T> > s2(v.begin(), v.end()); |
474 | BOOST_CHECK(false); |
475 | }catch(std::bad_alloc &){} |
476 | try{ |
477 | container_detail::varray<T, N/2, bad_alloc_strategy<T> > s1(N, T(0)); |
478 | BOOST_CHECK(false); |
479 | }catch(std::bad_alloc &){}*/ |
480 | #endif // BOOST_NO_EXCEPTIONS |
481 | } |
482 | |
483 | template <typename T, size_t N> |
484 | void test_swap_and_move_nd() |
485 | { |
486 | { |
487 | varray<T, N> v1, v2, v3, v4; |
488 | varray<T, N> s1, s2; |
489 | //varray<T, N, bad_alloc_strategy<T> > s4; |
490 | varray<T, N> s4; |
491 | |
492 | for (size_t i = 0 ; i < N ; ++i ) |
493 | { |
494 | v1.push_back(T(i)); |
495 | v2.push_back(T(i)); |
496 | v3.push_back(T(i)); |
497 | v4.push_back(T(i)); |
498 | } |
499 | for (size_t i = 0 ; i < N/2 ; ++i ) |
500 | { |
501 | s1.push_back(T(100 + i)); |
502 | s2.push_back(T(100 + i)); |
503 | s4.push_back(T(100 + i)); |
504 | } |
505 | |
506 | s1.swap(v1); |
507 | s2 = boost::move(v2); |
508 | varray<T, N> s3(boost::move(v3)); |
509 | s4.swap(v4); |
510 | |
511 | BOOST_CHECK(v1.size() == N/2); |
512 | BOOST_CHECK(s1.size() == N); |
513 | BOOST_CHECK(v2.size() == N); // objects aren't destroyed |
514 | BOOST_CHECK(s2.size() == N); |
515 | BOOST_CHECK(v3.size() == N); // objects aren't destroyed |
516 | BOOST_CHECK(s3.size() == N); |
517 | BOOST_CHECK(v4.size() == N/2); |
518 | BOOST_CHECK(s4.size() == N); |
519 | for (size_t i = 0 ; i < N/2 ; ++i ) |
520 | { |
521 | BOOST_CHECK(v1[i] == T(100 + i)); |
522 | BOOST_CHECK(v4[i] == T(100 + i)); |
523 | } |
524 | for (size_t i = 0 ; i < N ; ++i ) |
525 | { |
526 | BOOST_CHECK(s1[i] == T(i)); |
527 | BOOST_CHECK(s2[i] == T(i)); |
528 | BOOST_CHECK(s3[i] == T(i)); |
529 | BOOST_CHECK(s4[i] == T(i)); |
530 | } |
531 | } |
532 | { |
533 | varray<T, N> v1, v2, v3; |
534 | varray<T, N/2> s1, s2; |
535 | |
536 | for (size_t i = 0 ; i < N/2 ; ++i ) |
537 | { |
538 | v1.push_back(T(i)); |
539 | v2.push_back(T(i)); |
540 | v3.push_back(T(i)); |
541 | } |
542 | for (size_t i = 0 ; i < N/3 ; ++i ) |
543 | { |
544 | s1.push_back(T(100 + i)); |
545 | s2.push_back(T(100 + i)); |
546 | } |
547 | |
548 | s1.swap(v1); |
549 | s2 = boost::move(v2); |
550 | varray<T, N/2> s3(boost::move(v3)); |
551 | |
552 | BOOST_CHECK(v1.size() == N/3); |
553 | BOOST_CHECK(s1.size() == N/2); |
554 | BOOST_CHECK(v2.size() == N/2); // objects aren't destroyed |
555 | BOOST_CHECK(s2.size() == N/2); |
556 | BOOST_CHECK(v3.size() == N/2); // objects aren't destroyed |
557 | BOOST_CHECK(s3.size() == N/2); |
558 | for (size_t i = 0 ; i < N/3 ; ++i ) |
559 | BOOST_CHECK(v1[i] == T(100 + i)); |
560 | for (size_t i = 0 ; i < N/2 ; ++i ) |
561 | { |
562 | BOOST_CHECK(s1[i] == T(i)); |
563 | BOOST_CHECK(s2[i] == T(i)); |
564 | BOOST_CHECK(s3[i] == T(i)); |
565 | } |
566 | } |
567 | { |
568 | varray<T, N> v(N, T(0)); |
569 | //varray<T, N/2, bad_alloc_strategy<T> > s(N/2, T(1)); |
570 | varray<T, N/2> s(N/2, T(1)); |
571 | #ifndef BOOST_NO_EXCEPTIONS |
572 | //BOOST_CHECK_THROW(s.swap(v), std::bad_alloc); |
573 | //v.resize(N, T(0)); |
574 | //BOOST_CHECK_THROW(s = boost::move(v), std::bad_alloc); |
575 | //v.resize(N, T(0)); |
576 | //try { |
577 | // varray<T, N/2, bad_alloc_strategy<T> > s2(boost::move(v)); |
578 | // BOOST_CHECK(false); |
579 | //} catch (std::bad_alloc &) {} |
580 | #endif // BOOST_NO_EXCEPTIONS |
581 | } |
582 | } |
583 | |
584 | template <typename T, size_t N> |
585 | void test_emplace_0p() |
586 | { |
587 | //emplace_back() |
588 | { |
589 | //varray<T, N, bad_alloc_strategy<T> > v; |
590 | varray<T, N> v; |
591 | |
592 | for (int i = 0 ; i < int(N) ; ++i ) |
593 | v.emplace_back(); |
594 | BOOST_CHECK(v.size() == N); |
595 | #ifndef BOOST_NO_EXCEPTIONS |
596 | //BOOST_CHECK_THROW(v.emplace_back(), std::bad_alloc); |
597 | #endif |
598 | } |
599 | } |
600 | |
601 | template <typename T, size_t N> |
602 | void test_emplace_2p() |
603 | { |
604 | //emplace_back(pos, int, int) |
605 | { |
606 | //varray<T, N, bad_alloc_strategy<T> > v; |
607 | varray<T, N> v; |
608 | |
609 | for (int i = 0 ; i < int(N) ; ++i ) |
610 | v.emplace_back(i, 100 + i); |
611 | BOOST_CHECK(v.size() == N); |
612 | #ifndef BOOST_NO_EXCEPTIONS |
613 | //BOOST_CHECK_THROW(v.emplace_back(N, 100 + N), std::bad_alloc); |
614 | #endif |
615 | BOOST_CHECK(v.size() == N); |
616 | for (int i = 0 ; i < int(N) ; ++i ) |
617 | BOOST_CHECK(v[i] == T(i, 100 + i)); |
618 | } |
619 | |
620 | // emplace(pos, int, int) |
621 | { |
622 | //typedef typename varray<T, N, bad_alloc_strategy<T> >::iterator It; |
623 | typedef typename varray<T, N>::iterator It; |
624 | |
625 | int h = N / 2; |
626 | |
627 | //varray<T, N, bad_alloc_strategy<T> > v; |
628 | varray<T, N> v; |
629 | for ( int i = 0 ; i < h ; ++i ) |
630 | v.emplace_back(i, 100 + i); |
631 | |
632 | for ( int i = 0 ; i <= h ; ++i ) |
633 | { |
634 | //varray<T, N, bad_alloc_strategy<T> > vv(v); |
635 | varray<T, N> vv(v); |
636 | It it = vv.emplace(vv.begin() + i, i+100, i+200); |
637 | BOOST_CHECK(vv.begin() + i == it); |
638 | BOOST_CHECK(vv.size() == size_t(h+1)); |
639 | for ( int j = 0 ; j < i ; ++j ) |
640 | BOOST_CHECK(vv[j] == T(j, j+100)); |
641 | BOOST_CHECK(vv[i] == T(i+100, i+200)); |
642 | for ( int j = 0 ; j < h-i ; ++j ) |
643 | BOOST_CHECK(vv[j+i+1] == T(j+i, j+i+100)); |
644 | } |
645 | } |
646 | } |
647 | |
648 | template <typename T, size_t N> |
649 | void test_sv_elem(T const& t) |
650 | { |
651 | //typedef varray<T, N, bad_alloc_strategy<T> > V; |
652 | typedef varray<T, N> V; |
653 | |
654 | //varray<V, N, bad_alloc_strategy<V> > v; |
655 | varray<V, N> v; |
656 | |
657 | v.push_back(V(N/2, t)); |
658 | V vvv(N/2, t); |
659 | v.push_back(boost::move(vvv)); |
660 | v.insert(v.begin(), V(N/2, t)); |
661 | v.insert(v.end(), V(N/2, t)); |
662 | v.emplace_back(N/2, t); |
663 | } |
664 | |
665 | int test_main(int, char* []) |
666 | { |
667 | BOOST_CHECK(counting_value::count() == 0); |
668 | |
669 | test_ctor_ndc<size_t, 10>(); |
670 | test_ctor_ndc<value_ndc, 10>(); |
671 | test_ctor_ndc<counting_value, 10>(); |
672 | BOOST_CHECK(counting_value::count() == 0); |
673 | test_ctor_ndc<shptr_value, 10>(); |
674 | test_ctor_ndc<copy_movable, 10>(); |
675 | |
676 | test_ctor_nc<size_t, 10>(n: 5); |
677 | test_ctor_nc<value_nc, 10>(n: 5); |
678 | test_ctor_nc<counting_value, 10>(n: 5); |
679 | BOOST_CHECK(counting_value::count() == 0); |
680 | test_ctor_nc<shptr_value, 10>(n: 5); |
681 | test_ctor_nc<copy_movable, 10>(n: 5); |
682 | |
683 | test_ctor_nd<size_t, 10>(n: 5, v: 1); |
684 | test_ctor_nd<value_nd, 10>(n: 5, v: value_nd(1)); |
685 | test_ctor_nd<counting_value, 10>(n: 5, v: counting_value(1)); |
686 | BOOST_CHECK(counting_value::count() == 0); |
687 | test_ctor_nd<shptr_value, 10>(n: 5, v: shptr_value(1)); |
688 | test_ctor_nd<copy_movable, 10>(n: 5, v: produce()); |
689 | |
690 | test_resize_nc<size_t, 10>(n: 5); |
691 | test_resize_nc<value_nc, 10>(n: 5); |
692 | test_resize_nc<counting_value, 10>(n: 5); |
693 | BOOST_CHECK(counting_value::count() == 0); |
694 | test_resize_nc<shptr_value, 10>(n: 5); |
695 | test_resize_nc<copy_movable, 10>(n: 5); |
696 | |
697 | test_resize_nd<size_t, 10>(n: 5, v: 1); |
698 | test_resize_nd<value_nd, 10>(n: 5, v: value_nd(1)); |
699 | test_resize_nd<counting_value, 10>(n: 5, v: counting_value(1)); |
700 | BOOST_CHECK(counting_value::count() == 0); |
701 | test_resize_nd<shptr_value, 10>(n: 5, v: shptr_value(1)); |
702 | test_resize_nd<copy_movable, 10>(n: 5, v: produce()); |
703 | |
704 | test_push_back_nd<size_t, 10>(); |
705 | test_push_back_nd<value_nd, 10>(); |
706 | test_push_back_nd<counting_value, 10>(); |
707 | BOOST_CHECK(counting_value::count() == 0); |
708 | test_push_back_nd<shptr_value, 10>(); |
709 | test_push_back_nd<copy_movable, 10>(); |
710 | |
711 | test_pop_back_nd<size_t, 10>(); |
712 | test_pop_back_nd<value_nd, 10>(); |
713 | test_pop_back_nd<counting_value, 10>(); |
714 | BOOST_CHECK(counting_value::count() == 0); |
715 | test_pop_back_nd<shptr_value, 10>(); |
716 | test_pop_back_nd<copy_movable, 10>(); |
717 | |
718 | test_copy_and_assign_nd<size_t, 10>(val: 1); |
719 | test_copy_and_assign_nd<value_nd, 10>(val: value_nd(1)); |
720 | test_copy_and_assign_nd<counting_value, 10>(val: counting_value(1)); |
721 | BOOST_CHECK(counting_value::count() == 0); |
722 | test_copy_and_assign_nd<shptr_value, 10>(val: shptr_value(1)); |
723 | test_copy_and_assign_nd<copy_movable, 10>(val: produce()); |
724 | |
725 | test_iterators_nd<size_t, 10>(); |
726 | test_iterators_nd<value_nd, 10>(); |
727 | test_iterators_nd<counting_value, 10>(); |
728 | BOOST_CHECK(counting_value::count() == 0); |
729 | test_iterators_nd<shptr_value, 10>(); |
730 | test_iterators_nd<copy_movable, 10>(); |
731 | |
732 | test_erase_nd<size_t, 10>(); |
733 | test_erase_nd<value_nd, 10>(); |
734 | test_erase_nd<counting_value, 10>(); |
735 | BOOST_CHECK(counting_value::count() == 0); |
736 | test_erase_nd<shptr_value, 10>(); |
737 | test_erase_nd<copy_movable, 10>(); |
738 | |
739 | test_insert_nd<size_t, 10>(val: 50); |
740 | test_insert_nd<value_nd, 10>(val: value_nd(50)); |
741 | test_insert_nd<counting_value, 10>(val: counting_value(50)); |
742 | BOOST_CHECK(counting_value::count() == 0); |
743 | test_insert_nd<shptr_value, 10>(val: shptr_value(50)); |
744 | test_insert_nd<copy_movable, 10>(val: produce()); |
745 | |
746 | test_capacity_0_nd<size_t>(); |
747 | test_capacity_0_nd<value_nd>(); |
748 | test_capacity_0_nd<counting_value>(); |
749 | BOOST_CHECK(counting_value::count() == 0); |
750 | test_capacity_0_nd<shptr_value>(); |
751 | test_capacity_0_nd<copy_movable>(); |
752 | |
753 | test_exceptions_nd<size_t, 10>(); |
754 | test_exceptions_nd<value_nd, 10>(); |
755 | test_exceptions_nd<counting_value, 10>(); |
756 | BOOST_CHECK(counting_value::count() == 0); |
757 | test_exceptions_nd<shptr_value, 10>(); |
758 | test_exceptions_nd<copy_movable, 10>(); |
759 | |
760 | test_swap_and_move_nd<size_t, 10>(); |
761 | test_swap_and_move_nd<value_nd, 10>(); |
762 | test_swap_and_move_nd<counting_value, 10>(); |
763 | BOOST_CHECK(counting_value::count() == 0); |
764 | test_swap_and_move_nd<shptr_value, 10>(); |
765 | test_swap_and_move_nd<copy_movable, 10>(); |
766 | |
767 | test_emplace_0p<counting_value, 10>(); |
768 | BOOST_CHECK(counting_value::count() == 0); |
769 | |
770 | test_emplace_2p<counting_value, 10>(); |
771 | BOOST_CHECK(counting_value::count() == 0); |
772 | |
773 | test_sv_elem<size_t, 10>(t: 50); |
774 | test_sv_elem<value_nd, 10>(t: value_nd(50)); |
775 | test_sv_elem<counting_value, 10>(t: counting_value(50)); |
776 | BOOST_CHECK(counting_value::count() == 0); |
777 | test_sv_elem<shptr_value, 10>(t: shptr_value(50)); |
778 | test_sv_elem<copy_movable, 10>(t: copy_movable(50)); |
779 | |
780 | return 0; |
781 | } |
782 | |