3 #include <boost/test/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 Trace<NonIntrusive>::numericId;
35 /** Data class for intrusive_ptr */
36 struct Intrusive : public ReferenceCounted,
37 private Trace<Intrusive>
39 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 ); }
53 /******************************************************************
56 #define T_NULL assert( !ptr )
57 #define T_NOT_NULL assert( ptr )
58 #define T_UNIQUE assert( ptr.unique() ); assert( ptr.use_count() < 2 )
59 #define T_NOT_UNIQUE assert( !ptr.unique() ); assert( ptr.use_count() >= 2 )
60 #define T_EQ(a,b) assert( a == b )
61 #define T_NE(a,b) assert( a != b )
66 MIL << __PRETTY_FUNCTION__ << std::endl;
67 // typedefs that should be provided:
68 typedef typename _RW::_Ptr _Ptr;
69 typedef typename _RW::_constPtr _constPtr;
70 typedef typename _Ptr::element_type _Ptr_element_type;
71 typedef typename _constPtr::element_type _constPtr_element_type;
78 ptr = _RW( new _Ptr_element_type );
94 ptr2.reset( new _Ptr_element_type );
107 MIL << __PRETTY_FUNCTION__ << std::endl;
108 typedef typename _RW::_Ptr::element_type _Ptr_element_type;
110 _RW ptr( new _Ptr_element_type );
111 unsigned long ptrid = ptr->numericId();
115 unsigned long ptrid2 = ptr2->numericId();
116 assert( ptrid != ptrid2 );
119 /******************************************************************
121 ** FUNCTION NAME : main
122 ** FUNCTION TYPE : int
124 BOOST_AUTO_TEST_CASE(basic_test)
126 MIL << "===[START]=====" << endl;
127 test<RW_pointer<NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
128 test<RW_pointer<const NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
129 test<RW_pointer<Intrusive, rw_pointer::Intrusive<Intrusive> > >();
130 test<RW_pointer<const Intrusive, rw_pointer::Intrusive<Intrusive> > >();
132 cowt<RWCOW_pointer<NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
133 cowt<RWCOW_pointer<const NonIntrusive, rw_pointer::Shared<NonIntrusive> > >();
134 cowt<RWCOW_pointer<Intrusive, rw_pointer::Intrusive<Intrusive> > >();
135 cowt<RWCOW_pointer<const Intrusive, rw_pointer::Intrusive<Intrusive> > >();
137 MIL << "===[DONE]=====" << endl;