diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index 1967c3dc46..fd1e794217 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,36 +1,37 @@ # -# Copyright 2020 the Pacemaker project contributors +# Copyright 2020-2021 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include LDADD = $(top_builddir)/lib/common/libcrmcommon.la include $(top_srcdir)/mk/glib-tap.mk # Add each test program here. Each test should be written as a little standalone # program using the glib unit testing functions. See the documentation for more # information. # # https://developer.gnome.org/glib/unstable/glib-Testing.html # # Add "_test" to the end of all test program names to simplify .gitignore. test_programs = pcmk__add_word_test \ pcmk__btoa_test \ pcmk__char_in_any_str_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ pcmk__str_any_of_test \ + pcmk__str_in_list_test \ pcmk__strcmp_test # If any extra data needs to be added to the source distribution, add it to the # following list. dist_test_data = # If any extra data needs to be used by tests but should not be added to the # source distribution, add it to the following list. test_data = diff --git a/lib/common/tests/strings/pcmk__str_in_list_test.c b/lib/common/tests/strings/pcmk__str_in_list_test.c new file mode 100644 index 0000000000..809d760e5c --- /dev/null +++ b/lib/common/tests/strings/pcmk__str_in_list_test.c @@ -0,0 +1,114 @@ +/* + * Copyright 2021 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU Lesser General Public License + * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY. + */ + +#include + +#include +#include +#include + +static void +empty_input_list(void) { + g_assert_false(pcmk__str_in_list(NULL, NULL, pcmk__str_none)); + g_assert_false(pcmk__str_in_list(NULL, NULL, pcmk__str_null_matches)); + g_assert_false(pcmk__str_in_list(NULL, "xxx", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(NULL, "", pcmk__str_none)); +} + +static void +empty_string(void) { + GList *list = NULL; + + list = g_list_prepend(list, (gpointer) "xxx"); + + g_assert_false(pcmk__str_in_list(list, NULL, pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, NULL, pcmk__str_null_matches)); + g_assert_false(pcmk__str_in_list(list, "", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "", pcmk__str_null_matches)); + + g_list_free(list); +} + +static void +star_matches(void) { + GList *list = NULL; + + list = g_list_prepend(list, (gpointer) "*"); + + g_assert_true(pcmk__str_in_list(list, "xxx", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, "yyy", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, "XXX", pcmk__str_casei)); + g_assert_true(pcmk__str_in_list(list, "", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, NULL, pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, NULL, pcmk__str_null_matches)); + + g_list_free(list); +} + +static void +star_doesnt_match(void) { + GList *list = NULL; + + list = g_list_prepend(list, (gpointer) "*"); + list = g_list_append(list, (gpointer) "more"); + + g_assert_false(pcmk__str_in_list(list, "xxx", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "yyy", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "XXX", pcmk__str_casei)); + g_assert_false(pcmk__str_in_list(list, "", pcmk__str_none)); + + g_list_free(list); +} + +static void +in_list(void) { + GList *list = NULL; + + list = g_list_prepend(list, (gpointer) "xxx"); + list = g_list_prepend(list, (gpointer) "yyy"); + list = g_list_prepend(list, (gpointer) "zzz"); + + g_assert_true(pcmk__str_in_list(list, "xxx", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, "XXX", pcmk__str_casei)); + g_assert_true(pcmk__str_in_list(list, "yyy", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, "YYY", pcmk__str_casei)); + g_assert_true(pcmk__str_in_list(list, "zzz", pcmk__str_none)); + g_assert_true(pcmk__str_in_list(list, "ZZZ", pcmk__str_casei)); + + g_list_free(list); +} + +static void +not_in_list(void) { + GList *list = NULL; + + list = g_list_prepend(list, (gpointer) "xxx"); + list = g_list_prepend(list, (gpointer) "yyy"); + + g_assert_false(pcmk__str_in_list(list, "xx", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "XXX", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "zzz", pcmk__str_none)); + g_assert_false(pcmk__str_in_list(list, "zzz", pcmk__str_casei)); + + g_list_free(list); +} + +int main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/common/strings/in_list/empty_list", empty_input_list); + g_test_add_func("/common/strings/in_list/empty_string", empty_string); + g_test_add_func("/common/strings/in_list/star_matches", star_matches); + g_test_add_func("/common/strings/in_list/star_doesnt_match", star_doesnt_match); + g_test_add_func("/common/strings/in_list/in", in_list); + g_test_add_func("/common/strings/in_list/not_in", not_in_list); + + return g_test_run(); +}