From 6affda94248a1607c300ec30d2b53e8fa00089a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A1s=20P=C5=91cze?= Date: Thu, 3 Jun 2021 16:18:24 +0200 Subject: [PATCH] pipewire: utils: add more tests for pw_split_walk() --- src/tests/test-utils.c | 168 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 155 insertions(+), 13 deletions(-) diff --git a/src/tests/test-utils.c b/src/tests/test-utils.c index 36d0974fd..4ff8689d4 100644 --- a/src/tests/test-utils.c +++ b/src/tests/test-utils.c @@ -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 ";