DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
testCirculator.cpp
1 
30 
31 #include <iostream>
32 #include <vector>
33 #include "DGtal/base/Common.h"
34 #include "DGtal/base/Circulator.h"
36 
37 using namespace std;
38 using namespace DGtal;
39 
40 
42 // Functions for testing class Circulator.
44 
49 template<typename Iterator>
50 bool testOffset(const Iterator& itb, const Iterator& ite, const vector<int>& groundTruth)
51 {
52 
53  BOOST_CONCEPT_ASSERT(( boost::BidirectionalIterator<Iterator> ));
54  BOOST_CONCEPT_ASSERT(( boost::BidirectionalIterator< Circulator<Iterator> > ));
55 
56  //list
57  cout << endl;
58  copy(itb,ite,ostream_iterator<int>(cout, " "));
59  cout << " => ";
60 
61  //use of Circulators
62  vector<int> v;
63  Circulator<Iterator> cb( itb, itb, ite );
64  Circulator<Iterator> c( ++cb );
65  do {
66  v.push_back(*c);
67  c++;
68  } while (c != cb);
69 
70  //offset list
71  copy(v.begin(),v.end(),ostream_iterator<int>(cout, " "));
72 
73  //ground truth
74  cout << " ( == ";
75  copy(groundTruth.begin(),groundTruth.end(),ostream_iterator<int>(cout, " "));
76  cout << ")" << endl;
77 
78  return equal( v.begin(),v.end(),groundTruth.begin() );
79 }
80 
85 template<typename Type1, typename Type2>
86 bool
87 testComparison(const Type1& a, const Type2& b) {
88  return (a == b);
89 }
90 
95 template<typename Iterator>
96 bool testIsNotEmpty(const Iterator& itb, const Iterator& ite, const bool& aFlagIsNotEmpty)
97 {
98  return (isNotEmpty(itb,ite) == aFlagIsNotEmpty );
99 }
100 
101 template< typename IC>
102 inline
103 bool getGeneralTag( const IC& /*ic*/){
104  cout << typeid( typename IteratorCirculatorTraits<IC>::Category() ).name() << endl;
105  return true;
106 }
107 
108 template< typename IC>
109 inline
110 bool getSpecificTag( const IC& /*ic*/){
111  cout << typeid( typename IC::iterator_category() ).name() << endl;
112  return true;
113 }
114 
115 
116 template< typename IC >
117 inline
118 bool getType( const IC& , IteratorType ) {
119  cout << "IteratorType" << endl;
120  return true;
121 }
122 
123 template< typename IC >
124 inline
125 bool getType( const IC& , CirculatorType ) {
126  cout << "CirculatorType" << endl;
127  return true;
128 }
129 
130 template< typename IC>
131 inline
132 bool getType( const IC& ic){
133  return getType<IC>( ic, typename IteratorCirculatorTraits<IC>::Type() );
134 }
135 
137 // Standard services - public :
138 
139 int main( int argc, char** argv )
140 {
141  trace.beginBlock ( "Testing class BasicBoolFunctions" );
142  trace.info() << "Args:";
143  for ( int i = 0; i < argc; ++i )
144  trace.info() << " " << argv[ i ];
145  trace.info() << endl;
146 
147  vector<int> v;
148  v.push_back(1);
149  v.push_back(2);
150  v.push_back(3);
151  v.push_back(4);
152  v.push_back(5);
153 
154  vector<int> v2;
155  v2.push_back(2);
156  v2.push_back(3);
157  v2.push_back(4);
158  v2.push_back(5);
159  v2.push_back(1);
160 
161  vector<int> v3;
162  v3.push_back(4);
163  v3.push_back(3);
164  v3.push_back(2);
165  v3.push_back(1);
166  v3.push_back(5);
167 
168 //incrementation / decrementation
169  trace.info() << endl;
170  trace.info() << "Iterate" << endl;
171  bool res = testOffset<vector<int>::iterator>(v.begin(),v.end(), v2)
172  && testOffset<vector<int>::reverse_iterator>(v.rbegin(),v.rend(), v3)
173  && testOffset<vector<int>::const_iterator>(v.begin(),v.end(), v2)
174  && testOffset<vector<int>::const_reverse_iterator>(v.rbegin(),v.rend(), v3);
175 
176 //comparisons
177  trace.info() << endl;
178  trace.info() << "Compare" << endl;
179  trace.info() << "(const / not const)" << endl;
180  Circulator<vector<int>::iterator> c1( v.begin(), v.begin(), v.end() );
183  res = res && testComparison<Circulator<vector<int>::iterator>,Circulator<vector<int>::iterator> >(c1,c2)
184  && testComparison<Circulator<vector<int>::iterator>,Circulator<vector<int>::const_iterator> >(c1,c3);
185 
186  trace.info() << "(reverse_iterator<Circulator> / Circulator<reverse_iterator>)" << endl;
187  std::reverse_iterator<Circulator<vector<int>::iterator> > rc1( c1 );
188  Circulator <vector<int>::reverse_iterator> c4( v.rend(), v.rbegin(), v.rend() );
189  res = res && testComparison<vector<int>::iterator,vector<int>::iterator>(rc1.base().base(), c4.base().base());
190  trace.info() << "first element: (" << *--rc1 << " == " << *--c4 << ")" << endl;
191  res = res && testComparison<int,int>(*rc1, *c4);
192 
193 //tags
194  trace.info() << endl;
195  trace.info() << "Tags for classic iterators" << endl;
196  getGeneralTag< vector<int>::iterator > ( v.begin() );
197  getSpecificTag< vector<int>::iterator > ( v.begin() );
198  getGeneralTag< Circulator<vector<int>::iterator> > ( c2 );
199  getSpecificTag< Circulator<vector<int>::iterator> > ( c2 );
200  getType< vector<int>::iterator > ( v.begin() );
201  getType< Circulator<vector<int>::iterator> > ( c2 );
202 
203 
204 
205  trace.info() << "Tags for pointers" << endl;
206  int t[5] = {1, 2, 3, 4, 5};
207  getGeneralTag< int* > ( t );
208  getType< int* > ( t );
209 
210  Circulator<int*> tc(t, t, t+5);
211  trace.info() << *tc++ << *tc++ << *tc++ << *tc++ << *tc++ << *tc++ << *tc++ << *tc++ << endl;
212 
213  getSpecificTag< Circulator<int*> > ( tc );
214  getGeneralTag< Circulator<int*> > ( tc );
215  getType< Circulator<int*> > ( tc );
216 
217 //range validity
218  trace.info() << endl;
219  trace.info() << "Function isNotEmpty" << endl;
220  res = res && testIsNotEmpty<vector<int>::iterator>(v.begin(),v.end(),true)
221  && testIsNotEmpty<vector<int>::iterator>(v.end(),v.end(),false);
222  Circulator<vector<int>::iterator> validC( v.begin(), v.begin(), v.end() );
223  Circulator<vector<int>::iterator> validC2( v.end(), v.begin(), v.end() );
224  Circulator<vector<int>::iterator> notValidC( v.begin(), v.begin(), v.begin() );
225  res = res && testIsNotEmpty<Circulator<vector<int>::iterator> >(validC,validC,true)
226  && testIsNotEmpty<Circulator<vector<int>::iterator> >(validC,notValidC,false)
227  && testIsNotEmpty<Circulator<vector<int>::iterator> >(validC,validC2,true)
228 ;
229 
230  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
231  trace.endBlock();
232  return res ? 0 : 1;
233 }
234 // //