pacemaker  2.1.4-dc6eb4362
Scalable High-Availability cluster resource manager
pcmk__str_update_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 static void
19 update_null(void **state) {
20  char *str = NULL;
21 
22  // These just make sure they don't crash
23  pcmk__str_update(NULL, NULL);
24  pcmk__str_update(NULL, "value");
25 
26  // Update an already NULL string to NULL
27  pcmk__str_update(&str, NULL);
28  assert_null(str);
29 
30  // Update an already allocated string to NULL
31  str = strdup("hello");
32  pcmk__str_update(&str, NULL);
33  assert_null(str);
34 }
35 
36 static void
37 update_same(void **state) {
38  char *str = NULL;
39  char *saved = NULL;
40 
41  str = strdup("hello");
42  saved = str;
43  pcmk__str_update(&str, "hello");
44  assert_ptr_equal(saved, str); // No free and reallocation
45  free(str);
46 }
47 
48 static void
49 update_different(void **state) {
50  char *str = NULL;
51 
52  str = strdup("hello");
53  pcmk__str_update(&str, "world");
54  assert_string_equal(str, "world");
55  free(str);
56 }
57 
58 int
59 main(int argc, char **argv)
60 {
61  const struct CMUnitTest tests[] = {
62  cmocka_unit_test(update_null),
63  cmocka_unit_test(update_same),
64  cmocka_unit_test(update_different),
65  };
66 
67  cmocka_set_message_output(CM_OUTPUT_TAP);
68  return cmocka_run_group_tests(tests, NULL, NULL);
69 }
void pcmk__str_update(char **str, const char *value)
Definition: strings.c:1188
int main(int argc, char **argv)