1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
// Boost.Geometry (aka GGL, Generic Geometry Library)
// Unit Test
// Copyright (c) 2014, Oracle and/or its affiliates.
// Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
// Licensed under the Boost Software License version 1.0.
// http://www.boost.org/users/license.html
#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE test_flatten_iterator
#endif
#include <boost/test/included/unit_test.hpp>
#include <cstddef>
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <list>
#include <boost/core/ignore_unused.hpp>
#include <boost/mpl/if.hpp>
#include <boost/type_traits/is_const.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/assign/std/list.hpp>
#include <boost/assign/std/set.hpp>
#include "test_iterator_common.hpp"
#include <boost/geometry/iterators/flatten_iterator.hpp>
using namespace boost::assign;
template <typename InnerContainer>
struct access_begin
{
typedef typename boost::mpl::if_
<
typename boost::is_const<InnerContainer>::type,
typename InnerContainer::const_iterator,
typename InnerContainer::iterator
>::type return_type;
static inline return_type apply(InnerContainer& inner)
{
return inner.begin();
}
};
template <typename InnerContainer>
struct access_end
{
typedef typename boost::mpl::if_
<
typename boost::is_const<InnerContainer>::type,
typename InnerContainer::const_iterator,
typename InnerContainer::iterator
>::type return_type;
static inline return_type apply(InnerContainer& inner)
{
return inner.end();
}
};
template <typename NestedContainer>
inline std::size_t number_of_elements(NestedContainer const& c)
{
std::size_t num_elements(0);
for (typename NestedContainer::const_iterator outer = c.begin();
outer != c.end(); ++outer)
{
num_elements += outer->size();
}
return num_elements;
}
struct test_flatten_iterator
{
template
<
typename FlattenIterator,
typename ConstFlattenIterator,
typename NestedContainer
>
static inline
void test_using_max_element(FlattenIterator first,
FlattenIterator beyond,
ConstFlattenIterator const_first,
ConstFlattenIterator const_beyond,
NestedContainer const& c)
{
typedef typename std::iterator_traits
<
FlattenIterator
>::value_type value_type;
typedef typename NestedContainer::const_iterator const_outer_iterator;
typedef typename NestedContainer::value_type inner_container;
typedef typename inner_container::const_iterator const_inner_iterator;
if ( first == beyond )
{
return;
}
FlattenIterator it_max = std::max_element(first, beyond);
ConstFlattenIterator const_it_max =
std::max_element(const_first, const_beyond);
BOOST_CHECK( it_max == const_it_max );
BOOST_CHECK( *it_max == *const_it_max );
value_type old_value = *const_first;
value_type new_value = *it_max + 1;
*first = *it_max + 1;
const_outer_iterator outer = c.begin();
while ( outer->begin() == outer->end() )
{
++outer;
}
const_inner_iterator inner = outer->begin();
BOOST_CHECK( *inner == new_value );
#ifdef BOOST_GEOMETRY_TEST_DEBUG
std::cout << std::endl;
std::cout << "modified 1st element of 1st non-empty "
<< "inner container:" << std::endl;
print_nested_container(std::cout, c.begin(), c.end(), "nested :")
<< std::endl;
print_container(std::cout, first, beyond, "flattened:") << std::endl;
#endif
*first = old_value;
BOOST_CHECK( *inner == old_value );
}
template <typename NestedContainer>
static inline void apply(NestedContainer& c,
std::string const& case_id,
std::string const& container_id)
{
boost::ignore_unused(case_id, container_id);
#ifdef BOOST_GEOMETRY_TEST_DEBUG
std::stringstream sstream;
sstream << case_id << " [" << container_id << "]";
std::cout << "case id: " << sstream.str() << std::endl;
#endif
typedef typename NestedContainer::const_iterator const_outer_iterator;
typedef typename NestedContainer::iterator outer_iterator;
typedef typename NestedContainer::value_type inner_container;
typedef typename inner_container::const_iterator const_inner_iterator;
typedef typename inner_container::iterator inner_iterator;
typedef boost::geometry::flatten_iterator
<
const_outer_iterator,
const_inner_iterator,
typename inner_container::value_type const,
access_begin<inner_container const>,
access_end<inner_container const>
> const_flatten_iterator;
typedef boost::geometry::flatten_iterator
<
outer_iterator,
inner_iterator,
typename inner_container::value_type,
access_begin<inner_container>,
access_end<inner_container>
> flatten_iterator;
typedef typename std::iterator_traits
<
flatten_iterator
>::value_type value_type;
flatten_iterator begin(c.begin(), c.end());
flatten_iterator end(c.end());
const_flatten_iterator const_begin(begin);
const_flatten_iterator const_end(end);
const_begin = begin;
const_end = end;
// test copying, dereferencing and element equality
std::vector<value_type> combined;
for (const_outer_iterator outer = c.begin();
outer != c.end(); ++outer)
{
std::copy(outer->begin(), outer->end(),
std::back_inserter(combined));
}
test_equality(begin, end, combined);
test_equality(const_begin, const_end, combined);
combined.clear();
std::copy(begin, end, std::back_inserter(combined));
test_equality(begin, end, combined);
test_equality(const_begin, const_end, combined);
combined.clear();
std::copy(const_begin, const_end, std::back_inserter(combined));
test_equality(begin, end, combined);
test_equality(const_begin, const_end, combined);
// test sizes (and std::distance)
test_size(begin, end, combined);
test_size(const_begin, const_end, combined);
#ifdef BOOST_GEOMETRY_TEST_DEBUG
print_nested_container(std::cout, c.begin(), c.end(), "nested :")
<< std::endl;
print_container(std::cout, begin, end, "flattened :")
<< std::endl;
if ( begin != end )
{
std::cout << "min element: "
<< *std::min_element(begin, end)
<< std::endl;
std::cout << "max element: "
<< *std::max_element(const_begin, const_end)
<< std::endl;
}
#endif
// perform reversals (std::reverse)
test_using_reverse(begin, end, combined);
// test std::max_element, dereferencing and value assigment
test_using_max_element(begin, end, const_begin, const_end, c);
// test std::count_if / std::remove_if
test_using_remove_if(begin, end, combined);
#ifdef BOOST_GEOMETRY_TEST_DEBUG
std::cout << "====================" << std::endl << std::endl;
#endif
}
};
// the actual test cases -- START
template <int CaseNumber>
struct test_case_per_container;
template<>
struct test_case_per_container<0>
{
template <typename NestedContainer>
static inline void apply(std::string const& case_id,
std::string const& container_id)
{
NestedContainer c;
test_flatten_iterator::apply(c, case_id, container_id);
}
};
template<>
struct test_case_per_container<1>
{
template <typename NestedContainer>
static inline void apply(std::string const& case_id,
std::string const& container_id)
{
NestedContainer c;
for (int i = 0; i < 5; ++i)
{
c += typename NestedContainer::value_type();
}
test_flatten_iterator::apply(c, case_id, container_id);
}
};
template<>
struct test_case_per_container<2>
{
template <typename NestedContainer>
static inline void apply(std::string const& case_id,
std::string const& container_id)
{
NestedContainer c;
typename NestedContainer::value_type ic[4];
ic[0] += 5,4,3,2,1;
ic[1] += 6,7,8;
ic[2] += 9;
ic[3] += 9,8,7,6,5;
c += ic[0],ic[1],ic[2],ic[3];
test_flatten_iterator::apply(c, case_id, container_id);
}
};
template<>
struct test_case_per_container<3>
{
template <typename NestedContainer>
static inline void apply(std::string const& case_id,
std::string const& container_id)
{
NestedContainer c;
typename NestedContainer::value_type ic[20];
ic[2] += 5,4,3,2,1;
ic[3] += 6,7,8;
ic[8] += 9;
ic[9] += 9,8,7,6,5;
ic[14] += 4,3,2,1;
for (std::size_t i = 0; i < 20; ++i)
{
c += ic[i];
}
test_flatten_iterator::apply(c, case_id, container_id);
}
};
// the actual test cases -- END
template <int CaseNumber>
inline void test_case_all_containers(std::string const& case_id)
{
typedef typename std::vector<std::vector<int> > VV;
typedef typename std::vector<std::list<int> > VL;
typedef typename std::list<std::vector<int> > LV;
typedef typename std::list<std::list<int> > LL;
#ifdef BOOST_GEOMETRY_TEST_DEBUG
std::cout << std::endl << std::endl;
#endif
test_case_per_container<CaseNumber>::template apply<VV>(case_id, "VV");
test_case_per_container<CaseNumber>::template apply<VL>(case_id, "VL");
test_case_per_container<CaseNumber>::template apply<LV>(case_id, "LV");
test_case_per_container<CaseNumber>::template apply<LL>(case_id, "LL");
#ifdef BOOST_GEOMETRY_TEST_DEBUG
std::cout << std::endl;
std::cout << "********************************************************"
<< std::endl << std::endl;
#endif
}
BOOST_AUTO_TEST_CASE( test_flatten_iterator_all )
{
test_case_all_containers<0>("empty");
test_case_all_containers<1>("case1");
test_case_all_containers<2>("case2");
test_case_all_containers<3>("case3");
}
|