2 //=============================================================================
4 * @file Chrono_Test.cpp
6 * This is a test of the usage of 'std::chrono' throughout ACE
7 * The following items are tested:
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"
27 ACE_Time_Value tv
{ std::chrono::nanoseconds
{100} };
28 if (tv
.sec () != 0 || tv
.usec () != 0)
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 ()));
38 tv
= ACE_Time_Value
{ std::chrono::nanoseconds
{10005} };
39 if (tv
.sec () != 0 || tv
.usec () != 10)
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 ()));
49 tv
= ACE_Time_Value
{ std::chrono::microseconds
{1} };
50 if (tv
.sec () != 0 || tv
.usec () != 1)
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 ()));
60 tv
= ACE_Time_Value
{ std::chrono::microseconds
{10005} };
61 if (tv
.sec () != 0 || tv
.usec () != 10005)
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 ()));
71 std::chrono::milliseconds ms_test
{ tv
.msec () };
72 if (ms_test
.count () != 10)
75 ACE_TEXT ("(%P|%t) unexpected value after get_chrono_msec. ")
76 ACE_TEXT ("Expected <10> - got <%q>\n"),
81 tv
= ACE_Time_Value
{ std::chrono::milliseconds
{1} };
82 if (tv
.sec () != 0 || tv
.usec () != 1000)
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 ()));
92 tv
= ACE_Time_Value
{ std::chrono::milliseconds
{10005} };
93 if (tv
.sec () != 10 || tv
.usec () != 5000)
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 ()));
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 ()));
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 ()));
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 ()));
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 ()));
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
{
152 std::chrono::duration_cast
<std::chrono::microseconds
> (
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 ()));
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 ()));
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
{
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
;
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 ()));
231 std::chrono::minutes m
;
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 ()));
243 std::chrono::seconds s
;
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 ()));
255 std::chrono::milliseconds ms
;
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 ()));
267 std::chrono::microseconds us
;
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 ()));
279 std::chrono::nanoseconds ns
;
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 ()));
293 ACE_Time_Value
const test_sec
{12, 132};
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 ()));
306 ACE_Time_Value
const test_sec2
{ ACE_Time_Value
{12, 6 * std::mega::num
} };
307 std::chrono::seconds s2
;
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 ()));
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 ()));
331 std::chrono::milliseconds ms2
;
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 ()));
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 ()));
355 std::chrono::microseconds us2
;
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 ()));
371 test_ace_time_value_operators ()
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
) };
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 ()));
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 ()));
409 test_chrono_operators ()
413 std::chrono::hours hr
{1};
414 ACE_Time_Value
const tv_hr
{3645, 0};
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 ()));
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 ()));
438 std::chrono::minutes mn
{1};
439 ACE_Time_Value
const tv_min
{75, 0};
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 ()));
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 ()));
462 std::chrono::seconds sec
{1};
463 ACE_Time_Value
const tv_sec
{1, ACE_Utils::truncate_cast
<suseconds_t
>(std::mega::num
)};
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 ()));
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 ()));
486 std::chrono::milliseconds msec
{400};
487 ACE_Time_Value
const tv_msec
{1, 3000};
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 ()));
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 ()));
510 std::chrono::microseconds usec
{400};
511 ACE_Time_Value
const tv_usec
{0, 3000};
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 ()));
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 ()));
534 std::chrono::nanoseconds nsec
{4000};
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 ()));
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 ()));
563 int errors
= test_assignments ();
564 errors
+= test_streamers ();
565 errors
+= test_ace_time_value_operators ();
566 errors
+= test_chrono_operators ();
571 run_main (int, ACE_TCHAR
*[])
573 ACE_START_TEST (ACE_TEXT ("Chrono_Test"));
575 int errors
= test_chrono ();