diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index abfa03eb6b..5486cc627e 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,40 +1,41 @@ # # 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 = crm_is_true_test \ crm_str_to_boolean_test \ pcmk__add_word_test \ pcmk__btoa_test \ pcmk__char_in_any_str_test \ + pcmk__ends_with_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ pcmk__starts_with_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__ends_with_test.c b/lib/common/tests/strings/pcmk__ends_with_test.c new file mode 100644 index 0000000000..09085a181a --- /dev/null +++ b/lib/common/tests/strings/pcmk__ends_with_test.c @@ -0,0 +1,61 @@ +/* + * 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 + +static void +bad_input(void) { + g_assert_false(pcmk__ends_with(NULL, "xyz")); + + g_assert_true(pcmk__ends_with(NULL, NULL)); + g_assert_true(pcmk__ends_with(NULL, "")); + g_assert_true(pcmk__ends_with("", NULL)); + g_assert_true(pcmk__ends_with("", "")); + g_assert_true(pcmk__ends_with("abc", NULL)); + g_assert_true(pcmk__ends_with("abc", "")); +} + +static void +ends_with(void) { + g_assert_true(pcmk__ends_with("abc", "abc")); + g_assert_true(pcmk__ends_with("abc", "bc")); + g_assert_true(pcmk__ends_with("abc", "c")); + g_assert_true(pcmk__ends_with("abcbc", "bc")); + + g_assert_false(pcmk__ends_with("abc", "def")); + g_assert_false(pcmk__ends_with("abc", "defg")); + g_assert_false(pcmk__ends_with("abc", "bcd")); + g_assert_false(pcmk__ends_with("abc", "ab")); + + g_assert_false(pcmk__ends_with("abc", "BC")); +} + +static void +ends_with_ext(void) { + g_assert_true(pcmk__ends_with_ext("ab.c", ".c")); + g_assert_true(pcmk__ends_with_ext("ab.cb.c", ".c")); + + g_assert_false(pcmk__ends_with_ext("ab.c", ".def")); + g_assert_false(pcmk__ends_with_ext("ab.c", ".defg")); + g_assert_false(pcmk__ends_with_ext("ab.c", ".cd")); + g_assert_false(pcmk__ends_with_ext("ab.c", "ab")); + + g_assert_false(pcmk__ends_with_ext("ab.c", ".C")); +} + +int +main(int argc, char **argv) +{ + g_test_init(&argc, &argv, NULL); + + g_test_add_func("/common/strings/ends_with/bad_input", bad_input); + g_test_add_func("/common/strings/ends_with/ends_with", ends_with); + g_test_add_func("/common/strings/ends_with/ends_with_ext", ends_with_ext); + return g_test_run(); +}