34#include "DGtal/base/Common.h"
35#include "DGtal/base/CountedPtr.h"
50 unsigned int nbok = 0;
53 trace.beginBlock (
"Testing CountedPtr ..." );
55 int *value=
new int(5);
57 nbok += p.
unique() ? 1 : 0;
59 trace.info() << p <<
" value=" << *p<< std::endl;
60 trace.info() <<
"(" << nbok <<
"/" << nb <<
") "
61 <<
"unique" << std::endl;
64 trace.info() << p <<
" value=" << *p<< std::endl;
65 nbok += p.
unique() ? 1 : 0;
67 trace.info() <<
"(" << nbok <<
"/" << nb <<
") "
68 <<
"unique" << std::endl;
78 unsigned int nbok = 0;
81 trace.beginBlock (
"Testing CountedPtr copy..." );
83 int *value=
new int(5);
85 nbok += p.
unique() ? 1 : 0;
87 trace.info() << p <<std::endl;
88 trace.info() <<
"(" << nbok <<
"/" << nb <<
") "
89 <<
"unique" << std::endl;
95 trace.info() << p <<std::endl;
96 trace.info() << q<<std::endl;
97 trace.info() <<
"(" << nbok <<
"/" << nb <<
") "
98 <<
"not unique anymore" << std::endl;
107 A(
int _a ) :
a( _a )
110 trace.info() <<
"#" <<
nb <<
" A::A( int ), a is " <<
a << std::endl;
112 A(
const A& other ) :
a( other.
a )
115 trace.info() <<
"#" <<
nb <<
" A::A( const A& ), a is " <<
a << std::endl;
112 A(
const A& other ) :
a( other.
a ) {
…}
119 if (
this != &other )
121 trace.info() <<
"#" <<
nb <<
" A::op=( const A& ), a is " <<
a << std::endl;
127 trace.info() <<
"#" <<
nb <<
" A::~A(), a was " <<
a << std::endl;
137 unsigned int nbok = 0;
139 trace.beginBlock (
"Testing CountedPtr memory managment..." );
141 trace.beginBlock (
"An invalid CountedPtr does not create any instance." );
145 ++nb; nbok +=
A::nb == 0 ? 1 : 0;
146 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
149 trace.beginBlock (
"CountedPtr can be used as a simple pointer with automatic deallocation." );
152 ++nb; nbok +=
A::nb == 1 ? 1 : 0;
153 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
155 ++nb; nbok +=
A::nb == 0 ? 1 : 0;
156 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
159 trace.beginBlock (
"CountedPtr can be initialized with = CountedPtr<A>( pointer )." );
162 ++nb; nbok +=
A::nb == 1 ? 1 : 0;
163 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
165 ++nb; nbok +=
A::nb == 0 ? 1 : 0;
166 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
169 trace.beginBlock (
"CountedPtr allows to share objects." );
173 ++nb; nbok +=
A::nb == 1 ? 1 : 0;
174 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
175 ++nb; nbok += cptr.
get() == cptr2.
get() ? 1 : 0;
176 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() == cptr2.get()" << std::endl;
177 ++nb; nbok += cptr.
count() == 2 ? 1 : 0;
178 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.count() == 2" << std::endl;
179 ++nb; nbok += cptr2.
count() == 2 ? 1 : 0;
180 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr2.count() == 2" << std::endl;
182 ++nb; nbok +=
A::nb == 0 ? 1 : 0;
183 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
186 trace.beginBlock (
"CountedPtr are smart wrt assignment." );
190 ++nb; nbok +=
A::nb == 2 ? 1 : 0;
191 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 2" << std::endl;
192 ++nb; nbok += cptr.
get() != cptr2.
get() ? 1 : 0;
193 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() != cptr2.get()" << std::endl;
195 ++nb; nbok +=
A::nb == 1 ? 1 : 0;
196 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 1" << std::endl;
197 ++nb; nbok += cptr.
get()->a == 12 ? 1 : 0;
198 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get()->a == 12" << std::endl;
199 ++nb; nbok += cptr.
get() == cptr2.
get() ? 1 : 0;
200 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.get() == cptr2.get()" << std::endl;
201 ++nb; nbok += cptr.
count() == 2 ? 1 : 0;
202 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr.count() == 2" << std::endl;
203 ++nb; nbok += cptr2.
count() == 2 ? 1 : 0;
204 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"cptr2.count() == 2" << std::endl;
206 ++nb; nbok +=
A::nb == 0 ? 1 : 0;
207 trace.info() <<
"(" << nbok <<
"/" << nb <<
") " <<
"A::nb == 0" << std::endl;
218int main(
int argc,
char** argv )
220 trace.beginBlock (
"Testing class CountedPtr" );
221 trace.info() <<
"Args:";
222 for (
int i = 0; i < argc; ++i )
223 trace.info() <<
" " << argv[ i ];
224 trace.info() << endl;
229 trace.emphase() << ( res ?
"Passed." :
"Error." ) << endl;
218int main(
int argc,
char** argv ) {
…}
Aim: Smart pointer based on reference counts.
bool unique() const noexcept
unsigned int count() const
DGtal is the top-level namespace which contains all DGtal functions and types.
A & operator=(const A &other)
bool testCountedPtrMemory()
bool testCountedPtrCopy()