diff --git a/test/Jamfile b/test/Jamfile index 07a50dd..067cfab 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -306,3 +306,6 @@ run abi_test_main.cpp abi_test_nt : : : : abi_test_mt_nt ; run abi_test_main.cpp abi_test_mt/0x : : : 98 : abi_test_03_11 ; run abi_test_main.cpp abi_test_mt/98 : : : 0x : abi_test_11_03 ; + +run weak_ptr_alias_test.cpp ; +run weak_ptr_alias_move_test.cpp ; diff --git a/test/weak_ptr_alias_move_test.cpp b/test/weak_ptr_alias_move_test.cpp new file mode 100644 index 0000000..57a7143 --- /dev/null +++ b/test/weak_ptr_alias_move_test.cpp @@ -0,0 +1,97 @@ + +// weak_ptr_alias_move_test.cpp +// +// Copyright 2007, 2019 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 +#include +#include + +#if !defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) + +class incomplete; + +struct X +{ + int v_; + + explicit X( int v ): v_( v ) + { + } + + ~X() + { + v_ = 0; + } +}; + +template bool share_ownership( P1 const& p1, P2 const& p2 ) +{ + return !p1.owner_before( p2 ) && !p2.owner_before( p1 ); +} + +int main() +{ + { + boost::shared_ptr p( new float ); + boost::weak_ptr p2( p ); + + int m2 = 0; + boost::weak_ptr p3( std::move( p2 ), &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + + BOOST_TEST( p2.empty() ); + } + + { + boost::shared_ptr p; + boost::weak_ptr p2( p ); + + int m2 = 0; + boost::weak_ptr p3( std::move( p2 ), &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + + BOOST_TEST( p2.empty() ); + } + + { + boost::shared_ptr p( new X( 5 ) ); + boost::weak_ptr p2( p ); + + boost::weak_ptr p3( std::move( p2 ), &p2.lock()->v_ ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == &p->v_ ); + + BOOST_TEST( p2.empty() ); + + p.reset(); + BOOST_TEST( p3.expired() ); + } + + return boost::report_errors(); +} + +#else // defined( BOOST_NO_CXX11_RVALUE_REFERENCES ) + +int main() +{ + return 0; +} + +#endif diff --git a/test/weak_ptr_alias_test.cpp b/test/weak_ptr_alias_test.cpp new file mode 100644 index 0000000..39dd30e --- /dev/null +++ b/test/weak_ptr_alias_test.cpp @@ -0,0 +1,203 @@ + +// weak_ptr_alias_test.cpp +// +// Copyright 2007, 2019 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 +#include + +// + +class incomplete; + +struct X +{ + int v_; + + explicit X( int v ): v_( v ) + { + } + + ~X() + { + v_ = 0; + } +}; + +template bool share_ownership( P1 const& p1, P2 const& p2 ) +{ + return !p1.owner_before( p2 ) && !p2.owner_before( p1 ); +} + +int main() +{ + { + boost::shared_ptr p( new double ); + + float m = 0; + boost::weak_ptr p2( p, &m ); + + BOOST_TEST( p2.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p2 ) ); + BOOST_TEST( p2.lock().get() == &m ); + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == &m2 ); + } + } + + { + boost::shared_ptr p; + + float m = 0; + boost::weak_ptr p2( p, &m ); + + BOOST_TEST( p2.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p2 ) ); + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p, &m2 ); + + BOOST_TEST( p3.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + + { + int m2 = 0; + boost::weak_ptr p3( p2, &m2 ); + + BOOST_TEST( p3.use_count() == p2.use_count() ); + BOOST_TEST( share_ownership( p2, p3 ) ); + BOOST_TEST( p3.lock().get() == 0 ); + } + } + + { + boost::shared_ptr p( new X( 5 ) ); + + boost::weak_ptr p2( p, &p->v_ ); + + BOOST_TEST( p2.use_count() == p.use_count() ); + BOOST_TEST( share_ownership( p, p2 ) ); + BOOST_TEST( p2.lock().get() == &p->v_ ); + + p.reset(); + BOOST_TEST( p2.expired() ); + } + + return boost::report_errors(); +}