|
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 | * |
|
21 | 20 | #include "ace/Truncate.h" |
22 | 21 |
|
23 | 22 | int |
24 | | -test_assignments () |
| 23 | +tv_test_case (const ACE_Time_Value& tv, const char *what, time_t expect_sec, suseconds_t expect_usec = 0) |
25 | 24 | { |
26 | | - int errors {}; |
27 | | - ACE_Time_Value tv { std::chrono::nanoseconds {100} }; |
28 | | - if (tv.sec () != 0 || tv.usec () != 0) |
| 25 | + if (tv.sec () != expect_sec || tv.usec () != expect_usec) |
29 | 26 | { |
30 | 27 | 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; |
| 28 | + ACE_TEXT ("(%P|%t) unexpected value after converting %C to an ACE_Time_Value. ") |
| 29 | + ACE_TEXT ("Expected <sec=%d,usec=%d> - got <sec=%d,usec=%d>\n"), |
| 30 | + what, expect_sec, expect_usec, tv.sec (), tv.usec ())); |
| 31 | + return 1; |
36 | 32 | } |
| 33 | + return 0; |
| 34 | +} |
37 | 35 |
|
38 | | - tv = ACE_Time_Value { std::chrono::nanoseconds {10005} }; |
39 | | - if (tv.sec () != 0 || tv.usec () != 10) |
40 | | - { |
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; |
47 | | - } |
| 36 | +template <class Rep, class Period> |
| 37 | +int |
| 38 | +tv_test_case (const std::chrono::duration<Rep, Period>& duration, |
| 39 | + const char *what, time_t expect_sec, suseconds_t expect_usec = 0) |
| 40 | +{ |
| 41 | + return tv_test_case (ACE_Time_Value {duration}, what, expect_sec, expect_usec); |
| 42 | +} |
48 | 43 |
|
49 | | - tv = ACE_Time_Value { std::chrono::microseconds {1} }; |
50 | | - if (tv.sec () != 0 || tv.usec () != 1) |
51 | | - { |
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; |
58 | | - } |
| 44 | +int |
| 45 | +test_assignments () |
| 46 | +{ |
| 47 | + int errors {}; |
59 | 48 |
|
60 | | - tv = ACE_Time_Value { std::chrono::microseconds {10005} }; |
61 | | - if (tv.sec () != 0 || tv.usec () != 10005) |
62 | | - { |
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; |
69 | | - } |
| 49 | + errors += tv_test_case(std::chrono::nanoseconds {100}, "nanoseconds (100)", 0); |
70 | 50 |
|
71 | | - std::chrono::milliseconds ms_test { tv.msec () }; |
72 | | - if (ms_test.count () != 10) |
73 | | - { |
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; |
79 | | - } |
| 51 | + errors += tv_test_case(std::chrono::nanoseconds {10005}, "nanoseconds (10005)", 0, 10); |
80 | 52 |
|
81 | | - tv = ACE_Time_Value { std::chrono::milliseconds {1} }; |
82 | | - if (tv.sec () != 0 || tv.usec () != 1000) |
83 | | - { |
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; |
90 | | - } |
| 53 | + errors += tv_test_case(std::chrono::microseconds {1}, "microseconds (1)", 0, 1); |
91 | 54 |
|
92 | | - tv = ACE_Time_Value { std::chrono::milliseconds {10005} }; |
93 | | - if (tv.sec () != 10 || tv.usec () != 5000) |
94 | 55 | { |
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; |
101 | | - } |
| 56 | + ACE_Time_Value const tv = ACE_Time_Value { std::chrono::microseconds {10005} }; |
| 57 | + errors += tv_test_case(tv, "microseconds (10005)", 0, 10005); |
102 | 58 |
|
103 | | - tv = ACE_Time_Value { std::chrono::seconds {1} }; |
104 | | - if (tv.sec () != 1 || tv.usec () != 0) |
105 | | - { |
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; |
| 59 | + std::chrono::milliseconds ms_test { tv.msec () }; |
| 60 | + if (ms_test.count () != 10) |
| 61 | + { |
| 62 | + ACE_ERROR ((LM_ERROR, |
| 63 | + ACE_TEXT ("(%P|%t) unexpected value after get_chrono_msec. ") |
| 64 | + ACE_TEXT ("Expected <10> - got <%q>\n"), |
| 65 | + ms_test.count ())); |
| 66 | + ++errors; |
| 67 | + } |
112 | 68 | } |
113 | 69 |
|
114 | | - tv = ACE_Time_Value { std::chrono::seconds {10005} }; |
115 | | - if (tv.sec () != 10005 || tv.usec () != 0) |
116 | | - { |
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; |
123 | | - } |
| 70 | + errors += tv_test_case(std::chrono::milliseconds {1}, "milliseconds (1)", 0, 1000); |
124 | 71 |
|
125 | | - tv = ACE_Time_Value { std::chrono::hours {1} }; |
126 | | - if (tv.sec () != 3600 || tv.usec () != 0) |
127 | | - { |
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; |
134 | | - } |
| 72 | + errors += tv_test_case(std::chrono::milliseconds {10005}, "milliseconds (10005)", 10, 5000); |
135 | 73 |
|
136 | | - tv = ACE_Time_Value { std::chrono::hours {10005} }; |
137 | | - if (tv.sec () != 3600*10005 || tv.usec () != 0) |
138 | | - { |
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; |
145 | | - } |
| 74 | + errors += tv_test_case(std::chrono::seconds {1}, "seconds (1)", 1); |
| 75 | + |
| 76 | + errors += tv_test_case(std::chrono::seconds {10005}, "seconds (10005)", 10005); |
| 77 | + |
| 78 | + errors += tv_test_case(std::chrono::hours {1}, "hours (1)", 3600); |
| 79 | + |
| 80 | + errors += tv_test_case(std::chrono::hours {10005}, "hours (10005)", 3600*10005); |
| 81 | + |
| 82 | + // ACE_Time_Value should accept floating-point-based durations. |
| 83 | + std::chrono::duration<double, std::ratio<(24*3600)>> const half_day {0.5}; |
| 84 | + errors += tv_test_case(half_day, "duration<double, ratio<(24*3600)>>{0.5}", 3600*12, 0); |
| 85 | + errors += tv_test_case(std::chrono::duration<double> {0.1}, "duration<double>{0.1}", 0, 100000); |
| 86 | + errors += tv_test_case(std::chrono::duration<double> {-0.1}, "duration<double>{-0.1}", 0, -100000); |
| 87 | + // It being -99,999 instead of -100,000 seems to be a IEEE 754 thing |
| 88 | + errors += tv_test_case(std::chrono::duration<double> {-10.1}, "duration<double>{-10.1}", -10, -99999); |
146 | 89 |
|
147 | 90 | // 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> ( |
| 91 | + // 754 microseconds and 342 nanoseconds (lost). |
| 92 | + std::chrono::nanoseconds const nsec { |
| 93 | + 2 * std::chrono::duration_cast<std::chrono::nanoseconds> ( |
153 | 94 | half_day + |
154 | 95 | std::chrono::hours {3} + std::chrono::minutes {24} + |
155 | 96 | std::chrono::seconds {54} + std::chrono::milliseconds {238} + |
156 | | - std::chrono::microseconds {754} + std::chrono::nanoseconds {342})) |
| 97 | + std::chrono::microseconds {754} + std::chrono::nanoseconds {342}) |
157 | 98 | }; |
158 | 99 |
|
159 | | - |
160 | | - tv = ACE_Time_Value {usec}; |
161 | | - |
162 | 100 | // half a day 3 hours 24 minutes 54 seconds |
163 | 101 | time_t expected_sec = { ((12*3600) + (3*3600) + (24*60) + 54 ) * 2 }; |
164 | | - // 238 milli usec 342 nano |
| 102 | + // 238 milli 754 usec 342 nano (lost) |
165 | 103 | suseconds_t expected_usec = { ((238*1000) + 754 + 0) * 2 }; |
| 104 | + errors += tv_test_case(nsec, |
| 105 | + "two times half a day, 3 hours, 24 minutes, 54 seconds, " |
| 106 | + "238 milliseconds, 754 microseconds and 342 nanoseconds (lost)", |
| 107 | + expected_sec, expected_usec); |
166 | 108 |
|
167 | | - if (tv.sec () != expected_sec || tv.usec () != expected_usec) |
168 | | - { |
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; |
177 | | - } |
178 | | - |
179 | | - tv.set (std::chrono::milliseconds {1120}); |
180 | | - if (tv.sec () != 1 || tv.usec () != 120 * std::kilo::num) |
181 | | - { |
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; |
188 | | - } |
| 109 | + errors += tv_test_case(std::chrono::milliseconds {1120}, "milliseconds (1120)", 1, 120 * std::kilo::num); |
189 | 110 |
|
190 | 111 | return errors; |
191 | 112 | } |
@@ -378,29 +299,14 @@ test_ace_time_value_operators () |
378 | 299 | std::chrono::duration_cast<std::chrono::milliseconds>(sec) + |
379 | 300 | std::chrono::duration_cast<std::chrono::milliseconds>(usec) }; |
380 | 301 |
|
381 | | - |
382 | 302 | ACE_Time_Value tv; |
383 | 303 | tv = msec; |
384 | 304 | tv += std::chrono::milliseconds {300}; |
385 | | - if (tv.sec () != 2 || tv.usec () != 303 * std::kilo::num) |
386 | | - { |
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; |
393 | | - } |
| 305 | + errors += tv_test_case(tv, "seconds {2} + microseconds {3000} + milliseconds {300}", 2, 303 * std::kilo::num); |
| 306 | + |
394 | 307 | tv -= std::chrono::microseconds {400}; |
395 | | - if (tv.sec () != 2 || tv.usec () != 302600) |
396 | | - { |
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; |
403 | | - } |
| 308 | + errors += tv_test_case(tv, "seconds {2} + microseconds {3000} + milliseconds {300} - microseconds {400}", 2, 302600); |
| 309 | + |
404 | 310 | return errors; |
405 | 311 | } |
406 | 312 |
|
|
0 commit comments