Skip to content

Commit

Permalink
lib: Clarify *_strsplit_spaces() and add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen committed Feb 17, 2017
1 parent a108b91 commit b627d2a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/strfuncs.h
Expand Up @@ -74,7 +74,8 @@ char **p_strsplit(pool_t pool, const char *data, const char *separators)
const char **t_strsplit(const char *data, const char *separators)
ATTR_MALLOC ATTR_RETURNS_NONNULL;
/* like p_strsplit(), but treats multiple adjacent separators as a single
separator. */
separator. separators at the beginning or at the end of the string are also
ignored, so it's not possible for the result to have any empty strings. */
char **p_strsplit_spaces(pool_t pool, const char *data, const char *separators)
ATTR_MALLOC ATTR_RETURNS_NONNULL;
const char **t_strsplit_spaces(const char *data, const char *separators)
Expand Down
21 changes: 21 additions & 0 deletions src/lib/test-strfuncs.c
Expand Up @@ -92,6 +92,26 @@ static void test_t_strsplit_tab(void)
}
strsplit_verify(buf);
}
}

static void test_t_strsplit_spaces(void)
{
const char *const *args;

test_begin("t_strsplit_spaces");
/* empty strings */
args = t_strsplit_spaces("", "\n");
test_assert(args[0] == NULL);
args = t_strsplit_spaces("\n", "\n");
test_assert(args[0] == NULL);
args = t_strsplit_spaces("\n\n", "\n");
test_assert(args[0] == NULL);

/* multiple separators */
args = t_strsplit_spaces(" , , ,str1 , ,,, , str2 , ", " ,");
test_assert(strcmp(args[0], "str1") == 0);
test_assert(strcmp(args[1], "str2") == 0);
test_assert(args[2] == NULL);
test_end();
}

Expand Down Expand Up @@ -220,6 +240,7 @@ void test_strfuncs(void)
test_p_strarray_dup();
test_t_strsplit();
test_t_strsplit_tab();
test_t_strsplit_spaces();
test_t_str_replace();
/*test_t_str_trim();*/
test_t_str_ltrim();
Expand Down

0 comments on commit b627d2a

Please sign in to comment.