Skip to content

Commit 61ca42f

Browse files
authored
Merge pull request #2462 from iguessthislldo/igtd/fp-duration-to-time-value
`ACE_Time_Value` Should Accept Floating-point-based `std::chrono::duration`
2 parents 0245b63 + addc542 commit 61ca42f

4 files changed

Lines changed: 86 additions & 181 deletions

File tree

ACE/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
USER VISIBLE CHANGES BETWEEN ACE-8.0.5 and ACE-8.0.6
22
====================================================
33

4+
. Support floating-point-based `std::chrono::duration` in `ACE_Time_Value`
5+
46
USER VISIBLE CHANGES BETWEEN ACE-8.0.4 and ACE-8.0.5
57
====================================================
68

ACE/ace/Time_Value.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,13 @@ class ACE_Export ACE_Time_Value
118118
template< class Rep, class Period >
119119
void set (const std::chrono::duration<Rep, Period>& duration)
120120
{
121-
std::chrono::seconds const s {
122-
std::chrono::duration_cast<std::chrono::seconds> (duration)};
121+
using std::chrono::seconds;
122+
using std::chrono::microseconds;
123+
using std::chrono::duration_cast;
123124

124-
std::chrono::microseconds const usec {
125-
std::chrono::duration_cast<std::chrono::microseconds>(
126-
duration % std::chrono::seconds (1))};
127-
this->set (ACE_Utils::truncate_cast<time_t>(s.count ()), ACE_Utils::truncate_cast<suseconds_t>(usec.count ()));
125+
auto const sec = duration_cast<seconds> (duration);
126+
auto const usec = duration_cast<microseconds> (duration - sec);
127+
this->set (ACE_Utils::truncate_cast<time_t> (sec.count ()), ACE_Utils::truncate_cast<suseconds_t> (usec.count ()));
128128
}
129129

130130
/// Converts from ACE_Time_Value format into milliseconds format.
@@ -272,11 +272,7 @@ class ACE_Export ACE_Time_Value
272272
template< class Rep, class Period >
273273
ACE_Time_Value &operator += (const std::chrono::duration<Rep, Period>& duration)
274274
{
275-
const ACE_Time_Value tv (duration);
276-
this->sec (this->sec () + tv.sec ());
277-
this->usec (this->usec () + tv.usec ());
278-
this->normalize ();
279-
return *this;
275+
return *this += ACE_Time_Value (duration);
280276
}
281277

282278
/// Assign @a std::duration to this
@@ -291,11 +287,7 @@ class ACE_Export ACE_Time_Value
291287
template< class Rep, class Period >
292288
ACE_Time_Value &operator -= (const std::chrono::duration<Rep, Period>& duration)
293289
{
294-
const ACE_Time_Value tv (duration);
295-
this->sec (this->sec () - tv.sec ());
296-
this->usec (this->usec () - tv.usec ());
297-
this->normalize ();
298-
return *this;
290+
return *this -= ACE_Time_Value (duration);
299291
}
300292

301293
/**

ACE/tests/.gitignore

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@
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
103+
/Compiler_Features_42_Test
97104
/Config_Test
98105
/Conn_Test
99106
/Date_Time_Test
@@ -110,6 +117,7 @@
110117
/FlReactor_Test
111118
/Framework_Component_Test
112119
/Future_Set_Test
120+
/Future_Stress_Test
113121
/Future_Test
114122
/Get_Opt_Test
115123
/Handle_Set_Test
@@ -152,6 +160,7 @@
152160
/MT_Reference_Counted_Event_Handler_Test
153161
/MT_Reference_Counted_Notify_Test
154162
/MT_SOCK_Test
163+
/Multicast_Interfaces_Test
155164
/Multicast_Test
156165
/Multicast_Test_IPV6
157166
/Multihomed_INET_Addr_Test
@@ -216,8 +225,9 @@
216225
/Signal_Test
217226
/Sigset_Ops_Test
218227
/Simple_Message_Block_Test
219-
/Singleton_Test
220228
/Singleton_Restart_Test
229+
/Singleton_Test
230+
/SOCK_Acceptor_Test
221231
/SOCK_Connector_Test
222232
/SOCK_Dgram_Bcast_Test
223233
/SOCK_Dgram_Test
@@ -256,8 +266,8 @@
256266
/Token_Strategy_Test
257267
/Tokens_Test
258268
/TP_Reactor_Test
259-
/TSS_Static_Test
260269
/TSS_Leak_Test
270+
/TSS_Static_Test
261271
/TSS_Test
262272
/Unbounded_Set_Test
263273
/Unbounded_Set_Test_Ex
@@ -273,8 +283,3 @@
273283
/XtAthenaReactor_Test
274284
/XtMotifReactor_Test
275285
/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
*
@@ -21,171 +20,93 @@
2120
#include "ace/Truncate.h"
2221

2322
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)
2524
{
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)
2926
{
3027
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;
3632
}
33+
return 0;
34+
}
3735

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

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

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

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

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

92-
tv = ACE_Time_Value { std::chrono::milliseconds {10005} };
93-
if (tv.sec () != 10 || tv.usec () != 5000)
9455
{
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);
10258

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+
}
11268
}
11369

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

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

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

14790
// 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> (
15394
half_day +
15495
std::chrono::hours {3} + std::chrono::minutes {24} +
15596
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})
15798
};
15899

159-
160-
tv = ACE_Time_Value {usec};
161-
162100
// half a day 3 hours 24 minutes 54 seconds
163101
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)
165103
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);
166108

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

190111
return errors;
191112
}
@@ -378,29 +299,14 @@ test_ace_time_value_operators ()
378299
std::chrono::duration_cast<std::chrono::milliseconds>(sec) +
379300
std::chrono::duration_cast<std::chrono::milliseconds>(usec) };
380301

381-
382302
ACE_Time_Value tv;
383303
tv = msec;
384304
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+
394307
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+
404310
return errors;
405311
}
406312

0 commit comments

Comments
 (0)