123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- // Demonstration of rules when an iterator is considered to be valid if the soft
- // iterator invalidation definition is applied.
- // Note: The soft iterator invalidation definition CAN NOT be applied
- // to the space optimized circular buffer.
- // Copyright (c) 2003-2008 Jan Gaspar
- // Use, modification, and distribution is subject to the Boost Software
- // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
- // http://www.boost.org/LICENSE_1_0.txt)
- #include "test.hpp"
- // test of the example (introduced in the documentation)
- void validity_example_test() {
- circular_buffer<int> cb(3);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- circular_buffer<int>::iterator it = cb.begin();
- BOOST_TEST(*it == 1);
- cb.push_back(4);
- BOOST_TEST(*it == 4);
- }
- void validity_insert_test() {
- int array[] = { 1, 2, 3 };
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(4, array, array + 3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.insert(cb.begin() + 1, 4);
- // memory placement: { 1, 4, 2, 3 }
- // circular buffer: { 1, 4, 2, 3 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 4);
- BOOST_TEST(*it3 == 2);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 4);
- BOOST_TEST(cb[2] == 2);
- BOOST_TEST(cb[3] == 3);
- // it4 -> 3
- circular_buffer<int>::iterator it4 = it1 + 3;
- cb.insert(cb.begin() + 1, 5);
- // memory placement: { 3, 5, 4, 2 }
- // circular buffer: { 5, 4, 2, 3 }
- BOOST_TEST(*it1 == 3);
- BOOST_TEST(*it2 == 5);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(*it4 == 2);
- BOOST_TEST(cb[0] == 5);
- BOOST_TEST(cb[1] == 4);
- BOOST_TEST(cb[2] == 2);
- BOOST_TEST(cb[3] == 3);
- }
- void validity_insert_n_test() {
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(5);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.insert(cb.begin() + 1, 2, 4);
- // memory placement: { 1, 4, 4, 2, 3 }
- // circular buffer: { 1, 4, 4, 2, 3 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 4);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 4);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 2);
- BOOST_TEST(cb[4] == 3);
- // it4 -> 2, it5 -> 3
- circular_buffer<int>::iterator it4 = it1 + 3;
- circular_buffer<int>::iterator it5 = it1 + 4;
- cb.insert(cb.begin() + 1, 2, 5);
- // memory placement: { 3, 5, 4, 4, 2 } - 5 inserted only once
- // circular buffer: { 5, 4, 4, 2, 3 }
- BOOST_TEST(*it1 == 3);
- BOOST_TEST(*it2 == 5);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(*it4 == 4);
- BOOST_TEST(*it5 == 2);
- BOOST_TEST(cb[0] == 5);
- BOOST_TEST(cb[1] == 4);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 2);
- BOOST_TEST(cb[4] == 3);
- }
- void validity_insert_range_test() {
- vector<int> v1;
- v1.push_back(4);
- v1.push_back(5);
- vector<int> v2;
- v2.push_back(6);
- v2.push_back(7);
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb1(5);
- cb1.push_back(1);
- cb1.push_back(2);
- cb1.push_back(3);
- // it11 -> 1, it12 -> 2, it13 -> 3
- circular_buffer<int>::iterator it11 = cb1.begin();
- circular_buffer<int>::iterator it12 = cb1.begin() + 1;
- circular_buffer<int>::iterator it13 = cb1.begin() + 2;
- cb1.insert(cb1.begin() + 1, v1.begin(), v1.end());
- // memory placement: { 1, 4, 5, 2, 3 }
- // circular buffer: { 1, 4, 5, 2, 3 }
- BOOST_TEST(*it11 == 1);
- BOOST_TEST(*it12 == 4);
- BOOST_TEST(*it13 == 5);
- BOOST_TEST(cb1[0] == 1);
- BOOST_TEST(cb1[1] == 4);
- BOOST_TEST(cb1[2] == 5);
- BOOST_TEST(cb1[3] == 2);
- BOOST_TEST(cb1[4] == 3);
- // it14 -> 2, it15 -> 3
- circular_buffer<int>::iterator it14 = it11 + 3;
- circular_buffer<int>::iterator it15 = it11 + 4;
- cb1.insert(cb1.begin() + 1, v2.begin(), v2.end());
- // memory placement: { 3, 7, 4, 5, 2 } - 7 inserted only
- // circular buffer: { 7, 4, 5, 2, 3 }
- BOOST_TEST(*it11 == 3);
- BOOST_TEST(*it12 == 7);
- BOOST_TEST(*it13 == 4);
- BOOST_TEST(*it14 == 5);
- BOOST_TEST(*it15 == 2);
- BOOST_TEST(cb1[0] == 7);
- BOOST_TEST(cb1[1] == 4);
- BOOST_TEST(cb1[2] == 5);
- BOOST_TEST(cb1[3] == 2);
- BOOST_TEST(cb1[4] == 3);
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb2(5);
- cb2.push_back(1);
- cb2.push_back(2);
- cb2.push_back(3);
- // it21 -> 1, it22 -> 2, it23 -> 3
- circular_buffer<int>::iterator it21 = cb2.begin();
- circular_buffer<int>::iterator it22 = cb2.begin() + 1;
- circular_buffer<int>::iterator it23 = cb2.begin() + 2;
- cb2.insert(cb2.begin() + 1, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
- // memory placement: { 1, 4, 5, 2, 3 }
- // circular buffer: { 1, 4, 5, 2, 3 }
- BOOST_TEST(*it21 == 1);
- BOOST_TEST(*it22 == 4);
- BOOST_TEST(*it23 == 5);
- BOOST_TEST(cb2[0] == 1);
- BOOST_TEST(cb2[1] == 4);
- BOOST_TEST(cb2[2] == 5);
- BOOST_TEST(cb2[3] == 2);
- BOOST_TEST(cb2[4] == 3);
- // it24 -> 2, it25 -> 3
- circular_buffer<int>::iterator it24 = it21 + 3;
- circular_buffer<int>::iterator it25 = it21 + 4;
- cb2.insert(cb2.begin() + 1, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
- // memory placement: { 2, 3, 7, 4, 5 } - using input iterator inserts all items even if they are later replaced
- // circular buffer: { 7, 4, 5, 2, 3 }
- BOOST_TEST(*it21 == 2);
- BOOST_TEST(*it22 == 3);
- BOOST_TEST(*it23 == 7);
- BOOST_TEST(*it24 == 4);
- BOOST_TEST(*it25 == 5);
- BOOST_TEST(cb2[0] == 7);
- BOOST_TEST(cb2[1] == 4);
- BOOST_TEST(cb2[2] == 5);
- BOOST_TEST(cb2[3] == 2);
- BOOST_TEST(cb2[4] == 3);
- }
- void validity_rinsert_test() {
- int array[] = { 1, 2, 3 };
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(4, array, array + 3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.rinsert(cb.begin() + 2, 4);
- // memory placement: { 2, 4, 3, 1 }
- // circular buffer: { 1, 2, 4, 3 }
- BOOST_TEST(*it1 == 2);
- BOOST_TEST(*it2 == 4);
- BOOST_TEST(*it3 == 3);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 3);
- // it4 -> 1
- circular_buffer<int>::iterator it4 = it1 - 1;
- cb.rinsert(cb.begin() + 2, 5);
- // memory placement: { 5, 4, 1, 2 }
- // circular buffer: { 1, 2, 5, 4 }
- BOOST_TEST(*it1 == 5);
- BOOST_TEST(*it2 == 4);
- BOOST_TEST(*it3 == 1);
- BOOST_TEST(*it4 == 2);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 5);
- BOOST_TEST(cb[3] == 4);
- }
- void validity_rinsert_n_test() {
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(5);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.rinsert(cb.begin() + 2, 2, 4);
- // memory placement: { 4, 4, 3, 1, 2 }
- // circular buffer: { 1, 2, 4, 4, 3 }
- BOOST_TEST(*it1 == 4);
- BOOST_TEST(*it2 == 4);
- BOOST_TEST(*it3 == 3);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 4);
- BOOST_TEST(cb[4] == 3);
- // it4 -> 1, it5 -> 2
- circular_buffer<int>::iterator it4 = it1 - 2;
- circular_buffer<int>::iterator it5 = it1 - 1;
- cb.rinsert(cb.begin() + 4, 2, 5);
- // memory placement: { 4, 5, 1, 2, 4 } - 5 inserted only once
- // circular buffer: { 1, 2, 4, 4, 5 }
- BOOST_TEST(*it1 == 4);
- BOOST_TEST(*it2 == 5);
- BOOST_TEST(*it3 == 1);
- BOOST_TEST(*it4 == 2);
- BOOST_TEST(*it5 == 4);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 4);
- BOOST_TEST(cb[4] == 5);
- }
- void validity_rinsert_range_test() {
- vector<int> v1;
- v1.push_back(4);
- v1.push_back(5);
- vector<int> v2;
- v2.push_back(6);
- v2.push_back(7);
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb1(5);
- cb1.push_back(1);
- cb1.push_back(2);
- cb1.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it11 = cb1.begin();
- circular_buffer<int>::iterator it12 = cb1.begin() + 1;
- circular_buffer<int>::iterator it13 = cb1.begin() + 2;
- cb1.rinsert(cb1.begin() + 2, v1.begin(), v1.end());
- // memory placement: { 4, 5, 3, 1, 2 }
- // circular buffer: { 1, 2, 4, 5, 3 }
- BOOST_TEST(*it11 == 4);
- BOOST_TEST(*it12 == 5);
- BOOST_TEST(*it13 == 3);
- BOOST_TEST(cb1[0] == 1);
- BOOST_TEST(cb1[1] == 2);
- BOOST_TEST(cb1[2] == 4);
- BOOST_TEST(cb1[3] == 5);
- BOOST_TEST(cb1[4] == 3);
- // it14 -> 1, it15 -> 2
- circular_buffer<int>::iterator it14 = it11 - 2;
- circular_buffer<int>::iterator it15 = it11 - 1;
- cb1.rinsert(cb1.begin() + 4, v2.begin(), v2.end());
- // memory placement: { 5, 6, 1, 2, 4 } - 6 inserted only
- // circular buffer: { 1, 2, 4, 5, 6 }
- BOOST_TEST(*it11 == 5);
- BOOST_TEST(*it12 == 6);
- BOOST_TEST(*it13 == 1);
- BOOST_TEST(*it14 == 2);
- BOOST_TEST(*it15 == 4);
- BOOST_TEST(cb1[0] == 1);
- BOOST_TEST(cb1[1] == 2);
- BOOST_TEST(cb1[2] == 4);
- BOOST_TEST(cb1[3] == 5);
- BOOST_TEST(cb1[4] == 6);
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb2(5);
- cb2.push_back(1);
- cb2.push_back(2);
- cb2.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it21 = cb2.begin();
- circular_buffer<int>::iterator it22 = cb2.begin() + 1;
- circular_buffer<int>::iterator it23 = cb2.begin() + 2;
- cb2.rinsert(cb2.begin() + 2, MyInputIterator(v1.begin()), MyInputIterator(v1.end()));
- // memory placement: { 4, 5, 3, 1, 2 }
- // circular buffer: { 1, 2, 4, 5, 3 }
- BOOST_TEST(*it21 == 4);
- BOOST_TEST(*it22 == 5);
- BOOST_TEST(*it23 == 3);
- BOOST_TEST(cb2[0] == 1);
- BOOST_TEST(cb2[1] == 2);
- BOOST_TEST(cb2[2] == 4);
- BOOST_TEST(cb2[3] == 5);
- BOOST_TEST(cb2[4] == 3);
- // it24 -> 1, it25 -> 2
- circular_buffer<int>::iterator it24 = it21 - 2;
- circular_buffer<int>::iterator it25 = it21 - 1;
- cb2.rinsert(cb2.begin() + 4, MyInputIterator(v2.begin()), MyInputIterator(v2.end()));
- // memory placement: { 5, 6, 1, 2, 4 }
- // circular buffer: { 1, 2, 4, 5, 6 }
- BOOST_TEST(*it21 == 5);
- BOOST_TEST(*it22 == 6);
- BOOST_TEST(*it23 == 1);
- BOOST_TEST(*it24 == 2);
- BOOST_TEST(*it25 == 4);
- BOOST_TEST(cb2[0] == 1);
- BOOST_TEST(cb2[1] == 2);
- BOOST_TEST(cb2[2] == 4);
- BOOST_TEST(cb2[3] == 5);
- BOOST_TEST(cb2[4] == 6);
- }
- void validity_erase_test() {
- // memory placement: { 4, 5, 1, 2, 3 }
- // circular buffer: { 1, 2, 3, 4, 5 }
- circular_buffer<int> cb(5);
- cb.push_back(-1);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- cb.push_back(4);
- cb.push_back(5);
- // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- circular_buffer<int>::iterator it4 = cb.begin() + 3;
- cb.erase(cb.begin() + 1);
- // memory placement: { 5, X, 1, 3, 4 }
- // circular buffer: { 1, 3, 4, 5 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 3);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(*it4 == 5);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 3);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 5);
- }
- void validity_erase_range_test() {
- // memory placement: { 4, 5, 6, 1, 2, 3 }
- // circular buffer: { 1, 2, 3, 4, 5, 6 }
- circular_buffer<int> cb(6);
- cb.push_back(-2);
- cb.push_back(-1);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- cb.push_back(4);
- cb.push_back(5);
- cb.push_back(6);
- // it1 -> 1, it2 -> 2, it3 -> 3, it4 -> 4
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- circular_buffer<int>::iterator it4 = cb.begin() + 3;
- cb.erase(cb.begin() + 2, cb.begin() + 4);
- // memory placement: { 6, X, X, 1, 2, 5 }
- // circular buffer: { 1, 2, 5, 6 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 2);
- BOOST_TEST(*it3 == 5);
- BOOST_TEST(*it4 == 6);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 5);
- BOOST_TEST(cb[3] == 6);
- }
- void validity_rerase_test() {
- // memory placement: { 4, 5, 1, 2, 3 }
- // circular buffer: { 1, 2, 3, 4, 5 }
- circular_buffer<int> cb(5);
- cb.push_back(-1);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- cb.push_back(4);
- cb.push_back(5);
- // it1 -> 2, it2 -> 3, it3 -> 4, it4 -> 5
- circular_buffer<int>::iterator it1 = cb.begin() + 1;
- circular_buffer<int>::iterator it2 = cb.begin() + 2;
- circular_buffer<int>::iterator it3 = cb.begin() + 3;
- circular_buffer<int>::iterator it4 = cb.begin() + 4;
- cb.rerase(cb.begin() + 1);
- // memory placement: { 4, 5, X, 1, 3 }
- // circular buffer: { 1, 3, 4, 5 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 3);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(*it4 == 5);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 3);
- BOOST_TEST(cb[2] == 4);
- BOOST_TEST(cb[3] == 5);
- }
- void validity_rerase_range_test() {
- // memory placement: { 4, 5, 6, 1, 2, 3 }
- // circular buffer: { 1, 2, 3, 4, 5, 6 }
- circular_buffer<int> cb(6);
- cb.push_back(-2);
- cb.push_back(-1);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- cb.push_back(4);
- cb.push_back(5);
- cb.push_back(6);
- // it1 -> 3, it2 -> 4, it3 -> 5, it4 -> 6
- circular_buffer<int>::iterator it1 = cb.begin() + 2;
- circular_buffer<int>::iterator it2 = cb.begin() + 3;
- circular_buffer<int>::iterator it3 = cb.begin() + 4;
- circular_buffer<int>::iterator it4 = cb.begin() + 5;
- cb.rerase(cb.begin() + 2, cb.begin() + 4);
- // memory placement: { 2, 5, 6, X, X, 1 }
- // circular buffer: { 1, 2, 5, 6 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 2);
- BOOST_TEST(*it3 == 5);
- BOOST_TEST(*it4 == 6);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 5);
- BOOST_TEST(cb[3] == 6);
- }
- void validity_linearize_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(3);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.linearize();
- // memory placement: { 1, 2, 3 }
- // circular buffer: { 1, 2, 3 }
- BOOST_TEST(*it1 == 2);
- BOOST_TEST(*it2 == 3);
- BOOST_TEST(*it3 == 1);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- BOOST_TEST(cb[2] == 3);
- }
- void validity_swap_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb1(3);
- cb1.push_back(0);
- cb1.push_back(1);
- cb1.push_back(2);
- cb1.push_back(3);
- // it11 -> 1, it12 -> 2, it13 -> 3
- circular_buffer<int>::iterator it11 = cb1.begin();
- circular_buffer<int>::iterator it12 = cb1.begin() + 1;
- circular_buffer<int>::iterator it13 = cb1.begin() + 2;
- // memory placement: { 4, 5, 6 }
- // circular buffer: { 4, 5, 6 }
- circular_buffer<int> cb2(5);
- cb2.push_back(4);
- cb2.push_back(5);
- cb2.push_back(6);
- // it21 -> 4, it22 -> 5, it23 -> 6
- circular_buffer<int>::iterator it21 = cb2.begin();
- circular_buffer<int>::iterator it22 = cb2.begin() + 1;
- circular_buffer<int>::iterator it23 = cb2.begin() + 2;
- cb1.swap(cb2);
- // Although iterators refer to the original elements,
- // their internal state is inconsistent and no other operation
- // (except dereferencing) can be invoked on them any more.
- BOOST_TEST(*it11 == 1);
- BOOST_TEST(*it12 == 2);
- BOOST_TEST(*it13 == 3);
- BOOST_TEST(*it21 == 4);
- BOOST_TEST(*it22 == 5);
- BOOST_TEST(*it23 == 6);
- BOOST_TEST(cb1[0] == 4);
- BOOST_TEST(cb1[1] == 5);
- BOOST_TEST(cb1[2] == 6);
- BOOST_TEST(cb2[0] == 1);
- BOOST_TEST(cb2[1] == 2);
- BOOST_TEST(cb2[2] == 3);
- }
- void validity_push_back_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(3);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.push_back(4);
- // memory placement: { 3, 4, 2 }
- // circular buffer: { 2, 3, 4 }
- BOOST_TEST(*it1 == 4);
- BOOST_TEST(*it2 == 2);
- BOOST_TEST(*it3 == 3);
- BOOST_TEST(cb[0] == 2);
- BOOST_TEST(cb[1] == 3);
- BOOST_TEST(cb[2] == 4);
- }
- void validity_push_front_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(3);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2, it3 -> 3
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- circular_buffer<int>::iterator it3 = cb.begin() + 2;
- cb.push_front(4);
- // memory placement: { 4, 1, 2 }
- // circular buffer: { 4, 1, 2 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 2);
- BOOST_TEST(*it3 == 4);
- BOOST_TEST(cb[0] == 4);
- BOOST_TEST(cb[1] == 1);
- BOOST_TEST(cb[2] == 2);
- }
- void validity_pop_back_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(3);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 1, it2 -> 2
- circular_buffer<int>::iterator it1 = cb.begin();
- circular_buffer<int>::iterator it2 = cb.begin() + 1;
- cb.pop_back();
- // memory placement: { X, 1, 2 }
- // circular buffer: { 1, 2 }
- BOOST_TEST(*it1 == 1);
- BOOST_TEST(*it2 == 2);
- BOOST_TEST(cb[0] == 1);
- BOOST_TEST(cb[1] == 2);
- }
- void validity_pop_front_test() {
- // memory placement: { 3, 1, 2 }
- // circular buffer: { 1, 2, 3 }
- circular_buffer<int> cb(3);
- cb.push_back(0);
- cb.push_back(1);
- cb.push_back(2);
- cb.push_back(3);
- // it1 -> 2, it2 -> 3
- circular_buffer<int>::iterator it1 = cb.begin() + 1;
- circular_buffer<int>::iterator it2 = cb.begin() + 2;
- cb.pop_front();
- // memory placement: { 3, X, 2 }
- // circular buffer: { 2, 3 }
- BOOST_TEST(*it1 == 2);
- BOOST_TEST(*it2 == 3);
- BOOST_TEST(cb[0] == 2);
- BOOST_TEST(cb[1] == 3);
- }
- // test main
- int main()
- {
- validity_example_test();
- validity_insert_test();
- validity_insert_n_test();
- validity_insert_range_test();
- validity_rinsert_test();
- validity_rinsert_n_test();
- validity_rinsert_range_test();
- validity_erase_test();
- validity_erase_range_test();
- validity_rerase_test();
- validity_rerase_range_test();
- validity_linearize_test();
- validity_swap_test();
- validity_push_back_test();
- validity_push_front_test();
- validity_pop_back_test();
- validity_pop_front_test();
- return boost::report_errors();
- }
|