DGtal  0.9.3
testOwningOrAliasingPtr.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/base/OwningOrAliasingPtr.h"
35 
36 using namespace std;
37 using namespace DGtal;
38 
40 // Functions for testing class OwningOrAliasingPtr.
42 
43 template<typename Ptr, typename Value>
44 bool testAccessOperators(const Ptr& aPtr, const Value& aValue)
45 {
46  return ( (aPtr.operator->() == aPtr.get()) && (*aPtr == aValue) );
47 }
48 
49 template<typename Ptr>
50 bool testCopyAndAssignement(const Ptr& aPtr)
51 {
52  Ptr p1(aPtr); //copy
53  Ptr p2(0); //construction
54  Ptr p3 = p2; //copy
55  p3 = aPtr; //assignement
56 
57  if (aPtr.isOwning())
58  {
59  return ( ( (p1.get() != aPtr.get()) && (*p1 == *aPtr) )
60  && ( (p2.get() != aPtr.get()) && (*p2 != *aPtr) )
61  && ( (p3.get() != aPtr.get()) && (*p3 == *aPtr) ) );
62  }
63  else
64  {
65  return ( ( (p1.get() == aPtr.get()) && (*p1 == *aPtr) )
66  && ( (p2.get() != aPtr.get()) && (*p2 != *aPtr) )
67  && ( (p3.get() == aPtr.get()) && (*p3 == *aPtr) ) );
68  }
69 }
70 
76 {
77  unsigned int nbok = 0;
78  unsigned int nb = 0;
79 
80 
81  int i = 10;
82 
84 
85  trace.beginBlock ( "Owning pointer (1/2)..." );
86 
87  OwningOrAliasingPtr<int> owningPtr(i);
88  trace.info() << owningPtr << std::endl;
89  if ( (owningPtr.isOwning()) && (owningPtr.get() != &i) && (owningPtr.isValid()) )
90  nbok++;
91  nb++;
92  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
93  if ( testAccessOperators(owningPtr, i) )
94  nbok++;
95  nb++;
96  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
97  if ( testCopyAndAssignement(owningPtr) )
98  nbok++;
99  nb++;
100  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
101 
102  trace.endBlock();
103 
105 
106  trace.beginBlock ( "Owning pointer (2/2)..." );
107 
108  OwningOrAliasingPtr<int> owningPtr2(new int(10), true);
109  trace.info() << owningPtr2 << std::endl;
110  if ( (owningPtr2.isOwning()) && (owningPtr2.get() != &i) && (owningPtr2.isValid()) )
111  nbok++;
112  nb++;
113  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
114  if ( testAccessOperators(owningPtr2, *owningPtr2) )
115  nbok++;
116  nb++;
117  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
118  if ( testCopyAndAssignement(owningPtr2) )
119  nbok++;
120  nb++;
121  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
122 
123  trace.endBlock();
124 
126 
127  trace.beginBlock ( "Aliasing pointer ..." );
128 
129  OwningOrAliasingPtr<int> aliasingPtr(&i);
130  trace.info() << aliasingPtr << std::endl;
131  if ( (!aliasingPtr.isOwning()) && (aliasingPtr.get() == &i) && (aliasingPtr.isValid()) )
132  nbok++;
133  nb++;
134  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
135  if ( testAccessOperators(owningPtr, i) )
136  nbok++;
137  nb++;
138  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
139  if ( testCopyAndAssignement(owningPtr) )
140  nbok++;
141  nb++;
142  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
143 
144  trace.endBlock();
145 
146 
147  //conlusion
148  return nbok == nb;
149 }
150 
151 class DummyBigObject
152 {
153 };
154 
155 template<typename T>
156 class Dummy1
157 {
158 public:
159  OwningOrAliasingPtr<T> myPtr;
160 public:
161  Dummy1():myPtr(new T(), true) {} //default-construction (owning)
162  Dummy1(T* aPtr):myPtr(aPtr) {} //construction from an existing object (aliasing)
163 };
164 
165 template<typename T>
166 class Dummy2
167 {
168 public:
169  OwningOrAliasingPtr<T> myPtr;
170 public:
171  Dummy2(T* aPtr):myPtr(aPtr) {} //construction from an existing object (aliasing)
172  Dummy2(T data):myPtr(data) {} //construction with copy (owning)
173 };
174 
175 
181 {
182  trace.beginBlock ( "Basic usage ..." );
183 
184  //1) existing or default-constructed object
185  int obj = 5;
186  Dummy1<int> d1; //default-constructed
187  Dummy1<int> d2(&obj); //construction from an existing object
188 
189  //2) choice with/without copy
190  int smallObj = 5;
191  Dummy2<int> d3(smallObj); //small object copied
192  DummyBigObject bigObj;
193  Dummy2<DummyBigObject> d4(&bigObj); //big object not copied
194 
195  trace.endBlock();
196 
197  return true;
198 }
199 
201 // Standard services - public :
202 
203 int main( int argc, char** argv )
204 {
205  trace.beginBlock ( "Testing class OwningOrAliasingPtr" );
206  trace.info() << "Args:";
207  for ( int i = 0; i < argc; ++i )
208  trace.info() << " " << argv[ i ];
209  trace.info() << endl;
210 
211  bool res = testOwningOrAliasingPtr() && basicUsage();
212  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
213  trace.endBlock();
214  return res ? 0 : 1;
215 }
216 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:137
STL namespace.
double endBlock()
bool testCopyAndAssignement(const Ptr &aPtr)
bool basicUsage()
bool testAccessOperators(const Ptr &aPtr, const Value &aValue)
std::ostream & emphase()
int main(int argc, char **argv)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
bool testOwningOrAliasingPtr()
Aim: This class describes a smart pointer that is, given the constructor called by the user...