Code-Eli  0.3.6
constants_math_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 constants_math_test_suite_hpp
14 #define constants_math_test_suite_hpp
15 
16 #include <cmath> // std::pow, std::exp
17 
18 #include <typeinfo> // typeid
19 #include <string> // std::string
20 #include <sstream> // std::stringstream
21 #include <iomanip> // std::setw
22 #include <vector> // std::vector
23 
24 #include <cmath> // std::tan and others
25 
26 #include "eli/constants/math.hpp"
27 
28 template<typename T__>
29 void set_pi(T__ &p)
30 {
31  p=3.14;
32 }
33 template<>
34 void set_pi<float>(float &p)
35 {
36  p=3.141592653589793238462643383279502884196716939937510582f;
37 }
38 template<>
39 void set_pi<double>(double &p)
40 {
41  p=3.141592653589793238462643383279502884196716939937510582;
42 }
43 template<>
44 void set_pi<long double>(long double &p)
45 {
46  p=3.141592653589793238462643383279502884196716939937510582L;
47 }
48 
49 template<typename data__>
50 class constants_math_test_suite : public Test::Suite
51 {
52  protected:
53  void AddTests(const float &)
54  {
58  }
59 
60  void AddTests(const double &)
61  {
65  }
66 
67  void AddTests(const long double &)
68  {
72  }
73 
74  public:
76  {
77  // add the tests
78  AddTests(data__());
79  }
81  {
82  }
83 
84  private:
85  void exp_test()
86  {
87  data__ ans, one(1), two(2);
88 
89  // test exp(1)
90  ans=std::exp(one);
91  TEST_ASSERT(ans==eli::constants::math<data__>::exp());
92 
93  // test ln(2)
94  ans=std::log(two);
95  TEST_ASSERT(ans==eli::constants::math<data__>::ln_two());
96 
97  // test log(exp(1))
98  ans=std::log10(std::exp(one));
99  TEST_ASSERT(ans==eli::constants::math<data__>::log_exp());
100  }
101 
102  void pi_test()
103  {
104  data__ ans, one(1), two(2), four(4), pi;
105 
106  // set reference value for pi
107  set_pi(pi);
108 
109  // test pi
110  ans=pi;
111  TEST_ASSERT(ans==eli::constants::math<data__>::pi());
112 
113  // test 2*pi
114  ans=two*pi;
115  TEST_ASSERT(ans==eli::constants::math<data__>::two_pi());
116 
117  // test pi/2
118  ans=pi/two;
119  TEST_ASSERT(ans==eli::constants::math<data__>::pi_by_two());
120 
121  // test pi/4
122  ans=pi/four;
123  TEST_ASSERT(ans==eli::constants::math<data__>::pi_by_four());
124 
125  // test pi*pi
126  ans=pi*pi;
127  TEST_ASSERT(ans==eli::constants::math<data__>::pi_squared());
128 
129  // test pi*pi*pi
130  ans=pi*pi*pi;
131  TEST_ASSERT(ans==eli::constants::math<data__>::pi_cubed());
132 
133  // test sqrt(pi)
134  ans=std::sqrt(pi);
135  TEST_ASSERT(ans==eli::constants::math<data__>::sqrt_pi());
136 
137  // test cbrt(pi)
138  ans=std::cbrt(pi);
139  TEST_ASSERT(ans==eli::constants::math<data__>::cbrt_pi());
140 
141  // test 1/pi
142  ans=one/pi;
143  TEST_ASSERT(ans==eli::constants::math<data__>::one_by_pi());
144 
145  // test 2/pi
146  ans=two/pi;
147  TEST_ASSERT(ans==eli::constants::math<data__>::two_by_pi());
148 
149  // test 1/sqrt(pi)
150  ans=one/std::sqrt(pi);
152 
153  // test 2/sqrt(pi)
154  ans=two/std::sqrt(pi);
156  }
157 
158  void sqrt_test()
159  {
160  data__ ans, two(2);
161 
162  // test sqrt(2)
163  ans=std::sqrt(two);
164  TEST_ASSERT(ans==eli::constants::math<data__>::sqrt_two());
165 
166  // test sqrt(2)/2
167  ans=std::sqrt(two)/two;
169  }
170 };
171 
172 #endif
173 
~constants_math_test_suite()
Definition: constants_math_test_suite.hpp:80
void AddTests(const long double &)
Definition: constants_math_test_suite.hpp:67
Definition: constants_math_test_suite.hpp:50
void set_pi< long double >(long double &p)
Definition: constants_math_test_suite.hpp:44
void AddTests(const float &)
Definition: constants_math_test_suite.hpp:53
Definition: math.hpp:25
void exp_test()
Definition: constants_math_test_suite.hpp:85
void sqrt_test()
Definition: constants_math_test_suite.hpp:158
constants_math_test_suite()
Definition: constants_math_test_suite.hpp:75
void set_pi(T__ &p)
Definition: constants_math_test_suite.hpp:29
void AddTests(const double &)
Definition: constants_math_test_suite.hpp:60
void set_pi< double >(double &p)
Definition: constants_math_test_suite.hpp:39
void pi_test()
Definition: constants_math_test_suite.hpp:102
void set_pi< float >(float &p)
Definition: constants_math_test_suite.hpp:34