pacemaker  2.1.9-49aab99839
Scalable High-Availability cluster resource manager
pcmk__g_strcat_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2020-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 General Public License version 2
7  * or later (GPLv2+) WITHOUT ANY WARRANTY.
8  */
9 
10 #include <crm_internal.h>
11 
13 
14 static void
15 add_to_null(void **state)
16 {
18  pcmk__assert_asserts(pcmk__g_strcat(NULL, "hello", NULL));
19 }
20 
21 static void
22 add_nothing(void **state)
23 {
24  GString *buf = g_string_new(NULL);
25 
26  // Start with empty string
27  pcmk__g_strcat(buf, NULL);
28  assert_string_equal((const char *) buf->str, "");
29 
30  pcmk__g_strcat(buf, "", NULL);
31  assert_string_equal((const char *) buf->str, "");
32 
33  // Start with populated string
34  g_string_append(buf, "hello");
35  pcmk__g_strcat(buf, NULL);
36  assert_string_equal((const char *) buf->str, "hello");
37 
38  pcmk__g_strcat(buf, "", NULL);
39  assert_string_equal((const char *) buf->str, "hello");
40  g_string_free(buf, TRUE);
41 }
42 
43 static void
44 add_words(void **state)
45 {
46  GString *buf = g_string_new(NULL);
47 
48  // Verify a call with multiple words
49  pcmk__g_strcat(buf, "hello", " ", NULL);
50  assert_string_equal((const char *) buf->str, "hello ");
51 
52  // Verify that a second call doesn't overwrite the first one
53  pcmk__g_strcat(buf, "world", NULL);
54  assert_string_equal((const char *) buf->str, "hello world");
55  g_string_free(buf, TRUE);
56 }
57 
58 static void
59 stop_early(void **state)
60 {
61  GString *buf = g_string_new(NULL);
62 
63  // NULL anywhere after buf in the arg list should cause a return
64  pcmk__g_strcat(buf, "hello", NULL, " world", NULL);
65  assert_string_equal((const char *) buf->str, "hello");
66  g_string_free(buf, TRUE);
67 }
68 
69 PCMK__UNIT_TEST(NULL, NULL,
70  cmocka_unit_test(add_to_null),
71  cmocka_unit_test(add_nothing),
72  cmocka_unit_test(add_words),
73  cmocka_unit_test(stop_early))
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)
void pcmk__g_strcat(GString *buffer,...) G_GNUC_NULL_TERMINATED
Definition: strings.c:1308
#define pcmk__assert_asserts(stmt)