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 <stdio.h>
11 #include <string.h>
12 #include <sys/types.h>
13 #include <crm_internal.h>
14 
15 static void
add_words(void)16 add_words(void)
17 {
18     char *list = NULL;
19     size_t list_len = 0;
20 
21     pcmk__add_word(&list, &list_len, "hello");
22     pcmk__add_word(&list, &list_len, "world");
23     g_assert_cmpint(strcmp(list, "hello world"), ==, 0);
24     free(list);
25 }
26 
27 static void
add_with_no_len(void)28 add_with_no_len(void)
29 {
30     char *list = NULL;
31 
32     pcmk__add_word(&list, NULL, "hello");
33     pcmk__add_word(&list, NULL, "world");
34     g_assert_cmpint(strcmp(list, "hello world"), ==, 0);
35     free(list);
36 }
37 
38 static void
add_nothing(void)39 add_nothing(void)
40 {
41     char *list = NULL;
42 
43     pcmk__add_word(&list, NULL, "hello");
44     pcmk__add_word(&list, NULL, NULL);
45     pcmk__add_word(&list, NULL, "");
46     g_assert_cmpint(strcmp(list, "hello"), ==, 0);
47     free(list);
48 }
49 
50 static void
add_with_null(void)51 add_with_null(void)
52 {
53     char *list = NULL;
54     size_t list_len = 0;
55 
56     pcmk__add_separated_word(&list, &list_len, "hello", NULL);
57     pcmk__add_separated_word(&list, &list_len, "world", NULL);
58     pcmk__add_separated_word(&list, &list_len, "I am a unit test", NULL);
59     g_assert_cmpint(strcmp(list, "hello world I am a unit test"), ==, 0);
60     free(list);
61 }
62 
63 static void
add_with_comma(void)64 add_with_comma(void)
65 {
66     char *list = NULL;
67     size_t list_len = 0;
68 
69     pcmk__add_separated_word(&list, &list_len, "hello", ",");
70     pcmk__add_separated_word(&list, &list_len, "world", ",");
71     pcmk__add_separated_word(&list, &list_len, "I am a unit test", ",");
72     g_assert_cmpint(strcmp(list, "hello,world,I am a unit test"), ==, 0);
73     free(list);
74 }
75 
76 static void
add_with_comma_and_space(void)77 add_with_comma_and_space(void)
78 {
79     char *list = NULL;
80     size_t list_len = 0;
81 
82     pcmk__add_separated_word(&list, &list_len, "hello", ", ");
83     pcmk__add_separated_word(&list, &list_len, "world", ", ");
84     pcmk__add_separated_word(&list, &list_len, "I am a unit test", ", ");
85     g_assert_cmpint(strcmp(list, "hello, world, I am a unit test"), ==, 0);
86     free(list);
87 }
88 
89 int
main(int argc,char ** argv)90 main(int argc, char **argv)
91 {
92     g_test_init(&argc, &argv, NULL);
93 
94     g_test_add_func("/common/strings/add_word/add_words", add_words);
95     g_test_add_func("/common/strings/add_word/add_with_no_len",
96                     add_with_no_len);
97     g_test_add_func("/common/strings/add_word/add_nothing", add_nothing);
98     g_test_add_func("/common/strings/add_word/add_with_null", add_with_null);
99     g_test_add_func("/common/strings/add_word/add_with_comma", add_with_comma);
100     g_test_add_func("/common/strings/add_word/add_with_comma_and_space",
101                     add_with_comma_and_space);
102 
103     return g_test_run();
104 }
105