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