root/lib/common/tests/strings/pcmk__g_strcat_test.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. add_to_null
  2. add_nothing
  3. add_words
  4. stop_early

   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 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 <crm/common/unittest_internal.h>
  13 
  14 static void
  15 add_to_null(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  16 {
  17     pcmk__assert_asserts(pcmk__g_strcat(NULL, NULL));
  18     pcmk__assert_asserts(pcmk__g_strcat(NULL, "hello", NULL));
  19 }
  20 
  21 static void
  22 add_nothing(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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)
     /* [previous][next][first][last][top][bottom][index][help] */
  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))

/* [previous][next][first][last][top][bottom][index][help] */