DGtal
2.2.0
Toggle main menu visibility
Loading...
Searching...
No Matches
testCirculator.cpp
Go to the documentation of this file.
1
16
29
31
#include <iostream>
32
#include <vector>
33
#include <list>
34
#include <forward_list>
35
36
#include "DGtal/base/Common.h"
37
#include "DGtal/base/Circulator.h"
38
#include "boost/iterator/reverse_iterator.hpp"
40
41
42
using namespace
std
;
43
using namespace
DGtal
;
44
45
47
// Functions for testing class Circulator.
49
53
template
<
typename
Iterator>
54
bool
testOffset
(
const
Iterator
& itb,
const
Iterator
& ite,
const
vector<int>& groundTruth)
55
{
56
57
BOOST_CONCEPT_ASSERT((
boost::BidirectionalIterator<Iterator>
));
58
BOOST_CONCEPT_ASSERT((
boost::BidirectionalIterator
<
Circulator<Iterator>
> ));
59
60
//list
61
copy(itb,ite,ostream_iterator<int>(
trace
.
info
(),
" "
));
62
trace
.
info
() <<
" => "
;
63
64
//use of Circulators
65
vector<int> v;
66
Circulator<Iterator>
cb( itb, itb, ite );
67
Circulator<Iterator>
c( ++cb );
68
do
{
69
v.push_back(*c);
70
c++;
71
}
while
(c != cb);
72
73
//offset list
74
copy(v.begin(),v.end(),ostream_iterator<int>(
trace
.
info
(),
" "
));
75
76
//ground truth
77
trace
.
info
() <<
" ( == "
;
78
copy(groundTruth.begin(),groundTruth.end(),ostream_iterator<int>(
trace
.
info
(),
" "
));
79
trace
.
info
() <<
")"
<< std::endl;
80
81
return
equal( v.begin(),v.end(),groundTruth.begin() );
82
}
83
89
template
<
typename
Container>
90
bool
basicForwardTest
(
const
Container& cont)
91
{
92
unsigned
int
nbok = 0;
93
unsigned
int
nb = 0;
94
95
trace
.
beginBlock
(
"Operators of forward circulator"
);
96
typedef
typename
Container::const_iterator I;
97
98
//default construction
99
Circulator<I>
circ0;
100
if
( !(circ0.
isValid
()) )
101
nbok++;
102
nb++;
103
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
104
//construction
105
Circulator<I>
circ1(cont.begin(), cont.begin(), cont.end());
106
if
( circ1.
isValid
() )
107
nbok++;
108
nb++;
109
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
110
//copy
111
Circulator<I>
circ2 = circ1;
112
//assignment
113
circ0 = circ1;
114
//pre/post-incrementation
115
circ1++;
116
++circ2;
117
//equality
118
if
( (circ0 != circ1) && (circ0 != circ2) && (circ1 == circ2) )
119
nbok++;
120
nb++;
121
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
122
//base, begin, end
123
if
( (circ0.
base
() != circ1.
base
())
124
&& (circ0.
base
() != circ2.
base
())
125
&& (circ1.
base
() == circ2.
base
()) )
126
nbok++;
127
nb++;
128
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
129
if
( (circ0.
begin
() == circ1.
begin
())
130
&& (circ0.
end
() == circ1.
end
())
131
&& (circ1.
begin
() == circ2.
begin
()) )
132
nbok++;
133
nb++;
134
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
135
//operator *, ->
136
if
( (*circ1 == *circ2) && (circ1.operator->() == &(*circ1)) )
137
nbok++;
138
nb++;
139
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
140
141
trace
.
endBlock
();
142
143
return
(nbok == nb);
144
}
145
151
template
<
typename
Container>
152
bool
basicBidirectionalTest
(
const
Container& cont)
153
{
154
ASSERT( cont.size() >= 1 );
155
unsigned
int
nbok = 0;
156
unsigned
int
nb = 0;
157
158
trace
.
beginBlock
(
"Operators of bidirectional circulator"
);
159
typedef
typename
Container::const_iterator I;
160
161
//construction/copy
162
Circulator<I>
res(cont.begin(), cont.begin(), cont.end());
163
Circulator<I>
circ1(cont.begin(), cont.begin(), cont.end());
164
Circulator<I>
circ2 = circ1;
165
//pre/post-incrementation
166
circ1++;
167
++circ2;
168
if
(circ1 == circ2)
169
nbok++;
170
nb++;
171
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
172
//pre/post-decrementation
173
circ1--;
174
--circ2;
175
if
( (circ1 == circ2) && (circ1 == res) && (circ2 == res) )
176
nbok++;
177
nb++;
178
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
179
180
trace
.
endBlock
();
181
182
return
(nbok == nb);
183
}
184
190
template
<
typename
Container>
191
bool
basicRandomAccessTest
(
const
Container& cont)
192
{
193
ASSERT( cont.size() == 5 );
194
195
unsigned
int
nbok = 0;
196
unsigned
int
nb = 0;
197
198
trace
.
beginBlock
(
"Operators of random access circulator"
);
199
typedef
typename
Container::const_iterator I;
200
201
//construction/copy
202
Circulator<I>
circ1(cont.begin(), cont.begin(), cont.end());
203
Circulator<I>
circ2 = circ1;
204
205
trace
.
info
() <<
"arithmetic operators"
<< std::endl;
206
circ2 += 4;
207
if
( (circ2) == (circ1+4) )
208
nbok++;
209
nb++;
210
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
211
212
circ2 -= 5;
213
circ2++;
214
if
( circ1 == circ2 )
215
nbok++;
216
nb++;
217
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
218
219
if
( (circ1+3) == (3+circ1) )
220
nbok++;
221
nb++;
222
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
223
224
circ2 += 7;
225
if
( (circ1+2) == circ2 )
226
nbok++;
227
nb++;
228
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
229
230
if
( ((circ1-circ2) + (circ2-circ1)) == (cont.end()-cont.begin()) )
231
nbok++;
232
nb++;
233
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
234
235
trace
.
info
() <<
"comparison operators"
<< std::endl;
236
if
( (circ1 < circ2) && (circ1 <= circ2)
237
&& (circ2 < circ1) && (circ2 <= circ1) )
238
nbok++;
239
nb++;
240
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
241
242
if
( !( (circ1 > circ2) && (circ1 >= circ2)
243
&& (circ2 > circ1) && (circ2 >= circ1) ) )
244
nbok++;
245
nb++;
246
trace
.
info
() <<
"("
<< nbok <<
"/"
<< nb <<
") "
<< std::endl;
247
248
trace
.
endBlock
();
249
250
return
(nbok == nb);
251
}
252
254
// Standard services - public :
255
256
int
main
(
int
argc,
char
** argv )
257
{
258
trace
.
beginBlock
(
"Testing class Circulator"
);
259
trace
.
info
() <<
"Args:"
;
260
for
(
int
i = 0; i < argc; ++i )
261
trace
.
info
() <<
" "
<< argv[ i ];
262
trace
.
info
() << endl;
263
264
vector<int> v;
265
v.push_back(1);
266
v.push_back(2);
267
v.push_back(3);
268
v.push_back(4);
269
v.push_back(5);
270
271
vector<int> v2;
272
v2.push_back(2);
273
v2.push_back(3);
274
v2.push_back(4);
275
v2.push_back(5);
276
v2.push_back(1);
277
278
vector<int> v3;
279
v3.push_back(4);
280
v3.push_back(3);
281
v3.push_back(2);
282
v3.push_back(1);
283
v3.push_back(5);
284
285
//incrementation
286
trace
.
beginBlock
(
"Iteration"
);
287
bool
res =
testOffset
(v.begin(),v.end(), v2)
288
&&
testOffset
(v.rbegin(),v.rend(), v3)
289
&&
testOffset
(v.begin(),v.end(), v2)
290
&&
testOffset
(v.rbegin(),v.rend(), v3);
291
trace
.
endBlock
();
292
293
//comparisons
294
trace
.
beginBlock
(
"Comparison"
);
295
trace
.
info
() <<
"(const / not const)"
<< endl;
296
Circulator<vector<int>::iterator
> c1( v.begin(), v.begin(), v.end() );
297
Circulator<vector<int>::iterator
> c2( c1 );
298
Circulator<vector<int>::const_iterator
> c3( c2 );
299
res = res && (c1 == c2) && (c1 == c3);
300
301
trace
.
info
() <<
"(reverse_iterator<Circulator> / Circulator<reverse_iterator>)"
<< endl;
302
boost::reverse_iterator<Circulator<vector<int>::iterator> > rc1( c1 );
303
Circulator <vector<int>::reverse_iterator> c4( v.rend(), v.rbegin(), v.rend() );
304
res = res && (rc1.base().base() == c4.
base
().base());
305
trace
.
info
() <<
"first element: ("
<< *--rc1 <<
" == "
<< *--c4 <<
")"
<< endl;
306
res = res && ((*rc1) == (*c4));
307
trace
.
endBlock
();
308
309
std::forward_list<int> fl;
310
fl.push_front(1);
311
fl.push_front(2);
312
fl.push_front(3);
313
fl.push_front(4);
314
fl.push_front(5);
315
316
std::list<int> bl;
317
bl.push_back(1);
318
bl.push_back(2);
319
bl.push_back(3);
320
bl.push_back(4);
321
bl.push_back(5);
322
323
res = res &&
324
basicForwardTest
(fl) &&
325
basicForwardTest
(bl) &&
326
basicForwardTest
(v) &&
327
basicBidirectionalTest
(bl) &&
328
basicBidirectionalTest
(v) &&
329
basicRandomAccessTest
(v);
330
331
trace
.
emphase
() << ( res ?
"Passed."
:
"Error."
) << endl;
332
trace
.
endBlock
();
333
return
res ? 0 : 1;
334
}
335
// //
DGtal::Circulator
Aim: Provides an adapter for classical iterators that can iterate through the underlying data structu...
Definition
Circulator.h:86
DGtal::Circulator::isValid
bool isValid() const
Definition
Circulator.h:206
DGtal::Circulator::base
Iterator base() const
Definition
Circulator.h:216
DGtal::Circulator::end
Iterator end() const
Definition
Circulator.h:228
DGtal::Circulator::begin
Iterator begin() const
Definition
Circulator.h:222
DGtal::Iterator
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Trace::emphase
std::ostream & emphase()
DGtal::Trace::info
std::ostream & info()
DGtal::Trace::endBlock
double endBlock()
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition
ClosedIntegerHalfPlane.h:49
DGtal::trace
Trace trace
std
STL namespace.
boost::BidirectionalIterator
Go to http://www.sgi.com/tech/stl/BidirectionalIterator.html.
Definition
Boost.dox:42
testOffset
bool testOffset(const Iterator &itb, const Iterator &ite, const vector< int > &groundTruth)
Definition
testCirculator.cpp:54
basicBidirectionalTest
bool basicBidirectionalTest(const Container &cont)
Definition
testCirculator.cpp:152
basicRandomAccessTest
bool basicRandomAccessTest(const Container &cont)
Definition
testCirculator.cpp:191
basicForwardTest
bool basicForwardTest(const Container &cont)
Definition
testCirculator.cpp:90
main
int main(int, char **)
Definition
testIntegerComputer.cpp:331
tests
base
testCirculator.cpp
Generated on
for DGtal by
1.18.0