ChimeraTK-DeviceAccess  03.18.00
testDevice.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 DeviceTest
6 #include <boost/test/unit_test.hpp>
7 using namespace boost::unit_test_framework;
8 
9 #include "BackendFactory.h"
10 #include "Device.h"
11 #include "DeviceBackend.h"
12 #include "DummyRegisterAccessor.h"
13 #include "Exception.h"
14 
15 #include <cstring>
16 
17 namespace ChimeraTK {
18  using namespace ChimeraTK;
19 }
20 
22  public:
23  boost::shared_ptr<ChimeraTK::DeviceBackend> getBackend() { return _deviceBackendPointer; }
24 };
25 
26 BOOST_AUTO_TEST_SUITE(DeviceTestSuite)
27 
28 BOOST_AUTO_TEST_CASE(testConvenienceReadWrite) {
29  ChimeraTK::setDMapFilePath("dummies.dmap");
31  device.open("DUMMYD2");
32  boost::shared_ptr<ChimeraTK::DummyBackend> backend = boost::dynamic_pointer_cast<ChimeraTK::DummyBackend>(
33  ChimeraTK::BackendFactory::getInstance().createBackend("DUMMYD2"));
34 
35  ChimeraTK::DummyRegisterAccessor<int32_t> wordStatus(backend.get(), "APP0", "WORD_STATUS");
36  ChimeraTK::DummyRegisterAccessor<int32_t> module0(backend.get(), "APP0", "MODULE0");
37 
38  int32_t data;
39  std::vector<int32_t> dataVector;
40 
41  wordStatus = 0x444d4d59;
42  data = device.read<int32_t>("APP0.WORD_STATUS");
43  BOOST_CHECK(data == 0x444d4d59);
44 
45  wordStatus = -42;
46  data = device.read<int32_t>("APP0.WORD_STATUS");
47  BOOST_CHECK(data == -42);
48 
49  module0[0] = 120;
50  module0[1] = 0xDEADBEEF;
51 
52  data = device.read<int32_t>("APP0/MODULE0");
53  BOOST_CHECK(data == 120);
54 
55  dataVector = device.read<int32_t>("APP0/MODULE0", 2, 0);
56  BOOST_CHECK(dataVector.size() == 2);
57  BOOST_CHECK(dataVector[0] == 120);
58  BOOST_CHECK(dataVector[1] == static_cast<signed>(0xDEADBEEF));
59 
60  module0[0] = 66;
61  module0[1] = -33333;
62 
63  dataVector = device.read<int32_t>("APP0/MODULE0", 1, 0);
64  BOOST_CHECK(dataVector.size() == 1);
65  BOOST_CHECK(dataVector[0] == 66);
66 
67  dataVector = device.read<int32_t>("APP0/MODULE0", 1, 1);
68  BOOST_CHECK(dataVector.size() == 1);
69  BOOST_CHECK(dataVector[0] == -33333);
70 
71  BOOST_CHECK_THROW([[maybe_unused]] auto x = device.read<int32_t>("APP0/DOESNT_EXIST"), ChimeraTK::logic_error);
72  BOOST_CHECK_THROW(
73  [[maybe_unused]] auto x = device.read<int32_t>("DOESNT_EXIST/AT_ALL", 1, 0), ChimeraTK::logic_error);
74 }
75 
76 BOOST_AUTO_TEST_CASE(testDeviceCreation) {
77  std::string initialDmapFilePath = ChimeraTK::BackendFactory::getInstance().getDMapFilePath();
78  ChimeraTK::BackendFactory::getInstance().setDMapFilePath("dMapDir/testRelativePaths.dmap");
79 
80  ChimeraTK::Device device1;
81  BOOST_CHECK(device1.isOpened() == false);
82  device1.open("DUMMYD0");
83  BOOST_CHECK(device1.isOpened() == true);
84  BOOST_CHECK_NO_THROW(device1.open("DUMMYD0"));
85  { // scope to have a device which goes out of scope
86  ChimeraTK::Device device1a;
87  // open the same backend than device1
88  device1a.open("DUMMYD0");
89  BOOST_CHECK(device1a.isOpened() == true);
90  }
91  // check that device1 has not been closed by device 1a going out of scope
92  BOOST_CHECK(device1.isOpened() == true);
93 
94  ChimeraTK::Device device1b;
95  // open the same backend than device1
96  device1b.open("DUMMYD0");
97  // open another backend with the same device //ugly, might be deprecated soon
98  device1b.open("DUMMYD0");
99  // check that device1 has not been closed by device 1b being reassigned
100  BOOST_CHECK(device1.isOpened() == true);
101 
102  ChimeraTK::Device device2;
103  BOOST_CHECK(device2.isOpened() == false);
104  device2.open("DUMMYD1");
105  BOOST_CHECK(device2.isOpened() == true);
106  BOOST_CHECK_NO_THROW(device2.open("DUMMYD1"));
107  BOOST_CHECK(device2.isOpened() == true);
108 
109  ChimeraTK::Device device3;
110  BOOST_CHECK(device3.isOpened() == false);
111  BOOST_CHECK_NO_THROW(device3.open("DUMMYD0"));
112  BOOST_CHECK(device3.isOpened() == true);
113  ChimeraTK::Device device4;
114  BOOST_CHECK(device4.isOpened() == false);
115  BOOST_CHECK_NO_THROW(device4.open("DUMMYD1"));
116  BOOST_CHECK(device4.isOpened() == true);
117 
118  // check if opening without alias name fails
119  TestableDevice device5;
120  BOOST_CHECK(device5.isOpened() == false);
121  BOOST_CHECK_THROW(device5.open(), ChimeraTK::logic_error);
122  BOOST_CHECK(device5.isOpened() == false);
123  BOOST_CHECK_THROW(device5.open(), ChimeraTK::logic_error);
124  BOOST_CHECK(device5.isOpened() == false);
125 
126  // check if opening device with different backend keeps old backend open.
127  BOOST_CHECK_NO_THROW(device5.open("DUMMYD0"));
128  BOOST_CHECK(device5.isOpened() == true);
129  auto backend5 = device5.getBackend();
130  BOOST_CHECK_NO_THROW(device5.open("DUMMYD1"));
131  BOOST_CHECK(backend5->isOpen()); // backend5 is still the current backend of device5
132  BOOST_CHECK(device5.isOpened() == true);
133 
134  // check closing and opening again
135  backend5 = device5.getBackend();
136  BOOST_CHECK(backend5->isOpen());
137  BOOST_CHECK(device5.isOpened() == true);
138  device5.close();
139  BOOST_CHECK(device5.isOpened() == false);
140  BOOST_CHECK(!backend5->isOpen());
141  device5.open();
142  BOOST_CHECK(device5.isOpened() == true);
143  BOOST_CHECK(backend5->isOpen());
144 
145  // Now that we are done with the tests, move the factory to the state it was
146  // in before we started
148 }
149 
150 #if 0
151 BOOST_AUTO_TEST_CASE(testDeviceInfo) {
153  device.open("DUMMYD3");
154  std::string deviceInfo = device.readDeviceInfo();
155  std::cout << deviceInfo << std::endl;
156  BOOST_CHECK(deviceInfo.substr(0, 31) == "DummyBackend with mapping file ");
157 }
158 #endif
159 
160 BOOST_AUTO_TEST_CASE(testIsFunctional) {
162  // a disconnected device is not functional
163  BOOST_CHECK(d.isFunctional() == false);
164 
165  d.open("DUMMYD1");
166  BOOST_CHECK(d.isFunctional() == true);
167 
168  d.close();
169  BOOST_CHECK(d.isFunctional() == false);
170 }
171 
172 BOOST_AUTO_TEST_SUITE_END()
DeviceBackend.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testConvenienceReadWrite)
Definition: testDevice.cpp:28
device
ctk::Device device
Definition: testExceptionDummyDevice.cc:18
ChimeraTK::DummyRegisterAccessor< int32_t >
ChimeraTK::Device::close
void close()
Close the device.
Definition: Device.cc:66
ChimeraTK::BackendFactory::getInstance
static BackendFactory & getInstance()
Static function to get an instance of factory.
Definition: BackendFactory.cc:191
ChimeraTK::Device::read
UserType read(const RegisterPath &registerPathName, const AccessModeFlags &flags=AccessModeFlags({})) const
Inefficient convenience function to read a single-word register without obtaining an accessor.
Definition: Device.h:293
ChimeraTK::BackendFactory::setDMapFilePath
void setDMapFilePath(std::string dMapFilePath)
This function sets the _DMapFilePath.
Definition: BackendFactory.cc:161
ChimeraTK::Device::readDeviceInfo
std::string readDeviceInfo() const
Return a device information string.
Definition: Device.cc:36
ChimeraTK::BackendFactory::getDMapFilePath
std::string getDMapFilePath()
Returns the _DMapFilePath.
Definition: BackendFactory.cc:167
Device.h
TestableDevice
Definition: testDevice.cpp:21
ChimeraTK::Device
Class allows to read/write registers from device.
Definition: Device.h:39
TestableDevice::getBackend
boost::shared_ptr< ChimeraTK::DeviceBackend > getBackend()
Definition: testDevice.cpp:23
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
DummyRegisterAccessor.h
BackendFactory.h
ChimeraTK::Device::isOpened
bool isOpened() const
Check if the device is currently opened.
Definition: Device.cc:73
Exception.h
ChimeraTK::setDMapFilePath
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
Definition: Utilities.cpp:327
ChimeraTK::Device::isFunctional
bool isFunctional() const
Return wether a device is working as intended, usually this means it is opened and does not have any ...
Definition: Device.cc:82
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51