Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Polynomial_UnitTest.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
11 
12 #include "Teuchos_Polynomial.hpp"
13 #include "Teuchos_Array.hpp"
14 
16 using Teuchos::as;
17 using Teuchos::Array;
18 using Teuchos::RCP;
19 using Teuchos::rcp;
20 
21 
22 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, create ) {
23  Polynomial<double> P(0,1.0);
24  TEST_EQUALITY_CONST( P.degree(), 0 );
25 }
26 
27 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, degrees ) {
28  for (unsigned int degree=0 ; degree<10 ; ++degree) {
29  Polynomial<double> P(degree,1.0);
30  TEST_EQUALITY_CONST( P.degree(), degree );
31  }
32 }
33 
34 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffs ) {
35  unsigned int degree = 10;
36  Polynomial<double> P(degree,20.0);
37  for (unsigned int d=0 ; d <= degree ; ++d) {
38  P.setCoefficient(d,d*1.0);
39  }
40  for (unsigned int d=0 ; d <= degree ; ++d) {
41  TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
42  }
43 }
44 
45 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, coeffsPtr ) {
46  unsigned int degree = 10;
47  Polynomial<double> P(degree);
48  for (unsigned int d=0 ; d <= degree ; ++d) {
49  RCP<double> coeffPtr = rcp(new double(d*1.0));
50  P.setCoefficientPtr(d,coeffPtr);
51  }
52  for (unsigned int d=0 ; d <= degree ; ++d) {
53  TEST_EQUALITY_CONST( *(P.getCoefficient(d)), d*1.0 );
54  }
55 }
56 
57 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, RCPcoeffs ) {
58  int degree = 10;
59  Polynomial<double> P(degree,20.0);
60  for (int d=0 ; d <= degree ; ++d) {
61  P.setCoefficient(d,d*1.0);
62  }
63  RCP<const double> constCoeff = rcp(new double);
64  constCoeff = P.getCoefficient(8);
65  TEST_EQUALITY_CONST( *constCoeff, 8.0 );
66 
67  RCP<double> coeff = rcp(new double);
68  coeff = P.getCoefficient(4);
69  TEST_EQUALITY_CONST( *coeff, 4.0 );
70 
71 }
72 
73 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, evaluate ) {
74  int degree = 2;
75  Polynomial<double> P(degree,0.0);
76  P.setCoefficient(0,-1.0);
77  P.setCoefficient(1, 0.0);
78  P.setCoefficient(2, 1.0);
79  int numTests = 11;
80  Array<double> testValues(numTests);
81  for (int i=0 ; i<numTests ; ++i) {
82  testValues[i] = (i-5);
83  }
84  Array<double> polyValues(numTests);
85  Array<double> polyDotValues(numTests);
86  for (int i=0 ; i<numTests ; ++i) {
87  polyValues[i] = pow(testValues[i],2.0)-1.0;
88  polyDotValues[i] = 2*testValues[i];
89  }
90  for (int i=0 ; i<numTests ; ++i ) {
91  double x_out;
92  double x_dot_out;
93  P.evaluate(testValues[i], &x_out, &x_dot_out );
94  TEST_EQUALITY( x_out, polyValues[i] );
95  TEST_EQUALITY( x_dot_out, polyDotValues[i] );
96  }
97 }
98 
99 #ifdef TEUCHOS_DEBUG
100 TEUCHOS_UNIT_TEST( Teuchos_Polynomial, errors ) {
101  {
102  unsigned int degree = 2;
103  const Polynomial<double> constP(degree,20.0);
104  RCP<const double> constCoeff = rcp(new double);
105  TEST_THROW( constCoeff = constP.getCoefficient(3), std::out_of_range );
106  }
107  {
108  unsigned int degree = 2;
109  Polynomial<double> P(degree,20.0);
110  RCP<double> coeff = rcp(new double);
111  TEST_THROW( coeff = P.getCoefficient(3), std::out_of_range );
112  }
113  {
114  unsigned int degree = 2;
115  Polynomial<double> P(degree,20.0);
116  unsigned int i = 3;
117  const double coeff = 5.0;
118  TEST_THROW( P.setCoefficient(i,coeff), std::out_of_range );
119  }
120  {
121  unsigned int degree = 2;
122  Polynomial<double> P(degree);
123  unsigned int i = 0;
124  const double coeff = 5.0;
125  TEST_THROW( P.setCoefficient(i,coeff), std::runtime_error );
126  }
127  {
128  unsigned int degree = 2;
129  Polynomial<double> P(degree);
130  unsigned int i = 3;
131  RCP<double> coeff = rcp(new double(5.0));
132  TEST_THROW( P.setCoefficientPtr(i,coeff), std::out_of_range );
133  }
134  {
135  unsigned int degree = 2;
136  Polynomial<double> P(degree);
137  double t = 2.5;
138  double x;
139  double x_dot;
140  TEST_THROW( P.evaluate(t,&x,&x_dot), std::runtime_error );
141  }
142 }
143 #endif // TEUCHOS_DEBUG
144 
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
#define TEST_EQUALITY(v1, v2)
Assert the equality of v1 and v2.
#define TEST_THROW(code, ExceptType)
Assert that the statement &#39;code&#39; throws the exception &#39;ExceptType&#39; (otherwise the test fails)...
#define TEUCHOS_UNIT_TEST(TEST_GROUP, TEST_NAME)
Macro for defining a (non-templated) unit test.
void setCoefficient(unsigned int i, const CoeffT &v)
Set coefficient i to c.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
void setCoefficientPtr(unsigned int i, const Teuchos::RCP< CoeffT > &c_ptr)
Set pointer for coefficient i to c_ptr. DANGEROUS!
Unit testing support.
#define TEST_EQUALITY_CONST(v1, v2)
Assert the equality of v1 and constant v2.
Lightweight container class to represent a simple polynomial.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Templated array class derived from the STL std::vector.
Teuchos::RCP< CoeffT > getCoefficient(unsigned int i)
Return ref-count pointer to coefficient i.
Smart reference counting pointer class for automatic garbage collection.
void evaluate(typename Teuchos::Polynomial< CoeffT >::scalar_type &t, CoeffT *x, CoeffT *xdot=NULL) const
Evaluate polynomial and possibly its derivative at time t.
unsigned int degree() const
Return degree of polynomial.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...