tests/unit/test_strncpy.c: Test STRNCPY()
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
committed by
Serge Hallyn
parent
ce30dfe255
commit
fe62fc48bf
@@ -4,6 +4,7 @@ if HAVE_CMOCKA
|
||||
TESTS = $(check_PROGRAMS)
|
||||
|
||||
check_PROGRAMS = \
|
||||
test_strncpy \
|
||||
test_strtcpy \
|
||||
test_xasprintf
|
||||
|
||||
@@ -32,6 +33,18 @@ test_logind_LDADD = \
|
||||
$(LIBSYSTEMD) \
|
||||
$(NULL)
|
||||
|
||||
test_strncpy_SOURCES = \
|
||||
test_strncpy.c \
|
||||
$(NULL)
|
||||
test_strncpy_CFLAGS = \
|
||||
$(AM_FLAGS) \
|
||||
$(NULL)
|
||||
test_strncpy_LDFLAGS = \
|
||||
$(NULL)
|
||||
test_strncpy_LDADD = \
|
||||
$(CMOCKA_LIBS) \
|
||||
$(NULL)
|
||||
|
||||
test_strtcpy_SOURCES = \
|
||||
../../lib/strtcpy.c \
|
||||
test_strtcpy.c \
|
||||
|
||||
85
tests/unit/test_strncpy.c
Normal file
85
tests/unit/test_strncpy.c
Normal file
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <string.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 "sizeof.h"
|
||||
#include "strncpy.h"
|
||||
|
||||
|
||||
static void test_STRNCPY_trunc(void **state);
|
||||
static void test_STRNCPY_fit(void **state);
|
||||
static void test_STRNCPY_pad(void **state);
|
||||
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const struct CMUnitTest tests[] = {
|
||||
cmocka_unit_test(test_STRNCPY_trunc),
|
||||
cmocka_unit_test(test_STRNCPY_fit),
|
||||
cmocka_unit_test(test_STRNCPY_pad),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_trunc(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[4] = {'f', 'o', 'o', 'o'};
|
||||
char res1[3] = {'f', 'o', 'o'};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[5] = "barb";
|
||||
char res2[3] = {'b', 'a', 'r'};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_fit(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[3] = {'b', 'a', 'z'};
|
||||
char res1[3] = {'b', 'a', 'z'};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[4] = "qwe";
|
||||
char res2[3] = {'q', 'w', 'e'};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
test_STRNCPY_pad(void **state)
|
||||
{
|
||||
char buf[3];
|
||||
|
||||
char src1[3] = "as";
|
||||
char res1[3] = {'a', 's', 0};
|
||||
assert_true(memcmp(res1, STRNCPY(buf, src1), sizeof(buf)) == 0);
|
||||
|
||||
char src2[3] = "";
|
||||
char res2[3] = {0, 0, 0};
|
||||
assert_true(memcmp(res2, STRNCPY(buf, src2), sizeof(buf)) == 0);
|
||||
|
||||
char src3[3] = {'a', 0, 'b'};
|
||||
char res3[3] = {'a', 0, 0};
|
||||
assert_true(memcmp(res3, STRNCPY(buf, src3), sizeof(buf)) == 0);
|
||||
}
|
||||
Reference in New Issue
Block a user