DGtal  0.9.2
testIteratorCirculatorTraits.cpp
1 
30 #include <iostream>
32 #include <list>
33 #include <vector>
34 #include <forward_list>
35 
36 #include "DGtal/base/Common.h"
37 #include "DGtal/base/IteratorCirculatorTraits.h"
38 #include "DGtal/base/Circulator.h"
39 #include "DGtal/base/ReverseIterator.h"
40 #include "DGtal/base/BasicFunctors.h"
41 #include "DGtal/base/ConstIteratorAdapter.h"
42 #include "DGtal/base/IteratorAdapter.h"
44 
45 using namespace std;
46 using namespace DGtal;
47 
49 // Functions for testing class IteratorCirculatorTraits.
51 
57 template< typename IC, typename T>
58 inline
59 bool compareType( IC, T ){
60  return boost::is_same<
62  T >::value;
63 }
64 
70 template< typename IC, typename T>
71 inline
72 bool compareCategory( IC, T ){
73  return boost::is_same<
75  T >::value;
76 }
77 
82 bool testIteratorCirculatorTraits()
83 {
84  unsigned int nbok = 0;
85  unsigned int nb = 0;
86 
87  trace.beginBlock ( "Testing tags for various (circular) iterators..." );
88 
89  //forward list
90  trace.info() << "forward list" << std::endl;
91  std::forward_list<int> fl;
92  if ( compareType( fl.begin(), IteratorType() ) )
93  nbok++;
94  nb++;
95  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
96  if ( compareCategory( fl.begin(), ForwardCategory() ) )
97  nbok++;
98  nb++;
99  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
100  Circulator< std::forward_list<int>::iterator > cfl(fl.begin(), fl.begin(), fl.end());
101  if ( compareType( cfl, CirculatorType() ) )
102  nbok++;
103  nb++;
104  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
105  if ( compareCategory( cfl, ForwardCategory() ) )
106  nbok++;
107  nb++;
108  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
109 
110  //list
111  trace.info() << "list" << std::endl;
112  std::list<int> bl;
113  if ( compareType( bl.begin(), IteratorType() ) )
114  nbok++;
115  nb++;
116  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
117  if ( compareCategory( bl.begin(), BidirectionalCategory() ) )
118  nbok++;
119  nb++;
120  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
121  Circulator< std::list<int>::iterator > cbl(bl.begin(), bl.begin(), bl.end());
122  if ( compareType( cbl, CirculatorType() ) )
123  nbok++;
124  nb++;
125  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
126  if ( compareCategory( cbl, BidirectionalCategory() ) )
127  nbok++;
128  nb++;
129  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
130 
131  //vector
132  trace.info() << "vector" << std::endl;
133  std::vector<int> v;
134  if ( compareType( v.begin(), IteratorType() ) )
135  nbok++;
136  nb++;
137  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
138  if ( compareCategory( v.begin(), RandomAccessCategory() ) )
139  nbok++;
140  nb++;
141  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
142  Circulator< std::vector<int>::iterator > cv(v.begin(), v.begin(), v.end());
143  if ( compareType( cv, CirculatorType() ) )
144  nbok++;
145  nb++;
146  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
147  if ( compareCategory( cv, RandomAccessCategory() ) )
148  nbok++;
149  nb++;
150  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
151 
152  //basic array
153  trace.info() << "basic array" << std::endl;
154  int t[5] = {1, 2, 3, 4, 5};
155  if ( compareType( t, IteratorType() ) )
156  nbok++;
157  nb++;
158  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
159  if ( compareCategory( t, RandomAccessCategory() ) )
160  nbok++;
161  nb++;
162  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
163  Circulator< int* > ct(t+1, t, t+5);
164  if ( compareType( ct, CirculatorType() ) )
165  nbok++;
166  nb++;
167  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
168  if ( compareCategory( ct, RandomAccessCategory() ) )
169  nbok++;
170  nb++;
171  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
172 
173  trace.endBlock();
174 
175  return (nbok == nb);
176 }
177 
182 bool testIteratorCirculatorTraitsAndAdapters()
183 {
184  unsigned int nbok = 0;
185  unsigned int nb = 0;
186 
187  trace.beginBlock ( "Testing tags for adapters (ReverseIterator, (Const)IteratorAdapter) " );
188 
189  int t[5] = {1, 2, 3, 4, 5};
190  //reverse
191  trace.info() << "ReverseIterator" << std::endl;
192  typedef ReverseIterator< int* > RI;
193  RI rt(t+5);
194  if ( compareType( rt, IteratorType() ) )
195  nbok++;
196  nb++;
197  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
198  if ( compareCategory( rt, RandomAccessCategory() ) )
199  nbok++;
200  nb++;
201  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
202  //const adapter
203  trace.info() << "ConstIteratorAdapter" << std::endl;
205  IA1 at1( t, functors::Identity() );
206  if ( compareType( at1, IteratorType() ) )
207  nbok++;
208  nb++;
209  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
210  if ( compareCategory( at1, RandomAccessCategory() ) )
211  nbok++;
212  nb++;
213  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
214  //adapter
215  trace.info() << "IteratorAdapter" << std::endl;
217  IA2 at2( t, functors::Identity() );
218  if ( compareType( at2, IteratorType() ) )
219  nbok++;
220  nb++;
221  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
222  if ( compareCategory( at2, RandomAccessCategory() ) )
223  nbok++;
224  nb++;
225  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
226 
227  //with circulators
228  typedef Circulator< int* > CI;
229 
230  //reverse circ, circ reverse,
231  trace.info() << "ReverseIterator<Circulator> and Circulator<ReverseIterator>" << std::endl;
232  Circulator< RI > crt( rt, RI(t+5), RI(t) );
233  if ( compareType( crt, CirculatorType() ) )
234  nbok++;
235  nb++;
236  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
237  if ( compareCategory( crt, RandomAccessCategory() ) )
238  nbok++;
239  nb++;
240  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
241  ReverseIterator< CI > rct( CI(t+5, t, t+5) );
242  if ( compareType( rct, CirculatorType() ) )
243  nbok++;
244  nb++;
245  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
246  if ( compareCategory( rct, RandomAccessCategory() ) )
247  nbok++;
248  nb++;
249  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
250 
251  //const iterator adapter circ, circ const iterator adapter
252  trace.info() << "ConstIteratorAdapter of Circulator and Circulator of ConstIteratorAdapter" << std::endl;
253  Circulator< IA1 > cat1( at1, at1, IA1(t+5, functors::Identity()) );
254  if ( compareType( cat1, CirculatorType() ) )
255  nbok++;
256  nb++;
257  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
258  if ( compareCategory( cat1, RandomAccessCategory() ) )
259  nbok++;
260  nb++;
261  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
263  if ( compareType( a1ct, CirculatorType() ) )
264  nbok++;
265  nb++;
266  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
267  if ( compareCategory( a1ct, RandomAccessCategory() ) )
268  nbok++;
269  nb++;
270  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
271 
272  //iterator adapter circ, circ iterator adapter
273  trace.info() << "IteratorAdapter of Circulator and Circulator of IteratorAdapter" << std::endl;
274  Circulator< IA2 > cat2( at2, at2, IA2(t+5, functors::Identity()) );
275  if ( compareType( cat2, CirculatorType() ) )
276  nbok++;
277  nb++;
278  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
279  if ( compareCategory( cat2, RandomAccessCategory() ) )
280  nbok++;
281  nb++;
282  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
284  if ( compareType( a2ct, CirculatorType() ) )
285  nbok++;
286  nb++;
287  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
288  if ( compareCategory( a2ct, RandomAccessCategory() ) )
289  nbok++;
290  nb++;
291  trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
292 
293  trace.endBlock();
294 
295  return (nbok == nb);
296 }
297 
299 // Standard services - public :
300 
301 int main( int argc, char** argv )
302 {
303  trace.beginBlock ( "Testing class IteratorCirculatorTraits" );
304  trace.info() << "Args:";
305  for ( int i = 0; i < argc; ++i )
306  trace.info() << " " << argv[ i ];
307  trace.info() << endl;
308 
309  bool res = testIteratorCirculatorTraits() && testIteratorCirculatorTraitsAndAdapters();
310 
311  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
312  trace.endBlock();
313  return res ? 0 : 1;
314 }
315 // //
void beginBlock(const std::string &keyword="")
This class adapts any iterator so that operator* returns another element than the one pointed to by t...
Trace trace
Definition: Common.h:130
This class adapts any bidirectional iterator so that operator++ calls operator-- and vice versa...
STL namespace.
double endBlock()
Aim: Define a simple default functor that just returns its argument.
ToDGtalCategory< typename boost::iterator_category< IC >::type >::Category Category
std::ostream & emphase()
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition: Circulator.h:85
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()
This class adapts any lvalue iterator so that operator* returns a member on the element pointed to by...