20 using namespace folly;
29 template <
typename Clock,
typename Duration>
30 typename Clock::time_point createTimePoint(
const Duration& d) {
31 return typename Clock::time_point(
32 std::chrono::duration_cast<typename Clock::duration>(d));
36 TEST(Conv, timespecToStdChrono) {
48 EXPECT_EQ(1000000010ns, to<nanoseconds>(ts));
53 createTimePoint<system_clock>(1000000010ns),
54 to<system_clock::time_point>(ts));
56 createTimePoint<steady_clock>(1000000010ns),
57 to<steady_clock::time_point>(ts));
61 ts.tv_nsec = 3219876543;
69 createTimePoint<system_clock>(nanoseconds(8219876543LL)),
70 to<system_clock::time_point>(ts));
72 createTimePoint<steady_clock>(nanoseconds(8219876543LL)),
73 to<steady_clock::time_point>(ts));
80 EXPECT_EQ(-4999876544, duration_cast<nanoseconds>(-5
s + 123456ns).
count());
82 EXPECT_EQ(-4999876, duration_cast<microseconds>(-5
s + 123456ns).
count());
92 duration_cast<hours>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec})
99 duration_cast<hours>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec})
106 duration_cast<hours>(seconds{ts.tv_sec} + nanoseconds{ts.tv_nsec})
111 ts.tv_nsec = 500000000;
114 ts.tv_nsec = 500000000;
117 ts.tv_nsec = -500000000;
120 ts.tv_nsec = 500000000;
121 auto doubleNanos = to<duration<double, std::nano>>(ts);
122 EXPECT_EQ(1500000000, doubleNanos.count());
125 auto doubleMinutes = to<duration<double, std::ratio<60>>>(ts);
130 using five_sevenths = std::chrono::duration<int64_t, std::ratio<5, 7>>;
135 ts.tv_nsec = 428571500;
138 using thirteen_thirds = std::chrono::duration<double, std::ratio<13, 3>>;
147 TEST(Conv, timespecToStdChronoOverflow) {
152 if (!std::is_same<decltype(ts.tv_sec),
int64_t>::
value) {
153 LOG(
INFO) <<
"skipping most overflow tests: time_t is not int64_t";
156 using nsec_u64 = std::chrono::duration<uint64_t, std::nano>;
157 ts.tv_sec = 18446744073;
158 ts.tv_nsec = 709551615;
173 using usec_i64 = std::chrono::duration<int64_t, std::micro>;
174 ts.tv_sec = 9223372036854LL;
175 ts.tv_nsec = 775807000;
182 ts.tv_sec = -9223372036855LL;
183 ts.tv_nsec = 224192000;
190 using sec_i32 = std::chrono::duration<int32_t>;
191 ts.tv_sec = 2147483647;
194 ts.tv_nsec = 1000000000;
196 ts.tv_sec = -2147483648;
199 ts.tv_sec = -2147483649;
200 ts.tv_nsec = 999999999;
202 ts.tv_sec = -2147483649;
205 ts.tv_sec = -2147483650;
210 using hours_u32 = std::chrono::duration<uint32_t, std::ratio<3600>>;
211 ts.tv_sec = 15461882262000LL;
214 ts.tv_sec = 15461882265599LL;
216 ts.tv_sec = 15461882265600LL;
219 using nsec_i64 = std::chrono::duration<int64_t, std::nano>;
253 using hours_u64 = std::chrono::duration<uint64_t, std::ratio<3600>>;
254 ts.tv_sec = std::numeric_limits<decltype(ts.tv_sec)>
::max();
255 ts.tv_nsec = 1000000000;
258 ts.tv_nsec = 999999999;
260 std::numeric_limits<decltype(ts.tv_sec)>::
max() / 3600,
261 to<hours_u64>(ts).
count());
265 using unusual_time = std::chrono::duration<int16_t, std::ratio<13, 3>>;
267 ts.tv_nsec = 666666666;
269 ts.tv_nsec = 666666667;
273 ts.tv_nsec = 999999999;
280 TEST(Conv, timevalToStdChrono) {
292 EXPECT_EQ(1000010000ns, to<nanoseconds>(tv));
297 createTimePoint<system_clock>(1000010000ns),
298 to<system_clock::time_point>(tv));
300 createTimePoint<steady_clock>(1000010000ns),
301 to<steady_clock::time_point>(tv));
305 tv.tv_usec = 3219876;
311 createTimePoint<system_clock>(nanoseconds(8219876000LL)),
312 to<system_clock::time_point>(tv));
314 createTimePoint<steady_clock>(nanoseconds(8219876000LL)),
315 to<steady_clock::time_point>(tv));
318 if (std::numeric_limits<decltype(tv.tv_sec)>::
max() >=
322 using nsec_i64 = std::chrono::duration<int64_t, std::nano>;
323 tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>
::max();
324 tv.tv_usec = std::numeric_limits<decltype(tv.tv_usec)>
::max();
327 tv.tv_sec = std::numeric_limits<decltype(tv.tv_sec)>
::min();
328 tv.tv_usec = std::numeric_limits<decltype(tv.tv_usec)>
::max();
333 TEST(Conv, stdChronoToTimespec) {
334 auto ts = to<struct timespec>(10ns);
340 ts = to<struct timespec>(nanoseconds(987654321012LL));
344 ts = to<struct timespec>(nanoseconds(-987654321012LL));
348 ts = to<struct timespec>(microseconds(987654321012LL));
352 ts = to<struct timespec>(milliseconds(987654321012LL));
356 ts = to<struct timespec>(seconds(987654321012LL));
360 ts = to<struct timespec>(10
h);
364 ts = to<struct timespec>(createTimePoint<steady_clock>(123ns));
368 ts = to<struct timespec>(createTimePoint<system_clock>(123ns));
374 using five_sevenths = std::chrono::duration<int64_t, std::ratio<5, 7>>;
375 ts = to<struct timespec>(five_sevenths(7));
378 ts = to<struct timespec>(five_sevenths(19));
381 using seven_fifths = std::chrono::duration<int64_t, std::ratio<7, 5>>;
382 ts = to<struct timespec>(seven_fifths(5));
387 TEST(Conv, stdChronoToTimespecOverflow) {
391 if (!std::is_same<decltype(ts.tv_sec),
int64_t>::
value) {
392 LOG(
INFO) <<
"skipping most overflow tests: time_t is not int64_t";
395 using sec_u64 = duration<uint64_t>;
396 ts = to<struct timespec>(sec_u64(9223372036854775807ULL));
397 EXPECT_EQ(ts.tv_sec, 9223372036854775807ULL);
401 to<struct timespec>(sec_u64(9223372036854775808ULL)), std::range_error);
404 using hours_i64 = duration<int64_t, std::ratio<3600>>;
405 ts = to<struct timespec>(hours_i64(2562047788015215LL));
406 EXPECT_EQ(ts.tv_sec, 9223372036854774000LL);
409 to<struct timespec>(hours_i64(2562047788015216LL)), std::range_error);
413 using three_halves = std::chrono::duration<uint64_t, std::ratio<3, 2>>;
415 to<struct timespec>(three_halves(6148914691236517206ULL)),
422 using hours_timet = std::chrono::duration<time_t, std::ratio<3600>>;
428 TEST(Conv, stdChronoToTimeval) {
429 auto tv = to<struct timeval>(10ns);
433 tv = to<struct timeval>(10
us);
437 tv = to<struct timeval>(nanoseconds(987654321012LL));
441 tv = to<struct timeval>(nanoseconds(-987654321012LL));
445 tv = to<struct timeval>(microseconds(987654321012LL));
449 tv = to<struct timeval>(milliseconds(987654321012LL));
453 tv = to<struct timeval>(seconds(987654321012LL));
458 tv = to<struct timeval>(duration<double>{3.456789});
461 tv = to<struct timeval>(duration<double>{-3.456789});
466 tv = to<struct timeval>(duration<double, std::ratio<3600>>{3.456789});
471 tv = to<struct timeval>(duration<double, std::ratio<3600>>{-3.456789});
476 tv = to<struct timeval>(duration<double, std::milli>{9123.456789});
479 tv = to<struct timeval>(duration<double, std::milli>{-9123.456789});
483 tv = to<struct timeval>(duration<uint32_t, std::ratio<3600>>{3});
487 tv = to<struct timeval>(duration<uint32_t, std::nano>{3123});
490 tv = to<struct timeval>(duration<int32_t, std::nano>{-3123});
494 tv = to<struct timeval>(createTimePoint<steady_clock>(123
us));
498 tv = to<struct timeval>(createTimePoint<system_clock>(123
us));
#define EXPECT_THROW(statement, expected_exception)
#define EXPECT_EQ(val1, val2)
—— Concurrent Priority Queue Implementation ——
std::chrono::milliseconds Duration
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
#define EXPECT_NEAR(val1, val2, abs_error)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
TEST(SequencedExecutor, CPUThreadPoolExecutor)
static unordered_set< string > us