mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 21:24:40 +02:00
Regression test for enable_shared_from_this.
[SVN r43733]
This commit is contained in:
@@ -35,5 +35,6 @@ import testing ;
|
|||||||
[ run sp_unary_addr_test.cpp ]
|
[ run sp_unary_addr_test.cpp ]
|
||||||
[ compile-fail scoped_ptr_eq_fail.cpp ]
|
[ compile-fail scoped_ptr_eq_fail.cpp ]
|
||||||
[ compile-fail scoped_array_eq_fail.cpp ]
|
[ compile-fail scoped_array_eq_fail.cpp ]
|
||||||
|
[ run esft_regtest.cpp ]
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
136
test/esft_regtest.cpp
Normal file
136
test/esft_regtest.cpp
Normal file
@@ -0,0 +1,136 @@
|
|||||||
|
//
|
||||||
|
// esft_regtest.cpp
|
||||||
|
//
|
||||||
|
// A regression test for enable_shared_from_this
|
||||||
|
//
|
||||||
|
// Copyright (c) 2008 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/enable_shared_from_this.hpp>
|
||||||
|
#include <boost/shared_ptr.hpp>
|
||||||
|
#include <boost/weak_ptr.hpp>
|
||||||
|
#include <boost/detail/lightweight_test.hpp>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
class X: public boost::enable_shared_from_this< X >
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
|
||||||
|
int destroyed_;
|
||||||
|
int deleted_;
|
||||||
|
int expected_;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
X( X const& );
|
||||||
|
X& operator=( X const& );
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
static int instances;
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit X( int expected ): destroyed_( 0 ), deleted_( 0 ), expected_( expected )
|
||||||
|
{
|
||||||
|
++instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
~X()
|
||||||
|
{
|
||||||
|
BOOST_TEST( deleted_ == expected_ );
|
||||||
|
BOOST_TEST( destroyed_ == 0 );
|
||||||
|
++destroyed_;
|
||||||
|
--instances;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef void (*deleter_type)( X* );
|
||||||
|
|
||||||
|
static void deleter( X * px )
|
||||||
|
{
|
||||||
|
++px->deleted_;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void deleter2( X * px )
|
||||||
|
{
|
||||||
|
++px->deleted_;
|
||||||
|
delete px;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int X::instances = 0;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
X x( 0 );
|
||||||
|
BOOST_TEST( X::instances == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
std::auto_ptr<X> px( new X( 0 ) );
|
||||||
|
BOOST_TEST( X::instances == 1 );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::shared_ptr<X> px( new X( 0 ) );
|
||||||
|
BOOST_TEST( X::instances == 1 );
|
||||||
|
|
||||||
|
boost::weak_ptr<X> wp( px );
|
||||||
|
BOOST_TEST( !wp.expired() );
|
||||||
|
|
||||||
|
px.reset();
|
||||||
|
|
||||||
|
BOOST_TEST( wp.expired() );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
X x( 1 );
|
||||||
|
boost::shared_ptr<X> px( &x, X::deleter );
|
||||||
|
BOOST_TEST( X::instances == 1 );
|
||||||
|
|
||||||
|
X::deleter_type * pd = boost::get_deleter<X::deleter_type>( px );
|
||||||
|
BOOST_TEST( pd != 0 && *pd == X::deleter );
|
||||||
|
|
||||||
|
boost::weak_ptr<X> wp( px );
|
||||||
|
BOOST_TEST( !wp.expired() );
|
||||||
|
|
||||||
|
px.reset();
|
||||||
|
|
||||||
|
BOOST_TEST( wp.expired() );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
{
|
||||||
|
boost::shared_ptr<X> px( new X( 1 ), X::deleter2 );
|
||||||
|
BOOST_TEST( X::instances == 1 );
|
||||||
|
|
||||||
|
X::deleter_type * pd = boost::get_deleter<X::deleter_type>( px );
|
||||||
|
BOOST_TEST( pd != 0 && *pd == X::deleter2 );
|
||||||
|
|
||||||
|
boost::weak_ptr<X> wp( px );
|
||||||
|
BOOST_TEST( !wp.expired() );
|
||||||
|
|
||||||
|
px.reset();
|
||||||
|
|
||||||
|
BOOST_TEST( wp.expired() );
|
||||||
|
}
|
||||||
|
|
||||||
|
BOOST_TEST( X::instances == 0 );
|
||||||
|
|
||||||
|
return boost::report_errors();
|
||||||
|
}
|
Reference in New Issue
Block a user