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

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input
  2. leading_newline
  3. middle_newline
  4. trailing_newline
  5. other_whitespace
  6. main

   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 #include <string.h>
  19 
  20 static void
  21 empty_input(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  22 {
  23     char *s = strdup("");
  24 
  25     assert_null(pcmk__trim(NULL));
  26     assert_string_equal(pcmk__trim(s), "");
  27 
  28     free(s);
  29 }
  30 
  31 static void
  32 leading_newline(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34     char *s = strdup("\nabcd");
  35 
  36     assert_string_equal(pcmk__trim(s), "\nabcd");
  37     free(s);
  38 }
  39 
  40 static void
  41 middle_newline(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  42 {
  43     char *s = strdup("ab\ncd");
  44 
  45     assert_string_equal(pcmk__trim(s), "ab\ncd");
  46     free(s);
  47 }
  48 
  49 static void
  50 trailing_newline(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  51 {
  52     char *s = strdup("abcd\n\n");
  53 
  54     assert_string_equal(pcmk__trim(s), "abcd");
  55     free(s);
  56 
  57     s = strdup("abcd\n ");
  58     assert_string_equal(pcmk__trim(s), "abcd\n ");
  59     free(s);
  60 }
  61 
  62 static void
  63 other_whitespace(void **state)
     /* [previous][next][first][last][top][bottom][index][help] */
  64 {
  65     char *s = strdup("  ab\t\ncd  \t");
  66 
  67     assert_string_equal(pcmk__trim(s), "  ab\t\ncd  \t");
  68     free(s);
  69 }
  70 
  71 int main(int argc, char **argv) {
     /* [previous][next][first][last][top][bottom][index][help] */
  72     const struct CMUnitTest tests[] = {
  73         cmocka_unit_test(empty_input),
  74         cmocka_unit_test(leading_newline),
  75         cmocka_unit_test(middle_newline),
  76         cmocka_unit_test(trailing_newline),
  77         cmocka_unit_test(other_whitespace),
  78     };
  79 
  80     cmocka_set_message_output(CM_OUTPUT_TAP);
  81     return cmocka_run_group_tests(tests, NULL, NULL);
  82 }

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