1 // make_shared_test.cpp
3 // Copyright 2007-2009 Peter Dimov
5 // Distributed under the Boost Software License, Version 1.0.
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
9 #include <boost/detail/lightweight_test.hpp>
10 #include <boost/make_shared.hpp>
11 #include <boost/shared_ptr.hpp>
12 #include <boost/weak_ptr.hpp>
20 X & operator=( X const & );
22 void * operator new( std::size_t n )
24 // lack of this definition causes link errors on Comeau C++
25 BOOST_ERROR( "private X::new called" );
26 return ::operator new( n );
29 void operator delete( void * p )
31 // lack of this definition causes link errors on MSVC
32 BOOST_ERROR( "private X::delete called" );
33 ::operator delete( p );
42 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 )
58 boost::shared_ptr< int > pi = boost::make_shared< int >();
60 BOOST_TEST( pi.get() != 0 );
61 BOOST_TEST( *pi == 0 );
65 boost::shared_ptr< int > pi = boost::make_shared< int >( 5 );
67 BOOST_TEST( pi.get() != 0 );
68 BOOST_TEST( *pi == 5 );
71 BOOST_TEST( X::instances == 0 );
74 boost::shared_ptr< X > pi = boost::make_shared< X >();
75 boost::weak_ptr<X> wp( pi );
77 BOOST_TEST( X::instances == 1 );
78 BOOST_TEST( pi.get() != 0 );
79 BOOST_TEST( pi->v == 0 );
83 BOOST_TEST( X::instances == 0 );
87 boost::shared_ptr< X > pi = boost::make_shared< X >( 1 );
88 boost::weak_ptr<X> wp( pi );
90 BOOST_TEST( X::instances == 1 );
91 BOOST_TEST( pi.get() != 0 );
92 BOOST_TEST( pi->v == 1 );
96 BOOST_TEST( X::instances == 0 );
100 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2 );
101 boost::weak_ptr<X> wp( pi );
103 BOOST_TEST( X::instances == 1 );
104 BOOST_TEST( pi.get() != 0 );
105 BOOST_TEST( pi->v == 1+2 );
109 BOOST_TEST( X::instances == 0 );
113 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3 );
114 boost::weak_ptr<X> wp( pi );
116 BOOST_TEST( X::instances == 1 );
117 BOOST_TEST( pi.get() != 0 );
118 BOOST_TEST( pi->v == 1+2+3 );
122 BOOST_TEST( X::instances == 0 );
126 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4 );
127 boost::weak_ptr<X> wp( pi );
129 BOOST_TEST( X::instances == 1 );
130 BOOST_TEST( pi.get() != 0 );
131 BOOST_TEST( pi->v == 1+2+3+4 );
135 BOOST_TEST( X::instances == 0 );
139 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4, 5 );
140 boost::weak_ptr<X> wp( pi );
142 BOOST_TEST( X::instances == 1 );
143 BOOST_TEST( pi.get() != 0 );
144 BOOST_TEST( pi->v == 1+2+3+4+5 );
148 BOOST_TEST( X::instances == 0 );
152 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4, 5, 6 );
153 boost::weak_ptr<X> wp( pi );
155 BOOST_TEST( X::instances == 1 );
156 BOOST_TEST( pi.get() != 0 );
157 BOOST_TEST( pi->v == 1+2+3+4+5+6 );
161 BOOST_TEST( X::instances == 0 );
165 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7 );
166 boost::weak_ptr<X> wp( pi );
168 BOOST_TEST( X::instances == 1 );
169 BOOST_TEST( pi.get() != 0 );
170 BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
174 BOOST_TEST( X::instances == 0 );
178 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8 );
179 boost::weak_ptr<X> wp( pi );
181 BOOST_TEST( X::instances == 1 );
182 BOOST_TEST( pi.get() != 0 );
183 BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
187 BOOST_TEST( X::instances == 0 );
191 boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
192 boost::weak_ptr<X> wp( pi );
194 BOOST_TEST( X::instances == 1 );
195 BOOST_TEST( pi.get() != 0 );
196 BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
200 BOOST_TEST( X::instances == 0 );
203 return boost::report_errors();