DGtal 1.4.0
Loading...
Searching...
No Matches
testCirculator.cpp File Reference
#include <iostream>
#include <vector>
#include <list>
#include <forward_list>
#include "DGtal/base/Common.h"
#include "DGtal/base/Circulator.h"
Include dependency graph for testCirculator.cpp:

Go to the source code of this file.

Functions

template<typename Iterator >
bool testOffset (const Iterator &itb, const Iterator &ite, const vector< int > &groundTruth)
 
template<typename Container >
bool basicForwardTest (const Container &cont)
 
template<typename Container >
bool basicBidirectionalTest (const Container &cont)
 
template<typename Container >
bool basicRandomAccessTest (const Container &cont)
 
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
2011/07/05

Functions for testing class Circulator.h.

This file is part of the DGtal library.

Definition in file testCirculator.cpp.

Function Documentation

◆ basicBidirectionalTest()

template<typename Container >
bool basicBidirectionalTest ( const Container & cont)

Test of basic operators required for bidirectional circulators

Parameters
contany container
Template Parameters
Containermodel of iterable and pushable container

Definition at line 151 of file testCirculator.cpp.

152{
153 ASSERT( cont.size() >= 1 );
154 unsigned int nbok = 0;
155 unsigned int nb = 0;
156
157 trace.beginBlock ( "Operators of bidirectional circulator" );
158 typedef typename Container::const_iterator I;
159
160 //construction/copy
161 Circulator<I> res(cont.begin(), cont.begin(), cont.end());
162 Circulator<I> circ1(cont.begin(), cont.begin(), cont.end());
163 Circulator<I> circ2 = circ1;
164 //pre/post-incrementation
165 circ1++;
166 ++circ2;
167 if (circ1 == circ2)
168 nbok++;
169 nb++;
170 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
171 //pre/post-decrementation
172 circ1--;
173 --circ2;
174 if ( (circ1 == circ2) && (circ1 == res) && (circ2 == res) )
175 nbok++;
176 nb++;
177 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
178
179 trace.endBlock();
180
181 return (nbok == nb);
182}
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition Circulator.h:86
void beginBlock(const std::string &keyword="")
std::ostream & info()
double endBlock()
Trace trace
Definition Common.h:153

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

Referenced by main().

◆ basicForwardTest()

template<typename Container >
bool basicForwardTest ( const Container & cont)

Test of basic operators required for forward circulators

Parameters
contany container
Template Parameters
Containermodel of iterable and pushable container

Definition at line 89 of file testCirculator.cpp.

90{
91 unsigned int nbok = 0;
92 unsigned int nb = 0;
93
94 trace.beginBlock ( "Operators of forward circulator" );
95 typedef typename Container::const_iterator I;
96
97 //default construction
98 Circulator<I> circ0;
99 if ( !(circ0.isValid()) )
100 nbok++;
101 nb++;
102 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
103 //construction
104 Circulator<I> circ1(cont.begin(), cont.begin(), cont.end());
105 if ( circ1.isValid() )
106 nbok++;
107 nb++;
108 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
109 //copy
110 Circulator<I> circ2 = circ1;
111 //assignement
112 circ0 = circ1;
113 //pre/post-incrementation
114 circ1++;
115 ++circ2;
116 //equality
117 if ( (circ0 != circ1) && (circ0 != circ2) && (circ1 == circ2) )
118 nbok++;
119 nb++;
120 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
121 //base, begin, end
122 if ( (circ0.base() != circ1.base())
123 && (circ0.base() != circ2.base())
124 && (circ1.base() == circ2.base()) )
125 nbok++;
126 nb++;
127 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
128 if ( (circ0.begin() == circ1.begin())
129 && (circ0.end() == circ1.end())
130 && (circ1.begin() == circ2.begin()) )
131 nbok++;
132 nb++;
133 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
134 //operator *, ->
135 if ( (*circ1 == *circ2) && (circ1.operator->() == &(*circ1)) )
136 nbok++;
137 nb++;
138 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
139
140 trace.endBlock();
141
142 return (nbok == nb);
143}
bool isValid() const
Definition Circulator.h:206
Iterator base() const
Definition Circulator.h:216
Iterator end() const
Definition Circulator.h:228
Iterator begin() const
Definition Circulator.h:222

References DGtal::Circulator< TIterator >::base(), DGtal::Circulator< TIterator >::begin(), DGtal::Trace::beginBlock(), DGtal::Circulator< TIterator >::end(), DGtal::Trace::endBlock(), DGtal::Trace::info(), DGtal::Circulator< TIterator >::isValid(), and DGtal::trace.

Referenced by main().

◆ basicRandomAccessTest()

template<typename Container >
bool basicRandomAccessTest ( const Container & cont)

Test of basic operators required for random access circulators

Parameters
contany container
Template Parameters
Containermodel of iterable and pushable container

Definition at line 190 of file testCirculator.cpp.

191{
192 ASSERT( cont.size() == 5 );
193
194 unsigned int nbok = 0;
195 unsigned int nb = 0;
196
197 trace.beginBlock ( "Operators of random access circulator" );
198 typedef typename Container::const_iterator I;
199
200 //construction/copy
201 Circulator<I> circ1(cont.begin(), cont.begin(), cont.end());
202 Circulator<I> circ2 = circ1;
203
204 trace.info() << "arithmetic operators" << std::endl;
205 circ2 += 4;
206 if ( (circ2) == (circ1+4) )
207 nbok++;
208 nb++;
209 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
210
211 circ2 -= 5;
212 circ2++;
213 if ( circ1 == circ2 )
214 nbok++;
215 nb++;
216 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
217
218 if ( (circ1+3) == (3+circ1) )
219 nbok++;
220 nb++;
221 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
222
223 circ2 += 7;
224 if ( (circ1+2) == circ2 )
225 nbok++;
226 nb++;
227 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
228
229 if ( ((circ1-circ2) + (circ2-circ1)) == (cont.end()-cont.begin()) )
230 nbok++;
231 nb++;
232 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
233
234 trace.info() << "comparison operators" << std::endl;
235 if ( (circ1 < circ2) && (circ1 <= circ2)
236 && (circ2 < circ1) && (circ2 <= circ1) )
237 nbok++;
238 nb++;
239 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
240
241 if ( !( (circ1 > circ2) && (circ1 >= circ2)
242 && (circ2 > circ1) && (circ2 >= circ1) ) )
243 nbok++;
244 nb++;
245 trace.info() << "(" << nbok << "/" << nb << ") " << std::endl;
246
247 trace.endBlock();
248
249 return (nbok == nb);
250}

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

Referenced by main().

◆ main()

int main ( int argc,
char ** argv )

Definition at line 255 of file testCirculator.cpp.

256{
257 trace.beginBlock ( "Testing class Circulator" );
258 trace.info() << "Args:";
259 for ( int i = 0; i < argc; ++i )
260 trace.info() << " " << argv[ i ];
261 trace.info() << endl;
262
263 vector<int> v;
264 v.push_back(1);
265 v.push_back(2);
266 v.push_back(3);
267 v.push_back(4);
268 v.push_back(5);
269
270 vector<int> v2;
271 v2.push_back(2);
272 v2.push_back(3);
273 v2.push_back(4);
274 v2.push_back(5);
275 v2.push_back(1);
276
277 vector<int> v3;
278 v3.push_back(4);
279 v3.push_back(3);
280 v3.push_back(2);
281 v3.push_back(1);
282 v3.push_back(5);
283
284 //incrementation
285 trace.beginBlock ( "Iteration" );
286 bool res = testOffset(v.begin(),v.end(), v2)
287 && testOffset(v.rbegin(),v.rend(), v3)
288 && testOffset(v.begin(),v.end(), v2)
289 && testOffset(v.rbegin(),v.rend(), v3);
290 trace.endBlock();
291
292 //comparisons
293 trace.beginBlock ( "Comparison" );
294 trace.info() << "(const / not const)" << endl;
295 Circulator<vector<int>::iterator> c1( v.begin(), v.begin(), v.end() );
298 res = res && (c1 == c2) && (c1 == c3);
299
300 trace.info() << "(reverse_iterator<Circulator> / Circulator<reverse_iterator>)" << endl;
301 std::reverse_iterator<Circulator<vector<int>::iterator> > rc1( c1 );
302 Circulator <vector<int>::reverse_iterator> c4( v.rend(), v.rbegin(), v.rend() );
303 res = res && (rc1.base().base() == c4.base().base());
304 trace.info() << "first element: (" << *--rc1 << " == " << *--c4 << ")" << endl;
305 res = res && ((*rc1) == (*c4));
306 trace.endBlock();
307
308 std::forward_list<int> fl;
309 fl.push_front(1);
310 fl.push_front(2);
311 fl.push_front(3);
312 fl.push_front(4);
313 fl.push_front(5);
314
315 std::list<int> bl;
316 bl.push_back(1);
317 bl.push_back(2);
318 bl.push_back(3);
319 bl.push_back(4);
320 bl.push_back(5);
321
322 res = res &&
323 basicForwardTest(fl) &&
324 basicForwardTest(bl) &&
325 basicForwardTest(v) &&
329
330 trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
331 trace.endBlock();
332 return res ? 0 : 1;
333}
std::ostream & emphase()
bool testOffset(const Iterator &itb, const Iterator &ite, const vector< int > &groundTruth)
bool basicBidirectionalTest(const Container &cont)
bool basicRandomAccessTest(const Container &cont)
bool basicForwardTest(const Container &cont)

References DGtal::Circulator< TIterator >::base(), basicBidirectionalTest(), basicForwardTest(), basicRandomAccessTest(), DGtal::Trace::beginBlock(), DGtal::Trace::emphase(), DGtal::Trace::endBlock(), DGtal::Trace::info(), testOffset(), and DGtal::trace.

◆ testOffset()

template<typename Iterator >
bool testOffset ( const Iterator & itb,
const Iterator & ite,
const vector< int > & groundTruth )

Iteration accross the end of a range

Definition at line 53 of file testCirculator.cpp.

54{
55
56 BOOST_CONCEPT_ASSERT(( boost::BidirectionalIterator<Iterator> ));
57 BOOST_CONCEPT_ASSERT(( boost::BidirectionalIterator< Circulator<Iterator> > ));
58
59 //list
60 copy(itb,ite,ostream_iterator<int>(trace.info(), " "));
61 trace.info() << " => ";
62
63 //use of Circulators
64 vector<int> v;
65 Circulator<Iterator> cb( itb, itb, ite );
66 Circulator<Iterator> c( ++cb );
67 do {
68 v.push_back(*c);
69 c++;
70 } while (c != cb);
71
72 //offset list
73 copy(v.begin(),v.end(),ostream_iterator<int>(trace.info(), " "));
74
75 //ground truth
76 trace.info() << " ( == ";
77 copy(groundTruth.begin(),groundTruth.end(),ostream_iterator<int>(trace.info(), " "));
78 trace.info() << ")" << std::endl;
79
80 return equal( v.begin(),v.end(),groundTruth.begin() );
81}
Go to http://www.sgi.com/tech/stl/BidirectionalIterator.html.
Definition Boost.dox:42

References DGtal::Trace::info(), and DGtal::trace.

Referenced by main().