pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
crm_time_add_days_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2024 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <stdio.h> // NULL
13#include <limits.h> // INT_MAX
14
16
17#include <crm/common/iso8601.h>
18
19static void
20assert_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
36static void
37invalid_argument(void **state)
38{
40}
41
42static void
43positive_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
64static void
65negative_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
86static void
87positive_year_changes(void **state)
88{
89 // Non-leap year before March to leap year before March
90 assert_add_days("2023-01-01 00:40:20 +02:00", 365,
91 "2024-01-01 00:40:20 +02:00");
92
93 // Non-leap year before March to leap year after February
94 assert_add_days("2023-01-01 00:40:20 +02:00", 426,
95 "2024-03-02 00:40:20 +02:00");
96
97 // Non-leap year after February to leap year before March
98 assert_add_days("2023-03-02 00:40:20 +02:00", 325,
99 "2024-01-21 00:40:20 +02:00");
100
101 // Non-leap year after February to leap year after February
102 assert_add_days("2023-03-02 00:40:20 +02:00", 385,
103 "2024-03-21 00:40:20 +02:00");
104
105 // Leap year before March to non-leap year before March
106 assert_add_days("2024-01-01 00:40:20 +02:00", 366,
107 "2025-01-01 00:40:20 +02:00");
108
109 // Leap year before March to non-leap year after February
110 assert_add_days("2024-01-01 00:40:20 +02:00", 430,
111 "2025-03-06 00:40:20 +02:00");
112
113 // Leap year after February to non-leap year before March
114 assert_add_days("2024-12-31 09:41:23 +06:00", 1,
115 "2025-01-01 09:41:23 +06:00");
116
117 // Leap year after February to non-leap year after February
118 assert_add_days("2024-12-31 09:41:23 +06:00", 90,
119 "2025-03-31 09:41:23 +06:00");
120
121 // From and to non-leap years
122 assert_add_days("2025-01-01 01:00:00 -02:00", 366,
123 "2026-01-02 01:00:00 -02:00");
124
125 // Past "leap year if divisible by 4"
126 assert_add_days("2025-01-01 00:00:00 +00:00", 1500,
127 "2029-02-09 00:00:00 +00:00");
128
129 // Past "except if divisible by 100"
130 assert_add_days("2025-01-01 00:00:00 +00:00", 28000,
131 "2101-08-31 00:00:00 +00:00");
132
133 // Past "except if divisible by 400"
134 assert_add_days("2025-01-01 00:00:00 +00:00", 150000,
135 "2435-09-09 00:00:00 +00:00");
136}
137
138static void
139negative_year_changes(void **state)
140{
141 // Non-leap year before March to leap year before March
142 assert_add_days("2025-01-01 00:40:20 +02:00", -366,
143 "2024-01-01 00:40:20 +02:00");
144
145 // Non-leap year before March to leap year after February
146 assert_add_days("2025-01-01 00:40:20 +02:00", -300,
147 "2024-03-07 00:40:20 +02:00");
148
149 // Leap year before March to non-leap year before March
150 assert_add_days("2024-01-01 00:40:20 +02:00", -365,
151 "2023-01-01 00:40:20 +02:00");
152
153 // Leap year before March to non-leap year after February
154 assert_add_days("2024-01-01 00:40:20 +02:00", -1,
155 "2023-12-31 00:40:20 +02:00");
156
157 // Past "leap year if divisible by 4"
158 assert_add_days("1990-01-01 00:00:00 +00:00", -2000,
159 "1984-07-11 00:00:00 +00:00");
160
161 // Past "except if divisible by 100"
162 assert_add_days("1990-01-01 00:00:00 +00:00", -33000,
163 "1899-08-26 00:00:00 +00:00");
164
165 // Past "except if divisible by 400"
166 assert_add_days("1990-01-01 00:00:00 +00:00", -150000,
167 "1579-04-26 00:00:00 +00:00");
168}
169
170static void
171year_out_of_range(void **state)
172{
173 char *orig_datetime = NULL;
174 char *expected_datetime = NULL;
175
176 // Year too large
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 // Year too small
184 assert_add_days("01-02-01 00:00:00 +00:00", -40,
185 "01-01-01 00:00:00 +00:00");
186}
187
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));
ISO_8601 Date handling.
void crm_time_add_days(crm_time_t *dt, int value)
Add days to a date/time.
Definition iso8601.c:1799
void crm_time_free(crm_time_t *dt)
Definition iso8601.c:150
crm_time_t * crm_time_new(const char *string)
Definition iso8601.c:112
struct crm_time_s crm_time_t
Definition iso8601.h:32
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition iso8601.c:1736
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
#define pcmk__assert_asserts(stmt)
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)