123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- //
- // lsp_array_test.cpp
- //
- // Copyright 2012, 2017 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 <boost/smart_ptr/local_shared_ptr.hpp>
- #include <boost/weak_ptr.hpp>
- #include <boost/enable_shared_from_this.hpp>
- #include <boost/detail/lightweight_test.hpp>
- #include <memory>
- #include <utility>
- class X: public boost::enable_shared_from_this< X >
- {
- public:
- static int allocations;
- static int instances;
- X()
- {
- ++instances;
- }
- ~X()
- {
- --instances;
- }
- void* operator new[]( std::size_t n )
- {
- ++allocations;
- return ::operator new[]( n );
- }
- void operator delete[]( void* p )
- {
- --allocations;
- ::operator delete[]( p );
- }
- private:
- X( X const& );
- X& operator=( X const& );
- };
- int X::allocations = 0;
- int X::instances = 0;
- template< class T> class array_deleter
- {
- public:
- static int calls;
- void operator()( T * p ) const
- {
- ++calls;
- delete[] p;
- }
- private:
- template< class Y > void operator()( Y * p ) const;
- };
- template< class T > int array_deleter< T >::calls = 0;
- int main()
- {
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- {
- boost::local_shared_ptr<X[]> px;
- BOOST_TEST( !px );
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- boost::local_shared_ptr<X[]> px2( new X[ 3 ] );
- BOOST_TEST( px2 );
- try
- {
- px2[0].shared_from_this();
- BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
- }
- catch( boost::bad_weak_ptr const& )
- {
- }
- catch( ... )
- {
- BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
- }
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 3 );
- {
- X & rx = px2[ 0 ];
- BOOST_TEST( &rx == px2.get() );
- }
- boost::local_shared_ptr<X const[]> px3( px2 );
- BOOST_TEST( px3 == px2 );
- BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
- {
- X const & rx = px3[ 1 ];
- BOOST_TEST( &rx == px3.get() + 1 );
- }
- px3.reset();
- px3 = px2;
- BOOST_TEST( px3 == px2 );
- BOOST_TEST( !( px2 < px3 ) && !( px3 < px2 ) );
- boost::local_shared_ptr<X volatile[]> px4( px2 );
- BOOST_TEST( px4 == px2 );
- BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
- {
- X volatile & rx = px4[ 2 ];
- BOOST_TEST( &rx == px4.get() + 2 );
- }
- px4.reset();
- px4 = px2;
- BOOST_TEST( px4 == px2 );
- BOOST_TEST( !( px2 < px4 ) && !( px4 < px2 ) );
- boost::local_shared_ptr<void> px5( px2 );
- BOOST_TEST( px5 == px2 );
- BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
- px5.reset();
- px5 = px2;
- BOOST_TEST( px5 == px2 );
- BOOST_TEST( !( px2 < px5 ) && !( px5 < px2 ) );
- boost::weak_ptr<X[]> wp( px );
- BOOST_TEST( wp.lock() == px );
- boost::weak_ptr<X[]> wp2( px2 );
- BOOST_TEST( wp2.lock() == px2 );
- wp2.reset();
- wp2 = px2;
- BOOST_TEST( wp2.lock() == px2 );
- boost::weak_ptr<X const[]> wp3( px2 );
- BOOST_TEST( wp3.lock() == px2 );
- wp3.reset();
- wp3 = px2;
- BOOST_TEST( wp3.lock() == px2 );
- boost::weak_ptr<X volatile[]> wp4( px2 );
- BOOST_TEST( wp4.lock() == px2 );
- wp4.reset();
- wp4 = px2;
- BOOST_TEST( wp4.lock() == px2 );
- boost::weak_ptr<void> wp5( px2 );
- BOOST_TEST( wp5.lock() == px2 );
- wp5.reset();
- wp5 = px2;
- BOOST_TEST( wp5.lock() == px2 );
- px2.reset();
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 3 );
- px3.reset();
- px4.reset();
- px5.reset();
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- BOOST_TEST( wp2.lock() == 0 );
- BOOST_TEST( wp3.lock() == 0 );
- BOOST_TEST( wp4.lock() == 0 );
- BOOST_TEST( wp5.lock() == 0 );
- }
- #if !defined( BOOST_NO_CXX11_SMART_PTR ) && !defined( BOOST_NO_CXX11_RVALUE_REFERENCES )
- {
- std::unique_ptr<X[]> px( new X[ 4 ] );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 4 );
- boost::local_shared_ptr<X[]> px2( std::move( px ) );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 4 );
- BOOST_TEST( px.get() == 0 );
- try
- {
- px2[0].shared_from_this();
- BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
- }
- catch( boost::bad_weak_ptr const& )
- {
- }
- catch( ... )
- {
- BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
- }
- px2.reset();
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- }
- {
- std::unique_ptr<X[]> px( new X[ 4 ] );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 4 );
- boost::local_shared_ptr<X[]> px2;
- px2 = std::move( px );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 4 );
- BOOST_TEST( px.get() == 0 );
- try
- {
- px2[0].shared_from_this();
- BOOST_ERROR( "px2[0].shared_from_this() failed to throw" );
- }
- catch( boost::bad_weak_ptr const& )
- {
- }
- catch( ... )
- {
- BOOST_ERROR( "px2[0].shared_from_this() threw something else than bad_weak_ptr" );
- }
- px2.reset();
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- }
- #endif
- {
- boost::local_shared_ptr<X[]> px( new X[ 5 ], array_deleter< X >() );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 5 );
- try
- {
- px[0].shared_from_this();
- BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
- }
- catch( boost::bad_weak_ptr const& )
- {
- }
- catch( ... )
- {
- BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
- }
- px.reset();
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- BOOST_TEST( array_deleter< X >::calls == 1 );
- }
- {
- boost::local_shared_ptr<X[]> px( new X[ 6 ], array_deleter< X >(), std::allocator< X >() );
- BOOST_TEST( X::allocations == 1 );
- BOOST_TEST( X::instances == 6 );
- try
- {
- px[0].shared_from_this();
- BOOST_ERROR( "px[0].shared_from_this() failed to throw" );
- }
- catch( boost::bad_weak_ptr const& )
- {
- }
- catch( ... )
- {
- BOOST_ERROR( "px[0].shared_from_this() threw something else than bad_weak_ptr" );
- }
- px.reset();
- BOOST_TEST( X::allocations == 0 );
- BOOST_TEST( X::instances == 0 );
- BOOST_TEST( array_deleter< X >::calls == 2 );
- }
- return boost::report_errors();
- }
|