ChimeraTK-DeviceAccess  03.18.00
testLMapMathPlugin.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 LMapMathPluginTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 #include "Device.h"
10 
11 using namespace ChimeraTK;
12 
13 BOOST_AUTO_TEST_SUITE(LMapMathPluginTestSuite)
14 
15 /**********************************************************************************************************************/
16 
17 BOOST_AUTO_TEST_CASE(testSimpleReadWriteScalar) {
19  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
20 
21  auto accTarget = device.getScalarRegisterAccessor<int>("SimpleScalar");
22  auto accMathRead = device.getScalarRegisterAccessor<double>("SimpleScalarRead");
23  auto accMathWrite = device.getScalarRegisterAccessor<double>("SimpleScalarWrite");
24 
25  accTarget = 45;
26  accTarget.write();
27  accMathRead.read();
28  BOOST_CHECK_CLOSE(double(accMathRead), 45. / 7. + 13., 0.00001);
29 
30  accTarget = -666;
31  accTarget.write();
32  accMathRead.read();
33  BOOST_CHECK_CLOSE(double(accMathRead), -666. / 7. + 13., 0.00001);
34 
35  accMathWrite = 77.;
36  accMathWrite.write();
37  accTarget.read();
38  BOOST_CHECK_EQUAL(int(accTarget), 24); // 77/7 + 13
39 
40  accMathWrite = -140.;
41  accMathWrite.write();
42  accTarget.read();
43  BOOST_CHECK_EQUAL(int(accTarget), -7); // -140/7 + 13
44 }
45 
46 /**********************************************************************************************************************/
47 
48 BOOST_AUTO_TEST_CASE(testReturnedReadWriteScalar) {
50  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
51 
52  auto accTarget = device.getScalarRegisterAccessor<int>("SimpleScalar");
53  // ReturnedScalarReadWrite uses a return statement, but is otherwise identical to the "simple" version
54  auto accMathRead = device.getScalarRegisterAccessor<double>("ReturnedScalarRead");
55  auto accMathWrite = device.getScalarRegisterAccessor<double>("ReturnedScalarWrite");
56 
57  accTarget = 45;
58  accTarget.write();
59  accMathRead.read();
60  BOOST_CHECK_CLOSE(double(accMathRead), 45. / 7. + 13., 0.00001);
61 
62  accTarget = -666;
63  accTarget.write();
64  accMathRead.read();
65  BOOST_CHECK_CLOSE(double(accMathRead), -666. / 7. + 13., 0.00001);
66 
67  accMathWrite = 77.;
68  accMathWrite.write();
69  accTarget.read();
70  BOOST_CHECK_EQUAL(int(accTarget), 24); // 77/7 + 13
71 
72  accMathWrite = -140.;
73  accMathWrite.write();
74  accTarget.read();
75  BOOST_CHECK_EQUAL(int(accTarget), -7); // -140/7 + 13
76 }
77 
78 /**********************************************************************************************************************/
79 
80 BOOST_AUTO_TEST_CASE(testReadWriteArray) {
82  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
83 
84  auto accTarget = device.getOneDRegisterAccessor<int>("SimpleArray");
85  auto accMathRead = device.getOneDRegisterAccessor<double>("ArrayRead");
86  auto accMathWrite = device.getOneDRegisterAccessor<double>("ArrayWrite");
87  BOOST_CHECK_EQUAL(accMathRead.getNElements(), 6);
88  BOOST_CHECK_EQUAL(accMathWrite.getNElements(), 6);
89 
90  accTarget = {11, 22, 33, 44, 55, 66};
91  accTarget.write();
92  accMathRead.read();
93  for(size_t i = 0; i < 6; ++i) BOOST_CHECK_CLOSE(double(accMathRead[i]), accTarget[i] / 7. + 13., 0.00001);
94 
95  accTarget = {-120, 123456, -18, 9999, -999999999, 0};
96  accTarget.write();
97  accMathRead.read();
98  for(size_t i = 0; i < 6; ++i) BOOST_CHECK_CLOSE(double(accMathRead[i]), accTarget[i] / 7. + 13., 0.00001);
99 
100  accMathWrite = {-120, 123456, -18, 9999, -999999999, 0};
101  accMathWrite.write();
102  accTarget.read();
103  for(size_t i = 0; i < 6; ++i) BOOST_CHECK_EQUAL(double(accTarget[i]), std::round(accMathWrite[i] / 7. + 13.));
104 
105  accMathWrite = {0, 1, 2, 3, 4, 5};
106  accMathWrite.write();
107  accTarget.read();
108  for(size_t i = 0; i < 6; ++i) BOOST_CHECK_EQUAL(double(accTarget[i]), std::round(accMathWrite[i] / 7. + 13.));
109 }
110 
111 /**********************************************************************************************************************/
112 
113 BOOST_AUTO_TEST_CASE(testParameters) {
115  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
116 
117  auto accTarget = device.getScalarRegisterAccessor<int>("SimpleScalar");
118  auto scalarPar = device.getScalarRegisterAccessor<int>("ScalarParameter");
119  auto arrayPar = device.getOneDRegisterAccessor<int>("SimpleArray");
120  auto accMathRead = device.getScalarRegisterAccessor<double>("ScalarWithParametersRead");
121  auto accMathWrite = device.getScalarRegisterAccessor<double>("ScalarWithParametersWrite");
122 
123  accTarget = 42;
124  accTarget.write();
125  scalarPar = 6;
126  scalarPar.write();
127  arrayPar = {2, 3, 4, 5, 6, 7};
128  arrayPar.write();
129  accMathRead.read();
130  BOOST_CHECK_CLOSE(double(accMathRead), 42. / 6. + (2 + 3 + 4 + 5 + 6 + 7), 0.00001);
131 
132  scalarPar = 7;
133  scalarPar.write();
134  arrayPar = {1, -1, 1, -1, 1, -1};
135  arrayPar.write();
136  accMathRead.read();
137  BOOST_CHECK_CLOSE(double(accMathRead), 42. / 7., 0.00001);
138 
139  accMathWrite = 56;
140  accMathWrite.write();
141  accTarget.read();
142  BOOST_CHECK_EQUAL(int(accTarget), 8);
143 
144  scalarPar = 4;
145  scalarPar.write();
146  accMathWrite.write();
147  accTarget.read();
148  BOOST_CHECK_EQUAL(int(accTarget), 14);
149 }
150 
151 /**********************************************************************************************************************/
152 
153 BOOST_AUTO_TEST_CASE(testExceptions) {
154  // missing parameter "formula"
156 
157  // open device with map file which does not parse
158  BOOST_CHECK_THROW(device.open("(logicalNameMap?map=mathPlugin-broken.xlmap)"), ChimeraTK::logic_error);
159 
160  // open device with map file which parses but contains broken formula
161  // exception might be thrown in open or only when register requested
162  BOOST_CHECK_THROW(device.open("(logicalNameMap?map=mathPlugin-broken2.xlmap)");
163  std::ignore = device.getOneDRegisterAccessor<double>("BrokenFormula"), ChimeraTK::logic_error);
164 
165  // open device with map file which parses and all contained formulas compile
166  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
167 
168  auto acc1 = device.getOneDRegisterAccessor<double>("WrongReturnSizeInArray");
169  BOOST_CHECK_THROW(acc1.read(), ChimeraTK::logic_error);
170  BOOST_CHECK_THROW(acc1.write(), ChimeraTK::logic_error);
171 
172  auto acc2 = device.getOneDRegisterAccessor<double>("ReturnScalarDespiteArray");
173  BOOST_CHECK_THROW(acc2.read(), ChimeraTK::logic_error);
174  BOOST_CHECK_THROW(acc2.write(), ChimeraTK::logic_error);
175 
176  auto acc3 = device.getOneDRegisterAccessor<double>("ReturnString");
177  BOOST_CHECK_THROW(acc3.read(), ChimeraTK::logic_error);
178  BOOST_CHECK_THROW(acc3.write(), ChimeraTK::logic_error);
179 
180  auto acc4 = device.getOneDRegisterAccessor<double>("ReturnMultipleValues");
181  BOOST_CHECK_THROW(acc4.read(), ChimeraTK::logic_error);
182  BOOST_CHECK_THROW(acc4.write(), ChimeraTK::logic_error);
183 }
184 
185 /**********************************************************************************************************************/
186 
187 BOOST_AUTO_TEST_CASE(testCDATAFormula) {
189 
190  // open device with map file which parses
191  device.open("(logicalNameMap?map=mathPlugin.xlmap)");
192 
193  auto acc = device.getScalarRegisterAccessor<double>("FormulaWithCdata");
194  acc.read();
195  BOOST_TEST(int(acc) == 24);
196 }
197 
198 /**********************************************************************************************************************/
199 
200 BOOST_AUTO_TEST_SUITE_END()
device
ctk::Device device
Definition: testExceptionDummyDevice.cc:18
ChimeraTK::Device::getOneDRegisterAccessor
OneDRegisterAccessor< UserType > getOneDRegisterAccessor(const RegisterPath &registerPathName, size_t numberOfWords=0, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a OneDRegisterAccessor object for the given register.
Definition: Device.h:273
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testSimpleReadWriteScalar)
Definition: testLMapMathPlugin.cpp:17
Device.h
ChimeraTK::Device
Class allows to read/write registers from device.
Definition: Device.h:39
ChimeraTK::Device::open
void open(std::string const &aliasName)
Open a device by the given alias name from the DMAP file.
Definition: Device.cc:58
ChimeraTK::Device::getScalarRegisterAccessor
ScalarRegisterAccessor< UserType > getScalarRegisterAccessor(const RegisterPath &registerPathName, size_t wordOffsetInRegister=0, const AccessModeFlags &flags=AccessModeFlags({})) const
Get a ScalarRegisterObject object for the given register.
Definition: Device.h:263
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51