Files
shadow/tests/unit/test_adds.c
Alejandro Colomar 2e56af1902 lib/, tests/: addsl(): Add addsl(), a variadic macro
Add a variadic macro addsl() that accepts an arbitrary number of
addends, instead of having specific versions like addsl2() or addsl3().

It is internally implemented by the addslN() function, which itself
calls addsl2().  addsl3() is now obsolete and thus removed.

Code should just call addsl().

Link: <https://github.com/shadow-maint/shadow/pull/882#discussion_r1437155212>
Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
2024-01-16 16:58:18 +01:00

106 lines
2.6 KiB
C

// 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_2_ok(void **state);
static void test_addsl_2_underflow(void **state);
static void test_addsl_2_overflow(void **state);
static void test_addsl_3_ok(void **state);
static void test_addsl_3_underflow(void **state);
static void test_addsl_3_overflow(void **state);
static void test_addsl_5_ok(void **state);
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_addsl_2_ok),
cmocka_unit_test(test_addsl_2_underflow),
cmocka_unit_test(test_addsl_2_overflow),
cmocka_unit_test(test_addsl_3_ok),
cmocka_unit_test(test_addsl_3_underflow),
cmocka_unit_test(test_addsl_3_overflow),
cmocka_unit_test(test_addsl_5_ok),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
static void
test_addsl_2_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_2_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_2_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_addsl_3_ok(void **state)
{
assert_true(addsl(1, 2, 3) == 1 + 2 + 3);
assert_true(addsl(LONG_MIN, -3, 4) == LONG_MIN + 4 - 3);
assert_true(addsl(LONG_MAX, LONG_MAX, LONG_MIN)
== LONG_MAX + LONG_MIN + LONG_MAX);
}
static void
test_addsl_3_underflow(void **state)
{
assert_true(addsl(LONG_MIN, 2, -3) == LONG_MIN);
assert_true(addsl(LONG_MIN, -1, 0) == LONG_MIN);
}
static void
test_addsl_3_overflow(void **state)
{
assert_true(addsl(LONG_MAX, -1, 2) == LONG_MAX);
assert_true(addsl(LONG_MAX, +1, 0) == LONG_MAX);
assert_true(addsl(LONG_MAX, LONG_MAX, 0)== LONG_MAX);
}
static void
test_addsl_5_ok(void **state)
{
assert_true(addsl(LONG_MAX, LONG_MAX, LONG_MIN, LONG_MIN, 44) == 42);
}