2 * collectd - src/daemon/utils_time_test.c
3 * Copyright (C) 2015 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
27 #define DBL_PRECISION 1e-3
31 #include "utils_time.h"
42 /* cdtime double time_t timeval timespec */
43 { 0, 0.0 , 0, { 0, 0}, { 0, 0}},
44 { 10737418240, 10.0 , 10, { 10, 0}, { 10, 0}},
45 {1542908534771941376, 1436945549.0 , 1436945549, {1436945549, 0}, {1436945549, 0}},
46 {1542908535540740522, 1436945549.716, 1436945550, {1436945549, 716000}, {1436945549, 716000000}},
47 // 1426076671.123 * 2^30 = 1531238166015458148.352
48 {1531238166015458148, 1426076671.123, 1426076671, {1426076671, 123000}, {1426076671, 123000000}},
49 // 1426076681.234 * 2^30 = 1531238176872061730.816
50 {1531238176872061731, 1426076681.234, 1426076681, {1426076681, 234000}, {1426076681, 234000000}},
51 // 1426083986.314 * 2^30 = 1531246020641985396.736
52 {1531246020641985397, 1426083986.314, 1426083986, {1426083986, 314000}, {1426083986, 314000000}},
53 // 1426083986.494142531 * 2^30 = 1531246020835411966.5
54 {1531246020835411967, 1426083986.494, 1426083986, {1426083986, 494143}, {1426083986, 494142531}},
55 // 1426083986.987410814 * 2^30 = 1531246021365054752.4
56 {1531246021365054752, 1426083986.987, 1426083987, {1426083986, 987411}, {1426083986, 987410814}},
58 /* These cases test the cdtime_t -> ns conversion rounds correctly. */
59 // 1546167635576736987 / 2^30 = 1439980823.1524536265...
60 {1546167635576736987, 1439980823.152, 1439980823, {1439980823, 152454}, {1439980823, 152453627}},
61 // 1546167831554815222 / 2^30 = 1439981005.6712620165...
62 {1546167831554815222, 1439981005.671, 1439981006, {1439981005, 671262}, {1439981005, 671262017}},
63 // 1546167986577716567 / 2^30 = 1439981150.0475896215...
64 {1546167986577716567, 1439981150.048, 1439981150, {1439981150, 47590}, {1439981005, 47589622}},
68 for (i = 0; i < (sizeof (cases) / sizeof (cases[0])); i++) {
74 CDTIME_T_TO_TIMESPEC (cases[i].t, &ts);
75 EXPECT_EQ_UINT64 ((uint64_t) cases[i].ts.tv_nsec, (uint64_t) ts.tv_nsec);
78 CDTIME_T_TO_TIMEVAL (cases[i].t, &tv);
79 EXPECT_EQ_UINT64 ((uint64_t) cases[i].tv.tv_usec, (uint64_t) tv.tv_usec);
82 tt = CDTIME_T_TO_TIME_T (cases[i].t);
83 EXPECT_EQ_UINT64 ((uint64_t) cases[i].tt, (uint64_t) tt);
86 DBLEQ (cases[i].d, CDTIME_T_TO_DOUBLE (cases[i].t));
92 /* These cases test the ns -> cdtime_t conversion rounds correctly. */
93 DEF_TEST(ns_to_cdtime)
99 // 1439981652801860766 * 2^30 / 10^9 = 1546168526406004689.4
100 {1439981652801860766, 1546168526406004689},
101 // 1439981836985281914 * 2^30 / 10^9 = 1546168724171447263.4
102 {1439981836985281914, 1546168724171447263},
103 // 1439981880053705608 * 2^30 / 10^9 = 1546168770415815077.4
104 {1439981880053705608, 1546168770415815077},
108 for (i = 0; i < (sizeof (cases) / sizeof (cases[0])); i++) {
109 cdtime_t got = NS_TO_CDTIME_T (cases[i].ns);
110 EXPECT_EQ_UINT64 ((uint64_t) cases[i].want, (uint64_t) got);
118 RUN_TEST(conversion);
119 RUN_TEST(ns_to_cdtime);
124 /* vim: set sw=2 sts=2 et : */