diff --git a/lib/common/tests/utils/Makefile.am b/lib/common/tests/utils/Makefile.am index a70f681754..50359a6e73 100644 --- a/lib/common/tests/utils/Makefile.am +++ b/lib/common/tests/utils/Makefile.am @@ -1,30 +1,32 @@ # # 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. # include $(top_srcdir)/lib/common/mock.mk AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/lib/common LDADD = $(top_builddir)/lib/common/libcrmcommon.la -lcmocka include $(top_srcdir)/mk/tap.mk pcmk_hostname_test_LDADD = $(top_builddir)/lib/common/libcrmcommon_test.la -lcmocka pcmk_hostname_test_LDFLAGS = -Wl,--wrap=uname # Add "_test" to the end of all test program names to simplify .gitignore. check_PROGRAMS = \ char2score_test \ pcmk_str_is_infinity_test \ - pcmk_str_is_minus_infinity_test + pcmk_str_is_minus_infinity_test \ + score2char_stack_test \ + score2char_test if WRAPPABLE_UNAME check_PROGRAMS += pcmk_hostname_test endif TESTS = $(check_PROGRAMS) diff --git a/lib/common/tests/utils/score2char_stack_test.c b/lib/common/tests/utils/score2char_stack_test.c new file mode 100644 index 0000000000..3cd69965fa --- /dev/null +++ b/lib/common/tests/utils/score2char_stack_test.c @@ -0,0 +1,70 @@ +/* + * 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 +invalid_params(void **state) +{ + char *buf = malloc(9); + + assert_null(score2char_stack(100, NULL, 9)); + assert_null(score2char_stack(100, buf, 9)); + + free(buf); +} + +static void +outside_limits(void **state) +{ + char *buf = malloc(10); + + buf = score2char_stack(CRM_SCORE_INFINITY * 2, buf, 10); + assert_string_equal(buf, CRM_INFINITY_S); + + buf = score2char_stack(-CRM_SCORE_INFINITY * 2, buf, 10); + assert_string_equal(buf, CRM_MINUS_INFINITY_S); + + free(buf); +} + +static void +inside_limits(void **state) +{ + char *buf = malloc(10); + + buf = score2char_stack(0, buf, 10); + assert_string_equal(buf, "0"); + + buf = score2char_stack(1024, buf, 10); + assert_string_equal(buf, "1024"); + + buf = score2char_stack(-1024, buf, 10); + assert_string_equal(buf, "-1024"); + + free(buf); +} + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(invalid_params), + cmocka_unit_test(outside_limits), + cmocka_unit_test(inside_limits), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +} diff --git a/lib/common/tests/utils/score2char_test.c b/lib/common/tests/utils/score2char_test.c new file mode 100644 index 0000000000..98bbd8d1eb --- /dev/null +++ b/lib/common/tests/utils/score2char_test.c @@ -0,0 +1,59 @@ +/* + * 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 +outside_limits(void **state) +{ + char *a = NULL; + + a = score2char(CRM_SCORE_INFINITY * 2); + assert_string_equal(a, CRM_INFINITY_S); + free(a); + + a = score2char(-CRM_SCORE_INFINITY * 2); + assert_string_equal(a, CRM_MINUS_INFINITY_S); + free(a); +} + +static void +inside_limits(void **state) +{ + char *a = NULL; + + a = score2char(1024); + assert_string_equal(a, "1024"); + free(a); + + a = score2char(0); + assert_string_equal(a, "0"); + free(a); + + a = score2char(-1024); + assert_string_equal(a, "-1024"); + free(a); +} + +int main(int argc, char **argv) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(outside_limits), + cmocka_unit_test(inside_limits), + }; + + cmocka_set_message_output(CM_OUTPUT_TAP); + return cmocka_run_group_tests(tests, NULL, NULL); +}