]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: add separator also if arr[0] is empty in string array joins
authorMartti Rannanjärvi <martti.rannanjarvi@dovecot.fi>
Sun, 4 Dec 2016 12:58:14 +0000 (14:58 +0200)
committerGitLab <gitlab@git.dovecot.net>
Tue, 27 Dec 2016 13:35:43 +0000 (15:35 +0200)
Also add a couple test cases.

src/lib/strfuncs.c
src/lib/test-strfuncs.c

index d5d2b2ec2242ff7dbdf18226460791a446212e08..ac3b7a106d7818c69cc021581f1e3095c4cb1465 100644 (file)
@@ -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;
                }
index f46d51036e85efc40090fbde35a558aa5973b631..21f355caefe358d799148312cb115212cba251be 100644 (file)
@@ -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();
 }