DGtal  1.2.beta
Functions
testOwningOrAliasingPtr.cpp File Reference
#include <iostream>
#include "DGtal/base/Common.h"
#include "DGtal/base/OwningOrAliasingPtr.h"
Include dependency graph for testOwningOrAliasingPtr.cpp:

Go to the source code of this file.

Functions

template<typename Ptr , typename Value >
bool testAccessOperators (const Ptr &aPtr, const Value &aValue)
 
template<typename Ptr >
bool testCopyAndAssignement (const Ptr &aPtr)
 
bool testOwningOrAliasingPtr ()
 
bool basicUsage ()
 
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
Tristan Roussillon (trist.nosp@m.an.r.nosp@m.oussi.nosp@m.llon.nosp@m.@liri.nosp@m.s.cn.nosp@m.rs.fr ) Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
Date
2012/11/14

Functions for testing class OwningOrAliasingPtr.

This file is part of the DGtal library.

Definition in file testOwningOrAliasingPtr.cpp.

Function Documentation

◆ basicUsage()

bool basicUsage ( )

Basic usage

Examples:
geometry/tools/determinant/exampleInHalfPlane.cpp.

Definition at line 180 of file testOwningOrAliasingPtr.cpp.

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

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:150
double endBlock()

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 203 of file testOwningOrAliasingPtr.cpp.

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

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:150
double endBlock()
bool basicUsage()
std::ostream & emphase()
std::ostream & info()
bool testOwningOrAliasingPtr()

◆ testAccessOperators()

template<typename Ptr , typename Value >
bool testAccessOperators ( const Ptr &  aPtr,
const Value aValue 
)

Definition at line 44 of file testOwningOrAliasingPtr.cpp.

Referenced by testOwningOrAliasingPtr().

45 {
46  return ( (aPtr.operator->() == aPtr.get()) && (*aPtr == aValue) );
47 }

◆ testCopyAndAssignement()

template<typename Ptr >
bool testCopyAndAssignement ( const Ptr &  aPtr)

Definition at line 50 of file testOwningOrAliasingPtr.cpp.

Referenced by testOwningOrAliasingPtr().

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 }

◆ testOwningOrAliasingPtr()

bool testOwningOrAliasingPtr ( )

Unit tests

Definition at line 75 of file testOwningOrAliasingPtr.cpp.

References DGtal::Trace::beginBlock(), DGtal::Trace::endBlock(), DGtal::OwningOrAliasingPtr< T >::get(), DGtal::Trace::info(), DGtal::OwningOrAliasingPtr< T >::isOwning(), DGtal::OwningOrAliasingPtr< T >::isValid(), testAccessOperators(), testCopyAndAssignement(), and DGtal::trace.

Referenced by main().

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 }
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:150
double endBlock()
bool testCopyAndAssignement(const Ptr &aPtr)
bool testAccessOperators(const Ptr &aPtr, const Value &aValue)
std::ostream & info()
Aim: This class describes a smart pointer that is, given the constructor called by the user...