This source file includes following definitions.
- assert_add_seconds
- invalid_argument
- add_zero
- add_less_than_one_day
- add_more_than_one_day
- subtract_less_than_one_day
- subtract_more_than_one_day
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13 #include <limits.h>
14
15 #include <crm/common/unittest_internal.h>
16
17 #include <crm/common/iso8601.h>
18
19 static void
20 assert_add_seconds(const char *orig_date_time, int seconds,
21 const char *expected_date_time)
22 {
23 crm_time_t *orig = crm_time_new(orig_date_time);
24 crm_time_t *expected = crm_time_new(expected_date_time);
25
26 assert_non_null(orig);
27 assert_non_null(expected);
28
29 crm_time_add_seconds(orig, seconds);
30 assert_int_equal(crm_time_compare(orig, expected), 0);
31
32 crm_time_free(orig);
33 crm_time_free(expected);
34 }
35
36 static void
37 invalid_argument(void **state)
38 {
39 pcmk__assert_asserts(crm_time_add_seconds(NULL, 1));
40 }
41
42 static void
43 add_zero(void **state)
44 {
45 assert_add_seconds("2024-01-01 00:30:00 +01:00", 0,
46 "2024-01-01 00:30:00 +01:00");
47 }
48
49 static void
50 add_less_than_one_day(void **state)
51 {
52
53 assert_add_seconds("2024-01-01 00:30:00 +01:00", 1,
54 "2024-01-01 00:30:01 +01:00");
55 assert_add_seconds("2024-01-01 00:30:00 +01:00", 59,
56 "2024-01-01 00:30:59 +01:00");
57
58
59 assert_add_seconds("2024-01-01 00:30:59 +02:00", 1,
60 "2024-01-01 00:31:00 +02:00");
61 assert_add_seconds("2024-01-01 00:44:30 +02:00", 60,
62 "2024-01-01 00:45:30 +02:00");
63 assert_add_seconds("2024-01-01 00:44:30 +02:00", 125,
64 "2024-01-01 00:46:35 +02:00");
65
66
67 assert_add_seconds("2024-01-01 00:59:59 -03:00", 1,
68 "2024-01-01 01:00:00 -03:00");
69 assert_add_seconds("2024-01-01 00:23:34 -03:00", 3600,
70 "2024-01-01 01:23:34 -03:00");
71 assert_add_seconds("2024-01-01 00:23:34 -03:00", 7210,
72 "2024-01-01 02:23:44 -03:00");
73
74
75 assert_add_seconds("2024-01-01 23:59:59 +04:00", 1,
76 "2024-01-02 00:00:00 +04:00");
77 assert_add_seconds("2024-02-28 00:05:00 +04:00", 86200,
78 "2024-02-29 00:01:40 +04:00");
79
80
81 assert_add_seconds("2023-02-28 00:05:00 -05:00", 86200,
82 "2023-03-01 00:01:40 -05:00");
83 assert_add_seconds("2024-02-29 23:59:00 -05:00", 60,
84 "2024-03-01 00:00:00 -05:00");
85
86
87 assert_add_seconds("2024-12-31 23:59:59 +06:00", 1,
88 "2025-01-01 00:00:00 +06:00");
89 }
90
91 static void
92 add_more_than_one_day(void **state)
93 {
94
95 assert_add_seconds("2024-01-01 00:00:00 +01:00", 86400 * 2,
96 "2024-01-03 00:00:00 +01:00");
97 assert_add_seconds("2024-02-27 23:59:59 +01:00", 86400 * 2,
98 "2024-02-29 23:59:59 +01:00");
99
100
101 assert_add_seconds("2023-02-26 23:59:59 -02:00", 86400 * 2 + 1,
102 "2023-03-01 00:00:00 -02:00");
103 assert_add_seconds("2024-02-27 23:59:59 -02:00", 86400 * 2 + 1,
104 "2024-03-01 00:00:00 -02:00");
105
106
107 assert_add_seconds("2024-12-01 00:00:00 +06:00", 86400 * 31,
108 "2025-01-01 00:00:00 +06:00");
109 }
110
111 static void
112 subtract_less_than_one_day(void **state)
113 {
114
115 assert_add_seconds("2024-01-01 00:30:01 +01:00", -1,
116 "2024-01-01 00:30:00 +01:00");
117 assert_add_seconds("2024-01-01 00:30:30 +01:00", -5,
118 "2024-01-01 00:30:25 +01:00");
119 assert_add_seconds("2024-01-01 00:30:59 +01:00", -59,
120 "2024-01-01 00:30:00 +01:00");
121
122
123 assert_add_seconds("2024-01-01 00:30:00 +02:00", -1,
124 "2024-01-01 00:29:59 +02:00");
125 assert_add_seconds("2024-01-01 00:44:30 +02:00", -60,
126 "2024-01-01 00:43:30 +02:00");
127 assert_add_seconds("2024-01-01 00:14:30 +02:00", -125,
128 "2024-01-01 00:12:25 +02:00");
129
130
131 assert_add_seconds("2024-01-01 01:00:00 -03:00", -1,
132 "2024-01-01 00:59:59 -03:00");
133 assert_add_seconds("2024-01-01 01:23:34 -03:00", -3600,
134 "2024-01-01 00:23:34 -03:00");
135 assert_add_seconds("2024-01-01 02:23:34 -03:00", -7210,
136 "2024-01-01 00:23:24 -03:00");
137
138
139 assert_add_seconds("2024-01-02 00:00:00 +04:00", -1,
140 "2024-01-01 23:59:59 +04:00");
141 assert_add_seconds("2024-02-29 00:01:40 +04:00", -86200,
142 "2024-02-28 00:05:00 +04:00");
143
144
145 assert_add_seconds("2023-03-01 00:01:40 -05:00", -86200,
146 "2023-02-28 00:05:00 -05:00");
147 assert_add_seconds("2024-03-01 00:00:00 -05:00", -60,
148 "2024-02-29 23:59:00 -05:00");
149
150
151 assert_add_seconds("2025-01-01 00:00:00 +06:00", -1,
152 "2024-12-31 23:59:59 +06:00");
153 }
154
155 static void
156 subtract_more_than_one_day(void **state)
157 {
158
159 assert_add_seconds("2024-01-03 00:00:00 +01:00", 86400 * -2,
160 "2024-01-01 00:00:00 +01:00");
161 assert_add_seconds("2024-02-29 23:59:59 +01:00", 86400 * -2,
162 "2024-02-27 23:59:59 +01:00");
163
164
165 assert_add_seconds("2023-03-03 00:00:00 -02:00", 86400 * -2 - 1,
166 "2023-02-28 23:59:59 -02:00");
167 assert_add_seconds("2024-03-03 00:00:00 -02:00", 86400 * -2 - 1,
168 "2024-02-29 23:59:59 -02:00");
169
170
171 assert_add_seconds("2025-01-01 00:00:00 +06:00", 86400 * -31,
172 "2024-12-01 00:00:00 +06:00");
173 }
174
175 PCMK__UNIT_TEST(NULL, NULL,
176 cmocka_unit_test(invalid_argument),
177 cmocka_unit_test(add_zero),
178 cmocka_unit_test(add_less_than_one_day),
179 cmocka_unit_test(add_more_than_one_day),
180 cmocka_unit_test(subtract_less_than_one_day),
181 cmocka_unit_test(subtract_more_than_one_day));