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 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 
44 #include "Teuchos_Array.hpp"
47 
48 
49 namespace {
50 
51 
52 template<typename T>
53 class SelectAll {
54 public:
55  bool operator()(const T& x) const
56  { return true; }
57 };
58 
59 
60 template<typename IntegralType>
61 class SelectEven {
62 public:
63  bool operator()(const IntegralType& x) const
64  { return ( (x % 2) == 0 ); }
65 };
66 
67 
68 template<typename IntegralType>
69 class SelectOdd {
70 public:
71  bool operator()(const IntegralType& x) const
72  { return ( (x % 2) != 0 ); }
73 };
74 
75 
76 } // namespace
77 
78 
79 namespace Teuchos {
80 
81 
82 TEUCHOS_UNIT_TEST( FilteredIterator, default_construct )
83 {
85  // There is just nothing that we can check for an uninitialized iterator!
86 }
87 
88 
90 {
91  typedef Array<int>::iterator itr_t;
92  Array<int> a;
93  a.push_back(1);
95  TEST_ITER_EQUALITY(itr.current(), a.begin());
96  TEST_ITER_EQUALITY(itr.begin(), a.begin());
97  TEST_ITER_EQUALITY(itr.end(), a.end());
98  FilteredIterator<itr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
99  TEST_ITER_EQUALITY(itr_end.current(), a.end());
100  TEST_ITER_EQUALITY(itr_end.begin(), a.begin());
101  TEST_ITER_EQUALITY(itr_end.end(), a.end());
102 }
103 
104 
106 {
107  typedef Array<int>::iterator itr_t;
108  Array<int> a;
109  a.push_back(2);
111  TEST_EQUALITY_CONST(*itr, 2);
112 }
113 
114 
116 {
117  typedef std::pair<int,int> value_t;
118  typedef Array<value_t>::iterator itr_t;
119  Array<value_t> a;
120  a.push_back(std::make_pair(2, 4));
122  TEST_EQUALITY_CONST(itr->first, 2);
123  TEST_EQUALITY_CONST(itr->second, 4);
124 }
125 
126 
127 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_same )
128 {
129  typedef Array<int>::iterator itr_t;
130  Array<int> a;
131  a.push_back(1);
134  TEST_ITER_EQUALITY(itr2.current(), a.begin());
135  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
136  TEST_ITER_EQUALITY(itr2.end(), a.end());
137  TEST_EQUALITY(*itr1, *itr2);
138 }
139 
140 
141 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_different )
142 {
143  typedef Array<int>::iterator itr_t;
144  typedef Array<int>::const_iterator citr_t;
145  Array<int> a;
146  a.push_back(1);
149  TEST_ITER_EQUALITY(itr2.current(), a.begin());
150  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
151  TEST_ITER_EQUALITY(itr2.end(), a.end());
152  TEST_EQUALITY(*itr1, *itr2);
153 }
154 
155 
157 {
158  typedef Array<int>::iterator itr_t;
159  Array<int> a;
160  a.push_back(1);
163  itr2 = itr1;
164  TEST_ITER_EQUALITY(itr2.current(), a.begin());
165  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
166  TEST_ITER_EQUALITY(itr2.end(), a.end());
167  TEST_EQUALITY(*itr1, *itr2);
168 }
169 
170 
172 {
173  typedef Array<int>::iterator itr_t;
174  typedef Array<int>::const_iterator citr_t;
175  Array<int> a;
176  a.push_back(1);
179  itr2 = itr1;
180  TEST_ITER_EQUALITY(itr2.current(), a.begin());
181  TEST_ITER_EQUALITY(itr2.begin(), a.begin());
182  TEST_ITER_EQUALITY(itr2.end(), a.end());
183  TEST_EQUALITY(*itr1, *itr2);
184 }
185 
186 
187 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_same )
188 {
189  typedef Array<int>::iterator itr_t;
190  Array<int> a;
191  a.push_back(1);
194  TEST_EQUALITY_CONST(itr2 == itr1, true);
195  TEST_EQUALITY_CONST(itr2 != itr1, false);
196 }
197 
198 
199 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_different )
200 {
201  typedef Array<int>::iterator itr_t;
202  Array<int> a;
203  a.push_back(1);
205  FilteredIterator<itr_t,SelectAll<int> > itr2(a.end(), a.begin(), a.end());
206  TEST_EQUALITY_CONST(itr2 == itr1, false);
207  TEST_EQUALITY_CONST(itr2 != itr1, true);
208 }
209 
210 
211 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_no_filtering )
212 {
213  typedef Array<int>::const_iterator citr_t;
214  Array<int> a = tuple<int>(1, 2, 3);
216  FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
217  TEST_ITER_INEQUALITY(itr, itr_end);
218  TEST_EQUALITY_CONST(*itr, 1);
219  ECHO(++itr);
220  TEST_EQUALITY_CONST(*itr, 2);
221  ECHO(++itr);
222  TEST_EQUALITY_CONST(*itr, 3);
223  ECHO(++itr);
224  TEST_ITER_EQUALITY(itr, itr_end);
225 }
226 
227 
228 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_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_end(a.end(), a.begin(), a.end());
234  TEST_ITER_INEQUALITY(itr, itr_end);
235  ECHO(const int v0 = *itr++);
236  TEST_EQUALITY_CONST(v0, 1);
237  ECHO(const int v1 = *itr++);
238  TEST_EQUALITY_CONST(v1, 2);
239  ECHO(const int v2 = *itr++);
240  TEST_EQUALITY_CONST(v2, 3);
241  TEST_ITER_EQUALITY(itr, itr_end);
242 }
243 
244 
245 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_no_filtering )
246 {
247  typedef Array<int>::const_iterator citr_t;
248  Array<int> a = tuple<int>(1, 2, 3);
250  FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
251  ECHO(--itr);
252  TEST_EQUALITY_CONST(*itr, 3);
253  ECHO(--itr);
254  TEST_EQUALITY_CONST(*itr, 2);
255  ECHO(--itr);
256  TEST_EQUALITY_CONST(*itr, 1);
257  TEST_ITER_EQUALITY(itr, itr_begin);
258 }
259 
260 
261 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_no_filtering )
262 {
263  typedef Array<int>::const_iterator citr_t;
264  Array<int> a = tuple<int>(1, 2, 3);
266  FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
267  ECHO(--itr);
268  ECHO(const int v0 = *itr--);
269  TEST_EQUALITY_CONST(v0, 3);
270  ECHO(const int v1 = *itr--);
271  TEST_EQUALITY_CONST(v1, 2);
272  ECHO(const int v2 = *itr);
273  TEST_EQUALITY_CONST(v2, 1);
274  TEST_ITER_EQUALITY(itr, itr_begin);
275 }
276 
277 
278 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_even )
279 {
280  typedef Array<int>::const_iterator citr_t;
281  Array<int> a = tuple<int>(1, 2, 3, 4);
283  FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
284  TEST_ITER_INEQUALITY(itr, itr_end);
285  TEST_EQUALITY_CONST(*itr, 2);
286  ECHO(++itr);
287  TEST_EQUALITY_CONST(*itr, 4);
288  ECHO(++itr);
289  TEST_ITER_EQUALITY(itr, itr_end);
290 }
291 
292 
293 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_odd )
294 {
295  typedef Array<int>::const_iterator citr_t;
296  Array<int> a = tuple<int>(1, 2, 3, 4);
298  FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
299  TEST_ITER_INEQUALITY(itr, itr_end);
300  TEST_EQUALITY_CONST(*itr, 1);
301  ECHO(++itr);
302  TEST_EQUALITY_CONST(*itr, 3);
303  ECHO(++itr);
304  TEST_ITER_EQUALITY(itr, itr_end);
305 }
306 
307 
308 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_even )
309 {
310  typedef Array<int>::const_iterator citr_t;
311  Array<int> a = tuple<int>(1, 2, 3, 4);
313  FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
314  TEST_ITER_INEQUALITY(itr, itr_end);
315  ECHO(const int v0 = *itr++);
316  TEST_EQUALITY_CONST(v0, 2);
317  ECHO(const int v1 = *itr++);
318  TEST_EQUALITY_CONST(v1, 4);
319  TEST_ITER_EQUALITY(itr, itr_end);
320 }
321 
322 
323 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_odd )
324 {
325  typedef Array<int>::const_iterator citr_t;
326  Array<int> a = tuple<int>(1, 2, 3, 4);
328  FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
329  TEST_ITER_INEQUALITY(itr, itr_end);
330  ECHO(const int v0 = *itr++);
331  TEST_EQUALITY_CONST(v0, 1);
332  ECHO(const int v1 = *itr++);
333  TEST_EQUALITY_CONST(v1, 3);
334  TEST_ITER_EQUALITY(itr, itr_end);
335 }
336 
337 
338 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_even )
339 {
340  typedef Array<int>::const_iterator citr_t;
341  Array<int> a = tuple<int>(1, 2, 3, 4);
343  FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
344  ECHO(--itr);
345  TEST_EQUALITY_CONST(*itr, 4);
346  ECHO(--itr);
347  TEST_EQUALITY_CONST(*itr, 2);
348  TEST_ITER_EQUALITY(itr, itr_begin);
349 }
350 
351 
352 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_odd )
353 {
354  typedef Array<int>::const_iterator citr_t;
355  Array<int> a = tuple<int>(1, 2, 3, 4);
357  FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
358  ECHO(--itr);
359  TEST_EQUALITY_CONST(*itr, 3);
360  ECHO(--itr);
361  TEST_EQUALITY_CONST(*itr, 1);
362  TEST_ITER_EQUALITY(itr, itr_begin);
363 }
364 
365 
366 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_even )
367 {
368  typedef Array<int>::const_iterator citr_t;
369  Array<int> a = tuple<int>(1, 2, 3, 4);
371  FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
372  ECHO(--itr);
373  ECHO(const int v0 = *itr--);
374  TEST_EQUALITY_CONST(v0, 4);
375  ECHO(const int v1 = *itr);
376  TEST_EQUALITY_CONST(v1, 2);
377  TEST_ITER_EQUALITY(itr, itr_begin);
378 }
379 
380 
381 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_odd )
382 {
383  typedef Array<int>::const_iterator citr_t;
384  Array<int> a = tuple<int>(1, 2, 3, 4);
386  FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
387  ECHO(--itr);
388  ECHO(const int v0 = *itr--);
389  TEST_EQUALITY_CONST(v0, 3);
390  ECHO(const int v1 = *itr);
391  TEST_EQUALITY_CONST(v1, 1);
392  TEST_ITER_EQUALITY(itr, itr_begin);
393 }
394 
395 
396 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
397 
398 
399 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_forward_past_end_throw )
400 {
401  // Need to use an unchecked iterator to make sure class thows
402  int a_raw = 1;
403  FilteredIterator<int*,SelectAll<int> > itr_end((&a_raw)+1, &a_raw, (&a_raw)+1);
404  FilteredIterator<int*,SelectAll<int> > itr = itr_end;
405  TEST_THROW(++itr, RangeError);
406  TEST_ITER_EQUALITY(itr, itr_end);
407  TEST_THROW(itr++, RangeError);
408  TEST_ITER_EQUALITY(itr, itr_end);
409 }
410 
411 
412 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_backward_past_begin_throw )
413 {
414  // Need to use an unchecked iterator to make sure class thows
415  int a_raw = 1;
416  FilteredIterator<int*,SelectAll<int> > itr_begin(&a_raw, &a_raw, (&a_raw)+1);
417  FilteredIterator<int*,SelectAll<int> > itr = itr_begin;
418  TEST_THROW(--itr, RangeError);
419  TEST_ITER_EQUALITY(itr, itr_begin);
420  TEST_THROW(itr--, RangeError);
421  TEST_ITER_EQUALITY(itr, itr_begin);
422 }
423 
424 
425 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
426 
427 
428 } // namespace Teuchos
429 
430 
431 
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...