📄 soft_iterator_invalidation.cpp
字号:
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_CHECK(*it21 == 4); BOOST_CHECK(*it22 == 5); BOOST_CHECK(*it23 == 3); BOOST_CHECK(cb2[0] == 1); BOOST_CHECK(cb2[1] == 2); BOOST_CHECK(cb2[2] == 4); BOOST_CHECK(cb2[3] == 5); BOOST_CHECK(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_CHECK(*it21 == 5); BOOST_CHECK(*it22 == 6); BOOST_CHECK(*it23 == 1); BOOST_CHECK(*it24 == 2); BOOST_CHECK(*it25 == 4); BOOST_CHECK(cb2[0] == 1); BOOST_CHECK(cb2[1] == 2); BOOST_CHECK(cb2[2] == 4); BOOST_CHECK(cb2[3] == 5); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 3); BOOST_CHECK(*it3 == 4); BOOST_CHECK(*it4 == 5); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(cb[1] == 3); BOOST_CHECK(cb[2] == 4); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 2); BOOST_CHECK(*it3 == 5); BOOST_CHECK(*it4 == 6); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(cb[1] == 2); BOOST_CHECK(cb[2] == 5); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 3); BOOST_CHECK(*it3 == 4); BOOST_CHECK(*it4 == 5); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(cb[1] == 3); BOOST_CHECK(cb[2] == 4); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 2); BOOST_CHECK(*it3 == 5); BOOST_CHECK(*it4 == 6); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(cb[1] == 2); BOOST_CHECK(cb[2] == 5); BOOST_CHECK(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_CHECK(*it1 == 2); BOOST_CHECK(*it2 == 3); BOOST_CHECK(*it3 == 1); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(cb[1] == 2); BOOST_CHECK(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 interal state is inconsistent and no other operation // (except dereferencing) can be invoked on them any more. BOOST_CHECK(*it11 == 1); BOOST_CHECK(*it12 == 2); BOOST_CHECK(*it13 == 3); BOOST_CHECK(*it21 == 4); BOOST_CHECK(*it22 == 5); BOOST_CHECK(*it23 == 6); BOOST_CHECK(cb1[0] == 4); BOOST_CHECK(cb1[1] == 5); BOOST_CHECK(cb1[2] == 6); BOOST_CHECK(cb2[0] == 1); BOOST_CHECK(cb2[1] == 2); BOOST_CHECK(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_CHECK(*it1 == 4); BOOST_CHECK(*it2 == 2); BOOST_CHECK(*it3 == 3); BOOST_CHECK(cb[0] == 2); BOOST_CHECK(cb[1] == 3); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 2); BOOST_CHECK(*it3 == 4); BOOST_CHECK(cb[0] == 4); BOOST_CHECK(cb[1] == 1); BOOST_CHECK(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_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 2); BOOST_CHECK(cb[0] == 1); BOOST_CHECK(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_CHECK(*it1 == 2); BOOST_CHECK(*it2 == 3); BOOST_CHECK(cb[0] == 2); BOOST_CHECK(cb[1] == 3);}// test maintest_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) { test_suite* tests = BOOST_TEST_SUITE("Unit tests for the iterator of the circular_buffer."); tests->add(BOOST_TEST_CASE(&validity_example_test)); tests->add(BOOST_TEST_CASE(&validity_insert_test)); tests->add(BOOST_TEST_CASE(&validity_insert_n_test)); tests->add(BOOST_TEST_CASE(&validity_insert_range_test)); tests->add(BOOST_TEST_CASE(&validity_rinsert_test)); tests->add(BOOST_TEST_CASE(&validity_rinsert_n_test)); tests->add(BOOST_TEST_CASE(&validity_rinsert_range_test)); tests->add(BOOST_TEST_CASE(&validity_erase_test)); tests->add(BOOST_TEST_CASE(&validity_erase_range_test)); tests->add(BOOST_TEST_CASE(&validity_rerase_test)); tests->add(BOOST_TEST_CASE(&validity_rerase_range_test)); tests->add(BOOST_TEST_CASE(&validity_linearize_test)); tests->add(BOOST_TEST_CASE(&validity_swap_test)); tests->add(BOOST_TEST_CASE(&validity_push_back_test)); tests->add(BOOST_TEST_CASE(&validity_push_front_test)); tests->add(BOOST_TEST_CASE(&validity_pop_back_test)); tests->add(BOOST_TEST_CASE(&validity_pop_front_test)); return tests;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -