]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-time.c
tree-wide: use coccinelle to patch a lot of code to use mfree()
[thirdparty/systemd.git] / src / test / test-time.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "time-util.h"
23 #include "strv.h"
24
25 static void test_parse_sec(void) {
26 usec_t u;
27
28 assert_se(parse_sec("5s", &u) >= 0);
29 assert_se(u == 5 * USEC_PER_SEC);
30 assert_se(parse_sec("5s500ms", &u) >= 0);
31 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
32 assert_se(parse_sec(" 5s 500ms ", &u) >= 0);
33 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
34 assert_se(parse_sec(" 5.5s ", &u) >= 0);
35 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
36 assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
37 assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
38 assert_se(parse_sec(" .22s ", &u) >= 0);
39 assert_se(u == 220 * USEC_PER_MSEC);
40 assert_se(parse_sec(" .50y ", &u) >= 0);
41 assert_se(u == USEC_PER_YEAR / 2);
42 assert_se(parse_sec("2.5", &u) >= 0);
43 assert_se(u == 2500 * USEC_PER_MSEC);
44 assert_se(parse_sec(".7", &u) >= 0);
45 assert_se(u == 700 * USEC_PER_MSEC);
46 assert_se(parse_sec("infinity", &u) >= 0);
47 assert_se(u == USEC_INFINITY);
48 assert_se(parse_sec(" infinity ", &u) >= 0);
49 assert_se(u == USEC_INFINITY);
50
51 assert_se(parse_sec(" xyz ", &u) < 0);
52 assert_se(parse_sec("", &u) < 0);
53 assert_se(parse_sec(" . ", &u) < 0);
54 assert_se(parse_sec(" 5. ", &u) < 0);
55 assert_se(parse_sec(".s ", &u) < 0);
56 assert_se(parse_sec(" infinity .7", &u) < 0);
57 assert_se(parse_sec(".3 infinity", &u) < 0);
58 }
59
60 static void test_parse_nsec(void) {
61 nsec_t u;
62
63 assert_se(parse_nsec("5s", &u) >= 0);
64 assert_se(u == 5 * NSEC_PER_SEC);
65 assert_se(parse_nsec("5s500ms", &u) >= 0);
66 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
67 assert_se(parse_nsec(" 5s 500ms ", &u) >= 0);
68 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
69 assert_se(parse_nsec(" 5.5s ", &u) >= 0);
70 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
71 assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
72 assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
73 assert_se(parse_nsec(" .22s ", &u) >= 0);
74 assert_se(u == 220 * NSEC_PER_MSEC);
75 assert_se(parse_nsec(" .50y ", &u) >= 0);
76 assert_se(u == NSEC_PER_YEAR / 2);
77 assert_se(parse_nsec("2.5", &u) >= 0);
78 assert_se(u == 2);
79 assert_se(parse_nsec(".7", &u) >= 0);
80 assert_se(u == 0);
81 assert_se(parse_nsec("infinity", &u) >= 0);
82 assert_se(u == NSEC_INFINITY);
83 assert_se(parse_nsec(" infinity ", &u) >= 0);
84 assert_se(u == NSEC_INFINITY);
85
86 assert_se(parse_nsec(" xyz ", &u) < 0);
87 assert_se(parse_nsec("", &u) < 0);
88 assert_se(parse_nsec(" . ", &u) < 0);
89 assert_se(parse_nsec(" 5. ", &u) < 0);
90 assert_se(parse_nsec(".s ", &u) < 0);
91 assert_se(parse_nsec(" infinity .7", &u) < 0);
92 assert_se(parse_nsec(".3 infinity", &u) < 0);
93 }
94
95 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
96 char *r;
97 char l[FORMAT_TIMESPAN_MAX];
98 usec_t y;
99
100 log_info(USEC_FMT" (at accuracy "USEC_FMT")", x, accuracy);
101
102 r = format_timespan(l, sizeof(l), x, accuracy);
103 assert_se(r);
104
105 log_info(" = <%s>", l);
106
107 assert_se(parse_sec(l, &y) >= 0);
108
109 log_info(" = "USEC_FMT, y);
110
111 if (accuracy <= 0)
112 accuracy = 1;
113
114 assert_se(x / accuracy == y / accuracy);
115 }
116
117 static void test_format_timespan(usec_t accuracy) {
118 test_format_timespan_one(0, accuracy);
119 test_format_timespan_one(1, accuracy);
120 test_format_timespan_one(1*USEC_PER_SEC, accuracy);
121 test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
122 test_format_timespan_one(1234567, accuracy);
123 test_format_timespan_one(12, accuracy);
124 test_format_timespan_one(123, accuracy);
125 test_format_timespan_one(1234, accuracy);
126 test_format_timespan_one(12345, accuracy);
127 test_format_timespan_one(123456, accuracy);
128 test_format_timespan_one(1234567, accuracy);
129 test_format_timespan_one(12345678, accuracy);
130 test_format_timespan_one(1200000, accuracy);
131 test_format_timespan_one(1230000, accuracy);
132 test_format_timespan_one(1230000, accuracy);
133 test_format_timespan_one(1234000, accuracy);
134 test_format_timespan_one(1234500, accuracy);
135 test_format_timespan_one(1234560, accuracy);
136 test_format_timespan_one(1234567, accuracy);
137 test_format_timespan_one(986087, accuracy);
138 test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
139 test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
140 test_format_timespan_one(USEC_INFINITY, accuracy);
141 }
142
143 static void test_timezone_is_valid(void) {
144 assert_se(timezone_is_valid("Europe/Berlin"));
145 assert_se(timezone_is_valid("Australia/Sydney"));
146 assert_se(!timezone_is_valid("Europe/Do not exist"));
147 }
148
149 static void test_get_timezones(void) {
150 _cleanup_strv_free_ char **zones = NULL;
151 int r;
152 char **zone;
153
154 r = get_timezones(&zones);
155 assert_se(r == 0);
156
157 STRV_FOREACH(zone, zones) {
158 assert_se(timezone_is_valid(*zone));
159 }
160 }
161
162 int main(int argc, char *argv[]) {
163 test_parse_sec();
164 test_parse_nsec();
165 test_format_timespan(1);
166 test_format_timespan(USEC_PER_MSEC);
167 test_format_timespan(USEC_PER_SEC);
168 test_timezone_is_valid();
169 test_get_timezones();
170
171 return 0;
172 }