Full merge from trunk at revision 41356 of entire boost-root tree.

[SVN r41369]
This commit is contained in:
Beman Dawes
2007-11-25 18:07:19 +00:00
parent dd153e62b9
commit ee98eb933a
23 changed files with 0 additions and 15852 deletions

View File

@@ -1,17 +0,0 @@
# Boost circular_buffer test Jamfile.
#
# Copyright (c) 2003-2007 Jan Gaspar
#
# Distributed under 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)
# Bring in rules for testing.
import testing ;
test-suite "circular_buffer"
: [ run base_test.cpp : <threading>single : ]
[ run space_optimized_test.cpp : <threading>single : ]
[ run soft_iterator_invalidation.cpp : <threading>single : ]
[ compile bounded_buffer_comparison.cpp : <threading>multi : ]
;

View File

@@ -1,704 +0,0 @@
// Test of the base circular buffer container.
// Copyright (c) 2003-2007 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"
#define CB_CONTAINER circular_buffer
#include "common.ipp"
void iterator_constructor_and_assign_test() {
circular_buffer<MyInteger> cb(4, 3);
circular_buffer<MyInteger>::iterator it = cb.begin();
circular_buffer<MyInteger>::iterator itCopy;
itCopy = it;
it = it;
circular_buffer<MyInteger>::const_iterator cit;
cit = it;
circular_buffer<MyInteger>::const_iterator end1 = cb.end();
circular_buffer<MyInteger>::const_iterator end2 = end1;
BOOST_CHECK(itCopy == it);
BOOST_CHECK(cit == it);
BOOST_CHECK(end1 == end2);
BOOST_CHECK(it != end1);
BOOST_CHECK(cit != end2);
}
void iterator_reference_test() {
circular_buffer<Dummy> cb(3, Dummy());
circular_buffer<Dummy>::iterator it = cb.begin();
circular_buffer<Dummy>::const_iterator cit = cb.begin() + 1;
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() {
circular_buffer<MyInteger> cb(5, 1);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it1 = cb.begin() + 2;
circular_buffer<MyInteger>::iterator it2 = cb.begin() + 3;
circular_buffer<MyInteger>::const_iterator begin = cb.begin();
circular_buffer<MyInteger>::iterator end = cb.end();
BOOST_CHECK(begin - begin == 0);
BOOST_CHECK(end - cb.begin() == 5);
BOOST_CHECK(end - end == 0);
BOOST_CHECK(begin - cb.end() == -5);
BOOST_CHECK(it1 - cb.begin() == 2);
BOOST_CHECK(end - it1 == 3);
BOOST_CHECK(it2 - it1 == 1);
BOOST_CHECK(it1 - it2 == -1);
BOOST_CHECK(it2 - it2 == 0);
}
void iterator_increment_test() {
circular_buffer<MyInteger> cb(10, 1);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it1 = cb.begin();
circular_buffer<MyInteger>::iterator it2 = cb.begin() + 5;
circular_buffer<MyInteger>::iterator it3 = cb.begin() + 9;
it1++;
it2++;
++it3;
BOOST_CHECK(it1 == cb.begin() + 1);
BOOST_CHECK(it2 == cb.begin() + 6);
BOOST_CHECK(it3 == cb.end());
}
void iterator_decrement_test() {
circular_buffer<MyInteger> cb(10, 1);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it1= cb.end();
circular_buffer<MyInteger>::iterator it2= cb.end() - 5;
circular_buffer<MyInteger>::iterator it3= cb.end() - 9;
--it1;
it2--;
--it3;
BOOST_CHECK(it1 == cb.end() - 1);
BOOST_CHECK(it2 == cb.end() - 6);
BOOST_CHECK(it3 == cb.begin());
}
void iterator_addition_test() {
circular_buffer<MyInteger> cb(10, 1);
cb.push_back(2);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it1 = cb.begin() + 2;
circular_buffer<MyInteger>::iterator it2 = cb.end();
circular_buffer<MyInteger>::iterator it3 = cb.begin() + 5;
circular_buffer<MyInteger>::iterator it4 = cb.begin() + 9;
it1 += 3;
it2 += 0;
it3 += 5;
it4 += -2;
BOOST_CHECK(it1 == 5 + cb.begin());
BOOST_CHECK(it2 == cb.end());
BOOST_CHECK(it3 == cb.end());
BOOST_CHECK(it4 + 3 == cb.end());
BOOST_CHECK((-3) + it4 == cb.begin() + 4);
BOOST_CHECK(cb.begin() + 0 == cb.begin());
}
void iterator_subtraction_test() {
circular_buffer<MyInteger> cb(10, 1);
cb.push_back(2);
cb.push_back(2);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it1 = cb.begin();
circular_buffer<MyInteger>::iterator it2 = cb.end();
circular_buffer<MyInteger>::iterator it3 = cb.end() - 5;
circular_buffer<MyInteger>::iterator it4 = cb.begin() + 7;
it1 -= -2;
it2 -= 0;
it3 -= 5;
BOOST_CHECK(it1 == cb.begin() + 2);
BOOST_CHECK(it2 == cb.end());
BOOST_CHECK(it3 == cb.begin());
BOOST_CHECK(it4 - 7 == cb.begin());
BOOST_CHECK(it4 - (-3) == cb.end());
BOOST_CHECK(cb.begin() - 0 == cb.begin());
}
void iterator_element_access_test() {
circular_buffer<MyInteger> cb(10);
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
cb.push_back(4);
cb.push_back(5);
cb.push_back(6);
circular_buffer<MyInteger>::iterator it = cb.begin() + 1;
BOOST_CHECK(it[0] == 2);
BOOST_CHECK(it[-1] == 1);
BOOST_CHECK(it[2] == 4);
}
void iterator_comparison_test() {
circular_buffer<MyInteger> cb(5, 1);
cb.push_back(2);
circular_buffer<MyInteger>::iterator it = cb.begin() + 2;
circular_buffer<MyInteger>::const_iterator begin = cb.begin();
circular_buffer<MyInteger>::iterator end = cb.end();
BOOST_CHECK(begin == begin);
BOOST_CHECK(end > cb.begin());
BOOST_CHECK(begin < end);
BOOST_CHECK(end > begin);
BOOST_CHECK(end == end);
BOOST_CHECK(begin < cb.end());
BOOST_CHECK(!(begin + 1 > cb.end()));
BOOST_CHECK(it > cb.begin());
BOOST_CHECK(end > it);
BOOST_CHECK(begin >= begin);
BOOST_CHECK(end >= cb.begin());
BOOST_CHECK(end <= end);
BOOST_CHECK(begin <= cb.end());
BOOST_CHECK(it >= cb.begin());
BOOST_CHECK(end >= it);
BOOST_CHECK(!(begin + 4 < begin + 4));
BOOST_CHECK(begin + 4 < begin + 5);
BOOST_CHECK(!(begin + 5 < begin + 4));
BOOST_CHECK(it < end - 1);
BOOST_CHECK(!(end - 1 < it));
}
// TODO add insert, push_back etc.
void iterator_invalidation_test() {
#if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
circular_buffer<MyInteger>::iterator it1;
circular_buffer<MyInteger>::const_iterator it2;
circular_buffer<MyInteger>::iterator it3;
circular_buffer<MyInteger>::const_iterator it4;
circular_buffer<MyInteger>::const_iterator it5;
circular_buffer<MyInteger>::const_iterator it6;
BOOST_CHECK(it1.is_valid(0));
BOOST_CHECK(it2.is_valid(0));
BOOST_CHECK(it3.is_valid(0));
BOOST_CHECK(it4.is_valid(0));
BOOST_CHECK(it5.is_valid(0));
BOOST_CHECK(it6.is_valid(0));
{
circular_buffer<MyInteger> cb(5, 0);
const circular_buffer<MyInteger> ccb(5, 0);
it1 = cb.begin();
it2 = ccb.begin();
it3 = cb.end();
it4 = it1;
it5 = it2;
it6 = it1;
BOOST_CHECK(it1.is_valid(&cb));
BOOST_CHECK(it2.is_valid(&ccb));
BOOST_CHECK(it3.is_valid(&cb));
BOOST_CHECK(it4.is_valid(&cb));
BOOST_CHECK(it5.is_valid(&ccb));
BOOST_CHECK(it6.is_valid(&cb));
}
BOOST_CHECK(it1.is_valid(0));
BOOST_CHECK(it2.is_valid(0));
BOOST_CHECK(it3.is_valid(0));
BOOST_CHECK(it4.is_valid(0));
BOOST_CHECK(it5.is_valid(0));
BOOST_CHECK(it6.is_valid(0));
circular_buffer<MyInteger> cb(10, 0);
it1 = cb.end();
cb.clear();
BOOST_CHECK(it1.is_valid(&cb));
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
int i = 0;
for (it2 = cb.begin(); it2 != it1; it2++, i++);
BOOST_CHECK(i == 3);
circular_buffer<MyInteger> cb1(10, 0);
circular_buffer<MyInteger> cb2(20, 0);
it1 = cb1.end();
it2 = cb2.begin();
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(it2.is_valid(&cb2));
cb1.swap(cb2);
BOOST_CHECK(!it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb2));
it1 = cb1.begin() + 3;
it2 = cb1.begin();
cb1.push_back(1);
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(*it2.m_it == 1);
circular_buffer<MyInteger> cb3(5);
cb3.push_back(1);
cb3.push_back(2);
cb3.push_back(3);
cb3.push_back(4);
cb3.push_back(5);
it1 = cb3.begin() + 2;
it2 = cb3.begin();
cb3.insert(cb3.begin() + 3, 6);
BOOST_CHECK(it1.is_valid(&cb3));
BOOST_CHECK(!it2.is_valid(&cb3));
BOOST_CHECK(*it2.m_it == 5);
it1 = cb3.begin() + 3;
it2 = cb3.end() - 1;
cb3.push_front(7);
BOOST_CHECK(it1.is_valid(&cb3));
BOOST_CHECK(!it2.is_valid(&cb3));
BOOST_CHECK(*it2.m_it == 7);
circular_buffer<MyInteger> cb4(5);
cb4.push_back(1);
cb4.push_back(2);
cb4.push_back(3);
cb4.push_back(4);
cb4.push_back(5);
it1 = cb4.begin() + 3;
it2 = cb4.begin();
cb4.rinsert(cb4.begin() + 2, 6);
BOOST_CHECK(it1.is_valid(&cb4));
BOOST_CHECK(!it2.is_valid(&cb4));
BOOST_CHECK(*it2.m_it == 2);
it1 = cb1.begin() + 5;
it2 = cb1.end() - 1;
cb1.pop_back();
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
it1 = cb1.begin() + 5;
it2 = cb1.begin();
cb1.pop_front();
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
circular_buffer<MyInteger> cb5(20, 0);
it1 = cb5.begin() + 5;
it2 = it3 = cb5.begin() + 15;
cb5.erase(cb5.begin() + 10);
BOOST_CHECK(it1.is_valid(&cb5));
BOOST_CHECK(!it2.is_valid(&cb5));
BOOST_CHECK(!it3.is_valid(&cb5));
it1 = cb5.begin() + 1;
it2 = it3 = cb5.begin() + 8;
cb5.erase(cb5.begin() + 3, cb5.begin() + 7);
BOOST_CHECK(it1.is_valid(&cb5));
BOOST_CHECK(!it2.is_valid(&cb5));
BOOST_CHECK(!it3.is_valid(&cb5));
circular_buffer<MyInteger> cb6(20, 0);
it4 = it1 = cb6.begin() + 5;
it2 = cb6.begin() + 15;
cb6.rerase(cb6.begin() + 10);
BOOST_CHECK(!it1.is_valid(&cb6));
BOOST_CHECK(!it4.is_valid(&cb6));
BOOST_CHECK(it2.is_valid(&cb6));
it4 = it1 = cb6.begin() + 1;
it2 = cb6.begin() + 8;
cb6.rerase(cb6.begin() + 3, cb6.begin() + 7);
BOOST_CHECK(!it1.is_valid(&cb6));
BOOST_CHECK(!it4.is_valid(&cb6));
BOOST_CHECK(it2.is_valid(&cb6));
circular_buffer<MyInteger> cb7(10, 1);
cb7.push_back(2);
cb7.push_back(3);
cb7.push_back(4);
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.linearize();
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(!it2.is_valid(&cb7));
BOOST_CHECK(!it3.is_valid(&cb7));
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.linearize();
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(it2.is_valid(&cb7));
BOOST_CHECK(it3.is_valid(&cb7));
cb7.push_back(5);
cb7.push_back(6);
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.set_capacity(10);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(it2.is_valid(&cb7));
BOOST_CHECK(it3.is_valid(&cb7));
cb7.set_capacity(20);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(!it2.is_valid(&cb7));
BOOST_CHECK(!it3.is_valid(&cb7));
cb7.push_back(7);
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.set_capacity(10);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(!it2.is_valid(&cb7));
BOOST_CHECK(!it3.is_valid(&cb7));
cb7.push_back(8);
cb7.push_back(9);
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.rset_capacity(10);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(it2.is_valid(&cb7));
BOOST_CHECK(it3.is_valid(&cb7));
cb7.rset_capacity(20);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(!it2.is_valid(&cb7));
BOOST_CHECK(!it3.is_valid(&cb7));
cb7.push_back(10);
it1 = cb7.end();
it2 = cb7.begin();
it3 = cb7.begin() + 6;
cb7.rset_capacity(10);
BOOST_CHECK(it1.is_valid(&cb7));
BOOST_CHECK(!it2.is_valid(&cb7));
BOOST_CHECK(!it3.is_valid(&cb7));
circular_buffer<MyInteger> cb8(10, 1);
cb8.push_back(2);
cb8.push_back(3);
it1 = cb8.end();
it2 = cb8.begin();
it3 = cb8.begin() + 6;
cb8.resize(10);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(it2.is_valid(&cb8));
BOOST_CHECK(it3.is_valid(&cb8));
cb8.resize(20);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(!it2.is_valid(&cb8));
BOOST_CHECK(!it3.is_valid(&cb8));
cb8.push_back(4);
it1 = cb8.end();
it2 = cb8.begin();
it3 = cb8.begin() + 6;
it4 = cb8.begin() + 12;
cb8.resize(10);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(it2.is_valid(&cb8));
BOOST_CHECK(it3.is_valid(&cb8));
BOOST_CHECK(!it4.is_valid(&cb8));
cb8.set_capacity(10);
cb8.push_back(5);
cb8.push_back(6);
it1 = cb8.end();
it2 = cb8.begin();
it3 = cb8.begin() + 6;
cb8.rresize(10);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(it2.is_valid(&cb8));
BOOST_CHECK(it3.is_valid(&cb8));
cb8.rresize(20);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(!it2.is_valid(&cb8));
BOOST_CHECK(!it3.is_valid(&cb8));
cb8.push_back(7);
it1 = cb8.end();
it2 = cb8.begin();
it3 = cb8.begin() + 6;
it4 = cb8.begin() + 12;
cb8.rresize(10);
BOOST_CHECK(it1.is_valid(&cb8));
BOOST_CHECK(!it2.is_valid(&cb8));
BOOST_CHECK(!it3.is_valid(&cb8));
BOOST_CHECK(it4.is_valid(&cb8));
circular_buffer<MyInteger> cb9(15, 1);
it1 = cb9.end();
it2 = cb9.begin();
it3 = cb9.begin() + 6;
it4 = cb9.begin() + 12;
cb9 = cb8;
BOOST_CHECK(it1.is_valid(&cb9));
BOOST_CHECK(!it2.is_valid(&cb9));
BOOST_CHECK(!it3.is_valid(&cb9));
BOOST_CHECK(!it4.is_valid(&cb9));
circular_buffer<MyInteger> cb10(10, 1);
it1 = cb10.end();
it2 = cb10.begin();
it3 = cb10.begin() + 3;
it4 = cb10.begin() + 7;
cb10.assign(5, 2);
BOOST_CHECK(it1.is_valid(&cb10));
BOOST_CHECK(!it2.is_valid(&cb10));
BOOST_CHECK(!it3.is_valid(&cb10));
BOOST_CHECK(!it4.is_valid(&cb10));
circular_buffer<MyInteger> cb11(10, 1);
it1 = cb11.end();
it2 = cb11.begin();
it3 = cb11.begin() + 3;
it4 = cb11.begin() + 7;
cb11.assign(15, 5, 2);
BOOST_CHECK(it1.is_valid(&cb11));
BOOST_CHECK(!it2.is_valid(&cb11));
BOOST_CHECK(!it3.is_valid(&cb11));
BOOST_CHECK(!it4.is_valid(&cb11));
circular_buffer<MyInteger> cb12(10, 1);
it1 = cb12.end();
it2 = cb12.begin();
it3 = cb12.begin() + 3;
it4 = cb12.begin() + 7;
cb12.assign(cb11.begin(), cb11.end());
BOOST_CHECK(it1.is_valid(&cb12));
BOOST_CHECK(!it2.is_valid(&cb12));
BOOST_CHECK(!it3.is_valid(&cb12));
BOOST_CHECK(!it4.is_valid(&cb12));
circular_buffer<MyInteger> cb13(10, 1);
it1 = cb13.end();
it2 = cb13.begin();
it3 = cb13.begin() + 3;
it4 = cb13.begin() + 7;
cb13.assign(15, cb11.begin(), cb11.end());
BOOST_CHECK(it1.is_valid(&cb13));
BOOST_CHECK(!it2.is_valid(&cb13));
BOOST_CHECK(!it3.is_valid(&cb13));
BOOST_CHECK(!it4.is_valid(&cb13));
#endif // #if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
}
// basic exception safety test (it is useful to use any memory-leak detection tool)
void exception_safety_test() {
#if !defined(BOOST_NO_EXCEPTIONS)
circular_buffer<MyInteger> cb1(3, 5);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb1.set_capacity(5), std::exception);
BOOST_CHECK(cb1.capacity() == 3);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb1.rset_capacity(5), std::exception);
BOOST_CHECK(cb1.capacity() == 3);
generic_test(cb1);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(circular_buffer<MyInteger> cb2(5, 10), std::exception);
circular_buffer<MyInteger> cb3(5, 10);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(circular_buffer<MyInteger> cb4(cb3), std::exception);
vector<MyInteger> v(5, MyInteger(10));
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(circular_buffer<MyInteger> cb5(8, v.begin(), v.end()), std::exception);
circular_buffer<MyInteger> cb6(5, 10);
circular_buffer<MyInteger> cb7(8, 3);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb7 = cb6, std::exception);
BOOST_CHECK(cb7.size() == 8);
BOOST_CHECK(cb7.capacity() == 8);
BOOST_CHECK(cb7[0] == 3);
BOOST_CHECK(cb7[7] == 3);
generic_test(cb7);
circular_buffer<MyInteger> cb8(5, 10);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb8.push_front(1), std::exception);
circular_buffer<MyInteger> cb9(5);
cb9.push_back(1);
cb9.push_back(2);
cb9.push_back(3);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb9.insert(cb9.begin() + 1, 4), std::exception);
circular_buffer<MyInteger> cb10(5);
cb10.push_back(1);
cb10.push_back(2);
cb10.push_back(3);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb10.rinsert(cb10.begin() + 1, 4), std::exception);
circular_buffer<MyInteger> cb11(5);
cb11.push_back(1);
cb11.push_back(2);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb11.rinsert(cb11.begin(), 1), std::exception);
circular_buffer<MyInteger> cb12(5, 1);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb12.assign(4, 2), std::exception);
circular_buffer<MyInteger> cb13(5, 1);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb13.assign(6, 2), std::exception);
circular_buffer<MyInteger> cb14(5);
cb14.push_back(1);
cb14.push_back(2);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb14.insert(cb14.begin(), 10, 3), std::exception);
circular_buffer<MyInteger> cb15(5);
cb15.push_back(1);
cb15.push_back(2);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb15.insert(cb15.end(), 10, 3), std::exception);
circular_buffer<MyInteger> cb16(5);
cb16.push_back(1);
cb16.push_back(2);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb16.rinsert(cb16.begin(), 10, 3), std::exception);
circular_buffer<MyInteger> cb17(5);
cb17.push_back(1);
cb17.push_back(2);
MyInteger::set_exception_trigger(3);
BOOST_CHECK_THROW(cb17.rinsert(cb17.end(), 10, 3), std::exception);
circular_buffer<MyInteger> cb18(5, 0);
cb18.push_back(1);
cb18.push_back(2);
cb18.pop_front();
MyInteger::set_exception_trigger(4);
BOOST_CHECK_THROW(cb18.linearize(), std::exception);
circular_buffer<MyInteger> cb19(5, 0);
cb19.push_back(1);
cb19.push_back(2);
MyInteger::set_exception_trigger(5);
BOOST_CHECK_THROW(cb19.linearize(), std::exception);
circular_buffer<MyInteger> cb20(5, 0);
cb20.push_back(1);
cb20.push_back(2);
MyInteger::set_exception_trigger(6);
BOOST_CHECK_THROW(cb20.linearize(), std::exception);
circular_buffer<MyInteger> cb21(5);
cb21.push_back(1);
cb21.push_back(2);
cb21.push_back(3);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb21.insert(cb21.begin() + 1, 4), std::exception);
circular_buffer<MyInteger> cb22(5);
cb22.push_back(1);
cb22.push_back(2);
cb22.push_back(3);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb22.insert(cb22.end(), 4), std::exception);
circular_buffer<MyInteger> cb23(5, 0);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb23.insert(cb23.begin() + 1, 4), std::exception);
circular_buffer<MyInteger> cb24(5);
cb24.push_back(1);
cb24.push_back(2);
cb24.push_back(3);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb24.rinsert(cb24.begin() + 1, 4), std::exception);
circular_buffer<MyInteger> cb25(5, 0);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb25.rinsert(cb25.begin() + 3, 4), std::exception);
circular_buffer<MyInteger> cb26(5);
cb26.push_back(1);
cb26.push_back(2);
MyInteger::set_exception_trigger(5);
BOOST_CHECK_THROW(cb26.insert(cb26.begin(), 10, 3), std::exception);
circular_buffer<MyInteger> cb27(5);
cb27.push_back(1);
cb27.push_back(2);
MyInteger::set_exception_trigger(5);
BOOST_CHECK_THROW(cb27.insert(cb27.end(), 10, 3), std::exception);
circular_buffer<MyInteger> cb28(5);
cb28.push_back(1);
cb28.push_back(2);
MyInteger::set_exception_trigger(5);
BOOST_CHECK_THROW(cb28.rinsert(cb28.begin(), 10, 3), std::exception);
circular_buffer<MyInteger> cb29(5);
cb29.push_back(1);
cb29.push_back(2);
MyInteger::set_exception_trigger(5);
BOOST_CHECK_THROW(cb29.rinsert(cb29.end(), 10, 3), std::exception);
circular_buffer<MyInteger> cb30(10);
cb30.push_back(1);
cb30.push_back(2);
cb30.push_back(3);
MyInteger::set_exception_trigger(2);
BOOST_CHECK_THROW(cb30.rinsert(cb30.begin(), 10, 3), std::exception);
#endif // #if !defined(BOOST_NO_EXCEPTIONS)
}
// test main
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) {
test_suite* tests = BOOST_TEST_SUITE("Unit tests for the circular_buffer.");
add_common_tests(tests);
tests->add(BOOST_TEST_CASE(&iterator_constructor_and_assign_test));
tests->add(BOOST_TEST_CASE(&iterator_reference_test));
tests->add(BOOST_TEST_CASE(&iterator_difference_test));
tests->add(BOOST_TEST_CASE(&iterator_increment_test));
tests->add(BOOST_TEST_CASE(&iterator_decrement_test));
tests->add(BOOST_TEST_CASE(&iterator_addition_test));
tests->add(BOOST_TEST_CASE(&iterator_subtraction_test));
tests->add(BOOST_TEST_CASE(&iterator_element_access_test));
tests->add(BOOST_TEST_CASE(&iterator_comparison_test));
tests->add(BOOST_TEST_CASE(&iterator_invalidation_test));
tests->add(BOOST_TEST_CASE(&exception_safety_test));
return tests;
}

View File

@@ -1,288 +0,0 @@
// Comparison of bounded buffers based on different containers.
// Copyright (c) 2003-2007 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
#include <boost/circular_buffer.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/thread.hpp>
#include <boost/progress.hpp>
#include <boost/bind.hpp>
#include <deque>
#include <list>
#include <string>
#include <iostream>
const unsigned long QUEUE_SIZE = 1000L;
const unsigned long TOTAL_ELEMENTS = QUEUE_SIZE * 1000L;
template <class T>
class bounded_buffer {
public:
typedef boost::circular_buffer<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
explicit bounded_buffer(size_type capacity) : m_unread(0), m_container(capacity) {}
void push_front(const value_type& item) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_full.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_full, this));
m_container.push_front(item);
++m_unread;
lock.unlock();
m_not_empty.notify_one();
}
void pop_back(value_type* pItem) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_empty.wait(lock, boost::bind(&bounded_buffer<value_type>::is_not_empty, this));
*pItem = m_container[--m_unread];
lock.unlock();
m_not_full.notify_one();
}
private:
bounded_buffer(const bounded_buffer&); // Disabled copy constructor
bounded_buffer& operator = (const bounded_buffer&); // Disabled assign operator
bool is_not_empty() const { return m_unread > 0; }
bool is_not_full() const { return m_unread < m_container.capacity(); }
size_type m_unread;
container_type m_container;
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
};
template <class T>
class bounded_buffer_space_optimized {
public:
typedef boost::circular_buffer_space_optimized<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
explicit bounded_buffer_space_optimized(size_type capacity) : m_container(capacity) {}
void push_front(const value_type& item) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_full.wait(lock, boost::bind(&bounded_buffer_space_optimized<value_type>::is_not_full, this));
m_container.push_front(item);
lock.unlock();
m_not_empty.notify_one();
}
void pop_back(value_type* pItem) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_empty.wait(lock, boost::bind(&bounded_buffer_space_optimized<value_type>::is_not_empty, this));
*pItem = m_container.back();
m_container.pop_back();
lock.unlock();
m_not_full.notify_one();
}
private:
bounded_buffer_space_optimized(const bounded_buffer_space_optimized&); // Disabled copy constructor
bounded_buffer_space_optimized& operator = (const bounded_buffer_space_optimized&); // Disabled assign operator
bool is_not_empty() const { return m_container.size() > 0; }
bool is_not_full() const { return m_container.size() < m_container.capacity(); }
container_type m_container;
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
};
template <class T>
class bounded_buffer_deque_based {
public:
typedef std::deque<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
explicit bounded_buffer_deque_based(size_type capacity) : m_capacity(capacity) {}
void push_front(const value_type& item) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_full.wait(lock, boost::bind(&bounded_buffer_deque_based<value_type>::is_not_full, this));
m_container.push_front(item);
lock.unlock();
m_not_empty.notify_one();
}
void pop_back(value_type* pItem) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_empty.wait(lock, boost::bind(&bounded_buffer_deque_based<value_type>::is_not_empty, this));
*pItem = m_container.back();
m_container.pop_back();
lock.unlock();
m_not_full.notify_one();
}
private:
bounded_buffer_deque_based(const bounded_buffer_deque_based&); // Disabled copy constructor
bounded_buffer_deque_based& operator = (const bounded_buffer_deque_based&); // Disabled assign operator
bool is_not_empty() const { return m_container.size() > 0; }
bool is_not_full() const { return m_container.size() < m_capacity; }
const size_type m_capacity;
container_type m_container;
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
};
template <class T>
class bounded_buffer_list_based {
public:
typedef std::list<T> container_type;
typedef typename container_type::size_type size_type;
typedef typename container_type::value_type value_type;
explicit bounded_buffer_list_based(size_type capacity) : m_capacity(capacity) {}
void push_front(const value_type& item) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_full.wait(lock, boost::bind(&bounded_buffer_list_based<value_type>::is_not_full, this));
m_container.push_front(item);
lock.unlock();
m_not_empty.notify_one();
}
void pop_back(value_type* pItem) {
boost::mutex::scoped_lock lock(m_mutex);
m_not_empty.wait(lock, boost::bind(&bounded_buffer_list_based<value_type>::is_not_empty, this));
*pItem = m_container.back();
m_container.pop_back();
lock.unlock();
m_not_full.notify_one();
}
private:
bounded_buffer_list_based(const bounded_buffer_list_based&); // Disabled copy constructor
bounded_buffer_list_based& operator = (const bounded_buffer_list_based&); // Disabled assign operator
bool is_not_empty() const { return m_container.size() > 0; }
bool is_not_full() const { return m_container.size() < m_capacity; }
const size_type m_capacity;
container_type m_container;
boost::mutex m_mutex;
boost::condition m_not_empty;
boost::condition m_not_full;
};
template<class Buffer>
class Consumer {
typedef typename Buffer::value_type value_type;
Buffer* m_container;
value_type m_item;
public:
Consumer(Buffer* buffer) : m_container(buffer) {}
void operator()() {
for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) {
m_container->pop_back(&m_item);
}
}
};
template<class Buffer>
class Producer {
typedef typename Buffer::value_type value_type;
Buffer* m_container;
public:
Producer(Buffer* buffer) : m_container(buffer) {}
void operator()() {
for (unsigned long i = 0L; i < TOTAL_ELEMENTS; ++i) {
m_container->push_front(value_type());
}
}
};
template<class Buffer>
void fifo_test(Buffer* buffer) {
// Start of measurement
boost::progress_timer progress;
// Initialize the buffer with some values before launching producer and consumer threads.
for (unsigned long i = QUEUE_SIZE / 2L; i > 0; --i) {
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x581))
buffer->push_front(Buffer::value_type());
#else
buffer->push_front(BOOST_DEDUCED_TYPENAME Buffer::value_type());
#endif
}
Consumer<Buffer> consumer(buffer);
Producer<Buffer> producer(buffer);
// Start the threads.
boost::thread consume(consumer);
boost::thread produce(producer);
// Wait for completion.
consume.join();
produce.join();
// End of measurement
}
int main(int /*argc*/, char* /*argv*/[]) {
bounded_buffer<int> bb_int(QUEUE_SIZE);
std::cout << "bounded_buffer<int> ";
fifo_test(&bb_int);
bounded_buffer_space_optimized<int> bb_space_optimized_int(QUEUE_SIZE);
std::cout << "bounded_buffer_space_optimized<int> ";
fifo_test(&bb_space_optimized_int);
bounded_buffer_deque_based<int> bb_deque_based_int(QUEUE_SIZE);
std::cout << "bounded_buffer_deque_based<int> ";
fifo_test(&bb_deque_based_int);
bounded_buffer_list_based<int> bb_list_based_int(QUEUE_SIZE);
std::cout << "bounded_buffer_list_based<int> ";
fifo_test(&bb_list_based_int);
bounded_buffer<std::string> bb_string(QUEUE_SIZE);
std::cout << "bounded_buffer<std::string> ";
fifo_test(&bb_string);
bounded_buffer_space_optimized<std::string> bb_space_optimized_string(QUEUE_SIZE);
std::cout << "bounded_buffer_space_optimized<std::string> ";
fifo_test(&bb_space_optimized_string);
bounded_buffer_deque_based<std::string> bb_deque_based_string(QUEUE_SIZE);
std::cout << "bounded_buffer_deque_based<std::string> ";
fifo_test(&bb_deque_based_string);
bounded_buffer_list_based<std::string> bb_list_based_string(QUEUE_SIZE);
std::cout << "bounded_buffer_list_based<std::string> ";
fifo_test(&bb_list_based_string);
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,747 +0,0 @@
// 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-2007 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
#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_CHECK(*it == 1);
cb.push_back(4);
BOOST_CHECK(*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_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<int>::iterator it4 = it1 + 3;
cb.insert(cb.begin() + 1, 5);
// 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() {
// 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_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<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_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<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_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<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_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<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_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<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_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);
}
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_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<int>::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<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_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<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_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<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_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<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_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<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_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 main
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_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;
}

View File

@@ -1,191 +0,0 @@
// Test of the space optimized adaptor of the circular buffer.
// Copyright (c) 2003-2007 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"
#define CB_CONTAINER circular_buffer_space_optimized
#include "common.ipp"
typedef circular_buffer_space_optimized<MyInteger> cb_space_optimized;
typedef cb_space_optimized::capacity_type capacity_ctrl;
// min_capacity test (it is useful to use a debug tool)
void min_capacity_test() {
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
cb_space_optimized cb1(capacity_ctrl(10, 10));
cb_space_optimized cb2(capacity_ctrl(10, 5), 1);
cb_space_optimized cb3(capacity_ctrl(20, 10), v.begin(), v.end());
BOOST_CHECK(cb1.size() == 0);
BOOST_CHECK(cb1.capacity().capacity() == 10);
BOOST_CHECK(cb1.capacity().min_capacity() == 10);
BOOST_CHECK(cb2[0] == 1);
BOOST_CHECK(cb2.size() == 10);
BOOST_CHECK(cb2.capacity() == 10);
BOOST_CHECK(cb2.capacity().min_capacity() == 5);
BOOST_CHECK(cb3[0] == 1);
BOOST_CHECK(cb3.size() == 5);
BOOST_CHECK(cb3.capacity() == 20);
BOOST_CHECK(cb3.capacity().min_capacity() == 10);
BOOST_CHECK(cb1.capacity().min_capacity() <= cb1.internal_capacity());
BOOST_CHECK(cb2.capacity().min_capacity() <= cb2.internal_capacity());
BOOST_CHECK(cb3.capacity().min_capacity() <= cb3.internal_capacity());
cb2.erase(cb2.begin() + 2, cb2.end());
BOOST_CHECK(cb2.size() == 2);
BOOST_CHECK(cb2.capacity().min_capacity() <= cb2.internal_capacity());
cb2.clear();
cb3.clear();
BOOST_CHECK(cb2.empty());
BOOST_CHECK(cb3.empty());
BOOST_CHECK(cb2.capacity().min_capacity() <= cb2.internal_capacity());
BOOST_CHECK(cb3.capacity().min_capacity() <= cb3.internal_capacity());
}
void capacity_control_test() {
circular_buffer_space_optimized<int>::capacity_type c1 = 10;
circular_buffer_space_optimized<int>::capacity_type c2 =
circular_buffer_space_optimized<int>::capacity_type(20, 5);
circular_buffer_space_optimized<int>::capacity_type c3 = c2;
BOOST_CHECK(c1.capacity() == 10);
BOOST_CHECK(c1.min_capacity() == 0);
BOOST_CHECK(c2.capacity() == 20);
BOOST_CHECK(c2.min_capacity() == 5);
BOOST_CHECK(c3.capacity() == 20);
BOOST_CHECK(c3.min_capacity() == 5);
c1 = c2;
BOOST_CHECK(c1.capacity() == 20);
BOOST_CHECK(c1.min_capacity() == 5);
}
void some_constructors_test() {
cb_space_optimized cb1;
BOOST_CHECK(cb1.capacity() == cb1.max_size());
BOOST_CHECK(cb1.capacity().min_capacity() == 0);
BOOST_CHECK(cb1.internal_capacity() == 0);
BOOST_CHECK(cb1.size() == 0);
cb1.push_back(1);
cb1.push_back(2);
cb1.push_back(3);
BOOST_CHECK(cb1.size() == 3);
BOOST_CHECK(cb1.capacity() == cb1.max_size());
cb_space_optimized cb2(cb1.begin(), cb1.end());
BOOST_CHECK(cb2.capacity() == 3);
BOOST_CHECK(cb2.capacity().min_capacity() == 0);
BOOST_CHECK(cb2.size() == 3);
}
void shrink_to_fit_test() {
cb_space_optimized cb(1000);
cb.push_back(1);
cb.push_back(2);
cb.push_back(3);
BOOST_CHECK(cb.size() == 3);
BOOST_CHECK(cb.capacity() == 1000);
size_t internal_capacity = cb.internal_capacity();
cb_space_optimized(cb).swap(cb);
BOOST_CHECK(cb.size() == 3);
BOOST_CHECK(cb.capacity() == 1000);
BOOST_CHECK(internal_capacity >= cb.internal_capacity());
}
void iterator_invalidation_test() {
#if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
cb_space_optimized cb1(10, 1);
cb1.push_back(2);
cb1.push_back(3);
cb1.push_back(4);
cb_space_optimized::iterator it1 = cb1.end();
cb_space_optimized::const_iterator it2 = cb1.begin();
cb_space_optimized::iterator it3 = cb1.begin() + 6;
cb1.set_capacity(10);
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(!it3.is_valid(&cb1));
it1 = cb1.end();
it2 = cb1.begin();
it3 = cb1.begin() + 6;
cb1.rset_capacity(10);
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(!it3.is_valid(&cb1));
it1 = cb1.end();
it2 = cb1.begin();
it3 = cb1.begin() + 6;
cb1.resize(10);
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(!it3.is_valid(&cb1));
it1 = cb1.end();
it2 = cb1.begin();
it3 = cb1.begin() + 6;
cb1.rresize(10);
BOOST_CHECK(it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(!it3.is_valid(&cb1));
{
cb_space_optimized cb2(10, 1);
cb2.push_back(2);
cb2.push_back(3);
cb2.push_back(4);
it1 = cb2.end();
it2 = cb2.begin();
it3 = cb2.begin() + 6;
}
BOOST_CHECK(!it1.is_valid(&cb1));
BOOST_CHECK(!it2.is_valid(&cb1));
BOOST_CHECK(!it3.is_valid(&cb1));
#endif // #if !defined(NDEBUG) && !defined(BOOST_CB_DISABLE_DEBUG)
}
// test main
test_suite* init_unit_test_suite(int /*argc*/, char* /*argv*/[]) {
test_suite* tests = BOOST_TEST_SUITE("Unit tests for the circular_buffer_space_optimized.");
add_common_tests(tests);
tests->add(BOOST_TEST_CASE(&min_capacity_test));
tests->add(BOOST_TEST_CASE(&capacity_control_test));
tests->add(BOOST_TEST_CASE(&some_constructors_test));
tests->add(BOOST_TEST_CASE(&shrink_to_fit_test));
tests->add(BOOST_TEST_CASE(&iterator_invalidation_test));
return tests;
}

View File

@@ -1,146 +0,0 @@
// Header file for the test of the circular buffer library.
// Copyright (c) 2003-2007 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)
#if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP)
#define BOOST_CIRCULAR_BUFFER_TEST_HPP
#if defined(_MSC_VER) && _MSC_VER >= 1200
#pragma once
#endif
#define BOOST_CB_TEST
#include <boost/circular_buffer.hpp>
#include <boost/test/included/unit_test_framework.hpp>
#include <boost/iterator.hpp>
#include <iterator>
#include <numeric>
#include <vector>
#if !defined(BOOST_NO_EXCEPTIONS)
#include <exception>
#endif
// Integer (substitute for int) - more appropriate for testing
class MyInteger {
private:
int* m_pValue;
static int ms_exception_trigger;
void check_exception() {
if (ms_exception_trigger > 0) {
if (--ms_exception_trigger == 0) {
delete m_pValue;
m_pValue = 0;
#if !defined(BOOST_NO_EXCEPTIONS)
throw std::exception();
#endif
}
}
}
public:
MyInteger() : m_pValue(new int(0)) { check_exception(); }
MyInteger(int i) : m_pValue(new int(i)) { check_exception(); }
MyInteger(const MyInteger& src) : m_pValue(new int(src)) { check_exception(); }
~MyInteger() { delete m_pValue; }
MyInteger& operator = (const MyInteger& src) {
if (this == &src)
return *this;
check_exception();
delete m_pValue;
m_pValue = new int(src);
return *this;
}
operator int () const { return *m_pValue; }
static void set_exception_trigger(int n) { ms_exception_trigger = n; }
};
// default constructible class
class MyDefaultConstructible
{
public:
MyDefaultConstructible() : m_n(1) {}
MyDefaultConstructible(int n) : m_n(n) {}
int m_n;
};
// class counting instances of self
class InstanceCounter {
public:
InstanceCounter() { increment(); }
InstanceCounter(const InstanceCounter& y) { y.increment(); }
~InstanceCounter() { decrement(); }
static int count() { return ms_count; }
private:
void increment() const { ++ms_count; }
void decrement() const { --ms_count; }
static int ms_count;
};
// dummy class suitable for iterator referencing test
class Dummy {
public:
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
struct MyInputIterator
: boost::iterator<std::input_iterator_tag, int, ptrdiff_t, int*, int&> {
typedef std::vector<int>::iterator vector_iterator;
typedef int value_type;
typedef int* pointer;
typedef int& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
explicit MyInputIterator(const vector_iterator& it) : m_it(it) {}
MyInputIterator& operator = (const MyInputIterator& it) {
if (this == &it)
return *this;
m_it = it.m_it;
return *this;
}
reference operator * () const { return *m_it; }
pointer operator -> () const { return &(operator*()); }
MyInputIterator& operator ++ () {
++m_it;
return *this;
}
MyInputIterator operator ++ (int) {
MyInputIterator tmp = *this;
++*this;
return tmp;
}
bool operator == (const MyInputIterator& it) const { return m_it == it.m_it; }
bool operator != (const MyInputIterator& it) const { return m_it != it.m_it; }
private:
vector_iterator m_it;
};
#if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
inline std::input_iterator_tag iterator_category(const MyInputIterator&) {
return std::input_iterator_tag();
}
inline int* value_type(const MyInputIterator&) { return 0; }
inline ptrdiff_t* distance_type(const MyInputIterator&) { return 0; }
#endif // #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR)
using boost::unit_test::test_suite;
using namespace boost;
using namespace std;
#endif // #if !defined(BOOST_CIRCULAR_BUFFER_TEST_HPP)