diff options
Diffstat (limited to 'src/test/test-strv.c')
-rw-r--r-- | src/test/test-strv.c | 101 |
1 files changed, 49 insertions, 52 deletions
diff --git a/src/test/test-strv.c b/src/test/test-strv.c index fda5948f49..558ffeef51 100644 --- a/src/test/test-strv.c +++ b/src/test/test-strv.c @@ -100,6 +100,12 @@ static const char* const input_table_quoted[] = { NULL, }; +static const char* const input_table_quoted_joined[] = { + "one", + " two\t three " " four five", + NULL, +}; + static const char* const input_table_one[] = { "one", NULL, @@ -232,7 +238,7 @@ static void test_strv_unquote(const char *quoted, char **list) { log_info("/* %s */", __func__); - r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); + r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); assert_se(r == (int) strv_length(list)); assert_se(s); j = strv_join(s, " | "); @@ -251,7 +257,7 @@ static void test_invalid_unquote(const char *quoted) { log_info("/* %s */", __func__); - r = strv_split_extract(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); + r = strv_split_full(&s, quoted, WHITESPACE, EXTRACT_UNQUOTE); assert_se(s == NULL); assert_se(r == -EINVAL); } @@ -281,47 +287,39 @@ static void test_strv_split(void) { strv_free_erase(l); - l = strv_split_full(" one two\t three", NULL, 0); - assert_se(l); + assert_se(strv_split_full(&l, " one two\t three", NULL, 0) == 3); assert_se(strv_equal(l, (char**) input_table_multiple)); strv_free_erase(l); - l = strv_split_full(" 'one' \" two\t three \" ' four five'", NULL, SPLIT_QUOTES); - assert_se(l); + assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five'", NULL, EXTRACT_UNQUOTE) == 3); assert_se(strv_equal(l, (char**) input_table_quoted)); - strv_free_erase(l); + l = strv_free_erase(l); - /* missing last quote ignores the last element. */ - l = strv_split_full(" 'one' \" two\t three \" ' four five' ' ignored element ", NULL, SPLIT_QUOTES); - assert_se(l); - assert_se(strv_equal(l, (char**) input_table_quoted)); + /* missing last quote causes extraction to fail. */ + assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE) == -EINVAL); + assert_se(!l); - strv_free_erase(l); - - /* missing last quote, but the last element is _not_ ignored with SPLIT_RELAX. */ - l = strv_split_full(" 'one' \" two\t three \" ' four five", NULL, SPLIT_QUOTES | SPLIT_RELAX); - assert_se(l); + /* missing last quote, but the last element is _not_ ignored with EXTRACT_RELAX. */ + assert_se(strv_split_full(&l, " 'one' \" two\t three \" ' four five", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 3); assert_se(strv_equal(l, (char**) input_table_quoted)); - strv_free_erase(l); + l = strv_free_erase(l); - /* missing separator between */ - l = strv_split_full(" 'one' \" two\t three \"' four five'", NULL, SPLIT_QUOTES | SPLIT_RELAX); - assert_se(l); - assert_se(strv_equal(l, (char**) input_table_quoted)); + /* missing separator between items */ + assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five'", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 2); + assert_se(strv_equal(l, (char**) input_table_quoted_joined)); - strv_free_erase(l); + l = strv_free_erase(l); - l = strv_split_full(" 'one' \" two\t three \"' four five", NULL, SPLIT_QUOTES | SPLIT_RELAX); - assert_se(l); - assert_se(strv_equal(l, (char**) input_table_quoted)); + assert_se(strv_split_full(&l, " 'one' \" two\t three \"' four five", NULL, + EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_CUNESCAPE_RELAX) == 2); + assert_se(strv_equal(l, (char**) input_table_quoted_joined)); - strv_free_erase(l); + l = strv_free_erase(l); - l = strv_split_full("\\", NULL, SPLIT_QUOTES | SPLIT_RELAX); - assert_se(l); + assert_se(strv_split_full(&l, "\\", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX | EXTRACT_CUNESCAPE_RELAX) == 1); assert_se(strv_equal(l, STRV_MAKE("\\"))); } @@ -333,71 +331,70 @@ static void test_strv_split_empty(void) { l = strv_split("", WHITESPACE); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split("", NULL); - assert_se(l); + assert_se(l = strv_split("", NULL)); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full("", NULL, 0); + assert_se(strv_split_full(&l, "", NULL, 0) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full("", NULL, SPLIT_QUOTES); + assert_se(strv_split_full(&l, "", NULL, EXTRACT_UNQUOTE) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full("", WHITESPACE, SPLIT_QUOTES); + assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full("", WHITESPACE, SPLIT_QUOTES | SPLIT_RELAX); + assert_se(strv_split_full(&l, "", WHITESPACE, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0); assert_se(l); assert_se(strv_isempty(l)); - strv_free(l); + l = strv_split(" ", WHITESPACE); assert_se(l); assert_se(strv_isempty(l)); - strv_free(l); + l = strv_split(" ", NULL); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full(" ", NULL, 0); + assert_se(strv_split_full(&l, " ", NULL, 0) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full(" ", WHITESPACE, SPLIT_QUOTES); + assert_se(strv_split_full(&l, " ", WHITESPACE, EXTRACT_UNQUOTE) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full(" ", NULL, SPLIT_QUOTES); + assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE) == 0); assert_se(l); assert_se(strv_isempty(l)); + l = strv_free(l); - strv_free(l); - l = strv_split_full(" ", NULL, SPLIT_QUOTES | SPLIT_RELAX); + assert_se(strv_split_full(&l, " ", NULL, EXTRACT_UNQUOTE | EXTRACT_RELAX) == 0); assert_se(l); assert_se(strv_isempty(l)); } -static void test_strv_split_extract(void) { +static void test_strv_split_full(void) { _cleanup_strv_free_ char **l = NULL; const char *str = ":foo\\:bar::waldo:"; int r; log_info("/* %s */", __func__); - r = strv_split_extract(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS); + r = strv_split_full(&l, str, ":", EXTRACT_DONT_COALESCE_SEPARATORS); assert_se(r == (int) strv_length(l)); assert_se(streq_ptr(l[0], "")); assert_se(streq_ptr(l[1], "foo:bar")); @@ -1026,7 +1023,7 @@ int main(int argc, char *argv[]) { test_strv_split(); test_strv_split_empty(); - test_strv_split_extract(); + test_strv_split_full(); test_strv_split_colon_pairs(); test_strv_split_newlines(); test_strv_split_nulstr(); |