DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testCountedPtr.cpp
1 
30 
31 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/base/CountedPtr.h"
35 
36 using namespace std;
37 using namespace DGtal;
38 
40 // Functions for testing class CountedPtr.
42 
46 bool testCountedPtr()
47 {
48  unsigned int nbok = 0;
49  unsigned int nb = 0;
50 
51  trace.beginBlock ( "Testing CountedPtr ..." );
52 
53  int *value=new int(5);
54  CountedPtr<int> p( value );
55  nbok += p.unique() ? 1 : 0;
56  nb++;
57  trace.info() << p << " value=" << *p<< std::endl;
58  trace.info() << "(" << nbok << "/" << nb << ") "
59  << "unique" << std::endl;
60 
61  *p = 6;
62  trace.info() << p << " value=" << *p<< std::endl;
63  nbok += p.unique() ? 1 : 0;
64  nb++;
65  trace.info() << "(" << nbok << "/" << nb << ") "
66  << "unique" << std::endl;
67 
68  trace.endBlock();
69 
70  return nbok == nb;
71 }
72 
73 
74 bool testCountedPtrCopy()
75 {
76  unsigned int nbok = 0;
77  unsigned int nb = 0;
78 
79  trace.beginBlock ( "Testing CountedPtr copy..." );
80 
81  int *value= new int(5);
82  CountedPtr<int> p( value );
83  nbok += p.unique() ? 1 : 0;
84  nb++;
85  trace.info() << p <<std::endl;
86  trace.info() << "(" << nbok << "/" << nb << ") "
87  << "unique" << std::endl;
88 
89  CountedPtr<int> q ( p );
90 
91  nbok += p.unique() ? 0: 1;
92  nb++;
93  trace.info() << p <<std::endl;
94  trace.info() << q<<std::endl;
95  trace.info() << "(" << nbok << "/" << nb << ") "
96  << "not unique anymore" << std::endl;
97 
98 
99  trace.endBlock();
100 
101  return nbok == nb;
102 }
103 
104 
106 // Standard services - public :
107 
108 int main( int argc, char** argv )
109 {
110  trace.beginBlock ( "Testing class CountedPtr" );
111  trace.info() << "Args:";
112  for ( int i = 0; i < argc; ++i )
113  trace.info() << " " << argv[ i ];
114  trace.info() << endl;
115 
116  bool res = testCountedPtr() && testCountedPtrCopy(); // && ... other tests
117  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
118  trace.endBlock();
119  return res ? 0 : 1;
120 }
121 // //