From: Alejandro Colomar Date: Wed, 20 Dec 2023 21:09:36 +0000 (+0100) Subject: tests/unit/test_adds.c: Test addsl() and addsl3() X-Git-Tag: 4.15.0-rc1~57 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=173231a8ffa90fae80d26d15355590315f6b0f5d;p=thirdparty%2Fshadow.git tests/unit/test_adds.c: Test addsl() and addsl3() Signed-off-by: Alejandro Colomar --- diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 03240010b..993bc2007 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -4,6 +4,7 @@ if HAVE_CMOCKA TESTS = $(check_PROGRAMS) check_PROGRAMS = \ + test_adds \ test_chkname \ test_sprintf \ test_strncpy \ @@ -18,6 +19,19 @@ endif # ENABLE_LOGIND check_PROGRAMS += \ $(NULL) +test_adds_SOURCES = \ + ../../lib/adds.c \ + test_adds.c \ + $(NULL) +test_adds_CFLAGS = \ + $(AM_FLAGS) \ + $(NULL) +test_adds_LDFLAGS = \ + $(NULL) +test_adds_LDADD = \ + $(CMOCKA_LIBS) \ + $(NULL) + test_chkname_SOURCES = \ ../../lib/chkname.c \ test_chkname.c \ diff --git a/tests/unit/test_adds.c b/tests/unit/test_adds.c new file mode 100644 index 000000000..1435bd4e0 --- /dev/null +++ b/tests/unit/test_adds.c @@ -0,0 +1,96 @@ +// SPDX-FileCopyrightText: 2023, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include // Required by +#include // Required by +#include // Required by +#include // Required by +#include + +#include "adds.h" + + +static void test_addsl_ok(void **state); +static void test_addsl_underflow(void **state); +static void test_addsl_overflow(void **state); +static void test_addsl3_ok(void **state); +static void test_addsl3_underflow(void **state); +static void test_addsl3_overflow(void **state); + + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_addsl_ok), + cmocka_unit_test(test_addsl_underflow), + cmocka_unit_test(test_addsl_overflow), + cmocka_unit_test(test_addsl3_ok), + cmocka_unit_test(test_addsl3_underflow), + cmocka_unit_test(test_addsl3_overflow), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} + + +static void +test_addsl_ok(void **state) +{ + assert_true(addsl(1, 3) == 1 + 3); + assert_true(addsl(-4321, 7) == -4321 + 7); + assert_true(addsl(1, 1) == 1 + 1); + assert_true(addsl(-1, -2) == -1 - 2); + assert_true(addsl(LONG_MAX, -1) == LONG_MAX - 1); + assert_true(addsl(LONG_MIN, 1) == LONG_MIN + 1); + assert_true(addsl(LONG_MIN, LONG_MAX) == LONG_MIN + LONG_MAX); + assert_true(addsl(0, 0) == 0); +} + + +static void +test_addsl_underflow(void **state) +{ + assert_true(addsl(LONG_MIN, -1) == LONG_MIN); + assert_true(addsl(LONG_MIN + 3, -7) == LONG_MIN); + assert_true(addsl(LONG_MIN, LONG_MIN) == LONG_MIN); +} + + +static void +test_addsl_overflow(void **state) +{ + assert_true(addsl(LONG_MAX, 1) == LONG_MAX); + assert_true(addsl(LONG_MAX - 3, 7) == LONG_MAX); + assert_true(addsl(LONG_MAX, LONG_MAX) == LONG_MAX); +} + + +static void +test_addsl3_ok(void **state) +{ + assert_true(addsl3(1, 2, 3) == 1 + 2 + 3); + assert_true(addsl3(LONG_MIN, -3, 4) == LONG_MIN + 4 - 3); + assert_true(addsl3(LONG_MAX, LONG_MAX, LONG_MIN) + == LONG_MAX + LONG_MIN + LONG_MAX); +} + + +static void +test_addsl3_underflow(void **state) +{ + assert_true(addsl3(LONG_MIN, 2, -3) == LONG_MIN); + assert_true(addsl3(LONG_MIN, -1, 0) == LONG_MIN); +} + + +static void +test_addsl3_overflow(void **state) +{ + assert_true(addsl3(LONG_MAX, -1, 2) == LONG_MAX); + assert_true(addsl3(LONG_MAX, +1, 0) == LONG_MAX); + assert_true(addsl3(LONG_MAX, LONG_MAX, 0)== LONG_MAX); +}