• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Demonstration of rules when an iterator is considered to be valid if the soft
2 // iterator invalidation definition is applied.
3 // Note: The soft iterator invalidation definition CAN NOT be applied
4 //       to the space optimized circular buffer.
5 
6 // Copyright (c) 2003-2008 Jan Gaspar
7 
8 // Use, modification, and distribution is subject to the Boost Software
9 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
10 // http://www.boost.org/LICENSE_1_0.txt)
11 
12 #include "test.hpp"
13 
14 // test of the example (introduced in the documentation)
validity_example_test()15 void validity_example_test() {
16 
17     circular_buffer<int> cb(3);
18 
19     cb.push_back(1);
20     cb.push_back(2);
21     cb.push_back(3);
22 
23     circular_buffer<int>::iterator it = cb.begin();
24 
25     BOOST_TEST(*it == 1);
26 
27     cb.push_back(4);
28 
29     BOOST_TEST(*it == 4);
30 }
31 
validity_insert_test()32 void validity_insert_test() {
33 
34     int array[] = { 1, 2, 3 };
35 
36     // memory placement: { 1, 2, 3 }
37     // circular buffer:  { 1, 2, 3 }
38     circular_buffer<int> cb(4, array, array + 3);
39 
40     // it1 -> 1, it2 -> 2, it3 -> 3
41     circular_buffer<int>::iterator it1 = cb.begin();
42     circular_buffer<int>::iterator it2 = cb.begin() + 1;
43     circular_buffer<int>::iterator it3 = cb.begin() + 2;
44 
45     cb.insert(cb.begin() + 1, 4);
46 
47     // memory placement: { 1, 4, 2, 3 }
48     // circular buffer:  { 1, 4, 2, 3 }
49     BOOST_TEST(*it1 == 1);
50     BOOST_TEST(*it2 == 4);
51     BOOST_TEST(*it3 == 2);
52     BOOST_TEST(cb[0] == 1);
53     BOOST_TEST(cb[1] == 4);
54     BOOST_TEST(cb[2] == 2);
55     BOOST_TEST(cb[3] == 3);
56 
57     // it4 -> 3
58     circular_buffer<int>::iterator it4 = it1 + 3;
59 
60     cb.insert(cb.begin() + 1, 5);
61 
62     // memory placement: { 3, 5, 4, 2 }
63     // circular buffer:  { 5, 4, 2, 3 }
64     BOOST_TEST(*it1 == 3);
65     BOOST_TEST(*it2 == 5);
66     BOOST_TEST(*it3 == 4);
67     BOOST_TEST(*it4 == 2);
68     BOOST_TEST(cb[0] == 5);
69     BOOST_TEST(cb[1] == 4);
70     BOOST_TEST(cb[2] == 2);
71     BOOST_TEST(cb[3] == 3);
72 }
73 
validity_insert_n_test()74 void validity_insert_n_test() {
75 
76     // memory placement: { 1, 2, 3 }
77     // circular buffer:  { 1, 2, 3 }
78     circular_buffer<int> cb(5);
79     cb.push_back(1);
80     cb.push_back(2);
81     cb.push_back(3);
82 
83     // it1 -> 1, it2 -> 2, it3 -> 3
84     circular_buffer<int>::iterator it1 = cb.begin();
85     circular_buffer<int>::iterator it2 = cb.begin() + 1;
86     circular_buffer<int>::iterator it3 = cb.begin() + 2;
87 
88     cb.insert(cb.begin() + 1, 2, 4);
89 
90     // memory placement: { 1, 4, 4, 2, 3 }
91     // circular buffer:  { 1, 4, 4, 2, 3 }
92     BOOST_TEST(*it1 == 1);
93     BOOST_TEST(*it2 == 4);
94     BOOST_TEST(*it3 == 4);
95     BOOST_TEST(cb[0] == 1);
96     BOOST_TEST(cb[1] == 4);
97     BOOST_TEST(cb[2] == 4);
98     BOOST_TEST(cb[3] == 2);
99     BOOST_TEST(cb[4] == 3);
100 
101     // it4 -> 2, it5 -> 3
102     circular_buffer<int>::iterator it4 = it1 + 3;
103     circular_buffer<int>::iterator it5 = it1 + 4;
104 
105     cb.insert(cb.begin() + 1, 2, 5);
106 
107     // memory placement: { 3, 5, 4, 4, 2 } - 5 inserted only once
108     // circular buffer:  { 5, 4, 4, 2, 3 }
109     BOOST_TEST(*it1 == 3);
110     BOOST_TEST(*it2 == 5);
111     BOOST_TEST(*it3 == 4);
112     BOOST_TEST(*it4 == 4);
113     BOOST_TEST(*it5 == 2);
114     BOOST_TEST(cb[0] == 5);
115     BOOST_TEST(cb[1] == 4);
116     BOOST_TEST(cb[2] == 4);
117     BOOST_TEST(cb[3] == 2);
118     BOOST_TEST(cb[4] == 3);
119 }
120 
validity_insert_range_test()121 void validity_insert_range_test() {
122 
123     vector<int> v1;
124     v1.push_back(4);
125     v1.push_back(5);
126 
127     vector<int> v2;
128     v2.push_back(6);
129     v2.push_back(7);
130 
131 
132     // memory placement: { 1, 2, 3 }
133     // circular buffer:  { 1, 2, 3 }
134     circular_buffer<int> cb1(5);
135     cb1.push_back(1);
136     cb1.push_back(2);
137     cb1.push_back(3);
138 
139     // it11 -> 1, it12 -> 2, it13 -> 3
140     circular_buffer<int>::iterator it11 = cb1.begin();
141     circular_buffer<int>::iterator it12 = cb1.begin() + 1;
142     circular_buffer<int>::iterator it13 = cb1.begin() + 2;
143 
144     cb1.insert(cb1.begin() + 1, v1.begin(), v1.end());
145 
146     // memory placement: { 1, 4, 5, 2, 3 }
147     // circular buffer:  { 1, 4, 5, 2, 3 }
148     BOOST_TEST(*it11 == 1);
149     BOOST_TEST(*it12 == 4);
150     BOOST_TEST(*it13 == 5);
151     BOOST_TEST(cb1[0] == 1);
152     BOOST_TEST(cb1[1] == 4);
153     BOOST_TEST(cb1[2] == 5);
154     BOOST_TEST(cb1[3] == 2);
155     BOOST_TEST(cb1[4] == 3);
156 
157     // it14 -> 2, it15 -> 3
158     circular_buffer<int>::iterator it14 = it11 + 3;
159     circular_buffer<int>::iterator it15 = it11 + 4;
160 
161     cb1.insert(cb1.begin() + 1, v2.begin(), v2.end());
162 
163     // memory placement: { 3, 7, 4, 5, 2 } - 7 inserted only
164     // circular buffer:  { 7, 4, 5, 2, 3 }
165     BOOST_TEST(*it11 == 3);
166     BOOST_TEST(*it12 == 7);
167     BOOST_TEST(*it13 == 4);
168     BOOST_TEST(*it14 == 5);
169     BOOST_TEST(*it15 == 2);
170     BOOST_TEST(cb1[0] == 7);
171     BOOST_TEST(cb1[1] == 4);
172     BOOST_TEST(cb1[2] == 5);
173     BOOST_TEST(cb1[3] == 2);
174     BOOST_TEST(cb1[4] == 3);
175 
176     // memory placement: { 1, 2, 3 }
177     // circular buffer:  { 1, 2, 3 }
178     circular_buffer<int> cb2(5);
179     cb2.push_back(1);
180     cb2.push_back(2);
181     cb2.push_back(3);
182 
183     // it21 -> 1, it22 -> 2, it23 -> 3
184     circular_buffer<int>::iterator it21 = cb2.begin();
185     circular_buffer<int>::iterator it22 = cb2.begin() + 1;
186     circular_buffer<int>::iterator it23 = cb2.begin() + 2;
187 
188     cb2.insert(cb2.begin() + 1, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
189 
190     // memory placement: { 1, 4, 5, 2, 3 }
191     // circular buffer:  { 1, 4, 5, 2, 3 }
192     BOOST_TEST(*it21 == 1);
193     BOOST_TEST(*it22 == 4);
194     BOOST_TEST(*it23 == 5);
195     BOOST_TEST(cb2[0] == 1);
196     BOOST_TEST(cb2[1] == 4);
197     BOOST_TEST(cb2[2] == 5);
198     BOOST_TEST(cb2[3] == 2);
199     BOOST_TEST(cb2[4] == 3);
200 
201     // it24 -> 2, it25 -> 3
202     circular_buffer<int>::iterator it24 = it21 + 3;
203     circular_buffer<int>::iterator it25 = it21 + 4;
204 
205     cb2.insert(cb2.begin() + 1, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
206 
207     // memory placement: { 2, 3, 7, 4, 5 } - using input iterator inserts all items even if they are later replaced
208     // circular buffer:  { 7, 4, 5, 2, 3 }
209     BOOST_TEST(*it21 == 2);
210     BOOST_TEST(*it22 == 3);
211     BOOST_TEST(*it23 == 7);
212     BOOST_TEST(*it24 == 4);
213     BOOST_TEST(*it25 == 5);
214     BOOST_TEST(cb2[0] == 7);
215     BOOST_TEST(cb2[1] == 4);
216     BOOST_TEST(cb2[2] == 5);
217     BOOST_TEST(cb2[3] == 2);
218     BOOST_TEST(cb2[4] == 3);
219 }
220 
validity_rinsert_test()221 void validity_rinsert_test() {
222 
223     int array[] = { 1, 2, 3 };
224 
225     // memory placement: { 1, 2, 3 }
226     // circular buffer:  { 1, 2, 3 }
227     circular_buffer<int> cb(4, array, array + 3);
228 
229     // it1 -> 1, it2 -> 2, it3 -> 3
230     circular_buffer<int>::iterator it1 = cb.begin();
231     circular_buffer<int>::iterator it2 = cb.begin() + 1;
232     circular_buffer<int>::iterator it3 = cb.begin() + 2;
233 
234     cb.rinsert(cb.begin() + 2, 4);
235 
236     // memory placement: { 2, 4, 3, 1 }
237     // circular buffer:  { 1, 2, 4, 3 }
238     BOOST_TEST(*it1 == 2);
239     BOOST_TEST(*it2 == 4);
240     BOOST_TEST(*it3 == 3);
241     BOOST_TEST(cb[0] == 1);
242     BOOST_TEST(cb[1] == 2);
243     BOOST_TEST(cb[2] == 4);
244     BOOST_TEST(cb[3] == 3);
245 
246     // it4 -> 1
247     circular_buffer<int>::iterator it4 = it1 - 1;
248 
249     cb.rinsert(cb.begin() + 2, 5);
250 
251     // memory placement: { 5, 4, 1, 2 }
252     // circular buffer:  { 1, 2, 5, 4 }
253     BOOST_TEST(*it1 == 5);
254     BOOST_TEST(*it2 == 4);
255     BOOST_TEST(*it3 == 1);
256     BOOST_TEST(*it4 == 2);
257     BOOST_TEST(cb[0] == 1);
258     BOOST_TEST(cb[1] == 2);
259     BOOST_TEST(cb[2] == 5);
260     BOOST_TEST(cb[3] == 4);
261 }
262 
validity_rinsert_n_test()263 void validity_rinsert_n_test() {
264 
265     // memory placement: { 1, 2, 3 }
266     // circular buffer:  { 1, 2, 3 }
267     circular_buffer<int> cb(5);
268     cb.push_back(1);
269     cb.push_back(2);
270     cb.push_back(3);
271 
272     // it1 -> 1, it2 -> 2, it3 -> 3
273     circular_buffer<int>::iterator it1 = cb.begin();
274     circular_buffer<int>::iterator it2 = cb.begin() + 1;
275     circular_buffer<int>::iterator it3 = cb.begin() + 2;
276 
277     cb.rinsert(cb.begin() + 2, 2, 4);
278 
279     // memory placement: { 4, 4, 3, 1, 2 }
280     // circular buffer:  { 1, 2, 4, 4, 3 }
281     BOOST_TEST(*it1 == 4);
282     BOOST_TEST(*it2 == 4);
283     BOOST_TEST(*it3 == 3);
284     BOOST_TEST(cb[0] == 1);
285     BOOST_TEST(cb[1] == 2);
286     BOOST_TEST(cb[2] == 4);
287     BOOST_TEST(cb[3] == 4);
288     BOOST_TEST(cb[4] == 3);
289 
290     // it4 -> 1, it5 -> 2
291     circular_buffer<int>::iterator it4 = it1 - 2;
292     circular_buffer<int>::iterator it5 = it1 - 1;
293 
294     cb.rinsert(cb.begin() + 4, 2, 5);
295 
296     // memory placement: { 4, 5, 1, 2, 4 } - 5 inserted only once
297     // circular buffer:  { 1, 2, 4, 4, 5 }
298     BOOST_TEST(*it1 == 4);
299     BOOST_TEST(*it2 == 5);
300     BOOST_TEST(*it3 == 1);
301     BOOST_TEST(*it4 == 2);
302     BOOST_TEST(*it5 == 4);
303     BOOST_TEST(cb[0] == 1);
304     BOOST_TEST(cb[1] == 2);
305     BOOST_TEST(cb[2] == 4);
306     BOOST_TEST(cb[3] == 4);
307     BOOST_TEST(cb[4] == 5);
308 }
309 
validity_rinsert_range_test()310 void validity_rinsert_range_test() {
311 
312     vector<int> v1;
313     v1.push_back(4);
314     v1.push_back(5);
315 
316     vector<int> v2;
317     v2.push_back(6);
318     v2.push_back(7);
319 
320 
321     // memory placement: { 1, 2, 3 }
322     // circular buffer:  { 1, 2, 3 }
323     circular_buffer<int> cb1(5);
324     cb1.push_back(1);
325     cb1.push_back(2);
326     cb1.push_back(3);
327 
328     // it1 -> 1, it2 -> 2, it3 -> 3
329     circular_buffer<int>::iterator it11 = cb1.begin();
330     circular_buffer<int>::iterator it12 = cb1.begin() + 1;
331     circular_buffer<int>::iterator it13 = cb1.begin() + 2;
332 
333     cb1.rinsert(cb1.begin() + 2, v1.begin(), v1.end());
334 
335     // memory placement: { 4, 5, 3, 1, 2 }
336     // circular buffer:  { 1, 2, 4, 5, 3 }
337     BOOST_TEST(*it11 == 4);
338     BOOST_TEST(*it12 == 5);
339     BOOST_TEST(*it13 == 3);
340     BOOST_TEST(cb1[0] == 1);
341     BOOST_TEST(cb1[1] == 2);
342     BOOST_TEST(cb1[2] == 4);
343     BOOST_TEST(cb1[3] == 5);
344     BOOST_TEST(cb1[4] == 3);
345 
346     // it14 -> 1, it15 -> 2
347     circular_buffer<int>::iterator it14 = it11 - 2;
348     circular_buffer<int>::iterator it15 = it11 - 1;
349 
350     cb1.rinsert(cb1.begin() + 4, v2.begin(), v2.end());
351 
352     // memory placement: { 5, 6, 1, 2, 4 } - 6 inserted only
353     // circular buffer:  { 1, 2, 4, 5, 6 }
354     BOOST_TEST(*it11 == 5);
355     BOOST_TEST(*it12 == 6);
356     BOOST_TEST(*it13 == 1);
357     BOOST_TEST(*it14 == 2);
358     BOOST_TEST(*it15 == 4);
359     BOOST_TEST(cb1[0] == 1);
360     BOOST_TEST(cb1[1] == 2);
361     BOOST_TEST(cb1[2] == 4);
362     BOOST_TEST(cb1[3] == 5);
363     BOOST_TEST(cb1[4] == 6);
364 
365     // memory placement: { 1, 2, 3 }
366     // circular buffer:  { 1, 2, 3 }
367     circular_buffer<int> cb2(5);
368     cb2.push_back(1);
369     cb2.push_back(2);
370     cb2.push_back(3);
371 
372     // it1 -> 1, it2 -> 2, it3 -> 3
373     circular_buffer<int>::iterator it21 = cb2.begin();
374     circular_buffer<int>::iterator it22 = cb2.begin() + 1;
375     circular_buffer<int>::iterator it23 = cb2.begin() + 2;
376 
377     cb2.rinsert(cb2.begin() + 2, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
378 
379     // memory placement: { 4, 5, 3, 1, 2 }
380     // circular buffer:  { 1, 2, 4, 5, 3 }
381     BOOST_TEST(*it21 == 4);
382     BOOST_TEST(*it22 == 5);
383     BOOST_TEST(*it23 == 3);
384     BOOST_TEST(cb2[0] == 1);
385     BOOST_TEST(cb2[1] == 2);
386     BOOST_TEST(cb2[2] == 4);
387     BOOST_TEST(cb2[3] == 5);
388     BOOST_TEST(cb2[4] == 3);
389 
390     // it24 -> 1, it25 -> 2
391     circular_buffer<int>::iterator it24 = it21 - 2;
392     circular_buffer<int>::iterator it25 = it21 - 1;
393 
394     cb2.rinsert(cb2.begin() + 4, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
395 
396     // memory placement: { 5, 6, 1, 2, 4 }
397     // circular buffer:  { 1, 2, 4, 5, 6 }
398     BOOST_TEST(*it21 == 5);
399     BOOST_TEST(*it22 == 6);
400     BOOST_TEST(*it23 == 1);
401     BOOST_TEST(*it24 == 2);
402     BOOST_TEST(*it25 == 4);
403     BOOST_TEST(cb2[0] == 1);
404     BOOST_TEST(cb2[1] == 2);
405     BOOST_TEST(cb2[2] == 4);
406     BOOST_TEST(cb2[3] == 5);
407     BOOST_TEST(cb2[4] == 6);
408 }
409 
validity_erase_test()410 void validity_erase_test() {
411 
412     // memory placement: { 4, 5, 1, 2, 3 }
413     // circular buffer:  { 1, 2, 3, 4, 5 }
414     circular_buffer<int> cb(5);
415     cb.push_back(-1);
416     cb.push_back(0);
417     cb.push_back(1);
418     cb.push_back(2);
419     cb.push_back(3);
420     cb.push_back(4);
421     cb.push_back(5);
422 
423     // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
424     circular_buffer<int>::iterator it1 = cb.begin();
425     circular_buffer<int>::iterator it2 = cb.begin() + 1;
426     circular_buffer<int>::iterator it3 = cb.begin() + 2;
427     circular_buffer<int>::iterator it4 = cb.begin() + 3;
428 
429     cb.erase(cb.begin() + 1);
430 
431     // memory placement: { 5, X, 1, 3, 4 }
432     // circular buffer:  { 1, 3, 4, 5 }
433     BOOST_TEST(*it1 == 1);
434     BOOST_TEST(*it2 == 3);
435     BOOST_TEST(*it3 == 4);
436     BOOST_TEST(*it4 == 5);
437     BOOST_TEST(cb[0] == 1);
438     BOOST_TEST(cb[1] == 3);
439     BOOST_TEST(cb[2] == 4);
440     BOOST_TEST(cb[3] == 5);
441 }
442 
validity_erase_range_test()443 void validity_erase_range_test() {
444 
445     // memory placement: { 4, 5, 6, 1, 2, 3 }
446     // circular buffer:  { 1, 2, 3, 4, 5, 6 }
447     circular_buffer<int> cb(6);
448     cb.push_back(-2);
449     cb.push_back(-1);
450     cb.push_back(0);
451     cb.push_back(1);
452     cb.push_back(2);
453     cb.push_back(3);
454     cb.push_back(4);
455     cb.push_back(5);
456     cb.push_back(6);
457 
458     // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
459     circular_buffer<int>::iterator it1 = cb.begin();
460     circular_buffer<int>::iterator it2 = cb.begin() + 1;
461     circular_buffer<int>::iterator it3 = cb.begin() + 2;
462     circular_buffer<int>::iterator it4 = cb.begin() + 3;
463 
464     cb.erase(cb.begin() + 2, cb.begin() + 4);
465 
466     // memory placement: { 6, X, X, 1, 2, 5 }
467     // circular buffer:  { 1, 2, 5, 6 }
468     BOOST_TEST(*it1 == 1);
469     BOOST_TEST(*it2 == 2);
470     BOOST_TEST(*it3 == 5);
471     BOOST_TEST(*it4 == 6);
472     BOOST_TEST(cb[0] == 1);
473     BOOST_TEST(cb[1] == 2);
474     BOOST_TEST(cb[2] == 5);
475     BOOST_TEST(cb[3] == 6);
476 }
477 
validity_rerase_test()478 void validity_rerase_test() {
479 
480     // memory placement: { 4, 5, 1, 2, 3 }
481     // circular buffer:  { 1, 2, 3, 4, 5 }
482     circular_buffer<int> cb(5);
483     cb.push_back(-1);
484     cb.push_back(0);
485     cb.push_back(1);
486     cb.push_back(2);
487     cb.push_back(3);
488     cb.push_back(4);
489     cb.push_back(5);
490 
491     // it1 -> 2, it2 -> 3, it3 -> 4, it4 -> 5
492     circular_buffer<int>::iterator it1 = cb.begin() + 1;
493     circular_buffer<int>::iterator it2 = cb.begin() + 2;
494     circular_buffer<int>::iterator it3 = cb.begin() + 3;
495     circular_buffer<int>::iterator it4 = cb.begin() + 4;
496 
497     cb.rerase(cb.begin() + 1);
498 
499     // memory placement: { 4, 5, X, 1, 3 }
500     // circular buffer:  { 1, 3, 4, 5 }
501     BOOST_TEST(*it1 == 1);
502     BOOST_TEST(*it2 == 3);
503     BOOST_TEST(*it3 == 4);
504     BOOST_TEST(*it4 == 5);
505     BOOST_TEST(cb[0] == 1);
506     BOOST_TEST(cb[1] == 3);
507     BOOST_TEST(cb[2] == 4);
508     BOOST_TEST(cb[3] == 5);
509 }
510 
validity_rerase_range_test()511 void validity_rerase_range_test() {
512 
513     // memory placement: { 4, 5, 6, 1, 2, 3 }
514     // circular buffer:  { 1, 2, 3, 4, 5, 6 }
515     circular_buffer<int> cb(6);
516     cb.push_back(-2);
517     cb.push_back(-1);
518     cb.push_back(0);
519     cb.push_back(1);
520     cb.push_back(2);
521     cb.push_back(3);
522     cb.push_back(4);
523     cb.push_back(5);
524     cb.push_back(6);
525 
526     // it1 -> 3, it2 -> 4, it3 -> 5, it4 -> 6
527     circular_buffer<int>::iterator it1 = cb.begin() + 2;
528     circular_buffer<int>::iterator it2 = cb.begin() + 3;
529     circular_buffer<int>::iterator it3 = cb.begin() + 4;
530     circular_buffer<int>::iterator it4 = cb.begin() + 5;
531 
532     cb.rerase(cb.begin() + 2, cb.begin() + 4);
533 
534     // memory placement: { 2, 5, 6, X, X, 1 }
535     // circular buffer:  { 1, 2, 5, 6 }
536     BOOST_TEST(*it1 == 1);
537     BOOST_TEST(*it2 == 2);
538     BOOST_TEST(*it3 == 5);
539     BOOST_TEST(*it4 == 6);
540     BOOST_TEST(cb[0] == 1);
541     BOOST_TEST(cb[1] == 2);
542     BOOST_TEST(cb[2] == 5);
543     BOOST_TEST(cb[3] == 6);
544 }
545 
validity_linearize_test()546 void validity_linearize_test() {
547 
548     // memory placement: { 3, 1, 2 }
549     // circular buffer:  { 1, 2, 3 }
550     circular_buffer<int> cb(3);
551     cb.push_back(0);
552     cb.push_back(1);
553     cb.push_back(2);
554     cb.push_back(3);
555 
556     // it1 -> 1, it2 -> 2, it3 -> 3
557     circular_buffer<int>::iterator it1 = cb.begin();
558     circular_buffer<int>::iterator it2 = cb.begin() + 1;
559     circular_buffer<int>::iterator it3 = cb.begin() + 2;
560 
561     cb.linearize();
562 
563     // memory placement: { 1, 2, 3 }
564     // circular buffer:  { 1, 2, 3 }
565     BOOST_TEST(*it1 == 2);
566     BOOST_TEST(*it2 == 3);
567     BOOST_TEST(*it3 == 1);
568     BOOST_TEST(cb[0] == 1);
569     BOOST_TEST(cb[1] == 2);
570     BOOST_TEST(cb[2] == 3);
571 }
572 
validity_swap_test()573 void validity_swap_test() {
574 
575     // memory placement: { 3, 1, 2 }
576     // circular buffer:  { 1, 2, 3 }
577     circular_buffer<int> cb1(3);
578     cb1.push_back(0);
579     cb1.push_back(1);
580     cb1.push_back(2);
581     cb1.push_back(3);
582 
583     // it11 -> 1, it12 -> 2, it13 -> 3
584     circular_buffer<int>::iterator it11 = cb1.begin();
585     circular_buffer<int>::iterator it12 = cb1.begin() + 1;
586     circular_buffer<int>::iterator it13 = cb1.begin() + 2;
587 
588     // memory placement: { 4, 5, 6 }
589     // circular buffer:  { 4, 5, 6 }
590     circular_buffer<int> cb2(5);
591     cb2.push_back(4);
592     cb2.push_back(5);
593     cb2.push_back(6);
594 
595     // it21 -> 4, it22 -> 5, it23 -> 6
596     circular_buffer<int>::iterator it21 = cb2.begin();
597     circular_buffer<int>::iterator it22 = cb2.begin() + 1;
598     circular_buffer<int>::iterator it23 = cb2.begin() + 2;
599 
600     cb1.swap(cb2);
601 
602     // Although iterators refer to the original elements,
603     // their internal state is inconsistent and no other operation
604     // (except dereferencing) can be invoked on them any more.
605     BOOST_TEST(*it11 == 1);
606     BOOST_TEST(*it12 == 2);
607     BOOST_TEST(*it13 == 3);
608     BOOST_TEST(*it21 == 4);
609     BOOST_TEST(*it22 == 5);
610     BOOST_TEST(*it23 == 6);
611     BOOST_TEST(cb1[0] == 4);
612     BOOST_TEST(cb1[1] == 5);
613     BOOST_TEST(cb1[2] == 6);
614     BOOST_TEST(cb2[0] == 1);
615     BOOST_TEST(cb2[1] == 2);
616     BOOST_TEST(cb2[2] == 3);
617 }
618 
validity_push_back_test()619 void validity_push_back_test() {
620 
621     // memory placement: { 3, 1, 2 }
622     // circular buffer:  { 1, 2, 3 }
623     circular_buffer<int> cb(3);
624     cb.push_back(0);
625     cb.push_back(1);
626     cb.push_back(2);
627     cb.push_back(3);
628 
629     // it1 -> 1, it2 -> 2, it3 -> 3
630     circular_buffer<int>::iterator it1 = cb.begin();
631     circular_buffer<int>::iterator it2 = cb.begin() + 1;
632     circular_buffer<int>::iterator it3 = cb.begin() + 2;
633 
634     cb.push_back(4);
635 
636     // memory placement: { 3, 4, 2 }
637     // circular buffer:  { 2, 3, 4 }
638     BOOST_TEST(*it1 == 4);
639     BOOST_TEST(*it2 == 2);
640     BOOST_TEST(*it3 == 3);
641     BOOST_TEST(cb[0] == 2);
642     BOOST_TEST(cb[1] == 3);
643     BOOST_TEST(cb[2] == 4);
644 }
645 
validity_push_front_test()646 void validity_push_front_test() {
647 
648     // memory placement: { 3, 1, 2 }
649     // circular buffer:  { 1, 2, 3 }
650     circular_buffer<int> cb(3);
651     cb.push_back(0);
652     cb.push_back(1);
653     cb.push_back(2);
654     cb.push_back(3);
655 
656     // it1 -> 1, it2 -> 2, it3 -> 3
657     circular_buffer<int>::iterator it1 = cb.begin();
658     circular_buffer<int>::iterator it2 = cb.begin() + 1;
659     circular_buffer<int>::iterator it3 = cb.begin() + 2;
660 
661     cb.push_front(4);
662 
663     // memory placement: { 4, 1, 2 }
664     // circular buffer:  { 4, 1, 2 }
665     BOOST_TEST(*it1 == 1);
666     BOOST_TEST(*it2 == 2);
667     BOOST_TEST(*it3 == 4);
668     BOOST_TEST(cb[0] == 4);
669     BOOST_TEST(cb[1] == 1);
670     BOOST_TEST(cb[2] == 2);
671 }
672 
validity_pop_back_test()673 void validity_pop_back_test() {
674 
675     // memory placement: { 3, 1, 2 }
676     // circular buffer:  { 1, 2, 3 }
677     circular_buffer<int> cb(3);
678     cb.push_back(0);
679     cb.push_back(1);
680     cb.push_back(2);
681     cb.push_back(3);
682 
683     // it1 -> 1, it2 -> 2
684     circular_buffer<int>::iterator it1 = cb.begin();
685     circular_buffer<int>::iterator it2 = cb.begin() + 1;
686 
687     cb.pop_back();
688 
689     // memory placement: { X, 1, 2 }
690     // circular buffer:  { 1, 2 }
691     BOOST_TEST(*it1 == 1);
692     BOOST_TEST(*it2 == 2);
693     BOOST_TEST(cb[0] == 1);
694     BOOST_TEST(cb[1] == 2);
695 }
696 
validity_pop_front_test()697 void validity_pop_front_test() {
698 
699     // memory placement: { 3, 1, 2 }
700     // circular buffer:  { 1, 2, 3 }
701     circular_buffer<int> cb(3);
702     cb.push_back(0);
703     cb.push_back(1);
704     cb.push_back(2);
705     cb.push_back(3);
706 
707     // it1 -> 2, it2 -> 3
708     circular_buffer<int>::iterator it1 = cb.begin() + 1;
709     circular_buffer<int>::iterator it2 = cb.begin() + 2;
710 
711     cb.pop_front();
712 
713     // memory placement: { 3, X, 2 }
714     // circular buffer:  { 2, 3 }
715     BOOST_TEST(*it1 == 2);
716     BOOST_TEST(*it2 == 3);
717     BOOST_TEST(cb[0] == 2);
718     BOOST_TEST(cb[1] == 3);
719 }
720 
721 // test main
main()722 int main()
723 {
724     validity_example_test();
725     validity_insert_test();
726     validity_insert_n_test();
727     validity_insert_range_test();
728     validity_rinsert_test();
729     validity_rinsert_n_test();
730     validity_rinsert_range_test();
731     validity_erase_test();
732     validity_erase_range_test();
733     validity_rerase_test();
734     validity_rerase_range_test();
735     validity_linearize_test();
736     validity_swap_test();
737     validity_push_back_test();
738     validity_push_front_test();
739     validity_pop_back_test();
740     validity_pop_front_test();
741     return boost::report_errors();
742 }
743