Polynomial_UnitTest.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_UnitTestHarness.hpp"
00030 
00031 #include "Teuchos_Polynomial.hpp"
00032 #include "Teuchos_Array.hpp"
00033 
00034 using Teuchos::Polynomial;
00035 using Teuchos::as;
00036 using Teuchos::Array;
00037 using Teuchos::RCP;
00038 using Teuchos::rcp;
00039 
00040 
00041 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, create ) {
00042   Polynomial<double> P(0,1.0);
00043   TEST_EQUALITY_CONST( P.degree(), 0 );
00044 }
00045 
00046 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, degrees ) {
00047   for (unsigned int degree=0 ; degree<10 ; ++degree) {
00048     Polynomial<double> P(degree,1.0);
00049     TEST_EQUALITY_CONST( P.degree(), degree );
00050   }
00051 }
00052 
00053 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffs ) {
00054   unsigned int degree = 10;
00055   Polynomial<double> P(degree,20.0);
00056   for (unsigned int d=0 ; d <= degree ; ++d) {
00057     P.setCoefficient(d,d*1.0);
00058   }
00059   for (unsigned int d=0 ; d <= degree ; ++d) {
00060     TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
00061   }
00062 }
00063 
00064 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffsPtr ) {
00065   unsigned int degree = 10;
00066   Polynomial<double> P(degree);  
00067   for (unsigned int d=0 ; d <= degree ; ++d) {
00068     RCP<double> coeffPtr = rcp(new double(d*1.0));
00069     P.setCoefficientPtr(d,coeffPtr);
00070   }
00071   for (unsigned int d=0 ; d <= degree ; ++d) {
00072     TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
00073   }
00074 }
00075 
00076 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, RCPcoeffs ) {
00077   int degree = 10;
00078   Polynomial<double> P(degree,20.0);
00079   for (int d=0 ; d <= degree ; ++d) {
00080     P.setCoefficient(d,d*1.0);
00081   }
00082   RCP<const double> constCoeff = rcp(new double);
00083   constCoeff = P.getCoefficient(8);
00084   TEST_EQUALITY_CONST( *constCoeff, 8.0 );
00085 
00086   RCP<double> coeff = rcp(new double);
00087   coeff = P.getCoefficient(4);
00088   TEST_EQUALITY_CONST( *coeff, 4.0 );
00089   
00090 }
00091 
00092 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, evaluate ) {
00093   int degree = 2;
00094   Polynomial<double> P(degree,0.0);
00095   P.setCoefficient(0,-1.0);
00096   P.setCoefficient(1, 0.0);
00097   P.setCoefficient(2, 1.0);
00098   int numTests = 11;
00099   Array<double> testValues(numTests);
00100   for (int i=0 ; i<numTests ; ++i) {
00101     testValues[i] = (i-5);
00102   }
00103   Array<double> polyValues(numTests);
00104   Array<double> polyDotValues(numTests);
00105   for (int i=0 ; i<numTests ; ++i) {
00106     polyValues[i] = pow(testValues[i],2.0)-1.0;
00107     polyDotValues[i] = 2*testValues[i];
00108   }
00109   for (int i=0 ; i<numTests  ; ++i ) {
00110     double x_out;
00111     double x_dot_out;
00112     P.evaluate(testValues[i], &x_out, &x_dot_out );
00113     TEST_EQUALITY( x_out, polyValues[i] );
00114     TEST_EQUALITY( x_dot_out, polyDotValues[i] );
00115   }
00116 }
00117 
00118 #ifdef TEUCHOS_DEBUG
00119 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, errors ) {
00120   {
00121     unsigned int degree = 2;
00122     const Polynomial<double> constP(degree,20.0);
00123     RCP<const double> constCoeff = rcp(new double);
00124     TEST_THROW( constCoeff = constP.getCoefficient(3), std::out_of_range );
00125   }
00126   {
00127     unsigned int degree = 2;
00128     Polynomial<double> P(degree,20.0);
00129     RCP<double> coeff = rcp(new double);
00130     TEST_THROW( coeff = P.getCoefficient(3), std::out_of_range );
00131   }
00132   {
00133     unsigned int degree = 2;
00134     Polynomial<double> P(degree,20.0);
00135     unsigned int i = 3;
00136     const double coeff = 5.0;
00137     TEST_THROW( P.setCoefficient(i,coeff), std::out_of_range );
00138   }
00139   {
00140     unsigned int degree = 2;
00141     Polynomial<double> P(degree);
00142     unsigned int i = 0;
00143     const double coeff = 5.0;
00144     TEST_THROW( P.setCoefficient(i,coeff), std::runtime_error );
00145   }
00146   {
00147     unsigned int degree = 2;
00148     Polynomial<double> P(degree);
00149     unsigned int i = 3;
00150     RCP<double> coeff = rcp(new double(5.0));
00151     TEST_THROW( P.setCoefficientPtr(i,coeff), std::out_of_range );
00152   }
00153   {
00154     unsigned int degree = 2;
00155     Polynomial<double> P(degree);
00156     double t = 2.5;
00157     double x;
00158     double x_dot;
00159     TEST_THROW( P.evaluate(t,&x,&x_dot), std::runtime_error );
00160   }
00161 }
00162 #endif // TEUCHOS_DEBUG
00163 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:28 2011 for Teuchos Package Browser (Single Doxygen Collection) by  doxygen 1.6.3