ChimeraTK-DeviceAccess  03.18.00
testUtilities.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 #include "BackendFactory.h"
9 #include "DeviceInfoMap.h"
10 #include "Exception.h"
11 #include "SupportedUserTypes.h"
12 #include "Utilities.h"
13 
14 #include <cstdint>
15 #include <limits>
16 
17 #define VALID_SDM "sdm://./pci:pcieunidummys6;undefined"
18 #define VALID_SDM_WITH_PARAMS "sdm://./dummy=goodMapFile.map"
19 #define INVALID_SDM "://./pci:pcieunidummys6;" // no sdm at the start
20 #define INVALID_SDM_2 "sdm://./pci:pcieunidummys6;;" // more than one semi-colons(;)
21 #define INVALID_SDM_3 "sdm://./pci::pcieunidummys6;" // more than one colons(:)
22 #define INVALID_SDM_4 "sdm://./dummy=goodMapFile.map=MapFile.map" // more than one equals to(=)
23 #define INVALID_SDM_5 "sdm://.pci:pcieunidummys6;" // no slash (/) after host.
24 #define VALID_PCI_STRING "/dev/mtcadummys0"
25 #define VALID_DUMMY_STRING "testfile.map"
26 #define VALID_DUMMY_STRING_2 "testfile.mapp"
27 #define INVALID_DEVICE_STRING "/mtcadummys0"
28 #define INVALID_DEVICE_STRING_2 "/dev"
29 #define INVALID_DEVICE_STRING_3 "testfile.mappp"
30 
31 using namespace ChimeraTK;
32 
33 /**********************************************************************************************************************/
34 
35 BOOST_AUTO_TEST_CASE(testParseCDD) {
36  {
37  // check standard case
38  auto r = Utilities::parseDeviceDesciptor("(myBackendType:some/weired*address 234?par1=someValue with "
39  "spaces&map=file)");
40  BOOST_CHECK_EQUAL(r.backendType, "myBackendType");
41  BOOST_CHECK_EQUAL(r.address, "some/weired*address 234");
42  BOOST_CHECK_EQUAL(r.parameters.size(), 2);
43  BOOST_CHECK_EQUAL(r.parameters["par1"], "someValue with spaces");
44  BOOST_CHECK_EQUAL(r.parameters["map"], "file");
45  }
46  {
47  // check proper trimming
48  auto r = Utilities::parseDeviceDesciptor(" ( myBackendType : some/weired*address 234 ? par1 = "
49  "someValue with spaces & map = file ) ");
50  BOOST_CHECK_EQUAL(r.backendType, "myBackendType");
51  BOOST_CHECK_EQUAL(r.address, "some/weired*address 234");
52  BOOST_CHECK_EQUAL(r.parameters.size(), 2);
53  BOOST_CHECK_EQUAL(r.parameters["par1"], "someValue with spaces");
54  BOOST_CHECK_EQUAL(r.parameters["map"], "file");
55  }
56  {
57  // check only backend type
58  auto r = Utilities::parseDeviceDesciptor("(someStrangeBackendType)");
59  BOOST_CHECK_EQUAL(r.backendType, "someStrangeBackendType");
60  BOOST_CHECK_EQUAL(r.address, "");
61  BOOST_CHECK_EQUAL(r.parameters.size(), 0);
62  }
63  {
64  // check only backend type with address
65  auto r = Utilities::parseDeviceDesciptor("(pci:pcieunis6)");
66  BOOST_CHECK_EQUAL(r.backendType, "pci");
67  BOOST_CHECK_EQUAL(r.address, "pcieunis6");
68  BOOST_CHECK_EQUAL(r.parameters.size(), 0);
69  }
70  {
71  // check explicitly empty parameter list
72  auto r = Utilities::parseDeviceDesciptor("(pci:pcieunis6?)");
73  BOOST_CHECK_EQUAL(r.backendType, "pci");
74  BOOST_CHECK_EQUAL(r.address, "pcieunis6");
75  BOOST_CHECK_EQUAL(r.parameters.size(), 0);
76  }
77  {
78  // check explicitly empty parameter list with more empty parameters
79  auto r = Utilities::parseDeviceDesciptor("(pci:pcieunis6?&&)");
80  BOOST_CHECK_EQUAL(r.backendType, "pci");
81  BOOST_CHECK_EQUAL(r.address, "pcieunis6");
82  BOOST_CHECK_EQUAL(r.parameters.size(), 0);
83  }
84  {
85  // check only backend type with parameters
86  auto r = Utilities::parseDeviceDesciptor("(logicalNameMapper?map=myMapFile.xlmap)");
87  BOOST_CHECK_EQUAL(r.backendType, "logicalNameMapper");
88  BOOST_CHECK_EQUAL(r.address, "");
89  BOOST_CHECK_EQUAL(r.parameters.size(), 1);
90  BOOST_CHECK_EQUAL(r.parameters["map"], "myMapFile.xlmap");
91  }
92  {
93  // check explicitly empty address
94  auto r = Utilities::parseDeviceDesciptor("(logicalNameMapper:?map=myMapFile.xlmap)");
95  BOOST_CHECK_EQUAL(r.backendType, "logicalNameMapper");
96  BOOST_CHECK_EQUAL(r.address, "");
97  BOOST_CHECK_EQUAL(r.parameters.size(), 1);
98  BOOST_CHECK_EQUAL(r.parameters["map"], "myMapFile.xlmap");
99  }
100  {
101  // check explicitly empty parameters
102  auto r = Utilities::parseDeviceDesciptor("(logicalNameMapper?&map=myMapFile.xlmap& &a=b&)");
103  BOOST_CHECK_EQUAL(r.backendType, "logicalNameMapper");
104  BOOST_CHECK_EQUAL(r.address, "");
105  BOOST_CHECK_EQUAL(r.parameters.size(), 2);
106  BOOST_CHECK_EQUAL(r.parameters["map"], "myMapFile.xlmap");
107  BOOST_CHECK_EQUAL(r.parameters["a"], "b");
108  }
109  {
110  // check parameter value with equal sign
111  auto r = Utilities::parseDeviceDesciptor("(x?a=b=c)");
112  BOOST_CHECK_EQUAL(r.backendType, "x");
113  BOOST_CHECK_EQUAL(r.address, "");
114  BOOST_CHECK_EQUAL(r.parameters.size(), 1);
115  BOOST_CHECK_EQUAL(r.parameters["a"], "b=c");
116  }
117  {
118  // check escaping special characters
119  auto r = Utilities::parseDeviceDesciptor("(x:address\\?withQuestionmark?para=value\\&with\\&ampersand\\&&x="
120  "y\\\\&y=\\))");
121  BOOST_CHECK_EQUAL(r.backendType, "x");
122  BOOST_CHECK_EQUAL(r.address, "address?withQuestionmark");
123  BOOST_CHECK_EQUAL(r.parameters.size(), 3);
124  BOOST_CHECK_EQUAL(r.parameters["para"], "value&with&ampersand&");
125  BOOST_CHECK_EQUAL(r.parameters["x"], "y\\");
126  BOOST_CHECK_EQUAL(r.parameters["y"], ")");
127  }
128  {
129  // check nesting CDDs
130  auto r = Utilities::parseDeviceDesciptor("(nested:(pci:pcieunis6?map=dummy.map)?"
131  "anotherCdd=with(dummycdd)otherText)");
132  BOOST_CHECK_EQUAL(r.backendType, "nested");
133  BOOST_CHECK_EQUAL(r.address, "(pci:pcieunis6?map=dummy.map)");
134  BOOST_CHECK_EQUAL(r.parameters.size(), 1);
135  BOOST_CHECK_EQUAL(r.parameters["anotherCdd"], "with(dummycdd)otherText");
136  }
137  // check exceptions
139  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("noParantheses"), ChimeraTK::logic_error);
140  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("( )"), ChimeraTK::logic_error);
141  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(backend)ExtraChars"), ChimeraTK::logic_error);
142  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(:address)"), ChimeraTK::logic_error);
143  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(bad_backend_name)"), ChimeraTK::logic_error);
144  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(x?keyNoValue)"), ChimeraTK::logic_error);
145  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(x?=valueNoKey)"), ChimeraTK::logic_error);
146  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(x?bad*key=value)"), ChimeraTK::logic_error);
147  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(x?key=value&key=duplicateKey)"), ChimeraTK::logic_error);
148  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(unmatchedParentheses"), ChimeraTK::logic_error);
149  BOOST_CHECK_THROW(
150  Utilities::parseDeviceDesciptor("(another:Unmatched?parentheses=in(aValue)"), ChimeraTK::logic_error);
151  BOOST_CHECK_THROW(
152  Utilities::parseDeviceDesciptor("(another:Unmatched?parentheses=in)aValue)"), ChimeraTK::logic_error);
153  BOOST_CHECK_THROW(Utilities::parseDeviceDesciptor("(badEscaping:a\\bc)"), ChimeraTK::logic_error);
154 }
155 /**********************************************************************************************************************/
156 
157 BOOST_AUTO_TEST_CASE(testParseSdm) {
159  BOOST_CHECK(sdm.host == ".");
160  BOOST_CHECK(sdm.interface == "pci");
161  BOOST_CHECK(sdm.instance == "pcieunidummys6");
162  BOOST_CHECK(sdm.parameters.size() == 0);
163  BOOST_CHECK(sdm.protocol == "undefined");
164 
166  BOOST_CHECK(sdm.host == ".");
167  BOOST_CHECK(sdm.interface == "dummy");
168  BOOST_CHECK(sdm.parameters.size() == 1);
169  BOOST_CHECK(sdm.parameters.front() == "goodMapFile.map");
170  BOOST_CHECK_THROW(Utilities::parseSdm(""),
171  ChimeraTK::logic_error); // Empty string
172  BOOST_CHECK_THROW(Utilities::parseSdm("sdm:"),
173  ChimeraTK::logic_error); // shorter than sdm:// signature
179 }
180 
181 /**********************************************************************************************************************/
182 
183 BOOST_AUTO_TEST_CASE(testParseDeviceString) {
185  BOOST_CHECK(sdm.interface == "pci");
187  BOOST_CHECK(sdm.interface == "dummy");
189  BOOST_CHECK(sdm.interface == "dummy");
191  BOOST_CHECK(sdm.interface == "");
193  BOOST_CHECK(sdm.interface == "");
195  BOOST_CHECK(sdm.interface == "");
196 }
197 
198 /**********************************************************************************************************************/
199 
200 BOOST_AUTO_TEST_CASE(testcountOccurence) {
201  BOOST_CHECK(Utilities::countOccurence("this,is;a:test,string", ',') == 2); // 2 commas
202  BOOST_CHECK(Utilities::countOccurence("this,is;a:test,string", ';') == 1); // 1 semi-colon
203  BOOST_CHECK(Utilities::countOccurence("this,is;a:test,string", ':') == 1); // 1 colon
204 }
205 
206 /**********************************************************************************************************************/
207 
209  BOOST_CHECK(Utilities::isSdm(VALID_SDM) == true);
210  BOOST_CHECK(Utilities::isSdm(INVALID_SDM) == false);
211  BOOST_CHECK(Utilities::isSdm(VALID_PCI_STRING) == false);
212 }
213 
214 /**********************************************************************************************************************/
215 
216 BOOST_AUTO_TEST_CASE(testAliasLookUp) {
217  std::string testFilePath = TEST_DMAP_FILE_PATH;
218  BOOST_CHECK_THROW(Utilities::aliasLookUp("test", testFilePath), ChimeraTK::logic_error);
219  auto deviceInfo = Utilities::aliasLookUp("DUMMYD0", testFilePath);
220  BOOST_CHECK(deviceInfo.deviceName == "DUMMYD0");
221 }
222 
223 /**********************************************************************************************************************/
224 
225 BOOST_AUTO_TEST_CASE(testgetAliasList) {
226  auto initialDmapFile = ChimeraTK::getDMapFilePath();
227 
229  BOOST_CHECK_THROW(Utilities::getAliasList(), ChimeraTK::logic_error);
230 
231  // entries in dummies.dmap when this was written
232  std::vector<std::string> expectedListOfAliases{"PCIE1", "PCIE0", "PCIE2", "PCIE3", "PCIE0", "DUMMYD0", "DUMMYD1",
233  "DUMMYD2", "DUMMYD3", "DUMMYD9", "PERFTEST", "mskrebot", "mskrebot1", "OLD_PCIE", "SEQUENCES",
234  "INVALID_SEQUENCES", "PCIE_DOUBLEMAP", "REBOT_DOUBLEMAP", "REBOT_INVALID_HOST"};
235 
236  ChimeraTK::setDMapFilePath("./dummies.dmap");
237  auto returnedListOfAliases = Utilities::getAliasList();
238  ChimeraTK::setDMapFilePath(initialDmapFile);
239 
240  int index = 0;
241  BOOST_CHECK(returnedListOfAliases.size() == expectedListOfAliases.size());
242  for(auto alias : expectedListOfAliases) {
243  BOOST_CHECK(alias == returnedListOfAliases.at(index++));
244  }
245 }
246 
247 /**********************************************************************************************************************/
248 
249 BOOST_AUTO_TEST_CASE(testUserTypeToUserType_Boolean) {
250  ChimeraTK::Boolean myBool{true};
251  BOOST_TEST(ChimeraTK::userTypeToUserType<std::string>(myBool) == "true");
252  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(myBool) == 1);
253 
254  myBool = false;
255  BOOST_TEST(ChimeraTK::userTypeToUserType<std::string>(myBool) == "false");
256  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(myBool) == 0);
257 
258  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("false")) == false);
259  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("False")) == false);
260  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("fAlSe")) == false);
261  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0")) == false);
262  // BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("00")) == false); //FIXME this should pass
263  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("")) == false);
264  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("true")) == true);
265  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("TRUE")) == true);
266  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("anyOtherString")) == true);
267 }
268 
269 /**********************************************************************************************************************/
270 
271 BOOST_AUTO_TEST_CASE(testUserTypeToUserType_HexString) {
272  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint64_t>(std::string("banana"))); // invalid
273  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0xG"))); // invalid
274 
275  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0x0")) == false); // min, mid
276  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0X1")) == true); // max, mid, big X
277  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0x00BA0000F0cacc1a")) ==
278  true); // overflow, mixed case
279  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0xDung"))); // invalid
280  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0x"))); // empty test
281  // FIXME this should pass
282  // BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0x000")) == false); // odd extra zeros
283 
284  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("0x66"))) == 0x66); // mid+,
285  BOOST_TEST(
286  static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("0X7F"))) == INT8_MAX); // max, big X
287  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("0x00BA0000F0cacc1a"))) ==
288  INT8_MAX); // overflow, mixed case
289  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int8_t>(std::string("0xDung"))); // invalid
290  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int8_t>(std::string("0x"))); // empty test
291 
292  BOOST_TEST(static_cast<uint32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0x66"))) == 0x66); // mid+
293  BOOST_TEST(
294  static_cast<uint32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0XFF"))) == UINT8_MAX); // max, big X
295  BOOST_TEST(static_cast<uint32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0x0"))) == 0); // min
296  BOOST_TEST(static_cast<uint32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0x00BA0000F0cacc1a"))) ==
297  UINT8_MAX); // overflow, mixed case
298  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0xDung"))); // invalid
299  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0x"))); // empty test
300 
301  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("0x6666")) == 0x6666); // 0x6666 = 26214 mid+
302  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("0X7FFF")) == INT16_MAX); // max, big X
303  BOOST_TEST(
304  ChimeraTK::userTypeToUserType<int16_t>(std::string("0x00BA0000F0cacc1a")) == INT16_MAX); // overflow, mixed case
305  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int16_t>(std::string("0xDung"))); // invalid
306  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int16_t>(std::string("0x"))); // empty test
307 
308  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0x6666")) == 0x6666); // mid+
309  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0XFFFF")) == UINT16_MAX); // max, big X
310  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0x0")) == 0); // min
311  BOOST_TEST(
312  ChimeraTK::userTypeToUserType<uint16_t>(std::string("0x00BA0000F0cacc1a")) == UINT16_MAX); // overflow, mixed case
313  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0xDung"))); // invalid
314  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0x"))); // empty test
315 
316  BOOST_TEST(
317  ChimeraTK::userTypeToUserType<int32_t>(std::string("0x66666666")) == 0x6666'6666); // 0x6666'6666=1717986918 mid+
318  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("0X7FFFFFFF")) == INT32_MAX); // max, big X
319  BOOST_TEST(
320  ChimeraTK::userTypeToUserType<int32_t>(std::string("0x00BA0000F0cacc1a")) == INT32_MAX); // overflow, mixed case
321  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int32_t>(std::string("0xDung"))); // invalid
322  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int32_t>(std::string("0x"))); // empty test
323 
324  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0x66666666")) == 0x6666'6666); // mid+
325  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0XFFFFFFFF")) == UINT32_MAX); // max, big X
326  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0x0")) == 0); // min
327  BOOST_TEST(
328  ChimeraTK::userTypeToUserType<uint32_t>(std::string("0x00BA0000F0cacc1a")) == UINT32_MAX); // overflow, mixed case
329  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0xDung"))); // invalid
330  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0x"))); // empty test
331 
332  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("0x6666666666666666")) ==
333  0x6666'6666'6666'6666); // = 7378697629483820646 mid+
334  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("0X7FFFFFFFFFFFFFFF")) == INT64_MAX); // max, big X
335  BOOST_TEST(
336  ChimeraTK::userTypeToUserType<int64_t>(std::string("0x100BA0000F0cacc1a")) == INT64_MAX); // overflow, mixed case
337  // The previous two lines are likely to run out of room and report 0
338  BOOST_CHECK_NO_THROW(
339  ChimeraTK::userTypeToUserType<int64_t>(std::string("0xdung"))); // invalid, try lower case for a change.
340  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int64_t>(std::string("0x"))); // empty test
341 
342  BOOST_TEST(
343  ChimeraTK::userTypeToUserType<uint64_t>(std::string("0xC0CAC01AADD511FE")) == 0xC0CA'C01A'ADD5'11FE); // mid+
344  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0XFFFFFFFFFFFFFFFF")) == UINT64_MAX); // max, big X
345  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0x0")) == 0); // min
346  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0xFFFFFFFFFF0cacc1a")) ==
347  UINT64_MAX); // overflow, mixed case
348  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0xDung"))); // invalid
349  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0x"))); // empty test
350 
351  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("0x66666666")), 1.71799e9, 1.71799e5);
352  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<float>(std::string("0xDung"))); // invalid
353  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<float>(std::string("0x"))); // empty test
354  //
355  BOOST_CHECK_CLOSE(
356  ChimeraTK::userTypeToUserType<double>(std::string("0x6666666666666666")), 7.3786976e18, 7.3786976e14); // mid+
357  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<double>(std::string("0xDung"))); // invalid
358  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<double>(std::string("0x"))); // empty test
359 
360  BOOST_TEST(ChimeraTK::userTypeToUserType<std::string>(std::string("0xDung")) == "0xDung");
361 }
362 
363 /**********************************************************************************************************************/
364 
365 BOOST_AUTO_TEST_CASE(testUserTypeToUserType_DecString) {
366  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("0")) == false); // mid+, min
367  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("1")) == true); // max
368  BOOST_TEST(ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("2")) == true); // overflow
369  BOOST_TEST(
370  ChimeraTK::userTypeToUserType<ChimeraTK::Boolean>(std::string("-7378697629483820646")) == true); // underflow
371  // invalid case is done in testUserTypeToUserType_Boolean
372 
373  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("102"))) == 102); // mid+
374  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("-102"))) == -102); // mid-
375  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("127"))) == INT8_MAX); // max
376  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("-128"))) == INT8_MIN); // min
377  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("300"))) == INT8_MAX); // overflow
378  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("-300"))) == INT8_MIN); // underflow
379  // FIXME make this pass
380  // BOOST_TEST(
381  // static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("73786976294838206460"))) == INT8_MAX);
382  // overflow
383 
384  // FIXME make this pass
385  // BOOST_TEST(
386  // static_cast<int32_t>(ChimeraTK::userTypeToUserType<int8_t>(std::string("-73786976294838206460"))) == INT8_MIN);
387  // underflow
388 
389  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int8_t>(std::string("banana"))); // invalid
390 
391  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("102"))) == 102); // mid+
392  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("255"))) == UINT8_MAX); // max
393  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("0"))) == 0); // min
394  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("300"))) == UINT8_MAX); // overflow
395  BOOST_TEST(static_cast<int32_t>(ChimeraTK::userTypeToUserType<uint8_t>(std::string("-5"))) == 0); // underflow pass
396  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint8_t>(std::string("banana"))); // invalid
397 
398  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("26214")) == 26214); // mid+
399  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("-26214")) == -26214); // mid-
400  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("32767")) == INT16_MAX); // max
401  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("-32768")) == INT16_MIN); // min
402  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("73786976294838206460")) == INT16_MAX); // overflow
403  BOOST_TEST(ChimeraTK::userTypeToUserType<int16_t>(std::string("-73786976294838206460")) == INT16_MIN); // underflow
404  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<int16_t>(std::string("banana"))); // invalid
405 
406  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("26214")) == 26214); // mid+
407  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("65535")) == UINT16_MAX); // max
408  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("0")) == 0); // min
409  BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("73786976294838206460")) == UINT16_MAX); // overflow
410  // FIXME make this pass
411  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("-73786976294838206460")) == 0); // underflow
412  // FIXME make this pass
413  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint16_t>(std::string("-5")) == 0); // underflow fail -5 = 65531.
414 
415  BOOST_CHECK_NO_THROW(ChimeraTK::userTypeToUserType<uint16_t>(std::string("banana"))); // invalid
416 
417  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("1717986918")) == 1717986918); // mid+
418  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("-1717986918")) == -1717986918); // mid-
419  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("2147483647")) == INT32_MAX); // max
420  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("-2147483648")) == INT32_MIN); // min
421  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("73786976294838206460")) == INT32_MAX); // overflow
422  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("-73786976294838206460")) == INT32_MIN); // underflow
423  BOOST_TEST(ChimeraTK::userTypeToUserType<int32_t>(std::string("banana")) == 0); // invalid
424 
425  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("1717986918")) == 1717986918); // mid+
426  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("4294967295")) == UINT32_MAX); // max
427  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("0")) == 0); // min
428  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("73786976294838206460")) == UINT32_MAX); // overflow
429  // FIXME make this pass
430  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("-73786976294838206460")) == 0); // underflow
431  // FIXME make this pass
432  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("-5")) == 0); // underflow
433  BOOST_TEST(ChimeraTK::userTypeToUserType<uint32_t>(std::string("banana")) == 0); // invalid
434 
435  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("7378697629483820646")) == 7378697629483820646); // mid+
436  BOOST_TEST(
437  ChimeraTK::userTypeToUserType<int64_t>(std::string("-7378697629483820646")) == -7378697629483820646); // mid-
438  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("9223372036854775807")) == INT64_MAX); // max
439  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("-9223372036854775808")) == INT64_MIN); // min
440  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("9223372036854775810")) == INT64_MAX); // overflow
441  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("-9223372036854775810")) == INT64_MIN); // underflow
442  // The previous two lines are likely to run out of room and report 0
443  BOOST_TEST(ChimeraTK::userTypeToUserType<int64_t>(std::string("banana")) == 0); // invalid
444 
445  BOOST_TEST(
446  ChimeraTK::userTypeToUserType<uint64_t>(std::string("7378697629483820646")) == 7378697629483820646); // mid+
447  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("18446744073709551615")) == UINT64_MAX); // max
448  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("0")) == 0); // min
449  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("18446744073709551625")) == UINT64_MAX); // overflow
450  // FIXME make this pass
451  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("-18446744073709551625")) == 0); // underflow
452  // FIXME make this pass
453  // BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("-5")) == 0); // underflow
454  BOOST_TEST(ChimeraTK::userTypeToUserType<uint64_t>(std::string("banana")) == 0); // invalid
455 
456  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("3.14159")), 3.14159, 3.14159 / 1000.0); // mid+
457  BOOST_CHECK_CLOSE(
458  ChimeraTK::userTypeToUserType<float>(std::string("-3.14159e3")), -3.14159e3, 3.14159e3 / 1000.0); // mid-
459  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("3.40282e38")), std::numeric_limits<float>::max(),
460  std::numeric_limits<float>::max() / 1000.); // max
461  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("-3.40282e38")),
462  (-std::numeric_limits<float>::max()), std::numeric_limits<float>::max() / 1000.); // min
463  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("5.40282e39")), std::numeric_limits<float>::max(),
464  std::numeric_limits<float>::max() / 1000.); // overflow
465  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<float>(std::string("-5.40282e39")),
466  (-std::numeric_limits<float>::max()), std::numeric_limits<float>::max() / 1000.); // underflow
467  BOOST_TEST(ChimeraTK::userTypeToUserType<float>(std::string("banana")) == 0.f); // invalid
468  BOOST_CHECK_CLOSE(
469  ChimeraTK::userTypeToUserType<float>(std::string("5")), 5.0f, 5.0 / 1000.); // float-int conversion good
470 
471  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("2.718281828459")), 2.718281828459,
472  2.718281828459 / 1000.0); // mid+
473  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("-2.718281828459e3")), -2.718281828459e3,
474  2.718281828459e3 / 1000.0); // mid-
475  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("1.7976931e+308")),
476  std::numeric_limits<double>::max(), std::numeric_limits<double>::max() / 1000.); // max
477  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("-1.7976931e+308")),
478  (-std::numeric_limits<double>::max()), std::numeric_limits<double>::max() / 1000.); // min
479  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("1.8976931e+309")),
480  std::numeric_limits<double>::max(), std::numeric_limits<double>::max() / 1000.); // overflow
481  BOOST_CHECK_CLOSE(ChimeraTK::userTypeToUserType<double>(std::string("-1.8976931e+309")),
482  (-std::numeric_limits<double>::max()), std::numeric_limits<double>::max() / 1000.); // underflow
483  BOOST_CHECK_CLOSE(
484  ChimeraTK::userTypeToUserType<double>(std::string("5")), 5.0, 5.0 / 1000.); // double-int conversion good
485  BOOST_TEST(ChimeraTK::userTypeToUserType<double>(std::string("banana")) == 0.0); // invalid
486 
487  BOOST_TEST(ChimeraTK::userTypeToUserType<std::string>(std::string("Any\r\nthing")) == "Any\r\nthing");
488 }
INVALID_SDM_4
#define INVALID_SDM_4
Definition: testUtilities.cpp:22
VALID_SDM_WITH_PARAMS
#define VALID_SDM_WITH_PARAMS
Definition: testUtilities.cpp:18
VALID_SDM
#define VALID_SDM
Definition: testUtilities.cpp:17
ChimeraTK::Sdm::host
std::string host
Definition: Utilities.h:42
ChimeraTK::Sdm::interface
std::string interface
Definition: Utilities.h:43
ChimeraTK::Utilities::parseDeviceDesciptor
DeviceDescriptor parseDeviceDesciptor(std::string cddString)
Parse a ChimeraTK device descriptor (CDD) and return the information in the DeviceDescriptor struct.
Definition: Utilities.cpp:47
INVALID_DEVICE_STRING
#define INVALID_DEVICE_STRING
Definition: testUtilities.cpp:27
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(testParseCDD)
Definition: testUtilities.cpp:35
INVALID_DEVICE_STRING_2
#define INVALID_DEVICE_STRING_2
Definition: testUtilities.cpp:28
ChimeraTK::Utilities::countOccurence
size_t countOccurence(std::string theString, char delimiter)
Check if the given string only contains alphanumeric characters.
Definition: Utilities.cpp:21
Utilities.h
ChimeraTK::Utilities::getAliasList
std::vector< std::string > getAliasList()
Returns the list of device aliases from the DMap file set using BackendFactory::setDMapFilePath.
Definition: Utilities.cpp:295
ChimeraTK::Sdm::instance
std::string instance
Definition: Utilities.h:44
ChimeraTK::Sdm
This structure holds the information of an SDM.
Definition: Utilities.h:40
INVALID_SDM_2
#define INVALID_SDM_2
Definition: testUtilities.cpp:20
SupportedUserTypes.h
INVALID_SDM_5
#define INVALID_SDM_5
Definition: testUtilities.cpp:23
VALID_DUMMY_STRING_2
#define VALID_DUMMY_STRING_2
Definition: testUtilities.cpp:26
ChimeraTK::Utilities::parseDeviceString
Sdm parseDeviceString(const std::string &deviceString)
Parse an old-style device string (either path to device node or map file name for dummies)
Definition: Utilities.cpp:260
ChimeraTK::Utilities::isSdm
bool isSdm(const std::string &theString)
Check wehter the given string seems to be an SDM.
Definition: Utilities.cpp:28
VALID_PCI_STRING
#define VALID_PCI_STRING
Definition: testUtilities.cpp:24
INVALID_SDM
#define INVALID_SDM
Definition: testUtilities.cpp:19
ChimeraTK::Utilities::parseSdm
Sdm parseSdm(const std::string &sdmString)
Parse an SDM URI and return the device information in the Sdm struct.
Definition: Utilities.cpp:189
INVALID_SDM_3
#define INVALID_SDM_3
Definition: testUtilities.cpp:21
ChimeraTK::Sdm::parameters
std::list< std::string > parameters
Definition: Utilities.h:46
ChimeraTK::getDMapFilePath
std::string getDMapFilePath()
Returns the dmap file name which the library currently uses for looking up device(alias) names.
Definition: Utilities.cpp:321
BackendFactory.h
ChimeraTK::Utilities::aliasLookUp
DeviceInfoMap::DeviceInfo aliasLookUp(const std::string &aliasName, const std::string &dmapFilePath)
Search for an alias in a given DMap file and return the DeviceInfo entry.
Definition: Utilities.cpp:286
DeviceInfoMap.h
VALID_DUMMY_STRING
#define VALID_DUMMY_STRING
Definition: testUtilities.cpp:25
ChimeraTK::Sdm::protocol
std::string protocol
Definition: Utilities.h:45
TEST_DMAP_FILE_PATH
#define TEST_DMAP_FILE_PATH
Definition: BackendFactory.h:18
Exception.h
INVALID_DEVICE_STRING_3
#define INVALID_DEVICE_STRING_3
Definition: testUtilities.cpp:29
ChimeraTK::setDMapFilePath
void setDMapFilePath(std::string dmapFilePath)
Set the location of the dmap file.
Definition: Utilities.cpp:327
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