1 #include <boost/config.hpp>
3 // shared_ptr_alloc2_test.cpp
5 // Copyright (c) 2005 Peter Dimov
7 // Distributed under the Boost Software License, Version 1.0. (See
8 // accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
12 #include <boost/detail/lightweight_test.hpp>
13 #include <boost/shared_ptr.hpp>
19 struct test_allocator_base
23 static int last_global_id_;
26 explicit test_allocator_base( int id ): id_( id )
31 int test_allocator_base::last_global_id_ = 0;
32 int test_allocator_base::count_ = 0;
34 template<class T> class test_allocator: public test_allocator_base
39 typedef T const * const_pointer;
40 typedef T & reference;
41 typedef T const & const_reference;
43 typedef std::size_t size_type;
44 typedef std::ptrdiff_t difference_type;
48 static T * last_pointer_;
49 static std::size_t last_n_;
54 template<class U> struct rebind
56 typedef test_allocator<U> other;
59 pointer address( reference r ) const
64 const_pointer address( const_reference s ) const
69 explicit test_allocator( int id = 0 ): test_allocator_base( id )
73 template<class U> test_allocator( test_allocator<U> const & r ): test_allocator_base( r )
77 template<class U> test_allocator & operator=( test_allocator<U> const & r )
79 test_allocator_base::operator=( r );
83 void deallocate( pointer p, size_type n )
85 BOOST_TEST( p == last_pointer_ );
86 BOOST_TEST( n == last_n_ );
87 BOOST_TEST( id_ == last_id_ );
91 ::operator delete( p );
94 pointer allocate( size_type n, void const * )
96 T * p = static_cast< T* >( ::operator new( n * sizeof( T ) ) );
102 last_global_id_ = id_;
108 void construct( pointer p, T const & t )
113 void destroy( pointer p )
118 size_type max_size() const
120 return size_type( -1 ) / sizeof( T );
124 template<class T> T * test_allocator<T>::last_pointer_ = 0;
125 template<class T> std::size_t test_allocator<T>::last_n_ = 0;
126 template<class T> int test_allocator<T>::last_id_ = 0;
128 template<class T, class U> inline bool operator==( test_allocator<T> const & a1, test_allocator<U> const & a2 )
130 return a1.id_ == a2.id_;
133 template<class T, class U> inline bool operator!=( test_allocator<T> const & a1, test_allocator<U> const & a2 )
135 return a1.id_ != a2.id_;
138 template<> class test_allocator<void>: public test_allocator_base
142 typedef void * pointer;
143 typedef void const * const_pointer;
144 typedef void value_type;
146 template<class U> struct rebind
148 typedef test_allocator<U> other;
151 explicit test_allocator( int id = 0 ): test_allocator_base( id )
155 template<class U> test_allocator( test_allocator<U> const & r ): test_allocator_base( r )
159 template<class U> test_allocator & operator=( test_allocator<U> const & r )
161 test_allocator_base::operator=( r );
170 static int instances;
185 X & operator=( X const & );
188 int X::instances = 0;
192 BOOST_TEST( X::instances == 0 );
194 boost::shared_ptr<void> pv( new X, boost::checked_deleter<X>(), std::allocator<X>() );
196 BOOST_TEST( X::instances == 1 );
198 pv.reset( new X, boost::checked_deleter<X>(), test_allocator<float>( 42 ) );
200 BOOST_TEST( X::instances == 1 );
202 BOOST_TEST( test_allocator_base::last_global_id_ == 42 );
203 BOOST_TEST( test_allocator_base::count_ > 0 );
207 BOOST_TEST( X::instances == 0 );
208 BOOST_TEST( test_allocator_base::count_ == 0 );
210 pv.reset( new X, boost::checked_deleter<X>(), test_allocator<void>( 43 ) );
212 BOOST_TEST( X::instances == 1 );
213 BOOST_TEST( test_allocator_base::last_global_id_ == 43 );
215 pv.reset( new X, boost::checked_deleter<X>(), std::allocator<void>() );
217 BOOST_TEST( X::instances == 1 );
221 BOOST_TEST( X::instances == 0 );
223 return boost::report_errors();