Code-Eli  0.3.6
trapezoid_test_suite.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2 * Copyright (c) 2013 David D. Marshall <ddmarsha@calpoly.edu>
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * David D. Marshall - initial code and implementation
11 ********************************************************************************/
12 
13 #ifndef trapezoid_test_suite_hpp
14 #define trapezoid_test_suite_hpp
15 
16 #include <cmath> // std::exp
17 #include <typeinfo> // typeid
18 #include <string> // std::string
19 #include <sstream> // std::stringstream
20 #include <iomanip> // std::setw
21 #include <vector> // std::vector
22 
24 #include "eli/constants/math.hpp"
25 
26 template<typename data__>
27 class trapezoid_test_suite : public Test::Suite
28 {
29  protected:
30  void AddTests(const float &)
31  {
34  }
35 
36  void AddTests(const double &)
37  {
40  }
41 
42  void AddTests(const long double &)
43  {
46  }
47 
48  public:
50  {
51  // add the tests
52  AddTests(data__());
53  }
55  {
56  }
57 
58  private:
60  {
61  data__ x1(1.0), x2(3.0), F_exact(std::exp(x2)-std::exp(x1)), F_quad;
62  size_t n, npts(1001);
63  std::vector<data__> x(npts), f(npts);
64 
65  // set the x points
66  for (n=0; n<npts; ++n)
67  x[n]=(n)*(x2-x1)/(npts-1)+x1;
68 
69  // set the function points
70  for (n=0; n<npts; ++n)
71  f[n]=std::exp(x[n]);
72 
73  // find the integral approximation
75 
76  F_quad=quad(x[1]-x[0], f.begin(), f.end());
77 
78  if (typeid(data__)==typeid(float))
79  {
80  TEST_ASSERT_DELTA(1, F_quad/F_exact, 2e-5);
81  }
82  else
83  {
84  TEST_ASSERT_DELTA(1, F_quad/F_exact, 4e-7);
85  }
86  }
87 
89  {
90  data__ x1(1.0), x2(3.0), F_exact(std::exp(x2)-std::exp(x1)), F_quad;
91  size_t n, npts(1001);
92  std::vector<data__> x(npts), f(npts);
93 
94  // set the x points
95  for (n=0; n<npts; ++n)
96  x[n]=sin(eli::constants::math<data__>::pi_by_two()*n/(npts-1))*(x2-x1)+x1;
97 
98  // set the function points
99  for (n=0; n<npts; ++n)
100  f[n]=std::exp(x[n]);
101 
102  // find the integral approximation
104 
105  F_quad=quad(x.begin(), f.begin(), f.end());
106 
107  TEST_ASSERT_DELTA(1, F_quad/F_exact, 5e-7);
108  }
109 };
110 
111 #endif
void nonuniform_points_test()
Definition: trapezoid_test_suite.hpp:88
void AddTests(const long double &)
Definition: trapezoid_test_suite.hpp:42
Definition: trapezoid.hpp:25
Definition: math.hpp:25
trapezoid_test_suite()
Definition: trapezoid_test_suite.hpp:49
void uniform_points_test()
Definition: trapezoid_test_suite.hpp:59
void AddTests(const double &)
Definition: trapezoid_test_suite.hpp:36
~trapezoid_test_suite()
Definition: trapezoid_test_suite.hpp:54
void AddTests(const float &)
Definition: trapezoid_test_suite.hpp:30
Definition: trapezoid_test_suite.hpp:27