Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FilteredIterator_UnitTests.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 #include "Teuchos_Array.hpp"
14 
15 
16 namespace {
17 
18 
19 template<typename T>
20 class SelectAll {
21 public:
22  bool operator()(const T& x) const
23  { return true; }
24 };
25 
26 
27 template<typename IntegralType>
28 class SelectEven {
29 public:
30  bool operator()(const IntegralType& x) const
31  { return ( (x % 2) == 0 ); }
32 };
33 
34 
35 template<typename IntegralType>
36 class SelectOdd {
37 public:
38  bool operator()(const IntegralType& x) const
39  { return ( (x % 2) != 0 ); }
40 };
41 
42 
43 } // namespace
44 
45 
46 namespace Teuchos {
47 
48 
49 TEUCHOS_UNIT_TEST( FilteredIterator, default_construct )
50 {
52  // There is just nothing that we can check for an uninitialized iterator!
53 }
54 
55 
57 {
58  typedef Array<int>::iterator itr_t;
59  Array<int> a;
60  a.push_back(1);
62  TEST_ITER_EQUALITY(itr.current(), a.begin());
63  TEST_ITER_EQUALITY(itr.begin(), a.begin());
64  TEST_ITER_EQUALITY(itr.end(), a.end());
65  FilteredIterator<itr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
66  TEST_ITER_EQUALITY(itr_end.current(), a.end());
67  TEST_ITER_EQUALITY(itr_end.begin(), a.begin());
68  TEST_ITER_EQUALITY(itr_end.end(), a.end());
69 }
70 
71 
73 {
74  typedef Array<int>::iterator itr_t;
75  Array<int> a;
76  a.push_back(2);
78  TEST_EQUALITY_CONST(*itr, 2);
79 }
80 
81 
83 {
84  typedef std::pair<int,int> value_t;
85  typedef Array<value_t>::iterator itr_t;
87  a.push_back(std::make_pair(2, 4));
89  TEST_EQUALITY_CONST(itr->first, 2);
90  TEST_EQUALITY_CONST(itr->second, 4);
91 }
92 
93 
94 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_same )
95 {
96  typedef Array<int>::iterator itr_t;
97  Array<int> a;
98  a.push_back(1);
101  TEST_ITER_EQUALITY(itr2.current(), a.begin());
102  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
103  TEST_ITER_EQUALITY(itr2.end(), a.end());
104  TEST_EQUALITY(*itr1, *itr2);
105 }
106 
107 
108 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_different )
109 {
110  typedef Array<int>::iterator itr_t;
111  typedef Array<int>::const_iterator citr_t;
112  Array<int> a;
113  a.push_back(1);
116  TEST_ITER_EQUALITY(itr2.current(), a.begin());
117  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
118  TEST_ITER_EQUALITY(itr2.end(), a.end());
119  TEST_EQUALITY(*itr1, *itr2);
120 }
121 
122 
124 {
125  typedef Array<int>::iterator itr_t;
126  Array<int> a;
127  a.push_back(1);
130  itr2 = itr1;
131  TEST_ITER_EQUALITY(itr2.current(), a.begin());
132  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
133  TEST_ITER_EQUALITY(itr2.end(), a.end());
134  TEST_EQUALITY(*itr1, *itr2);
135 }
136 
137 
139 {
140  typedef Array<int>::iterator itr_t;
141  typedef Array<int>::const_iterator citr_t;
142  Array<int> a;
143  a.push_back(1);
146  itr2 = itr1;
147  TEST_ITER_EQUALITY(itr2.current(), a.begin());
148  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
149  TEST_ITER_EQUALITY(itr2.end(), a.end());
150  TEST_EQUALITY(*itr1, *itr2);
151 }
152 
153 
154 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_same )
155 {
156  typedef Array<int>::iterator itr_t;
157  Array<int> a;
158  a.push_back(1);
161  TEST_EQUALITY_CONST(itr2 == itr1, true);
162  TEST_EQUALITY_CONST(itr2 != itr1, false);
163 }
164 
165 
166 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_different )
167 {
168  typedef Array<int>::iterator itr_t;
169  Array<int> a;
170  a.push_back(1);
172  FilteredIterator<itr_t,SelectAll<int> > itr2(a.end(), a.begin(), a.end());
173  TEST_EQUALITY_CONST(itr2 == itr1, false);
174  TEST_EQUALITY_CONST(itr2 != itr1, true);
175 }
176 
177 
178 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_no_filtering )
179 {
180  typedef Array<int>::const_iterator citr_t;
181  Array<int> a = tuple<int>(1, 2, 3);
183  FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
184  TEST_ITER_INEQUALITY(itr, itr_end);
185  TEST_EQUALITY_CONST(*itr, 1);
186  ECHO(++itr);
187  TEST_EQUALITY_CONST(*itr, 2);
188  ECHO(++itr);
189  TEST_EQUALITY_CONST(*itr, 3);
190  ECHO(++itr);
191  TEST_ITER_EQUALITY(itr, itr_end);
192 }
193 
194 
195 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_no_filtering )
196 {
197  typedef Array<int>::const_iterator citr_t;
198  Array<int> a = tuple<int>(1, 2, 3);
200  FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
201  TEST_ITER_INEQUALITY(itr, itr_end);
202  ECHO(const int v0 = *itr++);
203  TEST_EQUALITY_CONST(v0, 1);
204  ECHO(const int v1 = *itr++);
205  TEST_EQUALITY_CONST(v1, 2);
206  ECHO(const int v2 = *itr++);
207  TEST_EQUALITY_CONST(v2, 3);
208  TEST_ITER_EQUALITY(itr, itr_end);
209 }
210 
211 
212 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_no_filtering )
213 {
214  typedef Array<int>::const_iterator citr_t;
215  Array<int> a = tuple<int>(1, 2, 3);
217  FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
218  ECHO(--itr);
219  TEST_EQUALITY_CONST(*itr, 3);
220  ECHO(--itr);
221  TEST_EQUALITY_CONST(*itr, 2);
222  ECHO(--itr);
223  TEST_EQUALITY_CONST(*itr, 1);
224  TEST_ITER_EQUALITY(itr, itr_begin);
225 }
226 
227 
228 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_no_filtering )
229 {
230  typedef Array<int>::const_iterator citr_t;
231  Array<int> a = tuple<int>(1, 2, 3);
233  FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
234  ECHO(--itr);
235  ECHO(const int v0 = *itr--);
236  TEST_EQUALITY_CONST(v0, 3);
237  ECHO(const int v1 = *itr--);
238  TEST_EQUALITY_CONST(v1, 2);
239  ECHO(const int v2 = *itr);
240  TEST_EQUALITY_CONST(v2, 1);
241  TEST_ITER_EQUALITY(itr, itr_begin);
242 }
243 
244 
245 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_even )
246 {
247  typedef Array<int>::const_iterator citr_t;
248  Array<int> a = tuple<int>(1, 2, 3, 4);
250  FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
251  TEST_ITER_INEQUALITY(itr, itr_end);
252  TEST_EQUALITY_CONST(*itr, 2);
253  ECHO(++itr);
254  TEST_EQUALITY_CONST(*itr, 4);
255  ECHO(++itr);
256  TEST_ITER_EQUALITY(itr, itr_end);
257 }
258 
259 
260 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_odd )
261 {
262  typedef Array<int>::const_iterator citr_t;
263  Array<int> a = tuple<int>(1, 2, 3, 4);
265  FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
266  TEST_ITER_INEQUALITY(itr, itr_end);
267  TEST_EQUALITY_CONST(*itr, 1);
268  ECHO(++itr);
269  TEST_EQUALITY_CONST(*itr, 3);
270  ECHO(++itr);
271  TEST_ITER_EQUALITY(itr, itr_end);
272 }
273 
274 
275 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_even )
276 {
277  typedef Array<int>::const_iterator citr_t;
278  Array<int> a = tuple<int>(1, 2, 3, 4);
280  FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
281  TEST_ITER_INEQUALITY(itr, itr_end);
282  ECHO(const int v0 = *itr++);
283  TEST_EQUALITY_CONST(v0, 2);
284  ECHO(const int v1 = *itr++);
285  TEST_EQUALITY_CONST(v1, 4);
286  TEST_ITER_EQUALITY(itr, itr_end);
287 }
288 
289 
290 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_odd )
291 {
292  typedef Array<int>::const_iterator citr_t;
293  Array<int> a = tuple<int>(1, 2, 3, 4);
295  FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
296  TEST_ITER_INEQUALITY(itr, itr_end);
297  ECHO(const int v0 = *itr++);
298  TEST_EQUALITY_CONST(v0, 1);
299  ECHO(const int v1 = *itr++);
300  TEST_EQUALITY_CONST(v1, 3);
301  TEST_ITER_EQUALITY(itr, itr_end);
302 }
303 
304 
305 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_even )
306 {
307  typedef Array<int>::const_iterator citr_t;
308  Array<int> a = tuple<int>(1, 2, 3, 4);
310  FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
311  ECHO(--itr);
312  TEST_EQUALITY_CONST(*itr, 4);
313  ECHO(--itr);
314  TEST_EQUALITY_CONST(*itr, 2);
315  TEST_ITER_EQUALITY(itr, itr_begin);
316 }
317 
318 
319 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_odd )
320 {
321  typedef Array<int>::const_iterator citr_t;
322  Array<int> a = tuple<int>(1, 2, 3, 4);
324  FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
325  ECHO(--itr);
326  TEST_EQUALITY_CONST(*itr, 3);
327  ECHO(--itr);
328  TEST_EQUALITY_CONST(*itr, 1);
329  TEST_ITER_EQUALITY(itr, itr_begin);
330 }
331 
332 
333 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_even )
334 {
335  typedef Array<int>::const_iterator citr_t;
336  Array<int> a = tuple<int>(1, 2, 3, 4);
338  FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
339  ECHO(--itr);
340  ECHO(const int v0 = *itr--);
341  TEST_EQUALITY_CONST(v0, 4);
342  ECHO(const int v1 = *itr);
343  TEST_EQUALITY_CONST(v1, 2);
344  TEST_ITER_EQUALITY(itr, itr_begin);
345 }
346 
347 
348 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_odd )
349 {
350  typedef Array<int>::const_iterator citr_t;
351  Array<int> a = tuple<int>(1, 2, 3, 4);
353  FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
354  ECHO(--itr);
355  ECHO(const int v0 = *itr--);
356  TEST_EQUALITY_CONST(v0, 3);
357  ECHO(const int v1 = *itr);
358  TEST_EQUALITY_CONST(v1, 1);
359  TEST_ITER_EQUALITY(itr, itr_begin);
360 }
361 
362 
363 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
364 
365 
366 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_forward_past_end_throw )
367 {
368  // Need to use an unchecked iterator to make sure class thows
369  int a_raw = 1;
370  FilteredIterator<int*,SelectAll<int> > itr_end((&a_raw)+1, &a_raw, (&a_raw)+1);
371  FilteredIterator<int*,SelectAll<int> > itr = itr_end;
372  TEST_THROW(++itr, RangeError);
373  TEST_ITER_EQUALITY(itr, itr_end);
374  TEST_THROW(itr++, RangeError);
375  TEST_ITER_EQUALITY(itr, itr_end);
376 }
377 
378 
379 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_backward_past_begin_throw )
380 {
381  // Need to use an unchecked iterator to make sure class thows
382  int a_raw = 1;
383  FilteredIterator<int*,SelectAll<int> > itr_begin(&a_raw, &a_raw, (&a_raw)+1);
384  FilteredIterator<int*,SelectAll<int> > itr = itr_begin;
385  TEST_THROW(--itr, RangeError);
386  TEST_ITER_EQUALITY(itr, itr_begin);
387  TEST_THROW(itr--, RangeError);
388  TEST_ITER_EQUALITY(itr, itr_begin);
389 }
390 
391 
392 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
393 
394 
395 } // namespace Teuchos
396 
397 
398 
C++ Standard Library compatable filtered iterator.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
TEUCHOS_UNIT_TEST(ConstNonconstObjectContainer, create)
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Templated array class derived from the STL std::vector.
void push_back(const value_type &x)
std::vector< T >::iterator iterator
The type of a forward iterator.
iterator begin()
Defines basic traits returning the name of a type in a portable and readable way. ...
#define TEST_ITER_INEQUALITY(iter1, iter2)
Assert that two iterators are NOT equal.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...