tests/unit/test_typetraits.c: Add tests for typetraits.h macros

Suggested-by: "Serge E. Hallyn" <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar 2024-06-29 19:32:32 +02:00
parent 500ec3f8f3
commit 24695e6f38
2 changed files with 68 additions and 0 deletions

View File

@ -10,6 +10,7 @@ check_PROGRAMS = \
test_sprintf \
test_strncpy \
test_strtcpy \
test_typetraits \
test_xasprintf \
test_zustr2stp
@ -115,6 +116,18 @@ test_strtcpy_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)
test_typetraits_SOURCES = \
test_typetraits.c \
$(NULL)
test_typetraits_CFLAGS = \
$(AM_CFLAGS) \
$(NULL)
test_typetraits_LDFLAGS = \
$(NULL)
test_typetraits_LDADD = \
$(CMOCKA_LIBS) \
$(NULL)
test_xasprintf_SOURCES = \
../../lib/string/sprintf.c \
test_xasprintf.c \

View File

@ -0,0 +1,55 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
// SPDX-License-Identifier: BSD-3-Clause
#include <config.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 "typetraits.h"
static void test_type_max(void **state);
static void test_type_min(void **state);
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_type_max),
cmocka_unit_test(test_type_min),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}
static void
test_type_max(void **state)
{
assert_true(type_max(long) == LONG_MAX);
assert_true(type_max(unsigned long) == ULONG_MAX);
assert_true(type_max(int) == INT_MAX);
assert_true(type_max(unsigned short) == USHRT_MAX);
assert_true(type_max(char) == CHAR_MAX);
assert_true(type_max(signed char) == SCHAR_MAX);
assert_true(type_max(unsigned char) == UCHAR_MAX);
}
static void
test_type_min(void **state)
{
assert_true(type_min(long) == LONG_MIN);
assert_true(type_min(unsigned long) == 0);
assert_true(type_min(int) == INT_MIN);
assert_true(type_min(unsigned short) == 0);
assert_true(type_min(char) == CHAR_MIN);
assert_true(type_min(signed char) == SCHAR_MIN);
assert_true(type_min(unsigned char) == 0);
}