Imported Upstream version 14.45.0
[platform/upstream/libzypp.git] / tests / zypp / PtrTypes_test.cc
1 #include <iostream>
2
3 #include <boost/test/auto_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   using Trace<NonIntrusive>::numericId;
33 };
34
35 /** Data class for intrusive_ptr */
36 struct Intrusive : public ReferenceCounted,
37                    private Trace<Intrusive>
38 {
39   using 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 */
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() );
64
65 template<class _RW>
66   void test()
67   {
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;
74     // initial NULL
75     _RW ptr;
76     T_NULL;
77     T_UNIQUE;
78     T_EQ(ptr,ptr);
79     // assign
80     ptr = _RW( new _Ptr_element_type );
81     T_NOT_NULL;
82     T_UNIQUE;
83     T_EQ(ptr,ptr);
84     {
85       // share
86       _RW ptr2( ptr );
87       T_NOT_NULL;
88       T_NOT_UNIQUE;
89       T_EQ(ptr,ptr2);
90       // unshare
91       ptr2.reset();
92       T_NOT_NULL;
93       T_UNIQUE;
94       T_NE(ptr,ptr2);
95       // different impl
96       ptr2.reset( new _Ptr_element_type );
97       T_NE(ptr,ptr2);
98    }
99     // assign
100     ptr.reset( 0 );
101     T_NULL;
102     T_UNIQUE;
103     // nullptr compatible
104     ptr.reset( nullptr );
105     T_NULL;
106     T_UNIQUE;
107     ptr = nullptr;
108     T_NULL;
109     T_UNIQUE;
110     ptr = _RW( nullptr );
111     T_NULL;
112     T_UNIQUE;
113
114
115   }
116
117 template<class _RW>
118   void cowt()
119   {
120     test<_RW>();
121     MIL << __PRETTY_FUNCTION__ << std::endl;
122     typedef typename _RW::_Ptr::element_type _Ptr_element_type;
123     // create
124     _RW ptr( new _Ptr_element_type );
125     unsigned long ptrid = ptr->numericId();
126     // share
127     _RW ptr2( ptr );
128     // clone aon access
129     unsigned long ptrid2 = ptr2->numericId();
130     assert( ptrid != ptrid2 );
131   }
132
133 /******************************************************************
134 **
135 **      FUNCTION NAME : main
136 **      FUNCTION TYPE : int
137 */
138 BOOST_AUTO_TEST_CASE(basic_test)
139 {
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> > >();
145
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> > >();
150
151   MIL << "===[DONE]=====" << endl;
152 }