DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
testCountedConstPtrOrConstPtr.cpp
Go to the documentation of this file.
1
16
29
31
#include <iostream>
32
#include "DGtal/base/Common.h"
33
#include "DGtal/base/CountedConstPtrOrConstPtr.h"
35
36
using namespace
std
;
37
using namespace
DGtal
;
38
40
// Functions for testing class CountedConstPtrOrConstPtr.
42
43
struct
A
{
44
A
(
int
_a ) :
a
( _a )
45
{
46
++
nb
;
47
trace
.
info
() <<
"#"
<<
nb
<<
" A::A( int ), a is "
<<
a
<< std::endl;
48
}
49
A
(
const
A
& other ) :
a
( other.
a
)
50
{
51
++
nb
;
52
trace
.
info
() <<
"#"
<<
nb
<<
" A::A( const A& ), a is "
<<
a
<< std::endl;
53
}
54
A
&
operator=
(
const
A
& other )
55
{
56
if
(
this
!= &other )
57
a
= other.
a
;
58
trace
.
info
() <<
"#"
<<
nb
<<
" A::op=( const A& ), a is "
<<
a
<< std::endl;
59
return
*
this
;
60
}
61
~A
()
62
{
63
--
nb
;
64
trace
.
info
() <<
"#"
<<
nb
<<
" A::~A(), a was "
<<
a
<< std::endl;
65
}
66
static
int
nb
;
67
int
a
;
68
};
69
70
int
A::nb
= 0;
71
72
bool
testCountedConstPtrOrConstPtrMemory
()
73
{
74
unsigned
int
nbok = 0;
75
unsigned
int
nb = 0;
76
trace
.
beginBlock
(
"Testing CountedConstPtrOrConstPtr memory management..."
);
77
78
trace
.
beginBlock
(
"An invalid CountedConstPtrOrConstPtr does not create any instance."
);
79
{
80
CountedConstPtrOrConstPtr<A>
cptr;
81
}
82
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
83
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
84
trace
.
endBlock
();
85
86
trace
.
beginBlock
(
"A CountedConstPtrOrConstPtr can be used as a simple pointer on an object without acquiring it."
);
87
{
88
A
a( 17 );
89
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
90
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
91
{
92
CountedConstPtrOrConstPtr<A>
cptr( &a,
false
);
93
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
94
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
95
++nb; nbok += cptr.
isSimple
() ? 1 : 0;
96
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.isSimple()"
<< std::endl;
97
}
98
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
99
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
100
}
101
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
102
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
103
trace
.
endBlock
();
104
105
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr can be used as a smart pointer with acquisition and automatic deallocation."
);
106
{
107
CountedConstPtrOrConstPtr<A>
cptr(
new
A
( 10 ) );
108
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
109
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
110
++nb; nbok += cptr.
isSmart
() ? 1 : 0;
111
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.isSmart()"
<< std::endl;
112
}
113
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
114
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
115
trace
.
endBlock
();
116
117
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr can be initialized with = CountedConstPtrOrConstPtr<A>( pointer )."
);
118
{
119
CountedConstPtrOrConstPtr<A>
cptr =
CountedConstPtrOrConstPtr<A>
(
new
A
( 5 ) );
120
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
121
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
122
++nb; nbok += cptr.
isSmart
() ? 1 : 0;
123
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.isSmart()"
<< std::endl;
124
}
125
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
126
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
127
trace
.
endBlock
();
128
129
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr can be initialized with = CountedPtr<A>( pointer )."
);
130
{
131
CountedConstPtrOrConstPtr<A>
cptr =
CountedPtr<A>
(
new
A
( 5 ) );
132
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
133
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
134
++nb; nbok += cptr.
isSmart
() ? 1 : 0;
135
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.isSmart()"
<< std::endl;
136
}
137
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
138
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
139
trace
.
endBlock
();
140
141
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr allows to share objects."
);
142
{
143
CountedConstPtrOrConstPtr<A>
cptr(
new
A
( 7 ) );
144
CountedConstPtrOrConstPtr<A>
cptr2 = cptr;
145
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
146
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
147
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
148
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
149
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
150
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
151
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
152
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
153
}
154
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
155
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
156
trace
.
endBlock
();
157
158
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr allows to share objects with CountedPtr."
);
159
{
160
CountedPtr<A>
cptr(
new
A
( 7 ) );
161
CountedConstPtrOrConstPtr<A>
cptr2 = cptr;
162
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
163
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
164
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
165
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
166
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
167
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
168
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
169
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
170
}
171
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
172
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
173
trace
.
endBlock
();
174
175
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr allows to share objects with CountedPtrOrPtr."
);
176
{
177
CountedPtrOrPtr<A>
cptr(
new
A
( 7 ) );
178
CountedConstPtrOrConstPtr<A>
cptr2 = cptr;
179
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
180
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
181
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
182
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
183
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
184
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
185
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
186
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
187
}
188
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
189
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
190
trace
.
endBlock
();
191
192
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr are smart wrt assignment."
);
193
{
194
CountedConstPtrOrConstPtr<A>
cptr(
new
A
( 3 ) );
195
CountedConstPtrOrConstPtr<A>
cptr2(
new
A
( 12 ) );
196
++nb; nbok +=
A::nb
== 2 ? 1 : 0;
197
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 2"
<< std::endl;
198
++nb; nbok += cptr.
get
() != cptr2.
get
() ? 1 : 0;
199
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() != cptr2.get()"
<< std::endl;
200
cptr = cptr2;
201
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
202
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
203
++nb; nbok += cptr.
get
()->a == 12 ? 1 : 0;
204
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get()->a == 12"
<< std::endl;
205
// cptr.get()->a = 5; // does not compile.
206
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
207
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
208
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
209
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
210
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
211
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
212
}
213
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
214
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
215
trace
.
endBlock
();
216
217
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr and CountedPtr are smart wrt assignment."
);
218
{
219
CountedConstPtrOrConstPtr<A>
cptr(
new
A
( 3 ) );
220
CountedPtr<A>
cptr2(
new
A
( 12 ) );
221
++nb; nbok +=
A::nb
== 2 ? 1 : 0;
222
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 2"
<< std::endl;
223
++nb; nbok += cptr.
get
() != cptr2.
get
() ? 1 : 0;
224
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() != cptr2.get()"
<< std::endl;
225
cptr = cptr2;
226
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
227
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
228
++nb; nbok += cptr.
get
()->a == 12 ? 1 : 0;
229
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get()->a == 12"
<< std::endl;
230
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
231
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
232
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
233
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
234
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
235
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
236
}
237
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
238
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
239
trace
.
endBlock
();
240
241
trace
.
beginBlock
(
"CountedConstPtrOrConstPtr and CountedPtrOrPtr are smart wrt assignment."
);
242
{
243
CountedConstPtrOrConstPtr<A>
cptr(
new
A
( 3 ) );
244
CountedPtrOrPtr<A>
cptr2(
new
A
( 12 ) );
245
++nb; nbok +=
A::nb
== 2 ? 1 : 0;
246
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 2"
<< std::endl;
247
++nb; nbok += cptr.
get
() != cptr2.
get
() ? 1 : 0;
248
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() != cptr2.get()"
<< std::endl;
249
cptr = cptr2;
250
++nb; nbok +=
A::nb
== 1 ? 1 : 0;
251
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 1"
<< std::endl;
252
++nb; nbok += cptr.
get
()->a == 12 ? 1 : 0;
253
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get()->a == 12"
<< std::endl;
254
++nb; nbok += cptr.
get
() == cptr2.
get
() ? 1 : 0;
255
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.get() == cptr2.get()"
<< std::endl;
256
++nb; nbok += cptr.
count
() == 2 ? 1 : 0;
257
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr.count() == 2"
<< std::endl;
258
++nb; nbok += cptr2.
count
() == 2 ? 1 : 0;
259
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"cptr2.count() == 2"
<< std::endl;
260
}
261
++nb; nbok +=
A::nb
== 0 ? 1 : 0;
262
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<<
"A::nb == 0"
<< std::endl;
263
trace
.
endBlock
();
264
265
trace
.
endBlock
();
266
return
nb == nbok;
267
}
268
269
271
// Standard services - public :
272
273
int
main
(
int
argc,
char
** argv )
274
{
275
trace
.
beginBlock
(
"Testing class CountedConstPtrOrConstPtr"
);
276
trace
.
info
() <<
"Args:"
;
277
for
(
int
i = 0; i < argc; ++i )
278
trace
.
info
() <<
" "
<< argv[ i ];
279
trace
.
info
() << endl;
280
281
bool
res =
testCountedConstPtrOrConstPtrMemory
();
282
trace
.
emphase
() << ( res ?
"Passed."
:
"Error."
) << endl;
283
trace
.
endBlock
();
284
return
res ? 0 : 1;
285
}
286
// //
DGtal::CountedConstPtrOrConstPtr
Aim: Smart or simple const pointer on T. It can be a smart pointer based on reference counts or a sim...
Definition
CountedConstPtrOrConstPtr.h:95
DGtal::CountedConstPtrOrConstPtr::CountedConstPtrOrConstPtr
CountedConstPtrOrConstPtr(const T *p=0, bool isCountedPtr=true)
Definition
CountedConstPtrOrConstPtr.h:119
DGtal::CountedConstPtrOrConstPtr::isSimple
bool isSimple() const
Definition
CountedConstPtrOrConstPtr.h:262
DGtal::CountedConstPtrOrConstPtr::isSmart
bool isSmart() const
Definition
CountedConstPtrOrConstPtr.h:254
DGtal::CountedConstPtrOrConstPtr::count
unsigned int count() const
Definition
CountedConstPtrOrConstPtr.h:351
DGtal::CountedConstPtrOrConstPtr::get
const T * get() const noexcept
Definition
CountedConstPtrOrConstPtr.h:327
DGtal::CountedPtrOrPtr
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
Definition
CountedPtrOrPtr.h:96
DGtal::CountedPtrOrPtr::get
T * get() const noexcept
Definition
CountedPtrOrPtr.h:290
DGtal::CountedPtrOrPtr::count
unsigned int count() const
Definition
CountedPtrOrPtr.h:314
DGtal::CountedPtr
Aim: Smart pointer based on reference counts.
Definition
CountedPtr.h:80
DGtal::CountedPtr::get
T * get() const noexcept
Definition
CountedPtr.h:195
DGtal::CountedPtr::count
unsigned int count() const
Definition
CountedPtr.h:236
DGtal::CountedPtr::CountedPtr
CountedPtr(T *p=0)
Definition
CountedPtr.h:119
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
std
STL namespace.
A
Definition
testCountedConstPtrOrConstPtr.cpp:43
A< TC, int >::a
int a
Definition
testCountedConstPtrOrConstPtr.cpp:67
A::operator=
A & operator=(const A &other)
Definition
testCountedConstPtrOrConstPtr.cpp:54
A< TC, int >::nb
static int nb
Definition
testCountedConstPtrOrConstPtr.cpp:66
A::A
A(int _a)
Definition
testCountedConstPtrOrConstPtr.cpp:44
A::~A
~A()
Definition
testCountedConstPtrOrConstPtr.cpp:61
A::A
A(const A &other)
Definition
testCountedConstPtrOrConstPtr.cpp:49
testCountedConstPtrOrConstPtrMemory
bool testCountedConstPtrOrConstPtrMemory()
Definition
testCountedConstPtrOrConstPtr.cpp:72
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
tests
base
testCountedConstPtrOrConstPtr.cpp
Generated on
for DGtal by
1.18.0