// // make_shared_test.cpp // // Copyright (c) 2007 Peter Dimov // // 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) // #include #include #include #include class X { private: X( X const & ); X & operator=( X const & ); public: static int instances; int v; explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0 ): v( a1+a2+a3+a4 ) { ++instances; } ~X() { --instances; } }; int X::instances = 0; int main() { { boost::shared_ptr< int > pi = boost::make_shared< int >(); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( *pi == 0 ); } { boost::shared_ptr< int > pi = boost::allocate_shared< int >( std::allocator() ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( *pi == 0 ); } { boost::shared_ptr< int > pi = boost::make_shared< int >( 5 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( *pi == 5 ); } { boost::shared_ptr< int > pi = boost::allocate_shared< int >( std::allocator(), 5 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( *pi == 5 ); } BOOST_TEST( X::instances == 0 ); { boost::shared_ptr< X > pi = boost::make_shared< X >(); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 0 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator() ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 0 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::make_shared< X >( 1 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator(), 1 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator(), 1, 2 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2+3 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator(), 1, 2, 3 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2+3 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::make_shared< X >( 1, 2, 3, 4 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2+3+4 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } { boost::shared_ptr< X > pi = boost::allocate_shared< X >( std::allocator(), 1, 2, 3, 4 ); boost::weak_ptr wp( pi ); BOOST_TEST( X::instances == 1 ); BOOST_TEST( pi.get() != 0 ); BOOST_TEST( pi->v == 1+2+3+4 ); pi.reset(); BOOST_TEST( X::instances == 0 ); } return boost::report_errors(); }