diff --git a/doc/TODO b/doc/TODO index 0a4f06a..4ba447b 100644 --- a/doc/TODO +++ b/doc/TODO @@ -172,7 +172,8 @@ t47 DONE t48 DONE verify the correctness of the SGIAssignableConcept -t49 test non-invalidation of the iterators after call to methods such as insert +t49 DONE + test non-invalidation of the iterators after call to methods such as insert or rinsert t50 DONE diff --git a/test/base_test.cpp b/test/base_test.cpp index 3a79546..b004502 100644 --- a/test/base_test.cpp +++ b/test/base_test.cpp @@ -33,12 +33,18 @@ void iterator_constructor_and_assign_test() { void iterator_reference_test() { - circular_buffer cb(3, Z()); - circular_buffer::iterator it = cb.begin(); - circular_buffer::const_iterator cit = cb.begin() + 1; + circular_buffer cb(3, Dummy()); + circular_buffer::iterator it = cb.begin(); + circular_buffer::const_iterator cit = cb.begin() + 1; - BOOST_CHECK((*it).test_reference1() == it->test_reference2()); - BOOST_CHECK((*cit).test_reference2() == cit->test_reference1()); + BOOST_CHECK((*it).m_n == Dummy::eVar); + BOOST_CHECK((*it).fnc() == Dummy::eFnc); + BOOST_CHECK((*cit).const_fnc() == Dummy::eConst); + BOOST_CHECK((*it).virtual_fnc() == Dummy::eVirtual); + BOOST_CHECK(it->m_n == Dummy::eVar); + BOOST_CHECK(it->fnc() == Dummy::eFnc); + BOOST_CHECK(cit->const_fnc() == Dummy::eConst); + BOOST_CHECK(it->virtual_fnc() == Dummy::eVirtual); } void iterator_difference_test() { @@ -183,6 +189,7 @@ void iterator_comparison_test() { BOOST_CHECK(!(end - 1 < it)); } +// TODO add insert, linearize etc. void iterator_invalidation_test() { #if BOOST_CB_ENABLE_DEBUG diff --git a/test/bounded_buffer_comparison.cpp b/test/bounded_buffer_comparison.cpp index 925f4fe..e20f0f8 100644 --- a/test/bounded_buffer_comparison.cpp +++ b/test/bounded_buffer_comparison.cpp @@ -1,7 +1,14 @@ +// Comparison of bounded buffers based on different containers. + +// Copyright (c) 2003-2005 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) #define BOOST_CB_DISABLE_DEBUG 1 -#include "boost/circular_buffer.hpp" +#include #include #include #include diff --git a/test/common.ipp b/test/common.ipp index db26471..1abb956 100644 --- a/test/common.ipp +++ b/test/common.ipp @@ -659,9 +659,9 @@ void swap_test() { void push_back_test() { - CB_CONTAINER cb1(5); + CB_CONTAINER cb1(5); cb1.push_back(); - cb1.push_back(X(2)); + cb1.push_back(DefaultConstructible(2)); BOOST_CHECK(cb1[0].m_n == 1); BOOST_CHECK(cb1[1].m_n == 2); @@ -821,9 +821,9 @@ void insert_range_test() { void push_front_test() { - CB_CONTAINER cb1(5); + CB_CONTAINER cb1(5); cb1.push_front(); - cb1.push_front(X(2)); + cb1.push_front(DefaultConstructible(2)); BOOST_CHECK(cb1[0].m_n == 2); BOOST_CHECK(cb1[1].m_n == 1); @@ -1336,16 +1336,16 @@ void example_test() { void element_destruction_test() { - CB_CONTAINER cb(5); - cb.push_back(Y()); - cb.push_back(Y()); - cb.push_back(Y()); - int prevCount = Y::count(); + CB_CONTAINER cb(5); + cb.push_back(InstanceCounter()); + cb.push_back(InstanceCounter()); + cb.push_back(InstanceCounter()); + int prevCount = InstanceCounter::count(); cb.clear(); BOOST_CHECK(cb.empty()); BOOST_CHECK(prevCount == 3); - BOOST_CHECK(Y::count() == 0); + BOOST_CHECK(InstanceCounter::count() == 0); } void const_methods_test() { @@ -1496,7 +1496,7 @@ void input_range_test() { } int Integer::ms_exception_trigger = 0; -int Y::ms_count = 0; +int InstanceCounter::ms_count = 0; // add common tests into a test suite void add_common_tests(test_suite* tests) { diff --git a/test/iterator_test.cpp b/test/iterator_test.cpp index 43aaf85..c682887 100644 --- a/test/iterator_test.cpp +++ b/test/iterator_test.cpp @@ -12,8 +12,6 @@ #include "test.hpp" -int array[] = { 1, 2, 3, 4, 5 }; - // test of the example (introduced in the documentation) void validity_example_test() { @@ -34,56 +32,692 @@ void validity_example_test() { void validity_insert_test() { + int array[] = { 1, 2, 3 }; + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } circular_buffer cb(4, array, array + 3); + + // it1 -> 1, it2 -> 2, it3 -> 3 circular_buffer::iterator it1 = cb.begin(); circular_buffer::iterator it2 = cb.begin() + 1; circular_buffer::iterator it3 = cb.begin() + 2; - // cb = {1, 2, 3} - BOOST_CHECK(*it1 == 1); - BOOST_CHECK(*it2 == 2); - BOOST_CHECK(*it3 == 3); - cb.insert(cb.begin() + 1, 4); - // cb = {1, 4, 2, 3} + // memory placement: { 1, 4, 2, 3 } + // circular buffer: { 1, 4, 2, 3 } BOOST_CHECK(*it1 == 1); BOOST_CHECK(*it2 == 4); BOOST_CHECK(*it3 == 2); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 4); + BOOST_CHECK(cb[2] == 2); + BOOST_CHECK(cb[3] == 3); + // it4 -> 3 + circular_buffer::iterator it4 = it1 + 3; + cb.insert(cb.begin() + 1, 5); - // cb = {3, 5, 4, 2} + // memory placement: { 3, 5, 4, 2 } + // circular buffer: { 5, 4, 2, 3 } BOOST_CHECK(*it1 == 3); BOOST_CHECK(*it2 == 5); BOOST_CHECK(*it3 == 4); + BOOST_CHECK(*it4 == 2); + BOOST_CHECK(cb[0] == 5); + BOOST_CHECK(cb[1] == 4); + BOOST_CHECK(cb[2] == 2); + BOOST_CHECK(cb[3] == 3); } void validity_insert_n_test() { - circular_buffer cb2(4); + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb(5); + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); + + // it1 -> 1, it2 -> 2, it3 -> 3 + circular_buffer::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::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_CHECK(*it1 == 1); + BOOST_CHECK(*it2 == 4); + BOOST_CHECK(*it3 == 4); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 4); + BOOST_CHECK(cb[2] == 4); + BOOST_CHECK(cb[3] == 2); + BOOST_CHECK(cb[4] == 3); + + // it4 -> 2, it5 -> 3 + circular_buffer::iterator it4 = it1 + 3; + circular_buffer::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_CHECK(*it1 == 3); + BOOST_CHECK(*it2 == 5); + BOOST_CHECK(*it3 == 4); + BOOST_CHECK(*it4 == 4); + BOOST_CHECK(*it5 == 2); + BOOST_CHECK(cb[0] == 5); + BOOST_CHECK(cb[1] == 4); + BOOST_CHECK(cb[2] == 4); + BOOST_CHECK(cb[3] == 2); + BOOST_CHECK(cb[4] == 3); +} + +void validity_insert_range_test() { + + vector v1; + v1.push_back(4); + v1.push_back(5); + + vector v2; + v2.push_back(6); + v2.push_back(7); + + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb1(5); + cb1.push_back(1); + cb1.push_back(2); + cb1.push_back(3); + + // it11 -> 1, it12 -> 2, it13 -> 3 + circular_buffer::iterator it11 = cb1.begin(); + circular_buffer::iterator it12 = cb1.begin() + 1; + circular_buffer::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_CHECK(*it11 == 1); + BOOST_CHECK(*it12 == 4); + BOOST_CHECK(*it13 == 5); + BOOST_CHECK(cb1[0] == 1); + BOOST_CHECK(cb1[1] == 4); + BOOST_CHECK(cb1[2] == 5); + BOOST_CHECK(cb1[3] == 2); + BOOST_CHECK(cb1[4] == 3); + + // it14 -> 2, it15 -> 3 + circular_buffer::iterator it14 = it11 + 3; + circular_buffer::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_CHECK(*it11 == 3); + BOOST_CHECK(*it12 == 7); + BOOST_CHECK(*it13 == 4); + BOOST_CHECK(*it14 == 5); + BOOST_CHECK(*it15 == 2); + BOOST_CHECK(cb1[0] == 7); + BOOST_CHECK(cb1[1] == 4); + BOOST_CHECK(cb1[2] == 5); + BOOST_CHECK(cb1[3] == 2); + BOOST_CHECK(cb1[4] == 3); + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb2(5); cb2.push_back(1); cb2.push_back(2); cb2.push_back(3); - cb2.push_back(4); - circular_buffer::iterator it2 = cb2.begin(); - BOOST_CHECK(*it2 == 1); - cb2.insert(cb2.begin() + 1, 2, 5); - BOOST_CHECK(*it2 == 4); + + // it21 -> 1, it22 -> 2, it23 -> 3 + circular_buffer::iterator it21 = cb2.begin(); + circular_buffer::iterator it22 = cb2.begin() + 1; + circular_buffer::iterator it23 = cb2.begin() + 2; + + cb2.insert(cb2.begin() + 1, InputIteratorSimulator(v1.begin()), InputIteratorSimulator(v1.end())); + + // memory placement: { 1, 4, 5, 2, 3 } + // circular buffer: { 1, 4, 5, 2, 3 } + BOOST_CHECK(*it21 == 1); + BOOST_CHECK(*it22 == 4); + BOOST_CHECK(*it23 == 5); + BOOST_CHECK(cb2[0] == 1); + BOOST_CHECK(cb2[1] == 4); + BOOST_CHECK(cb2[2] == 5); + BOOST_CHECK(cb2[3] == 2); + BOOST_CHECK(cb2[4] == 3); + + // it24 -> 2, it25 -> 3 + circular_buffer::iterator it24 = it21 + 3; + circular_buffer::iterator it25 = it21 + 4; + + cb2.insert(cb2.begin() + 1, InputIteratorSimulator(v2.begin()), InputIteratorSimulator(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_CHECK(*it21 == 2); + BOOST_CHECK(*it22 == 3); + BOOST_CHECK(*it23 == 7); + BOOST_CHECK(*it24 == 4); + BOOST_CHECK(*it25 == 5); + BOOST_CHECK(cb2[0] == 7); + BOOST_CHECK(cb2[1] == 4); + BOOST_CHECK(cb2[2] == 5); + BOOST_CHECK(cb2[3] == 2); + BOOST_CHECK(cb2[4] == 3); } -// erase -// rerase -// insert -// rinsert -// push_back -// push_front -// linearize -// swap -// set_capacity -// rset_capacity -// resize -// rresize +void validity_rinsert_test() { + + int array[] = { 1, 2, 3 }; + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb(4, array, array + 3); + + // it1 -> 1, it2 -> 2, it3 -> 3 + circular_buffer::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::iterator it3 = cb.begin() + 2; + + cb.rinsert(cb.begin() + 2, 4); + + // memory placement: { 2, 4, 3, 1 } + // circular buffer: { 1, 2, 4, 3 } + BOOST_CHECK(*it1 == 2); + BOOST_CHECK(*it2 == 4); + BOOST_CHECK(*it3 == 3); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 2); + BOOST_CHECK(cb[2] == 4); + BOOST_CHECK(cb[3] == 3); + + // it4 -> 1 + circular_buffer::iterator it4 = it1 - 1; + + cb.rinsert(cb.begin() + 2, 5); + + // memory placement: { 5, 4, 1, 2 } + // circular buffer: { 1, 2, 5, 4 } + BOOST_CHECK(*it1 == 5); + BOOST_CHECK(*it2 == 4); + BOOST_CHECK(*it3 == 1); + BOOST_CHECK(*it4 == 2); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 2); + BOOST_CHECK(cb[2] == 5); + BOOST_CHECK(cb[3] == 4); +} + +void validity_rinsert_n_test() { + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb(5); + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); + + // it1 -> 1, it2 -> 2, it3 -> 3 + circular_buffer::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::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_CHECK(*it1 == 4); + BOOST_CHECK(*it2 == 4); + BOOST_CHECK(*it3 == 3); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 2); + BOOST_CHECK(cb[2] == 4); + BOOST_CHECK(cb[3] == 4); + BOOST_CHECK(cb[4] == 3); + + // it4 -> 1, it5 -> 2 + circular_buffer::iterator it4 = it1 - 2; + circular_buffer::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_CHECK(*it1 == 4); + BOOST_CHECK(*it2 == 5); + BOOST_CHECK(*it3 == 1); + BOOST_CHECK(*it4 == 2); + BOOST_CHECK(*it5 == 4); + BOOST_CHECK(cb[0] == 1); + BOOST_CHECK(cb[1] == 2); + BOOST_CHECK(cb[2] == 4); + BOOST_CHECK(cb[3] == 4); + BOOST_CHECK(cb[4] == 5); +} + +void validity_rinsert_range_test() { + + vector v1; + v1.push_back(4); + v1.push_back(5); + + vector v2; + v2.push_back(6); + v2.push_back(7); + + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb1(5); + cb1.push_back(1); + cb1.push_back(2); + cb1.push_back(3); + + // it1 -> 1, it2 -> 2, it3 -> 3 + circular_buffer::iterator it11 = cb1.begin(); + circular_buffer::iterator it12 = cb1.begin() + 1; + circular_buffer::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_CHECK(*it11 == 4); + BOOST_CHECK(*it12 == 5); + BOOST_CHECK(*it13 == 3); + BOOST_CHECK(cb1[0] == 1); + BOOST_CHECK(cb1[1] == 2); + BOOST_CHECK(cb1[2] == 4); + BOOST_CHECK(cb1[3] == 5); + BOOST_CHECK(cb1[4] == 3); + + // it14 -> 1, it15 -> 2 + circular_buffer::iterator it14 = it11 - 2; + circular_buffer::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_CHECK(*it11 == 5); + BOOST_CHECK(*it12 == 6); + BOOST_CHECK(*it13 == 1); + BOOST_CHECK(*it14 == 2); + BOOST_CHECK(*it15 == 4); + BOOST_CHECK(cb1[0] == 1); + BOOST_CHECK(cb1[1] == 2); + BOOST_CHECK(cb1[2] == 4); + BOOST_CHECK(cb1[3] == 5); + BOOST_CHECK(cb1[4] == 6); + + // memory placement: { 1, 2, 3 } + // circular buffer: { 1, 2, 3 } + circular_buffer cb2(5); + cb2.push_back(1); + cb2.push_back(2); + cb2.push_back(3); + + // it1 -> 1, it2 -> 2, it3 -> 3 + circular_buffer::iterator it21 = cb2.begin(); + circular_buffer::iterator it22 = cb2.begin() + 1; + circular_buffer::iterator it23 = cb2.begin() + 2; + + cb2.rinsert(cb2.begin() + 2, InputIteratorSimulator(v1.begin()), InputIteratorSimulator(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::iterator it24 = it21 - 2; + circular_buffer::iterator it25 = it21 - 1; + + cb2.rinsert(cb2.begin() + 4, InputIteratorSimulator(v2.begin()), InputIteratorSimulator(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 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::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::iterator it3 = cb.begin() + 2; + circular_buffer::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 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::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::iterator it3 = cb.begin() + 2; + circular_buffer::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 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::iterator it1 = cb.begin() + 1; + circular_buffer::iterator it2 = cb.begin() + 2; + circular_buffer::iterator it3 = cb.begin() + 3; + circular_buffer::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 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::iterator it1 = cb.begin() + 2; + circular_buffer::iterator it2 = cb.begin() + 3; + circular_buffer::iterator it3 = cb.begin() + 4; + circular_buffer::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 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::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::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 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::iterator it11 = cb1.begin(); + circular_buffer::iterator it12 = cb1.begin() + 1; + circular_buffer::iterator it13 = cb1.begin() + 2; + + // memory placement: { 4, 5, 6 } + // circular buffer: { 4, 5, 6 } + circular_buffer cb2(5); + cb2.push_back(4); + cb2.push_back(5); + cb2.push_back(6); + + // it21 -> 4, it22 -> 5, it23 -> 6 + circular_buffer::iterator it21 = cb2.begin(); + circular_buffer::iterator it22 = cb2.begin() + 1; + circular_buffer::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 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::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::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 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::iterator it1 = cb.begin(); + circular_buffer::iterator it2 = cb.begin() + 1; + circular_buffer::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 cb(3); + cb.push_back(0); + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); + + // it1 -> 1, it2 -> 2 + circular_buffer::iterator it1 = cb.begin(); + circular_buffer::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 cb(3); + cb.push_back(0); + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); + + // it1 -> 2, it2 -> 3 + circular_buffer::iterator it1 = cb.begin() + 1; + circular_buffer::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 main test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) { @@ -93,6 +727,20 @@ test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) { 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; } diff --git a/test/test.hpp b/test/test.hpp index 2ad04d3..7c90fc1 100644 --- a/test/test.hpp +++ b/test/test.hpp @@ -56,20 +56,21 @@ public: static void set_exception_trigger(int n) { ms_exception_trigger = n; } }; -// TODO doc -struct X +// default constructible class +class DefaultConstructible { - X() : m_n(1) {} - X(int n) : m_n(n) {} +public: + DefaultConstructible() : m_n(1) {} + DefaultConstructible(int n) : m_n(n) {} int m_n; }; -// TODO doc -class Y { +// class counting instances of self +class InstanceCounter { public: - Y() { increment(); } - Y(const Y& y) { y.increment(); } - ~Y() { decrement(); } + InstanceCounter() { increment(); } + InstanceCounter(const InstanceCounter& y) { y.increment(); } + ~InstanceCounter() { decrement(); } static int count() { return ms_count; } private: void increment() const { ++ms_count; } @@ -77,14 +78,20 @@ private: static int ms_count; }; -// TODO doc -class Z { +// dummy class suitable for iterator referencing test +class Dummy { public: - Z() : m_num(255) {} - virtual int test_reference1() const { return m_num; } - int test_reference2() const { return 255; } -private: - int m_num; + enum DummyEnum { + eVar, + eFnc, + eConst, + eVirtual + }; + Dummy() : m_n(eVar) {} + DummyEnum fnc() { return eFnc; } + DummyEnum const_fnc() const { return eConst; } + virtual DummyEnum virtual_fnc() { return eVirtual; } + DummyEnum m_n; }; // simulator of an input iterator