1 /**
2 * collectd - src/daemon/utils_time_test.c
3 * Copyright (C) 2015 Florian octo Forster
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 #define DBL_PRECISION 1e-3
29 #include "collectd.h"
31 #include "testing.h"
32 #include "utils_time.h"
34 DEF_TEST(conversion) {
35 struct {
36 cdtime_t t;
37 double d;
38 time_t tt;
39 uint64_t ms;
40 struct timeval tv;
41 struct timespec ts;
42 } cases[] = {
43 /* cdtime double time_t milliseconds
44 timeval timespec */
45 {0, 0.0, 0, 0, {0, 0}, {0, 0}},
46 {10737418240ULL, 10.0, 10, 10000, {10, 0}, {10, 0}},
47 {1542908534771941376ULL,
48 1436945549.0,
49 1436945549,
50 1436945549000ULL,
51 {1436945549, 0},
52 {1436945549, 0}},
53 {1542908535540740522ULL,
54 1436945549.716,
55 1436945550,
56 1436945549716ULL,
57 {1436945549, 716000},
58 {1436945549, 716000000}},
59 // 1426076671.123 * 2^30 = 1531238166015458148.352
60 {1531238166015458148ULL,
61 1426076671.123,
62 1426076671,
63 1426076671123ULL,
64 {1426076671, 123000},
65 {1426076671, 123000000}},
66 // 1426076681.234 * 2^30 = 1531238176872061730.816
67 {1531238176872061731ULL,
68 1426076681.234,
69 1426076681,
70 1426076681234ULL,
71 {1426076681, 234000},
72 {1426076681, 234000000}},
73 // 1426083986.314 * 2^30 = 1531246020641985396.736
74 {1531246020641985397ULL,
75 1426083986.314,
76 1426083986,
77 1426083986314ULL,
78 {1426083986, 314000},
79 {1426083986, 314000000}},
80 // 1426083986.494142531 * 2^30 = 1531246020835411966.5
81 {1531246020835411967ULL,
82 1426083986.494,
83 1426083986,
84 1426083986494ULL,
85 {1426083986, 494143},
86 {1426083986, 494142531}},
87 // 1426083986.987410814 * 2^30 = 1531246021365054752.4
88 {1531246021365054752ULL,
89 1426083986.987,
90 1426083987,
91 1426083986987ULL,
92 {1426083986, 987411},
93 {1426083986, 987410814}},
95 /* These cases test the cdtime_t -> ns conversion rounds correctly. */
96 // 1546167635576736987 / 2^30 = 1439980823.1524536265...
97 {1546167635576736987ULL,
98 1439980823.152,
99 1439980823,
100 1439980823152ULL,
101 {1439980823, 152454},
102 {1439980823, 152453627}},
103 // 1546167831554815222 / 2^30 = 1439981005.6712620165...
104 {1546167831554815222ULL,
105 1439981005.671,
106 1439981006,
107 1439981005671ULL,
108 {1439981005, 671262},
109 {1439981005, 671262017}},
110 // 1546167986577716567 / 2^30 = 1439981150.0475896215...
111 {1546167986577716567ULL,
112 1439981150.048,
113 1439981150,
114 1439981150048ULL,
115 {1439981150, 47590},
116 {1439981150, 47589622}},
117 };
119 for (size_t i = 0; i < (sizeof(cases) / sizeof(cases[0])); i++) {
120 struct timeval tv;
121 struct timespec ts;
123 // cdtime -> s
124 EXPECT_EQ_UINT64(cases[i].tt, CDTIME_T_TO_TIME_T(cases[i].t));
126 // cdtime -> ms
127 EXPECT_EQ_UINT64(cases[i].ms, CDTIME_T_TO_MS(cases[i].t));
129 // cdtime -> us
130 CDTIME_T_TO_TIMEVAL(cases[i].t, &tv);
131 EXPECT_EQ_UINT64(cases[i].tv.tv_sec, tv.tv_sec);
132 EXPECT_EQ_UINT64(cases[i].tv.tv_usec, tv.tv_usec);
134 // cdtime -> ns
135 CDTIME_T_TO_TIMESPEC(cases[i].t, &ts);
136 EXPECT_EQ_UINT64(cases[i].ts.tv_sec, ts.tv_sec);
137 EXPECT_EQ_UINT64(cases[i].ts.tv_nsec, ts.tv_nsec);
139 // cdtime -> double
140 EXPECT_EQ_DOUBLE(cases[i].d, CDTIME_T_TO_DOUBLE(cases[i].t));
141 }
143 return 0;
144 }
146 /* These cases test the ns -> cdtime_t conversion rounds correctly. */
147 DEF_TEST(ns_to_cdtime) {
148 struct {
149 uint64_t ns;
150 cdtime_t want;
151 } cases[] = {
152 // 1439981652801860766 * 2^30 / 10^9 = 1546168526406004689.4
153 {1439981652801860766ULL, 1546168526406004689ULL},
154 // 1439981836985281914 * 2^30 / 10^9 = 1546168724171447263.4
155 {1439981836985281914ULL, 1546168724171447263ULL},
156 // 1439981880053705608 * 2^30 / 10^9 = 1546168770415815077.4
157 {1439981880053705608ULL, 1546168770415815077ULL},
158 };
160 for (size_t i = 0; i < (sizeof(cases) / sizeof(cases[0])); i++) {
161 EXPECT_EQ_UINT64(cases[i].want, NS_TO_CDTIME_T(cases[i].ns));
162 }
164 return 0;
165 }
167 int main(void) {
168 RUN_TEST(conversion);
169 RUN_TEST(ns_to_cdtime);
171 END_TEST;
172 }
174 /* vim: set sw=2 sts=2 et : */