Skip to content

Commit 2a2f2ee

Browse files
authored
Merge pull request #2465 from iguessthislldo/igtd/fp-duration-to-time-value62
[ACE6] `ACE_Time_Value` Should Accept Floating-point-based `std::chrono::duration`
2 parents 45decd5 + 340fd6c commit 2a2f2ee

File tree

4 files changed

+85
-181
lines changed

4 files changed

+85
-181
lines changed

ACE/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ USER VISIBLE CHANGES BETWEEN ACE-6.5.22 and ACE-6.5.23
77

88
. Updated thread name support to work with older Windows versions
99

10+
. Support floating-point-based `std::chrono::duration` in `ACE_Time_Value`
11+
1012
USER VISIBLE CHANGES BETWEEN ACE-6.5.21 and ACE-6.5.22
1113
======================================================
1214

ACE/ace/Time_Value.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,13 @@ class ACE_Export ACE_Time_Value
126126
template< class Rep, class Period >
127127
void set (const std::chrono::duration<Rep, Period>& duration)
128128
{
129-
std::chrono::seconds const s {
130-
std::chrono::duration_cast<std::chrono::seconds> (duration)};
129+
using std::chrono::seconds;
130+
using std::chrono::microseconds;
131+
using std::chrono::duration_cast;
131132

132-
std::chrono::microseconds const usec {
133-
std::chrono::duration_cast<std::chrono::microseconds>(
134-
duration % std::chrono::seconds (1))};
135-
this->set (ACE_Utils::truncate_cast<time_t>(s.count ()), ACE_Utils::truncate_cast<suseconds_t>(usec.count ()));
133+
auto const sec = duration_cast<seconds> (duration);
134+
auto const usec = duration_cast<microseconds> (duration - sec);
135+
this->set (ACE_Utils::truncate_cast<time_t> (sec.count ()), ACE_Utils::truncate_cast<suseconds_t> (usec.count ()));
136136
}
137137
#endif /* ACE_HAS_CPP11 */
138138

@@ -284,11 +284,7 @@ class ACE_Export ACE_Time_Value
284284
template< class Rep, class Period >
285285
ACE_Time_Value &operator += (const std::chrono::duration<Rep, Period>& duration)
286286
{
287-
const ACE_Time_Value tv (duration);
288-
this->sec (this->sec () + tv.sec ());
289-
this->usec (this->usec () + tv.usec ());
290-
this->normalize ();
291-
return *this;
287+
return *this += ACE_Time_Value (duration);
292288
}
293289

294290
/// Assign @a std::duration to this
@@ -303,11 +299,7 @@ class ACE_Export ACE_Time_Value
303299
template< class Rep, class Period >
304300
ACE_Time_Value &operator -= (const std::chrono::duration<Rep, Period>& duration)
305301
{
306-
const ACE_Time_Value tv (duration);
307-
this->sec (this->sec () - tv.sec ());
308-
this->usec (this->usec () - tv.usec ());
309-
this->normalize ();
310-
return *this;
302+
return *this -= ACE_Time_Value (duration);
311303
}
312304
#endif /* ACE_HAS_CPP11 */
313305

ACE/tests/.gitignore

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
/Compiler_Features_33_Test
9595
/Compiler_Features_34_Test
9696
/Compiler_Features_35_Test
97+
/Compiler_Features_36_Test
98+
/Compiler_Features_37_Test
99+
/Compiler_Features_38_Test
100+
/Compiler_Features_39_Test
101+
/Compiler_Features_40_Test
102+
/Compiler_Features_41_Test
97103
/Config_Test
98104
/Conn_Test
99105
/Date_Time_Test
@@ -110,6 +116,7 @@
110116
/FlReactor_Test
111117
/Framework_Component_Test
112118
/Future_Set_Test
119+
/Future_Stress_Test
113120
/Future_Test
114121
/Get_Opt_Test
115122
/Handle_Set_Test
@@ -152,6 +159,7 @@
152159
/MT_Reference_Counted_Event_Handler_Test
153160
/MT_Reference_Counted_Notify_Test
154161
/MT_SOCK_Test
162+
/Multicast_Interfaces_Test
155163
/Multicast_Test
156164
/Multicast_Test_IPV6
157165
/Multihomed_INET_Addr_Test
@@ -216,8 +224,9 @@
216224
/Signal_Test
217225
/Sigset_Ops_Test
218226
/Simple_Message_Block_Test
219-
/Singleton_Test
220227
/Singleton_Restart_Test
228+
/Singleton_Test
229+
/SOCK_Acceptor_Test
221230
/SOCK_Connector_Test
222231
/SOCK_Dgram_Bcast_Test
223232
/SOCK_Dgram_Test
@@ -256,8 +265,8 @@
256265
/Token_Strategy_Test
257266
/Tokens_Test
258267
/TP_Reactor_Test
259-
/TSS_Static_Test
260268
/TSS_Leak_Test
269+
/TSS_Static_Test
261270
/TSS_Test
262271
/Unbounded_Set_Test
263272
/Unbounded_Set_Test_Ex
@@ -273,8 +282,3 @@
273282
/XtAthenaReactor_Test
274283
/XtMotifReactor_Test
275284
/XtReactor_Test
276-
/Compiler_Features_36_Test
277-
/Compiler_Features_37_Test
278-
/Compiler_Features_38_Test
279-
/SOCK_Acceptor_Test
280-
Multicast_Interfaces_Test

ACE/tests/Chrono_Test.cpp

Lines changed: 64 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*
66
* This is a test of the usage of 'std::chrono' throughout ACE
77
* The following items are tested:
8-
* - ACE_OS::sleep
98
* - ACE_Time_Value
109
*
1110
*
@@ -23,171 +22,93 @@
2322
#include "ace/Truncate.h"
2423

2524
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)
2726
{
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)
3128
{
3229
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;
3834
}
35+
return 0;
36+
}
3937

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+
}
5045

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 {};
6150

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);
7252

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);
8254

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);
9356

94-
tv = ACE_Time_Value { std::chrono::milliseconds {10005} };
95-
if (tv.sec () != 10 || tv.usec () != 5000)
9657
{
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);
10460

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+
}
11470
}
11571

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);
12673

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);
13775

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);
14891

14992
// 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> (
15596
half_day +
15697
std::chrono::hours {3} + std::chrono::minutes {24} +
15798
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})
159100
};
160101

161-
162-
tv = ACE_Time_Value {usec};
163-
164102
// half a day 3 hours 24 minutes 54 seconds
165103
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)
167105
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);
168110

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);
191112

192113
return errors;
193114
}
@@ -381,29 +302,14 @@ test_ace_time_value_operators ()
381302
std::chrono::duration_cast<std::chrono::milliseconds>(sec) +
382303
std::chrono::duration_cast<std::chrono::milliseconds>(usec) };
383304

384-
385305
ACE_Time_Value tv;
386306
tv = msec;
387307
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+
397310
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+
407313
return errors;
408314
}
409315

0 commit comments

Comments
 (0)