pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
crm_time_add_years_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 
19 static void
20 assert_add_years(const char *orig_date_time, int years,
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_years(orig, years);
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 {
40 }
41 
42 static void
43 add_positive(void **state)
44 {
45  assert_add_years("2024-01-01 00:30:00 +01:00", 1,
46  "2025-01-01 00:30:00 +01:00");
47 
48  assert_add_years("2024-12-31 01:40:50 +02:00", 1000,
49  "3024-12-31 01:40:50 +02:00");
50 }
51 
52 static void
53 add_negative(void **state)
54 {
55  assert_add_years("2024-01-01 00:30:00 +01:00", -1,
56  "2023-01-01 00:30:00 +01:00");
57 
58  assert_add_years("2024-12-31 01:40:50 +02:00", -1000,
59  "1024-12-31 01:40:50 +02:00");
60 }
61 
62 static void
63 out_of_range(void **state)
64 {
65  char *expected_datetime = NULL;
66 
67  expected_datetime = crm_strdup_printf("%d-01-01 00:00:00 +00:00", INT_MAX);
68  assert_add_years("2024-01-01 00:00:00 +00:00", INT_MAX, expected_datetime);
69  free(expected_datetime);
70 
71  assert_add_years("2024-01-01 00:00:00 +00:00", -3000,
72  "01-01-01 00:00:00 +00:00");
73 }
74 
75 PCMK__UNIT_TEST(NULL, NULL,
76  cmocka_unit_test(invalid_argument),
77  cmocka_unit_test(add_positive),
78  cmocka_unit_test(add_negative),
79  cmocka_unit_test(out_of_range));
void crm_time_add_years(crm_time_t *dt, int value)
Definition: iso8601.c:1891
struct crm_time_s crm_time_t
Definition: iso8601.h:32
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
ISO_8601 Date handling.
#define pcmk__assert_asserts(stmt)
int crm_time_compare(const crm_time_t *a, const crm_time_t *b)
Definition: iso8601.c:1736
PCMK__UNIT_TEST(NULL, NULL, cmocka_unit_test(invalid_argument), cmocka_unit_test(add_positive), cmocka_unit_test(add_negative), cmocka_unit_test(out_of_range))
crm_time_t * crm_time_new(const char *string)
Definition: iso8601.c:112
void crm_time_free(crm_time_t *dt)
Definition: iso8601.c:150