Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
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
49namespace {
50
51
52template<typename T>
53class SelectAll {
54public:
55 bool operator()(const T& x) const
56 { return true; }
57};
58
59
60template<typename IntegralType>
61class SelectEven {
62public:
63 bool operator()(const IntegralType& x) const
64 { return ( (x % 2) == 0 ); }
65};
66
67
68template<typename IntegralType>
69class SelectOdd {
70public:
71 bool operator()(const IntegralType& x) const
72 { return ( (x % 2) != 0 ); }
73};
74
75
76} // namespace
77
78
79namespace Teuchos {
80
81
83{
85 // There is just nothing that we can check for an uninitialized iterator!
86}
87
88
90{
93 a.push_back(1);
94 FilteredIterator<itr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
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{
109 a.push_back(2);
110 FilteredIterator<itr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
112}
113
114
116{
117 typedef std::pair<int,int> value_t;
120 a.push_back(std::make_pair(2, 4));
121 FilteredIterator<itr_t,SelectAll<value_t> > itr(a.begin(), a.begin(), a.end());
122 TEST_EQUALITY_CONST(itr->first, 2);
123 TEST_EQUALITY_CONST(itr->second, 4);
124}
125
126
128{
131 a.push_back(1);
132 FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
134 TEST_ITER_EQUALITY(itr2.current(), a.begin());
135 TEST_ITER_EQUALITY(itr2.begin(), a.begin());
136 TEST_ITER_EQUALITY(itr2.end(), a.end());
138}
139
140
142{
146 a.push_back(1);
147 FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
149 TEST_ITER_EQUALITY(itr2.current(), a.begin());
150 TEST_ITER_EQUALITY(itr2.begin(), a.begin());
151 TEST_ITER_EQUALITY(itr2.end(), a.end());
153}
154
155
157{
160 a.push_back(1);
161 FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
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());
168}
169
170
172{
176 a.push_back(1);
177 FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
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());
184}
185
186
197
198
200{
203 a.push_back(1);
204 FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
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
226
227
229{
231 Array<int> a = tuple<int>(1, 2, 3);
232 FilteredIterator<citr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
233 FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
235 ECHO(const int v0 = *itr++);
237 ECHO(const int v1 = *itr++);
239 ECHO(const int v2 = *itr++);
242}
243
244
259
260
262{
264 Array<int> a = tuple<int>(1, 2, 3);
265 FilteredIterator<citr_t,SelectAll<int> > itr(a.end(), a.begin(), a.end());
266 FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
267 ECHO(--itr);
268 ECHO(const int v0 = *itr--);
270 ECHO(const int v1 = *itr--);
272 ECHO(const int v2 = *itr);
275}
276
277
291
292
306
307
321
322
336
337
350
351
364
365
367{
369 Array<int> a = tuple<int>(1, 2, 3, 4);
370 FilteredIterator<citr_t,SelectEven<int> > itr(a.end(), a.begin(), a.end());
371 FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
372 ECHO(--itr);
373 ECHO(const int v0 = *itr--);
375 ECHO(const int v1 = *itr);
378}
379
380
382{
384 Array<int> a = tuple<int>(1, 2, 3, 4);
385 FilteredIterator<citr_t,SelectOdd<int> > itr(a.end(), a.begin(), a.end());
386 FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
387 ECHO(--itr);
388 ECHO(const int v0 = *itr--);
390 ECHO(const int v1 = *itr);
393}
394
395
396#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
397
398
399TEUCHOS_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
412TEUCHOS_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
Templated array class derived from the STL std::vector.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement 'code' throws the exception 'ExceptType' (otherwise the test fails).
#define TEST_ITER_INEQUALITY(iter1, iter2)
Assert that two iterators are NOT equal.
#define ECHO(statement)
Echo the given statement before it is executed.
#define TEST_ITER_EQUALITY(iter1, iter2)
Assert that two iterators are equal.
Defines basic traits returning the name of a type in a portable and readable way.
Unit testing support.
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
std::vector< T >::iterator iterator
The type of a forward iterator.
std::vector< T >::const_iterator const_iterator
The type of a const forward iterator.
C++ Standard Library compatable filtered iterator.
Concrete serial communicator subclass.