| 1 |
/* $OpenBSD: test_xextendf.c,v 1.1 2025/09/02 11:04:58 djm Exp $ */ |
| 2 |
/* |
| 3 |
* Regress test for misc xextendf() function. |
| 4 |
* |
| 5 |
* Placed in the public domain. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "includes.h" |
| 9 |
|
| 10 |
#include <sys/types.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <stdlib.h> |
| 14 |
#include <string.h> |
| 15 |
|
| 16 |
#include "../test_helper/test_helper.h" |
| 17 |
|
| 18 |
#include "log.h" |
| 19 |
#include "misc.h" |
| 20 |
#include "xmalloc.h" |
| 21 |
|
| 22 |
void test_xextendf(void); |
| 23 |
|
| 24 |
void |
| 25 |
test_xextendf(void) |
| 26 |
{ |
| 27 |
char *s = NULL; |
| 28 |
|
| 29 |
TEST_START("xextendf NULL string"); |
| 30 |
xextendf(&s, ",", "hello"); |
| 31 |
ASSERT_STRING_EQ(s, "hello"); |
| 32 |
free(s); |
| 33 |
s = NULL; |
| 34 |
TEST_DONE(); |
| 35 |
|
| 36 |
TEST_START("xextendf empty string"); |
| 37 |
s = xstrdup(""); |
| 38 |
xextendf(&s, ",", "world"); |
| 39 |
ASSERT_STRING_EQ(s, "world"); |
| 40 |
free(s); |
| 41 |
s = NULL; |
| 42 |
TEST_DONE(); |
| 43 |
|
| 44 |
TEST_START("xextendf append to string"); |
| 45 |
s = xstrdup("foo"); |
| 46 |
xextendf(&s, ",", "bar"); |
| 47 |
ASSERT_STRING_EQ(s, "foo,bar"); |
| 48 |
free(s); |
| 49 |
s = NULL; |
| 50 |
TEST_DONE(); |
| 51 |
|
| 52 |
TEST_START("xextendf append with NULL separator"); |
| 53 |
s = xstrdup("foo"); |
| 54 |
xextendf(&s, NULL, "bar"); |
| 55 |
ASSERT_STRING_EQ(s, "foobar"); |
| 56 |
free(s); |
| 57 |
s = NULL; |
| 58 |
TEST_DONE(); |
| 59 |
|
| 60 |
TEST_START("xextendf append with empty separator"); |
| 61 |
s = xstrdup("foo"); |
| 62 |
xextendf(&s, "", "bar"); |
| 63 |
ASSERT_STRING_EQ(s, "foobar"); |
| 64 |
free(s); |
| 65 |
s = NULL; |
| 66 |
TEST_DONE(); |
| 67 |
|
| 68 |
TEST_START("xextendf with format string"); |
| 69 |
s = xstrdup("start"); |
| 70 |
xextendf(&s, ":", "s=%s,d=%d", "string", 123); |
| 71 |
ASSERT_STRING_EQ(s, "start:s=string,d=123"); |
| 72 |
free(s); |
| 73 |
s = NULL; |
| 74 |
TEST_DONE(); |
| 75 |
|
| 76 |
TEST_START("xextendf multiple appends"); |
| 77 |
s = NULL; |
| 78 |
xextendf(&s, ",", "one"); |
| 79 |
ASSERT_STRING_EQ(s, "one"); |
| 80 |
xextendf(&s, ",", "two"); |
| 81 |
ASSERT_STRING_EQ(s, "one,two"); |
| 82 |
xextendf(&s, ":", "three=%d", 3); |
| 83 |
ASSERT_STRING_EQ(s, "one,two:three=3"); |
| 84 |
xextendf(&s, NULL, "four"); |
| 85 |
ASSERT_STRING_EQ(s, "one,two:three=3four"); |
| 86 |
free(s); |
| 87 |
s = NULL; |
| 88 |
TEST_DONE(); |
| 89 |
} |