pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__add_word_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-2021 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 <stdlib.h>
16 #include <string.h>
17 #include <setjmp.h>
18 #include <cmocka.h>
19 
20 static void
21 add_words(void **state)
22 {
23  char *list = NULL;
24  size_t list_len = 0;
25 
26  pcmk__add_word(&list, &list_len, "hello");
27  pcmk__add_word(&list, &list_len, "world");
28  assert_int_equal(strcmp(list, "hello world"), 0);
29  free(list);
30 }
31 
32 static void
33 add_with_no_len(void **state)
34 {
35  char *list = NULL;
36 
37  pcmk__add_word(&list, NULL, "hello");
38  pcmk__add_word(&list, NULL, "world");
39  assert_int_equal(strcmp(list, "hello world"), 0);
40  free(list);
41 }
42 
43 static void
44 add_nothing(void **state)
45 {
46  char *list = NULL;
47 
48  pcmk__add_word(&list, NULL, "hello");
49  pcmk__add_word(&list, NULL, NULL);
50  pcmk__add_word(&list, NULL, "");
51  assert_int_equal(strcmp(list, "hello"), 0);
52  free(list);
53 }
54 
55 static void
56 add_with_null(void **state)
57 {
58  char *list = NULL;
59  size_t list_len = 0;
60 
61  pcmk__add_separated_word(&list, &list_len, "hello", NULL);
62  pcmk__add_separated_word(&list, &list_len, "world", NULL);
63  pcmk__add_separated_word(&list, &list_len, "I am a unit test", NULL);
64  assert_int_equal(strcmp(list, "hello world I am a unit test"), 0);
65  free(list);
66 }
67 
68 static void
69 add_with_comma(void **state)
70 {
71  char *list = NULL;
72  size_t list_len = 0;
73 
74  pcmk__add_separated_word(&list, &list_len, "hello", ",");
75  pcmk__add_separated_word(&list, &list_len, "world", ",");
76  pcmk__add_separated_word(&list, &list_len, "I am a unit test", ",");
77  assert_int_equal(strcmp(list, "hello,world,I am a unit test"), 0);
78  free(list);
79 }
80 
81 static void
82 add_with_comma_and_space(void **state)
83 {
84  char *list = NULL;
85  size_t list_len = 0;
86 
87  pcmk__add_separated_word(&list, &list_len, "hello", ", ");
88  pcmk__add_separated_word(&list, &list_len, "world", ", ");
89  pcmk__add_separated_word(&list, &list_len, "I am a unit test", ", ");
90  assert_int_equal(strcmp(list, "hello, world, I am a unit test"), 0);
91  free(list);
92 }
93 
94 int
95 main(int argc, char **argv)
96 {
97  const struct CMUnitTest tests[] = {
98  cmocka_unit_test(add_words),
99  cmocka_unit_test(add_with_no_len),
100  cmocka_unit_test(add_nothing),
101  cmocka_unit_test(add_with_null),
102  cmocka_unit_test(add_with_comma),
103  cmocka_unit_test(add_with_comma_and_space),
104  };
105 
106  cmocka_set_message_output(CM_OUTPUT_TAP);
107  return cmocka_run_group_tests(tests, NULL, NULL);
108 }
void pcmk__add_separated_word(char **list, size_t *len, const char *word, const char *separator)
Definition: strings.c:703
int main(int argc, char **argv)