mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-29 20:37:13 +02:00
Add lsp_array_* tests
This commit is contained in:
@ -57,6 +57,8 @@ public:
|
||||
|
||||
virtual void destroy() BOOST_SP_NOEXCEPT = 0;
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT = 0;
|
||||
|
||||
void add_ref() BOOST_SP_NOEXCEPT
|
||||
{
|
||||
#if defined( __has_builtin )
|
||||
@ -114,6 +116,11 @@ public:
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return const_pointer_cast<void>( pn_ );
|
||||
}
|
||||
};
|
||||
|
||||
class local_counted_impl_em: public local_counted_base
|
||||
@ -126,6 +133,11 @@ public:
|
||||
{
|
||||
pn_.reset();
|
||||
}
|
||||
|
||||
virtual boost::shared_ptr<void> get_shared_ptr() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return const_pointer_cast<void>( pn_ );
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
@ -437,6 +437,36 @@ public:
|
||||
return pn? pn->local_use_count(): 0;
|
||||
}
|
||||
|
||||
// conversions to shared_ptr, weak_ptr
|
||||
|
||||
template<class Y> operator shared_ptr<Y>() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
boost::detail::sp_assert_convertible<T, Y>();
|
||||
|
||||
if( pn )
|
||||
{
|
||||
return static_pointer_cast<Y>( pn->get_shared_ptr() );
|
||||
}
|
||||
else
|
||||
{
|
||||
return shared_ptr<Y>();
|
||||
}
|
||||
}
|
||||
|
||||
template<class Y> operator weak_ptr<Y>() const BOOST_SP_NOEXCEPT
|
||||
{
|
||||
boost::detail::sp_assert_convertible<T, Y>();
|
||||
|
||||
if( pn )
|
||||
{
|
||||
return static_pointer_cast<Y>( pn->get_shared_ptr() );
|
||||
}
|
||||
else
|
||||
{
|
||||
return weak_ptr<Y>();
|
||||
}
|
||||
}
|
||||
|
||||
// swap
|
||||
|
||||
void swap( local_shared_ptr & r ) BOOST_SP_NOEXCEPT
|
||||
@ -487,6 +517,31 @@ template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, local_sha
|
||||
|
||||
#endif
|
||||
|
||||
template<class T, class U> inline bool operator==( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.get() == b.get();
|
||||
}
|
||||
|
||||
template<class T, class U> inline bool operator!=( local_shared_ptr<T> const & a, shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.get() != b.get();
|
||||
}
|
||||
|
||||
template<class T, class U> inline bool operator==( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.get() == b.get();
|
||||
}
|
||||
|
||||
template<class T, class U> inline bool operator!=( shared_ptr<T> const & a, local_shared_ptr<U> const & b ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.get() != b.get();
|
||||
}
|
||||
|
||||
template<class T, class U> inline bool operator<(local_shared_ptr<T> const & a, local_shared_ptr<U> const & b) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
return a.owner_before( b );
|
||||
}
|
||||
|
||||
template<class T> inline void swap( local_shared_ptr<T> & a, local_shared_ptr<T> & b ) BOOST_SP_NOEXCEPT
|
||||
{
|
||||
a.swap( b );
|
||||
|
@ -212,5 +212,9 @@ import testing ;
|
||||
[ compile lwm_win32_cs_test.cpp ]
|
||||
|
||||
[ run local_sp_test.cpp ]
|
||||
[ run lsp_array_test.cpp ]
|
||||
[ run lsp_array_n_test.cpp ]
|
||||
[ run lsp_array_cv_test.cpp ]
|
||||
[ run lsp_array_cast_test.cpp ]
|
||||
;
|
||||
}
|
||||
|
202
test/lsp_array_cast_test.cpp
Normal file
202
test/lsp_array_cast_test.cpp
Normal file
@ -0,0 +1,202 @@
|
||||
//
|
||||
// lsp_array_cast_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/detail/lightweight_test.hpp>
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
void static_cast_test()
|
||||
{
|
||||
{
|
||||
boost::local_shared_ptr<void> pv;
|
||||
|
||||
boost::local_shared_ptr<int[]> pi = boost::static_pointer_cast<int[]>( pv );
|
||||
BOOST_TEST( pi.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<int[3]> pi2 = boost::static_pointer_cast<int[3]>( pv );
|
||||
BOOST_TEST( pi2.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<X[]> px = boost::static_pointer_cast<X[]>( pv );
|
||||
BOOST_TEST( px.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<X[5]> px2 = boost::static_pointer_cast<X[5]>( pv );
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<int[]> pi( new int[2] );
|
||||
boost::local_shared_ptr<void> pv( pi );
|
||||
|
||||
boost::local_shared_ptr<int[]> pi2 = boost::static_pointer_cast<int[]>( pv );
|
||||
BOOST_TEST(pi.get() == pi2.get());
|
||||
BOOST_TEST(!(pi < pi2 || pi2 < pi));
|
||||
|
||||
boost::local_shared_ptr<int[2]> pi3 = boost::static_pointer_cast<int[2]>( pv );
|
||||
BOOST_TEST(pi.get() == pi3.get());
|
||||
BOOST_TEST(!(pi < pi3 || pi3 < pi));
|
||||
|
||||
boost::local_shared_ptr<void> pv2( pi3 );
|
||||
|
||||
boost::local_shared_ptr<int[]> pi4 = boost::static_pointer_cast<int[]>( pv2 );
|
||||
BOOST_TEST(pi.get() == pi4.get());
|
||||
BOOST_TEST(!(pi < pi4 || pi4 < pi));
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X[]> px( new X[4] );
|
||||
boost::local_shared_ptr<void> pv( px );
|
||||
|
||||
boost::local_shared_ptr<X[]> px2 = boost::static_pointer_cast<X[]>( pv );
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
|
||||
boost::local_shared_ptr<X[4]> px3 = boost::static_pointer_cast<X[4]>( pv );
|
||||
BOOST_TEST(px.get() == px3.get());
|
||||
BOOST_TEST(!(px < px3 || px3 < px));
|
||||
|
||||
boost::local_shared_ptr<void> pv2( px3 );
|
||||
|
||||
boost::local_shared_ptr<X[]> px4 = boost::static_pointer_cast<X[]>( pv2 );
|
||||
BOOST_TEST(px.get() == px4.get());
|
||||
BOOST_TEST(!(px < px4 || px4 < px));
|
||||
}
|
||||
}
|
||||
|
||||
void const_cast_test()
|
||||
{
|
||||
{
|
||||
boost::local_shared_ptr<int const volatile[]> px;
|
||||
|
||||
boost::local_shared_ptr<int[]> px2 = boost::const_pointer_cast<int[]>(px);
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<int const volatile[2]> px;
|
||||
|
||||
boost::local_shared_ptr<int[2]> px2 = boost::const_pointer_cast<int[2]>(px);
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X const volatile[]> px;
|
||||
|
||||
boost::local_shared_ptr<X[]> px2 = boost::const_pointer_cast<X[]>(px);
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X const volatile[5]> px;
|
||||
|
||||
boost::local_shared_ptr<X[5]> px2 = boost::const_pointer_cast<X[5]>(px);
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<int const volatile[]> px( new int[3] );
|
||||
|
||||
boost::local_shared_ptr<int[]> px2 = boost::const_pointer_cast<int[]>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<int const volatile[3]> px( new int[3] );
|
||||
|
||||
boost::local_shared_ptr<int[3]> px2 = boost::const_pointer_cast<int[3]>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X const volatile[]> px( new X[4] );
|
||||
|
||||
boost::local_shared_ptr<X[]> px2 = boost::const_pointer_cast<X[]>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X const volatile[4]> px( new X[4] );
|
||||
|
||||
boost::local_shared_ptr<X[4]> px2 = boost::const_pointer_cast<X[4]>(px);
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
}
|
||||
}
|
||||
|
||||
void reinterpret_cast_test()
|
||||
{
|
||||
{
|
||||
boost::local_shared_ptr<int[]> pi;
|
||||
BOOST_TEST( pi.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<int[3]> pi2 = boost::reinterpret_pointer_cast<int[3]>( pi );
|
||||
BOOST_TEST( pi2.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<int[6]> pi3 = boost::reinterpret_pointer_cast<int[6]>( pi2 );
|
||||
BOOST_TEST( pi3.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X[]> px;
|
||||
BOOST_TEST( px.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<X[5]> px2 = boost::reinterpret_pointer_cast<X[5]>( px );
|
||||
BOOST_TEST( px2.get() == 0 );
|
||||
|
||||
boost::local_shared_ptr<X[9]> px3 = boost::reinterpret_pointer_cast<X[9]>( px2 );
|
||||
BOOST_TEST( px3.get() == 0 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<int[]> pi( new int[2] );
|
||||
|
||||
boost::local_shared_ptr<int[2]> pi2 = boost::reinterpret_pointer_cast<int[2]>( pi );
|
||||
BOOST_TEST(pi.get() == pi2.get());
|
||||
BOOST_TEST(!(pi < pi2 || pi2 < pi));
|
||||
|
||||
boost::local_shared_ptr<int[1]> pi3 = boost::reinterpret_pointer_cast<int[1]>( pi2 );
|
||||
BOOST_TEST(pi.get() == pi3.get());
|
||||
BOOST_TEST(!(pi < pi3 || pi3 < pi));
|
||||
|
||||
boost::local_shared_ptr<int[]> pi4 = boost::reinterpret_pointer_cast<int[]>( pi3 );
|
||||
BOOST_TEST(pi.get() == pi4.get());
|
||||
BOOST_TEST(!(pi < pi4 || pi4 < pi));
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X[]> px( new X[4] );
|
||||
|
||||
boost::local_shared_ptr<X[7]> px2 = boost::reinterpret_pointer_cast<X[7]>( px );
|
||||
BOOST_TEST(px.get() == px2.get());
|
||||
BOOST_TEST(!(px < px2 || px2 < px));
|
||||
|
||||
boost::local_shared_ptr<X[4]> px3 = boost::reinterpret_pointer_cast<X[4]>( px2 );
|
||||
BOOST_TEST(px.get() == px3.get());
|
||||
BOOST_TEST(!(px < px3 || px3 < px));
|
||||
|
||||
boost::local_shared_ptr<X[]> px4 = boost::reinterpret_pointer_cast<X[]>( px3 );
|
||||
BOOST_TEST(px.get() == px4.get());
|
||||
BOOST_TEST(!(px < px4 || px4 < px));
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
static_cast_test();
|
||||
const_cast_test();
|
||||
reinterpret_cast_test();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
60
test/lsp_array_cv_test.cpp
Normal file
60
test/lsp_array_cv_test.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
//
|
||||
// lsp_array_cv_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>
|
||||
|
||||
struct X
|
||||
{
|
||||
};
|
||||
|
||||
class B
|
||||
{
|
||||
};
|
||||
|
||||
class D: public B
|
||||
{
|
||||
};
|
||||
|
||||
#define TEST_CONV( T, U ) \
|
||||
{ \
|
||||
boost::local_shared_ptr< T > p1; \
|
||||
boost::local_shared_ptr< U > p2( p1 ); \
|
||||
p2 = p1; \
|
||||
boost::local_shared_ptr< U > p3 = boost::local_shared_ptr< T >(); \
|
||||
p3 = boost::local_shared_ptr< T >(); \
|
||||
}
|
||||
|
||||
#define TEST_CV_TRUE( T, U ) \
|
||||
TEST_CONV( T, U ) \
|
||||
TEST_CONV( T, const U ) \
|
||||
TEST_CONV( T, volatile U ) \
|
||||
TEST_CONV( T, const volatile U ) \
|
||||
TEST_CONV( const T, const U ) \
|
||||
TEST_CONV( const T, const volatile U ) \
|
||||
TEST_CONV( volatile T, volatile U ) \
|
||||
TEST_CONV( volatile T, const volatile U ) \
|
||||
TEST_CONV( const volatile T, const volatile U )
|
||||
|
||||
int main()
|
||||
{
|
||||
TEST_CV_TRUE( X, X )
|
||||
TEST_CV_TRUE( X, void )
|
||||
TEST_CV_TRUE( D, B )
|
||||
|
||||
TEST_CV_TRUE( X[], X[] )
|
||||
TEST_CV_TRUE( X[3], X[3] )
|
||||
|
||||
TEST_CV_TRUE( X[3], X[] )
|
||||
|
||||
TEST_CV_TRUE( X[], void )
|
||||
TEST_CV_TRUE( X[3], void )
|
||||
|
||||
return 0;
|
||||
}
|
248
test/lsp_array_n_test.cpp
Normal file
248
test/lsp_array_n_test.cpp
Normal file
@ -0,0 +1,248 @@
|
||||
//
|
||||
// lsp_array_n_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[3]> px;
|
||||
BOOST_TEST( !px );
|
||||
|
||||
BOOST_TEST( X::allocations == 0 );
|
||||
BOOST_TEST( X::instances == 0 );
|
||||
|
||||
boost::local_shared_ptr<X[3]> 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[3]> 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[3]> 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[3]> wp( px );
|
||||
BOOST_TEST( wp.lock() == px );
|
||||
|
||||
boost::weak_ptr<X[3]> wp2( px2 );
|
||||
BOOST_TEST( wp2.lock() == px2 );
|
||||
|
||||
wp2.reset();
|
||||
wp2 = px2;
|
||||
BOOST_TEST( wp2.lock() == px2 );
|
||||
|
||||
boost::weak_ptr<X const[3]> wp3( px2 );
|
||||
BOOST_TEST( wp3.lock() == px2 );
|
||||
|
||||
wp3.reset();
|
||||
wp3 = px2;
|
||||
BOOST_TEST( wp3.lock() == px2 );
|
||||
|
||||
boost::weak_ptr<X volatile[3]> 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 );
|
||||
}
|
||||
|
||||
{
|
||||
boost::local_shared_ptr<X[5]> 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[6]> 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();
|
||||
}
|
311
test/lsp_array_test.cpp
Normal file
311
test/lsp_array_test.cpp
Normal file
@ -0,0 +1,311 @@
|
||||
//
|
||||
// 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();
|
||||
}
|
Reference in New Issue
Block a user