Add cast tests to intrusive_ptr_test

This commit is contained in:
Peter Dimov
2019-04-20 18:54:13 +03:00
parent 2a4aca403a
commit 18974ea2db

View File

@ -42,7 +42,7 @@ class base
{ {
private: private:
boost::detail::atomic_count use_count_; mutable boost::detail::atomic_count use_count_;
base(base const &); base(base const &);
base & operator=(base const &); base & operator=(base const &);
@ -70,24 +70,24 @@ public:
#if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
inline friend void intrusive_ptr_add_ref(base * p) inline friend void intrusive_ptr_add_ref(base const * p)
{ {
++p->use_count_; ++p->use_count_;
} }
inline friend void intrusive_ptr_release(base * p) inline friend void intrusive_ptr_release(base const * p)
{ {
if(--p->use_count_ == 0) delete p; if(--p->use_count_ == 0) delete p;
} }
#else #else
void add_ref() void add_ref() const
{ {
++use_count_; ++use_count_;
} }
void release() void release() const
{ {
if(--use_count_ == 0) delete this; if(--use_count_ == 0) delete this;
} }
@ -104,12 +104,12 @@ long base::instances = 0;
namespace boost namespace boost
{ {
inline void intrusive_ptr_add_ref(N::base * p) inline void intrusive_ptr_add_ref(N::base const * p)
{ {
p->add_ref(); p->add_ref();
} }
inline void intrusive_ptr_release(N::base * p) inline void intrusive_ptr_release(N::base const * p)
{ {
p->release(); p->release();
} }
@ -904,15 +904,137 @@ namespace n_static_cast
void test() void test()
{ {
{
boost::intrusive_ptr<X> px(new Y);
boost::intrusive_ptr<Y> py = boost::static_pointer_cast<Y>(px);
BOOST_TEST(px.get() == py.get());
BOOST_TEST(px->use_count() == 2);
BOOST_TEST(py->use_count() == 2);
boost::intrusive_ptr<X> px2(py);
BOOST_TEST(px2.get() == px.get());
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<Y> py = boost::static_pointer_cast<Y>( boost::intrusive_ptr<X>(new Y) );
BOOST_TEST(py.get() != 0);
BOOST_TEST(py->use_count() == 1);
}
BOOST_TEST( N::base::instances == 0 );
} }
} // namespace n_static_cast } // namespace n_static_cast
namespace n_const_cast
{
void test()
{
{
boost::intrusive_ptr<X const> px;
boost::intrusive_ptr<X> px2 = boost::const_pointer_cast<X>(px);
BOOST_TEST(px2.get() == 0);
}
{
boost::intrusive_ptr<X> px2 = boost::const_pointer_cast<X>( boost::intrusive_ptr<X const>() );
BOOST_TEST(px2.get() == 0);
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<X const> px(new X);
boost::intrusive_ptr<X> px2 = boost::const_pointer_cast<X>(px);
BOOST_TEST(px2.get() == px.get());
BOOST_TEST(px2->use_count() == 2);
BOOST_TEST(px->use_count() == 2);
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<X> px = boost::const_pointer_cast<X>( boost::intrusive_ptr<X const>(new X) );
BOOST_TEST(px.get() != 0);
BOOST_TEST(px->use_count() == 1);
}
BOOST_TEST( N::base::instances == 0 );
}
} // namespace n_const_cast
namespace n_dynamic_cast namespace n_dynamic_cast
{ {
void test() void test()
{ {
{
boost::intrusive_ptr<X> px;
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>(px);
BOOST_TEST(py.get() == 0);
}
{
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>( boost::intrusive_ptr<X>() );
BOOST_TEST(py.get() == 0);
}
{
boost::intrusive_ptr<X> px(static_cast<X*>(0));
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>(px);
BOOST_TEST(py.get() == 0);
}
{
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>( boost::intrusive_ptr<X>(static_cast<X*>(0)) );
BOOST_TEST(py.get() == 0);
}
{
boost::intrusive_ptr<X> px(new X);
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>(px);
BOOST_TEST(py.get() == 0);
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>( boost::intrusive_ptr<X>(new X) );
BOOST_TEST(py.get() == 0);
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<X> px(new Y);
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>(px);
BOOST_TEST(py.get() == px.get());
BOOST_TEST(py->use_count() == 2);
BOOST_TEST(px->use_count() == 2);
}
BOOST_TEST( N::base::instances == 0 );
{
boost::intrusive_ptr<X> px(new Y);
boost::intrusive_ptr<Y> py = boost::dynamic_pointer_cast<Y>( boost::intrusive_ptr<X>(new Y) );
BOOST_TEST(py.get() != 0);
BOOST_TEST(py->use_count() == 1);
}
BOOST_TEST( N::base::instances == 0 );
} }
} // namespace n_dynamic_cast } // namespace n_dynamic_cast
@ -976,6 +1098,7 @@ int main()
n_swap::test(); n_swap::test();
n_comparison::test(); n_comparison::test();
n_static_cast::test(); n_static_cast::test();
n_const_cast::test();
n_dynamic_cast::test(); n_dynamic_cast::test();
n_transitive::test(); n_transitive::test();