diff --git a/lib/common/tests/strings/Makefile.am b/lib/common/tests/strings/Makefile.am index 7fb9ae71fc..3d8c26fd47 100644 --- a/lib/common/tests/strings/Makefile.am +++ b/lib/common/tests/strings/Makefile.am @@ -1,34 +1,35 @@ # # Copyright 2020-2022 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 -lcmocka include $(top_srcdir)/mk/tap.mk # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = \ crm_get_msec_test \ 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__numeric_strcasecmp_test \ pcmk__parse_ll_range_test \ pcmk__scan_double_test \ pcmk__scan_min_int_test \ pcmk__scan_port_test \ pcmk__starts_with_test \ pcmk__str_any_of_test \ pcmk__str_in_list_test \ pcmk__strcmp_test \ pcmk__trim_test TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c b/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c new file mode 100644 index 0000000000..4ead552563 --- /dev/null +++ b/lib/common/tests/strings/pcmk__numeric_strcasecmp_test.c @@ -0,0 +1,82 @@ +/* + * Copyright 2022 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 +#include +#include + +static void +no_numbers(void **state) +{ + /* All comparisons are done case-insensitively. */ + assert_int_equal(pcmk__numeric_strcasecmp("abcd", "efgh"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("abcd", "abcd"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("efgh", "abcd"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("AbCd", "eFgH"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("ABCD", "abcd"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("EFgh", "ABcd"), 1); +} + +static void +trailing_numbers(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node2"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node1"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("node2", "node1"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node10"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node10", "node10"), 0); + assert_int_equal(pcmk__numeric_strcasecmp("node10", "node1"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node10", "remotenode9"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("remotenode9", "node10"), 1); + + /* Longer numbers sort higher than shorter numbers. */ + assert_int_equal(pcmk__numeric_strcasecmp("node001", "node1"), 1); + assert_int_equal(pcmk__numeric_strcasecmp("node1", "node001"), -1); +} + +static void +middle_numbers(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1def"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1def", "node1abc"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node2abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node2abc", "node1abc"), 1); +} + +static void +unequal_lengths(void **state) +{ + assert_int_equal(pcmk__numeric_strcasecmp("node-ab", "node-abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node-abc", "node-ab"), 1); + + assert_int_equal(pcmk__numeric_strcasecmp("node1ab", "node1abc"), -1); + assert_int_equal(pcmk__numeric_strcasecmp("node1abc", "node1ab"), 1); +} + +int +main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(no_numbers), + cmocka_unit_test(trailing_numbers), + cmocka_unit_test(middle_numbers), + cmocka_unit_test(unequal_lengths), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}