diff --git a/test/Jamfile b/test/Jamfile index 0f95feb..02a8a2e 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -315,3 +315,7 @@ run sp_typeinfo_test.cpp : : : off : sp_typeinfo_test_no_rtti ; run get_deleter_test.cpp : : : off gcc-4.4.7,0x:no : get_deleter_test_no_rtti ; run get_deleter_test2.cpp : : : off gcc-4.4.7,0x:no : get_deleter_test2_no_rtti ; run get_deleter_test3.cpp : : : off gcc-4.4.7,0x:no : get_deleter_test3_no_rtti ; + +run shared_from_test.cpp ; +run weak_from_test.cpp ; +run weak_from_test2.cpp ; diff --git a/test/shared_from_test.cpp b/test/shared_from_test.cpp new file mode 100644 index 0000000..47e2905 --- /dev/null +++ b/test/shared_from_test.cpp @@ -0,0 +1,102 @@ + +// shared_from_test.cpp +// +// Copyright 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +// + +class X +{ +private: + + int m_; + +public: + + X(): m_() {} +}; + +class Y: public boost::enable_shared_from +{ +}; + +class Z: public X, public Y +{ +public: + + boost::shared_ptr shared_from_this() + { + return boost::shared_from( this ); + } +}; + +void null_deleter( void const* ) +{ +} + +int main() +{ + boost::shared_ptr p( new Z ); + + try + { + boost::shared_ptr q = p->shared_from_this(); + + BOOST_TEST_EQ( p, q ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + catch( boost::bad_weak_ptr const & ) + { + BOOST_ERROR( "p->shared_from_this() failed" ); + } + + Z v2( *p ); + + try + { + boost::shared_ptr q = v2.shared_from_this(); + BOOST_ERROR( "v2.shared_from_this() failed to throw" ); + } + catch( boost::bad_weak_ptr const & ) + { + } + + *p = Z(); + + try + { + boost::shared_ptr q = p->shared_from_this(); + + BOOST_TEST_EQ( p, q ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + catch( boost::bad_weak_ptr const & ) + { + BOOST_ERROR( "p->shared_from_this() threw bad_weak_ptr after *p = Z()" ); + } + + { + boost::shared_ptr p2( p.get(), null_deleter ); + } + + try + { + boost::shared_ptr q = p->shared_from_this(); + + BOOST_TEST_EQ( p, q ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + catch( boost::bad_weak_ptr const& ) + { + BOOST_ERROR( "p->shared_from_this() failed" ); + } + + return boost::report_errors(); +} diff --git a/test/weak_from_test.cpp b/test/weak_from_test.cpp new file mode 100644 index 0000000..eda1558 --- /dev/null +++ b/test/weak_from_test.cpp @@ -0,0 +1,97 @@ + +// weak_from_test.cpp +// +// Copyright 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +// + +class X +{ +private: + + int m_; + +public: + + X(): m_() {} +}; + +class Y: public boost::enable_shared_from +{ +}; + +class Z: public X, public Y +{ +public: + + boost::weak_ptr weak_from_this() + { + return boost::weak_from( this ); + } +}; + +void null_deleter( void const* ) +{ +} + +int main() +{ + boost::shared_ptr sp( new Z ); + boost::weak_ptr p( sp ); + + { + boost::weak_ptr q = sp->weak_from_this(); + + BOOST_TEST_EQ( p.lock(), q.lock() ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + + Z v2( *sp ); + + { + boost::weak_ptr q = v2.weak_from_this(); + BOOST_TEST( q.expired() ); + } + + *sp = Z(); + + { + boost::weak_ptr q = sp->weak_from_this(); + + BOOST_TEST_EQ( p.lock(), q.lock() ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + + { + boost::shared_ptr sp2( sp.get(), null_deleter ); + } + + { + boost::weak_ptr q = sp->weak_from_this(); + + BOOST_TEST_EQ( p.lock(), q.lock() ); + BOOST_TEST( !( p < q ) && !( q < p ) ); + } + + { + boost::weak_ptr p2 = sp->weak_from_this(); + + BOOST_TEST( !p.expired() ); + BOOST_TEST( !p2.expired() ); + + sp.reset(); + + BOOST_TEST( p.expired() ); + BOOST_TEST( p2.expired() ); + } + + return boost::report_errors(); +} diff --git a/test/weak_from_test2.cpp b/test/weak_from_test2.cpp new file mode 100644 index 0000000..19314a9 --- /dev/null +++ b/test/weak_from_test2.cpp @@ -0,0 +1,57 @@ + +// weak_from_test2.cpp +// +// Tests weak_from in a destructor +// +// Copyright 2014, 2015, 2019 Peter Dimov +// +// Distributed under the Boost Software License, Version 1.0. +// http://www.boost.org/LICENSE_1_0.txt + +#include +#include +#include + +class X: public boost::enable_shared_from +{ +private: + + boost::weak_ptr px_; + +public: + + X() + { + boost::weak_ptr p1 = weak_from( this ); + BOOST_TEST( p1._empty() ); + BOOST_TEST( p1.expired() ); + } + + void check() + { + boost::weak_ptr p2 = weak_from( this ); + BOOST_TEST( !p2.expired() ); + + BOOST_TEST( p2.lock().get() == this ); + + px_ = p2; + } + + ~X() + { + boost::weak_ptr p3 = weak_from( this ); + BOOST_TEST( p3.expired() ); + + BOOST_TEST( !(px_ < p3) && !(p3 < px_) ); + } +}; + +int main() +{ + { + boost::shared_ptr< X > px( new X ); + px->check(); + } + + return boost::report_errors(); +}