Teuchos Package Browser (Single Doxygen Collection) Version of the Day
vector_UnitTests.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA
00025 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00026 //
00027 // ***********************************************************************
00028 // @HEADER
00029 */
00030 
00031 #include "Teuchos_UnitTestHarness.hpp"
00032 #include "Teuchos_Array.hpp"
00033 #include "Teuchos_getConst.hpp"
00034 #include "Teuchos_as.hpp"
00035 
00036 
00037 namespace {
00038 
00039 
00040 int n = 4;
00041 
00042 
00043 TEUCHOS_STATIC_SETUP()
00044 {
00045   Teuchos::UnitTestRepository::getCLP().setOption(
00046     "n", &n, "Number of elements in the vectors" );
00047 }
00048 
00049 
00050 template<class T>
00051 std::vector<T> generatevector(const int n_in)
00052 {
00053   using Teuchos::as;
00054   std::vector<T> a(n_in);
00055   for( int i = 0; i < n_in; ++i )
00056     a[i] = as<T>(i); // tests non-const operator[](i)
00057   return a;
00058 }
00059 
00060 
00061 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, defaultConstruct, T )
00062 {
00063   using std::vector;
00064   using Teuchos::as;
00065   vector<T> a2;
00066   TEST_EQUALITY_CONST( as<int>(a2.size()), 0 );
00067   TEST_EQUALITY_CONST( a2.empty(), true );
00068 }
00069 
00070 
00071 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, sizedConstruct, T )
00072 {
00073   using std::vector;
00074   using Teuchos::as;
00075   using Teuchos::getConst;
00076   typedef typename std::vector<T>::size_type size_type;
00077   vector<T> a(n);
00078   TEST_EQUALITY_CONST( a.empty(), false );
00079   TEST_EQUALITY( as<int>(a.size()), n );
00080   TEST_COMPARE( a.max_size(), >=, as<size_type>(n) );
00081   TEST_COMPARE( as<int>(a.capacity()), >=, n );
00082 }
00083 
00084 
00085 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, operatorBracket, T )
00086 {
00087   using std::vector;
00088   using Teuchos::as;
00089   out << "\nTest that a[i] == i ... ";
00090   vector<T> a = generatevector<T>(n);;
00091   bool local_success = true;
00092   for( int i = 0; i < n; ++i ) {
00093     TEST_ARRAY_ELE_EQUALITY( a, i, as<T>(i) );
00094   }
00095   if (local_success) out << "passed\n";
00096   else success = false;
00097 }
00098 
00099 
00100 TEUCHOS_UNIT_TEST_TEMPLATE_1_DECL( vector, constAt, T )
00101 {
00102   using std::vector;
00103   using Teuchos::as;
00104   out << "\nTest that a.at(i) == i ...\n";
00105   vector<T> a = generatevector<T>(n);;
00106   bool local_success = true;
00107   for( int i = 0; i < n; ++i ) {
00108     TEUCHOS_TEST_EQUALITY( a.at(i), as<T>(i), out, local_success );
00109   }
00110   if (local_success) out << "passed\n";
00111   else success = false;
00112 }
00113 
00114 
00115 //
00116 // Instantiations
00117 //
00118 
00119 
00120 #define UNIT_TEST_GROUP( T ) \
00121   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, defaultConstruct, T ) \
00122   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, sizedConstruct, T ) \
00123   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, operatorBracket, T ) \
00124   TEUCHOS_UNIT_TEST_TEMPLATE_1_INSTANT( vector, constAt, T )
00125 
00126 
00127 UNIT_TEST_GROUP(int)
00128 UNIT_TEST_GROUP(float)
00129 UNIT_TEST_GROUP(double)
00130 
00131 
00132 } // namespace
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines