DGtal  1.4.2
testCountedPtr.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/CountedPtr.h"
Include dependency graph for testCountedPtr.cpp:

Go to the source code of this file.

Functions

bool testCountedPtr ()
 
bool testCountedPtrCopy ()
 
bool testCountedPtrMemory ()
 
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
David Coeurjolly (david.nosp@m..coe.nosp@m.urjol.nosp@m.ly@l.nosp@m.iris..nosp@m.cnrs.nosp@m..fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
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
2012/05/31

Functions for testing class CountedPtr.

This file is part of the DGtal library.

Definition in file testCountedPtr.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 218 of file testCountedPtr.cpp.

219 {
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;
225 
226  bool res = testCountedPtr()
227  && testCountedPtrCopy()
229  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
230  trace.endBlock();
231  return res ? 0 : 1;
232 }
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
Trace trace
Definition: Common.h:153
bool testCountedPtr()
bool testCountedPtrMemory()
bool testCountedPtrCopy()

References DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testCountedPtr(), testCountedPtrCopy(), testCountedPtrMemory(), and DGtal::trace.

◆ testCountedPtr()

bool testCountedPtr ( )

Example of a test. To be completed.

Definition at line 48 of file testCountedPtr.cpp.

49 {
50  unsigned int nbok = 0;
51  unsigned int nb = 0;
52 
53  trace.beginBlock ( "Testing CountedPtr ..." );
54 
55  int *value=new int(5);
56  CountedPtr<int> p( value );
57  nbok += p.unique() ? 1 : 0;
58  nb++;
59  trace.info() << p << " value=" << *p<< std::endl;
60  trace.info() << "(" << nbok << "/" << nb << ") "
61  << "unique" << std::endl;
62 
63  *p = 6;
64  trace.info() << p << " value=" << *p<< std::endl;
65  nbok += p.unique() ? 1 : 0;
66  nb++;
67  trace.info() << "(" << nbok << "/" << nb << ") "
68  << "unique" << std::endl;
69 
70  trace.endBlock();
71 
72  return nbok == nb;
73 }
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80

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

Referenced by main().

◆ testCountedPtrCopy()

bool testCountedPtrCopy ( )

Definition at line 76 of file testCountedPtr.cpp.

77 {
78  unsigned int nbok = 0;
79  unsigned int nb = 0;
80 
81  trace.beginBlock ( "Testing CountedPtr copy..." );
82 
83  int *value= new int(5);
84  CountedPtr<int> p( value );
85  nbok += p.unique() ? 1 : 0;
86  nb++;
87  trace.info() << p <<std::endl;
88  trace.info() << "(" << nbok << "/" << nb << ") "
89  << "unique" << std::endl;
90 
91  CountedPtr<int> q ( p );
92 
93  nbok += p.unique() ? 0: 1;
94  nb++;
95  trace.info() << p <<std::endl;
96  trace.info() << q<<std::endl;
97  trace.info() << "(" << nbok << "/" << nb << ") "
98  << "not unique anymore" << std::endl;
99 
100 
101  trace.endBlock();
102 
103  return nbok == nb;
104 }

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

Referenced by main().

◆ testCountedPtrMemory()

bool testCountedPtrMemory ( )

Definition at line 135 of file testCountedPtr.cpp.

136 {
137  unsigned int nbok = 0;
138  unsigned int nb = 0;
139  trace.beginBlock ( "Testing CountedPtr memory managment..." );
140 
141  trace.beginBlock ( "An invalid CountedPtr does not create any instance." );
142  {
143  CountedPtr<A> cptr;
144  }
145  ++nb; nbok += A::nb == 0 ? 1 : 0;
146  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
147  trace.endBlock();
148 
149  trace.beginBlock ( "CountedPtr can be used as a simple pointer with automatic deallocation." );
150  {
151  CountedPtr<A> cptr( new A( 10 ) );
152  ++nb; nbok += A::nb == 1 ? 1 : 0;
153  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 1" << std::endl;
154  }
155  ++nb; nbok += A::nb == 0 ? 1 : 0;
156  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
157  trace.endBlock();
158 
159  trace.beginBlock ( "CountedPtr can be initialized with = CountedPtr<A>( pointer )." );
160  {
161  CountedPtr<A> cptr = CountedPtr<A>( new A( 5 ) );
162  ++nb; nbok += A::nb == 1 ? 1 : 0;
163  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 1" << std::endl;
164  }
165  ++nb; nbok += A::nb == 0 ? 1 : 0;
166  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
167  trace.endBlock();
168 
169  trace.beginBlock ( "CountedPtr allows to share objects." );
170  {
171  CountedPtr<A> cptr( new A( 7 ) );
172  CountedPtr<A> cptr2 = cptr;
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;
181  }
182  ++nb; nbok += A::nb == 0 ? 1 : 0;
183  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
184  trace.endBlock();
185 
186  trace.beginBlock ( "CountedPtr are smart wrt assignment." );
187  {
188  CountedPtr<A> cptr( new A( 3 ) );
189  CountedPtr<A> cptr2( new A( 12 ) );
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;
194  cptr = cptr2;
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;
205  }
206  ++nb; nbok += A::nb == 0 ? 1 : 0;
207  trace.info() << "(" << nbok << "/" << nb << ") " << "A::nb == 0" << std::endl;
208  trace.endBlock();
209 
210  trace.endBlock();
211  return nb == nbok;
212 }
T * get() const noexcept
Definition: CountedPtr.h:195
unsigned int count() const
Definition: CountedPtr.h:236

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

Referenced by main().