chiark / gitweb /
bus: properly shift cgroup data returned from kdbus by the container's root before...
[elogind.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
24 static void test_parse_sec(void) {
25         usec_t u;
26
27         assert_se(parse_sec("5s", &u) >= 0);
28         assert_se(u == 5 * USEC_PER_SEC);
29         assert_se(parse_sec("5s500ms", &u) >= 0);
30         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
31         assert_se(parse_sec(" 5s 500ms  ", &u) >= 0);
32         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
33         assert_se(parse_sec(" 5.5s  ", &u) >= 0);
34         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC);
35         assert_se(parse_sec(" 5.5s 0.5ms ", &u) >= 0);
36         assert_se(u == 5 * USEC_PER_SEC + 500 * USEC_PER_MSEC + 500);
37         assert_se(parse_sec(" .22s ", &u) >= 0);
38         assert_se(u == 220 * USEC_PER_MSEC);
39         assert_se(parse_sec(" .50y ", &u) >= 0);
40         assert_se(u == USEC_PER_YEAR / 2);
41         assert_se(parse_sec("2.5", &u) >= 0);
42         assert_se(u == 2500 * USEC_PER_MSEC);
43         assert_se(parse_sec(".7", &u) >= 0);
44         assert_se(u == 700 * USEC_PER_MSEC);
45
46         assert_se(parse_sec(" xyz ", &u) < 0);
47         assert_se(parse_sec("", &u) < 0);
48         assert_se(parse_sec(" . ", &u) < 0);
49         assert_se(parse_sec(" 5. ", &u) < 0);
50         assert_se(parse_sec(".s ", &u) < 0);
51 }
52
53 static void test_parse_nsec(void) {
54         nsec_t u;
55
56         assert_se(parse_nsec("5s", &u) >= 0);
57         assert_se(u == 5 * NSEC_PER_SEC);
58         assert_se(parse_nsec("5s500ms", &u) >= 0);
59         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
60         assert_se(parse_nsec(" 5s 500ms  ", &u) >= 0);
61         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
62         assert_se(parse_nsec(" 5.5s  ", &u) >= 0);
63         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC);
64         assert_se(parse_nsec(" 5.5s 0.5ms ", &u) >= 0);
65         assert_se(u == 5 * NSEC_PER_SEC + 500 * NSEC_PER_MSEC + 500 * NSEC_PER_USEC);
66         assert_se(parse_nsec(" .22s ", &u) >= 0);
67         assert_se(u == 220 * NSEC_PER_MSEC);
68         assert_se(parse_nsec(" .50y ", &u) >= 0);
69         assert_se(u == NSEC_PER_YEAR / 2);
70         assert_se(parse_nsec("2.5", &u) >= 0);
71         assert_se(u == 2);
72         assert_se(parse_nsec(".7", &u) >= 0);
73         assert_se(u == 0);
74
75         assert_se(parse_nsec(" xyz ", &u) < 0);
76         assert_se(parse_nsec("", &u) < 0);
77         assert_se(parse_nsec(" . ", &u) < 0);
78         assert_se(parse_nsec(" 5. ", &u) < 0);
79         assert_se(parse_nsec(".s ", &u) < 0);
80 }
81
82 static void test_format_timespan_one(usec_t x, usec_t accuracy) {
83         char *r;
84         char l[FORMAT_TIMESPAN_MAX];
85         usec_t y;
86
87         log_info("%llu     (at accuracy %llu)", (unsigned long long) x, (unsigned long long) accuracy);
88
89         r = format_timespan(l, sizeof(l), x, accuracy);
90         assert_se(r);
91
92         log_info(" = <%s>", l);
93
94         assert_se(parse_sec(l, &y) >= 0);
95
96         log_info(" = %llu", (unsigned long long) y);
97
98         if (accuracy <= 0)
99                 accuracy = 1;
100
101         assert_se(x / accuracy == y / accuracy);
102 }
103
104 static void test_format_timespan(usec_t accuracy) {
105         test_format_timespan_one(0, accuracy);
106         test_format_timespan_one(1, accuracy);
107         test_format_timespan_one(1*USEC_PER_SEC, accuracy);
108         test_format_timespan_one(999*USEC_PER_MSEC, accuracy);
109         test_format_timespan_one(1234567, accuracy);
110         test_format_timespan_one(12, accuracy);
111         test_format_timespan_one(123, accuracy);
112         test_format_timespan_one(1234, accuracy);
113         test_format_timespan_one(12345, accuracy);
114         test_format_timespan_one(123456, accuracy);
115         test_format_timespan_one(1234567, accuracy);
116         test_format_timespan_one(12345678, accuracy);
117         test_format_timespan_one(1200000, accuracy);
118         test_format_timespan_one(1230000, accuracy);
119         test_format_timespan_one(1230000, accuracy);
120         test_format_timespan_one(1234000, accuracy);
121         test_format_timespan_one(1234500, accuracy);
122         test_format_timespan_one(1234560, accuracy);
123         test_format_timespan_one(1234567, accuracy);
124         test_format_timespan_one(986087, accuracy);
125         test_format_timespan_one(500 * USEC_PER_MSEC, accuracy);
126         test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
127 }
128
129 int main(int argc, char *argv[]) {
130         test_parse_sec();
131         test_parse_nsec();
132         test_format_timespan(1);
133         test_format_timespan(USEC_PER_MSEC);
134         test_format_timespan(USEC_PER_SEC);
135         return 0;
136 }