This source file includes following definitions.
- assert_add_days
- invalid_argument
- positive_same_year
- negative_same_year
- positive_year_changes
- negative_year_changes
- year_out_of_range
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_days(const char *orig_date_time, int days,
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_days(orig, days);
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_days(NULL, 1));
40 }
41
42 static void
43 positive_same_year(void **state)
44 {
45 assert_add_days("2024-01-01 00:30:00 +01:00", 1,
46 "2024-01-02 00:30:00 +01:00");
47
48 assert_add_days("2024-01-31 01:40:50 +02:00", 1,
49 "2024-02-01 01:40:50 +02:00");
50
51 assert_add_days("2024-02-28 11:45:11 +03:00", 1,
52 "2024-02-29 11:45:11 +03:00");
53
54 assert_add_days("2024-02-28 12:59:59 -03:00", 2,
55 "2024-03-01 12:59:59 -03:00");
56
57 assert_add_days("2024-01-01 00:00:00 +00:00", 365,
58 "2024-12-31 00:00:00 +00:00");
59
60 assert_add_days("2025-01-01 23:00:00 +00:00", 364,
61 "2025-12-31 23:00:00 +00:00");
62 }
63
64 static void
65 negative_same_year(void **state)
66 {
67 assert_add_days("2024-01-02 00:30:00 +01:00", -1,
68 "2024-01-01 00:30:00 +01:00");
69
70 assert_add_days("2024-02-01 01:40:50 +02:00", -1,
71 "2024-01-31 01:40:50 +02:00");
72
73 assert_add_days("2024-03-01 11:45:11 +03:00", -1,
74 "2024-02-29 11:45:11 +03:00");
75
76 assert_add_days("2024-03-01 12:59:59 -03:00", -2,
77 "2024-02-28 12:59:59 -03:00");
78
79 assert_add_days("2024-12-31 00:00:00 +00:00", -365,
80 "2024-01-01 00:00:00 +00:00");
81
82 assert_add_days("2025-12-31 23:00:00 +00:00", -364,
83 "2025-01-01 23:00:00 +00:00");
84 }
85
86 static void
87 positive_year_changes(void **state)
88 {
89
90 assert_add_days("2023-01-01 00:40:20 +02:00", 365,
91 "2024-01-01 00:40:20 +02:00");
92
93
94 assert_add_days("2023-01-01 00:40:20 +02:00", 426,
95 "2024-03-02 00:40:20 +02:00");
96
97
98 assert_add_days("2023-03-02 00:40:20 +02:00", 325,
99 "2024-01-21 00:40:20 +02:00");
100
101
102 assert_add_days("2023-03-02 00:40:20 +02:00", 385,
103 "2024-03-21 00:40:20 +02:00");
104
105
106 assert_add_days("2024-01-01 00:40:20 +02:00", 366,
107 "2025-01-01 00:40:20 +02:00");
108
109
110 assert_add_days("2024-01-01 00:40:20 +02:00", 430,
111 "2025-03-06 00:40:20 +02:00");
112
113
114 assert_add_days("2024-12-31 09:41:23 +06:00", 1,
115 "2025-01-01 09:41:23 +06:00");
116
117
118 assert_add_days("2024-12-31 09:41:23 +06:00", 90,
119 "2025-03-31 09:41:23 +06:00");
120
121
122 assert_add_days("2025-01-01 01:00:00 -02:00", 366,
123 "2026-01-02 01:00:00 -02:00");
124
125
126 assert_add_days("2025-01-01 00:00:00 +00:00", 1500,
127 "2029-02-09 00:00:00 +00:00");
128
129
130 assert_add_days("2025-01-01 00:00:00 +00:00", 28000,
131 "2101-08-31 00:00:00 +00:00");
132
133
134 assert_add_days("2025-01-01 00:00:00 +00:00", 150000,
135 "2435-09-09 00:00:00 +00:00");
136 }
137
138 static void
139 negative_year_changes(void **state)
140 {
141
142 assert_add_days("2025-01-01 00:40:20 +02:00", -366,
143 "2024-01-01 00:40:20 +02:00");
144
145
146 assert_add_days("2025-01-01 00:40:20 +02:00", -300,
147 "2024-03-07 00:40:20 +02:00");
148
149
150 assert_add_days("2024-01-01 00:40:20 +02:00", -365,
151 "2023-01-01 00:40:20 +02:00");
152
153
154 assert_add_days("2024-01-01 00:40:20 +02:00", -1,
155 "2023-12-31 00:40:20 +02:00");
156
157
158 assert_add_days("1990-01-01 00:00:00 +00:00", -2000,
159 "1984-07-11 00:00:00 +00:00");
160
161
162 assert_add_days("1990-01-01 00:00:00 +00:00", -33000,
163 "1899-08-26 00:00:00 +00:00");
164
165
166 assert_add_days("1990-01-01 00:00:00 +00:00", -150000,
167 "1579-04-26 00:00:00 +00:00");
168 }
169
170 static void
171 year_out_of_range(void **state)
172 {
173 char *orig_datetime = NULL;
174 char *expected_datetime = NULL;
175
176
177 orig_datetime = crm_strdup_printf("%d-01-01 00:00:00 +00:00", INT_MAX);
178 expected_datetime = crm_strdup_printf("%d-12-31 00:00:00 +00:00", INT_MAX);
179 assert_add_days(orig_datetime, 400, expected_datetime);
180 free(orig_datetime);
181 free(expected_datetime);
182
183
184 assert_add_days("01-02-01 00:00:00 +00:00", -40,
185 "01-01-01 00:00:00 +00:00");
186 }
187
188 PCMK__UNIT_TEST(NULL, NULL,
189 cmocka_unit_test(invalid_argument),
190 cmocka_unit_test(positive_same_year),
191 cmocka_unit_test(negative_same_year),
192 cmocka_unit_test(positive_year_changes),
193 cmocka_unit_test(negative_year_changes),
194 cmocka_unit_test(year_out_of_range));