Teuchos Package Browser (Single Doxygen Collection) Version of the Day
FilteredIterator_UnitTests.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 
00043 #include "Teuchos_FilteredIterator.hpp"
00044 #include "Teuchos_Array.hpp"
00045 #include "Teuchos_TypeNameTraits.hpp"
00046 #include "Teuchos_UnitTestHarness.hpp"
00047 
00048 
00049 namespace {
00050 
00051 
00052 template<typename T>
00053 class SelectAll {
00054 public:
00055   bool operator()(const T& x) const
00056     { return true; }
00057 };
00058 
00059 
00060 template<typename IntegralType>
00061 class SelectEven {
00062 public:
00063   bool operator()(const IntegralType& x) const
00064     { return ( (x % 2) == 0 ); }
00065 };
00066 
00067 
00068 template<typename IntegralType>
00069 class SelectOdd {
00070 public:
00071   bool operator()(const IntegralType& x) const
00072     { return ( (x % 2) != 0 ); }
00073 };
00074 
00075 
00076 } // namespace
00077 
00078 
00079 namespace Teuchos {
00080 
00081 
00082 TEUCHOS_UNIT_TEST( FilteredIterator, default_construct )
00083 {
00084   FilteredIterator<int*, SelectAll<int> > itr;
00085   // There is just nothing that we can check for an uninitialized iterator!
00086 }
00087 
00088 
00089 TEUCHOS_UNIT_TEST( FilteredIterator, construct )
00090 {
00091   typedef Array<int>::iterator itr_t;
00092   Array<int> a;
00093   a.push_back(1);
00094   FilteredIterator<itr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
00095   TEST_ITER_EQUALITY(itr.current(), a.begin());
00096   TEST_ITER_EQUALITY(itr.begin(), a.begin());
00097   TEST_ITER_EQUALITY(itr.end(), a.end());
00098   FilteredIterator<itr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
00099   TEST_ITER_EQUALITY(itr_end.current(), a.end());
00100   TEST_ITER_EQUALITY(itr_end.begin(), a.begin());
00101   TEST_ITER_EQUALITY(itr_end.end(), a.end());
00102 }
00103 
00104 
00105 TEUCHOS_UNIT_TEST( FilteredIterator, deref )
00106 {
00107   typedef Array<int>::iterator itr_t;
00108   Array<int> a;
00109   a.push_back(2);
00110   FilteredIterator<itr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
00111   TEST_EQUALITY_CONST(*itr, 2);
00112 }
00113 
00114 
00115 TEUCHOS_UNIT_TEST( FilteredIterator, member_access )
00116 {
00117   typedef std::pair<int,int> value_t;
00118   typedef Array<value_t>::iterator itr_t;
00119   Array<value_t> a;
00120   a.push_back(std::make_pair(2, 4));
00121   FilteredIterator<itr_t,SelectAll<value_t> > itr(a.begin(), a.begin(), a.end());
00122   TEST_EQUALITY_CONST(itr->first, 2);
00123   TEST_EQUALITY_CONST(itr->second, 4);
00124 }
00125 
00126 
00127 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_same )
00128 {
00129   typedef Array<int>::iterator itr_t;
00130   Array<int> a;
00131   a.push_back(1);
00132   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00133   FilteredIterator<itr_t,SelectAll<int> > itr2(itr1);
00134   TEST_ITER_EQUALITY(itr2.current(), a.begin());
00135   TEST_ITER_EQUALITY(itr2.begin(), a.begin());
00136   TEST_ITER_EQUALITY(itr2.end(), a.end());
00137   TEST_EQUALITY(*itr1, *itr2);
00138 }
00139 
00140 
00141 TEUCHOS_UNIT_TEST( FilteredIterator, copy_construct_different )
00142 {
00143   typedef Array<int>::iterator itr_t;
00144   typedef Array<int>::const_iterator citr_t;
00145   Array<int> a;
00146   a.push_back(1);
00147   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00148   FilteredIterator<citr_t,SelectAll<int> > itr2(itr1);
00149   TEST_ITER_EQUALITY(itr2.current(), a.begin());
00150   TEST_ITER_EQUALITY(itr2.begin(), a.begin());
00151   TEST_ITER_EQUALITY(itr2.end(), a.end());
00152   TEST_EQUALITY(*itr1, *itr2);
00153 }
00154 
00155 
00156 TEUCHOS_UNIT_TEST( FilteredIterator, assign_same )
00157 {
00158   typedef Array<int>::iterator itr_t;
00159   Array<int> a;
00160   a.push_back(1);
00161   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00162   FilteredIterator<itr_t,SelectAll<int> > itr2;
00163   itr2 = itr1;
00164   TEST_ITER_EQUALITY(itr2.current(), a.begin());
00165   TEST_ITER_EQUALITY(itr2.begin(), a.begin());
00166   TEST_ITER_EQUALITY(itr2.end(), a.end());
00167   TEST_EQUALITY(*itr1, *itr2);
00168 }
00169 
00170 
00171 TEUCHOS_UNIT_TEST( FilteredIterator, assign_different )
00172 {
00173   typedef Array<int>::iterator itr_t;
00174   typedef Array<int>::const_iterator citr_t;
00175   Array<int> a;
00176   a.push_back(1);
00177   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00178   FilteredIterator<citr_t,SelectAll<int> > itr2;
00179   itr2 = itr1;
00180   TEST_ITER_EQUALITY(itr2.current(), a.begin());
00181   TEST_ITER_EQUALITY(itr2.begin(), a.begin());
00182   TEST_ITER_EQUALITY(itr2.end(), a.end());
00183   TEST_EQUALITY(*itr1, *itr2);
00184 }
00185 
00186 
00187 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_same )
00188 {
00189   typedef Array<int>::iterator itr_t;
00190   Array<int> a;
00191   a.push_back(1);
00192   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00193   FilteredIterator<itr_t,SelectAll<int> > itr2(itr1);
00194   TEST_EQUALITY_CONST(itr2 == itr1, true);
00195   TEST_EQUALITY_CONST(itr2 != itr1, false);
00196 }
00197 
00198 
00199 TEUCHOS_UNIT_TEST( FilteredIterator, equality_operators_different )
00200 {
00201   typedef Array<int>::iterator itr_t;
00202   Array<int> a;
00203   a.push_back(1);
00204   FilteredIterator<itr_t,SelectAll<int> > itr1(a.begin(), a.begin(), a.end());
00205   FilteredIterator<itr_t,SelectAll<int> > itr2(a.end(), a.begin(), a.end());
00206   TEST_EQUALITY_CONST(itr2 == itr1, false);
00207   TEST_EQUALITY_CONST(itr2 != itr1, true);
00208 }
00209 
00210 
00211 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_no_filtering )
00212 {
00213   typedef Array<int>::const_iterator citr_t;
00214   Array<int> a = tuple<int>(1, 2, 3);
00215   FilteredIterator<citr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
00216   FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
00217   TEST_ITER_INEQUALITY(itr, itr_end);
00218   TEST_EQUALITY_CONST(*itr, 1);
00219   ECHO(++itr);
00220   TEST_EQUALITY_CONST(*itr, 2);
00221   ECHO(++itr);
00222   TEST_EQUALITY_CONST(*itr, 3);
00223   ECHO(++itr);
00224   TEST_ITER_EQUALITY(itr, itr_end);
00225 }
00226 
00227 
00228 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_no_filtering )
00229 {
00230   typedef Array<int>::const_iterator citr_t;
00231   Array<int> a = tuple<int>(1, 2, 3);
00232   FilteredIterator<citr_t,SelectAll<int> > itr(a.begin(), a.begin(), a.end());
00233   FilteredIterator<citr_t,SelectAll<int> > itr_end(a.end(), a.begin(), a.end());
00234   TEST_ITER_INEQUALITY(itr, itr_end);
00235   ECHO(const int v0 = *itr++);
00236   TEST_EQUALITY_CONST(v0, 1);
00237   ECHO(const int v1 = *itr++);
00238   TEST_EQUALITY_CONST(v1, 2);
00239   ECHO(const int v2 = *itr++);
00240   TEST_EQUALITY_CONST(v2, 3);
00241   TEST_ITER_EQUALITY(itr, itr_end);
00242 }
00243 
00244 
00245 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_no_filtering )
00246 {
00247   typedef Array<int>::const_iterator citr_t;
00248   Array<int> a = tuple<int>(1, 2, 3);
00249   FilteredIterator<citr_t,SelectAll<int> > itr(a.end(), a.begin(), a.end());
00250   FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
00251   ECHO(--itr);
00252   TEST_EQUALITY_CONST(*itr, 3);
00253   ECHO(--itr);
00254   TEST_EQUALITY_CONST(*itr, 2);
00255   ECHO(--itr);
00256   TEST_EQUALITY_CONST(*itr, 1);
00257   TEST_ITER_EQUALITY(itr, itr_begin);
00258 }
00259 
00260 
00261 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_no_filtering )
00262 {
00263   typedef Array<int>::const_iterator citr_t;
00264   Array<int> a = tuple<int>(1, 2, 3);
00265   FilteredIterator<citr_t,SelectAll<int> > itr(a.end(), a.begin(), a.end());
00266   FilteredIterator<citr_t,SelectAll<int> > itr_begin(a.begin(), a.begin(), a.end());
00267   ECHO(--itr);
00268   ECHO(const int v0 = *itr--);
00269   TEST_EQUALITY_CONST(v0, 3);
00270   ECHO(const int v1 = *itr--);
00271   TEST_EQUALITY_CONST(v1, 2);
00272   ECHO(const int v2 = *itr);
00273   TEST_EQUALITY_CONST(v2, 1);
00274   TEST_ITER_EQUALITY(itr, itr_begin);
00275 }
00276 
00277 
00278 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_even )
00279 {
00280   typedef Array<int>::const_iterator citr_t;
00281   Array<int> a = tuple<int>(1, 2, 3, 4);
00282   FilteredIterator<citr_t,SelectEven<int> > itr(a.begin(), a.begin(), a.end());
00283   FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
00284   TEST_ITER_INEQUALITY(itr, itr_end);
00285   TEST_EQUALITY_CONST(*itr, 2);
00286   ECHO(++itr);
00287   TEST_EQUALITY_CONST(*itr, 4);
00288   ECHO(++itr);
00289   TEST_ITER_EQUALITY(itr, itr_end);
00290 }
00291 
00292 
00293 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_forward_filter_odd )
00294 {
00295   typedef Array<int>::const_iterator citr_t;
00296   Array<int> a = tuple<int>(1, 2, 3, 4);
00297   FilteredIterator<citr_t,SelectOdd<int> > itr(a.begin(), a.begin(), a.end());
00298   FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
00299   TEST_ITER_INEQUALITY(itr, itr_end);
00300   TEST_EQUALITY_CONST(*itr, 1);
00301   ECHO(++itr);
00302   TEST_EQUALITY_CONST(*itr, 3);
00303   ECHO(++itr);
00304   TEST_ITER_EQUALITY(itr, itr_end);
00305 }
00306 
00307 
00308 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_even )
00309 {
00310   typedef Array<int>::const_iterator citr_t;
00311   Array<int> a = tuple<int>(1, 2, 3, 4);
00312   FilteredIterator<citr_t,SelectEven<int> > itr(a.begin(), a.begin(), a.end());
00313   FilteredIterator<citr_t,SelectEven<int> > itr_end(a.end(), a.begin(), a.end());
00314   TEST_ITER_INEQUALITY(itr, itr_end);
00315   ECHO(const int v0 = *itr++);
00316   TEST_EQUALITY_CONST(v0, 2);
00317   ECHO(const int v1 = *itr++);
00318   TEST_EQUALITY_CONST(v1, 4);
00319   TEST_ITER_EQUALITY(itr, itr_end);
00320 }
00321 
00322 
00323 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_forward_filter_odd )
00324 {
00325   typedef Array<int>::const_iterator citr_t;
00326   Array<int> a = tuple<int>(1, 2, 3, 4);
00327   FilteredIterator<citr_t,SelectOdd<int> > itr(a.begin(), a.begin(), a.end());
00328   FilteredIterator<citr_t,SelectOdd<int> > itr_end(a.end(), a.begin(), a.end());
00329   TEST_ITER_INEQUALITY(itr, itr_end);
00330   ECHO(const int v0 = *itr++);
00331   TEST_EQUALITY_CONST(v0, 1);
00332   ECHO(const int v1 = *itr++);
00333   TEST_EQUALITY_CONST(v1, 3);
00334   TEST_ITER_EQUALITY(itr, itr_end);
00335 }
00336 
00337 
00338 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_even )
00339 {
00340   typedef Array<int>::const_iterator citr_t;
00341   Array<int> a = tuple<int>(1, 2, 3, 4);
00342   FilteredIterator<citr_t,SelectEven<int> > itr(a.end(), a.begin(), a.end());
00343   FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
00344   ECHO(--itr);
00345   TEST_EQUALITY_CONST(*itr, 4);
00346   ECHO(--itr);
00347   TEST_EQUALITY_CONST(*itr, 2);
00348   TEST_ITER_EQUALITY(itr, itr_begin);
00349 }
00350 
00351 
00352 TEUCHOS_UNIT_TEST( FilteredIterator, pre_iterate_backward_filter_odd )
00353 {
00354   typedef Array<int>::const_iterator citr_t;
00355   Array<int> a = tuple<int>(1, 2, 3, 4);
00356   FilteredIterator<citr_t,SelectOdd<int> > itr(a.end(), a.begin(), a.end());
00357   FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
00358   ECHO(--itr);
00359   TEST_EQUALITY_CONST(*itr, 3);
00360   ECHO(--itr);
00361   TEST_EQUALITY_CONST(*itr, 1);
00362   TEST_ITER_EQUALITY(itr, itr_begin);
00363 }
00364 
00365 
00366 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_even )
00367 {
00368   typedef Array<int>::const_iterator citr_t;
00369   Array<int> a = tuple<int>(1, 2, 3, 4);
00370   FilteredIterator<citr_t,SelectEven<int> > itr(a.end(), a.begin(), a.end());
00371   FilteredIterator<citr_t,SelectEven<int> > itr_begin(a.begin(), a.begin(), a.end());
00372   ECHO(--itr);
00373   ECHO(const int v0 = *itr--);
00374   TEST_EQUALITY_CONST(v0, 4);
00375   ECHO(const int v1 = *itr);
00376   TEST_EQUALITY_CONST(v1, 2);
00377   TEST_ITER_EQUALITY(itr, itr_begin);
00378 }
00379 
00380 
00381 TEUCHOS_UNIT_TEST( FilteredIterator, post_iterate_backward_filter_odd )
00382 {
00383   typedef Array<int>::const_iterator citr_t;
00384   Array<int> a = tuple<int>(1, 2, 3, 4);
00385   FilteredIterator<citr_t,SelectOdd<int> > itr(a.end(), a.begin(), a.end());
00386   FilteredIterator<citr_t,SelectOdd<int> > itr_begin(a.begin(), a.begin(), a.end());
00387   ECHO(--itr);
00388   ECHO(const int v0 = *itr--);
00389   TEST_EQUALITY_CONST(v0, 3);
00390   ECHO(const int v1 = *itr);
00391   TEST_EQUALITY_CONST(v1, 1);
00392   TEST_ITER_EQUALITY(itr, itr_begin);
00393 }
00394 
00395 
00396 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00397 
00398 
00399 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_forward_past_end_throw )
00400 {
00401   // Need to use an unchecked iterator to make sure class thows
00402   int a_raw = 1;
00403   FilteredIterator<int*,SelectAll<int> > itr_end((&a_raw)+1, &a_raw, (&a_raw)+1);
00404   FilteredIterator<int*,SelectAll<int> > itr = itr_end;
00405   TEST_THROW(++itr, RangeError);
00406   TEST_ITER_EQUALITY(itr, itr_end);
00407   TEST_THROW(itr++, RangeError);
00408   TEST_ITER_EQUALITY(itr, itr_end);
00409 }
00410 
00411 
00412 TEUCHOS_UNIT_TEST( FilteredIterator, iterate_backward_past_begin_throw )
00413 {
00414   // Need to use an unchecked iterator to make sure class thows
00415   int a_raw = 1;
00416   FilteredIterator<int*,SelectAll<int> > itr_begin(&a_raw, &a_raw, (&a_raw)+1);
00417   FilteredIterator<int*,SelectAll<int> > itr = itr_begin;
00418   TEST_THROW(--itr, RangeError);
00419   TEST_ITER_EQUALITY(itr, itr_begin);
00420   TEST_THROW(itr--, RangeError);
00421   TEST_ITER_EQUALITY(itr, itr_begin);
00422 }
00423 
00424 
00425 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00426 
00427 
00428 } // namespace Teuchos
00429 
00430 
00431 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines