]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time.c
2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "time-util.h"
23 static void test_parse_sec(void) {
26 assert_se(parse_sec("5s", &u
) >= 0);
27 assert_se(u
== 5 * USEC_PER_SEC
);
28 assert_se(parse_sec("5s500ms", &u
) >= 0);
29 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
30 assert_se(parse_sec(" 5s 500ms ", &u
) >= 0);
31 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
32 assert_se(parse_sec(" 5.5s ", &u
) >= 0);
33 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
);
34 assert_se(parse_sec(" 5.5s 0.5ms ", &u
) >= 0);
35 assert_se(u
== 5 * USEC_PER_SEC
+ 500 * USEC_PER_MSEC
+ 500);
36 assert_se(parse_sec(" .22s ", &u
) >= 0);
37 assert_se(u
== 220 * USEC_PER_MSEC
);
38 assert_se(parse_sec(" .50y ", &u
) >= 0);
39 assert_se(u
== USEC_PER_YEAR
/ 2);
40 assert_se(parse_sec("2.5", &u
) >= 0);
41 assert_se(u
== 2500 * USEC_PER_MSEC
);
42 assert_se(parse_sec(".7", &u
) >= 0);
43 assert_se(u
== 700 * USEC_PER_MSEC
);
44 assert_se(parse_sec("infinity", &u
) >= 0);
45 assert_se(u
== USEC_INFINITY
);
46 assert_se(parse_sec(" infinity ", &u
) >= 0);
47 assert_se(u
== USEC_INFINITY
);
49 assert_se(parse_sec(" xyz ", &u
) < 0);
50 assert_se(parse_sec("", &u
) < 0);
51 assert_se(parse_sec(" . ", &u
) < 0);
52 assert_se(parse_sec(" 5. ", &u
) < 0);
53 assert_se(parse_sec(".s ", &u
) < 0);
54 assert_se(parse_sec(" infinity .7", &u
) < 0);
55 assert_se(parse_sec(".3 infinity", &u
) < 0);
58 static void test_parse_time(void) {
61 assert_se(parse_time("5", &u
, 1) >= 0);
64 assert_se(parse_time("5", &u
, USEC_PER_MSEC
) >= 0);
65 assert_se(u
== 5 * USEC_PER_MSEC
);
67 assert_se(parse_time("5", &u
, USEC_PER_SEC
) >= 0);
68 assert_se(u
== 5 * USEC_PER_SEC
);
70 assert_se(parse_time("5s", &u
, 1) >= 0);
71 assert_se(u
== 5 * USEC_PER_SEC
);
73 assert_se(parse_time("5s", &u
, USEC_PER_SEC
) >= 0);
74 assert_se(u
== 5 * USEC_PER_SEC
);
76 assert_se(parse_time("5s", &u
, USEC_PER_MSEC
) >= 0);
77 assert_se(u
== 5 * USEC_PER_SEC
);
80 static void test_parse_nsec(void) {
83 assert_se(parse_nsec("5s", &u
) >= 0);
84 assert_se(u
== 5 * NSEC_PER_SEC
);
85 assert_se(parse_nsec("5s500ms", &u
) >= 0);
86 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
87 assert_se(parse_nsec(" 5s 500ms ", &u
) >= 0);
88 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
89 assert_se(parse_nsec(" 5.5s ", &u
) >= 0);
90 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
);
91 assert_se(parse_nsec(" 5.5s 0.5ms ", &u
) >= 0);
92 assert_se(u
== 5 * NSEC_PER_SEC
+ 500 * NSEC_PER_MSEC
+ 500 * NSEC_PER_USEC
);
93 assert_se(parse_nsec(" .22s ", &u
) >= 0);
94 assert_se(u
== 220 * NSEC_PER_MSEC
);
95 assert_se(parse_nsec(" .50y ", &u
) >= 0);
96 assert_se(u
== NSEC_PER_YEAR
/ 2);
97 assert_se(parse_nsec("2.5", &u
) >= 0);
99 assert_se(parse_nsec(".7", &u
) >= 0);
101 assert_se(parse_nsec("infinity", &u
) >= 0);
102 assert_se(u
== NSEC_INFINITY
);
103 assert_se(parse_nsec(" infinity ", &u
) >= 0);
104 assert_se(u
== NSEC_INFINITY
);
106 assert_se(parse_nsec(" xyz ", &u
) < 0);
107 assert_se(parse_nsec("", &u
) < 0);
108 assert_se(parse_nsec(" . ", &u
) < 0);
109 assert_se(parse_nsec(" 5. ", &u
) < 0);
110 assert_se(parse_nsec(".s ", &u
) < 0);
111 assert_se(parse_nsec(" infinity .7", &u
) < 0);
112 assert_se(parse_nsec(".3 infinity", &u
) < 0);
115 static void test_format_timespan_one(usec_t x
, usec_t accuracy
) {
117 char l
[FORMAT_TIMESPAN_MAX
];
120 log_info(USEC_FMT
" (at accuracy "USEC_FMT
")", x
, accuracy
);
122 r
= format_timespan(l
, sizeof(l
), x
, accuracy
);
125 log_info(" = <%s>", l
);
127 assert_se(parse_sec(l
, &y
) >= 0);
129 log_info(" = "USEC_FMT
, y
);
134 assert_se(x
/ accuracy
== y
/ accuracy
);
137 static void test_format_timespan(usec_t accuracy
) {
138 test_format_timespan_one(0, accuracy
);
139 test_format_timespan_one(1, accuracy
);
140 test_format_timespan_one(1*USEC_PER_SEC
, accuracy
);
141 test_format_timespan_one(999*USEC_PER_MSEC
, accuracy
);
142 test_format_timespan_one(1234567, accuracy
);
143 test_format_timespan_one(12, accuracy
);
144 test_format_timespan_one(123, accuracy
);
145 test_format_timespan_one(1234, accuracy
);
146 test_format_timespan_one(12345, accuracy
);
147 test_format_timespan_one(123456, accuracy
);
148 test_format_timespan_one(1234567, accuracy
);
149 test_format_timespan_one(12345678, accuracy
);
150 test_format_timespan_one(1200000, accuracy
);
151 test_format_timespan_one(1230000, accuracy
);
152 test_format_timespan_one(1230000, accuracy
);
153 test_format_timespan_one(1234000, accuracy
);
154 test_format_timespan_one(1234500, accuracy
);
155 test_format_timespan_one(1234560, accuracy
);
156 test_format_timespan_one(1234567, accuracy
);
157 test_format_timespan_one(986087, accuracy
);
158 test_format_timespan_one(500 * USEC_PER_MSEC
, accuracy
);
159 test_format_timespan_one(9*USEC_PER_YEAR
/5 - 23, accuracy
);
160 test_format_timespan_one(USEC_INFINITY
, accuracy
);
163 static void test_timezone_is_valid(void) {
164 assert_se(timezone_is_valid("Europe/Berlin"));
165 assert_se(timezone_is_valid("Australia/Sydney"));
166 assert_se(!timezone_is_valid("Europe/Do not exist"));
169 static void test_get_timezones(void) {
170 _cleanup_strv_free_
char **zones
= NULL
;
174 r
= get_timezones(&zones
);
177 STRV_FOREACH(zone
, zones
)
178 assert_se(timezone_is_valid(*zone
));
181 static void test_usec_add(void) {
182 assert_se(usec_add(0, 0) == 0);
183 assert_se(usec_add(1, 4) == 5);
184 assert_se(usec_add(USEC_INFINITY
, 5) == USEC_INFINITY
);
185 assert_se(usec_add(5, USEC_INFINITY
) == USEC_INFINITY
);
186 assert_se(usec_add(USEC_INFINITY
-5, 2) == USEC_INFINITY
-3);
187 assert_se(usec_add(USEC_INFINITY
-2, 2) == USEC_INFINITY
);
188 assert_se(usec_add(USEC_INFINITY
-1, 2) == USEC_INFINITY
);
189 assert_se(usec_add(USEC_INFINITY
, 2) == USEC_INFINITY
);
192 static void test_usec_sub(void) {
193 assert_se(usec_sub(0, 0) == 0);
194 assert_se(usec_sub(4, 1) == 3);
195 assert_se(usec_sub(4, 4) == 0);
196 assert_se(usec_sub(4, 5) == 0);
197 assert_se(usec_sub(USEC_INFINITY
-3, -3) == USEC_INFINITY
);
198 assert_se(usec_sub(USEC_INFINITY
-3, -3) == USEC_INFINITY
);
199 assert_se(usec_sub(USEC_INFINITY
-3, -4) == USEC_INFINITY
);
200 assert_se(usec_sub(USEC_INFINITY
-3, -5) == USEC_INFINITY
);
201 assert_se(usec_sub(USEC_INFINITY
, 5) == USEC_INFINITY
);
204 int main(int argc
, char *argv
[]) {
210 test_format_timespan(1);
211 test_format_timespan(USEC_PER_MSEC
);
212 test_format_timespan(USEC_PER_SEC
);
213 test_timezone_is_valid();
214 test_get_timezones();
218 /* Ensure time_t is signed */
219 assert_cc((time_t) -1 < (time_t) 1);
221 /* Ensure TIME_T_MAX works correctly */
222 x
= (uintmax_t) TIME_T_MAX
;
224 assert((time_t) x
< 0);