DGtal 1.3.0
Loading...
Searching...
No Matches
Functions
testCountedPtrOrPtr.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/CountedPtrOrPtr.h"

Go to the source code of this file.

Functions

bool testCountedPtrOrPtrMemory ()
 
int main (int argc, char **argv)
 

Detailed Description

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author
Jacques-Olivier Lachaud (jacqu.nosp@m.es-o.nosp@m.livie.nosp@m.r.la.nosp@m.chaud.nosp@m.@uni.nosp@m.v-sav.nosp@m.oie..nosp@m.fr ) Laboratory of Mathematics (CNRS, UMR 5807), University of Savoie, France
Date
2014/07/22

Functions for testing class CountedPtrOrPtr.

This file is part of the DGtal library.

Definition in file testCountedPtrOrPtr.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 234 of file testCountedPtrOrPtr.cpp.

235{
236 trace.beginBlock ( "Testing class CountedPtrOrPtr" );
237 trace.info() << "Args:";
238 for ( int i = 0; i < argc; ++i )
239 trace.info() << " " << argv[ i ];
240 trace.info() << endl;
241
242 bool res = testCountedPtrOrPtrMemory();
243 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
244 trace.endBlock();
245 return res ? 0 : 1;
246}
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Trace trace
Definition: Common.h:154
bool testCountedPtrOrPtrMemory()

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testCountedPtrOrPtrMemory(), and DGtal::trace.

◆ testCountedPtrOrPtrMemory()

bool testCountedPtrOrPtrMemory ( )

Definition at line 72 of file testCountedPtrOrPtr.cpp.

73{
74 unsigned int nbok = 0;
75 unsigned int nb = 0;
76 trace.beginBlock ( "Testing CountedPtrOrPtr memory managment..." );
77
78 trace.beginBlock ( "An invalid CountedPtrOrPtr does not create any instance." );
79 {
81 }
82 ++nb; nbok += A::nb == 0 ? 1 : 0;
83 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
85
86 trace.beginBlock ( "A CountedPtrOrPtr 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 CountedPtrOrPtr<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 ( "CountedPtrOrPtr can be used as a smart pointer with acquisition and automatic deallocation." );
106 {
107 CountedPtrOrPtr<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 ( "CountedPtrOrPtr can be initialized with = CountedPtrOrPtr<A>( pointer )." );
118 {
119 CountedPtrOrPtr<A> cptr = CountedPtrOrPtr<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 ( "CountedPtrOrPtr can be initialized with = CountedPtr<A>( pointer )." );
130 {
131 CountedPtrOrPtr<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 ( "CountedPtrOrPtr allows to share objects." );
142 {
143 CountedPtrOrPtr<A> cptr( new A( 7 ) );
144 CountedPtrOrPtr<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 ( "CountedPtrOrPtr allows to share objects with CountedPtr." );
159 {
160 CountedPtr<A> cptr( new A( 7 ) );
161 CountedPtrOrPtr<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 ( "CountedPtrOrPtr are smart wrt assignment." );
176 {
177 CountedPtrOrPtr<A> cptr( new A( 3 ) );
178 CountedPtrOrPtr<A> cptr2( new A( 12 ) );
179 ++nb; nbok += A::nb == 2 ? 1 : 0;
180 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 2" << std::endl;
181 ++nb; nbok += cptr.get() != cptr2.get() ? 1 : 0;
182 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get() != cptr2.get()" << std::endl;
183 cptr = cptr2;
184 ++nb; nbok += A::nb == 1 ? 1 : 0;
185 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 1" << std::endl;
186 ++nb; nbok += cptr.get()->a == 12 ? 1 : 0;
187 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get()->a == 12" << std::endl;
188 ++nb; nbok += cptr.get() == cptr2.get() ? 1 : 0;
189 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get() == cptr2.get()" << std::endl;
190 ++nb; nbok += cptr.count() == 2 ? 1 : 0;
191 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.count() == 2" << std::endl;
192 ++nb; nbok += cptr2.count() == 2 ? 1 : 0;
193 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr2.count() == 2" << std::endl;
194 }
195 ++nb; nbok += A::nb == 0 ? 1 : 0;
196 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
197 trace.endBlock();
198
199 trace.beginBlock ( "CountedPtrOrPtr and CountedPtr are smart wrt assignment." );
200 {
201 CountedPtrOrPtr<A> cptr( new A( 3 ) );
202 CountedPtr<A> cptr2( new A( 12 ) );
203 ++nb; nbok += A::nb == 2 ? 1 : 0;
204 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 2" << std::endl;
205 ++nb; nbok += cptr.get() != cptr2.get() ? 1 : 0;
206 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get() != cptr2.get()" << std::endl;
207 cptr = cptr2;
208 ++nb; nbok += A::nb == 1 ? 1 : 0;
209 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 1" << std::endl;
210 ++nb; nbok += cptr.get()->a == 12 ? 1 : 0;
211 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get()->a == 12" << std::endl;
212 ++nb; nbok += cptr.get() == cptr2.get() ? 1 : 0;
213 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get() == cptr2.get()" << std::endl;
214 cptr.get()->a = 5; // does compile.
215 ++nb; nbok += cptr.get()->a == 5 ? 1 : 0;
216 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.get()->a == 5" << std::endl;
217 ++nb; nbok += cptr.count() == 2 ? 1 : 0;
218 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr.count() == 2" << std::endl;
219 ++nb; nbok += cptr2.count() == 2 ? 1 : 0;
220 trace.info() << "(" << nbok << "/" << nb << ") " << "cptr2.count() == 2" << std::endl;
221 }
222 ++nb; nbok += A::nb == 0 ? 1 : 0;
223 trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
224 trace.endBlock();
225
226 trace.endBlock();
227 return nb == nbok;
228}
Aim: Smart or simple pointer on T. It can be a smart pointer based on reference counts or a simple po...
T * get() const noexcept
unsigned int count() const
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80

References DGtal::Trace::beginBlock(), DGtal::CountedPtr< T >::count(), DGtal::CountedPtrOrPtr< T >::count(), DGtal::Trace::endBlock(), DGtal::CountedPtr< T >::get(), DGtal::CountedPtrOrPtr< T >::get(), DGtal::Trace::info(), DGtal::CountedPtrOrPtr< T >::isSimple(), DGtal::CountedPtrOrPtr< T >::isSmart(), and DGtal::trace.

Referenced by main().