3 #include <boost/test/auto_unit_test.hpp>
5 #include "zypp/base/Logger.h"
6 #include <zypp/base/PtrTypes.h>
7 #include <zypp/base/ReferenceCounted.h>
8 #include <zypp/base/ProvideNumericId.h>
10 #define BOOST_TEST_MODULE PtrTypes
14 using namespace zypp::base;
17 #define TRACE_TAG DBG << this->numericId() << " " << __PRETTY_FUNCTION__ << endl
19 /** Logs Ctor, CopyCtor, Assign and Dtor. */
20 template<class _Trace>
21 struct Trace : public ProvideNumericId<_Trace,unsigned>
23 Trace() { TRACE_TAG; }
24 Trace( const Trace & ) { TRACE_TAG; }
25 ~Trace() { TRACE_TAG; }
26 Trace & operator=( const Trace & ) { TRACE_TAG; return *this; }
29 /** Data class for shared_ptr */
30 struct NonIntrusive : private Trace<NonIntrusive>
32 using Trace<NonIntrusive>::numericId;
35 /** Data class for intrusive_ptr */
36 struct Intrusive : public ReferenceCounted,
37 private Trace<Intrusive>
39 using Trace<Intrusive>::numericId;
45 inline NonIntrusive * rwcowClone<NonIntrusive>( const NonIntrusive * rhs )
46 { return new NonIntrusive( *rhs ); }
49 inline Intrusive * rwcowClone<Intrusive>( const Intrusive * rhs )
50 { return new Intrusive( *rhs ); }
54 /******************************************************************
57 #define T_NULL assert( !ptr ); assert( ptr == nullptr )
58 #define T_NOT_NULL assert( ptr ); assert( ptr != nullptr )
59 #define T_UNIQUE assert( ptr.unique() ); assert( ptr.use_count() < 2 )
60 #define T_NOT_UNIQUE assert( !ptr.unique() ); assert( ptr.use_count() >= 2 )
61 // Also comapre with underlying shared ptr type.
62 #define T_EQ(a,b) assert( a == b ); assert( a == b.cgetPtr() ); assert( a.cgetPtr() == b ); assert( a.cgetPtr() == b.cgetPtr() );
63 #define T_NE(a,b) assert( a != b ); assert( a != b.cgetPtr() ); assert( a.cgetPtr() != b ); assert( a.cgetPtr() != b.cgetPtr() );
68 MIL << __PRETTY_FUNCTION__ << std::endl;
69 // typedefs that should be provided:
70 typedef typename _RW::_Ptr _Ptr;
71 typedef typename _RW::_constPtr _constPtr;
72 typedef typename _Ptr::element_type _Ptr_element_type;
73 typedef typename _constPtr::element_type _constPtr_element_type;
80 ptr = _RW( new _Ptr_element_type );
96 ptr2.reset( new _Ptr_element_type );
103 // nullptr compatible
104 ptr.reset( nullptr );
110 ptr = _RW( nullptr );
121 MIL << __PRETTY_FUNCTION__ << std::endl;
122 typedef typename _RW::_Ptr::element_type _Ptr_element_type;
124 _RW ptr( new _Ptr_element_type );
125 unsigned long ptrid = ptr->numericId();
129 unsigned long ptrid2 = ptr2->numericId();
130 assert( ptrid != ptrid2 );
133 /******************************************************************
135 ** FUNCTION NAME : main
136 ** FUNCTION TYPE : int
138 BOOST_AUTO_TEST_CASE(basic_test)
140 MIL << "===[START]=====" << endl;
141 test<RW_pointer<NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
142 test<RW_pointer<const NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
143 test<RW_pointer<Intrusive, rw_pointer::Intrusive<Intrusive> > >();
144 test<RW_pointer<const Intrusive, rw_pointer::Intrusive<Intrusive> > >();
146 cowt<RWCOW_pointer<NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
147 cowt<RWCOW_pointer<const NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
148 cowt<RWCOW_pointer<Intrusive, rw_pointer::Intrusive<Intrusive> > >();
149 cowt<RWCOW_pointer<const Intrusive, rw_pointer::Intrusive<Intrusive> > >();
151 MIL << "===[DONE]=====" << endl;