This source file includes following definitions.
- assert_hr_format
- null_format
- no_specifiers
- without_nano
- with_nano
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <stdio.h>
13
14 #include <crm/common/iso8601_internal.h>
15 #include <crm/common/unittest_internal.h>
16
17 #define TEST_TIME pcmk__time_hr_new("2024-01-02 03:04:05 +00:00")
18
19
20
21
22
23
24
25
26
27
28
29
30
31 static void
32 assert_hr_format(const char *format, const char *expected,
33 const char *alternate)
34 {
35 pcmk__time_hr_t *hr = TEST_TIME;
36 char *result = pcmk__time_format_hr(format, hr);
37
38 pcmk__time_hr_free(hr);
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);
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
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
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
79 "2 this starts and ends with %");
80
81
82
83
84 assert_hr_format("this ends with %10", "this ends with %10",
85
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
98 "3S seconds");
99
100
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))