|
Teuchos Package Browser (Single Doxygen Collection) Version of the Day
|
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 out << "\nTest reverse iterators dereferenced out of bounds ...\n"; 00286 TEST_THROW( *(a.rbegin()-1), Teuchos::RangeError ); 00287 TEST_THROW( *a.rend(), Teuchos::RangeError ); 00288 TEST_THROW( *(getConst(a).rbegin()-1), Teuchos::RangeError ); 00289 TEST_THROW( *getConst(a).rend(), Teuchos::RangeError ); 00290 } 00291 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 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 { 00306 Array<T> a2(n); 00307 iter = a2.begin(); 00308 } 00309 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00310 TEST_THROW(*iter=0, Teuchos::DanglingReferenceError ); 00311 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00312 } 00313 00314 00315 // 00316 out << "\nE) Test insertion and deletion functions ...\n"; 00317 // 00318 00319 { 00320 out << "\nTest push_back(x) ...\n"; 00321 Array<T> a2; 00322 for ( int i = 0; i < n; ++i ) { 00323 a2.push_back(as<T>(i)); 00324 TEST_EQUALITY_CONST(a2.front(),as<T>(0)); 00325 TEST_EQUALITY_CONST(getConst(a2).front(),as<T>(0)); 00326 TEST_EQUALITY(a2.back(),as<T>(i)); 00327 TEST_EQUALITY(getConst(a2).back(),as<T>(i)); 00328 } 00329 TEST_COMPARE_ARRAYS( a2, a ); 00330 } 00331 00332 { 00333 out << "\nTest pop_back() ...\n"; 00334 Array<T> a2(a); 00335 for ( int i = n-1; i >= 0; --i ) { 00336 TEST_EQUALITY(a2.back(),as<T>(i)); 00337 a2.pop_back(); 00338 } 00339 } 00340 00341 { 00342 out << "\nTest insert(iter,x) ...\n"; 00343 Array<T> a2; 00344 for ( int i = 0; i < n; ++i ) { 00345 const typename Array<T>::iterator 00346 iter = a2.insert(a2.end(), as<T>(i)); 00347 TEST_EQUALITY(*iter, as<T>(i)); 00348 } 00349 TEST_COMPARE_ARRAYS( a2, a ); 00350 } 00351 00352 { 00353 out << "\nTest insert(iter,1,x) ...\n"; 00354 Array<T> a2; 00355 for ( int i = 0; i < n; ++i ) 00356 a2.insert(a2.end(),1,i); 00357 TEST_COMPARE_ARRAYS( a2, a ); 00358 } 00359 00360 { 00361 out << "\nTest insert(iter,first,last) ...\n"; 00362 Array<T> a2; 00363 for ( int i = 0; i < n; ++i ) 00364 a2.insert(a2.end(),a.begin()+i,a.begin()+i+1); 00365 TEST_COMPARE_ARRAYS( a2, a ); 00366 } 00367 00368 { 00369 out << "\nTest append(x) ...\n"; 00370 Array<T> a2; 00371 for ( int i = 0; i < n; ++i ) 00372 a2.append(as<T>(i)); 00373 TEST_COMPARE_ARRAYS( a2, a ); 00374 } 00375 00376 { 00377 out << "\nTest erase(iter) ...\n"; 00378 Array<T> a2(a); 00379 for ( int i = 0; i < n; ++i ) { 00380 TEST_EQUALITY( as<int>(a2.size()), n-i ); 00381 TEST_EQUALITY( a2.front(), as<T>(i) ); 00382 a2.erase(a2.begin()); 00383 } 00384 TEST_EQUALITY_CONST( a2.empty(), true ); 00385 } 00386 00387 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00388 00389 { 00390 out << "\nTest trying to erase twice with the same iterator which should throw ...\n"; 00391 Array<T> a2(a); 00392 const typename Array<T>::iterator iter = a2.begin(); 00393 a2.erase(iter); // After this point, the iterator is no longer valid! 00394 // This is no longer a valid iterator and should throw! 00395 TEST_THROW( a2.erase(iter), Teuchos::IncompatibleIteratorsError ); 00396 } 00397 00398 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00399 00400 // 2007/11/08: rabartl: ToDo: Above, I have tested one use case where the 00401 // iterator should be invalidated and this tests that it throws an exception 00402 // as it should. However, currently, I don't have code written that will 00403 // catch the problem where the client would try to dereference the iterator 00404 // or something like that. This is a big no-no. I could do this by adding 00405 // an is_valid() property to RCP_node and then setting this to null when the 00406 // structure of the iterator changes. Then, I would have to put asserts in 00407 // ArrayRCP to constantly check is_valid() (with an assert_is_valid() 00408 // function or something) on any call other than operator=(...) which would 00409 // reset this iterator. Catching all of these user errors is a lot of work! 00410 00411 { 00412 out << "\nTest remove(i) ...\n"; 00413 Array<T> a2(a); 00414 for ( int i = 0; i < n; ++i ) { 00415 TEST_EQUALITY( as<int>(a2.size()), n-i ); 00416 TEST_EQUALITY( a2.front(), as<T>(i) ); 00417 a2.remove(0); // Always remove the "first" entry! 00418 } 00419 TEST_EQUALITY_CONST( a2.empty(), true ); 00420 } 00421 00422 { 00423 out << "\nTest erase(begin(),end()) ...\n"; 00424 Array<T> a2(a); 00425 a2.erase(a2.begin(),a2.end()); 00426 TEST_EQUALITY_CONST( a2.empty(), true ); 00427 } 00428 00429 { 00430 out << "\nTest member swap() ...\n"; 00431 Array<T> a2(a); 00432 Array<T> a3(a); 00433 for ( int i = 0; i < n; ++i ) 00434 a2[i] += as<T>(1); 00435 a2.swap(a3); 00436 TEST_COMPARE_ARRAYS( a2, a ); 00437 } 00438 00439 { 00440 out << "\nTest non-member swap() ...\n"; 00441 Array<T> a2(a); 00442 Array<T> a3(a); 00443 for ( int i = 0; i < n; ++i ) 00444 a2[i] += as<T>(1); 00445 swap(a2,a3); 00446 TEST_COMPARE_ARRAYS( a2, a ); 00447 } 00448 00449 { 00450 out << "\nTest clear() ...\n"; 00451 Array<T> a2(a); 00452 a2.clear(); 00453 TEST_EQUALITY_CONST( a2.empty(), true ); 00454 TEST_EQUALITY_CONST( a2.size(), 0 ); 00455 } 00456 00457 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00458 { 00459 out << "\nTest to string ...\n"; 00460 std::ostringstream o; 00461 o << "{"; 00462 for ( int i = 0; i < n; ++i ) { 00463 o << as<T>(i) << ( i < n-1 ? ", " : "" ); 00464 } 00465 o << "}"; 00466 TEST_EQUALITY( o.str(), a.toString() ); 00467 } 00468 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00469 00470 { 00471 out << "\nTest hasArrayBoundsChecking() ... \n"; 00472 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00473 TEST_EQUALITY_CONST( a.hasBoundsChecking(), true ); 00474 #else 00475 TEST_EQUALITY_CONST( a.hasBoundsChecking(), false ); 00476 #endif 00477 } 00478 00479 // 00480 out << "\nG) Test views ...\n"; 00481 // 00482 00483 { 00484 out << "\nTest full non-const subview ...\n"; 00485 const ArrayView<T> av2 = a(0,n); 00486 TEST_COMPARE_ARRAYS( av2, a ); 00487 } 00488 00489 { 00490 out << "\nTest full shorthand non-const subview ...\n"; 00491 const ArrayView<T> av2 = a(); 00492 TEST_COMPARE_ARRAYS( av2, a ); 00493 } 00494 00495 { 00496 out << "\nTest full const subview ...\n"; 00497 const ArrayView<const T> cav2 = getConst(a)(0, n); 00498 TEST_COMPARE_ARRAYS( cav2, a ); 00499 } 00500 00501 { 00502 out << "\nTest full non-const to const subview ...\n"; 00503 const ArrayView<const T> cav2 = a(0, n); 00504 TEST_COMPARE_ARRAYS( cav2, a ); 00505 } 00506 00507 { 00508 out << "\nTest full short-hand const subview ...\n"; 00509 const ArrayView<const T> cav2 = getConst(a)(); 00510 TEST_COMPARE_ARRAYS( cav2, a ); 00511 } 00512 00513 { 00514 out << "\nTest non-const initial range view ...\n"; 00515 Array<T> a2(n,as<T>(-1)); 00516 const ArrayView<T> av2 = a2; // Tests implicit conversion! 00517 const ArrayView<T> av2_end = av2(0,n-1); 00518 TEST_EQUALITY( av2_end.size(), n-1 ); 00519 av2_end.assign( a(0,n-1) ); 00520 av2.back() = as<T>(n-1); 00521 TEST_COMPARE_ARRAYS( a2, a ); 00522 } 00523 00524 { 00525 out << "\nTest non-const middle range view ...\n"; 00526 Array<T> a2(n,as<T>(-1)); 00527 const ArrayView<T> av2 = a2; // Tests implicit conversion! 00528 const ArrayView<T> av2_middle = av2(1,n-2); 00529 TEST_EQUALITY( av2_middle.size(), n-2 ); 00530 av2_middle.assign( a(1,n-2) ); 00531 av2.front() = as<T>(0); 00532 av2.back() = as<T>(n-1); 00533 TEST_COMPARE_ARRAYS( a2, a ); 00534 } 00535 00536 { 00537 out << "\nTest const view ... "; 00538 const ArrayView<const T> av2 = a; // Tests implicit conversion to const! 00539 const ArrayView<const T> av2_middle = av2(1,n-2); 00540 TEST_EQUALITY( av2_middle.size(), n-2 ); 00541 bool local_success = true; 00542 for ( int i = 0; i < n-2; ++i ) 00543 TEST_ARRAY_ELE_EQUALITY( av2_middle, i, as<T>(i+1) ); 00544 if (local_success) out << "passed\n"; 00545 else success = false; 00546 } 00547 00548 { 00549 out << "\nTest constructing Array<T> from ArrayView<T> ...\n"; 00550 const ArrayView<T> av2 = a; 00551 Array<T> a2(av2); 00552 TEST_COMPARE_ARRAYS( a2, a ); 00553 } 00554 00555 { 00556 out << "\nTest constructing Array<T> from ArrayView<const T> ...\n"; 00557 const ArrayView<const T> av2 = a; 00558 Array<T> a2(av2); 00559 TEST_COMPARE_ARRAYS( a2, a ); 00560 } 00561 00562 { 00563 out << "\nTest comparison operators ...\n"; 00564 Array<T> a2(a); 00565 TEST_EQUALITY_CONST( (a2==a), true ); 00566 TEST_EQUALITY_CONST( (a2!=a), false ); 00567 TEST_EQUALITY_CONST( (a2<=a), true ); 00568 TEST_EQUALITY_CONST( (a2>=a), true ); 00569 TEST_EQUALITY_CONST( (a2<a), false ); 00570 TEST_EQUALITY_CONST( (a2>a), false ); 00571 } 00572 00573 // 00574 out << "\nH) Test tuple(...) construction ...\n"; 00575 // 00576 00577 { 00578 const size_type m = 1; 00579 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00580 Array<T> am = tuple<T>(0); 00581 TEST_EQUALITY_CONST(am.size(), m); 00582 out << "Test that am[i] == i ... "; 00583 bool local_success = true; 00584 for( size_type i = 0; i < m; ++i ) { 00585 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00586 } 00587 if (local_success) out << "passed\n"; 00588 else success = false; 00589 } 00590 00591 { 00592 const size_type m = 2; 00593 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00594 Array<T> am = tuple<T>(0,1); 00595 TEST_EQUALITY_CONST(am.size(),m); 00596 out << "Test that am[i] == i ... "; 00597 bool local_success = true; 00598 for( size_type i = 0; i < m; ++i ) { 00599 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00600 } 00601 if (local_success) out << "passed\n"; 00602 else success = false; 00603 } 00604 00605 { 00606 const size_type m = 3; 00607 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00608 Array<T> am = tuple<T>(0,1,2); 00609 TEST_EQUALITY_CONST(am.size(),m); 00610 out << "Test that am[i] == i ... "; 00611 bool local_success = true; 00612 for( size_type i = 0; i < m; ++i ) { 00613 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00614 } 00615 if (local_success) out << "passed\n"; 00616 else success = false; 00617 } 00618 00619 { 00620 const size_type m = 4; 00621 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00622 Array<T> am = tuple<T>(0,1,2,3); 00623 TEST_EQUALITY_CONST(am.size(),m); 00624 out << "Test that am[i] == i ... "; 00625 bool local_success = true; 00626 for( size_type i = 0; i < m; ++i ) { 00627 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00628 } 00629 if (local_success) out << "passed\n"; 00630 else success = false; 00631 } 00632 00633 { 00634 const size_type m = 5; 00635 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00636 Array<T> am = tuple<T>(0,1,2,3,4); 00637 TEST_EQUALITY_CONST(am.size(),m); 00638 out << "Test that am[i] == i ... "; 00639 bool local_success = true; 00640 for( size_type i = 0; i < m; ++i ) { 00641 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00642 } 00643 if (local_success) out << "passed\n"; 00644 else success = false; 00645 } 00646 00647 { 00648 const size_type m = 6; 00649 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00650 Array<T> am = tuple<T>(0,1,2,3,4,5); 00651 TEST_EQUALITY_CONST(am.size(),m); 00652 out << "Test that am[i] == i ... "; 00653 bool local_success = true; 00654 for( size_type i = 0; i < m; ++i ) { 00655 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00656 } 00657 if (local_success) out << "passed\n"; 00658 else success = false; 00659 } 00660 00661 { 00662 const size_type m = 7; 00663 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00664 Array<T> am = tuple<T>(0,1,2,3,4,5,6); 00665 TEST_EQUALITY_CONST(am.size(),m); 00666 out << "Test that am[i] == i ... "; 00667 bool local_success = true; 00668 for( size_type i = 0; i < m; ++i ) { 00669 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00670 } 00671 if (local_success) out << "passed\n"; 00672 else success = false; 00673 } 00674 00675 { 00676 const size_type m = 8; 00677 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00678 Array<T> am = tuple<T>(0,1,2,3,4,5,6,7); 00679 TEST_EQUALITY_CONST(am.size(),m); 00680 out << "Test that am[i] == i ... "; 00681 bool local_success = true; 00682 for( size_type i = 0; i < m; ++i ) { 00683 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00684 } 00685 if (local_success) out << "passed\n"; 00686 else success = false; 00687 } 00688 00689 { 00690 const size_type m = 9; 00691 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00692 Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8); 00693 TEST_EQUALITY_CONST(am.size(),m); 00694 out << "Test that am[i] == i ... "; 00695 bool local_success = true; 00696 for( size_type i = 0; i < m; ++i ) { 00697 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00698 } 00699 if (local_success) out << "passed\n"; 00700 else success = false; 00701 } 00702 00703 { 00704 const size_type m = 10; 00705 out << "\nTest Array<T> = tuple(0,...,"<<m-1<<")\n"; 00706 Array<T> am = tuple<T>(0,1,2,3,4,5,6,7,8,9); 00707 TEST_EQUALITY_CONST(am.size(),m); 00708 out << "Test that am[i] == i ... "; 00709 bool local_success = true; 00710 for( size_type i = 0; i < m; ++i ) { 00711 TEST_ARRAY_ELE_EQUALITY( am, i, as<T>(i) ); 00712 } 00713 if (local_success) out << "passed\n"; 00714 else success = false; 00715 } 00716 00717 { 00718 out << "\nTest taking an empty view ...\n"; 00719 const ArrayView<T> av = a(0,0); 00720 TEST_EQUALITY_CONST( av.size(), 0 ); 00721 } 00722 00723 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00724 { 00725 out << "\nTest taking views outside of valid range ...\n"; 00726 TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError ); 00727 TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError ); 00728 TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError ); 00729 } 00730 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00731 00732 return success; 00733 00734 } 00735 00736 00737 template<class T> 00738 bool testArrayOpaqueWithoutTNT( const std::string &T_name, const int n, 00739 const T &someValue, Teuchos::FancyOStream &out ) 00740 { 00741 00742 using Teuchos::Array; 00743 using Teuchos::ArrayView; 00744 using Teuchos::TypeNameTraits; 00745 using Teuchos::as; 00746 typedef typename Array<T>::size_type size_type; 00747 00748 bool success = true; 00749 00750 out 00751 << "\n***" 00752 << "\n*** Testing Array<"<<T_name<<"> for opaque type without TNT of size = "<<n 00753 << "\n***\n"; 00754 00755 Teuchos::OSTab tab(out); 00756 00757 // 00758 out << "\nA) Initial setup ...\n\n"; 00759 // 00760 00761 // Tests construction using size 00762 00763 Array<T> a(n); 00764 00765 TEST_EQUALITY_CONST( a.empty(), false ); 00766 TEST_EQUALITY( a.length(), n ); 00767 TEST_EQUALITY( as<int>(a.size()), n ); 00768 TEST_EQUALITY( a.getRawPtr(), &a[0] ); 00769 TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] ); 00770 TEST_COMPARE( a.max_size(), >=, as<size_type>(n) ); 00771 TEST_COMPARE( as<int>(a.capacity()), >=, n ); 00772 00773 { 00774 out << "\nInitializing data ...\n"; 00775 for( int i = 0; i < n; ++i ) 00776 a[i] = someValue; // tests non-const operator[](i) 00777 } 00778 00779 { 00780 out << "\nTest that a[i] == "<<someValue<<" ... "; 00781 bool local_success = true; 00782 for( int i = 0; i < n; ++i ) { 00783 TEST_ARRAY_ELE_EQUALITY( a, i, someValue ); 00784 } 00785 if (local_success) out << "passed\n"; 00786 else success = false; 00787 } 00788 00789 #ifndef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00790 { 00791 out << "\nTest taking a view of the array ...\n"; 00792 const ArrayView<T> av = a(); 00793 TEST_COMPARE_ARRAYS( av, a ); 00794 } 00795 // 2008/08/01: rabartl: Above: We can not create an array view of an 00796 // undefined type in debug mode without a specialization of TypeNameTraits. 00797 #endif // not HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00798 00799 // ToDo: Do we need to be testing other things for opaque objects? 00800 00801 return success; 00802 00803 } 00804 00805 00806 template<class T> 00807 bool testArrayOpaqueWithTNT( const int n, const T &someValue, Teuchos::FancyOStream &out ) 00808 { 00809 00810 using Teuchos::Array; 00811 using Teuchos::ArrayView; 00812 using Teuchos::TypeNameTraits; 00813 using Teuchos::as; 00814 typedef typename Array<T>::size_type size_type; 00815 00816 bool success = true; 00817 00818 out 00819 << "\n***" 00820 << "\n*** Testing "<<TypeNameTraits<Array<T> >::name()<<" for opaque type with TNT of size = "<<n 00821 << "\n***\n"; 00822 00823 Teuchos::OSTab tab(out); 00824 00825 // 00826 out << "\nA) Initial setup ...\n\n"; 00827 // 00828 00829 // Tests construction using size 00830 00831 Array<T> a(n); 00832 00833 TEST_EQUALITY_CONST( a.empty(), false ); 00834 TEST_EQUALITY( a.length(), n ); 00835 TEST_EQUALITY( as<int>(a.size()), n ); 00836 TEST_EQUALITY( a.getRawPtr(), &a[0] ); 00837 TEST_EQUALITY( getConst(a).getRawPtr(), &getConst(a)[0] ); 00838 TEST_COMPARE( a.max_size(), >=, as<size_type>(n) ); 00839 TEST_COMPARE( as<int>(a.capacity()), >=, n ); 00840 00841 { 00842 out << "\nInitializing data ...\n"; 00843 for( int i = 0; i < n; ++i ) 00844 a[i] = someValue; // tests non-const operator[](i) 00845 } 00846 00847 { 00848 out << "\nTest that a[i] == "<<someValue<<" ... "; 00849 bool local_success = true; 00850 for( int i = 0; i < n; ++i ) { 00851 TEST_ARRAY_ELE_EQUALITY( a, i, someValue ); 00852 } 00853 if (local_success) out << "passed\n"; 00854 else success = false; 00855 } 00856 00857 { 00858 out << "\nTest taking a view of the array ...\n"; 00859 const ArrayView<T> av = a(); 00860 TEST_COMPARE_ARRAYS( av, a ); 00861 } 00862 00863 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00864 { 00865 out << "\nTest taking views outside of valid range ...\n"; 00866 TEST_THROW( const ArrayView<T> av = a(-1,n), Teuchos::RangeError ); 00867 TEST_THROW( const ArrayView<T> av = a(0,n+1), Teuchos::RangeError ); 00868 TEST_THROW( const ArrayView<T> av = a(0,-1), Teuchos::RangeError ); 00869 } 00870 #endif // HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00871 00872 // 2008/08/01: rabartl: Above: We can create ArrayViews and any other thing 00873 // that we would like since we have defined a TypeNameTraits class for the 00874 // undefined type. 00875 00876 // ToDo: Do we need to be testing other things for opaque objects? 00877 00878 return success; 00879 00880 } 00881 00882 00883 // 00884 // Main testing program 00885 // 00886 00887 int main( int argc, char* argv[] ) { 00888 00889 using Teuchos::CommandLineProcessor; 00890 using Teuchos::Array; 00891 00892 bool success = true; 00893 bool result; 00894 00895 Teuchos::GlobalMPISession mpiSession(&argc, &argv); 00896 //const int procRank = Teuchos::GlobalMPISession::getRank(); 00897 00898 Teuchos::RCP<Teuchos::FancyOStream> 00899 out = Teuchos::VerboseObjectBase::getDefaultOStream(); 00900 00901 try { 00902 00903 // 00904 // Read options from the commandline 00905 // 00906 00907 CommandLineProcessor clp(false); // Don't throw exceptions 00908 00909 int n = 4; 00910 clp.setOption( "n", &n, "Number of elements in the array" ); 00911 00912 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); 00913 00914 if ( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) { 00915 *out << "\nEnd Result: TEST FAILED" << std::endl; 00916 return parse_return; 00917 } 00918 00919 *out << std::endl << Teuchos::Teuchos_Version() << std::endl; 00920 00921 result = testArray<int>(n,*out); 00922 if (!result) success = false; 00923 00924 result = testArray<float>(n,*out); 00925 if (!result) success = false; 00926 00927 result = testArray<double>(n,*out); 00928 if (!result) success = false; 00929 00930 //result = testArray<std::complex<double> >(n,*out); 00931 //if (!result) success = false; 00932 // 2007/12/03: rabartl: Commented this out so I can test comparison operators 00933 00934 result = testArrayOpaqueWithoutTNT<Opaque_handle>("Opaque_handle", n, 00935 OPAQUE_HANDLE_NULL, *out); 00936 if (!result) success = false; 00937 00938 result = testArrayOpaqueWithTNT<Opaque2_handle>(n, OPAQUE2_HANDLE_NULL, *out); 00939 if (!result) success = false; 00940 00941 result = testArrayOpaqueWithTNT<Opaque3_handle>(n, OPAQUE3_HANDLE_NULL, *out); 00942 if (!result) success = false; 00943 00944 // ToDo: Fill in the rest of the types! 00945 00946 } 00947 TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success); 00948 00949 if (success) 00950 *out << "\nEnd Result: TEST PASSED" << std::endl; 00951 else 00952 *out << "\nEnd Result: TEST FAILED" << std::endl; 00953 00954 return ( success ? 0 : 1 ); 00955 00956 }
1.7.4