test-strfuncs.c revision 833fd67ffe6a1832d72b3d310e07345a42a7b6ab
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2009-2017 Dovecot authors, see the included COPYING file */
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
08d6658a4e2ec8104cd1307f6baa75fdb07a24f8Mark Washenberger#include "test-lib.h"
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen#include "array.h"
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainenstatic void test_p_strarray_dup(void)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen const char *input[][3] = {
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen { NULL },
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen { "a", NULL },
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen { "foobar", NULL },
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen { "a", "foo", NULL }
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen };
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen const char **ret;
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen unsigned int i, j;
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen
9ed2951bd0bb1878a27437d7c00611b2baadd614Timo Sirainen test_begin("p_strarray_dup");
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen for (i = 0; i < N_ELEMENTS(input); i++) {
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen ret = p_strarray_dup(default_pool, input[i]);
6cbe2facd40ea3461620571a1c168ce9884be3b3Timo Sirainen for (j = 0; input[i][j] != NULL; j++) {
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen test_assert(strcmp(input[i][j], ret[j]) == 0);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(input[i][j] != ret[j]);
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen }
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen test_assert(ret[j] == NULL);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen i_free(ret);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen }
5bb7c9863cbb62c41b13e7f42e04f1d57b4634f8Timo Sirainen test_end();
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainenstatic void test_t_strsplit(void)
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen{
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen const char *const *args;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
d31c77e63713a6cf3687a4b38ff8daf6d6c7a3ddTimo Sirainen test_begin("t_strsplit");
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen /* empty string -> empty array. was this perhaps a mistake for the
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen API to do this originally?.. can't really change now anyway. */
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen args = t_strsplit("", "\n");
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(args[0] == NULL);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen /* two empty strings */
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen args = t_strsplit("\n", "\n");
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(args[0][0] == '\0');
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(args[1][0] == '\0');
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(args[2] == NULL);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_end();
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen}
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainenstatic void test_t_strsplit_spaces(void)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen const char *const *args;
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_begin("t_strsplit_spaces");
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen /* empty strings */
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen args = t_strsplit_spaces("", "\n");
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(args[0] == NULL);
3c296d819c54e21ce05c3d2eeeedc79be42ac593Timo Sirainen args = t_strsplit_spaces("\n", "\n");
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainen test_assert(args[0] == NULL);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen args = t_strsplit_spaces("\n\n", "\n");
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainen test_assert(args[0] == NULL);
3ab7783791bd46cdd46e9b9de3e98e8efcb6c6bfTimo Sirainen
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen /* multiple separators */
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen args = t_strsplit_spaces(" , , ,str1 , ,,, , str2 , ", " ,");
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(strcmp(args[0], "str1") == 0);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(strcmp(args[1], "str2") == 0);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(args[2] == NULL);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_end();
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen}
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainenstatic void test_t_str_replace(void)
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen{
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_begin("t_str_replace");
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(strcmp(t_str_replace("foo", 'a', 'b'), "foo") == 0);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(strcmp(t_str_replace("fooa", 'a', 'b'), "foob") == 0);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(strcmp(t_str_replace("afooa", 'a', 'b'), "bfoob") == 0);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(strcmp(t_str_replace("", 'a', 'b'), "") == 0);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(strcmp(t_str_replace("a", 'a', 'b'), "b") == 0);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(strcmp(t_str_replace("aaa", 'a', 'b'), "bbb") == 0);
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_assert(strcmp(t_str_replace("bbb", 'a', 'b'), "bbb") == 0);
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen test_assert(strcmp(t_str_replace("aba", 'a', 'b'), "bbb") == 0);
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen test_end();
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen}
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainenstatic void test_t_str_trim(void)
553308791c097219e8eb31cbd03a29e9e1333848Timo Sirainen{
24d7c5fc9fa1cb1f49402ec796654113199ba4e6Timo Sirainen test_begin("t_str_trim");
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim("", " "), "") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim(" ", " "), "") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim(" \t ", "\t "), "") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim("f \t ", "\t "), "f") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim("foo", ""), "foo") == 0);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen test_assert(strcmp(t_str_trim("foo", " "), "foo") == 0);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen test_assert(strcmp(t_str_trim("foo ", " "), "foo") == 0);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen test_assert(strcmp(t_str_trim(" foo", " "), "foo") == 0);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen test_assert(strcmp(t_str_trim(" foo ", " "), "foo") == 0);
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen test_assert(strcmp(t_str_trim("\tfoo ", "\t "), "foo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim(" \tfoo\t ", "\t "), "foo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_trim("\r \tfoo\t \r", "\t \r"), "foo") == 0);
8759adc67109b5a12a7af3ed717c7040622a0a04Timo Sirainen test_assert(strcmp(t_str_trim("\r \tfoo foo\t \r", "\t \r"), "foo foo") == 0);
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_assert(strcmp(t_str_trim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_end();
d31c77e63713a6cf3687a4b38ff8daf6d6c7a3ddTimo Sirainen}
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainenstatic void test_t_str_ltrim(void)
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen{
145d2eef238ed8bbff635e3b06951a83f0ee5a03Timo Sirainen test_begin("t_str_ltrim");
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen test_assert(strcmp(t_str_ltrim("", " "), "") == 0);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(strcmp(t_str_ltrim(" ", " "), "") == 0);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(strcmp(t_str_ltrim(" \t ", "\t "), "") == 0);
6135260095e1704ed6edff9d00bdfc043c11429cTimo Sirainen test_assert(strcmp(t_str_ltrim(" \t f", "\t "), "f") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim("foo", ""), "foo") == 0);
00bde9ae9eab9e720462bf6ec9a4dd85e88c3bbfTimo Sirainen test_assert(strcmp(t_str_ltrim("foo", " "), "foo") == 0);
7ace5117d5f2395bd66f20b09e77dac05492f7ceTimo Sirainen test_assert(strcmp(t_str_ltrim("foo ", " "), "foo ") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim(" foo", " "), "foo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim(" foo ", " "), "foo ") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim("\tfoo ", "\t "), "foo ") == 0);
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen test_assert(strcmp(t_str_ltrim(" \tfoo\t ", "\t "), "foo\t ") == 0);
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen test_assert(strcmp(t_str_ltrim("\r \tfoo\t \r", "\t \r"), "foo\t \r") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim("\r \tfoo foo\t \r", "\t \r"), "foo foo\t \r") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_ltrim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo\t") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_end();
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen}
3c296d819c54e21ce05c3d2eeeedc79be42ac593Timo Sirainen
553308791c097219e8eb31cbd03a29e9e1333848Timo Sirainenstatic void test_t_str_rtrim(void)
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen{
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen test_begin("t_str_rtrim");
7dcb5545370faa9d4ff83b3ede65a69fc3dd4b65Timo Sirainen test_assert(strcmp(t_str_rtrim("", " "), "") == 0);
a3dd97fb6d92a89c3de0597fed2d4b044c7aeb84Timo Sirainen test_assert(strcmp(t_str_rtrim(" ", " "), "") == 0);
cc0495b3bbe3c3e41c512274b302d6f0fa028187Timo Sirainen test_assert(strcmp(t_str_rtrim(" \t ", "\t "), "") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim("f \t ", "\t "), "f") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim("foo", ""), "foo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim("foo", " "), "foo") == 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(strcmp(t_str_rtrim("foo ", " "), "foo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim(" foo", " "), " foo") == 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen test_assert(strcmp(t_str_rtrim(" foo ", " "), " foo") == 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen test_assert(strcmp(t_str_rtrim("\tfoo ", "\t "), "\tfoo") == 0);
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen test_assert(strcmp(t_str_rtrim(" \tfoo\t ", "\t "), " \tfoo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim("\r \tfoo\t \r", "\t \r"), "\r \tfoo") == 0);
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen test_assert(strcmp(t_str_rtrim("\r \tfoo foo\t \r", "\t \r"), "\r \tfoo foo") == 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen test_assert(strcmp(t_str_rtrim("\tfoo\tfoo\t", "\t \r"), "\tfoo\tfoo") == 0);
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen test_end();
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen}
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainenstatic const char *const test_strarray_input[] = {
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen "", "hello", "world", "", "yay", "", NULL
31a12066e4cd9310d64091c81b59fb8eb1986023Timo Sirainen};
61618d4c58080570f689614fec204ae14e90cef2Timo Sirainenstatic const struct {
a64adf62fa33f2463a86f990217b0c9078531a40Timo Sirainen const char *separator;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen const char *output;
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen} test_strarray_outputs[] = {
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen { "", "helloworldyay" },
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen { " ", " hello world yay " },
8ef7c24178fd798c3e0301c5b8afa1a9bdedd27fTimo Sirainen { "!-?", "!-?hello!-?world!-?!-?yay!-?" }
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen};
dc9bfb7dc057964238e181d3d8b08751527bb08aTimo Sirainen
static const char *const test_strarray_input2[] = {
"", "", "hello", "world", "", "yay", "", NULL
};
static struct {
const char *separator;
const char *output;
} test_strarray_outputs2[] = {
{ "", "helloworldyay" },
{ " ", " hello world yay " },
{ "!-?", "!-?!-?hello!-?world!-?!-?yay!-?" }
};
static const char *const test_strarray_input3[] = {
"hello", "", "", "yay", NULL
};
static struct {
const char *separator;
const char *output;
} test_strarray_outputs3[] = {
{ "", "helloyay" },
{ " ", "hello yay" },
{ "!-?", "hello!-?!-?!-?yay" }
};
static void test_t_strarray_join(void)
{
const char *null = NULL;
unsigned int i;
test_begin("t_strarray_join()");
/* empty array -> empty string */
test_assert(strcmp(t_strarray_join(&null, " "), "") == 0);
for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
test_assert_idx(strcmp(t_strarray_join(test_strarray_input,
test_strarray_outputs[i].separator),
test_strarray_outputs[i].output) == 0, i);
}
for (i = 0; i < N_ELEMENTS(test_strarray_outputs2); i++) {
test_assert_idx(strcmp(t_strarray_join(test_strarray_input2,
test_strarray_outputs2[i].separator),
test_strarray_outputs2[i].output) == 0, i);
}
for (i = 0; i < N_ELEMENTS(test_strarray_outputs3); i++) {
test_assert_idx(strcmp(t_strarray_join(test_strarray_input3,
test_strarray_outputs3[i].separator),
test_strarray_outputs3[i].output) == 0, i);
}
test_end();
}
static void test_p_array_const_string_join(void)
{
ARRAY_TYPE(const_string) arr;
unsigned int i;
char *res;
test_begin("p_array_const_string_join()");
i_array_init(&arr, 2);
/* empty array -> empty string */
test_assert(strcmp(t_array_const_string_join(&arr, " "), "") == 0);
array_append(&arr, test_strarray_input,
str_array_length(test_strarray_input));
for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
res = p_array_const_string_join(default_pool, &arr,
test_strarray_outputs[i].separator);
test_assert_idx(strcmp(res, test_strarray_outputs[i].output) == 0, i);
i_free(res);
}
array_free(&arr);
test_end();
}
void test_strfuncs(void)
{
test_p_strarray_dup();
test_t_strsplit();
test_t_strsplit_spaces();
test_t_str_replace();
test_t_str_trim();
test_t_str_ltrim();
test_t_str_rtrim();
test_t_strarray_join();
test_p_array_const_string_join();
}