ACE+TAO-7_0_8
[ACE_TAO.git] / ACE / tests / Chrono_Test.cpp
blob1c9e86c685778d06b2e4788f3666305e0e19d909
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;
293 ACE_Time_Value const test_sec {12, 132};
294 // Seconds
295 s << test_sec;
296 if (s.count () != 12)
298 ACE_ERROR ((LM_ERROR,
299 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
300 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
301 ACE_TEXT ("std::chrono::seconds. Expected <%d> - got <%q>.\n"),
302 test_sec.sec (), test_sec.usec (), test_sec.sec (), s.count ()));
303 ++errors;
306 ACE_Time_Value const test_sec2 { ACE_Time_Value {12, 6 * std::mega::num} };
307 std::chrono::seconds s2;
308 s2 << test_sec2;
309 if (s2.count () != 18)
311 ACE_ERROR ((LM_ERROR,
312 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
313 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
314 ACE_TEXT ("std::chrono::seconds. Expected <%d> - got <%q>.\n"),
315 test_sec2.sec (), test_sec2.usec (), test_sec2.sec (), s2.count ()));
316 ++errors;
319 // Milliseconds
320 ms << test_sec;
321 if (ms.count () != 12 * std::kilo::num)
323 ACE_ERROR ((LM_ERROR,
324 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
325 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
326 ACE_TEXT ("std::chrono::milliseconds. Expected <%d> - got <%q>.\n"),
327 test_sec.sec (), test_sec.usec (), 12000, ms.count ()));
328 ++errors;
331 std::chrono::milliseconds ms2;
332 ms2 << test_sec2;
333 if (ms2.count () != 18 * std::kilo::num)
335 ACE_ERROR ((LM_ERROR,
336 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
337 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
338 ACE_TEXT ("std::chrono::milliseconds. Expected <%d> - got <%q>.\n"),
339 test_sec2.sec (), test_sec2.usec (), 18000, ms2.count ()));
340 ++errors;
343 // Microseconds
344 us << test_sec;
345 if (us.count () != (12 * std::mega::num) + 132)
347 ACE_ERROR ((LM_ERROR,
348 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
349 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
350 ACE_TEXT ("std::chrono::microseconds. Expected <%d> - got <%q>.\n"),
351 test_sec.sec (), test_sec.usec (), test_sec.sec (), us.count ()));
352 ++errors;
355 std::chrono::microseconds us2;
356 us2 << test_sec2;
357 if (us2.count () != 18 * std::mega::num)
359 ACE_ERROR ((LM_ERROR,
360 ACE_TEXT ("(%P|%t) unexpected value after streaming an ")
361 ACE_TEXT ("ACE_Time_Value <sec=%d,usec=%d> into ")
362 ACE_TEXT ("std::chrono::microseconds. Expected <%d> - got <%q>.\n"),
363 test_sec2.sec (), test_sec2.usec (), test_sec2.sec (), us2.count ()));
364 ++errors;
367 return errors;
371 test_ace_time_value_operators ()
373 int errors {};
375 std::chrono::seconds const sec {2};
376 std::chrono::microseconds const usec {3000};
378 std::chrono::milliseconds const msec {
379 std::chrono::duration_cast<std::chrono::milliseconds>(sec) +
380 std::chrono::duration_cast<std::chrono::milliseconds>(usec) };
383 ACE_Time_Value tv;
384 tv = msec;
385 tv += std::chrono::milliseconds {300};
386 if (tv.sec () != 2 || tv.usec () != 303 * std::kilo::num)
388 ACE_ERROR ((LM_ERROR,
389 ACE_TEXT ("(%P|%t) unexpected value after adding a duration ")
390 ACE_TEXT ("of 300 ms. Expected <sec=2,usec=3300> - got <sec=%d,")
391 ACE_TEXT ("usec=%d>.\n"),
392 tv.sec (), tv.usec ()));
393 ++errors;
395 tv -= std::chrono::microseconds {400};
396 if (tv.sec () != 2 || tv.usec () != 302600)
398 ACE_ERROR ((LM_ERROR,
399 ACE_TEXT ("(%P|%t) unexpected value after substracting a duration ")
400 ACE_TEXT ("of 400 us. Expected <sec=2,usec=3300> - got <sec=%d,")
401 ACE_TEXT ("usec=%d>.\n"),
402 tv.sec (), tv.usec ()));
403 ++errors;
405 return errors;
409 test_chrono_operators ()
411 int errors {};
413 std::chrono::hours hr {1};
414 ACE_Time_Value const tv_hr {3645, 0};
415 hr += tv_hr;
416 if (hr.count () != 2)
418 ACE_ERROR ((LM_ERROR,
419 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
420 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::hours of 1. ")
421 ACE_TEXT ("Expected <2> - got <%d>.\n"),
422 tv_hr.sec (), tv_hr.usec (), hr.count ()));
423 ++errors;
426 hr -= tv_hr;
427 if (hr.count () != 1)
429 ACE_ERROR ((LM_ERROR,
430 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
431 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::hours of 2. ")
432 ACE_TEXT ("Expected <1> - got <%d>.\n"),
433 tv_hr.sec (), tv_hr.usec (), hr.count ()));
434 ++errors;
438 std::chrono::minutes mn {1};
439 ACE_Time_Value const tv_min {75, 0};
440 mn += tv_min;
441 if (mn.count () != 2)
443 ACE_ERROR ((LM_ERROR,
444 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
445 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::minutes of 1. ")
446 ACE_TEXT ("Expected <2> - got <%d>.\n"),
447 tv_min.sec (), tv_min.usec (), mn.count ()));
448 ++errors;
451 mn -= tv_min;
452 if (mn.count () != 1)
454 ACE_ERROR ((LM_ERROR,
455 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
456 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::minutes of 2. ")
457 ACE_TEXT ("Expected <1> - got <%d>.\n"),
458 tv_min.sec (), tv_min.usec (), mn.count ()));
459 ++errors;
462 std::chrono::seconds sec {1};
463 ACE_Time_Value const tv_sec {1, ACE_Utils::truncate_cast<suseconds_t>(std::mega::num)};
464 sec += tv_sec;
465 if (sec.count () != 3)
467 ACE_ERROR ((LM_ERROR,
468 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
469 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::seconds of 1. ")
470 ACE_TEXT ("Expected <3> - got <%d>.\n"),
471 tv_sec.sec (), tv_sec.usec (), sec.count ()));
472 ++errors;
475 sec -= tv_sec;
476 if (sec.count () != 1)
478 ACE_ERROR ((LM_ERROR,
479 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
480 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::seconds of 3. ")
481 ACE_TEXT ("Expected <1> - got <%d>.\n"),
482 tv_sec.sec (), tv_sec.usec (), sec.count ()));
483 ++errors;
486 std::chrono::milliseconds msec {400};
487 ACE_Time_Value const tv_msec {1, 3000};
488 msec += tv_msec;
489 if (msec.count () != 1403)
491 ACE_ERROR ((LM_ERROR,
492 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
493 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::milliseconds of 400. ")
494 ACE_TEXT ("Expected <1403> - got <%d>.\n"),
495 tv_msec.sec (), tv_msec.usec (), msec.count ()));
496 ++errors;
499 msec -= tv_msec;
500 if (msec.count () != 400)
502 ACE_ERROR ((LM_ERROR,
503 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
504 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::milliseconds of 1403. ")
505 ACE_TEXT ("Expected <400> - got <%d>.\n"),
506 tv_msec.sec (), tv_msec.usec (), msec.count ()));
507 ++errors;
510 std::chrono::microseconds usec {400};
511 ACE_Time_Value const tv_usec {0, 3000};
512 usec += tv_usec;
513 if (usec.count () != 3400)
515 ACE_ERROR ((LM_ERROR,
516 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
517 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::microseconds of 400. ")
518 ACE_TEXT ("Expected <3400> - got <%d>.\n"),
519 tv_usec.sec (), tv_usec.usec (), usec.count ()));
520 ++errors;
523 usec -= tv_usec;
524 if (usec.count () != 400)
526 ACE_ERROR ((LM_ERROR,
527 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
528 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::microseconds of 3400. ")
529 ACE_TEXT ("Expected <400> - got <%d>.\n"),
530 tv_usec.sec (), tv_usec.usec (), usec.count ()));
531 ++errors;
534 std::chrono::nanoseconds nsec {4000};
535 nsec += tv_usec;
536 if (nsec.count () != 3004 * std::kilo::num)
538 ACE_ERROR ((LM_ERROR,
539 ACE_TEXT ("(%P|%t) unexpected value after adding an ACE_Time_Value ")
540 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::nanoseconds of 4000. ")
541 ACE_TEXT ("Expected <3004000> - got <%d>.\n"),
542 tv_usec.sec (), tv_usec.usec (), nsec.count ()));
543 ++errors;
546 nsec -= tv_usec;
547 if (nsec.count () != 4000)
549 ACE_ERROR ((LM_ERROR,
550 ACE_TEXT ("(%P|%t) unexpected value after substracting an ACE_Time_Value ")
551 ACE_TEXT ("of <sec=%d,usec=%d> to a std::chrono::nanoseconds of 3004000. ")
552 ACE_TEXT ("Expected <4000> - got <%d>.\n"),
553 tv_usec.sec (), tv_usec.usec (), nsec.count ()));
554 ++errors;
557 return errors;
561 test_chrono ()
563 int errors = test_assignments ();
564 errors += test_streamers ();
565 errors += test_ace_time_value_operators ();
566 errors += test_chrono_operators ();
567 return errors;
571 run_main (int, ACE_TCHAR *[])
573 ACE_START_TEST (ACE_TEXT ("Chrono_Test"));
575 int errors = test_chrono ();
577 ACE_END_TEST;
578 return errors;