update from trunk

[SVN r46661]
This commit is contained in:
Thorsten Jørgen Ottosen 2008-06-24 20:36:58 +00:00
parent 3f91ea1ff5
commit 377920a2bd
12 changed files with 1085 additions and 45 deletions

8
test/Jamfile.v2 Executable file → Normal file
View File

@ -18,11 +18,13 @@ rule sc-test ( name : extra-sources * )
test-suite ptr_container :
[ sc-test ptr_inserter ]
[ sc-test ptr_vector ]
[ sc-test ptr_list ]
[ sc-test ptr_deque ]
[ sc-test ptr_set ]
[ sc-test ptr_map ]
[ sc-test ptr_map_adapter ]
[ sc-test ptr_array ]
[ sc-test tree_test ]
[ sc-test incomplete_type_test ]
@ -32,5 +34,9 @@ test-suite ptr_container :
[ sc-test indirect_fun ]
[ sc-test serialization : /boost/serialization//boost_serialization ]
[ sc-test no_exceptions ]
[ sc-test ptr_unordered_set ]
[ sc-test ptr_unordered_map ]
[ sc-test ptr_circular_buffer ]
;

55
test/associative_test_data.hpp Executable file → Normal file
View File

@ -13,10 +13,44 @@
#include <boost/ptr_container/exception.hpp>
#include <boost/range/sub_range.hpp>
template< typename C, typename B, typename T >
template< typename C, typename B, typename T, bool Ordered >
void ptr_set_test();
template< typename C, typename B, typename T >
template< class T, bool Ordered >
struct test_algorithms
{
template< class Cont >
void operator()( Cont& c, const Cont& c2 ) const
{
typename Cont::iterator i;
typename Cont::const_iterator ci;
T* t = new T;
i = c.lower_bound( *t );
ci = c2.lower_bound( *t );
i = c.upper_bound( *t );
ci = c2.upper_bound( *t );
delete t;
BOOST_DEDUCED_TYPENAME Cont::reverse_iterator ri = c.rbegin();
BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cri = c2.rbegin();
BOOST_DEDUCED_TYPENAME Cont::reverse_iterator rv2 = c.rend();
BOOST_DEDUCED_TYPENAME Cont::const_reverse_iterator cvr2 = c2.rend();
cri = c.crbegin();
cri = c.crend();
}
};
template< class T >
struct test_algorithms<T,false>
{
template< class Cont>
void operator()( Cont& c, const Cont& c2 ) const
{
}
};
template< typename C, typename B, typename T, bool Ordered >
void ptr_set_test()
{
using namespace boost;
@ -27,7 +61,7 @@ void ptr_set_test()
BOOST_CHECK( c.size() == 0 );
c.insert( c.end(), new T );
c.insert( c.end(), new T );
const C c2( c.begin(), c.end() );
BOOST_CHECK( c.size() == c2.size() );
@ -49,12 +83,10 @@ void ptr_set_test()
BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator();
BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
ci = c.cbegin();
ci = c.cend();
BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
BOOST_DEDUCED_TYPENAME C::reverse_iterator ri = c.rbegin();
BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cri = c2.rbegin();
BOOST_DEDUCED_TYPENAME C::reverse_iterator rv2 = c.rend();
BOOST_DEDUCED_TYPENAME C::const_reverse_iterator cvr2 = c2.rend();
BOOST_MESSAGE( "finished iterator test" );
@ -111,6 +143,7 @@ void ptr_set_test()
BOOST_CHECK( c3.empty() == false );
c.clear();
unsigned long c3size = c3.size();
hide_warning( c3size );
unsigned long num = c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(),
c3.end(),
c3 );
@ -142,14 +175,12 @@ void ptr_set_test()
i = c.find( *t );
ci = c2.find( *t );
c2.count( *t );
i = c.lower_bound( *t );
ci = c2.lower_bound( *t );
i = c.upper_bound( *t );
ci = c2.upper_bound( *t );
test_algorithms<T,Ordered>()( c, c2 );
sub = c.equal_range( *t );
csub = c2.equal_range( *t );
delete t;
BOOST_MESSAGE( "finished algorithms interface test" );
}

View File

@ -0,0 +1,198 @@
//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2003-2005. 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)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include <boost/test/unit_test.hpp>
#include "sequence_test_data.hpp"
#include <boost/ptr_container/ptr_circular_buffer.hpp>
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/assign/list_inserter.hpp>
template<class T>
struct set_capacity< ptr_circular_buffer<T> >
{
void operator()( ptr_circular_buffer<T>& c ) const
{
c.set_capacity( 100u );
}
};
void test_ptr_circular_buffer()
{
reversible_container_test< ptr_circular_buffer<Base>, Base, Derived_class >();
reversible_container_test< ptr_circular_buffer<Value>, Value, Value >();
#ifdef BOOST_NO_SFINAE
#else
reversible_container_test< ptr_circular_buffer< nullable<Base> >, Base, Derived_class >();
reversible_container_test< ptr_circular_buffer< nullable<Value> >, Value, Value >();
#endif
container_assignment_test< ptr_circular_buffer<Base>, ptr_circular_buffer<Derived_class>,
Derived_class>();
container_assignment_test< ptr_circular_buffer< nullable<Base> >,
ptr_circular_buffer< nullable<Derived_class> >,
Derived_class>();
container_assignment_test< ptr_circular_buffer< nullable<Base> >,
ptr_circular_buffer<Derived_class>,
Derived_class>();
container_assignment_test< ptr_circular_buffer<Base>,
ptr_circular_buffer< nullable<Derived_class> >,
Derived_class>();
test_transfer< ptr_circular_buffer<Derived_class>, ptr_circular_buffer<Base>, Derived_class>();
test_transfer< ptr_circular_buffer<Derived_class>, ptr_list<Base>, Derived_class>();
random_access_algorithms_test< ptr_circular_buffer<int> >();
BOOST_MESSAGE( "starting ptr_circular_buffer test" );
ptr_circular_buffer<int> vec( 100u );
BOOST_CHECK( vec.capacity() >= 100u );
#ifdef BOOST_PTR_CONTAINER_NO_EXCEPTIONS
#else
BOOST_CHECK_THROW( vec.push_back(0), bad_ptr_container_operation );
BOOST_CHECK_THROW( (vec.insert( vec.begin(), 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( vec.at( 42 ), bad_ptr_container_operation );
vec.push_back( new int(0) );
BOOST_CHECK_THROW( (vec.replace(10u, new int(0))), bad_ptr_container_operation );
BOOST_CHECK_THROW( (vec.replace(0u, 0)), bad_ptr_container_operation );
BOOST_CHECK_THROW( (vec.replace(vec.begin(), 0 )), bad_ptr_container_operation );
#endif
vec.clear();
assign::push_back( vec )( new int(2) )
( new int(4) )
( new int(6) )
( new int(8) );
ptr_circular_buffer<int> vec2( 100u );
assign::push_back( vec2 )
( new int(1) )
( new int(3) )
( new int(5) )
( new int(7) );
BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
BOOST_CHECK( vec > vec2 );
BOOST_CHECK( vec != vec2 );
BOOST_CHECK( !(vec == vec2) );
BOOST_CHECK( vec2 < vec );
BOOST_CHECK( vec2 <= vec );
BOOST_CHECK( vec >= vec2 );
BOOST_MESSAGE( "push_front test" );
assign::push_front( vec2 )
( new int(2) )
( new int(4) )
( new int(6) )
( new int(8) );
BOOST_CHECK_EQUAL( vec2.size(), 8u );
BOOST_CHECK_EQUAL( vec2[0], 8 );
BOOST_CHECK_EQUAL( vec2.front(), 8 );
BOOST_CHECK_EQUAL( vec2.back(), 7 );
//vec2.linearize();
vec2.rset_capacity( vec2.size() - 2u );
vec2.rresize( 0 );
//vec2.reverse();
BOOST_MESSAGE( "when full test" );
ptr_circular_buffer<int> vec3;
BOOST_CHECK_EQUAL( vec3.capacity(), 0u );
vec3.set_capacity( 2u );
BOOST_CHECK_EQUAL( vec3.capacity(), 2u );
vec3.push_back( new int(1) );
vec3.push_back( new int(2) );
BOOST_CHECK_EQUAL( vec3.size(), 2u );
BOOST_CHECK( vec3.full() );
vec3.push_back( new int(3) );
BOOST_CHECK_EQUAL( vec3.front(), 2 );
BOOST_CHECK_EQUAL( vec3.back(), 3 );
vec3.push_front( new int(4) );
BOOST_CHECK_EQUAL( vec3.size(), 2u );
BOOST_CHECK_EQUAL( vec3.front(), 4 );
BOOST_CHECK_EQUAL( vec3.back(), 2 );
vec3.insert( vec3.end(), new int(5) );
BOOST_CHECK_EQUAL( vec3.front(), 2 );
BOOST_CHECK_EQUAL( vec3.back(), 5 );
vec3.rinsert( vec3.begin(), new int(6) );
BOOST_CHECK_EQUAL( vec3.front(), 6 );
BOOST_CHECK_EQUAL( vec3.back(), 2 );
BOOST_MESSAGE( "transfer test" );
ptr_circular_buffer<int> vec4(2u);
vec4.transfer( vec4.end(), vec3 );
BOOST_CHECK_EQUAL( vec4.size(), 2u );
BOOST_CHECK_EQUAL( vec3.size(), 0u );
vec3.set_capacity(1u);
vec3.transfer( vec3.end(), vec4 );
BOOST_CHECK_EQUAL( vec4.size(), 0u );
BOOST_CHECK_EQUAL( vec3.size(), 1u );
BOOST_CHECK_EQUAL( vec3.front(), 2 );
BOOST_MESSAGE( "rerase test" );
vec.rerase( vec.begin() );
vec.rerase( boost::make_iterator_range( vec ) );
BOOST_MESSAGE( "array test" );
const int data_size = 10;
int** array = new int*[data_size];
for( int i = 0; i != data_size; ++i )
array[i] = new int(i);
vec.transfer( vec.begin(), array, data_size );
int** array2 = vec.c_array();
BOOST_CHECK( array2 != array );
ptr_circular_buffer<int>::array_range array_range = vec.array_one();
array_range = vec.array_two();
ptr_circular_buffer<int>::const_array_range const_array_range = array_range;
const_array_range = const_cast< const ptr_circular_buffer<int>& >(vec).array_one();
const_array_range = const_cast< const ptr_circular_buffer<int>& >(vec).array_two();
BOOST_MESSAGE( "finishing ptr_circular_buffer test" );
}
void test_circular_buffer()
{
boost::circular_buffer<void*> b(25u);
BOOST_CHECK_EQUAL( b.capacity(), 25u );
b.push_back( 0 );
BOOST_CHECK_EQUAL( b.size(), 1u );
boost::circular_buffer<void*> b2( b.begin(), b.end() );
BOOST_CHECK_EQUAL( b2.size(), b.size() );
}
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_circular_buffer ) );
test->add( BOOST_TEST_CASE( &test_ptr_circular_buffer ) );
return test;
}

93
test/ptr_inserter.cpp Normal file
View File

@ -0,0 +1,93 @@
//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2008. 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)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include <boost/ptr_container/ptr_inserter.hpp>
#include <boost/ptr_container/indirect_fun.hpp>
#include <boost/ptr_container/ptr_deque.hpp>
#include <boost/ptr_container/ptr_list.hpp>
#include <boost/assign/list_inserter.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/test/test_tools.hpp>
#include <algorithm>
#include <functional>
#include <string>
template< class T >
struct caster_to
{
typedef T result_type;
T operator()( void* obj ) const
{
return static_cast<T>( obj );
}
};
template< class PtrSequence >
void test_ptr_inserter_helper()
{
using namespace boost;
PtrSequence seq;
const int size = 1000;
for( int i = 0; i != size; ++i )
seq.push_back( i % 3 == 0 ? 0 : new int(i) );
PtrSequence seq2;
//
// @remark: we call .base() to avoid null pointer indirection.
// The clone_inserter will handle the nulls correctly.
//
std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ),
boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ),
ptr_container::ptr_back_inserter( seq2 ) );
std::copy( boost::make_transform_iterator( seq.begin().base(), caster_to<int*>() ),
boost::make_transform_iterator( seq.end().base(), caster_to<int*>() ),
ptr_container::ptr_front_inserter( seq2 ) );
BOOST_CHECK_EQUAL( seq.size()*2, seq2.size() );
PtrSequence seq3;
for( int i = 0; i != size; ++i )
seq3.push_back( new int(i%3) );
//
// @remark: since there are no nulls in this container, it
// is easier to handle.
//
std::copy( seq3.begin(), seq3.end(),
ptr_container::ptr_inserter( seq, seq.end() ) );
BOOST_CHECK_EQUAL( seq.size(), seq2.size() );
}
void test_ptr_inserter()
{
test_ptr_inserter_helper< boost::ptr_list< boost::nullable<int> > >();
test_ptr_inserter_helper< boost::ptr_deque< boost::nullable<int> > >();
}
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_ptr_inserter ) );
return test;
}

31
test/ptr_map.cpp Executable file → Normal file
View File

@ -381,6 +381,36 @@ void test_map()
}
#include <boost/tuple/tuple.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <map>
#include <boost/ptr_container/ptr_map.hpp>
void test_map_iterators()
{
using boost::zip_iterator;
using boost::tuple;
using boost::make_tuple;
using boost::ptr_map;
using std::map;
//typedef map<int, int> theMapType;
/*
@remark: the following will not compile
because of the proxy (non-reference) returned by operator*()
of the ptr_map's iterator type.
typedef boost::ptr_map<int, int> theMapType;
typedef zip_iterator
<tuple<theMapType::iterator, theMapType::iterator> > zipIter;
theMapType map1;
theMapType map2;
zipIter zip(make_tuple(map1.begin(), map2.begin()));
*/
}
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
@ -388,6 +418,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] )
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_map ) );
test->add( BOOST_TEST_CASE( &test_map_iterators ) );
return test;
}

37
test/ptr_map_adapter.cpp Executable file → Normal file
View File

@ -8,25 +8,26 @@
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include "test_data.hpp"
#include <boost/ptr_container/ptr_map.hpp>
#include <string>
using namespace std;
int test_main( int, char*[] )
void test_ptr_map_adapter()
{
typedef_test< ptr_map<int, Base>, Derived >();
typedef_test< ptr_map<int, Value>, Value >();
//typedef_test< ptr_map<int, Base>, Derived >();
//typedef_test< ptr_map<int, Value>, Value >();
associative_container_test< ptr_map<int, Base>, Base, Derived >();
associative_container_test< ptr_map<int, Value>, Value, Value >();
//associative_container_test< ptr_map<int, Base>, Base, Derived >();
//associative_container_test< ptr_map<int, Value>, Value, Value >();
typedef_test< ptr_multimap<int, Base>, Derived >();
typedef_test< ptr_multimap<int, Value>, Value >();
//typedef_test< ptr_multimap<int, Base>, Derived >();
//typedef_test< ptr_multimap<int, Value>, Value >();
associative_container_test< ptr_multimap<int, Base>, Base, Derived >();
associative_container_test< ptr_multimap<int, Value>, Value, Value >();
//associative_container_test< ptr_multimap<int, Base>, Base, Derived >();
//associative_container_test< ptr_multimap<int, Value>, Value, Value >();
string joe = "joe";
string brian = "brian";
@ -44,7 +45,19 @@ int test_main( int, char*[] )
catch( const bad_ptr_container_operation& )
{ }
return 0;
ptr_map<string,int> m2;
m2.insert( m2.begin(), *m.begin() );
BOOST_CHECK( m != m2 );
BOOST_CHECK( m2 < m );
m2.insert( m2.begin(), joe, new int(5) );
BOOST_CHECK( m != m2 );
BOOST_CHECK( m2 > m );
ptr_multimap<string,int> m3;
m3.insert( m3.begin(), *m.begin() );
BOOST_CHECK( m3.size() == 1u );
m3.insert( m3.begin(), brian, new int(11 ) );
BOOST_CHECK( m3.size() == 2u );
}
@ -56,7 +69,7 @@ test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_container_adapter ) );
test->add( BOOST_TEST_CASE( &test_ptr_map_adapter ) );
return test;
}

8
test/ptr_set.cpp Executable file → Normal file
View File

@ -41,11 +41,11 @@ void test_copy()
void test_set()
{
srand( 0 );
ptr_set_test< ptr_set<Base>, Base, Derived_class >();
ptr_set_test< ptr_set<Value>, Value, Value >();
ptr_set_test< ptr_set<Base>, Base, Derived_class, true >();
ptr_set_test< ptr_set<Value>, Value, Value, true >();
ptr_set_test< ptr_multiset<Base>, Base, Derived_class >();
ptr_set_test< ptr_multiset<Value>, Value, Value >();
ptr_set_test< ptr_multiset<Base>, Base, Derived_class, true >();
ptr_set_test< ptr_multiset<Value>, Value, Value, true >();
test_copy< ptr_set<Base>, ptr_set<Derived_class>,
Derived_class>();

406
test/ptr_unordered_map.cpp Normal file
View File

@ -0,0 +1,406 @@
//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2008. 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)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include "test_data.hpp"
#include <boost/test/unit_test.hpp>
#include <boost/ptr_container/exception.hpp>
#include <boost/range/sub_range.hpp>
#include <boost/cast.hpp>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <string>
//
// abstract base class definition
//
struct abstract_base
{
virtual ~abstract_base() {}
virtual void foo() = 0;
virtual abstract_base* clone() const = 0;
};
struct implementation : abstract_base
{
implementation()
{ }
implementation( const implementation& )
{ }
implementation( int, std::string, int, std::string )
{ }
virtual void foo() {}
virtual abstract_base* clone() const
{
return new implementation( *this );
}
};
inline std::ostream& operator<<( std::ostream& out, const abstract_base& r )
{
return out;
}
inline abstract_base* new_clone( const abstract_base& r )
{
return r.clone();
}
inline std::size_t hash_value( const abstract_base& b )
{
return boost::hash_value( &b );
}
//
// ptr_map test
//
template< typename C, typename B, typename T >
void ptr_map_test();
template< class Key >
Key get_next_key( const Key& k );
template<>
int get_next_key<int>( const int& )
{
return rand();
}
template<>
std::string get_next_key<std::string>( const std::string& )
{
return boost::lexical_cast<std::string>( rand() );
}
template< typename C, typename B, typename T >
void ptr_map_test()
{
using namespace boost;
BOOST_MESSAGE( "starting associative container test" );
enum { max_cnt = 10, size = 100 };
C c;
BOOST_CHECK( c.size() == 0 );
const C c2( c.begin(), c.end() );
BOOST_CHECK( c.size() == c2.size() );
C c3;
BOOST_MESSAGE( "finished construction test" );
BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator();
BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
BOOST_DEDUCED_TYPENAME C::const_iterator ci2 = c2.begin();
ci = c.cbegin();
ci = c.cend();
BOOST_DEDUCED_TYPENAME C::key_type a_key;
BOOST_MESSAGE( "finished iterator test" );
BOOST_DEDUCED_TYPENAME C::size_type s = c.size();
BOOST_DEDUCED_TYPENAME C::size_type s2 = c.max_size();
hide_warning(s2);
BOOST_CHECK_EQUAL( c.size(), s );
bool b = c.empty();
hide_warning(b);
BOOST_MESSAGE( "finished accessors test" );
a_key = get_next_key( a_key );
c.insert( a_key, new T );
a_key = get_next_key( a_key );
c.insert( a_key, new T );
c3.insert( c.begin(), c.end() );
c.insert( c3 );
c.erase( c.begin() );
BOOST_CHECK( c3.end() == c3.erase( boost::make_iterator_range(c3) ) );
c3.erase( a_key );
BOOST_CHECK( c3.empty() );
c.swap( c3 );
swap(c,c3);
swap(c3,c);
BOOST_CHECK( !c3.empty() );
c3.clear();
BOOST_CHECK( c3.empty() );
BOOST_MESSAGE( "finished modifiers test" );
a_key = get_next_key( a_key );
c.insert( a_key, new T );
a_key = get_next_key( a_key );
c.insert( a_key, std::auto_ptr<T>( new T ) );
typename C::auto_type ptr2 = c.release( c.begin() );
std::auto_ptr<C> ap = c.release();
c = c2.clone();
BOOST_MESSAGE( "finished release/clone test" );
a_key = get_next_key( a_key );
c3.insert( a_key, new T );
a_key = get_next_key( a_key );
c3.insert( a_key, new T );
c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c3 );
c. BOOST_NESTED_TEMPLATE transfer<C>( c3.begin(), c3.end(), c3 );
BOOST_CHECK( c3.empty() );
BOOST_CHECK( !c.empty() );
c3. BOOST_NESTED_TEMPLATE transfer<C>( c );
BOOST_CHECK( !c3.empty() );
BOOST_CHECK( c.empty() );
#ifdef BOOST_NO_SFINAE
#else
c. BOOST_NESTED_TEMPLATE transfer<C>( make_iterator_range(c3), c3 );
BOOST_CHECK( !c.empty() );
BOOST_CHECK( c3.empty() );
c3. BOOST_NESTED_TEMPLATE transfer<C>(c);
#endif
BOOST_MESSAGE( "finished transfer test" );
BOOST_CHECK( !c3.empty() );
c3.replace( c3.begin(), new T );
c3.replace( c3.begin(), std::auto_ptr<T>( new T ) );
BOOST_MESSAGE( "finished set/map interface test" );
// @todo: make macro with algorithms so that the right erase() is called.
// c.unique();
// c.unique( std::not_equal_to<T>() );
// c.remove( T() );
// c.remove_if( std::binder1st< std::equal_to<T> >( T() ) );
sub_range<C> sub;
sub_range<const C> csub;
i = c.find( get_next_key( a_key ) );
ci = c2.find( get_next_key( a_key ) );
c2.count( get_next_key( a_key ) );
sub = c.equal_range( get_next_key( a_key ) );
csub = c2.equal_range( get_next_key( a_key ) );
try
{
c.at( get_next_key( a_key ) );
}
catch( const bad_ptr_container_operation& )
{ }
try
{
c2.at( get_next_key( a_key ) );
}
catch( const bad_ptr_container_operation& )
{ }
BOOST_MESSAGE( "finished algorithms interface test" );
typename C::iterator it = c.begin(), e = c.end();
for( ; it != e; ++it )
{
std::cout << "\n mapped value = " << *it->second << " key = " << it->first;
//std::cout << "\n mapped value = " << it.value() << " key = " << it.key();
}
BOOST_MESSAGE( "finished iterator test" );
a_key = get_next_key( a_key );
c.insert( a_key, new T );
c.erase( a_key );
c.erase( a_key );
}
template< class CDerived, class CBase, class T >
void test_transfer()
{
CDerived from;
CBase to;
int key = get_next_key( key );
from.insert( key, new T );
key = get_next_key( key );
from.insert( key, new T );
transfer_test( from, to );
}
template< class BaseContainer, class DerivedContainer, class Derived >
void map_container_assignment_test()
{
DerivedContainer derived;
std::string foo( "foo" );
std::string bar( "foo" );
derived.insert( foo, new Derived );
derived.insert( bar, new Derived );
BaseContainer base_container( derived );
BOOST_CHECK_EQUAL( derived.size(), base_container.size() );
base_container.clear();
base_container = derived;
BOOST_CHECK_EQUAL( derived.size(), base_container.size() );
BaseContainer base2( base_container );
BOOST_CHECK_EQUAL( base2.size(), base_container.size() );
base2 = base_container;
BOOST_CHECK_EQUAL( base2.size(), base_container.size() );
base_container = base_container;
}
template< class Cont, class Key, class T >
void test_unordered_interface()
{
Cont c;
T* t = new T;
Key key = get_next_key( key );
c.insert( key, t );
typename Cont::local_iterator i = c.begin( 0 );
typename Cont::const_local_iterator ci = i;
ci = c.cbegin( 0 );
i = c.end( 0 );
ci = c.cend( 0 );
typename Cont::size_type s = c.bucket_count();
s = c.max_bucket_count();
s = c.bucket_size( 0 );
s = c.bucket( key );
float f = c.load_factor();
f = c.max_load_factor();
c.max_load_factor(f);
c.rehash(1000);
}
#include <boost/ptr_container/ptr_unordered_map.hpp>
using namespace std;
void test_map()
{
ptr_map_test< ptr_unordered_map<int, Base>, Base, Derived_class >();
ptr_map_test< ptr_unordered_map<int, Value>, Value, Value >();
ptr_map_test< ptr_unordered_map<int, nullable<Base> >, Base, Derived_class >();
ptr_map_test< ptr_unordered_map<int, nullable<Value> >, Value, Value >();
ptr_map_test< ptr_unordered_map<int, abstract_base>, abstract_base, implementation >();
ptr_map_test< ptr_unordered_multimap<int,Base>, Base, Derived_class >();
ptr_map_test< ptr_unordered_multimap<int,Value>, Value, Value >();
ptr_map_test< ptr_unordered_multimap<int, nullable<Base> >, Base, Derived_class >();
ptr_map_test< ptr_unordered_multimap<int, nullable<Value> >, Value, Value >();
map_container_assignment_test< ptr_unordered_map<std::string,Base>,
ptr_unordered_map<std::string,Derived_class>,
Derived_class>();
map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
ptr_unordered_map<std::string,Derived_class>,
Derived_class>();
map_container_assignment_test< ptr_unordered_map<std::string, nullable<Base> >,
ptr_unordered_map<std::string, nullable<Derived_class> >,
Derived_class>();
map_container_assignment_test< ptr_unordered_multimap<std::string,Base>,
ptr_unordered_multimap<std::string,Derived_class>,
Derived_class>();
map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
ptr_unordered_multimap<std::string,Derived_class>,
Derived_class>();
map_container_assignment_test< ptr_unordered_multimap<std::string, nullable<Base> >,
ptr_unordered_multimap<std::string, nullable<Derived_class> >,
Derived_class>();
test_transfer< ptr_unordered_map<int,Derived_class>, ptr_unordered_map<int,Base>, Derived_class >();
test_transfer< ptr_unordered_multimap<int,Derived_class>, ptr_unordered_multimap<int,Base>, Derived_class >();
string joe = "joe";
string brian = "brian";
string kenny = "kenny";
ptr_unordered_map<string,int> m;
m.insert( joe, new int( 4 ) );
m.insert( brian, new int( 6 ) );
BOOST_CHECK( m[ "foo" ] == 0 );
m[ "bar" ] += 5;
BOOST_CHECK( m[ "bar" ] == 5 );
m[ joe ] += 56;
m[ brian ] += 10;
BOOST_CHECK_THROW( (m.insert(kenny, 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( (m.replace(m.begin(), 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( (m.at("not there")), bad_ptr_container_operation );
for( ptr_unordered_map<string,int>::iterator i = m.begin();
i != m.end(); ++i )
{
if( is_null(i) )
BOOST_CHECK( false );
const string& ref = i->first;
hide_warning(ref);
int& ref2 = *(*i).second;
ref2++;
}
typedef ptr_unordered_map<string,Derived_class> map_type;
map_type m2;
m2.insert( joe, new Derived_class );
//
// This works fine since 'm2' is not const
//
m2.begin()->second->foo();
//
// These all return an implementation-defined proxy
// with two public members: 'first' and 'second'
//
map_type::value_type a_value = *m2.begin();
a_value.second->foo();
map_type::reference a_reference = *m2.begin();
a_reference.second->foo();
map_type::const_reference a_creference = *const_begin(m2);
//
//
// These will fail as iterators propagate constness
//
//a_creference.second->foo();
//a_cpointer->second->foo();
//const_begin(m2)->second->foo();
test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
test_unordered_interface< ptr_unordered_map<string,Base>, string, Derived_class >();
}
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_map ) );
return test;
}

106
test/ptr_unordered_set.cpp Normal file
View File

@ -0,0 +1,106 @@
//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2003-2005. 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)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include <boost/test/unit_test.hpp>
#include "associative_test_data.hpp"
#include <boost/ptr_container/ptr_unordered_set.hpp>
template< class SetDerived, class SetBase, class T >
void test_transfer()
{
SetBase to;
SetDerived from;
from.insert( new T );
from.insert( new T );
transfer_test( from, to );
}
template< class BaseContainer, class DerivedContainer, class Derived >
void test_copy()
{
DerivedContainer derived;
derived.insert( new Derived );
derived.insert( new Derived );
BaseContainer base( derived );
BOOST_CHECK_EQUAL( derived.size(), base.size() );
base.clear();
base = derived;
BOOST_CHECK_EQUAL( derived.size(), base.size() );
base = base;
}
template< class Cont, class T >
void test_unordered_interface()
{
Cont c;
T* t = new T;
c.insert( t );
typename Cont::local_iterator i = c.begin( 0 );
typename Cont::const_local_iterator ci = i;
ci = c.cbegin( 0 );
i = c.end( 0 );
ci = c.cend( 0 );
typename Cont::size_type s = c.bucket_count();
s = c.max_bucket_count();
s = c.bucket_size( 0 );
s = c.bucket( *t );
float f = c.load_factor();
f = c.max_load_factor();
c.max_load_factor(f);
c.rehash(1000);
}
void test_set()
{
srand( 0 );
ptr_set_test< ptr_unordered_set<Base>, Base, Derived_class, false >();
ptr_set_test< ptr_unordered_set<Value>, Value, Value, false >();
ptr_set_test< ptr_unordered_multiset<Base>, Base, Derived_class, false >();
ptr_set_test< ptr_unordered_multiset<Value>, Value, Value, false >();
test_copy< ptr_unordered_set<Base>, ptr_unordered_set<Derived_class>,
Derived_class>();
test_copy< ptr_unordered_multiset<Base>, ptr_unordered_multiset<Derived_class>,
Derived_class>();
test_transfer< ptr_unordered_set<Derived_class>, ptr_unordered_set<Base>, Derived_class>();
test_transfer< ptr_unordered_multiset<Derived_class>, ptr_unordered_multiset<Base>, Derived_class>();
ptr_unordered_set<int> set;
BOOST_CHECK_THROW( set.insert( 0 ), bad_ptr_container_operation );
set.insert( new int(0) );
set.insert( std::auto_ptr<int>( new int(1) ) );
BOOST_CHECK_THROW( (set.replace(set.begin(), 0 )), bad_ptr_container_operation );
BOOST_CHECK_THROW( (set.replace(set.begin(), std::auto_ptr<int>(0) )), bad_ptr_container_operation );
test_unordered_interface< ptr_unordered_set<Base>, Derived_class >();
test_unordered_interface< ptr_unordered_multiset<Base>, Derived_class >();
}
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &test_set ) );
return test;
}

28
test/sequence_test_data.hpp Executable file → Normal file
View File

@ -27,20 +27,25 @@ void reversible_container_test()
BOOST_MESSAGE( "starting reversible container test" );
enum { max_cnt = 10, size = 100 };
C c;
set_capacity<C>()( c );
BOOST_CHECK( c.size() == 0 );
c.push_back( new T );
BOOST_CHECK( c.size() == 1 );
const C c2_dummy( c.begin(), c.end() );
BOOST_CHECK_EQUAL( c2_dummy.size(), c.size() );
const C c2( c.clone() );
BOOST_CHECK( c2.size() == c.size() );
BOOST_CHECK_EQUAL( c2.size(), c.size() );
C c3( c.begin(), c.end() );
BOOST_CHECK( c.size() == c3.size() );
set_capacity<C>()( c3 );
BOOST_CHECK_EQUAL( c.size(), c3.size() );
c.assign( c3.begin(), c3.end() );
BOOST_CHECK( c.size() == c3.size() );
BOOST_CHECK_EQUAL( c.size(), c3.size() );
c.assign( c3 );
set_capacity<C>()( c );
BOOST_MESSAGE( "finished construction test" );
C a_copy( c );
@ -50,10 +55,11 @@ void reversible_container_test()
a_copy.clear();
a_copy = a_copy;
BOOST_CHECK( a_copy.empty() );
BOOST_CHECK( !c.empty() );
BOOST_MESSAGE( "finished copying test" );
BOOST_DEDUCED_TYPENAME C::allocator_type alloc = c.get_allocator();
hide_warning(alloc);
hide_warning(alloc);
BOOST_DEDUCED_TYPENAME C::iterator i = c.begin();
BOOST_DEDUCED_TYPENAME C::const_iterator ci = c2.begin();
BOOST_DEDUCED_TYPENAME C::iterator i2 = c.end();
@ -92,10 +98,13 @@ void reversible_container_test()
BOOST_MESSAGE( "finished accessors test" );
c.push_back( new T );
BOOST_CHECK_EQUAL( c.size(), 4u );
c.pop_back();
BOOST_CHECK( !c.empty() );
c.insert( c.end(), new T );
c.insert( c.end(), std::auto_ptr<T>( new T ) );
BOOST_CHECK_EQUAL( c.size(), 5u );
#if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
#else
@ -106,6 +115,7 @@ void reversible_container_test()
c3.erase( c3.begin(), c3.end() );
c3.erase( boost::make_iterator_range(c3) );
BOOST_CHECK( c3.empty() );
BOOST_CHECK( !c.empty() );
c.swap( c3 );
BOOST_CHECK( !c3.empty() );
c3.clear();
@ -163,12 +173,17 @@ void reversible_container_test()
template< class CDerived, class CBase, class T >
void test_transfer()
{
BOOST_MESSAGE( "starting transfer test" );
CDerived from;
CBase to;
set_capacity<CDerived>()( from );
set_capacity<CBase>()( to );
from.push_back( new T );
from.push_back( new T );
to. BOOST_NESTED_TEMPLATE transfer<CDerived>( to.end(), from );
BOOST_MESSAGE( "finished transfer test" );
}
@ -220,7 +235,10 @@ struct equal_to_int
template< class IntContainer >
void random_access_algorithms_test()
{
BOOST_MESSAGE( "starting random accessors algorithms test" );
IntContainer c;
set_capacity<IntContainer>()( c );
assign::push_back( c )
( new int(1) )
( new int(3) )
@ -265,6 +283,7 @@ void random_access_algorithms_test()
// C = [0,2,3,6]
IntContainer c2;
set_capacity<IntContainer>()( c2 );
assign::push_back( c2 )
( new int(-1) )
( new int(1) )
@ -276,5 +295,6 @@ void random_access_algorithms_test()
BOOST_CHECK( c2.empty() );
BOOST_CHECK( c.size() == 9u );
BOOST_CHECK( is_sorted< std::less_equal<int> >( c ) );
BOOST_MESSAGE( "finished random accessors algorithms test" );
}

99
test/serialization.cpp Executable file → Normal file
View File

@ -19,6 +19,7 @@
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/functional/hash.hpp>
#include <boost/ptr_container/ptr_container.hpp>
#include <boost/ptr_container/serialize_ptr_container.hpp>
#include <boost/serialization/export.hpp>
@ -37,6 +38,25 @@ inline T const& as_const( T const& r )
return r;
}
//
// used to customize tests for circular_buffer
//
template< class Cont >
struct set_capacity
{
void operator()( Cont& ) const
{ }
};
template<class T>
struct set_capacity< boost::ptr_circular_buffer<T> >
{
void operator()( boost::ptr_circular_buffer<T>& c ) const
{
c.set_capacity( 100u );
}
};
//
// class hierarchy
//
@ -48,7 +68,7 @@ struct Base
template< class Archive >
void serialize( Archive& ar, const unsigned int version )
void serialize( Archive& ar, const unsigned int /*version*/ )
{
ar & boost::serialization::make_nvp( "i", i );
}
@ -68,12 +88,22 @@ inline bool operator<( const Base& l, const Base& r )
return l.i < r.i;
}
inline bool operator==( const Base& l, const Base& r )
{
return l.i == r.i;
}
inline std::size_t hash_value( const Base& b )
{
return boost::hash_value( b.i );
}
struct Derived : Base
{
int i2;
template< class Archive >
void serialize( Archive& ar, const unsigned int version )
void serialize( Archive& ar, const unsigned int /*version*/ )
{
ar & boost::serialization::make_nvp( "Base",
boost::serialization::base_object<Base>( *this ) );
@ -95,7 +125,7 @@ BOOST_CLASS_EXPORT_GUID( Derived, "Derived" )
//
template< class C, class T >
void add( C& c, T* r, unsigned n )
void add( C& c, T* r, unsigned /*n*/ )
{
c.insert( c.end(), r );
}
@ -110,8 +140,10 @@ template< class Cont, class OArchive, class IArchive >
void test_serialization_helper()
{
Cont vec;
set_capacity<Cont>()( vec );
add( vec, new Base( -1 ), 0u );
add( vec, new Derived( 1 ), 1u );
BOOST_CHECK_EQUAL( vec.size(), 2u );
std::ofstream ofs("filename");
OArchive oa(ofs);
@ -127,15 +159,41 @@ void test_serialization_helper()
BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
BOOST_CHECK_EQUAL( (*vec2.begin()).i, -1 );
BOOST_CHECK_EQUAL( (*--vec2.end()).i, 0 );
BOOST_CHECK_EQUAL( (*++vec2.begin()).i, 0 );
typename Cont::iterator i = vec2.end();
--i;
typename Cont::iterator i = vec2.begin();
++i;
Derived* d = dynamic_cast<Derived*>( &*i );
BOOST_CHECK_EQUAL( d->i2, 1 );
}
template< class Cont, class OArchive, class IArchive >
void test_serialization_unordered_set_helper()
{
Cont vec;
set_capacity<Cont>()( vec );
add( vec, new Base( -1 ), 0u );
add( vec, new Derived( 1 ), 1u );
BOOST_CHECK_EQUAL( vec.size(), 2u );
std::ofstream ofs("filename");
OArchive oa(ofs);
oa << boost::serialization::make_nvp( "container", as_const(vec) );
ofs.close();
std::ifstream ifs("filename", std::ios::binary);
IArchive ia(ifs);
Cont vec2;
ia >> boost::serialization::make_nvp( "container", vec2 );
ifs.close();
BOOST_CHECK_EQUAL( vec.size(), vec2.size() );
BOOST_CHECK_EQUAL( (*vec2.begin()).i, 0 );
BOOST_CHECK_EQUAL( (*++vec2.begin()).i, -1 );
}
template< class Map, class OArchive, class IArchive >
void test_serialization_map_helper()
{
@ -209,6 +267,12 @@ void test_serialization()
test_serialization_helper< boost::ptr_vector<Base>,
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
test_serialization_helper< boost::ptr_circular_buffer<Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_helper< boost::ptr_circular_buffer<Base>,
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
test_serialization_helper< boost::ptr_array<Base,2>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
@ -218,7 +282,14 @@ void test_serialization()
test_serialization_helper< boost::ptr_multiset<Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_unordered_set_helper< boost::ptr_unordered_set<Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_unordered_set_helper<boost::ptr_unordered_multiset<Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_map_helper< boost::ptr_map<std::string,Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
@ -233,6 +304,20 @@ void test_serialization()
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
boost::archive::text_oarchive,
boost::archive::text_iarchive>();
test_serialization_map_helper< boost::ptr_unordered_map<std::string,Base>,
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
test_serialization_map_helper< boost::ptr_unordered_multimap<std::string,Base>,
boost::archive::xml_oarchive,
boost::archive::xml_iarchive>();
}

61
test/test_data.hpp Executable file → Normal file
View File

@ -14,6 +14,7 @@
#include <boost/config.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/functional/hash.hpp>
#include <algorithm>
#include <iostream>
#include <string>
@ -39,6 +40,8 @@ class Base
}
Base& operator=( const Base& );
public: // for test reasons only
int data1, data2, data3;
string data;
@ -67,7 +70,7 @@ public:
Base* clone() const { return do_clone(); }
void foo() { do_foo(); }
virtual bool less_than( const Base& b ) const
virtual bool less_than( const Base& /*b*/ ) const
{
return true;
}
@ -92,9 +95,9 @@ public:
#endif
private:
virtual void do_print( ostream& out ) const { };
virtual Base* do_clone() const { return new Base( *this ); };
virtual void do_foo() { };
virtual void do_print( ostream& /*out*/ ) const { };
virtual Base* do_clone() const { return new Base( *this ); };
virtual void do_foo() { };
};
#ifdef PTR_CONTAINER_DEBUG
@ -149,9 +152,23 @@ inline bool operator!=( const Base& l, const Base& r )
inline std::size_t hash_value( const Base& b )
{
std::size_t seed = 0;
boost::hash_combine( seed, b.data );
boost::hash_combine( seed, b.data1 );
boost::hash_combine( seed, b.data2 );
boost::hash_combine( seed, b.data3 );
return seed;
}
class Derived_class : public Base
{
public: // for test reasons only
int i_;
private:
virtual void do_print( ostream& out ) const
{
@ -180,12 +197,22 @@ public:
}
};
inline std::size_t hash_value( const Derived_class& b )
{
std::size_t seed = hash_value( static_cast<const Base&>( b ) );
boost::hash_combine( seed, b.i_ );
return seed;
}
//////////////////////////////////////////////////////////////////////////////
// Test class 2: a value class
//////////////////////////////////////////////////////////////////////////////
class Value
{
public: // for test reasons only
string s_;
public:
@ -236,13 +263,30 @@ inline ostream& operator<<( ostream& out, const Value& v )
return out << v.name() << " ";
}
inline std::size_t hash_value( const Value& v )
{
return boost::hash_value( v.s_ );
}
//
// used to hide "unused variable" warnings
//
template< class T >
inline void hide_warning( T& r )
inline void hide_warning( T& /*r*/ )
{ }
//
// used to customize tests for circular_buffer
//
template< class Cont >
struct set_capacity
{
void operator()( Cont& ) const
{ }
};
//
// transfer() test
//
@ -250,9 +294,11 @@ inline void hide_warning( T& r )
template< class Cont1, class Cont2 >
void transfer_test( Cont1& from, Cont2& to )
{
BOOST_MESSAGE( "starting container transfer test" );
BOOST_CHECK( !from.empty() );
to. BOOST_NESTED_TEMPLATE transfer<Cont1>( from );
BOOST_CHECK( !to.empty() );
BOOST_MESSAGE( "finishing container transfer test" );
}
@ -263,7 +309,10 @@ void transfer_test( Cont1& from, Cont2& to )
template< class BaseContainer, class DerivedContainer, class Derived >
void container_assignment_test()
{
BOOST_MESSAGE( "starting container assignment test" );
DerivedContainer derived;
set_capacity<DerivedContainer>()( derived );
derived.insert( derived.begin(), new Derived );
derived.insert( derived.begin(), new Derived );
@ -277,6 +326,8 @@ void container_assignment_test()
base2 = base;
BOOST_CHECK_EQUAL( base2.size(), base.size() );
base = base;
BOOST_MESSAGE( "finished container assignment test" );
}