1 // allocate_shared_alloc11_test.cpp
3 // allocate_shared with a minimal C++11 allocator
5 // Copyright 2007-2009, 2014 Peter Dimov
7 // Distributed under the Boost Software License, Version 1.0.
8 // See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/make_shared.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/weak_ptr.hpp>
15 #include <boost/config.hpp>
18 #if !defined( BOOST_NO_CXX11_ALLOCATOR )
20 template< class T > class cxx11_allocator
30 template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
34 T * allocate( std::size_t n )
36 return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
39 void deallocate( T * p, std::size_t n )
41 ::operator delete( p );
50 X & operator=( X const & );
52 void * operator new( std::size_t n )
54 BOOST_ERROR( "private X::new called" );
55 return ::operator new( n );
58 void operator delete( void * p )
60 BOOST_ERROR( "private X::delete called" );
61 ::operator delete( p );
70 explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0, int a5 = 0, int a6 = 0, int a7 = 0, int a8 = 0, int a9 = 0 ): v( a1+a2+a3+a4+a5+a6+a7+a8+a9 )
86 boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>() );
88 BOOST_TEST( pi.get() != 0 );
89 BOOST_TEST( *pi == 0 );
93 boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>(), 5 );
95 BOOST_TEST( pi.get() != 0 );
96 BOOST_TEST( *pi == 5 );
99 BOOST_TEST( X::instances == 0 );
102 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>() );
103 boost::weak_ptr<X> wp( pi );
105 BOOST_TEST( X::instances == 1 );
106 BOOST_TEST( pi.get() != 0 );
107 BOOST_TEST( pi->v == 0 );
111 BOOST_TEST( X::instances == 0 );
115 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1 );
116 boost::weak_ptr<X> wp( pi );
118 BOOST_TEST( X::instances == 1 );
119 BOOST_TEST( pi.get() != 0 );
120 BOOST_TEST( pi->v == 1 );
124 BOOST_TEST( X::instances == 0 );
128 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2 );
129 boost::weak_ptr<X> wp( pi );
131 BOOST_TEST( X::instances == 1 );
132 BOOST_TEST( pi.get() != 0 );
133 BOOST_TEST( pi->v == 1+2 );
137 BOOST_TEST( X::instances == 0 );
141 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3 );
142 boost::weak_ptr<X> wp( pi );
144 BOOST_TEST( X::instances == 1 );
145 BOOST_TEST( pi.get() != 0 );
146 BOOST_TEST( pi->v == 1+2+3 );
150 BOOST_TEST( X::instances == 0 );
154 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4 );
155 boost::weak_ptr<X> wp( pi );
157 BOOST_TEST( X::instances == 1 );
158 BOOST_TEST( pi.get() != 0 );
159 BOOST_TEST( pi->v == 1+2+3+4 );
163 BOOST_TEST( X::instances == 0 );
167 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5 );
168 boost::weak_ptr<X> wp( pi );
170 BOOST_TEST( X::instances == 1 );
171 BOOST_TEST( pi.get() != 0 );
172 BOOST_TEST( pi->v == 1+2+3+4+5 );
176 BOOST_TEST( X::instances == 0 );
180 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6 );
181 boost::weak_ptr<X> wp( pi );
183 BOOST_TEST( X::instances == 1 );
184 BOOST_TEST( pi.get() != 0 );
185 BOOST_TEST( pi->v == 1+2+3+4+5+6 );
189 BOOST_TEST( X::instances == 0 );
193 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
194 boost::weak_ptr<X> wp( pi );
196 BOOST_TEST( X::instances == 1 );
197 BOOST_TEST( pi.get() != 0 );
198 BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
202 BOOST_TEST( X::instances == 0 );
206 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
207 boost::weak_ptr<X> wp( pi );
209 BOOST_TEST( X::instances == 1 );
210 BOOST_TEST( pi.get() != 0 );
211 BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
215 BOOST_TEST( X::instances == 0 );
219 boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
220 boost::weak_ptr<X> wp( pi );
222 BOOST_TEST( X::instances == 1 );
223 BOOST_TEST( pi.get() != 0 );
224 BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
228 BOOST_TEST( X::instances == 0 );
231 return boost::report_errors();
234 #else // !defined( BOOST_NO_CXX11_ALLOCATOR )