ChimeraTK-DeviceAccess  03.18.00
testVersionNumber.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 using namespace boost::unit_test_framework;
8 
9 #include "VersionNumber.h"
10 
11 #include <iostream>
12 #include <thread>
13 
14 using namespace ChimeraTK;
15 
17  public:
18  void testEqual();
19  void testNotEqual();
20  void testSmaller();
21  void testSmallerOrEqual();
22  void testGreater();
23  void testGreaterOrEqual();
24  void testCopyConstruct();
25  void testAssign();
26  void testThreadedCreation();
27  void testStringConvert();
28  void testTimeStamp();
29 
34 
35  std::list<VersionNumber> listA;
36  std::list<VersionNumber> listB;
37 };
38 
39 class VersionNumberTestSuite : public test_suite {
40  public:
41  VersionNumberTestSuite() : test_suite("VersionNumber test suite") {
42  boost::shared_ptr<VersionNumberTest> test(new VersionNumberTest);
43  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testEqual, test));
44  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testNotEqual, test));
45  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testSmaller, test));
46  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testSmallerOrEqual, test));
47  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testGreater, test));
48  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testGreaterOrEqual, test));
49  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testAssign, test));
50  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testThreadedCreation, test));
51  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testStringConvert, test));
52  add(BOOST_CLASS_TEST_CASE(&VersionNumberTest::testTimeStamp, test));
53  }
54 };
55 
57  framework::master_test_suite().p_name.value = "VersionNumber test suite";
58  framework::master_test_suite().add(new VersionNumberTestSuite);
59  return true;
60 }
61 
63  BOOST_CHECK((v1 == v1));
64  BOOST_CHECK(!(v1 == v2));
65  BOOST_CHECK(!(v1 == v3));
66  BOOST_CHECK(!(v1 == v4));
67 
68  BOOST_CHECK(!(v2 == v1));
69  BOOST_CHECK((v2 == v2));
70  BOOST_CHECK(!(v2 == v3));
71  BOOST_CHECK(!(v2 == v4));
72 
73  BOOST_CHECK(!(v3 == v1));
74  BOOST_CHECK(!(v3 == v2));
75  BOOST_CHECK((v3 == v3));
76  BOOST_CHECK(!(v3 == v4));
77 
78  BOOST_CHECK(!(v4 == v1));
79  BOOST_CHECK(!(v4 == v2));
80  BOOST_CHECK(!(v4 == v3));
81  BOOST_CHECK((v4 == v4));
82 }
83 
85  BOOST_CHECK(!(v1 != v1));
86  BOOST_CHECK((v1 != v2));
87  BOOST_CHECK((v1 != v3));
88  BOOST_CHECK((v1 != v4));
89 
90  BOOST_CHECK((v2 != v1));
91  BOOST_CHECK(!(v2 != v2));
92  BOOST_CHECK((v2 != v3));
93  BOOST_CHECK((v2 != v4));
94 
95  BOOST_CHECK((v3 != v1));
96  BOOST_CHECK((v3 != v2));
97  BOOST_CHECK(!(v3 != v3));
98  BOOST_CHECK((v3 != v4));
99 
100  BOOST_CHECK((v4 != v1));
101  BOOST_CHECK((v4 != v2));
102  BOOST_CHECK((v4 != v3));
103  BOOST_CHECK(!(v4 != v4));
104 }
105 
107  BOOST_CHECK(!(v1 < v1));
108  BOOST_CHECK((v1 < v2));
109  BOOST_CHECK((v1 < v3));
110  BOOST_CHECK((v1 < v4));
111 
112  BOOST_CHECK(!(v2 < v1));
113  BOOST_CHECK(!(v2 < v2));
114  BOOST_CHECK((v2 < v3));
115  BOOST_CHECK((v2 < v4));
116 
117  BOOST_CHECK(!(v3 < v1));
118  BOOST_CHECK(!(v3 < v2));
119  BOOST_CHECK(!(v3 < v3));
120  BOOST_CHECK((v3 < v4));
121 
122  BOOST_CHECK(!(v4 < v1));
123  BOOST_CHECK(!(v4 < v2));
124  BOOST_CHECK(!(v4 < v3));
125  BOOST_CHECK(!(v4 < v4));
126 }
127 
129  BOOST_CHECK((v1 <= v1));
130  BOOST_CHECK((v1 <= v2));
131  BOOST_CHECK((v1 <= v3));
132  BOOST_CHECK((v1 <= v4));
133 
134  BOOST_CHECK(!(v2 <= v1));
135  BOOST_CHECK((v2 <= v2));
136  BOOST_CHECK((v2 <= v3));
137  BOOST_CHECK((v2 <= v4));
138 
139  BOOST_CHECK(!(v3 <= v1));
140  BOOST_CHECK(!(v3 <= v2));
141  BOOST_CHECK((v3 <= v3));
142  BOOST_CHECK((v3 <= v4));
143 
144  BOOST_CHECK(!(v4 <= v1));
145  BOOST_CHECK(!(v4 <= v2));
146  BOOST_CHECK(!(v4 <= v3));
147  BOOST_CHECK((v4 <= v4));
148 }
149 
151  BOOST_CHECK(!(v1 > v1));
152  BOOST_CHECK(!(v1 > v2));
153  BOOST_CHECK(!(v1 > v3));
154  BOOST_CHECK(!(v1 > v4));
155 
156  BOOST_CHECK((v2 > v1));
157  BOOST_CHECK(!(v2 > v2));
158  BOOST_CHECK(!(v2 > v3));
159  BOOST_CHECK(!(v2 > v4));
160 
161  BOOST_CHECK((v3 > v1));
162  BOOST_CHECK((v3 > v2));
163  BOOST_CHECK(!(v3 > v3));
164  BOOST_CHECK(!(v3 > v4));
165 
166  BOOST_CHECK((v4 > v1));
167  BOOST_CHECK((v4 > v2));
168  BOOST_CHECK((v4 > v3));
169  BOOST_CHECK(!(v4 > v4));
170 }
171 
173  BOOST_CHECK((v1 >= v1));
174  BOOST_CHECK(!(v1 >= v2));
175  BOOST_CHECK(!(v1 >= v3));
176  BOOST_CHECK(!(v1 >= v4));
177 
178  BOOST_CHECK((v2 >= v1));
179  BOOST_CHECK((v2 >= v2));
180  BOOST_CHECK(!(v2 >= v3));
181  BOOST_CHECK(!(v2 >= v4));
182 
183  BOOST_CHECK((v3 >= v1));
184  BOOST_CHECK((v3 >= v2));
185  BOOST_CHECK((v3 >= v3));
186  BOOST_CHECK(!(v3 >= v4));
187 
188  BOOST_CHECK((v4 >= v1));
189  BOOST_CHECK((v4 >= v2));
190  BOOST_CHECK((v4 >= v3));
191  BOOST_CHECK((v4 >= v4));
192 }
193 
195  VersionNumber v1copied(v1);
196  VersionNumber v2copied(v2);
197  VersionNumber v3copied(v3);
198  VersionNumber v4copied(v4);
199 
200  BOOST_CHECK(v1copied == v1);
201  BOOST_CHECK(v2copied == v2);
202  BOOST_CHECK(v3copied == v3);
203  BOOST_CHECK(v4copied == v4);
204 
205  BOOST_CHECK(v1copied != v2);
206  BOOST_CHECK(v1copied != v3);
207  BOOST_CHECK(v1copied != v4);
208  BOOST_CHECK(v2copied != v3);
209  BOOST_CHECK(v2copied != v4);
210  BOOST_CHECK(v3copied != v4);
211 }
212 
214  VersionNumber v1assigned;
215  VersionNumber v2assigned;
216  VersionNumber v3assigned;
217  VersionNumber v4assigned;
218 
219  BOOST_CHECK(v1assigned > v4);
220  BOOST_CHECK(v2assigned > v4);
221  BOOST_CHECK(v3assigned > v4);
222  BOOST_CHECK(v4assigned > v4);
223 
224  v1assigned = v1;
225  v2assigned = v2;
226  v3assigned = v3;
227  v4assigned = v4;
228 
229  BOOST_CHECK(v1assigned == v1);
230  BOOST_CHECK(v2assigned == v2);
231  BOOST_CHECK(v3assigned == v3);
232  BOOST_CHECK(v4assigned == v4);
233 
234  v1assigned = VersionNumber();
235  BOOST_CHECK(v1assigned > v4assigned);
236 
237  v2assigned = VersionNumber();
238  BOOST_CHECK(v2assigned > v1assigned);
239 
240  v3assigned = VersionNumber();
241  BOOST_CHECK(v3assigned > v2assigned);
242 
243  v4assigned = VersionNumber();
244  BOOST_CHECK(v4assigned > v3assigned);
245 
246  v1assigned = VersionNumber(v1);
247  v2assigned = VersionNumber(v2);
248  v3assigned = VersionNumber(v3);
249  v4assigned = VersionNumber(v4);
250 
251  BOOST_CHECK(v1assigned == v1);
252  BOOST_CHECK(v2assigned == v2);
253  BOOST_CHECK(v3assigned == v3);
254  BOOST_CHECK(v4assigned == v4);
255 }
256 
258  std::cout << "Filling lists of VersionNumbers..." << std::endl;
259 
260  std::thread t([this] {
261  for(int i = 0; i < 1000; ++i) this->listA.push_back(VersionNumber());
262  });
263  for(int i = 0; i < 1000; ++i) listB.push_back(VersionNumber());
264  t.join();
265 
266  std::cout << "Now comparing all pairs of elements in the lists..." << std::endl;
267 
268  for(auto& a : listA) {
269  for(auto& b : listB) {
270  BOOST_CHECK(a != b);
271  }
272  int nSelfMatches = 0;
273  for(auto& a2 : listA) {
274  if(a == a2) nSelfMatches++;
275  }
276  BOOST_CHECK_EQUAL(nSelfMatches, 1);
277  }
278 
279  for(auto& b : listB) {
280  int nSelfMatches = 0;
281  for(auto& b2 : listB) {
282  if(b == b2) nSelfMatches++;
283  }
284  BOOST_CHECK_EQUAL(nSelfMatches, 1);
285  }
286 
287  std::cout << "done." << std::endl;
288 }
289 
291  std::string s1 = (std::string)v1;
292  std::string s2 = (std::string)v2;
293  std::string s3 = (std::string)v3;
294  std::string s4 = (std::string)v4;
295 
296  VersionNumber v1copied(v1);
297  VersionNumber v2copied(v2);
298  VersionNumber v3copied(v3);
299  VersionNumber v4copied(v4);
300 
301  std::string s1c = (std::string)v1copied;
302  std::string s2c = (std::string)v2copied;
303  std::string s3c = (std::string)v3copied;
304  std::string s4c = (std::string)v4copied;
305 
306  BOOST_CHECK(s1 != "");
307  BOOST_CHECK(s2 != "");
308  BOOST_CHECK(s3 != "");
309  BOOST_CHECK(s4 != "");
310 
311  BOOST_CHECK(s1c != "");
312  BOOST_CHECK(s2c != "");
313  BOOST_CHECK(s3c != "");
314  BOOST_CHECK(s4c != "");
315 
316  BOOST_CHECK_EQUAL(s1, s1c);
317  BOOST_CHECK_EQUAL(s2, s2c);
318  BOOST_CHECK_EQUAL(s3, s3c);
319  BOOST_CHECK_EQUAL(s4, s4c);
320 }
321 
323  auto t0 = std::chrono::system_clock::now();
324  VersionNumber vv0;
325  BOOST_CHECK(vv0.getTime() >= t0);
326  VersionNumber vv1;
327  BOOST_CHECK(vv1.getTime() >= vv0.getTime());
328  sleep(1);
329  VersionNumber vv2;
330  BOOST_CHECK(vv2.getTime() > vv1.getTime());
331  sleep(1);
332  auto t1 = std::chrono::system_clock::now();
333  BOOST_CHECK(vv2.getTime() < t1);
334 }
VersionNumberTest::testThreadedCreation
void testThreadedCreation()
Definition: testVersionNumber.cpp:257
VersionNumber.h
VersionNumberTestSuite
Definition: testVersionNumber.cpp:39
VersionNumberTest::v1
VersionNumber v1
Definition: testVersionNumber.cpp:30
VersionNumberTestSuite::VersionNumberTestSuite
VersionNumberTestSuite()
Definition: testVersionNumber.cpp:41
ChimeraTK::testable_rebot_sleep::now
boost::chrono::steady_clock::time_point now()
Definition: testableRebotSleep.cc:7
boost_dynamic_init_test.h
VersionNumberTest::testCopyConstruct
void testCopyConstruct()
Definition: testVersionNumber.cpp:194
VersionNumberTest::testGreaterOrEqual
void testGreaterOrEqual()
Definition: testVersionNumber.cpp:172
VersionNumberTest::testAssign
void testAssign()
Definition: testVersionNumber.cpp:213
init_unit_test
bool init_unit_test()
Definition: testVersionNumber.cpp:56
VersionNumberTest::v3
VersionNumber v3
Definition: testVersionNumber.cpp:32
VersionNumberTest::testNotEqual
void testNotEqual()
Definition: testVersionNumber.cpp:84
VersionNumberTest::listB
std::list< VersionNumber > listB
Definition: testVersionNumber.cpp:36
VersionNumberTest
Definition: testVersionNumber.cpp:16
VersionNumberTest::testEqual
void testEqual()
Definition: testVersionNumber.cpp:62
VersionNumberTest::testTimeStamp
void testTimeStamp()
Definition: testVersionNumber.cpp:322
VersionNumberTest::testGreater
void testGreater()
Definition: testVersionNumber.cpp:150
VersionNumberTest::testSmaller
void testSmaller()
Definition: testVersionNumber.cpp:106
VersionNumberTest::testSmallerOrEqual
void testSmallerOrEqual()
Definition: testVersionNumber.cpp:128
ChimeraTK::VersionNumber::getTime
std::chrono::time_point< std::chrono::system_clock > getTime() const
Return the time stamp associated with this version number.
Definition: VersionNumber.h:48
VersionNumberTest::listA
std::list< VersionNumber > listA
Definition: testVersionNumber.cpp:35
VersionNumberTest::testStringConvert
void testStringConvert()
Definition: testVersionNumber.cpp:290
ChimeraTK::VersionNumber
Class for generating and holding version numbers without exposing a numeric representation.
Definition: VersionNumber.h:23
VersionNumberTest::v2
VersionNumber v2
Definition: testVersionNumber.cpp:31
ChimeraTK
Definition: DummyBackend.h:16
VersionNumberTest::v4
VersionNumber v4
Definition: testVersionNumber.cpp:33