From 32bfbced60d5112481bf2325d21fa45513fbb6d6 Mon Sep 17 00:00:00 2001 From: Jan Gaspar Date: Thu, 22 Dec 2005 00:03:30 +0000 Subject: [PATCH] item_wrapper bug fix [SVN r2787] --- include/boost/circular_buffer/base.hpp | 12 ++--- include/boost/circular_buffer/details.hpp | 8 +-- test/iterator_test.cpp | 63 ++++++++++++++++++++--- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/include/boost/circular_buffer/base.hpp b/include/boost/circular_buffer/base.hpp index ef7dbbb..a3348cd 100644 --- a/include/boost/circular_buffer/base.hpp +++ b/include/boost/circular_buffer/base.hpp @@ -1480,9 +1480,9 @@ private: pointer p = m_last; BOOST_TRY { for (; ii < construct; ++ii, increment(p)) - m_alloc.construct(p, *wrapper.get()); + m_alloc.construct(p, *wrapper()); for (;ii < n; ++ii, increment(p)) - replace(p, *wrapper.get()); + replace(p, *wrapper()); } BOOST_CATCH(...) { size_type constructed = std::min(ii, construct); m_last = add(m_last, constructed); @@ -1502,7 +1502,7 @@ private: decrement(dest); } for (; ii < n; ++ii, increment(p)) - construct_or_replace(is_uninitialized(p), p, *wrapper.get()); + construct_or_replace(is_uninitialized(p), p, *wrapper()); } BOOST_CATCH(...) { for (p = add(m_last, n - 1); p != dest; decrement(p)) destroy_if_constructed(p); @@ -1567,9 +1567,9 @@ private: size_type ii = n; BOOST_TRY { for (;ii > construct; --ii, increment(p)) - replace(p, *wrapper.get()); + replace(p, *wrapper()); for (; ii > 0; --ii, increment(p)) - m_alloc.construct(p, *wrapper.get()); + m_alloc.construct(p, *wrapper()); } BOOST_CATCH(...) { size_type constructed = ii < construct ? construct - ii : 0; m_last = add(m_last, constructed); @@ -1588,7 +1588,7 @@ private: increment(dest); } for (size_type ii = 0; ii < n; ++ii, increment(dest)) - construct_or_replace(is_uninitialized(dest), dest, *wrapper.get()); + construct_or_replace(is_uninitialized(dest), dest, *wrapper()); } BOOST_CATCH(...) { for (src = sub(m_first, n); src != dest; increment(src)) destroy_if_constructed(src); diff --git a/include/boost/circular_buffer/details.hpp b/include/boost/circular_buffer/details.hpp index 19757fc..39f2b8d 100644 --- a/include/boost/circular_buffer/details.hpp +++ b/include/boost/circular_buffer/details.hpp @@ -76,7 +76,7 @@ template struct iterator_wrapper { mutable Iterator m_it; explicit iterator_wrapper(Iterator it) : m_it(it) {} - Iterator get() const { return m_it++; } + Iterator operator () () const { return m_it++; } }; /*! @@ -85,9 +85,9 @@ struct iterator_wrapper { */ template struct item_wrapper { - Pointer m_item; - explicit item_wrapper(Value item) : m_item(&item) {} - Pointer get() const { return m_item; } + Value m_item; + explicit item_wrapper(Value item) : m_item(item) {} + Pointer operator () () const { return &m_item; } }; /*! diff --git a/test/iterator_test.cpp b/test/iterator_test.cpp index 4806ae7..13b4ab2 100644 --- a/test/iterator_test.cpp +++ b/test/iterator_test.cpp @@ -1,5 +1,5 @@ // Test of the iterator of the circular buffer. -// Note: This test concentrates on interator validity only. Other iterator +// Note: This test concentrates on iterator validity only. Other iterator // tests are included in the base_test.cpp. // Copyright (c) 2003-2005 Jan Gaspar @@ -12,12 +12,60 @@ #include "test.hpp" -// TODO -void validity_test() { +// test of the example (introduced in the documentation) +void validity_example_test() { - circular_buffer_space_optimized cb; - - BOOST_CHECK(cb.capacity() == cb.max_size()); + circular_buffer cb(3); + + cb.push_back(1); + cb.push_back(2); + cb.push_back(3); + + circular_buffer::iterator it = cb.begin(); + + BOOST_CHECK(*it == 1); + + cb.push_back(4); + + BOOST_CHECK(*it == 4); +} + +void validity_insert_test() { + + circular_buffer cb1(4); + cb1.push_back(1); + cb1.push_back(2); + cb1.push_back(3); + cb1.push_back(4); + circular_buffer::iterator it1 = cb1.begin(); + BOOST_CHECK(*it1 == 1); + cb1.insert(cb1.begin() + 1, 5); + BOOST_CHECK(*it1 == 4); + + circular_buffer cb2(4); + 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); +} + +void validity_test() { + // erase + // rerase + // insert + // rinsert + // push_back + // push_front + // linearize + // swap + // set_capacity + // rset_capacity + // resize + // rresize } // test main @@ -25,7 +73,8 @@ test_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_test)); + tests->add(BOOST_TEST_CASE(&validity_example_test)); + tests->add(BOOST_TEST_CASE(&validity_insert_test)); return tests; }