Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / ACE / tests / Chrono_Test.cpp
blob7a09b1d9a99d92f9b38ff46884678a6edfab01f8
2 //=============================================================================
3 /**
4 * @file Chrono_Test.cpp
6 * This is a test of the usage of 'std::chrono' throughout ACE
7 * The following items are tested:
8 * - ACE_OS::sleep
9 * - ACE_Time_Value
12 * @author Marcel Smit <msmit@remedy.nl>
14 //=============================================================================
17 #include "test_config.h"
18 #include "ace/OS_NS_unistd.h"
19 #include "ace/Time_Value.h"
21 #include "ace/Truncate.h"
23 int
24 test_assignments ()
26 int errors {};
27 ACE_Time_Value tv { std::chrono::nanoseconds {100} };
28 if (tv.sec () != 0 || tv.usec () != 0)
30 ACE_ERROR ((LM_ERROR,
31 ACE_TEXT ("(%P|%t) unexpected value after converting ")
32 ACE_TEXT ("std::chrono::nanoseconds (100) to an ACE_Time_Value. ")
33 ACE_TEXT ("<sec=0,usec=0> - got <sec=%d,usec=%d>\n"),
34 tv.sec (), tv.usec ()));
35 ++errors;
38 tv = ACE_Time_Value { std::chrono::nanoseconds {10005} };
39 if (tv.sec () != 0 || tv.usec () != 10)
41 ACE_ERROR ((LM_ERROR,
42 ACE_TEXT ("(%P|%t) unexpected value after converting ")
43 ACE_TEXT ("std::chrono::nanoseconds (10005) to an ACE_Time_Value. ")
44 ACE_TEXT ("<sec=0,usec=10> - got <sec=%d,usec=%d>\n"),
45 tv.sec (), tv.usec ()));
46 ++errors;
49 tv = ACE_Time_Value { std::chrono::microseconds {1} };
50 if (tv.sec () != 0 || tv.usec () != 1)
52 ACE_ERROR ((LM_ERROR,
53 ACE_TEXT ("(%P|%t) unexpected value after converting ")
54 ACE_TEXT ("std::chrono::microseconds (1) to an ACE_Time_Value. ")
55 ACE_TEXT ("<sec=0,usec=1> - got <sec=%d,usec=%d>\n"),
56 tv.sec (), tv.usec ()));
57 ++errors;
60 tv = ACE_Time_Value { std::chrono::microseconds {10005} };
61 if (tv.sec () != 0 || tv.usec () != 10005)
63 ACE_ERROR ((LM_ERROR,
64 ACE_TEXT ("(%P|%t) unexpected value after converting ")
65 ACE_TEXT ("std::chrono::microseconds (10005) to an ACE_Time_Value. ")
66 ACE_TEXT ("<sec=0,usec=10005> - got <sec=%d,usec=%d>\n"),
67 tv.sec (), tv.usec ()));
68 ++errors;
71 std::chrono::milliseconds ms_test { tv.msec () };
72 if (ms_test.count () != 10)
74 ACE_ERROR ((LM_ERROR,
75 ACE_TEXT ("(%P|%t) unexpected value after get_chrono_msec. ")
76 ACE_TEXT ("Expected <10> - got <%q>\n"),
77 ms_test.count ()));
78 ++errors;
81 tv = ACE_Time_Value { std::chrono::milliseconds {1} };
82 if (tv.sec () != 0 || tv.usec () != 1000)
84 ACE_ERROR ((LM_ERROR,
85 ACE_TEXT ("(%P|%t) unexpected value after converting ")
86 ACE_TEXT ("std::chrono::milliseconds (1) to an ACE_Time_Value. ")
87 ACE_TEXT ("<sec=0,usec=1000> - got <sec=%d,usec=%d>\n"),
88 tv.sec (), tv.usec ()));
89 ++errors;
92 tv = ACE_Time_Value { std::chrono::milliseconds {10005} };
93 if (tv.sec () != 10 || tv.usec () != 5000)
95 ACE_ERROR ((LM_ERROR,
96 ACE_TEXT ("(%P|%t) unexpected value after converting ")
97 ACE_TEXT ("std::chrono::milliseconds (10005) to an ACE_Time_Value. ")
98 ACE_TEXT ("<sec=10,usec=5000> - got <sec=%d,usec=%d>\n"),
99 tv.sec (), tv.usec ()));
100 ++errors;
103 tv = ACE_Time_Value { std::chrono::seconds {1} };
104 if (tv.sec () != 1 || tv.usec () != 0)
106 ACE_ERROR ((LM_ERROR,
107 ACE_TEXT ("(%P|%t) unexpected value after converting ")
108 ACE_TEXT ("std::chrono::seconds (1) to an ACE_Time_Value. ")
109 ACE_TEXT ("<sec=1,usec=0> - got <sec=%d,usec=%d>\n"),
110 tv.sec (), tv.usec ()));
111 ++errors;
114 tv = ACE_Time_Value { std::chrono::seconds {10005} };
115 if (tv.sec () != 10005 || tv.usec () != 0)
117 ACE_ERROR ((LM_ERROR,
118 ACE_TEXT ("(%P|%t) unexpected value after converting ")
119 ACE_TEXT ("std::chrono::seconds (10005) to an ACE_Time_Value. ")
120 ACE_TEXT ("<sec=10005,usec=0> - got <sec=%d,usec=%d>\n"),
121 tv.sec (), tv.usec ()));
122 ++errors;
125 tv = ACE_Time_Value { std::chrono::hours {1} };
126 if (tv.sec () != 3600 || tv.usec () != 0)
128 ACE_ERROR ((LM_ERROR,
129 ACE_TEXT ("(%P|%t) unexpected value after converting ")
130 ACE_TEXT ("std::chrono::hours (1) to an ACE_Time_Value. ")
131 ACE_TEXT ("<sec=3600,usec=0> - got <sec=%d,usec=%d>\n"),
132 tv.sec (), tv.usec ()));
133 ++errors;
136 tv = ACE_Time_Value { std::chrono::hours {10005} };
137 if (tv.sec () != 3600*10005 || tv.usec () != 0)
139 ACE_ERROR ((LM_ERROR,
140 ACE_TEXT ("(%P|%t) unexpected value after converting ")
141 ACE_TEXT ("std::chrono::hours (10005) to an ACE_Time_Value. ")
142 ACE_TEXT ("<sec=%d,usec=0> - got <sec=%d,usec=%d>\n"),
143 3600*10005, tv.sec (), tv.usec ()));
144 ++errors;
147 // Two times half a day, 3 hours, 24 minutes, 54 seconds, 238 milliseconds,
148 // 754 microseconds and 342 nanoseconds.
149 std::chrono::duration<double, std::ratio<(24*3600)>> half_day {0.5};
150 std::chrono::microseconds const usec {
151 2 * (
152 std::chrono::duration_cast<std::chrono::microseconds> (
153 half_day +
154 std::chrono::hours {3} + std::chrono::minutes {24} +
155 std::chrono::seconds {54} + std::chrono::milliseconds {238} +
156 std::chrono::microseconds {754} + std::chrono::nanoseconds {342}))
160 tv = ACE_Time_Value {usec};
162 // half a day 3 hours 24 minutes 54 seconds
163 time_t expected_sec = { ((12*3600) + (3*3600) + (24*60) + 54 ) * 2 };
164 // 238 milli usec 342 nano
165 suseconds_t expected_usec = { ((238*1000) + 754 + 0) * 2 };
167 if (tv.sec () != expected_sec || tv.usec () != expected_usec)
169 ACE_ERROR ((LM_ERROR,
170 ACE_TEXT ("(%P|%t) unexpected value after converting ")
171 ACE_TEXT ("two times half a day, 3 hours, 24 minutes, 54 seconds, ")
172 ACE_TEXT ("238 milliseconds, 754 microseconds and 342 nanoseconds ")
173 ACE_TEXT ("to an ACE_Time_Value. Expected <sec=%d,usec=%d> - ")
174 ACE_TEXT ("got <sec=%d,usec=%d>\n"),
175 expected_sec, expected_usec, tv.sec (), tv.usec ()));
176 ++errors;
179 tv.set (std::chrono::milliseconds {1120});
180 if (tv.sec () != 1 || tv.usec () != 120 * std::kilo::num)
182 ACE_ERROR ((LM_ERROR,
183 ACE_TEXT ("(%P|%t) unexpected value after converting ")
184 ACE_TEXT ("a std::chrono::milliseconds of 1120 to an ACE_Time_Value ")
185 ACE_TEXT ("Expected <sec=1,usec=120000> - got <sec=%d,usec=%d>\n"),
186 tv.sec (), tv.usec ()));
187 ++errors;
190 return errors;
194 test_streamers ()
196 int errors {};
198 // Three days, 13 hours, 54 seconds, 25 milliseconds and 132 microseconds
199 constexpr int nr_hours { (3*24) + 13 };
201 std::chrono::hours day_test_h {nr_hours};
202 std::chrono::seconds day_test_s {54};
203 std::chrono::milliseconds day_test_ms {25};
204 std::chrono::microseconds day_test_us {132};
206 std::chrono::seconds day_test_ts { day_test_h+day_test_s };
207 std::chrono::microseconds day_test_tus { day_test_ms+day_test_us };
208 ACE_Time_Value const test_day {
209 ACE_Time_Value {
210 ACE_Utils::truncate_cast<time_t>(day_test_ts.count ()),
211 ACE_Utils::truncate_cast<suseconds_t>(day_test_tus.count ())}};
213 constexpr int expected_min {nr_hours * 60};
214 constexpr int64_t expected_sec { expected_min * 60 + 54 };
215 constexpr int64_t expected_msec { (expected_sec * std::kilo::num) + 25 };
216 constexpr int64_t expected_usec { (expected_msec * std::kilo::num) + 132 };
217 constexpr int64_t expected_nsec { (expected_usec * std::kilo::num) };
219 std::chrono::hours h;
220 h << test_day;
221 if (h.count () != nr_hours)
223 ACE_ERROR ((LM_ERROR,
224 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
225 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
226 ACE_TEXT ("std::chrono::hours. Expected <%d> - got <%q>.\n"),
227 test_day.sec (), test_day.usec (), nr_hours, h.count ()));
228 ++errors;
231 std::chrono::minutes m;
232 m << test_day;
233 if (m.count () != expected_min)
235 ACE_ERROR ((LM_ERROR,
236 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
237 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
238 ACE_TEXT ("std::chrono::minutes. Expected <%d> - got <%q>.\n"),
239 test_day.sec (), test_day.usec (), expected_min, m.count ()));
240 ++errors;
243 std::chrono::seconds s;
244 s << test_day;
245 if (s.count () != expected_sec)
247 ACE_ERROR ((LM_ERROR,
248 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
249 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
250 ACE_TEXT ("std::chrono::seconds. Expected <%q> - got <%q>.\n"),
251 test_day.sec (), test_day.usec (), expected_sec, s.count ()));
252 ++errors;
255 std::chrono::milliseconds ms;
256 ms << test_day;
257 if (ms.count () != expected_msec)
259 ACE_ERROR ((LM_ERROR,
260 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
261 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
262 ACE_TEXT ("std::chrono::milliseconds. Expected <%q> - got <%q>.\n"),
263 test_day.sec (), test_day.usec (), expected_msec, ms.count ()));
264 ++errors;
267 std::chrono::microseconds us;
268 us << test_day;
269 if (us.count () != expected_usec)
271 ACE_ERROR ((LM_ERROR,
272 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
273 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
274 ACE_TEXT ("std::chrono::microseconds. Expected <%q> - got <%q>.\n"),
275 test_day.sec (), test_day.usec (), expected_usec, us.count ()));
276 ++errors;
279 std::chrono::nanoseconds ns;
280 ns << test_day;
281 if (ns.count () != expected_nsec)
283 ACE_ERROR ((LM_ERROR,
284 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
285 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
286 ACE_TEXT ("std::chrono::nanoseconds. Expected <%q> - got <%q>.\n"),
287 test_day.sec (), test_day.usec (), expected_nsec, ns.count ()));
288 ++errors;
292 ACE_Time_Value const test_sec {12, 132};
293 // Seconds
294 s << test_sec;
295 if (s.count () != 12)
297 ACE_ERROR ((LM_ERROR,
298 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
299 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
300 ACE_TEXT ("std::chrono::seconds. Expected <%d> - got <%q>.\n"),
301 test_sec.sec (), test_sec.usec (), test_sec.sec (), s.count ()));
302 ++errors;
305 ACE_Time_Value const test_sec2 { ACE_Time_Value {12, 6 * std::mega::num} };
306 std::chrono::seconds s2;
307 s2 << test_sec2;
308 if (s2.count () != 18)
310 ACE_ERROR ((LM_ERROR,
311 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
312 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
313 ACE_TEXT ("std::chrono::seconds. Expected <%d> - got <%q>.\n"),
314 test_sec2.sec (), test_sec2.usec (), test_sec2.sec (), s2.count ()));
315 ++errors;
318 // Milliseconds
319 ms << test_sec;
320 if (ms.count () != 12 * std::kilo::num)
322 ACE_ERROR ((LM_ERROR,
323 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
324 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
325 ACE_TEXT ("std::chrono::milliseconds. Expected <%d> - got <%q>.\n"),
326 test_sec.sec (), test_sec.usec (), 12000, ms.count ()));
327 ++errors;
330 std::chrono::milliseconds ms2;
331 ms2 << test_sec2;
332 if (ms2.count () != 18 * std::kilo::num)
334 ACE_ERROR ((LM_ERROR,
335 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
336 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
337 ACE_TEXT ("std::chrono::milliseconds. Expected <%d> - got <%q>.\n"),
338 test_sec2.sec (), test_sec2.usec (), 18000, ms2.count ()));
339 ++errors;
342 // Microseconds
343 us << test_sec;
344 if (us.count () != (12 * std::mega::num) + 132)
346 ACE_ERROR ((LM_ERROR,
347 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
348 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
349 ACE_TEXT ("std::chrono::microseconds. Expected <%d> - got <%q>.\n"),
350 test_sec.sec (), test_sec.usec (), test_sec.sec (), us.count ()));
351 ++errors;
354 std::chrono::microseconds us2;
355 us2 << test_sec2;
356 if (us2.count () != 18 * std::mega::num)
358 ACE_ERROR ((LM_ERROR,
359 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
360 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
361 ACE_TEXT ("std::chrono::microseconds. Expected <%d> - got <%q>.\n"),
362 test_sec2.sec (), test_sec2.usec (), test_sec2.sec (), us2.count ()));
363 ++errors;
366 return errors;
370 test_ace_time_value_operators ()
372 int errors {};
374 std::chrono::seconds const sec {2};
375 std::chrono::microseconds const usec {3000};
377 std::chrono::milliseconds const msec {
378 std::chrono::duration_cast<std::chrono::milliseconds>(sec) +
379 std::chrono::duration_cast<std::chrono::milliseconds>(usec) };
382 ACE_Time_Value tv;
383 tv = msec;
384 tv += std::chrono::milliseconds {300};
385 if (tv.sec () != 2 || tv.usec () != 303 * std::kilo::num)
387 ACE_ERROR ((LM_ERROR,
388 ACE_TEXT ("(%P|%t) unexpected value after adding a duration ")
389 ACE_TEXT ("of 300 ms. Expected <sec=2,usec=3300> - got <sec=%d,")
390 ACE_TEXT ("usec=%d>.\n"),
391 tv.sec (), tv.usec ()));
392 ++errors;
394 tv -= std::chrono::microseconds {400};
395 if (tv.sec () != 2 || tv.usec () != 302600)
397 ACE_ERROR ((LM_ERROR,
398 ACE_TEXT ("(%P|%t) unexpected value after substracting a duration ")
399 ACE_TEXT ("of 400 us. Expected <sec=2,usec=3300> - got <sec=%d,")
400 ACE_TEXT ("usec=%d>.\n"),
401 tv.sec (), tv.usec ()));
402 ++errors;
404 return errors;
408 test_chrono_operators ()
410 int errors {};
412 std::chrono::hours hr {1};
413 ACE_Time_Value const tv_hr {3645, 0};
414 hr += tv_hr;
415 if (hr.count () != 2)
417 ACE_ERROR ((LM_ERROR,
418 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
419 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::hours of 1. ")
420 ACE_TEXT ("Expected <2> - got <%d>.\n"),
421 tv_hr.sec (), tv_hr.usec (), hr.count ()));
422 ++errors;
425 hr -= tv_hr;
426 if (hr.count () != 1)
428 ACE_ERROR ((LM_ERROR,
429 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
430 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::hours of 2. ")
431 ACE_TEXT ("Expected <1> - got <%d>.\n"),
432 tv_hr.sec (), tv_hr.usec (), hr.count ()));
433 ++errors;
437 std::chrono::minutes mn {1};
438 ACE_Time_Value const tv_min {75, 0};
439 mn += tv_min;
440 if (mn.count () != 2)
442 ACE_ERROR ((LM_ERROR,
443 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
444 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::minutes of 1. ")
445 ACE_TEXT ("Expected <2> - got <%d>.\n"),
446 tv_min.sec (), tv_min.usec (), mn.count ()));
447 ++errors;
450 mn -= tv_min;
451 if (mn.count () != 1)
453 ACE_ERROR ((LM_ERROR,
454 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
455 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::minutes of 2. ")
456 ACE_TEXT ("Expected <1> - got <%d>.\n"),
457 tv_min.sec (), tv_min.usec (), mn.count ()));
458 ++errors;
461 std::chrono::seconds sec {1};
462 ACE_Time_Value const tv_sec {1, ACE_Utils::truncate_cast<suseconds_t>(std::mega::num)};
463 sec += tv_sec;
464 if (sec.count () != 3)
466 ACE_ERROR ((LM_ERROR,
467 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
468 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::seconds of 1. ")
469 ACE_TEXT ("Expected <3> - got <%d>.\n"),
470 tv_sec.sec (), tv_sec.usec (), sec.count ()));
471 ++errors;
474 sec -= tv_sec;
475 if (sec.count () != 1)
477 ACE_ERROR ((LM_ERROR,
478 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
479 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::seconds of 3. ")
480 ACE_TEXT ("Expected <1> - got <%d>.\n"),
481 tv_sec.sec (), tv_sec.usec (), sec.count ()));
482 ++errors;
485 std::chrono::milliseconds msec {400};
486 ACE_Time_Value const tv_msec {1, 3000};
487 msec += tv_msec;
488 if (msec.count () != 1403)
490 ACE_ERROR ((LM_ERROR,
491 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
492 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::milliseconds of 400. ")
493 ACE_TEXT ("Expected <1403> - got <%d>.\n"),
494 tv_msec.sec (), tv_msec.usec (), msec.count ()));
495 ++errors;
498 msec -= tv_msec;
499 if (msec.count () != 400)
501 ACE_ERROR ((LM_ERROR,
502 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
503 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::milliseconds of 1403. ")
504 ACE_TEXT ("Expected <400> - got <%d>.\n"),
505 tv_msec.sec (), tv_msec.usec (), msec.count ()));
506 ++errors;
509 std::chrono::microseconds usec {400};
510 ACE_Time_Value const tv_usec {0, 3000};
511 usec += tv_usec;
512 if (usec.count () != 3400)
514 ACE_ERROR ((LM_ERROR,
515 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
516 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::microseconds of 400. ")
517 ACE_TEXT ("Expected <3400> - got <%d>.\n"),
518 tv_usec.sec (), tv_usec.usec (), usec.count ()));
519 ++errors;
522 usec -= tv_usec;
523 if (usec.count () != 400)
525 ACE_ERROR ((LM_ERROR,
526 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
527 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::microseconds of 3400. ")
528 ACE_TEXT ("Expected <400> - got <%d>.\n"),
529 tv_usec.sec (), tv_usec.usec (), usec.count ()));
530 ++errors;
533 std::chrono::nanoseconds nsec {4000};
534 nsec += tv_usec;
535 if (nsec.count () != 3004 * std::kilo::num)
537 ACE_ERROR ((LM_ERROR,
538 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
539 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::nanoseconds of 4000. ")
540 ACE_TEXT ("Expected <3004000> - got <%d>.\n"),
541 tv_usec.sec (), tv_usec.usec (), nsec.count ()));
542 ++errors;
545 nsec -= tv_usec;
546 if (nsec.count () != 4000)
548 ACE_ERROR ((LM_ERROR,
549 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
550 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::nanoseconds of 3004000. ")
551 ACE_TEXT ("Expected <4000> - got <%d>.\n"),
552 tv_usec.sec (), tv_usec.usec (), nsec.count ()));
553 ++errors;
556 return errors;
560 test_chrono ()
562 int errors = test_assignments ();
563 errors += test_streamers ();
564 errors += test_ace_time_value_operators ();
565 errors += test_chrono_operators ();
566 return errors;
570 run_main (int, ACE_TCHAR *[])
572 ACE_START_TEST (ACE_TEXT ("Chrono_Test"));
574 int errors = test_chrono ();
576 ACE_END_TEST;
577 return errors;