Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Array_test.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 #include "Teuchos_Array.hpp"
00043 #include "Teuchos_GlobalMPISession.hpp"
00044 #include "Teuchos_CommandLineProcessor.hpp"
00045 #include "Teuchos_VerboseObject.hpp"
00046 #include "Teuchos_StandardCatchMacros.hpp"
00047 #include "Teuchos_Version.hpp"
00048 #include "Teuchos_getConst.hpp"
00049 #include "Teuchos_as.hpp"
00050 #include "Teuchos_LocalTestingHelpers.hpp"
00051 
00052 #include "TestClasses.hpp"
00053 
00054 
00055 //
00056 // Main templated array test function
00057 //
00058 
00059 
00060 template<class T>
00061 bool testArray( const int n, Teuchos::FancyOStream &out )
00062 {
00063   
00064   using Teuchos::Array;
00065   using Teuchos::ArrayView;
00066   using Teuchos::outArg;
00067   using Teuchos::getConst;
00068   using Teuchos::NullIteratorTraits;
00069   using Teuchos::TypeNameTraits;
00070   using Teuchos::as;
00071   using Teuchos::tuple;
00072   typedef typename Array<T>::size_type size_type;
00073 
00074   bool success = true;
00075  
00076   out
00077     << "\n***"
00078     << "\n*** Testing "<<TypeNameTraits<Array<T> >::name()<<" of size = "<<n
00079     << "\n***\n";
00080   
00081   Teuchos::OSTab tab(out);
00082 
00083   //
00084   out << "\nA) Initial setup ...\n\n";
00085   //
00086 
00087   // Tests construction using size
00088 
00089   Array<T> a(n);
00090 
00091   TEST_EQUALITY_CONST( a.empty(), false );
00092   TEST_EQUALITY( a.length(), n );
00093   TEST_EQUALITY( as<int>(a.size()), n );
00094   TEST_EQUALITY( a.getRawPtr(), &a[0] );
00095   TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
00096   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00097   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00098  
00099   {
00100     out << "\nInitializing data ...\n";
00101     for( int i = 0; i < n; ++i )
00102       a[i] = as<T>(i); // tests non-const operator[](i)
00103   }
00104 
00105   {
00106     out << "\nTest that a[i] == i ... ";
00107     bool local_success = true;
00108     for( int i = 0; i < n; ++i ) {
00109       TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
00110     }
00111     if (local_success) out << "passed\n";
00112     else success = false;
00113   }
00114 
00115   {
00116     out << "\nTest that a.at(i) == i ...\n";
00117     bool local_success = true;
00118     for( int i = 0; i < n; ++i ) {
00119       TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
00120     }
00121     if (local_success) out << "passed\n";
00122     else success = false;
00123   }
00124 
00125   //
00126   out << "\nB) Test constructors, assignment operators etc ...\n";
00127   //
00128 
00129   {
00130     out << "\nTest default constructor ...\n";
00131     Array<T> a2;
00132     TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
00133     TEST_EQUALITY_CONST( as<bool>(a2.empty()), true );
00134     TEST_EQUALITY_CONST( a2.getRawPtr(), 0 );
00135     TEST_EQUALITY_CONST( getConst(a2).getRawPtr(), 0 );
00136   }
00137 
00138   {
00139     out << "\nTest copy conversion to and from Teuchos::Array and std::vector ...\n";
00140     std::vector<T> v2 = createVector(a);
00141     Array<T> a2(v2);
00142     TEST_COMPARE_ARRAYS( a2, a );
00143   }
00144 
00145   {
00146     out << "\nTest assignment operator taking an std::vector ...\n";
00147     std::vector<T> v2 = createVector(a);
00148     Array<T> a2;
00149     a2 = v2;
00150     TEST_COMPARE_ARRAYS( a2, a );
00151   }
00152 
00153   {
00154     out << "\nTest construction using iterators ...\n";
00155     std::vector<T> v2 = createVector(a);
00156     Array<T> a2(a.begin(),a.end());
00157     TEST_COMPARE_ARRAYS( a2, a );
00158   }
00159 
00160   {
00161     out << "\nTest copy construction ...\n";
00162     Array<T> a2(a);
00163     TEST_COMPARE_ARRAYS( a2, a );
00164   }
00165 
00166   {
00167     out << "\nTest array assignment operator ...\n";
00168     Array<T> a2;
00169     a2 = a;
00170     TEST_COMPARE_ARRAYS( a2, a );
00171   }
00172 
00173   {
00174     out << "\nTest array assign(...) ...\n";
00175     Array<T> a2;
00176     a2.assign(a.begin(),a.end());
00177     TEST_COMPARE_ARRAYS( a2, a );
00178   }
00179 
00180   {
00181     out << "\nTest iterator access and then resize ...\n";
00182     Array<T> a2(a);
00183     const Array<T> &ca2 = a2;
00184     Array<T> a3(ca2.begin(),ca2.end());
00185     TEST_COMPARE_ARRAYS( a3, a );
00186     TEST_NOTHROW(a2.resize(0)); // This used to throw exception!
00187   }
00188 
00189   //
00190   out << "\nC) Test element access ...\n";
00191   //
00192 
00193   TEST_EQUALITY_CONST( a.front(), as<T>(0) );
00194   TEST_EQUALITY( a.back(), as<T>(n-1) );
00195 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00196   TEST_THROW( a[-1], Teuchos::RangeError );
00197   TEST_THROW( a[n], Teuchos::RangeError );
00198   TEST_THROW( a.at(-1), Teuchos::RangeError );
00199   TEST_THROW( a.at(n), Teuchos::RangeError );
00200 #else //HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00201   TEST_THROW( a.at(-1), std::out_of_range );
00202   TEST_THROW( a.at(n), std::out_of_range );
00203 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00204 
00205   //
00206   out << "\nD) Test iterator access ...\n";
00207   //
00208 
00209 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00210 
00211   {
00212     out << "\nTesting functions that should throw for empty container ...\n";
00213     Array<T> a2;
00214     TEST_THROW( *a2.begin(), Teuchos::NullReferenceError );
00215     TEST_THROW( a2.front(), Teuchos::NullReferenceError );
00216     TEST_THROW( a2.back(), Teuchos::NullReferenceError );
00217     TEST_THROW( getConst(a2).front(), Teuchos::NullReferenceError );
00218     TEST_THROW( getConst(a2).back(), Teuchos::NullReferenceError );
00219     TEST_THROW( a2.erase(a2.begin()), Teuchos::NullReferenceError );
00220   }
00221 
00222 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00223 
00224   {
00225     out << "\nTest that a2.begin() == a2.end() for empty a2 ...\n";
00226     Array<T> a2;
00227     TEST_ITER_EQUALITY( a2.begin(), a2.end() );
00228   }
00229 
00230   {
00231     out << "\nTest nonconst forward iterator access ... ";
00232     bool local_success = true;
00233     typedef typename Array<T>::iterator iter_t;
00234     iter_t iter = a.begin();
00235     for ( int i = 0; i < n; ++i, ++iter )
00236       TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
00237     iter = NullIteratorTraits<iter_t>::getNull();
00238     if (local_success) out << "passed\n";
00239     else success = false;
00240   }
00241 
00242   {
00243     out << "\nTest const forward iterator access ... ";
00244     bool local_success = true;
00245     typedef typename Array<T>::const_iterator iter_t;
00246     iter_t iter = getConst(a).begin();
00247     for ( int i = 0; i < n; ++i, ++iter )
00248       TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
00249     iter = NullIteratorTraits<iter_t>::getNull();
00250     if (local_success) out << "passed\n";
00251     else success = false;
00252   }
00253 
00254 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00255 
00256   {
00257     out << "\nTest forward iterators dereferenced out of bounds ...\n";
00258     TEST_THROW( *(a.begin()-1), Teuchos::RangeError );
00259     TEST_THROW( *a.end(), Teuchos::RangeError );
00260     TEST_THROW( *(getConst(a).begin()-1), Teuchos::RangeError );
00261     TEST_THROW( *getConst(a).end(), Teuchos::RangeError );
00262   }
00263 
00264 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00265 
00266   {
00267     out << "\nTest that a2.rbegin() == a2.rend() for empty a2 ...\n";
00268     Array<T> a2;
00269     TEST_ITER_EQUALITY( a2.rbegin(), a2.rend() );
00270   }
00271 
00272   {
00273     out << "\nTest nonconst reverse iterator access ... ";
00274     bool local_success = true;
00275     typedef typename Array<T>::reverse_iterator iter_t;
00276     iter_t iter = a.rbegin();
00277     for ( int i = n-1; i >= 0; --i, ++iter )
00278       TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
00279     iter = NullIteratorTraits<iter_t>::getNull();
00280     if (local_success) out << "passed\n";
00281     else success = false;
00282   }
00283 
00284   {
00285     out << "\nTest const reverse iterator access ... ";
00286     bool local_success = true;
00287     typedef typename Array<T>::const_reverse_iterator iter_t;
00288     iter_t iter = getConst(a).rbegin();
00289     for ( int i = n-1; i >= 0; --i, ++iter )
00290       TEST_ARRAY_ELE_EQUALITY( a, i, *iter );
00291     iter = NullIteratorTraits<iter_t>::getNull();
00292     if (local_success) out << "passed\n";
00293     else success = false;
00294   }
00295 
00296 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00297   {
00298     out << "\nTest reverse iterators dereferenced out of bounds ...\n";
00299     TEST_THROW( *(a.rbegin()-1), Teuchos::RangeError );
00300     TEST_THROW( *a.rend(), Teuchos::RangeError );
00301     TEST_THROW( *(getConst(a).rbegin()-1), Teuchos::RangeError );
00302     TEST_THROW( *getConst(a).rend(), Teuchos::RangeError );
00303   }
00304 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00305 
00306   {
00307     out << "\nTest that an iterator reference set to null does not throw ...\n";
00308     typedef typename Array<T>::iterator iter_t;
00309     iter_t iter = NullIteratorTraits<iter_t>::getNull();
00310     TEST_NOTHROW( Array<T> a2(n); iter = a2.begin();
00311       iter = NullIteratorTraits<iter_t>::getNull() );
00312   }
00313 
00314   {
00315     out << "\nTest that a dangling iterator reference throws exception ...\n";
00316     typedef typename Array<T>::iterator iter_t;
00317     iter_t iter = NullIteratorTraits<iter_t>::getNull();
00318     {
00319       Array<T> a2(n);
00320       iter = a2.begin();
00321     }
00322 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00323     TEST_THROW(*iter=0, Teuchos::DanglingReferenceError );
00324 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00325   }
00326 
00327 
00328   //
00329   out << "\nE) Test insertion and deletion functions ...\n";
00330   //
00331 
00332   {
00333     out << "\nTest push_back(x) ...\n";
00334     Array<T> a2;
00335     for ( int i = 0; i < n; ++i ) {
00336       a2.push_back(as<T>(i));
00337       TEST_EQUALITY_CONST(a2.front(),as<T>(0));
00338       TEST_EQUALITY_CONST(getConst(a2).front(),as<T>(0));
00339       TEST_EQUALITY(a2.back(),as<T>(i));
00340       TEST_EQUALITY(getConst(a2).back(),as<T>(i));
00341     }
00342     TEST_COMPARE_ARRAYS( a2, a );
00343   }
00344 
00345   {
00346     out << "\nTest pop_back() ...\n";
00347     Array<T> a2(a);
00348     for ( int i = n-1; i >= 0; --i ) {
00349       TEST_EQUALITY(a2.back(),as<T>(i));
00350       a2.pop_back();
00351     }
00352   }
00353 
00354   {
00355     out << "\nTest insert(iter,x) ...\n";
00356     Array<T> a2;
00357     for ( int i = 0; i < n; ++i ) {
00358       const typename Array<T>::iterator
00359         iter = a2.insert(a2.end(), as<T>(i));
00360       TEST_EQUALITY(*iter, as<T>(i));
00361     }
00362     TEST_COMPARE_ARRAYS( a2, a );
00363   }
00364 
00365   {
00366     out << "\nTest insert(iter,1,x) ...\n";
00367     Array<T> a2;
00368     for ( int i = 0; i < n; ++i )
00369       a2.insert(a2.end(),1,i);
00370     TEST_COMPARE_ARRAYS( a2, a );
00371   }
00372 
00373   {
00374     out << "\nTest insert(iter,first,last) ...\n";
00375     Array<T> a2;
00376     for ( int i = 0; i < n; ++i )
00377       a2.insert(a2.end(),a.begin()+i,a.begin()+i+1);
00378     TEST_COMPARE_ARRAYS( a2, a );
00379   }
00380 
00381   {
00382     out << "\nTest append(x) ...\n";
00383     Array<T> a2;
00384     for ( int i = 0; i < n; ++i )
00385       a2.append(as<T>(i));
00386     TEST_COMPARE_ARRAYS( a2, a );
00387   }
00388 
00389   {
00390     out << "\nTest erase(iter) ...\n";
00391     Array<T> a2(a);
00392     for ( int i = 0; i < n; ++i ) {
00393       TEST_EQUALITY( as<int>(a2.size()), n-i );
00394       TEST_EQUALITY( a2.front(), as<T>(i) );
00395       a2.erase(a2.begin());
00396     }
00397     TEST_EQUALITY_CONST( a2.empty(), true );
00398   }
00399 
00400 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00401 
00402   {
00403     out << "\nTest trying to erase twice with the same iterator which should throw ...\n";
00404     Array<T> a2(a);
00405     const typename Array<T>::iterator iter = a2.begin();
00406     a2.erase(iter); // After this point, the iterator is no longer valid!
00407     // This is no longer a valid iterator and should throw!
00408     TEST_THROW( a2.erase(iter), Teuchos::IncompatibleIteratorsError );
00409   }
00410 
00411 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00412 
00413   // 2007/11/08: rabartl: ToDo: Above, I have tested one use case where the
00414   // iterator should be invalidated and this tests that it throws an exception
00415   // as it should.  However, currently, I don't have code written that will
00416   // catch the problem where the client would try to dereference the iterator
00417   // or something like that.  This is a big no-no.  I could do this by adding
00418   // an is_valid() property to RCP_node and then setting this to null when the
00419   // structure of the iterator changes.  Then, I would have to put asserts in
00420   // ArrayRCP to constantly check is_valid() (with an assert_is_valid()
00421   // function or something) on any call other than operator=(...) which would
00422   // reset this iterator.  Catching all of these user errors is a lot of work!
00423 
00424   {
00425     out << "\nTest remove(i) ...\n";
00426     Array<T> a2(a);
00427     for ( int i = 0; i < n; ++i ) {
00428       TEST_EQUALITY( as<int>(a2.size()), n-i );
00429       TEST_EQUALITY( a2.front(), as<T>(i) );
00430       a2.remove(0); // Always remove the "first" entry!
00431     }
00432     TEST_EQUALITY_CONST( a2.empty(), true );
00433   }
00434 
00435   {
00436     out << "\nTest erase(begin(),end()) ...\n";
00437     Array<T> a2(a);
00438     a2.erase(a2.begin(),a2.end());
00439     TEST_EQUALITY_CONST( a2.empty(), true );
00440   }
00441 
00442   {
00443     out << "\nTest member swap() ...\n";
00444     Array<T> a2(a);
00445     Array<T> a3(a);
00446     for ( int i = 0; i < n; ++i )
00447       a2[i] += as<T>(1);
00448     a2.swap(a3);
00449     TEST_COMPARE_ARRAYS( a2, a );
00450   }
00451 
00452   {
00453     out << "\nTest non-member swap() ...\n";
00454     Array<T> a2(a);
00455     Array<T> a3(a);
00456     for ( int i = 0; i < n; ++i )
00457       a2[i] += as<T>(1);
00458     swap(a2,a3);
00459     TEST_COMPARE_ARRAYS( a2, a );
00460   }
00461 
00462   {
00463     out << "\nTest clear() ...\n";
00464     Array<T> a2(a);
00465     a2.clear();
00466     TEST_EQUALITY_CONST( a2.empty(), true );
00467     TEST_EQUALITY_CONST( a2.size(), 0 );
00468   }
00469 
00470 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00471   {
00472     out << "\nTest to string ...\n";
00473     std::ostringstream o;
00474     o << "{";
00475     for ( int i = 0; i < n; ++i ) {
00476       o << as<T>(i) << ( i < n-1 ? ", " : "" );
00477     }
00478     o << "}";
00479     TEST_EQUALITY( o.str(), a.toString() );
00480   }
00481 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00482 
00483   {
00484     out << "\nTest hasArrayBoundsChecking() ... \n";
00485 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00486     TEST_EQUALITY_CONST( a.hasBoundsChecking(), true );
00487 #else
00488     TEST_EQUALITY_CONST( a.hasBoundsChecking(), false );
00489 #endif
00490   }
00491 
00492   //
00493   out << "\nG) Test views ...\n";
00494   //
00495 
00496   {
00497     out << "\nTest full non-const subview ...\n";
00498     const ArrayView<T> av2 = a(0,n);
00499     TEST_COMPARE_ARRAYS( av2, a );
00500   }
00501 
00502   {
00503     out << "\nTest full shorthand non-const subview ...\n";
00504     const ArrayView<T> av2 = a();
00505     TEST_COMPARE_ARRAYS( av2, a );
00506   }
00507 
00508   {
00509     out << "\nTest full const subview ...\n";
00510     const ArrayView<const T> cav2 = getConst(a)(0, n);
00511     TEST_COMPARE_ARRAYS( cav2, a );
00512   }
00513 
00514   {
00515     out << "\nTest full non-const to const subview ...\n";
00516     const ArrayView<const T> cav2 = a(0, n);
00517     TEST_COMPARE_ARRAYS( cav2, a );
00518   }
00519 
00520   {
00521     out << "\nTest full short-hand const subview ...\n";
00522     const ArrayView<const T> cav2 = getConst(a)();
00523     TEST_COMPARE_ARRAYS( cav2, a );
00524   }
00525 
00526   {
00527     out << "\nTest non-const initial range view ...\n";
00528     Array<T> a2(n,as<T>(-1));
00529     const ArrayView<T> av2 = a2; // Tests implicit conversion!
00530     const ArrayView<T> av2_end = av2(0,n-1);
00531     TEST_EQUALITY( av2_end.size(), n-1 );
00532     av2_end.assign( a(0,n-1) );
00533     av2.back() = as<T>(n-1);
00534     TEST_COMPARE_ARRAYS( a2, a );
00535   }
00536 
00537   {
00538     out << "\nTest non-const middle range view ...\n";
00539     Array<T> a2(n,as<T>(-1));
00540     const ArrayView<T> av2 = a2; // Tests implicit conversion!
00541     const ArrayView<T> av2_middle = av2(1,n-2);
00542     TEST_EQUALITY( av2_middle.size(), n-2 );
00543     av2_middle.assign( a(1,n-2) );
00544     av2.front() = as<T>(0);
00545     av2.back() = as<T>(n-1);
00546     TEST_COMPARE_ARRAYS( a2, a );
00547   }
00548 
00549   {
00550     out << "\nTest const view ... ";
00551     const ArrayView<const T> av2 = a; // Tests implicit conversion to const!
00552     const ArrayView<const T> av2_middle = av2(1,n-2);
00553     TEST_EQUALITY( av2_middle.size(), n-2 );
00554     bool local_success = true;
00555     for ( int i = 0; i < n-2; ++i )
00556       TEST_ARRAY_ELE_EQUALITY( av2_middle, i, as<T>(i+1) );
00557     if (local_success) out << "passed\n";
00558     else success = false;
00559   }
00560 
00561   {
00562     out << "\nTest constructing Array<T> from ArrayView<T> ...\n";
00563     const ArrayView<T> av2 = a;
00564     Array<T> a2(av2);
00565     TEST_COMPARE_ARRAYS( a2, a );
00566   }
00567 
00568   {
00569     out << "\nTest constructing Array<T> from ArrayView<const T> ...\n";
00570     const ArrayView<const T> av2 = a;
00571     Array<T> a2(av2);
00572     TEST_COMPARE_ARRAYS( a2, a );
00573   }
00574 
00575   {
00576     out << "\nTest comparison operators ...\n";
00577     Array<T> a2(a);
00578     TEST_EQUALITY_CONST( (a2==a), true );
00579     TEST_EQUALITY_CONST( (a2!=a), false );
00580     TEST_EQUALITY_CONST( (a2<=a), true );
00581     TEST_EQUALITY_CONST( (a2>=a), true );
00582     TEST_EQUALITY_CONST( (a2<a), false );
00583     TEST_EQUALITY_CONST( (a2>a), false );
00584   }
00585 
00586   //
00587   out << "\nH) Test tuple(...) construction ...\n";
00588   //
00589 
00590   {
00591     const size_type m = 1;
00592     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00593     Array<T> am = tuple<T>(0);
00594     TEST_EQUALITY_CONST(am.size(), m);
00595     out << "Test that am[i] == i ... ";
00596     bool local_success = true;
00597     for( size_type i = 0; i < m; ++i ) {
00598       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00599     }
00600     if (local_success) out << "passed\n";
00601     else success = false;
00602   }
00603 
00604   {
00605     const size_type m = 2;
00606     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00607     Array<T> am = tuple<T>(0,1);
00608     TEST_EQUALITY_CONST(am.size(),m);
00609     out << "Test that am[i] == i ... ";
00610     bool local_success = true;
00611     for( size_type i = 0; i < m; ++i ) {
00612       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00613     }
00614     if (local_success) out << "passed\n";
00615     else success = false;
00616   }
00617 
00618   {
00619     const size_type m = 3;
00620     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00621     Array<T> am = tuple<T>(0,1,2);
00622     TEST_EQUALITY_CONST(am.size(),m);
00623     out << "Test that am[i] == i ... ";
00624     bool local_success = true;
00625     for( size_type i = 0; i < m; ++i ) {
00626       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00627     }
00628     if (local_success) out << "passed\n";
00629     else success = false;
00630   }
00631 
00632   {
00633     const size_type m = 4;
00634     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00635     Array<T> am = tuple<T>(0,1,2,3);
00636     TEST_EQUALITY_CONST(am.size(),m);
00637     out << "Test that am[i] == i ... ";
00638     bool local_success = true;
00639     for( size_type i = 0; i < m; ++i ) {
00640       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00641     }
00642     if (local_success) out << "passed\n";
00643     else success = false;
00644   }
00645 
00646   {
00647     const size_type m = 5;
00648     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00649     Array<T> am = tuple<T>(0,1,2,3,4);
00650     TEST_EQUALITY_CONST(am.size(),m);
00651     out << "Test that am[i] == i ... ";
00652     bool local_success = true;
00653     for( size_type i = 0; i < m; ++i ) {
00654       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00655     }
00656     if (local_success) out << "passed\n";
00657     else success = false;
00658   }
00659 
00660   {
00661     const size_type m = 6;
00662     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00663     Array<T> am = tuple<T>(0,1,2,3,4,5);
00664     TEST_EQUALITY_CONST(am.size(),m);
00665     out << "Test that am[i] == i ... ";
00666     bool local_success = true;
00667     for( size_type i = 0; i < m; ++i ) {
00668       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00669     }
00670     if (local_success) out << "passed\n";
00671     else success = false;
00672   }
00673 
00674   {
00675     const size_type m = 7;
00676     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00677     Array<T> am = tuple<T>(0,1,2,3,4,5,6);
00678     TEST_EQUALITY_CONST(am.size(),m);
00679     out << "Test that am[i] == i ... ";
00680     bool local_success = true;
00681     for( size_type i = 0; i < m; ++i ) {
00682       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00683     }
00684     if (local_success) out << "passed\n";
00685     else success = false;
00686   }
00687 
00688   {
00689     const size_type m = 8;
00690     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00691     Array<T> am = tuple<T>(0,1,2,3,4,5,6,7);
00692     TEST_EQUALITY_CONST(am.size(),m);
00693     out << "Test that am[i] == i ... ";
00694     bool local_success = true;
00695     for( size_type i = 0; i < m; ++i ) {
00696       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00697     }
00698     if (local_success) out << "passed\n";
00699     else success = false;
00700   }
00701 
00702   {
00703     const size_type m = 9;
00704     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00705     Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8);
00706     TEST_EQUALITY_CONST(am.size(),m);
00707     out << "Test that am[i] == i ... ";
00708     bool local_success = true;
00709     for( size_type i = 0; i < m; ++i ) {
00710       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00711     }
00712     if (local_success) out << "passed\n";
00713     else success = false;
00714   }
00715 
00716   {
00717     const size_type m = 10;
00718     out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n";
00719     Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8,9);
00720     TEST_EQUALITY_CONST(am.size(),m);
00721     out << "Test that am[i] == i ... ";
00722     bool local_success = true;
00723     for( size_type i = 0; i < m; ++i ) {
00724       TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) );
00725     }
00726     if (local_success) out << "passed\n";
00727     else success = false;
00728   }
00729 
00730   {
00731     out << "\nTest taking an empty view ...\n";
00732     const ArrayView<T> av = a(0,0);
00733     TEST_EQUALITY_CONST( av.size(), 0 );
00734   }
00735 
00736 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00737   {
00738     out << "\nTest taking views outside of valid range ...\n";
00739     TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError );
00740     TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError );
00741     TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError );
00742   }
00743 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00744   
00745   return success;
00746 
00747 }
00748 
00749 
00750 template<class T>
00751 bool testArrayOpaqueWithoutTNT( const std::string &T_name, const int n,
00752   const T &someValue, Teuchos::FancyOStream &out )
00753 {
00754   
00755   using Teuchos::Array;
00756   using Teuchos::ArrayView;
00757   using Teuchos::TypeNameTraits;
00758   using Teuchos::as;
00759   typedef typename Array<T>::size_type size_type;
00760 
00761   bool success = true;
00762  
00763   out
00764     << "\n***"
00765     << "\n*** Testing Array<"<<T_name<<"> for opaque type without TNT of size = "<<n
00766     << "\n***\n";
00767   
00768   Teuchos::OSTab tab(out);
00769 
00770   //
00771   out << "\nA) Initial setup ...\n\n";
00772   //
00773 
00774   // Tests construction using size
00775 
00776   Array<T> a(n);
00777 
00778   TEST_EQUALITY_CONST( a.empty(), false );
00779   TEST_EQUALITY( a.length(), n );
00780   TEST_EQUALITY( as<int>(a.size()), n );
00781   TEST_EQUALITY( a.getRawPtr(), &a[0] );
00782   TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
00783   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00784   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00785  
00786   {
00787     out << "\nInitializing data ...\n";
00788     for( int i = 0; i < n; ++i )
00789       a[i] = someValue; // tests non-const operator[](i)
00790   }
00791 
00792   {
00793     out << "\nTest that a[i] == "<<someValue<<" ... ";
00794     bool local_success = true;
00795     for( int i = 0; i < n; ++i ) {
00796       TEST_ARRAY_ELE_EQUALITY( a, i, someValue );
00797     }
00798     if (local_success) out << "passed\n";
00799     else success = false;
00800   }
00801 
00802 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00803   {
00804     out << "\nTest taking a view of the array ...\n";
00805     const ArrayView<T> av = a();
00806     TEST_COMPARE_ARRAYS( av, a );
00807   }
00808   // 2008/08/01: rabartl: Above: We can not create an array view of an
00809   // undefined type in debug mode without a specialization of TypeNameTraits.
00810 #endif // not HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00811 
00812   // ToDo: Do we need to be testing other things for opaque objects?
00813   
00814   return success;
00815 
00816 }
00817 
00818 
00819 template<class T>
00820 bool testArrayOpaqueWithTNT( const int n, const T &someValue, Teuchos::FancyOStream &out )
00821 {
00822   
00823   using Teuchos::Array;
00824   using Teuchos::ArrayView;
00825   using Teuchos::TypeNameTraits;
00826   using Teuchos::as;
00827   typedef typename Array<T>::size_type size_type;
00828 
00829   bool success = true;
00830  
00831   out
00832     << "\n***"
00833     << "\n*** Testing "<<TypeNameTraits<Array<T> >::name()<<" for opaque type with TNT of size = "<<n
00834     << "\n***\n";
00835   
00836   Teuchos::OSTab tab(out);
00837 
00838   //
00839   out << "\nA) Initial setup ...\n\n";
00840   //
00841 
00842   // Tests construction using size
00843 
00844   Array<T> a(n);
00845 
00846   TEST_EQUALITY_CONST( a.empty(), false );
00847   TEST_EQUALITY( a.length(), n );
00848   TEST_EQUALITY( as<int>(a.size()), n );
00849   TEST_EQUALITY( a.getRawPtr(), &a[0] );
00850   TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] );
00851   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00852   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00853  
00854   {
00855     out << "\nInitializing data ...\n";
00856     for( int i = 0; i < n; ++i )
00857       a[i] = someValue; // tests non-const operator[](i)
00858   }
00859 
00860   {
00861     out << "\nTest that a[i] == "<<someValue<<" ... ";
00862     bool local_success = true;
00863     for( int i = 0; i < n; ++i ) {
00864       TEST_ARRAY_ELE_EQUALITY( a, i, someValue );
00865     }
00866     if (local_success) out << "passed\n";
00867     else success = false;
00868   }
00869 
00870   {
00871     out << "\nTest taking a view of the array ...\n";
00872     const ArrayView<T> av = a();
00873     TEST_COMPARE_ARRAYS( av, a );
00874   }
00875 
00876 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00877   {
00878     out << "\nTest taking views outside of valid range ...\n";
00879     TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError );
00880     TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError );
00881     TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError );
00882   }
00883 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00884 
00885   // 2008/08/01: rabartl: Above: We can create ArrayViews and any other thing
00886   // that we would like since we have defined a TypeNameTraits class for the
00887   // undefined type.
00888 
00889   // ToDo: Do we need to be testing other things for opaque objects?
00890   
00891   return success;
00892 
00893 }
00894 
00895 
00896 //
00897 // Main testing program
00898 //
00899 
00900 int main( int argc, char* argv[] ) {
00901 
00902   using Teuchos::CommandLineProcessor;
00903   using Teuchos::Array;
00904   
00905   bool success = true;
00906   bool result;
00907  
00908   Teuchos::GlobalMPISession mpiSession(&argc, &argv);
00909   //const int procRank = Teuchos::GlobalMPISession::getRank();
00910  
00911   Teuchos::RCP<Teuchos::FancyOStream>
00912     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00913  
00914   try {
00915     
00916     //
00917     // Read options from the commandline
00918     //
00919 
00920     CommandLineProcessor clp(false); // Don't throw exceptions
00921 
00922     int n = 4;
00923     clp.setOption( "n", &n, "Number of elements in the array" );
00924 
00925     CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00926 
00927     if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00928       *out << "\nEnd Result: TEST FAILED" << std::endl;
00929       return parse_return;
00930     }
00931 
00932     *out << std::endl << Teuchos::Teuchos_Version() << std::endl;
00933  
00934     result = testArray<int>(n,*out);
00935     if (!result) success = false;
00936 
00937     result = testArray<float>(n,*out);
00938     if (!result) success = false;
00939 
00940     result = testArray<double>(n,*out);
00941     if (!result) success = false;
00942 
00943     //result = testArray<std::complex<double> >(n,*out);
00944     //if (!result) success = false;
00945     // 2007/12/03: rabartl: Commented this out so I can test comparison operators
00946 
00947     result = testArrayOpaqueWithoutTNT<Opaque_handle>("Opaque_handle", n,
00948       OPAQUE_HANDLE_NULL, *out);
00949     if (!result) success = false;
00950 
00951     result = testArrayOpaqueWithTNT<Opaque2_handle>(n, OPAQUE2_HANDLE_NULL, *out);
00952     if (!result) success = false;
00953 
00954     result = testArrayOpaqueWithTNT<Opaque3_handle>(n, OPAQUE3_HANDLE_NULL, *out);
00955     if (!result) success = false;
00956  
00957     // ToDo: Fill in the rest of the types!
00958  
00959   }
00960   TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success);
00961  
00962   if (success)
00963     *out << "\nEnd Result: TEST PASSED" << std::endl;
00964   else
00965     *out << "\nEnd Result: TEST FAILED" << std::endl;
00966  
00967   return ( success ? 0 : 1 );
00968  
00969 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines