1// Test of the base circular buffer container.
2
3// Copyright (c) 2003-2008 Jan Gaspar
4// Copyright (c) 2013 Antony Polukhin
5
6// Use, modification, and distribution is subject to the Boost Software
7// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10#include "test.hpp"
11
12#define CB_CONTAINER circular_buffer
13
14#include "common.ipp"
15
16void iterator_constructor_and_assign_test() {
17
18 circular_buffer<MyInteger> cb(4, 3);
19 circular_buffer<MyInteger>::iterator it = cb.begin();
20 circular_buffer<MyInteger>::iterator itCopy;
21 itCopy = it;
22 it = it;
23 circular_buffer<MyInteger>::const_iterator cit;
24 cit = it;
25 circular_buffer<MyInteger>::const_iterator end1 = cb.end();
26 circular_buffer<MyInteger>::const_iterator end2 = end1;
27
28 BOOST_CHECK(itCopy == it);
29 BOOST_CHECK(cit == it);
30 BOOST_CHECK(end1 == end2);
31 BOOST_CHECK(it != end1);
32 BOOST_CHECK(cit != end2);
33}
34
35void iterator_reference_test() {
36
37 circular_buffer<Dummy> cb(3, Dummy());
38 circular_buffer<Dummy>::iterator it = cb.begin();
39 circular_buffer<Dummy>::const_iterator cit = cb.begin() + 1;
40
41 BOOST_CHECK((*it).m_n == Dummy::eVar);
42 BOOST_CHECK((*it).fnc() == Dummy::eFnc);
43 BOOST_CHECK((*cit).const_fnc() == Dummy::eConst);
44 BOOST_CHECK((*it).virtual_fnc() == Dummy::eVirtual);
45 BOOST_CHECK(it->m_n == Dummy::eVar);
46 BOOST_CHECK(it->fnc() == Dummy::eFnc);
47 BOOST_CHECK(cit->const_fnc() == Dummy::eConst);
48 BOOST_CHECK(it->virtual_fnc() == Dummy::eVirtual);
49}
50
51void iterator_difference_test() {
52
53 circular_buffer<MyInteger> cb(5, 1);
54 cb.push_back(2);
55 circular_buffer<MyInteger>::iterator it1 = cb.begin() + 2;
56 circular_buffer<MyInteger>::iterator it2 = cb.begin() + 3;
57 circular_buffer<MyInteger>::const_iterator begin = cb.begin();
58 circular_buffer<MyInteger>::iterator end = cb.end();
59
60 BOOST_CHECK(begin - begin == 0);
61 BOOST_CHECK(end - cb.begin() == 5);
62 BOOST_CHECK(end - end == 0);
63 BOOST_CHECK(begin - cb.end() == -5);
64 BOOST_CHECK(it1 - cb.begin() == 2);
65 BOOST_CHECK(it1 - begin == 2);
66 BOOST_CHECK(end - it1 == 3);
67 BOOST_CHECK(it2 - it1 == 1);
68 BOOST_CHECK(it1 - it2 == -1);
69 BOOST_CHECK(it2 - it2 == 0);
70}
71
72void iterator_increment_test() {
73
74 circular_buffer<MyInteger> cb(10, 1);
75 cb.push_back(2);
76 circular_buffer<MyInteger>::iterator it1 = cb.begin();
77 circular_buffer<MyInteger>::iterator it2 = cb.begin() + 5;
78 circular_buffer<MyInteger>::iterator it3 = cb.begin() + 9;
79 it1++;
80 it2++;
81 ++it3;
82
83 BOOST_CHECK(it1 == cb.begin() + 1);
84 BOOST_CHECK(it2 == cb.begin() + 6);
85 BOOST_CHECK(it3 == cb.end());
86}
87
88void iterator_decrement_test() {
89
90 circular_buffer<MyInteger> cb(10, 1);
91 cb.push_back(2);
92 circular_buffer<MyInteger>::iterator it1= cb.end();
93 circular_buffer<MyInteger>::iterator it2= cb.end() - 5;
94 circular_buffer<MyInteger>::iterator it3= cb.end() - 9;
95 --it1;
96 it2--;
97 --it3;
98
99 BOOST_CHECK(it1 == cb.end() - 1);
100 BOOST_CHECK(it2 == cb.end() - 6);
101 BOOST_CHECK(it3 == cb.begin());
102}
103
104void iterator_addition_test() {
105
106 circular_buffer<MyInteger> cb(10, 1);
107 cb.push_back(2);
108 cb.push_back(2);
109 circular_buffer<MyInteger>::iterator it1 = cb.begin() + 2;
110 circular_buffer<MyInteger>::iterator it2 = cb.end();
111 circular_buffer<MyInteger>::iterator it3 = cb.begin() + 5;
112 circular_buffer<MyInteger>::iterator it4 = cb.begin() + 9;
113 it1 += 3;
114 it2 += 0;
115 it3 += 5;
116 it4 += -2;
117
118 BOOST_CHECK(it1 == 5 + cb.begin());
119 BOOST_CHECK(it2 == cb.end());
120 BOOST_CHECK(it3 == cb.end());
121 BOOST_CHECK(it4 + 3 == cb.end());
122 BOOST_CHECK((-3) + it4 == cb.begin() + 4);
123 BOOST_CHECK(cb.begin() + 0 == cb.begin());
124}
125
126void iterator_subtraction_test() {
127
128 circular_buffer<MyInteger> cb(10, 1);
129 cb.push_back(2);
130 cb.push_back(2);
131 cb.push_back(2);
132 circular_buffer<MyInteger>::iterator it1 = cb.begin();
133 circular_buffer<MyInteger>::iterator it2 = cb.end();
134 circular_buffer<MyInteger>::iterator it3 = cb.end() - 5;
135 circular_buffer<MyInteger>::iterator it4 = cb.begin() + 7;
136 it1 -= -2;
137 it2 -= 0;
138 it3 -= 5;
139
140 BOOST_CHECK(it1 == cb.begin() + 2);
141 BOOST_CHECK(it2 == cb.end());
142 BOOST_CHECK(it3 == cb.begin());
143 BOOST_CHECK(it4 - 7 == cb.begin());
144 BOOST_CHECK(it4 - (-3) == cb.end());
145 BOOST_CHECK(cb.begin() - 0 == cb.begin());
146}
147
148void iterator_element_access_test() {
149
150 circular_buffer<MyInteger> cb(10);
151 cb.push_back(1);
152 cb.push_back(2);
153 cb.push_back(3);
154 cb.push_back(4);
155 cb.push_back(5);
156 cb.push_back(6);
157 circular_buffer<MyInteger>::iterator it = cb.begin() + 1;
158
159 BOOST_CHECK(it[0] == 2);
160 BOOST_CHECK(it[-1] == 1);
161 BOOST_CHECK(it[2] == 4);
162}
163
164void iterator_comparison_test() {
165
166 circular_buffer<MyInteger> cb(5, 1);
167 cb.push_back(2);
168 circular_buffer<MyInteger>::iterator it = cb.begin() + 2;
169 circular_buffer<MyInteger>::const_iterator begin = cb.begin();
170 circular_buffer<MyInteger>::iterator end = cb.end();
171
172 BOOST_CHECK(begin == begin);
173 BOOST_CHECK(end > cb.begin());
174 BOOST_CHECK(begin < end);
175 BOOST_CHECK(end > begin);
176 BOOST_CHECK(end == end);
177 BOOST_CHECK(begin < cb.end());
178 BOOST_CHECK(!(begin + 1 > cb.end()));
179 BOOST_CHECK(it > cb.begin());
180 BOOST_CHECK(end > it);
181 BOOST_CHECK(begin >= begin);
182 BOOST_CHECK(end >= cb.begin());
183 BOOST_CHECK(end <= end);
184 BOOST_CHECK(begin <= cb.end());
185 BOOST_CHECK(it >= cb.begin());
186 BOOST_CHECK(end >= it);
187 BOOST_CHECK(!(begin + 4 < begin + 4));
188 BOOST_CHECK(begin + 4 < begin + 5);
189 BOOST_CHECK(!(begin + 5 < begin + 4));
190 BOOST_CHECK(it < end - 1);
191 BOOST_CHECK(!(end - 1 < it));
192}
193
194void iterator_invalidation_test() {
195
196#if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
197
198 circular_buffer<MyInteger>::iterator it1;
199 circular_buffer<MyInteger>::const_iterator it2;
200 circular_buffer<MyInteger>::iterator it3;
201 circular_buffer<MyInteger>::const_iterator it4;
202 circular_buffer<MyInteger>::const_iterator it5;
203 circular_buffer<MyInteger>::const_iterator it6;
204
205 BOOST_CHECK(it1.is_valid(0));
206 BOOST_CHECK(it2.is_valid(0));
207 BOOST_CHECK(it3.is_valid(0));
208 BOOST_CHECK(it4.is_valid(0));
209 BOOST_CHECK(it5.is_valid(0));
210 BOOST_CHECK(it6.is_valid(0));
211
212 {
213 circular_buffer<MyInteger> cb(5, 0);
214 const circular_buffer<MyInteger> ccb(5, 0);
215
216 it1 = cb.begin();
217 it2 = ccb.begin();
218 it3 = cb.end();
219 it4 = it1;
220 it5 = it2;
221 it6 = it1;
222
223 BOOST_CHECK(it1.is_valid(&cb));
224 BOOST_CHECK(it2.is_valid(&ccb));
225 BOOST_CHECK(it3.is_valid(&cb));
226 BOOST_CHECK(it4.is_valid(&cb));
227 BOOST_CHECK(it5.is_valid(&ccb));
228 BOOST_CHECK(it6.is_valid(&cb));
229 }
230
231 BOOST_CHECK(it1.is_valid(0));
232 BOOST_CHECK(it2.is_valid(0));
233 BOOST_CHECK(it3.is_valid(0));
234 BOOST_CHECK(it4.is_valid(0));
235 BOOST_CHECK(it5.is_valid(0));
236 BOOST_CHECK(it6.is_valid(0));
237
238 circular_buffer<MyInteger> cb(10, 0);
239 it1 = cb.end();
240 cb.clear();
241 BOOST_CHECK(it1.is_valid(&cb));
242 cb.push_back(1);
243 cb.push_back(2);
244 cb.push_back(3);
245 int i = 0;
246 for (it2 = cb.begin(); it2 != it1; it2++, i++);
247 BOOST_CHECK(i == 3);
248
249 circular_buffer<MyInteger> cb1(10, 0);
250 circular_buffer<MyInteger> cb2(20, 0);
251 it1 = cb1.end();
252 it2 = cb2.begin();
253 BOOST_CHECK(it1.is_valid(&cb1));
254 BOOST_CHECK(it2.is_valid(&cb2));
255
256 cb1.swap(cb2);
257 BOOST_CHECK(!it1.is_valid(&cb1));
258 BOOST_CHECK(!it2.is_valid(&cb2));
259
260 it1 = cb1.begin() + 3;
261 it2 = cb1.begin();
262 cb1.push_back(1);
263 BOOST_CHECK(it1.is_valid(&cb1));
264 BOOST_CHECK(!it2.is_valid(&cb1));
265 BOOST_CHECK(*it2.m_it == 1);
266
267 circular_buffer<MyInteger> cb3(5);
268 cb3.push_back(1);
269 cb3.push_back(2);
270 cb3.push_back(3);
271 cb3.push_back(4);
272 cb3.push_back(5);
273 it1 = cb3.begin() + 2;
274 it2 = cb3.begin();
275 cb3.insert(cb3.begin() + 3, 6);
276 BOOST_CHECK(it1.is_valid(&cb3));
277 BOOST_CHECK(!it2.is_valid(&cb3));
278 BOOST_CHECK(*it2.m_it == 5);
279
280 it1 = cb3.begin() + 3;
281 it2 = cb3.end() - 1;
282 cb3.push_front(7);
283 BOOST_CHECK(it1.is_valid(&cb3));
284 BOOST_CHECK(!it2.is_valid(&cb3));
285 BOOST_CHECK(*it2.m_it == 7);
286
287 circular_buffer<MyInteger> cb4(5);
288 cb4.push_back(1);
289 cb4.push_back(2);
290 cb4.push_back(3);
291 cb4.push_back(4);
292 cb4.push_back(5);
293 it1 = cb4.begin() + 3;
294 it2 = cb4.begin();
295 cb4.rinsert(cb4.begin() + 2, 6);
296 BOOST_CHECK(it1.is_valid(&cb4));
297 BOOST_CHECK(!it2.is_valid(&cb4));
298 BOOST_CHECK(*it2.m_it == 2);
299
300 it1 = cb1.begin() + 5;
301 it2 = cb1.end() - 1;
302 cb1.pop_back();
303 BOOST_CHECK(it1.is_valid(&cb1));
304 BOOST_CHECK(!it2.is_valid(&cb1));
305
306 it1 = cb1.begin() + 5;
307 it2 = cb1.begin();
308 cb1.pop_front();
309 BOOST_CHECK(it1.is_valid(&cb1));
310 BOOST_CHECK(!it2.is_valid(&cb1));
311
312 circular_buffer<MyInteger> cb5(20, 0);
313 it1 = cb5.begin() + 5;
314 it2 = it3 = cb5.begin() + 15;
315 cb5.erase(cb5.begin() + 10);
316 BOOST_CHECK(it1.is_valid(&cb5));
317 BOOST_CHECK(!it2.is_valid(&cb5));
318 BOOST_CHECK(!it3.is_valid(&cb5));
319
320 it1 = cb5.begin() + 1;
321 it2 = it3 = cb5.begin() + 8;
322 cb5.erase(cb5.begin() + 3, cb5.begin() + 7);
323 BOOST_CHECK(it1.is_valid(&cb5));
324 BOOST_CHECK(!it2.is_valid(&cb5));
325 BOOST_CHECK(!it3.is_valid(&cb5));
326
327 circular_buffer<MyInteger> cb6(20, 0);
328 it4 = it1 = cb6.begin() + 5;
329 it2 = cb6.begin() + 15;
330 cb6.rerase(cb6.begin() + 10);
331 BOOST_CHECK(!it1.is_valid(&cb6));
332 BOOST_CHECK(!it4.is_valid(&cb6));
333 BOOST_CHECK(it2.is_valid(&cb6));
334
335 it4 = it1 = cb6.begin() + 1;
336 it2 = cb6.begin() + 8;
337 cb6.rerase(cb6.begin() + 3, cb6.begin() + 7);
338 BOOST_CHECK(!it1.is_valid(&cb6));
339 BOOST_CHECK(!it4.is_valid(&cb6));
340 BOOST_CHECK(it2.is_valid(&cb6));
341
342 circular_buffer<MyInteger> cb7(10, 1);
343 cb7.push_back(2);
344 cb7.push_back(3);
345 cb7.push_back(4);
346 it1 = cb7.end();
347 it2 = cb7.begin();
348 it3 = cb7.begin() + 6;
349 cb7.linearize();
350 BOOST_CHECK(it1.is_valid(&cb7));
351 BOOST_CHECK(!it2.is_valid(&cb7));
352 BOOST_CHECK(!it3.is_valid(&cb7));
353 it1 = cb7.end();
354 it2 = cb7.begin();
355 it3 = cb7.begin() + 6;
356 cb7.linearize();
357 BOOST_CHECK(it1.is_valid(&cb7));
358 BOOST_CHECK(it2.is_valid(&cb7));
359 BOOST_CHECK(it3.is_valid(&cb7));
360
361 cb7.push_back(5);
362 cb7.push_back(6);
363 it1 = cb7.end();
364 it2 = cb7.begin();
365 it3 = cb7.begin() + 6;
366 cb7.set_capacity(10);
367 BOOST_CHECK(it1.is_valid(&cb7));
368 BOOST_CHECK(it2.is_valid(&cb7));
369 BOOST_CHECK(it3.is_valid(&cb7));
370 cb7.set_capacity(20);
371 BOOST_CHECK(it1.is_valid(&cb7));
372 BOOST_CHECK(!it2.is_valid(&cb7));
373 BOOST_CHECK(!it3.is_valid(&cb7));
374 cb7.push_back(7);
375 it1 = cb7.end();
376 it2 = cb7.begin();
377 it3 = cb7.begin() + 6;
378 cb7.set_capacity(10);
379 BOOST_CHECK(it1.is_valid(&cb7));
380 BOOST_CHECK(!it2.is_valid(&cb7));
381 BOOST_CHECK(!it3.is_valid(&cb7));
382
383 cb7.push_back(8);
384 cb7.push_back(9);
385 it1 = cb7.end();
386 it2 = cb7.begin();
387 it3 = cb7.begin() + 6;
388 cb7.rset_capacity(10);
389 BOOST_CHECK(it1.is_valid(&cb7));
390 BOOST_CHECK(it2.is_valid(&cb7));
391 BOOST_CHECK(it3.is_valid(&cb7));
392 cb7.rset_capacity(20);
393 BOOST_CHECK(it1.is_valid(&cb7));
394 BOOST_CHECK(!it2.is_valid(&cb7));
395 BOOST_CHECK(!it3.is_valid(&cb7));
396 cb7.push_back(10);
397 it1 = cb7.end();
398 it2 = cb7.begin();
399 it3 = cb7.begin() + 6;
400 cb7.rset_capacity(10);
401 BOOST_CHECK(it1.is_valid(&cb7));
402 BOOST_CHECK(!it2.is_valid(&cb7));
403 BOOST_CHECK(!it3.is_valid(&cb7));
404
405 circular_buffer<MyInteger> cb8(10, 1);
406 cb8.push_back(2);
407 cb8.push_back(3);
408 it1 = cb8.end();
409 it2 = cb8.begin();
410 it3 = cb8.begin() + 6;
411 cb8.resize(10);
412 BOOST_CHECK(it1.is_valid(&cb8));
413 BOOST_CHECK(it2.is_valid(&cb8));
414 BOOST_CHECK(it3.is_valid(&cb8));
415 cb8.resize(20);
416 BOOST_CHECK(it1.is_valid(&cb8));
417 BOOST_CHECK(!it2.is_valid(&cb8));
418 BOOST_CHECK(!it3.is_valid(&cb8));
419 cb8.push_back(4);
420 it1 = cb8.end();
421 it2 = cb8.begin();
422 it3 = cb8.begin() + 6;
423 it4 = cb8.begin() + 12;
424 cb8.resize(10);
425 BOOST_CHECK(it1.is_valid(&cb8));
426 BOOST_CHECK(it2.is_valid(&cb8));
427 BOOST_CHECK(it3.is_valid(&cb8));
428 BOOST_CHECK(!it4.is_valid(&cb8));
429
430 cb8.set_capacity(10);
431 cb8.push_back(5);
432 cb8.push_back(6);
433 it1 = cb8.end();
434 it2 = cb8.begin();
435 it3 = cb8.begin() + 6;
436 cb8.rresize(10);
437 BOOST_CHECK(it1.is_valid(&cb8));
438 BOOST_CHECK(it2.is_valid(&cb8));
439 BOOST_CHECK(it3.is_valid(&cb8));
440 cb8.rresize(20);
441 BOOST_CHECK(it1.is_valid(&cb8));
442 BOOST_CHECK(!it2.is_valid(&cb8));
443 BOOST_CHECK(!it3.is_valid(&cb8));
444 cb8.push_back(7);
445 it1 = cb8.end();
446 it2 = cb8.begin();
447 it3 = cb8.begin() + 6;
448 it4 = cb8.begin() + 12;
449 cb8.rresize(10);
450 BOOST_CHECK(it1.is_valid(&cb8));
451 BOOST_CHECK(!it2.is_valid(&cb8));
452 BOOST_CHECK(!it3.is_valid(&cb8));
453 BOOST_CHECK(it4.is_valid(&cb8));
454
455 circular_buffer<MyInteger> cb9(15, 1);
456 it1 = cb9.end();
457 it2 = cb9.begin();
458 it3 = cb9.begin() + 6;
459 it4 = cb9.begin() + 12;
460 cb9 = cb8;
461 BOOST_CHECK(it1.is_valid(&cb9));
462 BOOST_CHECK(!it2.is_valid(&cb9));
463 BOOST_CHECK(!it3.is_valid(&cb9));
464 BOOST_CHECK(!it4.is_valid(&cb9));
465
466 circular_buffer<MyInteger> cb10(10, 1);
467 it1 = cb10.end();
468 it2 = cb10.begin();
469 it3 = cb10.begin() + 3;
470 it4 = cb10.begin() + 7;
471 cb10.assign(5, 2);
472 BOOST_CHECK(it1.is_valid(&cb10));
473 BOOST_CHECK(!it2.is_valid(&cb10));
474 BOOST_CHECK(!it3.is_valid(&cb10));
475 BOOST_CHECK(!it4.is_valid(&cb10));
476
477 circular_buffer<MyInteger> cb11(10, 1);
478 it1 = cb11.end();
479 it2 = cb11.begin();
480 it3 = cb11.begin() + 3;
481 it4 = cb11.begin() + 7;
482 cb11.assign(15, 5, 2);
483 BOOST_CHECK(it1.is_valid(&cb11));
484 BOOST_CHECK(!it2.is_valid(&cb11));
485 BOOST_CHECK(!it3.is_valid(&cb11));
486 BOOST_CHECK(!it4.is_valid(&cb11));
487
488 circular_buffer<MyInteger> cb12(10, 1);
489 it1 = cb12.end();
490 it2 = cb12.begin();
491 it3 = cb12.begin() + 3;
492 it4 = cb12.begin() + 7;
493 cb12.assign(cb11.begin(), cb11.end());
494 BOOST_CHECK(it1.is_valid(&cb12));
495 BOOST_CHECK(!it2.is_valid(&cb12));
496 BOOST_CHECK(!it3.is_valid(&cb12));
497 BOOST_CHECK(!it4.is_valid(&cb12));
498
499 circular_buffer<MyInteger> cb13(10, 1);
500 it1 = cb13.end();
501 it2 = cb13.begin();
502 it3 = cb13.begin() + 3;
503 it4 = cb13.begin() + 7;
504 cb13.assign(15, cb11.begin(), cb11.end());
505 BOOST_CHECK(it1.is_valid(&cb13));
506 BOOST_CHECK(!it2.is_valid(&cb13));
507 BOOST_CHECK(!it3.is_valid(&cb13));
508 BOOST_CHECK(!it4.is_valid(&cb13));
509
510 circular_buffer<MyInteger> cb14(10);
511 cb14.push_back(1);
512 cb14.push_back(2);
513 cb14.push_back(3);
514 cb14.push_back(4);
515 cb14.push_back(5);
516 cb14.push_back(6);
517 cb14.push_back(7);
518 it1 = cb14.end();
519 it2 = cb14.begin() + 2;
520 it3 = cb14.begin() + 1;
521 it4 = cb14.begin() + 5;
522 cb14.rotate(it2);
523 BOOST_CHECK(it1.is_valid(&cb14));
524 BOOST_CHECK(it2.is_valid(&cb14));
525 BOOST_CHECK(!it3.is_valid(&cb14));
526 BOOST_CHECK(it4.is_valid(&cb14));
527
528 circular_buffer<MyInteger> cb15(7);
529 cb15.push_back(1);
530 cb15.push_back(2);
531 cb15.push_back(3);
532 cb15.push_back(4);
533 cb15.push_back(5);
534 cb15.push_back(6);
535 cb15.push_back(7);
536 cb15.push_back(8);
537 cb15.push_back(9);
538 it1 = cb15.end();
539 it2 = cb15.begin() + 2;
540 it3 = cb15.begin() + 1;
541 it4 = cb15.begin() + 5;
542 cb15.rotate(it3);
543 BOOST_CHECK(it1.is_valid(&cb15));
544 BOOST_CHECK(it2.is_valid(&cb15));
545 BOOST_CHECK(it3.is_valid(&cb15));
546 BOOST_CHECK(it4.is_valid(&cb15));
547
548 circular_buffer<MyInteger> cb16(10);
549 cb16.push_back(1);
550 cb16.push_back(2);
551 cb16.push_back(3);
552 cb16.push_back(4);
553 cb16.push_back(5);
554 cb16.push_back(6);
555 cb16.push_back(7);
556 it1 = cb16.end();
557 it2 = cb16.begin() + 6;
558 it3 = cb16.begin();
559 it4 = cb16.begin() + 5;
560 cb16.rotate(it4);
561 BOOST_CHECK(it1.is_valid(&cb16));
562 BOOST_CHECK(!it2.is_valid(&cb16));
563 BOOST_CHECK(it3.is_valid(&cb16));
564 BOOST_CHECK(!it4.is_valid(&cb16));
565
566#endif // #if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
567}
568
569// basic exception safety test (it is useful to use any memory-leak detection tool)
570void exception_safety_test() {
571
572#if !defined(BOOST_NO_EXCEPTIONS)
573
574 circular_buffer<MyInteger> cb1(3, 5);
575 MyInteger::set_exception_trigger(3);
576 BOOST_CHECK_THROW(cb1.set_capacity(5), std::exception);
577 BOOST_CHECK(cb1.capacity() == 3);
578 MyInteger::set_exception_trigger(3);
579 BOOST_CHECK_THROW(cb1.rset_capacity(5), std::exception);
580 BOOST_CHECK(cb1.capacity() == 3);
581 generic_test(cb1);
582
583 MyInteger::set_exception_trigger(3);
584 BOOST_CHECK_THROW(circular_buffer<MyInteger> cb2(5, 10), std::exception);
585
586 circular_buffer<MyInteger> cb3(5, 10);
587 MyInteger::set_exception_trigger(3);
588 BOOST_CHECK_THROW(circular_buffer<MyInteger> cb4(cb3), std::exception);
589
590 vector<MyInteger> v(5, MyInteger(10));
591 MyInteger::set_exception_trigger(3);
592 BOOST_CHECK_THROW(circular_buffer<MyInteger> cb5(8, v.begin(), v.end()), std::exception);
593
594 circular_buffer<MyInteger> cb6(5, 10);
595 circular_buffer<MyInteger> cb7(8, 3);
596 MyInteger::set_exception_trigger(3);
597 BOOST_CHECK_THROW(cb7 = cb6, std::exception);
598 BOOST_CHECK(cb7.size() == 8);
599 BOOST_CHECK(cb7.capacity() == 8);
600 BOOST_CHECK(cb7[0] == 3);
601 BOOST_CHECK(cb7[7] == 3);
602 generic_test(cb7);
603
604 circular_buffer<MyInteger> cb8(5, 10);
605 MyInteger::set_exception_trigger(2);
606 BOOST_CHECK_THROW(cb8.push_front(1), std::exception);
607
608 circular_buffer<MyInteger> cb9(5);
609 cb9.push_back(1);
610 cb9.push_back(2);
611 cb9.push_back(3);
612 MyInteger::set_exception_trigger(3);
613 BOOST_CHECK_THROW(cb9.insert(cb9.begin() + 1, 4), std::exception);
614
615 circular_buffer<MyInteger> cb10(5);
616 cb10.push_back(1);
617 cb10.push_back(2);
618 cb10.push_back(3);
619 MyInteger::set_exception_trigger(3);
620 BOOST_CHECK_THROW(cb10.rinsert(cb10.begin() + 1, 4), std::exception);
621
622 circular_buffer<MyInteger> cb11(5);
623 cb11.push_back(1);
624 cb11.push_back(2);
625 MyInteger::set_exception_trigger(2);
626 BOOST_CHECK_THROW(cb11.rinsert(cb11.begin(), 1), std::exception);
627
628 circular_buffer<MyInteger> cb12(5, 1);
629 MyInteger::set_exception_trigger(3);
630 BOOST_CHECK_THROW(cb12.assign(4, 2), std::exception);
631
632 circular_buffer<MyInteger> cb13(5, 1);
633 MyInteger::set_exception_trigger(3);
634 BOOST_CHECK_THROW(cb13.assign(6, 2), std::exception);
635
636 circular_buffer<MyInteger> cb14(5);
637 cb14.push_back(1);
638 cb14.push_back(2);
639 MyInteger::set_exception_trigger(3);
640 BOOST_CHECK_THROW(cb14.insert(cb14.begin(), 10, 3), std::exception);
641
642 circular_buffer<MyInteger> cb15(5);
643 cb15.push_back(1);
644 cb15.push_back(2);
645 MyInteger::set_exception_trigger(3);
646 BOOST_CHECK_THROW(cb15.insert(cb15.end(), 10, 3), std::exception);
647
648 circular_buffer<MyInteger> cb16(5);
649 cb16.push_back(1);
650 cb16.push_back(2);
651 MyInteger::set_exception_trigger(3);
652 BOOST_CHECK_THROW(cb16.rinsert(cb16.begin(), 10, 3), std::exception);
653
654 circular_buffer<MyInteger> cb17(5);
655 cb17.push_back(1);
656 cb17.push_back(2);
657 MyInteger::set_exception_trigger(3);
658 BOOST_CHECK_THROW(cb17.rinsert(cb17.end(), 10, 3), std::exception);
659
660 circular_buffer<MyInteger> cb18(5, 0);
661 cb18.push_back(1);
662 cb18.push_back(2);
663 cb18.pop_front();
664 MyInteger::set_exception_trigger(4);
665 BOOST_CHECK_THROW(cb18.linearize(), std::exception);
666
667 circular_buffer<MyInteger> cb19(5, 0);
668 cb19.push_back(1);
669 cb19.push_back(2);
670 MyInteger::set_exception_trigger(5);
671 BOOST_CHECK_THROW(cb19.linearize(), std::exception);
672
673 circular_buffer<MyInteger> cb20(5, 0);
674 cb20.push_back(1);
675 cb20.push_back(2);
676 MyInteger::set_exception_trigger(6);
677 BOOST_CHECK_THROW(cb20.linearize(), std::exception);
678
679 circular_buffer<MyInteger> cb21(5);
680 cb21.push_back(1);
681 cb21.push_back(2);
682 cb21.push_back(3);
683 MyInteger::set_exception_trigger(2);
684 BOOST_CHECK_THROW(cb21.insert(cb21.begin() + 1, 4), std::exception);
685
686 circular_buffer<MyInteger> cb22(5);
687 cb22.push_back(1);
688 cb22.push_back(2);
689 cb22.push_back(3);
690 MyInteger::set_exception_trigger(2);
691 BOOST_CHECK_THROW(cb22.insert(cb22.end(), 4), std::exception);
692
693 circular_buffer<MyInteger> cb23(5, 0);
694 MyInteger::set_exception_trigger(2);
695 BOOST_CHECK_THROW(cb23.insert(cb23.begin() + 1, 4), std::exception);
696
697 circular_buffer<MyInteger> cb24(5);
698 cb24.push_back(1);
699 cb24.push_back(2);
700 cb24.push_back(3);
701 MyInteger::set_exception_trigger(2);
702 BOOST_CHECK_THROW(cb24.rinsert(cb24.begin() + 1, 4), std::exception);
703
704 circular_buffer<MyInteger> cb25(5, 0);
705 MyInteger::set_exception_trigger(2);
706 BOOST_CHECK_THROW(cb25.rinsert(cb25.begin() + 3, 4), std::exception);
707
708 circular_buffer<MyInteger> cb26(5);
709 cb26.push_back(1);
710 cb26.push_back(2);
711 MyInteger::set_exception_trigger(5);
712 BOOST_CHECK_THROW(cb26.insert(cb26.begin(), 10, 3), std::exception);
713
714 circular_buffer<MyInteger> cb27(5);
715 cb27.push_back(1);
716 cb27.push_back(2);
717 MyInteger::set_exception_trigger(5);
718 BOOST_CHECK_THROW(cb27.insert(cb27.end(), 10, 3), std::exception);
719
720 circular_buffer<MyInteger> cb28(5);
721 cb28.push_back(1);
722 cb28.push_back(2);
723 MyInteger::set_exception_trigger(5);
724 BOOST_CHECK_THROW(cb28.rinsert(cb28.begin(), 10, 3), std::exception);
725
726 circular_buffer<MyInteger> cb29(5);
727 cb29.push_back(1);
728 cb29.push_back(2);
729 MyInteger::set_exception_trigger(5);
730 BOOST_CHECK_THROW(cb29.rinsert(cb29.end(), 10, 3), std::exception);
731
732 circular_buffer<MyInteger> cb30(10);
733 cb30.push_back(1);
734 cb30.push_back(2);
735 cb30.push_back(3);
736 MyInteger::set_exception_trigger(2);
737 BOOST_CHECK_THROW(cb30.rinsert(cb30.begin(), 10, 3), std::exception);
738
739#endif // #if !defined(BOOST_NO_EXCEPTIONS)
740}
741
742
743void move_container_values_except() {
744 move_container_values_impl<noncopyable_movable_except_t>();
745}
746
747template <class T>
748void move_container_values_resetting_impl() {
749 typedef T noncopyable_movable_test_t;
750 CB_CONTAINER<noncopyable_movable_test_t> cb1(1);
751 noncopyable_movable_test_t var;
752 cb1.push_back();
753
754 cb1.push_back(boost::move(var));
755 BOOST_CHECK(!cb1.back().is_moved());
756 BOOST_CHECK(var.is_moved());
757 BOOST_CHECK(cb1.size() == 1);
758 var = boost::move(cb1.back());
759 BOOST_CHECK(cb1.back().is_moved());
760
761 cb1.push_front(boost::move(var));
762 BOOST_CHECK(!cb1.front().is_moved());
763 BOOST_CHECK(var.is_moved());
764 BOOST_CHECK(cb1.size() == 1);
765 var = boost::move(cb1.back());
766 BOOST_CHECK(cb1.back().is_moved());
767
768 cb1.push_back();
769 BOOST_CHECK(!cb1.back().is_moved());
770 BOOST_CHECK(cb1.size() == 1);
771 var = boost::move(cb1.back());
772 BOOST_CHECK(cb1.back().is_moved());
773
774 cb1.push_front();
775 BOOST_CHECK(!cb1.front().is_moved());
776 BOOST_CHECK(cb1.size() == 1);
777 var = boost::move(cb1.back());
778 BOOST_CHECK(cb1.back().is_moved());
779
780
781 cb1.insert(cb1.begin());
782 // If the circular_buffer is full and the pos points to begin(),
783 // then the item will not be inserted.
784 BOOST_CHECK(cb1.front().is_moved());
785 BOOST_CHECK(cb1.size() == 1);
786 var = boost::move(cb1.back());
787 BOOST_CHECK(cb1.back().is_moved());
788
789 cb1.insert(cb1.begin(), boost::move(var));
790 // If the circular_buffer is full and the pos points to begin(),
791 // then the item will not be inserted.
792 BOOST_CHECK(cb1.front().is_moved());
793 BOOST_CHECK(cb1.size() == 1);
794 var = boost::move(cb1.back());
795 BOOST_CHECK(cb1.back().is_moved());
796
797 cb1.rinsert(cb1.begin());
798 BOOST_CHECK(!cb1.back().is_moved());
799 BOOST_CHECK(cb1.size() == 1);
800 var = boost::move(cb1.back());
801 BOOST_CHECK(cb1.back().is_moved());
802
803 var.reinit();
804 cb1.rinsert(cb1.begin(), boost::move(var));
805 BOOST_CHECK(!cb1.back().is_moved());
806 BOOST_CHECK(cb1.size() == 1);
807 var = boost::move(cb1.back());
808 BOOST_CHECK(cb1.back().is_moved());
809
810 cb1.rinsert(cb1.end());
811 BOOST_CHECK(cb1.back().is_moved());
812 BOOST_CHECK(cb1.size() == 1);
813 var = boost::move(cb1.back());
814 BOOST_CHECK(cb1.back().is_moved());
815
816 var.reinit();
817 cb1.rinsert(cb1.end(), boost::move(var));
818 BOOST_CHECK(cb1.back().is_moved());
819 BOOST_CHECK(cb1.size() == 1);
820 var = boost::move(cb1.back());
821 BOOST_CHECK(cb1.back().is_moved());
822 cb1.push_back();
823 BOOST_CHECK(!cb1[0].is_moved());
824
825 const int val = cb1[0].value();
826 cb1.linearize();
827 BOOST_CHECK(!cb1[0].is_moved());
828 BOOST_CHECK(cb1[0].value() == val);
829
830 cb1.rotate(cb1.begin());
831 BOOST_CHECK(!cb1[0].is_moved());
832 BOOST_CHECK(cb1[0].value() == val);
833}
834
835void move_container_values_resetting_except() {
836 move_container_values_resetting_impl<noncopyable_movable_except_t>();
837}
838
839void move_container_values_resetting_noexcept() {
840 move_container_values_resetting_impl<noncopyable_movable_noexcept_t>();
841}
842
843// test main
844test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) {
845
846 test_suite* tests = BOOST_TEST_SUITE("Unit tests for the circular_buffer.");
847 add_common_tests(tests);
848
849 tests->add(BOOST_TEST_CASE(&iterator_constructor_and_assign_test));
850 tests->add(BOOST_TEST_CASE(&iterator_reference_test));
851 tests->add(BOOST_TEST_CASE(&iterator_difference_test));
852 tests->add(BOOST_TEST_CASE(&iterator_increment_test));
853 tests->add(BOOST_TEST_CASE(&iterator_decrement_test));
854 tests->add(BOOST_TEST_CASE(&iterator_addition_test));
855 tests->add(BOOST_TEST_CASE(&iterator_subtraction_test));
856 tests->add(BOOST_TEST_CASE(&iterator_element_access_test));
857 tests->add(BOOST_TEST_CASE(&iterator_comparison_test));
858 tests->add(BOOST_TEST_CASE(&iterator_invalidation_test));
859 tests->add(BOOST_TEST_CASE(&exception_safety_test));
860 tests->add(BOOST_TEST_CASE(&move_container_values_except));
861 tests->add(BOOST_TEST_CASE(&move_container_values_resetting_except));
862 tests->add(BOOST_TEST_CASE(&move_container_values_resetting_noexcept));
863
864 return tests;
865}
866