From: Florian Forster Date: Fri, 21 Aug 2015 11:34:49 +0000 (+0200) Subject: src/daemon/utils_time_test.c: Test ms conversion. X-Git-Tag: collectd-5.6.0~610 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=8eecec122510b686ec6d1279041e0794615084a5;p=collectd.git src/daemon/utils_time_test.c: Test ms conversion. The CDTIME_T_TO_MS() macro is used by all plugins using the cURL library, so we should properly test this macro, too. --- diff --git a/src/daemon/utils_time_test.c b/src/daemon/utils_time_test.c index 0c527290..eaf72a6c 100644 --- a/src/daemon/utils_time_test.c +++ b/src/daemon/utils_time_test.c @@ -36,51 +36,53 @@ DEF_TEST(conversion) cdtime_t t; double d; time_t tt; + uint64_t ms; struct timeval tv; struct timespec ts; } cases[] = { - /* cdtime double time_t timeval timespec */ - { 0, 0.0 , 0, { 0, 0}, { 0, 0}}, - { 10737418240, 10.0 , 10, { 10, 0}, { 10, 0}}, - {1542908534771941376, 1436945549.0 , 1436945549, {1436945549, 0}, {1436945549, 0}}, - {1542908535540740522, 1436945549.716, 1436945550, {1436945549, 716000}, {1436945549, 716000000}}, + /* cdtime double time_t milliseconds timeval timespec */ + { 0, 0.0 , 0, 0, { 0, 0}, { 0, 0}}, + { 10737418240, 10.0 , 10, 10000, { 10, 0}, { 10, 0}}, + {1542908534771941376, 1436945549.0 , 1436945549, 1436945549000, {1436945549, 0}, {1436945549, 0}}, + {1542908535540740522, 1436945549.716, 1436945550, 1436945549716, {1436945549, 716000}, {1436945549, 716000000}}, // 1426076671.123 * 2^30 = 1531238166015458148.352 - {1531238166015458148, 1426076671.123, 1426076671, {1426076671, 123000}, {1426076671, 123000000}}, + {1531238166015458148, 1426076671.123, 1426076671, 1426076671123, {1426076671, 123000}, {1426076671, 123000000}}, // 1426076681.234 * 2^30 = 1531238176872061730.816 - {1531238176872061731, 1426076681.234, 1426076681, {1426076681, 234000}, {1426076681, 234000000}}, + {1531238176872061731, 1426076681.234, 1426076681, 1426076681234, {1426076681, 234000}, {1426076681, 234000000}}, // 1426083986.314 * 2^30 = 1531246020641985396.736 - {1531246020641985397, 1426083986.314, 1426083986, {1426083986, 314000}, {1426083986, 314000000}}, + {1531246020641985397, 1426083986.314, 1426083986, 1426083986314, {1426083986, 314000}, {1426083986, 314000000}}, // 1426083986.494142531 * 2^30 = 1531246020835411966.5 - {1531246020835411967, 1426083986.494, 1426083986, {1426083986, 494143}, {1426083986, 494142531}}, + {1531246020835411967, 1426083986.494, 1426083986, 1426083986494, {1426083986, 494143}, {1426083986, 494142531}}, // 1426083986.987410814 * 2^30 = 1531246021365054752.4 - {1531246021365054752, 1426083986.987, 1426083987, {1426083986, 987411}, {1426083986, 987410814}}, + {1531246021365054752, 1426083986.987, 1426083987, 1426083986987, {1426083986, 987411}, {1426083986, 987410814}}, /* These cases test the cdtime_t -> ns conversion rounds correctly. */ // 1546167635576736987 / 2^30 = 1439980823.1524536265... - {1546167635576736987, 1439980823.152, 1439980823, {1439980823, 152454}, {1439980823, 152453627}}, + {1546167635576736987, 1439980823.152, 1439980823, 1439980823152, {1439980823, 152454}, {1439980823, 152453627}}, // 1546167831554815222 / 2^30 = 1439981005.6712620165... - {1546167831554815222, 1439981005.671, 1439981006, {1439981005, 671262}, {1439981005, 671262017}}, + {1546167831554815222, 1439981005.671, 1439981006, 1439981005671, {1439981005, 671262}, {1439981005, 671262017}}, // 1546167986577716567 / 2^30 = 1439981150.0475896215... - {1546167986577716567, 1439981150.048, 1439981150, {1439981150, 47590}, {1439981005, 47589622}}, + {1546167986577716567, 1439981150.048, 1439981150, 1439981150048, {1439981150, 47590}, {1439981005, 47589622}}, }; size_t i; for (i = 0; i < (sizeof (cases) / sizeof (cases[0])); i++) { struct timeval tv; struct timespec ts; - time_t tt; - // cdtime -> ns - CDTIME_T_TO_TIMESPEC (cases[i].t, &ts); - EXPECT_EQ_UINT64 ((uint64_t) cases[i].ts.tv_nsec, (uint64_t) ts.tv_nsec); + // cdtime -> s + EXPECT_EQ ((unsigned long) cases[i].tt, (unsigned long) CDTIME_T_TO_TIME_T (cases[i].t), "%lu"); + + // cdtime -> ms + EXPECT_EQ_UINT64 ((uint64_t) cases[i].ms, CDTIME_T_TO_MS (cases[i].t)); // cdtime -> us CDTIME_T_TO_TIMEVAL (cases[i].t, &tv); EXPECT_EQ_UINT64 ((uint64_t) cases[i].tv.tv_usec, (uint64_t) tv.tv_usec); - // cdtime -> s - tt = CDTIME_T_TO_TIME_T (cases[i].t); - EXPECT_EQ_UINT64 ((uint64_t) cases[i].tt, (uint64_t) tt); + // cdtime -> ns + CDTIME_T_TO_TIMESPEC (cases[i].t, &ts); + EXPECT_EQ_UINT64 ((uint64_t) cases[i].ts.tv_nsec, (uint64_t) ts.tv_nsec); // cdtime -> double DBLEQ (cases[i].d, CDTIME_T_TO_DOUBLE (cases[i].t));