556ad5894dd01879f4464287fe29b7a3575ae6ae
[platform/upstream/libzypp.git] / tests / zypp / PtrTypes_test.cc
1 #include <iostream>
2
3 #include <boost/test/unit_test.hpp>
4
5 #include "zypp/base/Logger.h"
6 #include <zypp/base/PtrTypes.h>
7 #include <zypp/base/ReferenceCounted.h>
8 #include <zypp/base/ProvideNumericId.h>
9
10 #define BOOST_TEST_MODULE PtrTypes
11
12 using std::endl;
13 using namespace zypp;
14 using namespace zypp::base;
15
16
17 #define TRACE_TAG DBG << this->numericId() << " " << __PRETTY_FUNCTION__ << endl
18
19 /** Logs Ctor, CopyCtor, Assign and Dtor. */
20 template<class _Trace>
21   struct Trace : public ProvideNumericId<_Trace,unsigned>
22   {
23     Trace()                            { TRACE_TAG; }
24     Trace( const Trace & )             { TRACE_TAG; }
25     ~Trace()                           { TRACE_TAG; }
26     Trace & operator=( const Trace & ) { TRACE_TAG; return *this; }
27   };
28
29 /** Data class for shared_ptr */
30 struct NonIntrusive : private Trace<NonIntrusive>
31 {
32   Trace<NonIntrusive>::numericId;
33 };
34
35 /** Data class for intrusive_ptr */
36 struct Intrusive : public ReferenceCounted,
37                    private Trace<Intrusive>
38 {
39   Trace<Intrusive>::numericId;
40 };
41
42 namespace zypp
43 {
44   template<>
45     inline NonIntrusive * rwcowClone<NonIntrusive>( const NonIntrusive * rhs )
46     { return new NonIntrusive( *rhs ); }
47
48   template<>
49     inline Intrusive * rwcowClone<Intrusive>( const Intrusive * rhs )
50     { return new Intrusive( *rhs ); }
51
52 }
53 /******************************************************************
54 **
55 */
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 )
62
63 template<class _RW>
64   void test()
65   {
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;
72     // initial NULL
73     _RW ptr;
74     T_NULL;
75     T_UNIQUE;
76     T_EQ(ptr,ptr);
77     // assign
78     ptr = _RW( new _Ptr_element_type );
79     T_NOT_NULL;
80     T_UNIQUE;
81     T_EQ(ptr,ptr);
82     {
83       // share
84       _RW ptr2( ptr );
85       T_NOT_NULL;
86       T_NOT_UNIQUE;
87       T_EQ(ptr,ptr2);
88       // unshare
89       ptr2.reset();
90       T_NOT_NULL;
91       T_UNIQUE;
92       T_NE(ptr,ptr2);
93       // different impl
94       ptr2.reset( new _Ptr_element_type );
95       T_NE(ptr,ptr2);
96    }
97     // assign
98     ptr.reset( 0 );
99     T_NULL;
100     T_UNIQUE;
101   }
102
103 template<class _RW>
104   void cowt()
105   {
106     test<_RW>();
107     MIL << __PRETTY_FUNCTION__ << std::endl;
108     typedef typename _RW::_Ptr::element_type _Ptr_element_type;
109     // create
110     _RW ptr( new _Ptr_element_type );
111     unsigned long ptrid = ptr->numericId();
112     // share
113     _RW ptr2( ptr );
114     // clone aon access
115     unsigned long ptrid2 = ptr2->numericId();
116     assert( ptrid != ptrid2 );
117   }
118
119 /******************************************************************
120 **
121 **      FUNCTION NAME : main
122 **      FUNCTION TYPE : int
123 */
124 BOOST_AUTO_TEST_CASE(basic_test)
125 {
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> > >();
131
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> > >();
136
137   MIL << "===[DONE]=====" << endl;
138 }