From 18974ea2db48571277f9b6ecd8e18d6e2aec62cc Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Sat, 20 Apr 2019 18:54:13 +0300 Subject: [PATCH] Add cast tests to intrusive_ptr_test --- test/intrusive_ptr_test.cpp | 137 ++++++++++++++++++++++++++++++++++-- 1 file changed, 130 insertions(+), 7 deletions(-) diff --git a/test/intrusive_ptr_test.cpp b/test/intrusive_ptr_test.cpp index 0c49b97..475428f 100644 --- a/test/intrusive_ptr_test.cpp +++ b/test/intrusive_ptr_test.cpp @@ -42,7 +42,7 @@ class base { private: - boost::detail::atomic_count use_count_; + mutable boost::detail::atomic_count use_count_; base(base const &); base & operator=(base const &); @@ -70,24 +70,24 @@ public: #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_; } - inline friend void intrusive_ptr_release(base * p) + inline friend void intrusive_ptr_release(base const * p) { if(--p->use_count_ == 0) delete p; } #else - void add_ref() + void add_ref() const { ++use_count_; } - void release() + void release() const { if(--use_count_ == 0) delete this; } @@ -104,12 +104,12 @@ long base::instances = 0; namespace boost { -inline void intrusive_ptr_add_ref(N::base * p) +inline void intrusive_ptr_add_ref(N::base const * p) { p->add_ref(); } -inline void intrusive_ptr_release(N::base * p) +inline void intrusive_ptr_release(N::base const * p) { p->release(); } @@ -904,15 +904,137 @@ namespace n_static_cast void test() { + { + boost::intrusive_ptr px(new Y); + + boost::intrusive_ptr py = boost::static_pointer_cast(px); + BOOST_TEST(px.get() == py.get()); + BOOST_TEST(px->use_count() == 2); + BOOST_TEST(py->use_count() == 2); + + boost::intrusive_ptr px2(py); + BOOST_TEST(px2.get() == px.get()); + } + + BOOST_TEST( N::base::instances == 0 ); + + { + boost::intrusive_ptr py = boost::static_pointer_cast( boost::intrusive_ptr(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_const_cast +{ + +void test() +{ + { + boost::intrusive_ptr px; + + boost::intrusive_ptr px2 = boost::const_pointer_cast(px); + BOOST_TEST(px2.get() == 0); + } + + { + boost::intrusive_ptr px2 = boost::const_pointer_cast( boost::intrusive_ptr() ); + BOOST_TEST(px2.get() == 0); + } + + BOOST_TEST( N::base::instances == 0 ); + + { + boost::intrusive_ptr px(new X); + + boost::intrusive_ptr px2 = boost::const_pointer_cast(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 px = boost::const_pointer_cast( boost::intrusive_ptr(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 { void test() { + { + boost::intrusive_ptr px; + + boost::intrusive_ptr py = boost::dynamic_pointer_cast(px); + BOOST_TEST(py.get() == 0); + } + + { + boost::intrusive_ptr py = boost::dynamic_pointer_cast( boost::intrusive_ptr() ); + BOOST_TEST(py.get() == 0); + } + + { + boost::intrusive_ptr px(static_cast(0)); + + boost::intrusive_ptr py = boost::dynamic_pointer_cast(px); + BOOST_TEST(py.get() == 0); + } + + { + boost::intrusive_ptr py = boost::dynamic_pointer_cast( boost::intrusive_ptr(static_cast(0)) ); + BOOST_TEST(py.get() == 0); + } + + { + boost::intrusive_ptr px(new X); + + boost::intrusive_ptr py = boost::dynamic_pointer_cast(px); + BOOST_TEST(py.get() == 0); + } + + BOOST_TEST( N::base::instances == 0 ); + + { + boost::intrusive_ptr py = boost::dynamic_pointer_cast( boost::intrusive_ptr(new X) ); + BOOST_TEST(py.get() == 0); + } + + BOOST_TEST( N::base::instances == 0 ); + + { + boost::intrusive_ptr px(new Y); + + boost::intrusive_ptr py = boost::dynamic_pointer_cast(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 px(new Y); + + boost::intrusive_ptr py = boost::dynamic_pointer_cast( boost::intrusive_ptr(new Y) ); + BOOST_TEST(py.get() != 0); + BOOST_TEST(py->use_count() == 1); + } + + BOOST_TEST( N::base::instances == 0 ); } } // namespace n_dynamic_cast @@ -976,6 +1098,7 @@ int main() n_swap::test(); n_comparison::test(); n_static_cast::test(); + n_const_cast::test(); n_dynamic_cast::test(); n_transitive::test();