From: Martti Rannanjärvi Date: Sun, 4 Dec 2016 12:58:14 +0000 (+0200) Subject: lib: add separator also if arr[0] is empty in string array joins X-Git-Tag: 2.3.0.rc1~2349 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=16bb1ac73aed6e30be610a2dc4eb9d1719f28559;p=thirdparty%2Fdovecot%2Fcore.git lib: add separator also if arr[0] is empty in string array joins Also add a couple test cases. --- diff --git a/src/lib/strfuncs.c b/src/lib/strfuncs.c index d5d2b2ec22..ac3b7a106d 100644 --- a/src/lib/strfuncs.c +++ b/src/lib/strfuncs.c @@ -628,7 +628,7 @@ p_strarray_join_n(pool_t pool, const char *const *arr, unsigned int arr_len, str = t_buffer_reget(str, alloc_len); } - if (pos != 0) { + if (i != 0) { memcpy(str + pos, separator, sep_len); pos += sep_len; } diff --git a/src/lib/test-strfuncs.c b/src/lib/test-strfuncs.c index f46d51036e..21f355caef 100644 --- a/src/lib/test-strfuncs.c +++ b/src/lib/test-strfuncs.c @@ -127,9 +127,32 @@ static const struct { const char *output; } test_strarray_outputs[] = { { "", "helloworldyay" }, - /* FIXME: v2.3 - test_output should have separator in the beginning */ - { " ", "hello world yay " }, - { "!-?", "hello!-?world!-?!-?yay!-?" } + { " ", " hello world yay " }, + { "!-?", "!-?hello!-?world!-?!-?yay!-?" } +}; + +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) @@ -147,6 +170,16 @@ static void test_t_strarray_join(void) 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(); }