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

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

DEFINITIONS

This source file includes following definitions.
  1. empty_input_list
  2. empty_string
  3. star_matches
  4. star_doesnt_match
  5. in_list
  6. not_in_list
  7. main

   1 /*
   2  * Copyright 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 <crm_internal.h>
  11 
  12 #include <glib.h>
  13 
  14 #include <stdarg.h>
  15 #include <stddef.h>
  16 #include <stdint.h>
  17 #include <setjmp.h>
  18 #include <cmocka.h>
  19 
  20 static void
  21 empty_input_list(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  22     assert_false(pcmk__str_in_list(NULL, NULL, pcmk__str_none));
  23     assert_false(pcmk__str_in_list(NULL, NULL, pcmk__str_null_matches));
  24     assert_false(pcmk__str_in_list("xxx", NULL, pcmk__str_none));
  25     assert_false(pcmk__str_in_list("", NULL, pcmk__str_none));
  26 }
  27 
  28 static void
  29 empty_string(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  30     GList *list = NULL;
  31 
  32     list = g_list_prepend(list, (gpointer) "xxx");
  33 
  34     assert_false(pcmk__str_in_list(NULL, list, pcmk__str_none));
  35     assert_true(pcmk__str_in_list(NULL, list, pcmk__str_null_matches));
  36     assert_false(pcmk__str_in_list("", list, pcmk__str_none));
  37     assert_false(pcmk__str_in_list("", list, pcmk__str_null_matches));
  38 
  39     g_list_free(list);
  40 }
  41 
  42 static void
  43 star_matches(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  44     GList *list = NULL;
  45 
  46     list = g_list_prepend(list, (gpointer) "*");
  47     list = g_list_append(list, (gpointer) "more");
  48 
  49     assert_true(pcmk__str_in_list("xxx", list, pcmk__str_star_matches));
  50     assert_true(pcmk__str_in_list("yyy", list, pcmk__str_star_matches));
  51     assert_true(pcmk__str_in_list("XXX", list, pcmk__str_star_matches|pcmk__str_casei));
  52     assert_true(pcmk__str_in_list("", list, pcmk__str_star_matches));
  53 
  54     g_list_free(list);
  55 }
  56 
  57 static void
  58 star_doesnt_match(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  59     GList *list = NULL;
  60 
  61     list = g_list_prepend(list, (gpointer) "*");
  62 
  63     assert_false(pcmk__str_in_list("xxx", list, pcmk__str_none));
  64     assert_false(pcmk__str_in_list("yyy", list, pcmk__str_none));
  65     assert_false(pcmk__str_in_list("XXX", list, pcmk__str_casei));
  66     assert_false(pcmk__str_in_list("", list, pcmk__str_none));
  67     assert_false(pcmk__str_in_list(NULL, list, pcmk__str_star_matches));
  68 
  69     g_list_free(list);
  70 }
  71 
  72 static void
  73 in_list(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  74     GList *list = NULL;
  75 
  76     list = g_list_prepend(list, (gpointer) "xxx");
  77     list = g_list_prepend(list, (gpointer) "yyy");
  78     list = g_list_prepend(list, (gpointer) "zzz");
  79 
  80     assert_true(pcmk__str_in_list("xxx", list, pcmk__str_none));
  81     assert_true(pcmk__str_in_list("XXX", list, pcmk__str_casei));
  82     assert_true(pcmk__str_in_list("yyy", list, pcmk__str_none));
  83     assert_true(pcmk__str_in_list("YYY", list, pcmk__str_casei));
  84     assert_true(pcmk__str_in_list("zzz", list, pcmk__str_none));
  85     assert_true(pcmk__str_in_list("ZZZ", list, pcmk__str_casei));
  86 
  87     g_list_free(list);
  88 }
  89 
  90 static void
  91 not_in_list(void **state) {
     /* [previous][next][first][last][top][bottom][index][help] */
  92     GList *list = NULL;
  93 
  94     list = g_list_prepend(list, (gpointer) "xxx");
  95     list = g_list_prepend(list, (gpointer) "yyy");
  96 
  97     assert_false(pcmk__str_in_list("xx", list, pcmk__str_none));
  98     assert_false(pcmk__str_in_list("XXX", list, pcmk__str_none));
  99     assert_false(pcmk__str_in_list("zzz", list, pcmk__str_none));
 100     assert_false(pcmk__str_in_list("zzz", list, pcmk__str_casei));
 101 
 102     g_list_free(list);
 103 }
 104 
 105 int main(int argc, char **argv)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107     const struct CMUnitTest tests[] = {
 108         cmocka_unit_test(empty_input_list),
 109         cmocka_unit_test(empty_string),
 110         cmocka_unit_test(star_matches),
 111         cmocka_unit_test(star_doesnt_match),
 112         cmocka_unit_test(in_list),
 113         cmocka_unit_test(not_in_list),
 114     };
 115 
 116     cmocka_set_message_output(CM_OUTPUT_TAP);
 117     return cmocka_run_group_tests(tests, NULL, NULL);
 118 }

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