]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-calendarspec.c
Merge pull request #1690 from evverx/run-runtime-directory
[thirdparty/systemd.git] / src / test / test-calendarspec.c
CommitLineData
36697dc0
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2012 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 <string.h>
23
b5efdb8a 24#include "alloc-util.h"
36697dc0 25#include "calendarspec.h"
07630cea 26#include "string-util.h"
36697dc0
LP
27#include "util.h"
28
29static void test_one(const char *input, const char *output) {
30 CalendarSpec *c;
aaa87092 31 _cleanup_free_ char *p = NULL, *q = NULL;
36697dc0
LP
32 usec_t u;
33 char buf[FORMAT_TIMESTAMP_MAX];
34 int r;
35
36 assert_se(calendar_spec_from_string(input, &c) >= 0);
37
38 assert_se(calendar_spec_to_string(c, &p) >= 0);
39 printf("\"%s\" → \"%s\"\n", input, p);
40
41 assert_se(streq(p, output));
36697dc0
LP
42
43 u = now(CLOCK_REALTIME);
44 r = calendar_spec_next_usec(c, u, &u);
45 printf("Next: %s\n", r < 0 ? strerror(-r) : format_timestamp(buf, sizeof(buf), u));
aaa87092 46 calendar_spec_free(c);
36697dc0 47
aaa87092
LP
48 assert_se(calendar_spec_from_string(p, &c) >= 0);
49 assert_se(calendar_spec_to_string(c, &q) >= 0);
36697dc0 50 calendar_spec_free(c);
aaa87092
LP
51
52 assert_se(streq(q, p));
36697dc0
LP
53}
54
9d5bd70d
HV
55static void test_next(const char *input, const char *new_tz, usec_t after, usec_t expect) {
56 CalendarSpec *c;
57 usec_t u;
58 char *old_tz;
59 char buf[FORMAT_TIMESTAMP_MAX];
60 int r;
61
62 old_tz = getenv("TZ");
63 if (old_tz)
64 old_tz = strdupa(old_tz);
65
66 if (new_tz)
67 assert_se(setenv("TZ", new_tz, 1) >= 0);
68 else
69 assert_se(unsetenv("TZ") >= 0);
70 tzset();
71
72 assert_se(calendar_spec_from_string(input, &c) >= 0);
73
74 printf("\"%s\"\n", input);
75
76 u = after;
77 r = calendar_spec_next_usec(c, after, &u);
78 printf("At: %s\n", r < 0 ? strerror(-r) : format_timestamp(buf, sizeof(buf), u));
79 if (expect != (usec_t)-1)
80 assert_se(r >= 0 && u == expect);
81 else
82 assert(r == -ENOENT);
83
84 calendar_spec_free(c);
85
86 if (old_tz)
87 assert_se(setenv("TZ", old_tz, 1) >= 0);
88 else
89 assert_se(unsetenv("TZ") >= 0);
90 tzset();
91}
92
36697dc0
LP
93int main(int argc, char* argv[]) {
94 CalendarSpec *c;
95
96 test_one("Sat,Thu,Mon-Wed,Sat-Sun", "Mon-Thu,Sat,Sun *-*-* 00:00:00");
97 test_one("Mon,Sun 12-*-* 2,1:23", "Mon,Sun 2012-*-* 01,02:23:00");
98 test_one("Wed *-1", "Wed *-*-01 00:00:00");
99 test_one("Wed-Wed,Wed *-1", "Wed *-*-01 00:00:00");
100 test_one("Wed, 17:48", "Wed *-*-* 17:48:00");
101 test_one("Wed-Sat,Tue 12-10-15 1:2:3", "Tue-Sat 2012-10-15 01:02:03");
102 test_one("*-*-7 0:0:0", "*-*-07 00:00:00");
103 test_one("10-15", "*-10-15 00:00:00");
104 test_one("monday *-12-* 17:00", "Mon *-12-* 17:00:00");
105 test_one("Mon,Fri *-*-3,1,2 *:30:45", "Mon,Fri *-*-01,02,03 *:30:45");
106 test_one("12,14,13,12:20,10,30", "*-*-* 12,13,14:10,20,30:00");
107 test_one("mon,fri *-1/2-1,3 *:30:45", "Mon,Fri *-01/2-01,03 *:30:45");
108 test_one("03-05 08:05:40", "*-03-05 08:05:40");
109 test_one("08:05:40", "*-*-* 08:05:40");
110 test_one("05:40", "*-*-* 05:40:00");
111 test_one("Sat,Sun 12-05 08:05:40", "Sat,Sun *-12-05 08:05:40");
112 test_one("Sat,Sun 08:05:40", "Sat,Sun *-*-* 08:05:40");
113 test_one("2003-03-05 05:40", "2003-03-05 05:40:00");
114 test_one("2003-03-05", "2003-03-05 00:00:00");
115 test_one("03-05", "*-03-05 00:00:00");
116 test_one("hourly", "*-*-* *:00:00");
117 test_one("daily", "*-*-* 00:00:00");
118 test_one("monthly", "*-*-01 00:00:00");
119 test_one("weekly", "Mon *-*-* 00:00:00");
dbfd41e2
LP
120 test_one("minutely", "*-*-* *:*:00");
121 test_one("quarterly", "*-01,04,07,10-01 00:00:00");
122 test_one("semi-annually", "*-01,07-01 00:00:00");
123 test_one("annually", "*-01-01 00:00:00");
36697dc0 124 test_one("*:2/3", "*-*-* *:02/3:00");
9d5bd70d
HV
125 test_one("2015-10-25 01:00:00 uTc", "2015-10-25 01:00:00 UTC");
126
127 test_next("2016-03-27 03:17:00", "", 12345, 1459048620000000);
128 test_next("2016-03-27 03:17:00", "CET", 12345, 1459041420000000);
129 test_next("2016-03-27 03:17:00", "EET", 12345, -1);
130 test_next("2016-03-27 03:17:00 UTC", NULL, 12345, 1459048620000000);
131 test_next("2016-03-27 03:17:00 UTC", "", 12345, 1459048620000000);
132 test_next("2016-03-27 03:17:00 UTC", "CET", 12345, 1459048620000000);
133 test_next("2016-03-27 03:17:00 UTC", "EET", 12345, 1459048620000000);
36697dc0
LP
134
135 assert_se(calendar_spec_from_string("test", &c) < 0);
136 assert_se(calendar_spec_from_string("", &c) < 0);
137 assert_se(calendar_spec_from_string("7", &c) < 0);
138 assert_se(calendar_spec_from_string("121212:1:2", &c) < 0);
139
140 return 0;
141}