ChimeraTK-DeviceAccess  03.18.00
testDeviceInfoMap.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 
7 #include "DeviceInfoMap.h"
8 #include "Exception.h"
9 #include "helperFunctions.h"
10 
11 using namespace boost::unit_test_framework;
12 namespace ChimeraTK {
13  using namespace ChimeraTK;
14 }
15 
17  public:
18  void testInsertElement();
19  void testGetDeviceInfo();
20  void testCheckForDuplicateElements();
21  void testGetDeviceFileAndMapFileName();
22  void testErrorElemErrTypeStreamOperator();
23  void testDeviceInfoStreamOperator();
24  void testDeviceInfoMapStreamOperator();
25  void testErrorElemStreamOperator();
26  void testErrorListStreamOperator();
27 };
28 
29 class DeviceInfoMapTestSuite : public test_suite {
30  public:
31  DeviceInfoMapTestSuite() : test_suite("DeviceInfoMap test suite") {
32  boost::shared_ptr<DeviceInfoMapTest> deviceInfoMapTest(new DeviceInfoMapTest());
33 
34  test_case* insertElementsTestCase = BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testInsertElement, deviceInfoMapTest);
35  test_case* getDeviceInfoTestCase = BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testGetDeviceInfo, deviceInfoMapTest);
36  test_case* checkForDuplicateElementEntries =
37  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testCheckForDuplicateElements, deviceInfoMapTest);
38  test_case* testGetDeviceFileAndMapFileName =
39  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testGetDeviceFileAndMapFileName, deviceInfoMapTest);
40  test_case* testErrorElemErrTypeStreamOperator =
41  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testErrorElemErrTypeStreamOperator, deviceInfoMapTest);
42  test_case* testDeviceInfoStreamOperator =
43  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testDeviceInfoStreamOperator, deviceInfoMapTest);
44  test_case* testDeviceInfoMapStreamOperator =
45  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testDeviceInfoMapStreamOperator, deviceInfoMapTest);
46  test_case* testErrorElemStreamOperator =
47  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testErrorElemStreamOperator, deviceInfoMapTest);
48 
49  test_case* testErrorListStreamOperator =
50  BOOST_CLASS_TEST_CASE(&DeviceInfoMapTest::testErrorListStreamOperator, deviceInfoMapTest);
51  add(insertElementsTestCase);
52  add(getDeviceInfoTestCase);
53  add(checkForDuplicateElementEntries);
54  add(testGetDeviceFileAndMapFileName);
55  add(testErrorElemErrTypeStreamOperator);
56  add(testDeviceInfoStreamOperator);
57  add(testDeviceInfoMapStreamOperator);
58  add(testErrorElemStreamOperator);
59  add(testErrorListStreamOperator);
60  }
61 };
62 
64  framework::master_test_suite().p_name.value = "DeviceInfoMap test suite";
65  framework::master_test_suite().add(new DeviceInfoMapTestSuite());
66 
67  return true;
68  ;
69 }
70 
72  std::string dMapFileName = "dummy.map";
73  ChimeraTK::DeviceInfoMap deviceInfoMap(dMapFileName);
74 
78 
79  populateDummyDeviceInfo(deviceInfo1, dMapFileName);
80  populateDummyDeviceInfo(deviceInfo2, dMapFileName);
81  populateDummyDeviceInfo(deviceInfo3, dMapFileName);
82 
83  deviceInfoMap.insert(deviceInfo1);
84  deviceInfoMap.insert(deviceInfo2);
85  deviceInfoMap.insert(deviceInfo3);
86 
88  ptrList[0] = &deviceInfo1;
89  ptrList[1] = &deviceInfo2;
90  ptrList[2] = &deviceInfo3;
91  int index;
92 
94  for(it = deviceInfoMap.begin(), index = 0; (it != deviceInfoMap.end()) && (index < 3); ++it, ++index) {
95  BOOST_CHECK((compareDeviceInfos(*ptrList[index], *it)) == true);
96  }
97  BOOST_CHECK(deviceInfoMap.getSize() == 3);
98 }
99 
101  std::string dMapFileName = "dummy.map";
102  ChimeraTK::DeviceInfoMap RegisterInfoMap(dMapFileName);
103 
106 
107  populateDummyDeviceInfo(deviceInfo1, dMapFileName);
108  populateDummyDeviceInfo(deviceInfo2, dMapFileName);
109 
110  RegisterInfoMap.insert(deviceInfo1);
111  RegisterInfoMap.insert(deviceInfo2);
112 
113  ChimeraTK::DeviceInfoMap::DeviceInfo retrievedElement1;
114  ChimeraTK::DeviceInfoMap::DeviceInfo retrievedElement2;
115  ChimeraTK::DeviceInfoMap::DeviceInfo retrievedElement3;
116 
117  RegisterInfoMap.getDeviceInfo(deviceInfo1.deviceName, retrievedElement1);
118  RegisterInfoMap.getDeviceInfo(deviceInfo2.deviceName, retrievedElement2);
119 
120  BOOST_CHECK((compareDeviceInfos(retrievedElement1, deviceInfo1)) == true);
121  BOOST_CHECK((compareDeviceInfos(retrievedElement2, deviceInfo2)) == true);
122  BOOST_CHECK_THROW(RegisterInfoMap.getDeviceInfo("invalid_card_name", retrievedElement3), ChimeraTK::logic_error);
123 }
124 
126  std::string dMapFileName = "dummy.map";
127  std::string commonCardName = "common_card";
128  ChimeraTK::DeviceInfoMap RegisterInfoMap(dMapFileName);
129 
134 
135  populateDummyDeviceInfo(deviceInfo1, dMapFileName);
136  populateDummyDeviceInfo(deviceInfo2, dMapFileName);
137  populateDummyDeviceInfo(deviceInfo3, dMapFileName);
138  populateDummyDeviceInfo(deviceInfo4, dMapFileName);
139 
140  deviceInfo1.deviceName = commonCardName;
141  deviceInfo2.deviceName = commonCardName;
142  deviceInfo3.deviceName = commonCardName;
143 
144  ChimeraTK::DeviceInfoMap::ErrorList elementDuplications;
145  RegisterInfoMap.insert(deviceInfo1);
146  BOOST_CHECK(
147  RegisterInfoMap.check(elementDuplications, ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ERROR) == true);
148 
149  RegisterInfoMap.insert(deviceInfo2);
150  RegisterInfoMap.insert(deviceInfo3);
151  RegisterInfoMap.insert(deviceInfo4);
152 
153  RegisterInfoMap.check(elementDuplications, ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ERROR);
154 
155  int numberOfIncorrectLinesInFile = elementDuplications._errors.size();
156  BOOST_CHECK(numberOfIncorrectLinesInFile == 2);
157 
158  std::list<ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem>::iterator errorIterator;
159  for(errorIterator = elementDuplications._errors.begin(); errorIterator != elementDuplications._errors.end();
160  ++errorIterator) {
161  bool doesDetectedElementsHaveSameName =
162  (errorIterator->_errorDevice1.deviceName == errorIterator->_errorDevice2.deviceName);
163  BOOST_CHECK(doesDetectedElementsHaveSameName);
164  }
165 }
166 
169  deviceInfo1.uri = "/dev/test";
170  deviceInfo1.mapFileName = "test_mapfile";
171 
172  std::pair<std::string, std::string> expected_pair("/dev/test", "test_mapfile");
173  std::pair<std::string, std::string> actual_pair = deviceInfo1.getDeviceFileAndMapFileName();
174  BOOST_CHECK(expected_pair == actual_pair);
175 }
176 
178  std::stringstream file_stream1;
180  BOOST_CHECK(file_stream1.str() == "ERROR");
181 
182  std::stringstream file_stream2;
184  BOOST_CHECK(file_stream2.str() == "WARNING");
185 
186  std::stringstream file_stream3;
188  BOOST_CHECK(file_stream3.str() == "UNKNOWN");
189 }
190 
193  deviceInfo1.uri = "/dev/dev1";
194  deviceInfo1.deviceName = "card1";
195  deviceInfo1.dmapFileLineNumber = 1;
196  deviceInfo1.dmapFileName = "dummy.dmap";
197  deviceInfo1.mapFileName = "mapped_file";
198 
199  std::stringstream expected_file_stream;
200  expected_file_stream << "("
201  << "dummy.dmap"
202  << ") NAME: "
203  << "card1"
204  << " DEV : "
205  << "/dev/dev1"
206  << " MAP : "
207  << "mapped_file";
208 
209  std::stringstream actual_file_stream;
210  actual_file_stream << deviceInfo1;
211 
212  BOOST_CHECK(expected_file_stream.str() == actual_file_stream.str());
213 }
214 
216  std::string dMapFileName = "dummy.dmap";
217  ChimeraTK::DeviceInfoMap deviceInfoMap(dMapFileName);
218 
220  populateDummyDeviceInfo(deviceInfo1, dMapFileName, "card1", "/dev/dev1", "map_file");
221  deviceInfoMap.insert(deviceInfo1);
222 
223  std::stringstream expected_file_stream;
224  expected_file_stream << "=======================================" << std::endl;
225  expected_file_stream << "MAP FILE NAME: "
226  << "dummy.dmap" << std::endl;
227  expected_file_stream << "---------------------------------------" << std::endl;
228 
229  expected_file_stream << "("
230  << "dummy.dmap"
231  << ") NAME: "
232  << "card1"
233  << " DEV : "
234  << "/dev/dev1"
235  << " MAP : "
236  << "map_file" << std::endl;
237 
238  expected_file_stream << "=======================================";
239 
240  std::stringstream actual_file_stream;
241  actual_file_stream << deviceInfoMap;
242  BOOST_CHECK(expected_file_stream.str() == actual_file_stream.str());
243 }
244 
246  std::string dMapFileName = "dummy.map";
247  // std::string commonCardName = "common_card";
248  ChimeraTK::DeviceInfoMap RegisterInfoMap(dMapFileName);
249 
252 
253  populateDummyDeviceInfo(deviceInfo1, "dummy.dmap", "card1", "/dev/dev1", "map_file");
254  populateDummyDeviceInfo(deviceInfo2, "dummy.dmap", "card1", "/dev/dev1", "map_file");
255 
256  deviceInfo1.dmapFileLineNumber = 1;
257  deviceInfo2.dmapFileLineNumber = 2;
258 
261  std::stringstream expected_file_stream;
263  << ": Found two devices with the same name but different properties: \""
264  << "card1"
265  << "\" in file \""
266  << "dummy.dmap"
267  << "\" in line " << 1 << " and \""
268  << "dummy.dmap"
269  << "\" in line " << 2;
270 
271  std::stringstream actual_file_stream;
272  actual_file_stream << error_element;
273 
274  BOOST_CHECK(expected_file_stream.str() == actual_file_stream.str());
275 }
276 
278  std::string dMapFileName = "dummy.dmap";
279  std::string commonCardName = "card1";
280  ChimeraTK::DeviceInfoMap RegisterInfoMap(dMapFileName);
281 
284 
285  populateDummyDeviceInfo(deviceInfo1, dMapFileName);
286  populateDummyDeviceInfo(deviceInfo2, dMapFileName);
287 
288  deviceInfo1.deviceName = commonCardName;
289  deviceInfo2.deviceName = commonCardName;
290 
291  deviceInfo1.dmapFileLineNumber = 1;
292  deviceInfo2.dmapFileLineNumber = 2;
293 
294  ChimeraTK::DeviceInfoMap::ErrorList elementDuplications;
295  RegisterInfoMap.insert(deviceInfo1);
296  RegisterInfoMap.insert(deviceInfo2);
297 
298  RegisterInfoMap.check(elementDuplications, ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ERROR);
299  std::stringstream expected_file_stream;
301  << ": Found two devices with the same name but different properties: \""
302  << "card1"
303  << "\" in file \""
304  << "dummy.dmap"
305  << "\" in line " << 1 << " and \""
306  << "dummy.dmap"
307  << "\" in line " << 2 << std::endl;
308 
309  std::stringstream actual_file_stream;
310  actual_file_stream << elementDuplications;
311 
312  BOOST_CHECK(expected_file_stream.str() == actual_file_stream.str());
313 }
DeviceInfoMapTest::testErrorElemStreamOperator
void testErrorElemStreamOperator()
Definition: testDeviceInfoMap.cpp:245
helperFunctions.h
DeviceInfoMapTest::testErrorListStreamOperator
void testErrorListStreamOperator()
Definition: testDeviceInfoMap.cpp:277
ChimeraTK::DeviceInfoMap::iterator
std::vector< DeviceInfo >::iterator iterator
Definition: DeviceInfoMap.h:60
ChimeraTK::DeviceInfoMap
Definition: DeviceInfoMap.h:25
ChimeraTK::DeviceInfoMap::DeviceInfo::deviceName
std::string deviceName
logical name of the device
Definition: DeviceInfoMap.h:32
ChimeraTK::DeviceInfoMap::DeviceInfo::mapFileName
std::string mapFileName
name of the MAP file storing information about PCIe registers mapping
Definition: DeviceInfoMap.h:35
DeviceInfoMapTest::testDeviceInfoMapStreamOperator
void testDeviceInfoMapStreamOperator()
Definition: testDeviceInfoMap.cpp:215
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::WARNING
@ WARNING
Non-critical error was detected.
Definition: DeviceInfoMap.h:97
ChimeraTK::DeviceInfoMap::begin
iterator begin()
Return iterator to first device described in DMAP file.
Definition: DeviceInfoMap.cc:139
boost_dynamic_init_test.h
ChimeraTK::DeviceInfoMap::end
iterator end()
Return iterator to element after last one in DMAP file.
Definition: DeviceInfoMap.cc:143
ChimeraTK::DeviceInfoMap::getSize
size_t getSize()
Returns number of records in DMAP file.
Definition: DeviceInfoMap.cc:16
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileName
std::string dmapFileName
name of the DMAP file
Definition: DeviceInfoMap.h:37
DeviceInfoMapTest::testDeviceInfoStreamOperator
void testDeviceInfoStreamOperator()
Definition: testDeviceInfoMap.cpp:191
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::TYPE
TYPE
Defines available classes of detected problems.
Definition: DeviceInfoMap.h:95
ChimeraTK::DeviceInfoMap::ErrorList
Stores information about errors and warnings.
Definition: DeviceInfoMap.h:68
populateDummyDeviceInfo
void populateDummyDeviceInfo(ChimeraTK::DeviceInfoMap::DeviceInfo &deviceInfo, std::string dmapFileName, std::string deviceName="card", std::string dev_file="/dev/dummy_device_identifier", std::string map_file_name="/dev/dummy_map_file")
Definition: helperFunctions.cc:8
DeviceInfoMapTest::testGetDeviceFileAndMapFileName
void testGetDeviceFileAndMapFileName()
Definition: testDeviceInfoMap.cpp:167
ChimeraTK::DeviceInfoMap::DeviceInfo::dmapFileLineNumber
uint32_t dmapFileLineNumber
line number in DMAP file storing listed above information
Definition: DeviceInfoMap.h:38
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem
Stores detailed information about one error or warning.
Definition: DeviceInfoMap.h:78
compareDeviceInfos
bool compareDeviceInfos(const ChimeraTK::DeviceInfoMap::DeviceInfo &deviceInfo1, const ChimeraTK::DeviceInfoMap::DeviceInfo &deviceInfo2)
Definition: helperFunctions.cc:28
ChimeraTK::DeviceInfoMap::check
bool check(ErrorList &err, ErrorList::ErrorElem::TYPE level)
Checks logical correctness of DMAP file.
Definition: DeviceInfoMap.cc:58
ChimeraTK::DeviceInfoMap::DeviceInfo
Stores information about one device.
Definition: DeviceInfoMap.h:30
DeviceInfoMapTest
Definition: testDeviceInfoMap.cpp:16
DeviceInfoMapTestSuite
Definition: testDeviceInfoMap.cpp:29
DeviceInfoMapTest::testInsertElement
void testInsertElement()
Definition: testDeviceInfoMap.cpp:71
init_unit_test
bool init_unit_test()
Definition: testDeviceInfoMap.cpp:63
DeviceInfoMapTest::testCheckForDuplicateElements
void testCheckForDuplicateElements()
Definition: testDeviceInfoMap.cpp:125
DeviceInfoMap.h
DeviceInfoMapTest::testGetDeviceInfo
void testGetDeviceInfo()
Definition: testDeviceInfoMap.cpp:100
ChimeraTK::DeviceInfoMap::ErrorList::_errors
std::list< ErrorElem > _errors
Lists of errors or warnings detected during MAP file correctness checking.
Definition: DeviceInfoMap.h:121
DeviceInfoMapTestSuite::DeviceInfoMapTestSuite
DeviceInfoMapTestSuite()
Definition: testDeviceInfoMap.cpp:31
ChimeraTK::DeviceInfoMap::DeviceInfo::uri
std::string uri
uri which describes the device (or name of the device file in /dev in backward compatibility mode)
Definition: DeviceInfoMap.h:33
ChimeraTK::DeviceInfoMap::DeviceInfo::getDeviceFileAndMapFileName
std::pair< std::string, std::string > getDeviceFileAndMapFileName() const
Convenience function to extract the device file name and the map file name as one object (a pair).
Definition: DeviceInfoMap.cc:47
Exception.h
ChimeraTK
Definition: DummyBackend.h:16
ChimeraTK::DeviceInfoMap::insert
void insert(const DeviceInfo &elem)
Insert new element read from DMAP file.
Definition: DeviceInfoMap.cc:32
DeviceInfoMapTest::testErrorElemErrTypeStreamOperator
void testErrorElemErrTypeStreamOperator()
Definition: testDeviceInfoMap.cpp:177
ChimeraTK::DeviceInfoMap::getDeviceInfo
void getDeviceInfo(const std::string &deviceName, DeviceInfo &value)
Returns information about specified device.
Definition: DeviceInfoMap.cc:36
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::NONUNIQUE_DEVICE_NAME
@ NONUNIQUE_DEVICE_NAME
Names of two devices are the same - treated as critical error.
Definition: DeviceInfoMap.h:84
ChimeraTK::logic_error
Exception thrown when a logic error has occured.
Definition: Exception.h:51
ChimeraTK::DeviceInfoMap::ErrorList::ErrorElem::ERROR
@ ERROR
Critical error was detected.
Definition: DeviceInfoMap.h:96