tests/unit/test_adds.c: Test addsl() and addsl3()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Serge Hallyn
parent
89e5a32966
commit
173231a8ff
@@ -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 \
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
#include <stdarg.h> // Required by <cmocka.h>
|
||||
#include <stddef.h> // Required by <cmocka.h>
|
||||
#include <setjmp.h> // Required by <cmocka.h>
|
||||
#include <stdint.h> // Required by <cmocka.h>
|
||||
#include <cmocka.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
Reference in New Issue
Block a user