diff options
author | Ed Maste <emaste@FreeBSD.org> | 2025-10-06 12:29:14 +0000 |
---|---|---|
committer | Ed Maste <emaste@FreeBSD.org> | 2025-10-06 12:29:14 +0000 |
commit | 6409980cbba7323bd1c86249ed16f8bea9fa5490 (patch) | |
tree | 3710eb5988e5c6b0dd82c46413e96bba7cd18544 /regress/unittests/misc/test_xextendf.c | |
parent | 9792a032f0a99557271d6b7f7b0a955386c1fdbe (diff) |
Diffstat (limited to 'regress/unittests/misc/test_xextendf.c')
-rw-r--r-- | regress/unittests/misc/test_xextendf.c | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/regress/unittests/misc/test_xextendf.c b/regress/unittests/misc/test_xextendf.c new file mode 100644 index 000000000000..3c1ee8b8b65c --- /dev/null +++ b/regress/unittests/misc/test_xextendf.c @@ -0,0 +1,89 @@ +/* $OpenBSD: test_xextendf.c,v 1.1 2025/09/02 11:04:58 djm Exp $ */ +/* + * Regress test for misc xextendf() function. + * + * Placed in the public domain. + */ + +#include "includes.h" + +#include <sys/types.h> +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> + +#include "../test_helper/test_helper.h" + +#include "log.h" +#include "misc.h" +#include "xmalloc.h" + +void test_xextendf(void); + +void +test_xextendf(void) +{ + char *s = NULL; + + TEST_START("xextendf NULL string"); + xextendf(&s, ",", "hello"); + ASSERT_STRING_EQ(s, "hello"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf empty string"); + s = xstrdup(""); + xextendf(&s, ",", "world"); + ASSERT_STRING_EQ(s, "world"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf append to string"); + s = xstrdup("foo"); + xextendf(&s, ",", "bar"); + ASSERT_STRING_EQ(s, "foo,bar"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf append with NULL separator"); + s = xstrdup("foo"); + xextendf(&s, NULL, "bar"); + ASSERT_STRING_EQ(s, "foobar"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf append with empty separator"); + s = xstrdup("foo"); + xextendf(&s, "", "bar"); + ASSERT_STRING_EQ(s, "foobar"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf with format string"); + s = xstrdup("start"); + xextendf(&s, ":", "s=%s,d=%d", "string", 123); + ASSERT_STRING_EQ(s, "start:s=string,d=123"); + free(s); + s = NULL; + TEST_DONE(); + + TEST_START("xextendf multiple appends"); + s = NULL; + xextendf(&s, ",", "one"); + ASSERT_STRING_EQ(s, "one"); + xextendf(&s, ",", "two"); + ASSERT_STRING_EQ(s, "one,two"); + xextendf(&s, ":", "three=%d", 3); + ASSERT_STRING_EQ(s, "one,two:three=3"); + xextendf(&s, NULL, "four"); + ASSERT_STRING_EQ(s, "one,two:three=3four"); + free(s); + s = NULL; + TEST_DONE(); +} |