test-strfuncs.c revision be1749e2421dea6465f9096fd35b28b2500d06e4
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2015 Dovecot authors, see the included COPYING file */
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen#include "test-lib.h"
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen#include <stdlib.h>
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenstatic void test_p_strarray_dup(void)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen const char *input[][3] = {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen { NULL },
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen { "a", NULL },
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen { "foobar", NULL },
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen { "a", "foo", NULL }
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen };
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen const char **ret;
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen unsigned int i, j;
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_begin("p_strarray_dup");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (i = 0; i < N_ELEMENTS(input); i++) {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen ret = p_strarray_dup(default_pool, input[i]);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (j = 0; input[i][j] != NULL; j++) {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(input[i][j], ret[j]) == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(input[i][j] != ret[j]);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen }
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(ret[j] == NULL);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen i_free(ret);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen }
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_end();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenstatic void test_t_strsplit(void)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen const char *const *args;
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_begin("t_strsplit");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen /* empty string -> empty array. was this perhaps a mistake for the
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen API to do this originally?.. can't really change now anyway. */
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen args = t_strsplit("", "\n");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(args[0] == NULL);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen /* two empty strings */
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen args = t_strsplit("\n", "\n");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(args[0][0] == '\0');
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(args[1][0] == '\0');
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(args[2] == NULL);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_end();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenstatic void strsplit_verify(const char *str)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen T_BEGIN {
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen const char **s1, **s2;
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen unsigned int i;
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen s1 = t_strsplit_tab(str);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen s2 = t_strsplit(str, "\t");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (i = 0; s1[i] != NULL; i++)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(null_strcmp(s1[i], s2[i]) == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(s2[i] == NULL);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen } T_END;
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenstatic void test_t_strsplit_tab(void)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen char buf[4096];
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen unsigned int i, j, max;
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_begin("t_strsplit_tab");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("\t");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("\t\t");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("foo");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("foo\tbar");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("foo\tbar\tbaz");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify("foo\t\tbaz");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[sizeof(buf)-1] = '\0';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (i = 0; i < sizeof(buf)-1; i++)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[i] = '\t';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify(buf);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (j = 0; j < 256; j++) {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen memset(buf, '\t', j);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[j+1] = '\0';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify(buf);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen }
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (j = 0; j < 100; j++) {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen max = (rand() % sizeof(buf)) + 1;
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[--max] = '\0';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen for (i = 0; i < max; i++) {
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen if (rand() % 10 == 0)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[i] = '\t';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen else
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen buf[i] = 'x';
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen }
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen strsplit_verify(buf);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen }
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_end();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenstatic void test_t_str_replace(void)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_begin("t_str_replace");
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("foo", 'a', 'b'), "foo") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("fooa", 'a', 'b'), "foob") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("afooa", 'a', 'b'), "bfoob") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("", 'a', 'b'), "") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("a", 'a', 'b'), "b") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("aaa", 'a', 'b'), "bbb") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("bbb", 'a', 'b'), "bbb") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_assert(strcmp(t_str_replace("aba", 'a', 'b'), "bbb") == 0);
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_end();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainenvoid test_strfuncs(void)
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen{
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_p_strarray_dup();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_t_strsplit();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_t_strsplit_tab();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen test_t_str_replace();
9761e0036c87f459abe040632e1252f794ffe5f7Timo Sirainen}
1139a1f61032e3b81bd1c7f526b9e9553f51a5a8Timo Sirainen