ChimeraTK-DeviceAccess  03.18.00
testFixedPointConverter.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2 // SPDX-License-Identifier: LGPL-3.0-or-later
3 
4 #define BOOST_TEST_DYN_LINK
5 #define BOOST_TEST_MODULE FixedPointConverterTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 /* Test requirements:
10  * Test to and from double for the follwing cases:
11  * int32, uint32, int16, uint16, int8, uint8. No fractional bits (standard data
12  * types) 32 bits with -12 (negative), -1 (test rounding), 1 (test rounding), 7
13  * (somewhere in the middle), 31, 32 (resolution edge) and 43 (larger than 32
14  * bits), fractional bits, signed and unsigned 18 bits with -12, 0, 7, 17, 18,
15  * 43 fractional bits, signed and unsigned
16  *
17  * All tests are run with the bit sequence 0xAAAAAAAA (negative when signed)
18  * and 0x55555555 (positive when signed) to float,
19  * and with +-0.25, +-0.75, +-3.25 +-5.75 to fixed
20  * to test correct rounding.
21  *
22  */
23 
24 #define HEX_TO_DOUBLE(INPUT) static_cast<double>(INPUT)
25 #define SIGNED_HEX_TO_DOUBLE(INPUT) static_cast<double>(static_cast<int32_t>(INPUT))
26 #define SIGNED_HEX_TO_INT64(INPUT) static_cast<int64_t>(static_cast<int32_t>(INPUT))
27 
28 #define CHECK_SIGNED_FIXED_POINT_NO_FRACTIONAL \
29  checkToFixedPoint(converter, 0.25, 0); \
30  checkToFixedPoint(converter, -0.25, 0); \
31  checkToFixedPoint(converter, 0.75, 1); \
32  checkToFixedPoint(converter, -0.75, -1); \
33  checkToFixedPoint(converter, 3.25, 3); \
34  checkToFixedPoint(converter, -3.25, -3); \
35  checkToFixedPoint(converter, 5.75, 6); \
36  checkToFixedPoint(converter, -5.75, -6);
37 
38 #include "Exception.h"
39 #include "FixedPointConverter.h"
40 
41 #include <sstream>
42 namespace ChimeraTK {
43  using namespace ChimeraTK;
44 }
45 using namespace ChimeraTK;
46 
47 // get name of a type for better readible error messages
48 template<typename T>
49 inline const char* typeName(void) {
50  return "unknown";
51 }
52 #define DEF_TYPENAME(name) \
53  template<> \
54  inline const char* typeName<name>(void) { \
55  return #name; \
56  }
57 DEF_TYPENAME(int32_t)
58 DEF_TYPENAME(uint32_t)
59 DEF_TYPENAME(int16_t)
60 DEF_TYPENAME(uint16_t)
61 DEF_TYPENAME(int8_t)
62 DEF_TYPENAME(uint8_t)
63 DEF_TYPENAME(int64_t)
64 DEF_TYPENAME(uint64_t)
65 DEF_TYPENAME(float)
66 DEF_TYPENAME(double)
67 DEF_TYPENAME(std::string)
69 
70 template<typename T>
71 void checkToCookedOverflowNeg(FixedPointConverter const& converter, uint32_t input) {
72  BOOST_CHECK_EQUAL(converter.scalarToCooked<T>(input), std::numeric_limits<T>::min());
73 }
74 
75 template<typename T>
76 void checkToCookedOverflowPos(FixedPointConverter const& converter, uint32_t input) {
77  BOOST_CHECK_EQUAL(converter.scalarToCooked<T>(input), std::numeric_limits<T>::max());
78 }
79 
80 template<typename T>
81 void checkToCooked(FixedPointConverter const& converter, uint32_t input, T expectedValue) {
82  std::stringstream message;
83  message << "testToCooked failed for type " << typeName<T>() << " with input " << std::hex << "0x" << input << std::hex
84  << ", expected 0x" << expectedValue << std::dec;
85 
86  BOOST_TEST_CHECKPOINT(message.str());
87 
88  T output = converter.scalarToCooked<T>(input);
89 
90  message << std::hex << ", output 0x" << output << std::dec;
91 
92  BOOST_CHECK_MESSAGE(output == expectedValue, message.str());
93 }
94 
95 template<typename T>
96 void checkToRaw(FixedPointConverter const& converter, T input, uint32_t expectedValue) {
97  std::stringstream message;
98  message << "testToRaw failed for type " << typeName<T>() << " with input 0x" << std::hex << input << ", expected 0x"
99  << expectedValue << std::dec;
100 
101  BOOST_TEST_CHECKPOINT(message.str());
102 
103  uint32_t result = converter.toRaw(input);
104 
105  message << std::hex << ", output 0x" << result << std::dec;
106 
107  BOOST_CHECK_MESSAGE(result == expectedValue, message.str());
108 }
109 
110 BOOST_AUTO_TEST_SUITE(FixedPointConverterTestSuite)
111 
112 BOOST_AUTO_TEST_CASE(testConstructor) {
113  BOOST_CHECK_NO_THROW(FixedPointConverter("UnknownVariable"));
114  BOOST_CHECK_NO_THROW(FixedPointConverter("UnknownVariable", 16, 42, false));
115 
116  // number of significant bits
117  BOOST_CHECK_THROW(FixedPointConverter("UnknownVariable", 33), ChimeraTK::logic_error);
118 
119  // dynamic range of sufficient for bit shift
120  BOOST_CHECK_THROW(FixedPointConverter("UnknownVariable", 2, 1021 - 1), ChimeraTK::logic_error);
121  BOOST_CHECK_THROW(FixedPointConverter("UnknownVariable", 2, -1024 + 1), ChimeraTK::logic_error);
122  BOOST_CHECK_NO_THROW(FixedPointConverter("UnknownVariable", 2, 1021 - 2));
123  BOOST_CHECK_NO_THROW(FixedPointConverter("UnknownVariable", 2, -1024 + 2));
124 }
125 
127  FixedPointConverter converter("Variable32signed"); // default parameters are signed 32 bit
128  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA));
129  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555));
130  checkToCooked(converter, 0xAAAAAAAA, (int)0xAAAAAAAA);
131  checkToCooked(converter, 0x55555555, (int)0x55555555);
132  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_INT64(0xAAAAAAAA));
133  checkToCooked(converter, 0x55555555, (uint64_t)0x55555555);
134 
135  checkToCookedOverflowNeg<unsigned int>(converter, 0xAAAAAAAA);
136  checkToCooked(converter, 0x55555555, (unsigned int)0x55555555);
137  checkToCookedOverflowNeg<short>(converter, 0xAAAAAAAA);
138  checkToCookedOverflowPos<short>(converter, 0x55555555);
139  checkToCookedOverflowNeg<unsigned short>(converter, 0xAAAAAAAA);
140  checkToCookedOverflowPos<unsigned short>(converter, 0x55555555);
141 
142  checkToRaw(converter, 0.25, 0);
143  checkToRaw(converter, -0.25, 0);
144  checkToRaw(converter, 0.75, 1);
145  checkToRaw(converter, -0.75, -1);
146  checkToRaw(converter, 3.25, 3);
147  checkToRaw(converter, -3.25, -3);
148  checkToRaw(converter, 5.75, 6);
149  checkToRaw(converter, -5.75, -6);
150 
151  checkToRaw(converter, (int)0x55555555, 0x55555555);
152  checkToRaw(converter, (int)0xAAAAAAAA, 0xAAAAAAAA);
153  checkToRaw(converter, (unsigned int)0x55555555, 0x55555555);
154  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x7FFFFFFF);
155  checkToRaw(converter, (short)0x5555, 0x5555);
156  checkToRaw(converter, (short)0xAAAA, 0xFFFFAAAA);
157  checkToRaw(converter, (unsigned short)0x5555, 0x5555);
158  checkToRaw(converter, (unsigned short)0xAAAA, 0xAAAA);
159  checkToRaw(converter, (int64_t)0x5555, 0x5555);
160  checkToRaw(converter, (int64_t)0xFFFFFFFFFFFFAAAA, 0xFFFFAAAA);
161  checkToRaw(converter, (int64_t)0xFFFFFFFAAAAAAAAA,
162  0x80000000); // Smallest signed representation possible
163  checkToRaw(converter, (int64_t)0xFFFFFFFFF, 0x7FFFFFFF);
164  checkToRaw(converter, (uint64_t)0xFFFFFFFFF,
165  0x7FFFFFFF); // max signed representation possible
166 
167  checkToCooked(converter, 0x55555555, std::string("1431655765"));
168  checkToRaw(converter, std::string("1431655765"), 0x55555555);
169 
170  // Boolean check
171  checkToCooked(converter, 0x55555555, Boolean(true));
172 }
173 
174 BOOST_AUTO_TEST_CASE(testUInt32) {
175  FixedPointConverter converter("Variable32unsigned", 32, 0,
176  false); // 32 bits, 0 fractional bits, not signed
177 
178  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA));
179  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555));
180  checkToCookedOverflowPos<int>(converter, 0xAAAAAAAA);
181  checkToCooked(converter, 0x55555555, (int)0x55555555);
182  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0xAAAAAAAA);
183  checkToCooked(converter, 0x55555555, (unsigned int)0x55555555);
184  checkToCookedOverflowPos<short>(converter, 0xAAAAAAAA);
185  checkToCookedOverflowPos<unsigned short>(converter, 0x55555555);
186  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xAAAAAAAA);
187  checkToCooked(converter, 0x55555555, (uint64_t)0x55555555);
188 
189  checkToRaw(converter, 0.25, 0);
190  checkToRaw(converter, -0.25, 0);
191  checkToRaw(converter, 0.75, 1);
192  checkToRaw(converter, -0.75, 0);
193  checkToRaw(converter, 3.25, 3);
194  checkToRaw(converter, -3.25, 0);
195  checkToRaw(converter, 5.75, 6);
196  checkToRaw(converter, -5.75, 0);
197 
198  checkToRaw(converter, (int)0x55555555, 0x55555555);
199  checkToRaw(converter, (int)0xAAAAAAAA, 0);
200  checkToRaw(converter, (unsigned int)0x55555555, 0x55555555);
201  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0xAAAAAAAA);
202  checkToRaw(converter, (short)0x5555, 0x5555);
203  checkToRaw(converter, (short)0xAAAA, 0);
204  checkToRaw(converter, (unsigned short)0x5555, 0x5555);
205  checkToRaw(converter, (unsigned short)0xAAAA, 0xAAAA);
206  checkToRaw(converter, (int64_t)0x5555, 0x5555);
207  checkToRaw(converter, SIGNED_HEX_TO_INT64(0xAAAAAAAA),
208  0x0); // Lowest range of 32 bit wide unsigned register
209  checkToRaw(converter, (int64_t)0x100000000, 0xFFFFFFFF);
210  checkToRaw(converter, (uint64_t)0x100000000,
211  0xFFFFFFFF); // max signed representation possible
212 
213  checkToCooked(converter, 0x55555555, std::string("1431655765"));
214  checkToRaw(converter, std::string("1431655765"), 0x55555555);
215 
216  checkToCooked(converter, 0xAAAAAAAA, std::string("2863311530"));
217  checkToRaw(converter, std::string("2863311530"), 0xAAAAAAAA);
218 
219  checkToCooked(converter, 0xAAAAAAAA, Boolean(true));
220 }
221 
223  FixedPointConverter converter("Variable16signed",
224  16); // 16 bits, 0 fractional bits, signed
225 
226  checkToCooked(converter, 0xAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFFAAAA));
227  checkToCooked(converter, 0x5555, HEX_TO_DOUBLE(0x5555));
228  checkToCooked(converter, 0xAAAA, (int)0xFFFFAAAA);
229  checkToCooked(converter, 0x5555, (int)0x5555);
230  checkToCookedOverflowNeg<unsigned int>(converter, 0xAAAA);
231  checkToCooked(converter, 0x5555, (unsigned int)0x5555);
232  checkToCooked(converter, 0xAAAA, (short)0xAAAA);
233  checkToCooked(converter, 0x5555, (short)0x5555);
234  checkToCookedOverflowNeg<unsigned short>(converter, 0xAAAA);
235  checkToCooked(converter, 0x5555, (unsigned short)0x5555);
236  checkToCooked(converter, 0x5555, (int64_t)0x5555);
237  checkToCooked(converter, 0xAAAA, static_cast<int64_t>(static_cast<int16_t>(0xAAAA)));
238 
239  checkToCooked(converter, 0x5555, (uint64_t)0x5555);
240  checkToCookedOverflowNeg<uint64_t>(converter, 0xAAAA);
241 
242  checkToRaw(converter, 0.25, 0);
243  checkToRaw(converter, -0.25, 0);
244  checkToRaw(converter, 0.75, 1);
245  checkToRaw(converter, -0.75, 0xFFFF);
246  checkToRaw(converter, 3.25, 3);
247  checkToRaw(converter, -3.25, 0xFFFD);
248  checkToRaw(converter, 5.75, 6);
249  checkToRaw(converter, -5.75, 0xFFFA);
250 
251  checkToRaw(converter, (int)0x55555555, 0x7FFF);
252  checkToRaw(converter, (int)0xAAAAAAAA, 0x8000);
253  checkToRaw(converter, (unsigned int)0x55555555, 0x7FFF);
254  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x7FFF);
255  checkToRaw(converter, (short)0x5555, 0x5555);
256  checkToRaw(converter, (short)0xAAAA, 0xAAAA);
257  checkToRaw(converter, (unsigned short)0x5555, 0x5555);
258  checkToRaw(converter, (unsigned short)0xAAAA, 0x7FFF);
259  checkToRaw(converter, (int64_t)0x5555, 0x5555);
260  checkToRaw(converter, static_cast<int64_t>(static_cast<int16_t>(0xAAAA)), 0xAAAA);
261  checkToRaw(converter, (int64_t)0x555555, 0x7FFF);
262  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xAAAAAAAA)), 0x8000);
263  checkToRaw(converter, (uint64_t)0x5555, 0x5555);
264  checkToRaw(converter, (uint64_t)0x0, 0x0);
265  checkToRaw(converter, (uint64_t)0xF555, 0x7FFF);
266 }
267 
268 BOOST_AUTO_TEST_CASE(testUInt16) {
269  FixedPointConverter converter("Variable16unsigned", 16, 0,
270  false); // 16 bits, 0 fractional bits, not signed
271 
272  checkToCooked(converter, 0xAAAA, HEX_TO_DOUBLE(0xAAAA));
273  checkToCooked(converter, 0x5555, HEX_TO_DOUBLE(0x5555));
274  checkToCooked(converter, 0xAAAA, (int)0xAAAA);
275  checkToCooked(converter, 0x5555, (int)0x5555);
276  checkToCooked(converter, 0xAAAA, (unsigned int)0xAAAA);
277  checkToCooked(converter, 0x5555, (unsigned int)0x5555);
278  checkToCookedOverflowPos<short>(converter, 0xAAAA);
279  checkToCooked(converter, 0x5555, (short)0x5555);
280  checkToCooked(converter, 0xAAAA, (unsigned short)0xAAAA);
281  checkToCooked(converter, 0x5555, (unsigned short)0x5555);
282  checkToCooked(converter, 0x5555, (int64_t)0x5555);
283  checkToCooked(converter, 0xAAAA, (int64_t)0xAAAA);
284  checkToCooked(converter, 0x5555, (uint64_t)0x5555);
285  checkToCooked(converter, 0xAAAA, (uint64_t)0xAAAA);
286 
287  checkToRaw(converter, 0.25, 0);
288  checkToRaw(converter, -0.25, 0);
289  checkToRaw(converter, 0.75, 1);
290  checkToRaw(converter, -0.75, 0);
291  checkToRaw(converter, 3.25, 3);
292  checkToRaw(converter, -3.25, 0);
293  checkToRaw(converter, 5.75, 6);
294  checkToRaw(converter, -5.75, 0);
295 
296  checkToRaw(converter, (int)0x55555555, 0xFFFF);
297  checkToRaw(converter, (int)0xAAAAAAAA, 0);
298  checkToRaw(converter, (unsigned int)0x55555555, 0xFFFF);
299  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0xFFFF);
300  checkToRaw(converter, (short)0x5555, 0x5555);
301  checkToRaw(converter, (short)0xAAAA, 0);
302  checkToRaw(converter, (unsigned short)0x5555, 0x5555);
303  checkToRaw(converter, (unsigned short)0xAAAA, 0xAAAA);
304  checkToRaw(converter, (int64_t)0x5555, 0x5555);
305  checkToRaw(converter, static_cast<int64_t>(static_cast<int16_t>(0xAAAA)), 0);
306  checkToRaw(converter, (int64_t)0x555555, 0xFFFF);
307  checkToRaw(converter, (uint64_t)0x5555, 0x5555);
308  checkToRaw(converter, (uint64_t)0x0, 0x0);
309  checkToRaw(converter, (uint64_t)0xFF555, 0xFFFF);
310 }
311 
313  FixedPointConverter converter("Variable8signed",
314  8); // 8 bits, 0 fractional bits, signed
315 
316  checkToCooked(converter, 0xAA, SIGNED_HEX_TO_DOUBLE(0xFFFFFFAA));
317  checkToCooked(converter, 0x55, HEX_TO_DOUBLE(0x55));
318  checkToCooked(converter, 0xAA, (int)0xFFFFFFAA);
319  checkToCooked(converter, 0x55, (int)0x55);
320  checkToCookedOverflowNeg<unsigned int>(converter, 0xAA);
321  checkToCooked(converter, 0x55, (unsigned int)0x55);
322  checkToCooked(converter, 0xAA, (short)0xFFAA);
323  checkToCooked(converter, 0x55, (short)0x55);
324  checkToCookedOverflowNeg<unsigned short>(converter, 0xAA);
325  checkToCooked(converter, 0x55, (unsigned short)0x55);
326  checkToCooked(converter, 0x55, (int64_t)0x55);
327  checkToCooked(converter, 0xAA, static_cast<int64_t>(static_cast<int8_t>(0xAA)));
328  checkToCooked(converter, 0x55, (uint64_t)0x55);
329  checkToCookedOverflowNeg<uint64_t>(converter, 0xAA);
330 
331  checkToRaw(converter, 0.25, 0);
332  checkToRaw(converter, -0.25, 0);
333  checkToRaw(converter, 0.75, 1);
334  checkToRaw(converter, -0.75, 0xFF);
335  checkToRaw(converter, 3.25, 3);
336  checkToRaw(converter, -3.25, 0xFD);
337  checkToRaw(converter, 5.75, 6);
338  checkToRaw(converter, -5.75, 0xFA);
339 
340  checkToRaw(converter, (int)0x55555555, 0x7F);
341  checkToRaw(converter, (int)0xAAAAAAAA, 0x80);
342  checkToRaw(converter, (unsigned int)0x55555555, 0x7F);
343  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x7F);
344  checkToRaw(converter, (short)0x5555, 0x7F);
345  checkToRaw(converter, (short)0xAAAA, 0x80);
346  checkToRaw(converter, (unsigned short)0x5555, 0x7F);
347  checkToRaw(converter, (unsigned short)0xAAAA, 0x7F);
348 
349  checkToRaw(converter, (int64_t)0x55, 0x55);
350  checkToRaw(converter, static_cast<int64_t>(static_cast<int8_t>(0xAA)), 0xAA);
351  checkToRaw(converter, (int64_t)0x5555, 0x7F);
352  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xAAAAAAAA)), 0x80);
353  checkToRaw(converter, (uint64_t)0x55, 0x55);
354  checkToRaw(converter, (uint64_t)0xF5, 0x7F);
355 }
356 
358  FixedPointConverter converter("Variable8unsigned", 8, 0,
359  false); // 8 bits, 0 fractional bits, not signed
360 
361  checkToCooked(converter, 0xAA, HEX_TO_DOUBLE(0xAA));
362  checkToCooked(converter, 0x55, HEX_TO_DOUBLE(0x55));
363  checkToCooked(converter, 0xAA, (int)0xAA);
364  checkToCooked(converter, 0x55, (int)0x55);
365  checkToCooked(converter, 0xAA, (unsigned int)0xAA);
366  checkToCooked(converter, 0x55, (unsigned int)0x55);
367  checkToCooked(converter, 0xAA, (short)0xAA);
368  checkToCooked(converter, 0x55, (short)0x55);
369  checkToCooked(converter, 0xAA, (unsigned short)0xAA);
370  checkToCooked(converter, 0x55, (unsigned short)0x55);
371  checkToCooked(converter, 0x55, (int64_t)0x55);
372  checkToCooked(converter, 0xAA, (int64_t)0xAA);
373  checkToCooked(converter, 0x55, (uint64_t)0x55);
374  checkToCooked(converter, 0xAA, (uint64_t)0xAA);
375 
376  checkToRaw(converter, 0.25, 0);
377  checkToRaw(converter, -0.25, 0);
378  checkToRaw(converter, 0.75, 1);
379  checkToRaw(converter, -0.75, 0);
380  checkToRaw(converter, 3.25, 3);
381  checkToRaw(converter, -3.25, 0);
382  checkToRaw(converter, 5.75, 6);
383  checkToRaw(converter, -5.75, 0);
384 
385  checkToRaw(converter, (int)0x55555555, 0xFF);
386  checkToRaw(converter, (int)0xAAAAAAAA, 0);
387  checkToRaw(converter, (unsigned int)0x55555555, 0xFF);
388  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0xFF);
389  checkToRaw(converter, (short)0x5555, 0xFF);
390  checkToRaw(converter, (short)0xAAAA, 0);
391  checkToRaw(converter, (unsigned short)0x5555, 0xFF);
392  checkToRaw(converter, (unsigned short)0xAAAA, 0xFF);
393  checkToRaw(converter, (int64_t)0x55, 0x55);
394  checkToRaw(converter, static_cast<int64_t>(static_cast<int16_t>(0xAAAA)), 0);
395  checkToRaw(converter, (int64_t)0x555555, 0xFF);
396  checkToRaw(converter, (uint64_t)0x55, 0x55);
397  checkToRaw(converter, (uint64_t)0x0, 0x0);
398  checkToRaw(converter, (uint64_t)0xFF555, 0xFF);
399 }
400 
401 BOOST_AUTO_TEST_CASE(testInt32_fractionMinus12) {
402  FixedPointConverter converter("Variable32minus12signed", 32,
403  -12); // 32 bits, -12 fractional bits, signed
404 
405  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, 12)); // Basically a left shift 12
406  // places
407  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * pow(2, 12));
408  checkToCookedOverflowPos<int>(converter, 0x000AAAAA);
409  checkToCooked(converter, 0xFFFAAAAA, (int)0xAAAAA000);
410  checkToCooked(converter, 0x00055555, (int)0x55555000);
411  checkToCookedOverflowNeg<unsigned int>(converter, 0xFFFAAAAA);
412  checkToCooked(converter, 0x00055555, (unsigned int)0x55555000);
413  checkToCooked(converter, 0x000AAAAA, (unsigned int)0xAAAAA000);
414  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xFFFFFAAAAAAAA000);
415  checkToCooked(converter, 0x55555555, (int64_t)0x55555555000);
416  checkToCooked(converter, 0x55555555, (uint64_t)0x55555555000);
417  checkToCookedOverflowNeg<uint64_t>(converter, 0xAAAAAAAA);
418 
419  checkToRaw(converter, 0.25, 0);
420  checkToRaw(converter, -0.25, 0);
421  checkToRaw(converter, 0.75, 0);
422  checkToRaw(converter, -0.75, 0);
423  checkToRaw(converter, 3.25, 0);
424  checkToRaw(converter, -3.25, 0);
425  checkToRaw(converter, 5.75, 0);
426  checkToRaw(converter, -5.75, 0);
427 
428  checkToRaw(converter, (int)0x55555555, 0x00055555);
429  checkToRaw(converter, (int)0xAAAAAAAA, 0xFFFAAAAB);
430  checkToRaw(converter, (unsigned int)0x55555555, 0x00055555);
431  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x000AAAAB);
432  checkToRaw(converter, (short)0x5555, 0x00000005);
433  checkToRaw(converter, (short)0xAAAA, 0xFFFFFFFB);
434  checkToRaw(converter, (unsigned short)0x5555, 0x00000005);
435  checkToRaw(converter, (unsigned short)0xAAAA, 0x0000000B);
436  checkToRaw(converter, (int64_t)0x55555555, 0x00055555);
437  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xAAAAAAAA)), 0XFFFAAAAB);
438  checkToRaw(converter, (int64_t)0x5555555555555, 0x7fffffff); // full range
439  checkToRaw(converter, (int64_t)0xFFFFA55555555555, 0x80000000);
440  checkToRaw(converter, (uint64_t)0x55555, 0x00000055);
441  checkToRaw(converter, (uint64_t)0x5555555555555, 0X7FFFFFFF);
442 }
443 
444 BOOST_AUTO_TEST_CASE(testUInt32_fractionMinus12) {
445  FixedPointConverter converter("Variable32minus12unsigned", 32, -12,
446  false); // 32 bits, -12 fractional bits, not
447  // signed
448 
449  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, 12));
450  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * pow(2, 12));
451  checkToCookedOverflowPos<int>(converter, 0x000AAAAA);
452  checkToCooked(converter, 0x00055555, (int)0x55555000);
453  checkToCooked(converter, 0x00055555, (unsigned int)0x55555000);
454  checkToCooked(converter, 0x000AAAAA, (unsigned int)0xAAAAA000);
455  checkToCookedOverflowPos<unsigned short>(converter, 0x000AAAAA);
456  checkToCooked(converter, 0x00055555, (int64_t)0x55555000);
457  checkToCooked(converter, 0x000AAAAA, (int64_t)0xAAAAA000);
458  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xAAAAAAAA000);
459  checkToCooked(converter, 0x00055555, (uint64_t)0x55555000);
460  checkToCooked(converter, 0xAAAAAAAA, (uint64_t)0xAAAAAAAA000);
461 
462  checkToRaw(converter, 0.25, 0);
463  checkToRaw(converter, -0.25, 0);
464  checkToRaw(converter, 0.75, 0);
465  checkToRaw(converter, -0.75, 0);
466  checkToRaw(converter, 3.25, 0);
467  checkToRaw(converter, -3.25, 0);
468  checkToRaw(converter, 5.75, 0);
469  checkToRaw(converter, -5.75, 0);
470 
471  checkToRaw(converter, (int)0x55555555, 0x00055555);
472  checkToRaw(converter, (int)0xAAAAAAAA, 0);
473  checkToRaw(converter, (unsigned int)0x55555555, 0x00055555);
474  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x000AAAAB);
475  checkToRaw(converter, (short)0x5555, 0x00000005);
476  checkToRaw(converter, (short)0xAAAA, 0);
477  checkToRaw(converter, (unsigned short)0x5555, 0x00000005);
478  checkToRaw(converter, (unsigned short)0xAAAA, 0x0000000B);
479  checkToRaw(converter, (int64_t)0x55555555, 0x00055555);
480  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xAAAAAAAA)), 0x0);
481  checkToRaw(converter, (int64_t)0x5555555555555, 0xFFFFFFFF); // full range
482  checkToRaw(converter, (uint64_t)0x55555, 0x00000055);
483  checkToRaw(converter, (uint64_t)0x5555555555555, 0XFFFFFFFF);
484 }
485 
486 BOOST_AUTO_TEST_CASE(testInt32_fractionMinus1) {
487  FixedPointConverter converter("Variable32minus1signed", 32,
488  -1); // 32 bits, -1 fractional bits, signed
489 
490  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * 2);
491  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * 2);
492  checkToCookedOverflowNeg<int>(converter, 0xAAAAAAAA);
493  checkToCookedOverflowPos<int>(converter, 0x55555555);
494  checkToCooked(converter, 0x22222202, (int)0x44444404);
495  checkToCookedOverflowNeg<unsigned int>(converter, 0xAAAAAAAA);
496  checkToCooked(converter, 0x55555555, (unsigned int)0xAAAAAAAA);
497  checkToCooked(converter, 0x22222202, (unsigned int)0x44444404);
498  checkToCooked(converter, 0x7FFFFFFF, (unsigned int)0xFFFFFFFE);
499  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xFFFFFFFF55555554);
500 
501  checkToRaw(converter, 0.25, 0);
502  checkToRaw(converter, -0.25, 0);
503  checkToRaw(converter, 0.75, 0);
504  checkToRaw(converter, -0.75, 0);
505 
506  // bit pattern of 3 is 11, where the last digit is rounded up, and afterwards
507  // one bit is shifted. So the actual value is 4
508  checkToRaw(converter, 3.25, 0x2);
509  checkToRaw(converter, -3.25, 0xFFFFFFFE); // (-2)
510  checkToRaw(converter, 5.75, 0x3);
511  checkToRaw(converter, -5.75, 0xFFFFFFFD); // (-3)
512 
513  checkToRaw(converter, (int)0x55555554, 0x2AAAAAAA);
514  checkToRaw(converter, (int)0x55555555, 0x2AAAAAAB);
515  checkToRaw(converter, (int)0x55555556, 0x2AAAAAAB);
516  checkToRaw(converter, (int)0xAAAAAAAA, 0xD5555555);
517  checkToRaw(converter, (unsigned int)0x55555555, 0x2AAAAAAB);
518  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x55555555);
519  checkToRaw(converter, (short)0x5555, 0x00002AAB);
520  checkToRaw(converter, (short)0xAAAA, 0xFFFFD555);
521  checkToRaw(converter, (unsigned short)0x5555, 0x00002AAB);
522  checkToRaw(converter, (unsigned short)0xAAAA, 0x00005555);
523  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xAAAAAAAA)), 0xD5555555);
524  checkToRaw(converter, static_cast<uint64_t>(0xAAAAAAAA), 0x55555555);
525 }
526 
527 BOOST_AUTO_TEST_CASE(testUInt32_fractionMinus1) {
528  FixedPointConverter converter("Variable32minus1unsigned", 32, -1,
529  false); // 32 bits, -1 fractional bits, not signed
530 
531  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * 2);
532  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * 2);
533  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * 2);
534  checkToCooked(converter, 0x22222202, (int)0x44444404);
535  checkToCooked(converter, 0x55555555, (unsigned int)0xAAAAAAAA);
536  checkToCooked(converter, 0x22222202, (unsigned int)0x44444404);
537 
538  checkToRaw(converter, 0.25, 0);
539  checkToRaw(converter, -0.25, 0);
540  checkToRaw(converter, 0.75, 0);
541  checkToRaw(converter, -0.75, 0);
542 
543  // bit pattern of 3 is 11, where the last digit is rounded up, and afterwards
544  // one bit is shifted. So the actual value is 4
545  checkToRaw(converter, 3.25, 0x2);
546  checkToRaw(converter, -3.25, 0);
547  checkToRaw(converter, 5.75, 0x3);
548  checkToRaw(converter, -5.75, 0);
549 
550  checkToRaw(converter, (int)0x55555555, 0x2AAAAAAB);
551  checkToRaw(converter, (int)0xAAAAAAAA, 0);
552  checkToRaw(converter, (unsigned int)0x55555555, 0x2AAAAAAB);
553  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x55555555);
554  checkToRaw(converter, (short)0x5555, 0x00002AAB);
555  checkToRaw(converter, (short)0xAAAA, 0);
556  checkToRaw(converter, (unsigned short)0x5555, 0x00002AAB);
557  checkToRaw(converter, (unsigned short)0xAAAA, 0x00005555);
558 }
559 
560 BOOST_AUTO_TEST_CASE(testInt16_fractionMinus1) {
561  FixedPointConverter converter("Variable16minus1signed", 16, -1); // 16 bits, -1 fractional bits, signed
562 
563  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFFAAAA) * 2);
564  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x5555) * 2);
565  checkToCookedOverflowNeg<int16_t>(converter, 0xAAAAAAAA);
566  checkToCookedOverflowPos<int16_t>(converter, 0x55555555);
567  checkToCooked(converter, 0x22222202, (int)0x4404);
568  checkToCookedOverflowNeg<unsigned int>(converter, 0xAAAA);
569  checkToCooked(converter, 0x55555555, (unsigned int)0xAAAA);
570  checkToCooked(converter, 0x22222202, (unsigned int)0x4404);
571  checkToCooked(converter, 0x00007FFF, (unsigned int)0xFFFE);
572  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xFFFFFFFFFFFF5554);
573 
574  checkToRaw(converter, 0.25, 0);
575  checkToRaw(converter, -0.25, 0);
576  checkToRaw(converter, 0.75, 0);
577  checkToRaw(converter, -0.75, 0);
578 
579  // bit pattern of 3 is 11, where the last digit is rounded up, and afterwards
580  // one bit is shifted. So the actual value is 4
581  checkToRaw(converter, 3.25, 0x2);
582  checkToRaw(converter, -3.25, 0xFFFE); // (-2)
583  checkToRaw(converter, 5.75, 0x3);
584  checkToRaw(converter, -5.75, 0xFFFD); // (-3)
585 
586  checkToRaw(converter, (int)0x5554, 0x2AAA);
587  checkToRaw(converter, (int)0x5555, 0x2AAB);
588  checkToRaw(converter, (int)0x5556, 0x2AAB);
589  checkToRaw(converter, (int)0xFFFFAAAA, 0xD555);
590  checkToRaw(converter, (unsigned int)0x5555, 0x2AAB);
591  checkToRaw(converter, (unsigned int)0xAAAA, 0x5555);
592  checkToRaw(converter, (short)0x5555, 0x00002AAB);
593  checkToRaw(converter, (short)0xAAAA, 0xD555);
594  checkToRaw(converter, (unsigned short)0x5555, 0x00002AAB);
595  checkToRaw(converter, (unsigned short)0xAAAA, 0x00005555);
596  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xFFFFAAAA)), 0xD555);
597  checkToRaw(converter, static_cast<uint64_t>(0xAAAA), 0x5555);
598 }
599 
600 BOOST_AUTO_TEST_CASE(testUInt16_fractionMinus1) {
601  FixedPointConverter converter("Variable16minus1unsigned", 16, -1, false); // 16 bits, -1 fractional bits, not signed
602 
603  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAA) * 2);
604  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x5555) * 2);
605  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x5555) * 2);
606  checkToCooked(converter, 0x22222202, (int)0x4404);
607  checkToCooked(converter, 0x55555555, (unsigned int)0xAAAA);
608  checkToCooked(converter, 0x22222202, (unsigned int)0x4404);
609 
610  checkToRaw(converter, 0.25, 0);
611  checkToRaw(converter, -0.25, 0);
612  checkToRaw(converter, 0.75, 0);
613  checkToRaw(converter, -0.75, 0);
614 
615  // bit pattern of 3 is 11, where the last digit is rounded up, and afterwards
616  // one bit is shifted. So the actual value is 4
617  checkToRaw(converter, 3.25, 0x2);
618  checkToRaw(converter, -3.25, 0);
619  checkToRaw(converter, 5.75, 0x3);
620  checkToRaw(converter, -5.75, 0);
621 
622  checkToRaw(converter, (int)0x5555, 0x2AAB);
623  checkToRaw(converter, (int)0xFFFFAAAA, 0);
624  checkToRaw(converter, (unsigned int)0x5555, 0x2AAB);
625  checkToRaw(converter, (unsigned int)0xAAAA, 0x5555);
626  checkToRaw(converter, (short)0x5555, 0x00002AAB);
627  checkToRaw(converter, (short)0xAAAA, 0);
628  checkToRaw(converter, (unsigned short)0x5555, 0x00002AAB);
629  checkToRaw(converter, (unsigned short)0xAAAA, 0x00005555);
630 }
631 
632 BOOST_AUTO_TEST_CASE(testInt32_fraction1) {
633  FixedPointConverter converter("Variable32plus1signed", 32,
634  1); // 32 bits, 1 fractional bit, signed
635 
636  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * 0.5);
637  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * 0.5);
638  checkToCooked(converter, 0xAAAAAAA9, (int)0xD5555554);
639  checkToCooked(converter, 0xAAAAAAAA, (int)0xD5555555);
640  checkToCooked(converter, 0xAAAAAAAB, (int)0xD5555555);
641  checkToCooked(converter, 0xFFFFFE11, (int)0xFFFFFF08);
642  checkToCooked(converter, 0x55555554, (int)0x2AAAAAAA);
643  checkToCooked(converter, 0x55555555, (int)0x2AAAAAAB);
644  checkToCooked(converter, 0x55555556, (int)0x2AAAAAAB);
645  checkToCooked(converter, 0x22222202, (int)0x11111101);
646  checkToCooked(converter, 0x55555555, (unsigned int)0x2AAAAAAB);
647  checkToCooked(converter, 0x22222202, (unsigned int)0x11111101);
648  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0xFFFFFFFFD5555555);
649  checkToCooked(converter, 0x55555555, (int64_t)0x2AAAAAAB);
650 
651  checkToRaw(converter, 0.25, 0x1);
652  checkToRaw(converter, -0.25, 0xFFFFFFFF);
653  checkToRaw(converter, 0.75, 0x2);
654  checkToRaw(converter, -0.75, 0xFFFFFFFE);
655 
656  checkToRaw(converter, 3.25, 0x7);
657  checkToRaw(converter, -3.25, 0xFFFFFFF9); // (-7)
658  checkToRaw(converter, 5.75, 0xC);
659  checkToRaw(converter, -5.75, 0xFFFFFFF4); // (-12)
660 
661  checkToRaw(converter, (int)0x55555555, 0x7FFFFFFF);
662  checkToRaw(converter, (int)0xAAAAAAAA, 0x80000000);
663  checkToRaw(converter, (int)0x22222202, 0x44444404);
664  checkToRaw(converter, (int)0xE2222202, 0xC4444404);
665  checkToRaw(converter, (unsigned int)0x55555555, 0x7FFFFFFF);
666  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x7FFFFFFF);
667  checkToRaw(converter, (unsigned int)0x22222202, 0x44444404);
668  checkToRaw(converter, (unsigned int)0xE2222202, 0x7FFFFFFF);
669  checkToRaw(converter, (short)0x5555, 0x0000AAAA);
670  checkToRaw(converter, (short)0xAAAA, 0xFFFF5554);
671  checkToRaw(converter, (unsigned short)0x5555, 0x0000AAAA);
672  checkToRaw(converter, (unsigned short)0xAAAA, 0x00015554);
673  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xFAAAAAAA)), 0XF5555554);
674  checkToRaw(converter, static_cast<uint64_t>(0xAAAAAAA), 0X15555554);
675 }
676 
677 BOOST_AUTO_TEST_CASE(testUInt32_fraction1) {
678  FixedPointConverter converter("Variable32plus1unsigned", 32, 1,
679  false); // 32 bits, 1 fractional bit, not signed
680 
681  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * 0.5);
682  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * 0.5);
683  checkToCooked(converter, 0xAAAAAAAA, (int)0x55555555);
684  checkToCooked(converter, 0x55555555, (int)0x2AAAAAAB);
685  checkToCooked(converter, 0x22222202, (int)0x11111101);
686  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0x55555555);
687  checkToCooked(converter, 0x55555555, (unsigned int)0x2AAAAAAB);
688  checkToCooked(converter, 0x22222202, (unsigned int)0x11111101);
689  checkToCooked(converter, 0xAAAAAAAA, (int64_t)0x55555555);
690  checkToCooked(converter, 0x55555555, (int64_t)0x2AAAAAAB);
691 
692  checkToRaw(converter, 0.25, 0x1);
693  checkToRaw(converter, -0.25, 0x0);
694  checkToRaw(converter, 0.75, 0x2);
695  checkToRaw(converter, -0.75, 0x0);
696 
697  checkToRaw(converter, 3.25, 0x7);
698  checkToRaw(converter, -3.25, 0x0);
699  checkToRaw(converter, 5.75, 0xC);
700  checkToRaw(converter, -5.75, 0x0);
701 
702  checkToRaw(converter, (int)0x55555555, 0xAAAAAAAA);
703  checkToRaw(converter, (int)0xAAAAAAAA, 0);
704  checkToRaw(converter, (int)0x22222202, 0x44444404);
705  checkToRaw(converter, (int)0xE2222202, 0);
706  checkToRaw(converter, (unsigned int)0x55555555, 0xAAAAAAAA);
707  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0xFFFFFFFF);
708  checkToRaw(converter, (unsigned int)0x22222202, 0x44444404);
709  checkToRaw(converter, (unsigned int)0xE2222202, 0xFFFFFFFF);
710  checkToRaw(converter, (short)0x5555, 0x0000AAAA);
711  checkToRaw(converter, (short)0xAAAA, 0);
712  checkToRaw(converter, (unsigned short)0x5555, 0x0000AAAA);
713  checkToRaw(converter, (unsigned short)0xAAAA, 0x00015554);
714  checkToRaw(converter, static_cast<int64_t>(static_cast<int32_t>(0xFAAAAAAA)), 0x0);
715  checkToRaw(converter, static_cast<uint64_t>(0xFAAAAAAA), 0XFFFFFFFF);
716 }
717 
718 BOOST_AUTO_TEST_CASE(testInt32_fraction7) {
719  FixedPointConverter converter("Variable32plus7signed", 32,
720  7); // 32 bits, 7 fractional bits, signed
721 
722  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -7));
723  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * pow(2, -7));
724  checkToCooked(converter, 0xAAAAAA20, (int)0xFF555554);
725  checkToCooked(converter, 0xAAAAAA60, (int)0xFF555555);
726  checkToCooked(converter, 0xAAAAAA80, (int)0xFF555555);
727  checkToCooked(converter, 0xAAAAAAAA, (int)0xFF555555);
728  checkToCooked(converter, 0xAAAAAAC0, (int)0xFF555555);
729  checkToCooked(converter, 0xAAAAAAD0, (int)0xFF555556);
730  checkToCooked(converter, 0xAAAAAAFF, (int)0xFF555556);
731  checkToCooked(converter, 0x5555553F, (int)0x00AAAAAA);
732  checkToCooked(converter, 0x55555540, (int)0x00AAAAAB);
733  checkToCooked(converter, 0x555555BF, (int)0x00AAAAAB);
734  checkToCooked(converter, 0x555555C0, (int)0x00AAAAAC);
735  checkToCooked(converter, 0x22220222, (int)0x00444404);
736  checkToCooked(converter, 0x55555555, (unsigned int)0x00AAAAAB);
737  checkToCooked(converter, 0x22220222, (unsigned int)0x00444404);
738 
739  checkToRaw(converter, 0.25, 0x20);
740  checkToRaw(converter, -0.25, 0xFFFFFFE0);
741  checkToRaw(converter, 0.75, 0x60);
742  checkToRaw(converter, -0.75, 0xFFFFFFA0);
743 
744  checkToRaw(converter, 3.25, 0x1A0);
745  checkToRaw(converter, -3.25, 0xFFFFFE60);
746  checkToRaw(converter, 5.75, 0x2E0);
747  checkToRaw(converter, -5.75, 0xFFFFFD20);
748 
749  checkToRaw(converter, (int)0x55555555, 0x7FFFFFFF);
750  checkToRaw(converter, (int)0xAAAAAAAA, 0x80000000);
751  checkToRaw(converter, (int)0x00888808, 0x44440400);
752  checkToRaw(converter, (int)0xFF888808, 0xC4440400);
753  checkToRaw(converter, (unsigned int)0x55555555, 0x7FFFFFFF);
754  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0x7FFFFFFF);
755  checkToRaw(converter, (unsigned int)0x00888808, 0x44440400);
756  checkToRaw(converter, (unsigned int)0xFF888808, 0x7FFFFFFF);
757  checkToRaw(converter, (short)0x5555, 0x002AAA80);
758  checkToRaw(converter, (short)0xAAAA, 0xFFD55500);
759  checkToRaw(converter, (unsigned short)0x5555, 0x002AAA80);
760  checkToRaw(converter, (unsigned short)0xAAAA, 0x00555500);
761 
762  checkToCooked(converter, 0x20, std::string("0.250000"));
763  checkToRaw(converter, std::string("0.25"), 0x20);
764 }
765 
766 BOOST_AUTO_TEST_CASE(testUInt32_fraction7) {
767  FixedPointConverter converter("Variable32plus7unsigned", 32, 7,
768  false); // 32 bits, -7 fractional bits, not signed
769 
770  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -7));
771  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * pow(2, -7));
772  checkToCooked(converter, 0xAAAAAAAA, (int)0x01555555);
773  checkToCooked(converter, 0x55555555, (int)0x00AAAAAB);
774  checkToCooked(converter, 0x22220222, (int)0x00444404);
775  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0x01555555);
776  checkToCooked(converter, 0x55555555, (unsigned int)0x00AAAAAB);
777  checkToCooked(converter, 0x22220222, (unsigned int)0x00444404);
778 
779  checkToRaw(converter, 0.25, 0x20);
780  checkToRaw(converter, -0.25, 0x0);
781  checkToRaw(converter, 0.75, 0x60);
782  checkToRaw(converter, -0.75, 0x0);
783 
784  checkToRaw(converter, 3.25, 0x1A0);
785  checkToRaw(converter, -3.25, 0x0);
786  checkToRaw(converter, 5.75, 0x2E0);
787  checkToRaw(converter, -5.75, 0x0);
788 
789  checkToRaw(converter, (int)0x55555555, 0xFFFFFFFF);
790  checkToRaw(converter, (int)0xAAAAAAAA, 0);
791  checkToRaw(converter, (int)0x00888808, 0x44440400);
792  checkToRaw(converter, (int)0xFF888808, 0);
793  checkToRaw(converter, (unsigned int)0x55555555, 0xFFFFFFFF);
794  checkToRaw(converter, (unsigned int)0xAAAAAAAA, 0xFFFFFFFF);
795  checkToRaw(converter, (unsigned int)0x00888808, 0x44440400);
796  checkToRaw(converter, (unsigned int)0xFF888808, 0xFFFFFFFF);
797  checkToRaw(converter, (short)0x5555, 0x002AAA80);
798  checkToRaw(converter, (short)0xAAAA, 0);
799  checkToRaw(converter, (unsigned short)0x5555, 0x002AAA80);
800  checkToRaw(converter, (unsigned short)0xAAAA, 0x00555500);
801 }
802 
803 BOOST_AUTO_TEST_CASE(testInt32_fraction31) {
804  FixedPointConverter converter("Variable32plus31signed", 32,
805  31); // 32 bits, 31 fractional bits, signed
806 
807  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -31));
808  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * pow(2, -31));
809  checkToCooked(converter, 0xAAAAAAAA, (int)-1);
810  checkToCooked(converter, 0x55555555, (int)1);
811  checkToCooked(converter, 0x22220222, (int)0);
812  checkToCookedOverflowNeg<unsigned int>(converter, 0xAAAAAAAA);
813  checkToCooked(converter, 0x55555555, (unsigned int)1);
814  checkToCooked(converter, 0x22220222, (unsigned int)0);
815  checkToCooked(converter, 0xAAAAAAAA, (short)-1);
816  checkToCooked(converter, 0x55555555, (short)1);
817  checkToCookedOverflowNeg<unsigned short>(converter, 0xAAAAAAAA);
818  checkToCooked(converter, 0x55555555, (unsigned short)1);
819 
820  checkToRaw(converter, 0.25, 0x20000000);
821  checkToRaw(converter, -0.25, 0xE0000000);
822  checkToRaw(converter, 0.75, 0x60000000);
823  checkToRaw(converter, -0.75, 0xA0000000);
824 
825  // these values are out of range
826  checkToRaw(converter, 3.25, 0x7FFFFFFF);
827  checkToRaw(converter, -3.25, 0x80000000);
828  checkToRaw(converter, 5.75, 0x7FFFFFFF);
829  checkToRaw(converter, -5.75, 0x80000000);
830 
831  checkToCooked(converter, 0xA0000000, -0.75);
832  checkToCooked(converter, 0x60000000, 0.75);
833  checkToCooked(converter, 0xE0000000, -0.25);
834  checkToCooked(converter, 0x20000000, 0.25);
835 
836  checkToRaw(converter, (int)0x55555555, 0x7FFFFFFF);
837  checkToRaw(converter, (int)0xAAAAAAAA, 0x80000000);
838  checkToRaw(converter, (int)0, 0);
839  checkToRaw(converter, (int)-1, 0x80000000);
840  checkToRaw(converter, (unsigned int)0x55555555, 0x7FFFFFFF);
841  checkToRaw(converter, (short)0x5555, 0x7FFFFFFF);
842  checkToRaw(converter, (short)0xAAAA, 0x80000000);
843  checkToRaw(converter, (short)-1, 0x80000000);
844  checkToRaw(converter, (unsigned short)0x5555, 0x7FFFFFFF);
845 }
846 
847 BOOST_AUTO_TEST_CASE(testUInt32_fraction31) {
848  FixedPointConverter converter("Variable32plus31unsigned", 32, 31,
849  false); // 32 bits, 31 fractional bits, not signed
850 
851  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -31));
852  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * pow(2, -31));
853  checkToCooked(converter, 0xAAAAAAAA, (int)1);
854  checkToCooked(converter, 0x55555555, (int)1);
855  checkToCooked(converter, 0x22220222, (int)0);
856  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)1);
857  checkToCooked(converter, 0x55555555, (unsigned int)1);
858  checkToCooked(converter, 0x22220222, (unsigned int)0);
859  checkToCooked(converter, 0xAAAAAAAA, (short)1);
860  checkToCooked(converter, 0x55555555, (short)1);
861  checkToCooked(converter, 0xAAAAAAAA, (unsigned short)1);
862  checkToCooked(converter, 0x55555555, (unsigned short)1);
863 
864  checkToRaw(converter, 0.25, 0x20000000);
865  checkToRaw(converter, -0.25, 0x0);
866  checkToRaw(converter, 0.75, 0x60000000);
867  checkToRaw(converter, -0.75, 0x0);
868 
869  // these values are out of range
870  checkToRaw(converter, 3.25, 0xFFFFFFFF);
871  checkToRaw(converter, -3.25, 0x0);
872  checkToRaw(converter, 5.75, 0xFFFFFFFF);
873  checkToRaw(converter, -5.75, 0x0);
874 
875  checkToCooked(converter, 0xA0000000, 1.25);
876  checkToCooked(converter, 0x60000000, 0.75);
877  checkToCooked(converter, 0xE0000000, 1.75);
878  checkToCooked(converter, 0x20000000, 0.25);
879 
880  checkToRaw(converter, (int)0x55555555, 0xFFFFFFFF);
881  checkToRaw(converter, (int)0xAAAAAAAA, 0);
882  checkToRaw(converter, (int)1, 0x80000000);
883  checkToRaw(converter, (unsigned int)0x55555555, 0xFFFFFFFF);
884  checkToRaw(converter, (unsigned int)1, 0x80000000);
885  checkToRaw(converter, (short)0x5555, 0xFFFFFFFF);
886  checkToRaw(converter, (short)0xAAAA, 0);
887  checkToRaw(converter, (short)1, 0x80000000);
888  checkToRaw(converter, (unsigned short)0x5555, 0xFFFFFFFF);
889  checkToRaw(converter, (unsigned short)1, 0x80000000);
890 }
891 
892 BOOST_AUTO_TEST_CASE(testInt32_fraction32) {
893  FixedPointConverter converter("Variable32plus32signed", 32,
894  32); // 32 bits, 32 fractional bits, signed
895 
896  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -32));
897  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * pow(2, -32));
898  checkToCooked(converter, 0xAAAAAAAA, (int)0);
899  checkToCooked(converter, 0x55555555, (int)0);
900  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0);
901  checkToCooked(converter, 0x55555555, (unsigned int)0);
902  checkToCooked(converter, 0xAAAAAAAA, (short)0);
903  checkToCooked(converter, 0x55555555, (short)0);
904  checkToCooked(converter, 0xAAAAAAAA, (unsigned short)0);
905  checkToCooked(converter, 0x55555555, (unsigned short)0);
906 
907  checkToRaw(converter, 0.25, 0x40000000);
908  checkToRaw(converter, -0.25, 0xC0000000);
909 
910  // these values are out of range
911  checkToRaw(converter, 0.75, 0x7FFFFFFF);
912  checkToRaw(converter, -0.75, 0x80000000);
913  checkToRaw(converter, 3.25, 0x7FFFFFFF);
914  checkToRaw(converter, -3.25, 0x80000000);
915  checkToRaw(converter, 5.75, 0x7FFFFFFF);
916  checkToRaw(converter, -5.75, 0x80000000);
917 
918  checkToCooked(converter, 0x40000000, 0.25);
919  checkToCooked(converter, 0xC0000000, -0.25);
920 
921  checkToRaw(converter, (int)0x55555555, 0x7FFFFFFF);
922  checkToRaw(converter, (int)0xAAAAAAAA, 0x80000000);
923  checkToRaw(converter, (int)0, 0);
924  checkToRaw(converter, (int)1, 0x7FFFFFFF);
925  checkToRaw(converter, (int)-1, 0x80000000);
926  // checkToRaw( converter, (unsigned int)0x55555555, 0x7FFFFFFF );
927  checkToRaw(converter, (short)0x5555, 0x7FFFFFFF);
928  checkToRaw(converter, (short)0xAAAA, 0x80000000);
929  checkToRaw(converter, (short)-1, 0x80000000);
930  // checkToRaw( converter, (unsigned short)0x5555, 0x7FFFFFFF );
931 }
932 
933 BOOST_AUTO_TEST_CASE(testUInt32_fraction32) {
934  FixedPointConverter converter("Variable32plus32unsigned", 32, 32,
935  false); // 32 bits, 32 fractional bits, not signed
936 
937  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -32));
938  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * pow(2, -32));
939  checkToCooked(converter, 0xAAAAAAAA, (int)1);
940  checkToCooked(converter, 0x55555555, (int)0);
941  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)1);
942  checkToCooked(converter, 0x55555555, (unsigned int)0);
943  checkToCooked(converter, 0xAAAAAAAA, (short)1);
944  checkToCooked(converter, 0x55555555, (short)0);
945  checkToCooked(converter, 0xAAAAAAAA, (unsigned short)1);
946  checkToCooked(converter, 0x55555555, (unsigned short)0);
947 
948  checkToRaw(converter, 0.25, 0x40000000);
949  checkToRaw(converter, -0.25, 0x0);
950  checkToRaw(converter, 0.75, 0xC0000000);
951  checkToRaw(converter, -0.75, 0x0);
952 
953  // these values are out of range
954  checkToRaw(converter, 3.25, 0xFFFFFFFF);
955  checkToRaw(converter, -3.25, 0x0);
956  checkToRaw(converter, 5.75, 0xFFFFFFFF);
957  checkToRaw(converter, -5.75, 0x0);
958 
959  checkToCooked(converter, 0x40000000, 0.25);
960  checkToCooked(converter, 0xC0000000, 0.75);
961 
962  checkToRaw(converter, (int)0x55555555, 0xFFFFFFFF);
963  checkToRaw(converter, (int)0xAAAAAAAA, 0);
964  checkToRaw(converter, (int)0, 0);
965  checkToRaw(converter, (int)-1, 0);
966  checkToRaw(converter, (unsigned int)0x55555555, 0xFFFFFFFF);
967  checkToRaw(converter, (short)0x5555, 0xFFFFFFFF);
968  checkToRaw(converter, (short)0xAAAA, 0);
969  checkToRaw(converter, (short)-1, 0);
970  checkToRaw(converter, (unsigned short)0x5555, 0xFFFFFFFF);
971 }
972 
973 BOOST_AUTO_TEST_CASE(testInt32_fraction43) {
974  FixedPointConverter converter("Variable32plus43signed", 32,
975  43); // 32 bits, 43 fractional bits, signed
976 
977  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -43));
978  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x55555555) * pow(2, -43));
979  checkToCooked(converter, 0xAAAAAAAA, (int)0);
980  checkToCooked(converter, 0x55555555, (int)0);
981  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0);
982  checkToCooked(converter, 0x55555555, (unsigned int)0);
983  checkToCooked(converter, 0xAAAAAAAA, (short)0);
984  checkToCooked(converter, 0x55555555, (short)0);
985  checkToCooked(converter, 0xAAAAAAAA, (unsigned short)0);
986  checkToCooked(converter, 0x55555555, (unsigned short)0);
987  checkToCooked(converter, 0x555, (int64_t)0);
988  checkToCooked(converter, 0x555, (uint64_t)0);
989 
990  // all out of range
991  checkToRaw(converter, 0.25, 0x7FFFFFFF);
992  checkToRaw(converter, -0.25, 0x80000000);
993  checkToRaw(converter, 0.75, 0x7FFFFFFF);
994  checkToRaw(converter, -0.75, 0x80000000);
995 
996  checkToRaw(converter, 3.25, 0x7FFFFFFF);
997  checkToRaw(converter, -3.25, 0x80000000);
998  checkToRaw(converter, 5.75, 0x7FFFFFFF);
999  checkToRaw(converter, -5.75, 0x80000000);
1000 
1001  checkToRaw(converter, (int)0x55555555, 0x7FFFFFFF);
1002  checkToRaw(converter, (int)0xAAAAAAAA, 0x80000000);
1003  checkToRaw(converter, (int)0, 0);
1004  checkToRaw(converter, (int)-1, 0x80000000);
1005  checkToRaw(converter, (unsigned int)0x55555555, 0x7FFFFFFF);
1006  checkToRaw(converter, (short)0x5555, 0x7FFFFFFF);
1007  checkToRaw(converter, (short)0xAAAA, 0x80000000);
1008  checkToRaw(converter, (short)-1, 0x80000000);
1009  checkToRaw(converter, (unsigned short)0x5555, 0x7FFFFFFF);
1010  checkToRaw(converter, (int64_t)0xFFFFFFFAAAAAAAAA, 0x80000000);
1011  checkToRaw(converter, (uint64_t)0xAAAAAAAAA, 0x7FFFFFFF);
1012 }
1013 
1014 BOOST_AUTO_TEST_CASE(testUInt32_fraction43) {
1015  FixedPointConverter converter("Variable32plus43unsigned", 32, 43,
1016  false); // 32 bits, 43 fractional bits, not signed
1017 
1018  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0xAAAAAAAA) * pow(2, -43));
1019  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x55555555) * pow(2, -43));
1020  checkToCooked(converter, 0xAAAAAAAA, (int)0);
1021  checkToCooked(converter, 0x55555555, (int)0);
1022  checkToCooked(converter, 0xAAAAAAAA, (unsigned int)0);
1023  checkToCooked(converter, 0x55555555, (unsigned int)0);
1024  checkToCooked(converter, 0xAAAAAAAA, (short)0);
1025  checkToCooked(converter, 0x55555555, (short)0);
1026  checkToCooked(converter, 0xAAAAAAAA, (unsigned short)0);
1027  checkToCooked(converter, 0x55555555, (unsigned short)0);
1028 
1029  // all out of range
1030  checkToRaw(converter, 0.25, 0xFFFFFFFF);
1031  checkToRaw(converter, -0.25, 0x0);
1032  checkToRaw(converter, 0.75, 0xFFFFFFFF);
1033  checkToRaw(converter, -0.75, 0x0);
1034 
1035  checkToRaw(converter, 3.25, 0xFFFFFFFF);
1036  checkToRaw(converter, -3.25, 0x0);
1037  checkToRaw(converter, 5.75, 0xFFFFFFFF);
1038  checkToRaw(converter, -5.75, 0x0);
1039 
1040  checkToRaw(converter, (int)0x55555555, 0xFFFFFFFF);
1041  checkToRaw(converter, (int)0xAAAAAAAA, 0);
1042  checkToRaw(converter, (int)0, 0);
1043  checkToRaw(converter, (int)-1, 0);
1044  checkToRaw(converter, (unsigned int)0x55555555, 0xFFFFFFFF);
1045  checkToRaw(converter, (short)0x5555, 0xFFFFFFFF);
1046  checkToRaw(converter, (short)0xAAAA, 0);
1047  checkToRaw(converter, (short)-1, 0);
1048  checkToRaw(converter, (unsigned short)0x5555, 0xFFFFFFFF);
1049 }
1050 
1051 BOOST_AUTO_TEST_CASE(testInt18_fractionMinus12) {
1052  FixedPointConverter converter("int18_fractionMinus12", 18,
1053  -12); // 10 bits, -12 fractional bits, signed
1054 
1055  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, 12));
1056  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, 12));
1057 
1058  // the converter should ignore bits which are not in the spec
1059  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, 12));
1060  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, 12));
1061 
1062  checkToCooked(converter, 0x2AAAA, (int)0xEAAAA000);
1063  checkToCooked(converter, 0x15555, (int)0x15555000);
1064  checkToCooked(converter, 0x15555, (unsigned int)0x15555000);
1065 
1066  checkToRaw(converter, 0.25, 0);
1067  checkToRaw(converter, -0.25, 0);
1068  checkToRaw(converter, 0.75, 0);
1069  checkToRaw(converter, -0.75, 0);
1070 
1071  checkToRaw(converter, 3.25, 0);
1072  checkToRaw(converter, -3.25, 0);
1073  checkToRaw(converter, 5.75, 0);
1074  checkToRaw(converter, -5.75, 0);
1075 
1076  checkToRaw(converter, (int)0xEAAAA000, 0x2AAAA);
1077  checkToRaw(converter, (int)0x15555000, 0x15555);
1078  checkToRaw(converter, (unsigned int)0x15555000, 0x15555);
1079  checkToRaw(converter, (short)0xA000, 0x3FFFA);
1080  checkToRaw(converter, (short)0x5000, 0x00005);
1081  checkToRaw(converter, (unsigned short)0xA000, 0x0000A);
1082 }
1083 
1084 BOOST_AUTO_TEST_CASE(testUInt18_fractionMinus12) {
1085  FixedPointConverter converter("Variable18minus12unsigned", 18, -12,
1086  false); // 10 bits, -12 fractional bits, not
1087  // signed
1088 
1089  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, 12));
1090  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555) * pow(2, 12));
1091 
1092  // the converter should ignore bits which are not in the spec
1093  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, 12));
1094  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555) * pow(2, 12));
1095 
1096  checkToCooked(converter, 0x2AAAA, (int)0x2AAAA000);
1097  checkToCooked(converter, 0x15555, (int)0x15555000);
1098  checkToCooked(converter, 0x2AAAA, (unsigned int)0x2AAAA000);
1099  checkToCooked(converter, 0x15555, (unsigned int)0x15555000);
1100 
1101  checkToRaw(converter, 0.25, 0);
1102  checkToRaw(converter, -0.25, 0);
1103  checkToRaw(converter, 0.75, 0);
1104  checkToRaw(converter, -0.75, 0);
1105 
1106  checkToRaw(converter, 3.25, 0);
1107  checkToRaw(converter, -3.25, 0);
1108  checkToRaw(converter, 5.75, 0);
1109  checkToRaw(converter, -5.75, 0);
1110 
1111  checkToRaw(converter, (int)0xEAAAA000, 0);
1112  checkToRaw(converter, (int)0x15555000, 0x15555);
1113  checkToRaw(converter, (unsigned int)0x15555000, 0x15555);
1114  checkToRaw(converter, (short)0xA000, 0);
1115  checkToRaw(converter, (short)0x5000, 0x00005);
1116  checkToRaw(converter, (unsigned short)0xA000, 0x0000A);
1117 }
1118 
1119 BOOST_AUTO_TEST_CASE(testInt18_fraction0) {
1120  FixedPointConverter converter("Variable18minus12signed",
1121  18); // 18 bits, 0 fractional bits, signed
1122 
1123  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA));
1124  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555));
1125 
1126  // the converter should ignore bits which are not in the spec
1127  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA));
1128  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555));
1129 
1130  checkToCooked(converter, 0x2AAAA, (int)0xFFFEAAAA);
1131  checkToCooked(converter, 0x15555, (int)0x15555);
1132  checkToCooked(converter, 0x15555, (unsigned int)0x15555);
1133 
1134  checkToCooked(converter, 0x2AAAA, (int64_t)0xFFFFFFFFFFFEAAAA);
1135  checkToCooked(converter, 0x15555, (int64_t)0x15555);
1136  checkToCooked(converter, 0x15555, (uint64_t)0x15555);
1137 
1138  checkToRaw(converter, 0.25, 0);
1139  checkToRaw(converter, -0.25, 0);
1140  checkToRaw(converter, 0.75, 1);
1141  checkToRaw(converter, -0.75, 0x3FFFF);
1142 
1143  checkToRaw(converter, 3.25, 3);
1144  checkToRaw(converter, -3.25, 0x3FFFD);
1145  checkToRaw(converter, 5.75, 6);
1146  checkToRaw(converter, -5.75, 0x3FFFA);
1147 
1148  checkToRaw(converter, (int)0xFFFEAAAA, 0x2AAAA);
1149  checkToRaw(converter, (int)0x00015555, 0x15555);
1150  checkToRaw(converter, (unsigned int)0x00015555, 0x15555);
1151  checkToRaw(converter, (short)0xA000, 0x3A000);
1152  checkToRaw(converter, (short)0x5000, 0x05000);
1153  checkToRaw(converter, (unsigned short)0xA000, 0x0A000);
1154 
1155  checkToRaw(converter, (int64_t)0xFFFFFFFFFFFFA000, 0x3A000);
1156  checkToRaw(converter, (int64_t)0xA000, 0xA000);
1157  checkToRaw(converter, (uint64_t)0xA000, 0x0A000);
1158 }
1159 
1160 BOOST_AUTO_TEST_CASE(testUInt18_fraction0) {
1161  FixedPointConverter converter("Variable18unsigned", 18, 0,
1162  false); // 10 bits, -12 fractional bits, not signed
1163 
1164  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA));
1165  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555));
1166 
1167  // the converter should ignore bits which are not in the spec
1168  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA));
1169  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555));
1170 
1171  checkToCooked(converter, 0x2AAAA, (int)0x2AAAA);
1172  checkToCooked(converter, 0x15555, (int)0x15555);
1173  checkToCooked(converter, 0x2AAAA, (unsigned int)0x2AAAA);
1174  checkToCooked(converter, 0x15555, (unsigned int)0x15555);
1175 
1176  checkToRaw(converter, 0.25, 0);
1177  checkToRaw(converter, -0.25, 0);
1178  checkToRaw(converter, 0.75, 1);
1179  checkToRaw(converter, -0.75, 0x0);
1180 
1181  checkToRaw(converter, 3.25, 3);
1182  checkToRaw(converter, -3.25, 0x0);
1183  checkToRaw(converter, 5.75, 6);
1184  checkToRaw(converter, -5.75, 0x0);
1185 
1186  checkToRaw(converter, (int)0xFFFEAAAA, 0);
1187  checkToRaw(converter, (int)0x00015555, 0x15555);
1188  checkToRaw(converter, (unsigned int)0x00015555, 0x15555);
1189  checkToRaw(converter, (short)0xA000, 0);
1190  checkToRaw(converter, (short)0x5000, 0x05000);
1191  checkToRaw(converter, (unsigned short)0xA000, 0x0A000);
1192 }
1193 
1194 BOOST_AUTO_TEST_CASE(testInt18_fraction7) {
1195  FixedPointConverter converter("Variable18plus7signed", 18,
1196  7); // 18 bits, 7 fractional bits, signed
1197 
1198  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -7));
1199  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -7));
1200 
1201  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -7));
1202  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -7));
1203 
1204  checkToCooked(converter, 0x2AAAA, (int)0xFFFFFD55);
1205  checkToCooked(converter, 0x15555, (int)0x02AB);
1206  checkToCooked(converter, 0x15555, (unsigned int)0x2AB);
1207 
1208  checkToRaw(converter, 0.25, 0x20);
1209  checkToRaw(converter, -0.25, 0x3FFE0);
1210  checkToRaw(converter, 0.75, 0x60);
1211  checkToRaw(converter, -0.75, 0x3FFA0);
1212 
1213  checkToRaw(converter, 3.25, 0x1A0);
1214  checkToRaw(converter, -3.25, 0x3FE60);
1215  checkToRaw(converter, 5.75, 0x2E0);
1216  checkToRaw(converter, -5.75, 0x3FD20);
1217 
1218  checkToRaw(converter, (int)0xFFFFFD55, 0x2AA80);
1219  checkToRaw(converter, (int)0x02AA, 0x15500);
1220  checkToRaw(converter, (unsigned int)0x2AA, 0x15500);
1221  checkToRaw(converter, (short)0xFFAA, 0x3D500);
1222  checkToRaw(converter, (short)0x0055, 0x02A80);
1223  checkToRaw(converter, (unsigned short)0x0055, 0x02A80);
1224 }
1225 
1226 BOOST_AUTO_TEST_CASE(testUInt18_fraction7) {
1227  FixedPointConverter converter("Variable18plus7unsigned", 18, 7,
1228  false); // 10 bits, -12 fractional bits, not signed
1229 
1230  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -7));
1231  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555) * pow(2, -7));
1232 
1233  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -7));
1234  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555) * pow(2, -7));
1235 
1236  checkToCooked(converter, 0x2AAAA, (int)0x0555);
1237  checkToCooked(converter, 0x15555, (int)0x02AB);
1238  checkToCooked(converter, 0x2AAAA, (unsigned int)0x0555);
1239  checkToCooked(converter, 0x15555, (unsigned int)0x02AB);
1240  checkToCooked(converter, 0x2AAAA, (short)0x0555);
1241  checkToCooked(converter, 0x15555, (short)0x02AB);
1242  checkToCooked(converter, 0x2AAAA, (unsigned short)0x0555);
1243  checkToCooked(converter, 0x15555, (unsigned short)0x02AB);
1244 
1245  checkToRaw(converter, 0.25, 0x20);
1246  checkToRaw(converter, -0.25, 0x0);
1247  checkToRaw(converter, 0.75, 0x60);
1248  checkToRaw(converter, -0.75, 0x0);
1249 
1250  checkToRaw(converter, 3.25, 0x1A0);
1251  checkToRaw(converter, -3.25, 0x0);
1252  checkToRaw(converter, 5.75, 0x2E0);
1253  checkToRaw(converter, -5.75, 0x0);
1254 
1255  checkToRaw(converter, (int)0x0555, 0x2AA80);
1256  checkToRaw(converter, (int)0x02AA, 0x15500);
1257  checkToRaw(converter, (unsigned int)0x02AA, 0x15500);
1258  checkToRaw(converter, (short)0xFFAA, 0);
1259  checkToRaw(converter, (short)0x0055, 0x02A80);
1260  checkToRaw(converter, (unsigned short)0x0055, 0x02A80);
1261 }
1262 
1263 BOOST_AUTO_TEST_CASE(testInt18_fraction17) {
1264  FixedPointConverter converter("Variable18plus17signed", 18,
1265  17); // 18 bits, 17 fractional bits, signed
1266 
1267  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -17));
1268  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -17));
1269 
1270  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -17));
1271  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -17));
1272 
1273  checkToRaw(converter, 0.25, 0x8000);
1274  checkToRaw(converter, -0.25, 0x38000);
1275  checkToRaw(converter, 0.75, 0x18000);
1276  checkToRaw(converter, -0.75, 0x28000);
1277 
1278  // these values are out of range
1279  checkToRaw(converter, 3.25, 0x1FFFF);
1280  checkToRaw(converter, -3.25, 0x20000);
1281  checkToRaw(converter, 5.75, 0x1FFFF);
1282  checkToRaw(converter, -5.75, 0x20000);
1283 }
1284 
1285 BOOST_AUTO_TEST_CASE(testUInt18_fraction17) {
1286  FixedPointConverter converter("Variable18plus17unsigned", 18, 17,
1287  false); // 18 bits, 17 fractional bits, not signed
1288 
1289  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -17));
1290  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555) * pow(2, -17));
1291 
1292  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -17));
1293  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555) * pow(2, -17));
1294 
1295  checkToRaw(converter, 0.25, 0x8000);
1296  checkToRaw(converter, -0.25, 0x0);
1297  checkToRaw(converter, 0.75, 0x18000);
1298  checkToRaw(converter, -0.75, 0x0);
1299 
1300  // these values are out of range
1301  checkToRaw(converter, 3.25, 0x3FFFF);
1302  checkToRaw(converter, -3.25, 0x0);
1303  checkToRaw(converter, 5.75, 0x3FFFF);
1304  checkToRaw(converter, -5.75, 0x0);
1305 }
1306 
1307 BOOST_AUTO_TEST_CASE(testInt18_fraction18) {
1308  FixedPointConverter converter("Variable18plus18signed", 18,
1309  18); // 18 bits, 18 fractional bits, signed
1310 
1311  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -18));
1312  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -18));
1313 
1314  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -18));
1315  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -18));
1316 
1317  checkToRaw(converter, 0.25, 0x10000);
1318  checkToRaw(converter, -0.25, 0x30000);
1319  // +- 0.75 are out of range
1320  checkToRaw(converter, 0.75,
1321  0x1FFFF); // the largest possible value (0.5 - 1e-18)
1322  checkToRaw(converter, -0.75, 0x20000); // -0.5, the smallest possible value
1323 
1324  checkToCooked(converter, 0x10000, 0.25);
1325  checkToCooked(converter, 0x30000, -0.25);
1326 
1327  // these values are out of range
1328  checkToRaw(converter, 3.25, 0x1FFFF);
1329  checkToRaw(converter, -3.25, 0x20000);
1330  checkToRaw(converter, 5.75, 0x1FFFF);
1331  checkToRaw(converter, -5.75, 0x20000);
1332 }
1333 
1334 BOOST_AUTO_TEST_CASE(testUInt18_fraction18) {
1335  FixedPointConverter converter("Variable18plus18unsigned", 18, 18,
1336  false); // 10 bits, -12 fractional bits, not signed
1337 
1338  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -18));
1339  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555) * pow(2, -18));
1340 
1341  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -18));
1342  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555) * pow(2, -18));
1343 
1344  checkToRaw(converter, 0.25, 0x10000);
1345  checkToRaw(converter, -0.25, 0x0);
1346  checkToRaw(converter, 0.75, 0x30000);
1347  checkToRaw(converter, -0.75, 0x0);
1348 
1349  checkToCooked(converter, 0x10000, 0.25);
1350  checkToCooked(converter, 0x30000, 0.75);
1351 
1352  checkToRaw(converter, 3.25, 0x3FFFF);
1353  checkToRaw(converter, -3.25, 0x0);
1354  checkToRaw(converter, 5.75, 0x3FFFF);
1355  checkToRaw(converter, -5.75, 0x0);
1356 }
1357 
1358 BOOST_AUTO_TEST_CASE(testInt18_fraction43) {
1359  FixedPointConverter converter("int18_fraction43", 18,
1360  43); // 18 bits, 43 fractional bits, signed
1361 
1362  checkToCooked(converter, 0x2AAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -43));
1363  checkToCooked(converter, 0x15555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -43));
1364 
1365  checkToCooked(converter, 0xAAAAAAAA, SIGNED_HEX_TO_DOUBLE(0xFFFEAAAA) * pow(2, -43));
1366  checkToCooked(converter, 0x55555555, SIGNED_HEX_TO_DOUBLE(0x15555) * pow(2, -43));
1367 
1368  // all out of range
1369  checkToRaw(converter, 0.25, 0x1FFFF);
1370  checkToRaw(converter, -0.25, 0x20000);
1371  checkToRaw(converter, 0.75, 0x1FFFF);
1372  checkToRaw(converter, -0.75, 0x20000);
1373 
1374  checkToRaw(converter, 3.25, 0x1FFFF);
1375  checkToRaw(converter, -3.25, 0x20000);
1376  checkToRaw(converter, 5.75, 0x1FFFF);
1377  checkToRaw(converter, -5.75, 0x20000);
1378 }
1379 
1380 BOOST_AUTO_TEST_CASE(testUInt18_fraction43) {
1381  FixedPointConverter converter("Variable18plus43unsigned", 18, 43,
1382  false); // 18 bits, -43 fractional bits, not signed
1383 
1384  checkToCooked(converter, 0x2AAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -43));
1385  checkToCooked(converter, 0x15555, HEX_TO_DOUBLE(0x15555) * pow(2, -43));
1386 
1387  checkToCooked(converter, 0xAAAAAAAA, HEX_TO_DOUBLE(0x2AAAA) * pow(2, -43));
1388  checkToCooked(converter, 0x55555555, HEX_TO_DOUBLE(0x15555) * pow(2, -43));
1389 
1390  // all out of range
1391  checkToRaw(converter, 0.25, 0x3FFFF);
1392  checkToRaw(converter, -0.25, 0x0);
1393  checkToRaw(converter, 0.75, 0x3FFFF);
1394  checkToRaw(converter, -0.75, 0x0);
1395 
1396  checkToRaw(converter, 3.25, 0x3FFFF);
1397  checkToRaw(converter, -3.25, 0x0);
1398  checkToRaw(converter, 5.75, 0x3FFFF);
1399  checkToRaw(converter, -5.75, 0x0);
1400 }
1401 
1402 BOOST_AUTO_TEST_CASE(testGetters) {
1403  FixedPointConverter defaultConverter("default");
1404  BOOST_CHECK(defaultConverter.getNBits() == 32);
1405  BOOST_CHECK(defaultConverter.getFractionalBits() == 0);
1406  BOOST_CHECK(defaultConverter.isSigned() == true);
1407 
1408  FixedPointConverter customConverter("custom", 13, 7, false);
1409  BOOST_CHECK(customConverter.getNBits() == 13);
1410  BOOST_CHECK(customConverter.getFractionalBits() == 7);
1411  BOOST_CHECK(customConverter.isSigned() == false);
1412 }
1413 
1414 BOOST_AUTO_TEST_CASE(testInt32ToInt32) {
1415  FixedPointConverter converter("int32toInt32"); // default parameters are signed 32 bit
1416 
1417  checkToCooked(converter, 0, (int32_t)0);
1418  checkToCooked(converter, 1, (int32_t)1);
1419  checkToCooked(converter, 0xFFFFFFFF, (int32_t)-1);
1420  checkToCooked(converter, 3, (int32_t)3);
1421  checkToCooked(converter, 0xFFFFFFFD, (int32_t)-3);
1422  checkToCooked(converter, 6, (int32_t)6);
1423  checkToCooked(converter, 0xFFFFFFFA, (int32_t)-6);
1424  checkToCooked(converter, 0xAAAAAAAA, (int32_t)0xAAAAAAAA);
1425  checkToCooked(converter, 0x55555555, (int32_t)0x55555555);
1426 
1427  checkToRaw(converter, 0, 0);
1428  checkToRaw(converter, 1, 1);
1429  checkToRaw(converter, -1, -1);
1430  checkToRaw(converter, 3, 3);
1431  checkToRaw(converter, -3, -3);
1432  checkToRaw(converter, 6, 6);
1433  checkToRaw(converter, -6, -6);
1434 }
1435 
1436 BOOST_AUTO_TEST_CASE(testInt32ToInt16) {
1437  FixedPointConverter converter("int32ToInt16"); // default constructor is signed 32 bit
1438 
1439  checkToCooked(converter, 0, (int16_t)0);
1440  checkToCooked(converter, 1, (int16_t)1);
1441  checkToCooked(converter, 0xFFFFFFFF, (int16_t)-1);
1442  checkToCooked(converter, 3, (int16_t)3);
1443  checkToCooked(converter, 0xFFFFFFFD, (int16_t)-3);
1444  checkToCooked(converter, 6, (int16_t)6);
1445  checkToCooked(converter, 0xFFFFFFFA, (int16_t)-6);
1446  checkToCookedOverflowNeg<int16_t>(converter, 0xAAAAAAAA);
1447  checkToCookedOverflowPos<int16_t>(converter, 0x55555555);
1448 
1449  checkToRaw(converter, (int16_t)0, 0);
1450  checkToRaw(converter, (int16_t)1, 1);
1451  checkToRaw(converter, (int16_t)-1, -1);
1452  checkToRaw(converter, (int16_t)3, 3);
1453  checkToRaw(converter, (int16_t)-3, -3);
1454  checkToRaw(converter, (int16_t)6, 6);
1455  checkToRaw(converter, (int16_t)-6, -6);
1456  checkToRaw(converter, (int16_t)0x7FFF, 0x7FFF);
1457  checkToRaw(converter, (int16_t)0x8000, 0xFFFF8000);
1458  checkToRaw(converter, (int16_t)0xFFFF, 0xFFFFFFFF);
1459 }
1460 
1461 BOOST_AUTO_TEST_CASE(testInt18ToInt32) {
1462  FixedPointConverter converter("int18ToInt32", 18, 0, true);
1463 
1464  checkToCooked(converter, 0, 0);
1465  checkToCooked(converter, 1, 1);
1466  checkToCooked(converter, 0x3FFFF, -1);
1467  checkToCooked(converter, 3, 3);
1468  checkToCooked(converter, 0x3FFFD, -3);
1469  checkToCooked(converter, 6, 6);
1470  checkToCooked(converter, 0x3FFFA, -6);
1471  checkToCooked(converter, 0xFFFFFFFF, (int32_t)0xFFFFFFFF);
1472  checkToCooked(converter, 0xFFFFFFFE, (int32_t)0xFFFFFFFE);
1473  checkToCooked(converter, 0x55555555, (int32_t)0x15555);
1474 
1475  checkToRaw(converter, (int32_t)0, 0);
1476  checkToRaw(converter, (int32_t)1, 1);
1477  checkToRaw(converter, (int32_t)-1, 0x3FFFF);
1478  checkToRaw(converter, (int32_t)3, 3);
1479  checkToRaw(converter, (int32_t)-3, 0x3FFFD);
1480  checkToRaw(converter, (int32_t)6, 6);
1481  checkToRaw(converter, (int32_t)-6, 0x3FFFA);
1482  checkToRaw(converter, (int32_t)0x1FFFF, 0x1FFFF);
1483  checkToRaw(converter, (int32_t)0x20000, 0x1FFFF);
1484  checkToRaw(converter, (int32_t)-1, 0x3FFFF);
1485  checkToRaw(converter, (int32_t)-0x20000, 0x20000);
1486 }
1487 
1488 BOOST_AUTO_TEST_CASE(testIntSignedToUnsigned) {
1489  FixedPointConverter converter("signedToUnsigned"); // default parameters are signed 32 bit
1490 
1491  checkToCooked(converter, 0, (uint32_t)0);
1492  checkToCooked(converter, 1, (uint32_t)1);
1493  checkToCooked(converter, 3, (uint32_t)3);
1494  checkToCooked(converter, 6, (uint32_t)6);
1495  checkToCookedOverflowNeg<uint32_t>(converter, 0xFFFFFFFF);
1496  checkToCookedOverflowNeg<uint16_t>(converter, 0xFFFFFFFA);
1497  checkToCookedOverflowNeg<uint16_t>(converter, 0xAAAAAAAA);
1498  checkToCooked(converter, 0x55555555, (uint32_t)0x55555555);
1499 
1500  checkToRaw(converter, (uint32_t)0, 0);
1501  checkToRaw(converter, (uint32_t)1, 1);
1502  checkToRaw(converter, (uint32_t)3, 3);
1503  checkToRaw(converter, (uint32_t)6, 6);
1504  checkToRaw(converter, (uint32_t)0x7FFFFFFF, 0x7FFFFFFF);
1505  checkToRaw(converter, (uint32_t)0x80000000, 0x7FFFFFFF);
1506  checkToRaw(converter, (uint32_t)0xFFFFFFFF, 0x7FFFFFFF);
1507 }
1508 
1509 BOOST_AUTO_TEST_CASE(testInt17SignedToInt16Unsigned) {
1510  FixedPointConverter converter("int17SignedToInt16Unsigned", 17, 0, true);
1511 
1512  checkToCooked(converter, 0, (uint16_t)0);
1513  checkToCooked(converter, 1, (uint16_t)1);
1514  checkToCookedOverflowNeg<uint16_t>(converter, 0xFFFFFFFF);
1515  checkToCooked(converter, 3, (uint16_t)3);
1516  checkToCooked(converter, 6, (uint16_t)6);
1517  checkToCooked(converter, 0xAAAAAAAA, (uint16_t)0xAAAA);
1518  checkToCookedOverflowNeg<int16_t>(converter, 0x55555555);
1519 
1520  checkToRaw(converter, (uint16_t)0, 0);
1521  checkToRaw(converter, (uint16_t)1, 1);
1522  checkToRaw(converter, (uint16_t)3, 3);
1523  checkToRaw(converter, (uint16_t)6, 6);
1524  checkToRaw(converter, (uint16_t)0x7FFF, 0x7FFF);
1525  checkToRaw(converter, (uint16_t)0x8000, 0x8000);
1526  checkToRaw(converter, (uint16_t)0xFFFF, 0xFFFF);
1527 }
1528 
1529 BOOST_AUTO_TEST_CASE(testInt0unsigned) { // test with 0 significant bits
1530  // (unsigned, no fractional bits)
1531  FixedPointConverter converter("int0unsigned", 0, 0, false);
1532 
1533  checkToCooked(converter, 0, 0);
1534  checkToCooked(converter, 1, 0);
1535  checkToCooked(converter, 0x0000FFFF, 0);
1536  checkToCooked(converter, 0xFFFF0000, 0);
1537  checkToCooked(converter, 0xFFFFFFFF, 0);
1538 
1539  checkToRaw(converter, 0, 0);
1540  checkToRaw(converter, 1, 0);
1541  checkToRaw(converter, 0xFFFF, 0);
1542  checkToRaw(converter, -1, 0);
1543 }
1544 
1545 BOOST_AUTO_TEST_CASE(testInt0signed) { // test with 0 significant bits (signed,
1546  // no fractional bits)
1547  FixedPointConverter converter("int0signed", 0, 0, true);
1548 
1549  checkToCooked(converter, 0, 0);
1550  checkToCooked(converter, 1, 0);
1551  checkToCooked(converter, 0x0000FFFF, 0);
1552  checkToCooked(converter, 0xFFFF0000, 0);
1553  checkToCooked(converter, 0xFFFFFFFF, 0);
1554 
1555  checkToRaw(converter, 0, 0);
1556  checkToRaw(converter, 1, 0);
1557  checkToRaw(converter, 0xFFFF, 0);
1558  checkToRaw(converter, -1, 0);
1559 }
1560 
1561 BOOST_AUTO_TEST_CASE(testInt0unsignedFractional) { // test with 0 significant bits (unsigned,
1562  // with fractional bits)
1563  FixedPointConverter converter("int0unsignedFractional", 0, 5, false);
1564 
1565  checkToCooked(converter, 0, 0);
1566  checkToCooked(converter, 1, 0);
1567  checkToCooked(converter, 0x0000FFFF, 0);
1568  checkToCooked(converter, 0xFFFF0000, 0);
1569  checkToCooked(converter, 0xFFFFFFFF, 0);
1570 
1571  checkToRaw(converter, 0, 0);
1572  checkToRaw(converter, 1, 0);
1573  checkToRaw(converter, 0xFFFF, 0);
1574  checkToRaw(converter, -1, 0);
1575 }
1576 
1577 BOOST_AUTO_TEST_CASE(testInt0signedFractional) { // test with 0 significant bits (signed, with
1578  // negative fractional bits)
1579  FixedPointConverter converter("int0signedFractional", 0, -5, true);
1580 
1581  checkToCooked(converter, 0, 0);
1582  checkToCooked(converter, 1, 0);
1583  checkToCooked(converter, 0x0000FFFF, 0);
1584  checkToCooked(converter, 0xFFFF0000, 0);
1585  checkToCooked(converter, 0xFFFFFFFF, 0);
1586 
1587  checkToRaw(converter, 0, 0);
1588  checkToRaw(converter, 1, 0);
1589  checkToRaw(converter, 0xFFFF, 0);
1590  checkToRaw(converter, -1, 0);
1591 }
1592 
1593 BOOST_AUTO_TEST_CASE(testDynamicRangePos) {
1594  FixedPointConverter converter("dynamicRangePos", 16, 1021 - 16, false);
1595 
1596  checkToCooked(converter, 0, 0.);
1597  checkToCooked(converter, 1, pow(2., -(1021 - 16)));
1598  checkToCooked(converter, 0xFFFF, 65535. * pow(2., -(1021 - 16)));
1599 
1600  // check that our comparison values are not already exceeding the dynamic
1601  // range
1602  BOOST_CHECK(!std::isinf(65535. * pow(2., -(1021 - 16))));
1603 }
1604 
1605 BOOST_AUTO_TEST_CASE(testDynamicRangeNeg) {
1606  FixedPointConverter converter("dynamicRangeNeg", 16, -1024 + 16, false);
1607 
1608  checkToCooked(converter, 0, 0.);
1609  checkToCooked(converter, 1, pow(2., 1024 - 16));
1610  checkToCooked(converter, 0xFFFF, 65535. * pow(2., 1024 - 16));
1611 
1612  // check that our comparison values are not already exceeding the dynamic
1613  // range
1614  BOOST_CHECK(pow(2., -(1024 - 16)) > 0.);
1615 }
1616 
1617 BOOST_AUTO_TEST_CASE(testBoolean0) {
1618  FixedPointConverter converter("Variable32signed"); // default parameters are signed 32 bit
1619 
1620  checkToCooked(converter, 0x00000000, Boolean(false));
1621 }
1622 
1624  FixedPointConverter converter("Variable32signed"); // default parameters are signed 32 bit
1625 
1626  Void output = converter.scalarToCooked<Void>(23);
1627  (void)output;
1628 }
1629 
1630 BOOST_AUTO_TEST_SUITE_END()
checkToCookedOverflowPos
void checkToCookedOverflowPos(FixedPointConverter const &converter, uint32_t input)
Definition: testFixedPointConverter.cpp:76
ChimeraTK::Void
Wrapper Class for void.
Definition: SupportedUserTypes.h:71
ChimeraTK::FixedPointConverter::getFractionalBits
int getFractionalBits() const
Read back the fractional bits the converter is using.
Definition: FixedPointConverter.h:96
checkToCooked
void checkToCooked(FixedPointConverter const &converter, uint32_t input, T expectedValue)
Definition: testFixedPointConverter.cpp:81
SIGNED_HEX_TO_INT64
#define SIGNED_HEX_TO_INT64(INPUT)
Definition: testFixedPointConverter.cpp:26
SIGNED_HEX_TO_DOUBLE
#define SIGNED_HEX_TO_DOUBLE(INPUT)
Definition: testFixedPointConverter.cpp:25
FixedPointConverter.h
ChimeraTK::FixedPointConverter::scalarToCooked
UserType scalarToCooked(int32_t const &raw) const
Inefficient convenience function for converting a single value to cooked.
Definition: FixedPointConverter.h:86
DEF_TYPENAME
#define DEF_TYPENAME(name)
Definition: testFixedPointConverter.cpp:52
checkToRaw
void checkToRaw(FixedPointConverter const &converter, T input, uint32_t expectedValue)
Definition: testFixedPointConverter.cpp:96
HEX_TO_DOUBLE
#define HEX_TO_DOUBLE(INPUT)
Definition: testFixedPointConverter.cpp:24
typeName
const char * typeName(void)
Definition: testFixedPointConverter.cpp:49
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testConstructor)
Definition: testFixedPointConverter.cpp:112
ChimeraTK::FixedPointConverter::isSigned
bool isSigned() const
Read back wether the conversion is using signed values.
Definition: FixedPointConverter.h:99
checkToCookedOverflowNeg
void checkToCookedOverflowNeg(FixedPointConverter const &converter, uint32_t input)
Definition: testFixedPointConverter.cpp:71
ChimeraTK::FixedPointConverter
The fixed point converter provides conversion functions between a user type and up to 32 bit fixed po...
Definition: FixedPointConverter.h:28
ChimeraTK::FixedPointConverter::getNBits
unsigned int getNBits() const
Read back the number of bits the converter is using.
Definition: FixedPointConverter.h:93
Exception.h
ChimeraTK::FixedPointConverter::toRaw
uint32_t toRaw(UserType cookedValue) const
Conversion function from type T to fixed point.
Definition: FixedPointConverter.h:336
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::Boolean
Wrapper Class to avoid vector<bool> problems.
Definition: SupportedUserTypes.h:21
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51