Code-Eli  0.3.6
floating_point_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 floating_point_test_suite_hpp
14 #define floating_point_test_suite_hpp
15 
16 #include <string>
17 #include <sstream>
18 
20 
21 template<typename data__>
22 class floating_point_test_suite : public Test::Suite
23 {
24  protected:
25  void AddTests(const float &)
26  {
29  }
30 
31  void AddTests(const double &)
32  {
35  }
36 
37  void AddTests(const long double &)
38  {
41  }
42 
43  public:
45  {
46  // add the tests
47  AddTests(data__());
48  }
50  {
51  }
52 
53  private:
54  typedef data__ data_type;
55 
56  private:
57 // TODO: Need to have methods that create data for each test for each data type
58 
60  {
62  }
63 
64  void printing_test(const float &)
65  {
66  float f(2);
67  const float cf(f);
70  std::stringstream ostr;
71  std::string ref;
72 
73  ref="0x0 0x0 0x80";
74  ostr << (*pft);
75  TEST_ASSERT(ostr.str()==ref);
76  ostr.str("");
77  ostr << (*pcft);
78  TEST_ASSERT(ostr.str()==ref);
79  ostr.str("");
80  }
81 
82  void printing_test(const double &)
83  {
84  double f(2);
85  const double cf(f);
88  std::stringstream ostr;
89  std::string ref;
90 
91  ref="0x0 0x0 0x400";
92  ostr << (*pft);
93  TEST_ASSERT(ostr.str()==ref);
94  ostr.str("");
95  ostr << (*pcft);
96  TEST_ASSERT(ostr.str()==ref);
97  ostr.str("");
98  }
99 
100  void printing_test(const long double &)
101  {
102  long double f(2);
103  const long double cf(f);
106  std::stringstream ostr;
107  std::string ref;
108 
109 #if defined(_WIN32)
110  ref="0x0 0x0 0x400";
111 #else
112  ref="0x0 0x1 0x0 0x4000";
113 #endif
114  ostr << (*pft);
115  TEST_ASSERT(ostr.str()==ref);
116  ostr.str("");
117  ostr << (*pcft);
118  TEST_ASSERT(ostr.str()==ref);
119  ostr.str("");
120  }
121 
123  {
124  // increment from zero by one ulp
125  {
126  data_type f, f_inc, f_inc_ref;
127 
128  get_zero_and_one_ulp(f, f_inc_ref);
129  f_inc=eli::util::increment_ulp(f, 1);
130  TEST_ASSERT(f_inc==f_inc_ref);
131  }
132 
133  // increment from one by one ulp
134  {
135  data_type f, f_inc, f_inc_ref;
136 
137  get_one_and_many_ulp(f, f_inc_ref, 1);
138  f_inc=eli::util::increment_ulp(f, 1);
139  TEST_ASSERT(f_inc==f_inc_ref);
140  }
141 
142  // increment from one by multiple ulps
143  {
144  data_type f, f_inc, f_inc_ref;
145 
146  get_one_and_many_ulp(f, f_inc_ref, 1000);
147  f_inc=eli::util::increment_ulp(f, 1000);
148  TEST_ASSERT(f_inc==f_inc_ref);
149  }
150 
151 
152  // increment from largest mantissa by one ulp
153  {
154  data_type f, f_inc, f_inc_ref;
155 
156  get_max_mantissa_and_many_ulp(f, f_inc_ref, 1);
157  f_inc=eli::util::increment_ulp(f, 1);
158  TEST_ASSERT(f_inc==f_inc_ref);
159  }
160 
161  // increment from largest mantissa by multiple ulps
162  {
163  data_type f, f_inc, f_inc_ref;
164 
165  get_max_mantissa_and_many_ulp(f, f_inc_ref, 1000);
166  f_inc=eli::util::increment_ulp(f, 1000);
167  TEST_ASSERT(f_inc==f_inc_ref);
168  }
169  }
170 
171  void get_zero_and_one_ulp(float &f, float &f_ulp)
172  {
173  int32_t *pi;
174  f=0;
175  f_ulp=f;
176 
177  pi=static_cast<int32_t *>(static_cast<void *>(&f_ulp));
178  ++(*pi);
179  }
180 
181  void get_zero_and_one_ulp(double &f, double &f_ulp)
182  {
183  int64_t *pi;
184  f=0;
185  f_ulp=f;
186 
187  pi=static_cast<int64_t *>(static_cast<void *>(&f_ulp));
188  ++(*pi);
189  }
190 
191  void get_zero_and_one_ulp(long double &f, long double &f_ulp)
192  {
193  int64_t *pi;
194  f=0;
195  f_ulp=f;
196 
197  pi=static_cast<int64_t *>(static_cast<void *>(&f_ulp));
198  ++(*pi);
199  }
200 
201  void get_one_and_many_ulp(data_type &f, data_type &f_ulp, int ulps)
202  {
203  f=1;
204  f_ulp=f+ulps*std::numeric_limits<data_type>::epsilon();
205  }
206 
207  void get_max_mantissa_and_many_ulp(data_type &f, data_type &f_ulp, int ulps)
208  {
209  f_ulp=2;
210  f=2-ulps*std::numeric_limits<data_type>::epsilon();
211  }
212 };
213 
214 #endif
void AddTests(const double &)
Definition: floating_point_test_suite.hpp:31
void printing_test()
Definition: floating_point_test_suite.hpp:59
const float_type * set_floating_point_type(const float *pcf)
Definition: floating_point.hpp:74
~floating_point_test_suite()
Definition: floating_point_test_suite.hpp:49
floating_point_test_suite()
Definition: floating_point_test_suite.hpp:44
Definition: floating_point_test_suite.hpp:22
Definition: floating_point.hpp:50
void AddTests(const long double &)
Definition: floating_point_test_suite.hpp:37
Definition: floating_point.hpp:38
void get_max_mantissa_and_many_ulp(data_type &f, data_type &f_ulp, int ulps)
Definition: floating_point_test_suite.hpp:207
void get_zero_and_one_ulp(float &f, float &f_ulp)
Definition: floating_point_test_suite.hpp:171
void get_one_and_many_ulp(data_type &f, data_type &f_ulp, int ulps)
Definition: floating_point_test_suite.hpp:201
void printing_test(const double &)
Definition: floating_point_test_suite.hpp:82
data__ data_type
Definition: floating_point_test_suite.hpp:54
void printing_test(const float &)
Definition: floating_point_test_suite.hpp:64
data__ increment_ulp(const data__ &, const int &)
Definition: floating_point.hpp:164
void AddTests(const float &)
Definition: floating_point_test_suite.hpp:25
Definition: floating_point.hpp:26
void increment_ulp_test()
Definition: floating_point_test_suite.hpp:122
void printing_test(const long double &)
Definition: floating_point_test_suite.hpp:100
void get_zero_and_one_ulp(double &f, double &f_ulp)
Definition: floating_point_test_suite.hpp:181
void get_zero_and_one_ulp(long double &f, long double &f_ulp)
Definition: floating_point_test_suite.hpp:191