pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__time_format_hr_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 
16 
17 #define TEST_TIME pcmk__time_hr_new("2024-01-02 03:04:05 +00:00")
18 
31 static void
32 assert_hr_format(const char *format, const char *expected,
33  const char *alternate)
34 {
36  char *result = pcmk__time_format_hr(format, hr);
37 
39 
40  if (expected == NULL) {
41  assert_null(result);
42  return;
43  }
44 
45  assert_non_null(result);
46 
47  if (alternate == NULL) {
48  assert_string_equal(result, expected);
49  } else {
50  assert_true((strcmp(result, expected) == 0)
51  || (strcmp(result, alternate) == 0));
52  }
53 
54  free(result);
55 }
56 
57 static void
58 null_format(void **state)
59 {
60  assert_null(pcmk__time_format_hr(NULL, NULL));
61  assert_hr_format(NULL, NULL, NULL); // for pcmk__time_format_hr(NULL, hr)
62 }
63 
64 static void
65 no_specifiers(void **state)
66 {
67  assert_hr_format("no specifiers", "no specifiers", NULL);
68  assert_hr_format("this has a literal % in it",
69  "this has a literal % in it",
70  // *BSD strftime() strips single %
71  "this has a literal in it");
72  assert_hr_format("this has a literal %01 in it",
73  "this has a literal %01 in it",
74  // *BSD strftime() strips %0
75  "this has a literal 1 in it");
76  assert_hr_format("%2 this starts and ends with %",
77  "%2 this starts and ends with %",
78  // *BSD strftime() strips % in front of nonzero number
79  "2 this starts and ends with %");
80 
81  /* strftime() treats % with a number (and no specifier) as a literal string
82  * to be formatted with a field width (undocumented and probably a bug ...)
83  */
84  assert_hr_format("this ends with %10", "this ends with %10",
85  // *BSD strftime() strips % in front of nonzero number
86  "this ends with 10");
87 }
88 
89 static void
90 without_nano(void **state)
91 {
92  assert_hr_format("%H:%M %a %b %d", "03:04 Tue Jan 02", NULL);
93  assert_hr_format("%H:%M:%S", "03:04:05", NULL);
94  assert_hr_format("The time is %H:%M right now",
95  "The time is 03:04 right now", NULL);
96  assert_hr_format("%3S seconds", "005 seconds",
97  // *BSD strftime() doesn't support field widths
98  "3S seconds");
99 
100  // strftime() treats %% as a literal %
101  assert_hr_format("%%H %%N", "%H %N", NULL);
102 }
103 
104 static void
105 with_nano(void **state)
106 {
107  assert_hr_format("%H:%M:%S.%06N", "03:04:05.000000", NULL);
108  assert_hr_format("The time is %H:%M:%S.%06N right NOW",
109  "The time is 03:04:05.000000 right NOW", NULL);
110 }
111 
112 PCMK__UNIT_TEST(NULL, NULL,
113  cmocka_unit_test(null_format),
114  cmocka_unit_test(no_specifiers),
115  cmocka_unit_test(without_nano),
116  cmocka_unit_test(with_nano))
void pcmk__time_hr_free(pcmk__time_hr_t *hr_dt)
Definition: iso8601.c:2013
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
#define TEST_TIME
char * pcmk__time_format_hr(const char *format, const pcmk__time_hr_t *hr_dt)
Definition: iso8601.c:2029
pcmk__action_result_t result
Definition: pcmk_fence.c:35