pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__trim_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2022 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 Lesser General Public License
7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
12 #include <stdarg.h>
13 #include <stddef.h>
14 #include <stdint.h>
15 #include <setjmp.h>
16 #include <cmocka.h>
17 
18 #include <string.h>
19 
20 static void
21 empty_input(void **state)
22 {
23  char *s = strdup("");
24 
25  assert_null(pcmk__trim(NULL));
26  assert_string_equal(pcmk__trim(s), "");
27 
28  free(s);
29 }
30 
31 static void
32 leading_newline(void **state)
33 {
34  char *s = strdup("\nabcd");
35 
36  assert_string_equal(pcmk__trim(s), "\nabcd");
37  free(s);
38 }
39 
40 static void
41 middle_newline(void **state)
42 {
43  char *s = strdup("ab\ncd");
44 
45  assert_string_equal(pcmk__trim(s), "ab\ncd");
46  free(s);
47 }
48 
49 static void
50 trailing_newline(void **state)
51 {
52  char *s = strdup("abcd\n\n");
53 
54  assert_string_equal(pcmk__trim(s), "abcd");
55  free(s);
56 
57  s = strdup("abcd\n ");
58  assert_string_equal(pcmk__trim(s), "abcd\n ");
59  free(s);
60 }
61 
62 static void
63 other_whitespace(void **state)
64 {
65  char *s = strdup(" ab\t\ncd \t");
66 
67  assert_string_equal(pcmk__trim(s), " ab\t\ncd \t");
68  free(s);
69 }
70 
71 int main(int argc, char **argv) {
72  const struct CMUnitTest tests[] = {
73  cmocka_unit_test(empty_input),
74  cmocka_unit_test(leading_newline),
75  cmocka_unit_test(middle_newline),
76  cmocka_unit_test(trailing_newline),
77  cmocka_unit_test(other_whitespace),
78  };
79 
80  cmocka_set_message_output(CM_OUTPUT_TAP);
81  return cmocka_run_group_tests(tests, NULL, NULL);
82 }
int main(int argc, char **argv)
char * pcmk__trim(char *str)
Definition: strings.c:456