1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108 |
- #include <boost/config.hpp>
- #if defined(BOOST_MSVC)
- #pragma warning(disable: 4786) // identifier truncated in debug info
- #pragma warning(disable: 4710) // function not inlined
- #pragma warning(disable: 4711) // function selected for automatic inline expansion
- #pragma warning(disable: 4514) // unreferenced inline removed
- #pragma warning(disable: 4355) // 'this' : used in base member initializer list
- #pragma warning(disable: 4511) // copy constructor could not be generated
- #pragma warning(disable: 4512) // assignment operator could not be generated
- #if (BOOST_MSVC >= 1310)
- #pragma warning(disable: 4675) // resolved overload found with Koenig lookup
- #endif
- #endif
- //
- // intrusive_ptr_test.cpp
- //
- // Copyright (c) 2002-2005 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/detail/lightweight_test.hpp>
- #include <boost/intrusive_ptr.hpp>
- #include <boost/detail/atomic_count.hpp>
- #include <boost/config.hpp>
- #include <algorithm>
- #include <functional>
- //
- namespace N
- {
- class base
- {
- private:
- mutable boost::detail::atomic_count use_count_;
- base(base const &);
- base & operator=(base const &);
- protected:
- base(): use_count_(0)
- {
- ++instances;
- }
- virtual ~base()
- {
- --instances;
- }
- public:
- static long instances;
- long use_count() const
- {
- return use_count_;
- }
- #if !defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- inline friend void intrusive_ptr_add_ref(base const * p)
- {
- ++p->use_count_;
- }
- inline friend void intrusive_ptr_release(base const * p)
- {
- if(--p->use_count_ == 0) delete p;
- }
- #else
- void add_ref() const
- {
- ++use_count_;
- }
- void release() const
- {
- if(--use_count_ == 0) delete this;
- }
- #endif
- };
- long base::instances = 0;
- } // namespace N
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- namespace boost
- {
- inline void intrusive_ptr_add_ref(N::base const * p)
- {
- p->add_ref();
- }
- inline void intrusive_ptr_release(N::base const * p)
- {
- p->release();
- }
- } // namespace boost
- #endif
- //
- struct X: public virtual N::base
- {
- };
- struct Y: public X
- {
- };
- //
- namespace n_element_type
- {
- void f(X &)
- {
- }
- void test()
- {
- typedef boost::intrusive_ptr<X>::element_type T;
- T t;
- f(t);
- }
- } // namespace n_element_type
- namespace n_constructors
- {
- void default_constructor()
- {
- boost::intrusive_ptr<X> px;
- BOOST_TEST(px.get() == 0);
- }
- void pointer_constructor()
- {
- {
- boost::intrusive_ptr<X> px(0);
- BOOST_TEST(px.get() == 0);
- }
- {
- boost::intrusive_ptr<X> px(0, false);
- BOOST_TEST(px.get() == 0);
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- X * p = new X;
- BOOST_TEST(p->use_count() == 0);
- BOOST_TEST( N::base::instances == 1 );
- boost::intrusive_ptr<X> px(p);
- BOOST_TEST(px.get() == p);
- BOOST_TEST(px->use_count() == 1);
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- X * p = new X;
- BOOST_TEST(p->use_count() == 0);
- BOOST_TEST( N::base::instances == 1 );
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::intrusive_ptr_add_ref;
- #endif
- intrusive_ptr_add_ref(p);
- BOOST_TEST(p->use_count() == 1);
- boost::intrusive_ptr<X> px(p, false);
- BOOST_TEST(px.get() == p);
- BOOST_TEST(px->use_count() == 1);
- }
- BOOST_TEST( N::base::instances == 0 );
- }
- void copy_constructor()
- {
- {
- boost::intrusive_ptr<X> px;
- boost::intrusive_ptr<X> px2(px);
- BOOST_TEST(px2.get() == px.get());
- }
- {
- boost::intrusive_ptr<Y> py;
- boost::intrusive_ptr<X> px(py);
- BOOST_TEST(px.get() == py.get());
- }
- {
- boost::intrusive_ptr<X> px(0);
- boost::intrusive_ptr<X> px2(px);
- BOOST_TEST(px2.get() == px.get());
- }
- {
- boost::intrusive_ptr<Y> py(0);
- boost::intrusive_ptr<X> px(py);
- BOOST_TEST(px.get() == py.get());
- }
- {
- boost::intrusive_ptr<X> px(0, false);
- boost::intrusive_ptr<X> px2(px);
- BOOST_TEST(px2.get() == px.get());
- }
- {
- boost::intrusive_ptr<Y> py(0, false);
- boost::intrusive_ptr<X> px(py);
- BOOST_TEST(px.get() == py.get());
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px(new X);
- boost::intrusive_ptr<X> px2(px);
- BOOST_TEST( px2.get() == px.get() );
- BOOST_TEST( N::base::instances == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<Y> py(new Y);
- boost::intrusive_ptr<X> px(py);
- BOOST_TEST( px.get() == py.get() );
- BOOST_TEST( N::base::instances == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- }
- void test()
- {
- default_constructor();
- pointer_constructor();
- copy_constructor();
- }
- } // namespace n_constructors
- namespace n_destructor
- {
- void test()
- {
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px(new X);
- BOOST_TEST(px->use_count() == 1);
- BOOST_TEST( N::base::instances == 1 );
- {
- boost::intrusive_ptr<X> px2(px);
- BOOST_TEST(px->use_count() == 2);
- }
- BOOST_TEST(px->use_count() == 1);
- }
- BOOST_TEST( N::base::instances == 0 );
- }
- } // namespace n_destructor
- namespace n_assignment
- {
- void copy_assignment()
- {
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> p1;
- p1 = p1;
- BOOST_TEST(p1 == p1);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- boost::intrusive_ptr<X> p2;
- p1 = p2;
- BOOST_TEST(p1 == p2);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- boost::intrusive_ptr<X> p3(p1);
- p1 = p3;
- BOOST_TEST(p1 == p3);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- BOOST_TEST(N::base::instances == 0);
- boost::intrusive_ptr<X> p4(new X);
- BOOST_TEST(N::base::instances == 1);
- p1 = p4;
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p1 == p4);
- BOOST_TEST(p1->use_count() == 2);
- p1 = p2;
- BOOST_TEST(p1 == p2);
- BOOST_TEST(N::base::instances == 1);
- p4 = p3;
- BOOST_TEST(p4 == p3);
- BOOST_TEST(N::base::instances == 0);
- }
- }
- void conversion_assignment()
- {
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> p1;
- boost::intrusive_ptr<Y> p2;
- p1 = p2;
- BOOST_TEST(p1 == p2);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- BOOST_TEST(N::base::instances == 0);
- boost::intrusive_ptr<Y> p4(new Y);
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p4->use_count() == 1);
- boost::intrusive_ptr<X> p5(p4);
- BOOST_TEST(p4->use_count() == 2);
- p1 = p4;
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p1 == p4);
- BOOST_TEST(p1->use_count() == 3);
- BOOST_TEST(p4->use_count() == 3);
- p1 = p2;
- BOOST_TEST(p1 == p2);
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p4->use_count() == 2);
- p4 = p2;
- p5 = p2;
- BOOST_TEST(p4 == p2);
- BOOST_TEST(N::base::instances == 0);
- }
- }
- void pointer_assignment()
- {
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> p1;
- p1 = p1.get();
- BOOST_TEST(p1 == p1);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- boost::intrusive_ptr<X> p2;
- p1 = p2.get();
- BOOST_TEST(p1 == p2);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- boost::intrusive_ptr<X> p3(p1);
- p1 = p3.get();
- BOOST_TEST(p1 == p3);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- BOOST_TEST(N::base::instances == 0);
- boost::intrusive_ptr<X> p4(new X);
- BOOST_TEST(N::base::instances == 1);
- p1 = p4.get();
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p1 == p4);
- BOOST_TEST(p1->use_count() == 2);
- p1 = p2.get();
- BOOST_TEST(p1 == p2);
- BOOST_TEST(N::base::instances == 1);
- p4 = p3.get();
- BOOST_TEST(p4 == p3);
- BOOST_TEST(N::base::instances == 0);
- }
- {
- boost::intrusive_ptr<X> p1;
- boost::intrusive_ptr<Y> p2;
- p1 = p2.get();
- BOOST_TEST(p1 == p2);
- BOOST_TEST(p1? false: true);
- BOOST_TEST(!p1);
- BOOST_TEST(p1.get() == 0);
- BOOST_TEST(N::base::instances == 0);
- boost::intrusive_ptr<Y> p4(new Y);
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p4->use_count() == 1);
- boost::intrusive_ptr<X> p5(p4);
- BOOST_TEST(p4->use_count() == 2);
- p1 = p4.get();
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p1 == p4);
- BOOST_TEST(p1->use_count() == 3);
- BOOST_TEST(p4->use_count() == 3);
- p1 = p2.get();
- BOOST_TEST(p1 == p2);
- BOOST_TEST(N::base::instances == 1);
- BOOST_TEST(p4->use_count() == 2);
- p4 = p2.get();
- p5 = p2.get();
- BOOST_TEST(p4 == p2);
- BOOST_TEST(N::base::instances == 0);
- }
- }
- void test()
- {
- copy_assignment();
- conversion_assignment();
- pointer_assignment();
- }
- } // namespace n_assignment
- namespace n_reset
- {
- void test()
- {
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px;
- BOOST_TEST( px.get() == 0 );
- px.reset();
- BOOST_TEST( px.get() == 0 );
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- BOOST_TEST( N::base::instances == 1 );
- px.reset( p );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- px.reset();
- BOOST_TEST( px.get() == 0 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( N::base::instances == 1 );
- px.reset( 0 );
- BOOST_TEST( px.get() == 0 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( N::base::instances == 1 );
- px.reset( 0, false );
- BOOST_TEST( px.get() == 0 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( N::base::instances == 1 );
- px.reset( 0, true );
- BOOST_TEST( px.get() == 0 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- BOOST_TEST( N::base::instances == 1 );
- boost::intrusive_ptr<X> px;
- BOOST_TEST( px.get() == 0 );
- px.reset( p, true );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- BOOST_TEST( N::base::instances == 1 );
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::intrusive_ptr_add_ref;
- #endif
- intrusive_ptr_add_ref( p );
- BOOST_TEST( p->use_count() == 1 );
- boost::intrusive_ptr<X> px;
- BOOST_TEST( px.get() == 0 );
- px.reset( p, false );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( px.get() != 0 );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- BOOST_TEST( N::base::instances == 2 );
- px.reset( p );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( px.get() != 0 );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- BOOST_TEST( N::base::instances == 2 );
- px.reset( p, true );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- {
- boost::intrusive_ptr<X> px( new X );
- BOOST_TEST( px.get() != 0 );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::intrusive_ptr_add_ref;
- #endif
- intrusive_ptr_add_ref( p );
- BOOST_TEST( p->use_count() == 1 );
- BOOST_TEST( N::base::instances == 2 );
- px.reset( p, false );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- BOOST_TEST( N::base::instances == 1 );
- }
- BOOST_TEST( N::base::instances == 0 );
- }
- } // namespace n_reset
- namespace n_access
- {
- void test()
- {
- {
- boost::intrusive_ptr<X> px;
- BOOST_TEST(px? false: true);
- BOOST_TEST(!px);
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::get_pointer;
- #endif
- BOOST_TEST(get_pointer(px) == px.get());
- }
- {
- boost::intrusive_ptr<X> px(0);
- BOOST_TEST(px? false: true);
- BOOST_TEST(!px);
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::get_pointer;
- #endif
- BOOST_TEST(get_pointer(px) == px.get());
- }
- {
- boost::intrusive_ptr<X> px(new X);
- BOOST_TEST(px? true: false);
- BOOST_TEST(!!px);
- BOOST_TEST(&*px == px.get());
- BOOST_TEST(px.operator ->() == px.get());
- #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
- using boost::get_pointer;
- #endif
- BOOST_TEST(get_pointer(px) == px.get());
- }
- {
- boost::intrusive_ptr<X> px;
- X* detached = px.detach();
- BOOST_TEST( px.get() == 0 );
- BOOST_TEST( detached == 0 );
- }
- {
- X * p = new X;
- BOOST_TEST( p->use_count() == 0 );
- boost::intrusive_ptr<X> px( p );
- BOOST_TEST( px.get() == p );
- BOOST_TEST( px->use_count() == 1 );
- X * detached = px.detach();
- BOOST_TEST( px.get() == 0 );
- BOOST_TEST( detached == p );
- BOOST_TEST( detached->use_count() == 1 );
- delete detached;
- }
- }
- } // namespace n_access
- namespace n_swap
- {
- void test()
- {
- {
- boost::intrusive_ptr<X> px;
- boost::intrusive_ptr<X> px2;
- px.swap(px2);
- BOOST_TEST(px.get() == 0);
- BOOST_TEST(px2.get() == 0);
- using std::swap;
- swap(px, px2);
- BOOST_TEST(px.get() == 0);
- BOOST_TEST(px2.get() == 0);
- }
- {
- X * p = new X;
- boost::intrusive_ptr<X> px;
- boost::intrusive_ptr<X> px2(p);
- boost::intrusive_ptr<X> px3(px2);
- px.swap(px2);
- BOOST_TEST(px.get() == p);
- BOOST_TEST(px->use_count() == 2);
- BOOST_TEST(px2.get() == 0);
- BOOST_TEST(px3.get() == p);
- BOOST_TEST(px3->use_count() == 2);
- using std::swap;
- swap(px, px2);
- BOOST_TEST(px.get() == 0);
- BOOST_TEST(px2.get() == p);
- BOOST_TEST(px2->use_count() == 2);
- BOOST_TEST(px3.get() == p);
- BOOST_TEST(px3->use_count() == 2);
- }
- {
- X * p1 = new X;
- X * p2 = new X;
- boost::intrusive_ptr<X> px(p1);
- boost::intrusive_ptr<X> px2(p2);
- boost::intrusive_ptr<X> px3(px2);
- px.swap(px2);
- BOOST_TEST(px.get() == p2);
- BOOST_TEST(px->use_count() == 2);
- BOOST_TEST(px2.get() == p1);
- BOOST_TEST(px2->use_count() == 1);
- BOOST_TEST(px3.get() == p2);
- BOOST_TEST(px3->use_count() == 2);
- using std::swap;
- swap(px, px2);
- BOOST_TEST(px.get() == p1);
- BOOST_TEST(px->use_count() == 1);
- BOOST_TEST(px2.get() == p2);
- BOOST_TEST(px2->use_count() == 2);
- BOOST_TEST(px3.get() == p2);
- BOOST_TEST(px3->use_count() == 2);
- }
- }
- } // namespace n_swap
- namespace n_comparison
- {
- template<class T, class U> void test2(boost::intrusive_ptr<T> const & p, boost::intrusive_ptr<U> const & q)
- {
- BOOST_TEST((p == q) == (p.get() == q.get()));
- BOOST_TEST((p != q) == (p.get() != q.get()));
- }
- template<class T> void test3(boost::intrusive_ptr<T> const & p, boost::intrusive_ptr<T> const & q)
- {
- BOOST_TEST((p == q) == (p.get() == q.get()));
- BOOST_TEST((p.get() == q) == (p.get() == q.get()));
- BOOST_TEST((p == q.get()) == (p.get() == q.get()));
- BOOST_TEST((p != q) == (p.get() != q.get()));
- BOOST_TEST((p.get() != q) == (p.get() != q.get()));
- BOOST_TEST((p != q.get()) == (p.get() != q.get()));
- // 'less' moved here as a g++ 2.9x parse error workaround
- std::less<T*> less;
- BOOST_TEST((p < q) == less(p.get(), q.get()));
- }
- void test()
- {
- {
- boost::intrusive_ptr<X> px;
- test3(px, px);
- boost::intrusive_ptr<X> px2;
- test3(px, px2);
- boost::intrusive_ptr<X> px3(px);
- test3(px3, px3);
- test3(px, px3);
- }
- {
- boost::intrusive_ptr<X> px;
- boost::intrusive_ptr<X> px2(new X);
- test3(px, px2);
- test3(px2, px2);
- boost::intrusive_ptr<X> px3(new X);
- test3(px2, px3);
- boost::intrusive_ptr<X> px4(px2);
- test3(px2, px4);
- test3(px4, px4);
- }
- {
- boost::intrusive_ptr<X> px(new X);
- boost::intrusive_ptr<Y> py(new Y);
- test2(px, py);
- boost::intrusive_ptr<X> px2(py);
- test2(px2, py);
- test3(px, px2);
- test3(px2, px2);
- }
- }
- } // namespace n_comparison
- namespace n_static_cast
- {
- 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_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
- {
- 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_transitive
- {
- struct X: public N::base
- {
- boost::intrusive_ptr<X> next;
- };
- void test()
- {
- boost::intrusive_ptr<X> p(new X);
- p->next = boost::intrusive_ptr<X>(new X);
- BOOST_TEST(!p->next->next);
- p = p->next;
- BOOST_TEST(!p->next);
- }
- } // namespace n_transitive
- namespace n_report_1
- {
- class foo: public N::base
- {
- public:
- foo(): m_self(this)
- {
- }
- void suicide()
- {
- m_self = 0;
- }
- private:
- boost::intrusive_ptr<foo> m_self;
- };
- void test()
- {
- foo * foo_ptr = new foo;
- foo_ptr->suicide();
- }
- } // namespace n_report_1
- int main()
- {
- n_element_type::test();
- n_constructors::test();
- n_destructor::test();
- n_assignment::test();
- n_reset::test();
- n_access::test();
- n_swap::test();
- n_comparison::test();
- n_static_cast::test();
- n_const_cast::test();
- n_dynamic_cast::test();
- n_transitive::test();
- n_report_1::test();
- return boost::report_errors();
- }
|