mirror of
https://gitlab.freedesktop.org/pipewire/pipewire.git
synced 2025-10-29 05:40:27 -04:00
pipewire: utils: add more tests for pw_split_walk()
This commit is contained in:
parent
3b681f2138
commit
6affda9424
1 changed files with 155 additions and 13 deletions
|
|
@ -40,23 +40,159 @@ static void test_abi(void)
|
|||
spa_assert(f == test_destroy);
|
||||
}
|
||||
|
||||
static void test_split(void)
|
||||
static void test__pw_split_walk(void)
|
||||
{
|
||||
const struct test_case {
|
||||
const char * const input;
|
||||
const char * const delim;
|
||||
const char * const * const expected;
|
||||
} test_cases[] = {
|
||||
{
|
||||
.input = "a \n test string \n \r ",
|
||||
.delim = " \r\n",
|
||||
.expected = (const char *[]) {
|
||||
"a",
|
||||
"test",
|
||||
"string",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "::field1::field2:: field3:::::",
|
||||
.delim = ":",
|
||||
.expected = (const char *[]) {
|
||||
"field1",
|
||||
"field2",
|
||||
" field3",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = ",,,,,,,,,,,,",
|
||||
.delim = ",",
|
||||
.expected = (const char *[]) {
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = ",;,,,'''':::':::,,,,;",
|
||||
.delim = ",:';",
|
||||
.expected = (const char *[]) {
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "aaa:bbb,ccc##ddd/#,eee?fff...",
|
||||
.delim = ":,#/?",
|
||||
.expected = (const char *[]) {
|
||||
"aaa",
|
||||
"bbb",
|
||||
"ccc",
|
||||
"ddd",
|
||||
"eee",
|
||||
"fff...",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "line 1\na different line\nthe third line\n",
|
||||
.delim = "\n",
|
||||
.expected = (const char *[]) {
|
||||
"line 1",
|
||||
"a different line",
|
||||
"the third line",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "no delimiters",
|
||||
.delim = ",:/;",
|
||||
.expected = (const char *[]) {
|
||||
"no delimiters",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "delimiter at the end,;",
|
||||
.delim = ",;",
|
||||
.expected = (const char *[]) {
|
||||
"delimiter at the end",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "/delimiter on both ends,",
|
||||
.delim = "/,",
|
||||
.expected = (const char *[]) {
|
||||
"delimiter on both ends",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = ",delimiter at the beginning",
|
||||
.delim = ",",
|
||||
.expected = (const char *[]) {
|
||||
"delimiter at the beginning",
|
||||
NULL
|
||||
},
|
||||
},
|
||||
{
|
||||
.input = "/usr/lib/pipewire-0.3/libpipewire.so",
|
||||
.delim = "/",
|
||||
.expected = (const char *[]) {
|
||||
"usr",
|
||||
"lib",
|
||||
"pipewire-0.3",
|
||||
"libpipewire.so",
|
||||
NULL
|
||||
}
|
||||
},
|
||||
{
|
||||
.input = "/home/x/.ladspa:/usr/lib/ladspa:/usr/local/lib/ladspa",
|
||||
.delim = ":",
|
||||
.expected = (const char *[]) {
|
||||
"/home/x/.ladspa",
|
||||
"/usr/lib/ladspa",
|
||||
"/usr/local/lib/ladspa",
|
||||
NULL
|
||||
}
|
||||
},
|
||||
{
|
||||
.input = "\n field1 \t\n field2 \t \t field3",
|
||||
.delim = " \n\t",
|
||||
.expected = (const char *[]) {
|
||||
"field1",
|
||||
"field2",
|
||||
"field3",
|
||||
NULL
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const struct test_case *tc;
|
||||
|
||||
SPA_FOR_EACH_ELEMENT(test_cases, tc) {
|
||||
const char *str = tc->input, *s;
|
||||
const char *state = NULL;
|
||||
size_t j = 0, len;
|
||||
|
||||
while ((s = pw_split_walk(str, tc->delim, &len, &state)) != NULL && tc->expected[j] != NULL) {
|
||||
spa_assert(strlen(tc->expected[j]) == len);
|
||||
spa_assert(strncmp(s, tc->expected[j], len) == 0);
|
||||
|
||||
j += 1;
|
||||
}
|
||||
|
||||
spa_assert(s == NULL && tc->expected[j] == NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static void test__pw_split_strv(void)
|
||||
{
|
||||
const char *test1 = "a \n test string \n \r ";
|
||||
const char *del = "\n\r ";
|
||||
size_t len;
|
||||
const char *str, *state = NULL;
|
||||
char **res;
|
||||
int n_tokens;
|
||||
|
||||
str = pw_split_walk(test1, del, &len, &state);
|
||||
spa_assert(!strncmp(str, "a", len));
|
||||
str = pw_split_walk(test1, del, &len, &state);
|
||||
spa_assert(!strncmp(str, "test", len));
|
||||
str = pw_split_walk(test1, del, &len, &state);
|
||||
spa_assert(!strncmp(str, "string", len));
|
||||
str = pw_split_walk(test1, del, &len, &state);
|
||||
spa_assert(str == NULL);
|
||||
char **res;
|
||||
|
||||
res = pw_split_strv(test1, del, INT_MAX, &n_tokens);
|
||||
spa_assert(res != NULL);
|
||||
|
|
@ -76,6 +212,12 @@ static void test_split(void)
|
|||
pw_free_strv(res);
|
||||
}
|
||||
|
||||
static void test_split(void)
|
||||
{
|
||||
test__pw_split_walk();
|
||||
test__pw_split_strv();
|
||||
}
|
||||
|
||||
static void test_strip(void)
|
||||
{
|
||||
char test1[] = " \n\r \n a test string \n \r ";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue