DGtal  1.5.beta
testCountedPtr.cpp
Go to the documentation of this file.
1 
33 #include <iostream>
34 #include "DGtal/base/Common.h"
35 #include "DGtal/base/CountedPtr.h"
37 
38 using namespace std;
39 using namespace DGtal;
40 
42 // Functions for testing class CountedPtr.
44 
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 }
74 
75 
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 }
105 
106 struct A {
107  A( int _a ) : a( _a )
108  {
109  ++nb;
110  trace.info() << "#" << nb << " A::A( int ), a is " << a << std::endl;
111  }
112  A( const A& other ) : a( other.a )
113  {
114  ++nb;
115  trace.info() << "#" << nb << " A::A( const A& ), a is " << a << std::endl;
116  }
117  A& operator=( const A& other )
118  {
119  if ( this != &other )
120  a = other.a;
121  trace.info() << "#" << nb << " A::op=( const A& ), a is " << a << std::endl;
122  return *this;
123  }
124  ~A()
125  {
126  --nb;
127  trace.info() << "#" << nb << " A::~A(), a was " << a << std::endl;
128  }
129  static int nb;
130  int a;
131 };
132 
133 int A::nb = 0;
134 
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 }
213 
214 
216 // Standard services - public :
217 
218 int main( int argc, char** argv )
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 }
233 // //
Aim: Smart pointer based on reference counts.
Definition: CountedPtr.h:80
T * get() const noexcept
Definition: CountedPtr.h:195
unsigned int count() const
Definition: CountedPtr.h:236
void beginBlock(const std::string &keyword="")
std::ostream & emphase()
std::ostream & info()
double endBlock()
DGtal is the top-level namespace which contains all DGtal functions and types.
Trace trace
Definition: Common.h:153
int main(int argc, char **argv)
bool testCountedPtr()
bool testCountedPtrMemory()
bool testCountedPtrCopy()