From b76fc2947f145133e584aec6a823ef976b4e7a8c Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Tue, 30 Jan 2024 00:22:33 +0100 Subject: [PATCH] tests/unit/test_zustr2stp.c: Test ZUSTR2STP() Signed-off-by: Alejandro Colomar --- tests/unit/Makefile.am | 15 ++++++++++- tests/unit/test_zustr2stp.c | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 tests/unit/test_zustr2stp.c diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index ff882b8d4..228d87961 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -11,7 +11,8 @@ check_PROGRAMS = \ test_sprintf \ test_strncpy \ test_strtcpy \ - test_xasprintf + test_xasprintf \ + test_zustr2stp if ENABLE_LOGIND check_PROGRAMS += \ @@ -143,4 +144,16 @@ test_xasprintf_LDADD = \ $(CMOCKA_LIBS) \ $(NULL) +test_zustr2stp_SOURCES = \ + test_zustr2stp.c \ + $(NULL) +test_zustr2stp_CFLAGS = \ + $(AM_CFLAGS) \ + $(NULL) +test_zustr2stp_LDFLAGS = \ + $(NULL) +test_zustr2stp_LDADD = \ + $(CMOCKA_LIBS) \ + $(NULL) + endif # HAVE_CMOCKA diff --git a/tests/unit/test_zustr2stp.c b/tests/unit/test_zustr2stp.c new file mode 100644 index 000000000..198d2eb65 --- /dev/null +++ b/tests/unit/test_zustr2stp.c @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: 2024, Alejandro Colomar +// SPDX-License-Identifier: BSD-3-Clause + + +#include + +#include +#include + +#include // Required by +#include // Required by +#include // Required by +#include // Required by +#include + +#include "string/zustr2stp.h" + + +static void test_ZUSTR2STP(void **state); + + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_ZUSTR2STP), + }; + + return cmocka_run_group_tests(tests, NULL, NULL); +} + + +static void +test_ZUSTR2STP(void **state) +{ + char src[3] = {'1', '2', '3'}; + char dst[4]; + + assert_true(ZUSTR2STP(&dst, src) == dst + strlen("123")); + assert_true(strcmp("123", dst) == 0); + + src[2] = '\0'; + assert_true(ZUSTR2STP(&dst, src) == dst + strlen("12")); + assert_true(strcmp("12", dst) == 0); + + src[1] = '\0'; + assert_true(ZUSTR2STP(&dst, src) == dst + strlen("1")); + assert_true(strcmp("1", dst) == 0); + + src[0] = '\0'; + assert_true(ZUSTR2STP(&dst, src) == dst + strlen("")); + assert_true(strcmp("", dst) == 0); +}