ChimeraTK-DeviceAccess  03.18.00
testIEEE754_SingleConverter.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 IEEE754_SingleConverterTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 #include "Exception.h"
11 using namespace ChimeraTK;
12 
13 #include <float.h> // for float limits
14 
15 BOOST_AUTO_TEST_CASE(test_toCooked_3_25) {
16  IEEE754_SingleConverter converter;
17 
18  float testValue = 3.25;
19  void* warningAvoider = &testValue;
20  int32_t rawValue = *(reinterpret_cast<int32_t*>(warningAvoider));
21 
22  BOOST_CHECK_CLOSE(converter.scalarToCooked<float>(rawValue), 3.25, 0.0001);
23  BOOST_CHECK_CLOSE(converter.scalarToCooked<double>(rawValue), 3.25, 0.0001);
24  BOOST_CHECK_EQUAL(converter.scalarToCooked<int8_t>(rawValue), 3);
25  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint8_t>(rawValue), 3);
26  BOOST_CHECK_EQUAL(converter.scalarToCooked<int16_t>(rawValue), 3);
27  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint16_t>(rawValue), 3);
28  BOOST_CHECK_EQUAL(converter.scalarToCooked<int32_t>(rawValue), 3);
29  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint32_t>(rawValue), 3);
30  BOOST_CHECK_EQUAL(converter.scalarToCooked<int64_t>(rawValue), 3);
31  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint64_t>(rawValue), 3);
32  BOOST_CHECK_EQUAL(converter.scalarToCooked<std::string>(rawValue), std::to_string(testValue));
33  BOOST_CHECK_EQUAL(converter.scalarToCooked<Boolean>(rawValue), true);
34 }
35 
36 BOOST_AUTO_TEST_CASE(test_toCooked_60k) {
37  IEEE754_SingleConverter converter;
38 
39  // tests two functionalities: range check of the target (value too large for
40  // int8 and int16)
41  float testValue = 60000.7;
42  void* warningAvoider = &testValue;
43  int32_t rawValue = *(reinterpret_cast<int32_t*>(warningAvoider));
44 
45  BOOST_CHECK_CLOSE(converter.scalarToCooked<float>(rawValue), 60000.7, 0.0001);
46  BOOST_CHECK_CLOSE(converter.scalarToCooked<double>(rawValue), 60000.7, 0.0001);
47  BOOST_CHECK_THROW(converter.scalarToCooked<int8_t>(rawValue), boost::numeric::positive_overflow);
48  BOOST_CHECK_THROW(converter.scalarToCooked<uint8_t>(rawValue), boost::numeric::positive_overflow);
49  BOOST_CHECK_THROW(converter.scalarToCooked<int16_t>(rawValue), boost::numeric::positive_overflow); // +- 32k
50  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint16_t>(rawValue), 60001); // unsigned 16 bit is up to 65k
51  BOOST_CHECK_EQUAL(converter.scalarToCooked<int32_t>(rawValue), 60001);
52  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint32_t>(rawValue), 60001);
53  BOOST_CHECK_EQUAL(converter.scalarToCooked<int64_t>(rawValue), 60001);
54  BOOST_CHECK_EQUAL(converter.scalarToCooked<uint64_t>(rawValue), 60001);
55  BOOST_CHECK_EQUAL(converter.scalarToCooked<std::string>(rawValue), std::to_string(testValue));
56  BOOST_CHECK_EQUAL(converter.scalarToCooked<Boolean>(rawValue), true);
57 }
58 
59 BOOST_AUTO_TEST_CASE(test_toCooked_minus240) {
60  IEEE754_SingleConverter converter;
61 
62  float testValue = -240.6;
63  void* warningAvoider = &testValue;
64  int32_t rawValue = *(reinterpret_cast<int32_t*>(warningAvoider));
65 
66  BOOST_CHECK_CLOSE(converter.scalarToCooked<float>(rawValue), -240.6, 0.0001);
67  BOOST_CHECK_CLOSE(converter.scalarToCooked<double>(rawValue), -240.6, 0.0001);
68  BOOST_CHECK_THROW(converter.scalarToCooked<int8_t>(rawValue), boost::numeric::negative_overflow);
69  BOOST_CHECK_THROW(converter.scalarToCooked<uint8_t>(rawValue), boost::numeric::negative_overflow);
70  BOOST_CHECK_EQUAL(converter.scalarToCooked<int16_t>(rawValue), -241);
71  BOOST_CHECK_THROW(converter.scalarToCooked<uint16_t>(rawValue), boost::numeric::negative_overflow);
72  BOOST_CHECK_EQUAL(converter.scalarToCooked<int32_t>(rawValue), -241);
73  BOOST_CHECK_THROW(converter.scalarToCooked<uint32_t>(rawValue), boost::numeric::negative_overflow);
74  BOOST_CHECK_EQUAL(converter.scalarToCooked<int64_t>(rawValue), -241);
75  BOOST_CHECK_THROW(converter.scalarToCooked<uint64_t>(rawValue), boost::numeric::negative_overflow);
76  BOOST_CHECK_EQUAL(converter.scalarToCooked<std::string>(rawValue), std::to_string(testValue));
77  BOOST_CHECK_EQUAL(converter.scalarToCooked<Boolean>(rawValue), true);
78 }
79 
80 void checkAsRaw(int32_t rawValue, float expectedValue) {
81  void* warningAvoider = &rawValue;
82  float testValue = *(reinterpret_cast<float*>(warningAvoider));
83 
84  BOOST_CHECK_CLOSE(testValue, expectedValue, 0.0001);
85 }
86 
87 BOOST_AUTO_TEST_CASE(test_from_3_25) {
88  IEEE754_SingleConverter converter;
89 
90  checkAsRaw(converter.toRaw(float(3.25)), 3.25);
91  checkAsRaw(converter.toRaw(double(-3.25)), -3.25);
92  checkAsRaw(converter.toRaw(int8_t(-3)), -3);
93  checkAsRaw(converter.toRaw(uint8_t(3)), 3);
94  checkAsRaw(converter.toRaw(int16_t(-3)), -3);
95  checkAsRaw(converter.toRaw(uint16_t(3)), 3);
96  checkAsRaw(converter.toRaw(int32_t(3)), 3);
97  checkAsRaw(converter.toRaw(uint32_t(3)), 3);
98  checkAsRaw(converter.toRaw(int64_t(3)), 3);
99  checkAsRaw(converter.toRaw(uint64_t(3)), 3);
100  checkAsRaw(converter.toRaw(std::string("3.25")), 3.25);
101  checkAsRaw(converter.toRaw(Boolean("3.25")), true);
102 
103  // corner cases
104  BOOST_CHECK_THROW(std::ignore = converter.toRaw(std::string("notAFloat")), ChimeraTK::logic_error);
105 
106  double tooLarge = DBL_MAX + 1;
107  double tooSmall = -(DBL_MAX);
108 
109  // converter should limit, not throw
110  checkAsRaw(converter.toRaw(tooLarge), FLT_MAX);
111  checkAsRaw(converter.toRaw(tooSmall), -FLT_MAX);
112 }
113 
114 BOOST_AUTO_TEST_CASE(test_toCooked_00) {
115  IEEE754_SingleConverter converter;
116 
117  // tests if Boolean turns 0.0 into false
118  float testValue = 0.0;
119  void* warningAvoider = &testValue;
120  int32_t rawValue = *(reinterpret_cast<int32_t*>(warningAvoider));
121 
122  BOOST_CHECK_EQUAL(converter.scalarToCooked<Boolean>(rawValue), false);
123 }
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_toCooked_3_25)
Definition: testIEEE754_SingleConverter.cpp:15
IEEE754_SingleConverter.h
ChimeraTK::IEEE754_SingleConverter::scalarToCooked
CookedType scalarToCooked(int32_t const &raw) const
Inefficient convenience function for converting a single value to cooked.
Definition: IEEE754_SingleConverter.h:69
Exception.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::IEEE754_SingleConverter::toRaw
uint32_t toRaw(CookedType cookedValue) const
Definition: IEEE754_SingleConverter.h:100
ChimeraTK::to_string
std::string to_string(Boolean &value)
Definition: SupportedUserTypes.h:59
ChimeraTK::IEEE754_SingleConverter
Needs to have the same interface as FixedPointConverter, except for the constructor.
Definition: IEEE754_SingleConverter.h:46
checkAsRaw
void checkAsRaw(int32_t rawValue, float expectedValue)
Definition: testIEEE754_SingleConverter.cpp:80
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