|
5 | 5 | *
|
6 | 6 | * This is a test of the usage of 'std::chrono' throughout ACE
|
7 | 7 | * The following items are tested:
|
8 |
| - * - ACE_OS::sleep |
9 | 8 | * - ACE_Time_Value
|
10 | 9 | *
|
11 | 10 | *
|
|
23 | 22 | #include "ace/Truncate.h"
|
24 | 23 |
|
25 | 24 | int
|
26 |
| -test_assignments () |
| 25 | +tv_test_case (const ACE_Time_Value& tv, const char *what, time_t expect_sec, suseconds_t expect_usec = 0) |
27 | 26 | {
|
28 |
| - int errors {}; |
29 |
| - ACE_Time_Value tv { std::chrono::nanoseconds {100} }; |
30 |
| - if (tv.sec () != 0 || tv.usec () != 0) |
| 27 | + if (tv.sec () != expect_sec || tv.usec () != expect_usec) |
31 | 28 | {
|
32 | 29 | ACE_ERROR ((LM_ERROR,
|
33 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
34 |
| - ACE_TEXT ("std::chrono::nanoseconds (100) to an ACE_Time_Value. ") |
35 |
| - ACE_TEXT ("<sec=0,usec=0> - got <sec=%d,usec=%d>\n"), |
36 |
| - tv.sec (), tv.usec ())); |
37 |
| - ++errors; |
| 30 | + ACE_TEXT ("(%P|%t) unexpected value after converting %C to an ACE_Time_Value. ") |
| 31 | + ACE_TEXT ("Expected <sec=%d,usec=%d> - got <sec=%d,usec=%d>\n"), |
| 32 | + what, expect_sec, expect_usec, tv.sec (), tv.usec ())); |
| 33 | + return 1; |
38 | 34 | }
|
| 35 | + return 0; |
| 36 | +} |
39 | 37 |
|
40 |
| - tv = ACE_Time_Value { std::chrono::nanoseconds {10005} }; |
41 |
| - if (tv.sec () != 0 || tv.usec () != 10) |
42 |
| - { |
43 |
| - ACE_ERROR ((LM_ERROR, |
44 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
45 |
| - ACE_TEXT ("std::chrono::nanoseconds (10005) to an ACE_Time_Value. ") |
46 |
| - ACE_TEXT ("<sec=0,usec=10> - got <sec=%d,usec=%d>\n"), |
47 |
| - tv.sec (), tv.usec ())); |
48 |
| - ++errors; |
49 |
| - } |
| 38 | +template <class Rep, class Period> |
| 39 | +int |
| 40 | +tv_test_case (const std::chrono::duration<Rep, Period>& duration, |
| 41 | + const char *what, time_t expect_sec, suseconds_t expect_usec = 0) |
| 42 | +{ |
| 43 | + return tv_test_case (ACE_Time_Value {duration}, what, expect_sec, expect_usec); |
| 44 | +} |
50 | 45 |
|
51 |
| - tv = ACE_Time_Value { std::chrono::microseconds {1} }; |
52 |
| - if (tv.sec () != 0 || tv.usec () != 1) |
53 |
| - { |
54 |
| - ACE_ERROR ((LM_ERROR, |
55 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
56 |
| - ACE_TEXT ("std::chrono::microseconds (1) to an ACE_Time_Value. ") |
57 |
| - ACE_TEXT ("<sec=0,usec=1> - got <sec=%d,usec=%d>\n"), |
58 |
| - tv.sec (), tv.usec ())); |
59 |
| - ++errors; |
60 |
| - } |
| 46 | +int |
| 47 | +test_assignments () |
| 48 | +{ |
| 49 | + int errors {}; |
61 | 50 |
|
62 |
| - tv = ACE_Time_Value { std::chrono::microseconds {10005} }; |
63 |
| - if (tv.sec () != 0 || tv.usec () != 10005) |
64 |
| - { |
65 |
| - ACE_ERROR ((LM_ERROR, |
66 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
67 |
| - ACE_TEXT ("std::chrono::microseconds (10005) to an ACE_Time_Value. ") |
68 |
| - ACE_TEXT ("<sec=0,usec=10005> - got <sec=%d,usec=%d>\n"), |
69 |
| - tv.sec (), tv.usec ())); |
70 |
| - ++errors; |
71 |
| - } |
| 51 | + errors += tv_test_case(std::chrono::nanoseconds {100}, "nanoseconds (100)", 0); |
72 | 52 |
|
73 |
| - std::chrono::milliseconds ms_test { tv.msec () }; |
74 |
| - if (ms_test.count () != 10) |
75 |
| - { |
76 |
| - ACE_ERROR ((LM_ERROR, |
77 |
| - ACE_TEXT ("(%P|%t) unexpected value after get_chrono_msec. ") |
78 |
| - ACE_TEXT ("Expected <10> - got <%q>\n"), |
79 |
| - ms_test.count ())); |
80 |
| - ++errors; |
81 |
| - } |
| 53 | + errors += tv_test_case(std::chrono::nanoseconds {10005}, "nanoseconds (10005)", 0, 10); |
82 | 54 |
|
83 |
| - tv = ACE_Time_Value { std::chrono::milliseconds {1} }; |
84 |
| - if (tv.sec () != 0 || tv.usec () != 1000) |
85 |
| - { |
86 |
| - ACE_ERROR ((LM_ERROR, |
87 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
88 |
| - ACE_TEXT ("std::chrono::milliseconds (1) to an ACE_Time_Value. ") |
89 |
| - ACE_TEXT ("<sec=0,usec=1000> - got <sec=%d,usec=%d>\n"), |
90 |
| - tv.sec (), tv.usec ())); |
91 |
| - ++errors; |
92 |
| - } |
| 55 | + errors += tv_test_case(std::chrono::microseconds {1}, "microseconds (1)", 0, 1); |
93 | 56 |
|
94 |
| - tv = ACE_Time_Value { std::chrono::milliseconds {10005} }; |
95 |
| - if (tv.sec () != 10 || tv.usec () != 5000) |
96 | 57 | {
|
97 |
| - ACE_ERROR ((LM_ERROR, |
98 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
99 |
| - ACE_TEXT ("std::chrono::milliseconds (10005) to an ACE_Time_Value. ") |
100 |
| - ACE_TEXT ("<sec=10,usec=5000> - got <sec=%d,usec=%d>\n"), |
101 |
| - tv.sec (), tv.usec ())); |
102 |
| - ++errors; |
103 |
| - } |
| 58 | + ACE_Time_Value const tv = ACE_Time_Value { std::chrono::microseconds {10005} }; |
| 59 | + errors += tv_test_case(tv, "microseconds (10005)", 0, 10005); |
104 | 60 |
|
105 |
| - tv = ACE_Time_Value { std::chrono::seconds {1} }; |
106 |
| - if (tv.sec () != 1 || tv.usec () != 0) |
107 |
| - { |
108 |
| - ACE_ERROR ((LM_ERROR, |
109 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
110 |
| - ACE_TEXT ("std::chrono::seconds (1) to an ACE_Time_Value. ") |
111 |
| - ACE_TEXT ("<sec=1,usec=0> - got <sec=%d,usec=%d>\n"), |
112 |
| - tv.sec (), tv.usec ())); |
113 |
| - ++errors; |
| 61 | + std::chrono::milliseconds ms_test { tv.msec () }; |
| 62 | + if (ms_test.count () != 10) |
| 63 | + { |
| 64 | + ACE_ERROR ((LM_ERROR, |
| 65 | + ACE_TEXT ("(%P|%t) unexpected value after get_chrono_msec. ") |
| 66 | + ACE_TEXT ("Expected <10> - got <%q>\n"), |
| 67 | + ms_test.count ())); |
| 68 | + ++errors; |
| 69 | + } |
114 | 70 | }
|
115 | 71 |
|
116 |
| - tv = ACE_Time_Value { std::chrono::seconds {10005} }; |
117 |
| - if (tv.sec () != 10005 || tv.usec () != 0) |
118 |
| - { |
119 |
| - ACE_ERROR ((LM_ERROR, |
120 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
121 |
| - ACE_TEXT ("std::chrono::seconds (10005) to an ACE_Time_Value. ") |
122 |
| - ACE_TEXT ("<sec=10005,usec=0> - got <sec=%d,usec=%d>\n"), |
123 |
| - tv.sec (), tv.usec ())); |
124 |
| - ++errors; |
125 |
| - } |
| 72 | + errors += tv_test_case(std::chrono::milliseconds {1}, "milliseconds (1)", 0, 1000); |
126 | 73 |
|
127 |
| - tv = ACE_Time_Value { std::chrono::hours {1} }; |
128 |
| - if (tv.sec () != 3600 || tv.usec () != 0) |
129 |
| - { |
130 |
| - ACE_ERROR ((LM_ERROR, |
131 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
132 |
| - ACE_TEXT ("std::chrono::hours (1) to an ACE_Time_Value. ") |
133 |
| - ACE_TEXT ("<sec=3600,usec=0> - got <sec=%d,usec=%d>\n"), |
134 |
| - tv.sec (), tv.usec ())); |
135 |
| - ++errors; |
136 |
| - } |
| 74 | + errors += tv_test_case(std::chrono::milliseconds {10005}, "milliseconds (10005)", 10, 5000); |
137 | 75 |
|
138 |
| - tv = ACE_Time_Value { std::chrono::hours {10005} }; |
139 |
| - if (tv.sec () != 3600*10005 || tv.usec () != 0) |
140 |
| - { |
141 |
| - ACE_ERROR ((LM_ERROR, |
142 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
143 |
| - ACE_TEXT ("std::chrono::hours (10005) to an ACE_Time_Value. ") |
144 |
| - ACE_TEXT ("<sec=%d,usec=0> - got <sec=%d,usec=%d>\n"), |
145 |
| - 3600*10005, tv.sec (), tv.usec ())); |
146 |
| - ++errors; |
147 |
| - } |
| 76 | + errors += tv_test_case(std::chrono::seconds {1}, "seconds (1)", 1); |
| 77 | + |
| 78 | + errors += tv_test_case(std::chrono::seconds {10005}, "seconds (10005)", 10005); |
| 79 | + |
| 80 | + errors += tv_test_case(std::chrono::hours {1}, "hours (1)", 3600); |
| 81 | + |
| 82 | + errors += tv_test_case(std::chrono::hours {10005}, "hours (10005)", 3600*10005); |
| 83 | + |
| 84 | + // ACE_Time_Value should accept floating-point-based durations. |
| 85 | + std::chrono::duration<double, std::ratio<(24*3600)>> const half_day {0.5}; |
| 86 | + errors += tv_test_case(half_day, "duration<double, ratio<(24*3600)>>{0.5}", 3600*12, 0); |
| 87 | + errors += tv_test_case(std::chrono::duration<double> {0.1}, "duration<double>{0.1}", 0, 100000); |
| 88 | + errors += tv_test_case(std::chrono::duration<double> {-0.1}, "duration<double>{-0.1}", 0, -100000); |
| 89 | + // It being -99,999 instead of -100,000 seems to be a IEEE 754 thing |
| 90 | + errors += tv_test_case(std::chrono::duration<double> {-10.1}, "duration<double>{-10.1}", -10, -99999); |
148 | 91 |
|
149 | 92 | // Two times half a day, 3 hours, 24 minutes, 54 seconds, 238 milliseconds,
|
150 |
| - // 754 microseconds and 342 nanoseconds. |
151 |
| - std::chrono::duration<double, std::ratio<(24*3600)>> half_day {0.5}; |
152 |
| - std::chrono::microseconds const usec { |
153 |
| - 2 * ( |
154 |
| - std::chrono::duration_cast<std::chrono::microseconds> ( |
| 93 | + // 754 microseconds and 342 nanoseconds (lost). |
| 94 | + std::chrono::nanoseconds const nsec { |
| 95 | + 2 * std::chrono::duration_cast<std::chrono::nanoseconds> ( |
155 | 96 | half_day +
|
156 | 97 | std::chrono::hours {3} + std::chrono::minutes {24} +
|
157 | 98 | std::chrono::seconds {54} + std::chrono::milliseconds {238} +
|
158 |
| - std::chrono::microseconds {754} + std::chrono::nanoseconds {342})) |
| 99 | + std::chrono::microseconds {754} + std::chrono::nanoseconds {342}) |
159 | 100 | };
|
160 | 101 |
|
161 |
| - |
162 |
| - tv = ACE_Time_Value {usec}; |
163 |
| - |
164 | 102 | // half a day 3 hours 24 minutes 54 seconds
|
165 | 103 | time_t expected_sec = { ((12*3600) + (3*3600) + (24*60) + 54 ) * 2 };
|
166 |
| - // 238 milli usec 342 nano |
| 104 | + // 238 milli 754 usec 342 nano (lost) |
167 | 105 | suseconds_t expected_usec = { ((238*1000) + 754 + 0) * 2 };
|
| 106 | + errors += tv_test_case(nsec, |
| 107 | + "two times half a day, 3 hours, 24 minutes, 54 seconds, " |
| 108 | + "238 milliseconds, 754 microseconds and 342 nanoseconds (lost)", |
| 109 | + expected_sec, expected_usec); |
168 | 110 |
|
169 |
| - if (tv.sec () != expected_sec || tv.usec () != expected_usec) |
170 |
| - { |
171 |
| - ACE_ERROR ((LM_ERROR, |
172 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
173 |
| - ACE_TEXT ("two times half a day, 3 hours, 24 minutes, 54 seconds, ") |
174 |
| - ACE_TEXT ("238 milliseconds, 754 microseconds and 342 nanoseconds ") |
175 |
| - ACE_TEXT ("to an ACE_Time_Value. Expected <sec=%d,usec=%d> - ") |
176 |
| - ACE_TEXT ("got <sec=%d,usec=%d>\n"), |
177 |
| - expected_sec, expected_usec, tv.sec (), tv.usec ())); |
178 |
| - ++errors; |
179 |
| - } |
180 |
| - |
181 |
| - tv.set (std::chrono::milliseconds {1120}); |
182 |
| - if (tv.sec () != 1 || tv.usec () != 120 * std::kilo::num) |
183 |
| - { |
184 |
| - ACE_ERROR ((LM_ERROR, |
185 |
| - ACE_TEXT ("(%P|%t) unexpected value after converting ") |
186 |
| - ACE_TEXT ("a std::chrono::milliseconds of 1120 to an ACE_Time_Value ") |
187 |
| - ACE_TEXT ("Expected <sec=1,usec=120000> - got <sec=%d,usec=%d>\n"), |
188 |
| - tv.sec (), tv.usec ())); |
189 |
| - ++errors; |
190 |
| - } |
| 111 | + errors += tv_test_case(std::chrono::milliseconds {1120}, "milliseconds (1120)", 1, 120 * std::kilo::num); |
191 | 112 |
|
192 | 113 | return errors;
|
193 | 114 | }
|
@@ -381,29 +302,14 @@ test_ace_time_value_operators ()
|
381 | 302 | std::chrono::duration_cast<std::chrono::milliseconds>(sec) +
|
382 | 303 | std::chrono::duration_cast<std::chrono::milliseconds>(usec) };
|
383 | 304 |
|
384 |
| - |
385 | 305 | ACE_Time_Value tv;
|
386 | 306 | tv = msec;
|
387 | 307 | tv += std::chrono::milliseconds {300};
|
388 |
| - if (tv.sec () != 2 || tv.usec () != 303 * std::kilo::num) |
389 |
| - { |
390 |
| - ACE_ERROR ((LM_ERROR, |
391 |
| - ACE_TEXT ("(%P|%t) unexpected value after adding a duration ") |
392 |
| - ACE_TEXT ("of 300 ms. Expected <sec=2,usec=3300> - got <sec=%d,") |
393 |
| - ACE_TEXT ("usec=%d>.\n"), |
394 |
| - tv.sec (), tv.usec ())); |
395 |
| - ++errors; |
396 |
| - } |
| 308 | + errors += tv_test_case(tv, "seconds {2} + microseconds {3000} + milliseconds {300}", 2, 303 * std::kilo::num); |
| 309 | + |
397 | 310 | tv -= std::chrono::microseconds {400};
|
398 |
| - if (tv.sec () != 2 || tv.usec () != 302600) |
399 |
| - { |
400 |
| - ACE_ERROR ((LM_ERROR, |
401 |
| - ACE_TEXT ("(%P|%t) unexpected value after substracting a duration ") |
402 |
| - ACE_TEXT ("of 400 us. Expected <sec=2,usec=3300> - got <sec=%d,") |
403 |
| - ACE_TEXT ("usec=%d>.\n"), |
404 |
| - tv.sec (), tv.usec ())); |
405 |
| - ++errors; |
406 |
| - } |
| 311 | + errors += tv_test_case(tv, "seconds {2} + microseconds {3000} + milliseconds {300} - microseconds {400}", 2, 302600); |
| 312 | + |
407 | 313 | return errors;
|
408 | 314 | }
|
409 | 315 |
|
|
0 commit comments