Skip to content

Commit

Permalink
tests/unit/test_zustr2stp.c: Test ZUSTR2STP()
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 29, 2024
1 parent 0804e89 commit 32e4854
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tests/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ check_PROGRAMS = \
test_sprintf \
test_strncpy \
test_strtcpy \
test_xasprintf
test_xasprintf \
test_zustr2stp

if ENABLE_LOGIND
check_PROGRAMS += \
Expand Down Expand Up @@ -129,4 +130,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
56 changes: 56 additions & 0 deletions tests/unit/test_zustr2stp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// SPDX-FileCopyrightText: 2024, Alejandro Colomar <[email protected]>
// SPDX-License-Identifier: BSD-3-Clause


#include <config.h>

#include <stddef.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 "string/zustr2stp.h"

#undef NDEBUG
#include <assert.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];

ZUSTR2STP(&dst, src);
assert(strcmp("123", dst));

src[2] = '\0';
ZUSTR2STP(&dst, src);
assert(strcmp("12", dst));

src[1] = '\0';
ZUSTR2STP(&dst, src);
assert(strcmp("1", dst));

src[0] = '\0';
ZUSTR2STP(&dst, src);
assert(strcmp("", dst));
}

0 comments on commit 32e4854

Please sign in to comment.