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

Generated on Tue Oct 20 10:13:58 2009 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.1