bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup(default_pool, NULL) == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(str != src && str != NULL && strcmp(src, str) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup_empty(default_pool, NULL) == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(p_strdup_empty(default_pool, "") == NULL);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen char *str = p_strdup_empty(default_pool, src);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(str != src && str != NULL && strcmp(src, str) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+2);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+3);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen /* \0 is ignored */
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+7);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(memcmp(str, src, sizeof(src)) == 0);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen str = p_strdup_until(default_pool, src, src+8);
8322e7400bbeee586529e797f6e332149178aa4fTimo Sirainen test_assert(memcmp(str, src, sizeof(src)) == 0);
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen unsigned int i, j;
12ff27eadb44c6877298d3e214c7039342669669Timo Sirainen test_assert(strcmp(input[i][j], ret[j]) == 0);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* empty string -> empty array. was this perhaps a mistake for
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen the API to do this originally?.. can't really change now
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n", (const char *const []) { "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\n\n", (const char *const []) { "", "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\n", (const char *const []) { "foo", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar\n", (const char *const []) { "foo", "bar", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\nfoo\n\nbar\n\n", (const char *const []) { "", "foo", "", "bar", "", "", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *args, *const *args2, *const *args3;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
b7b202953ea655bba74ca8b0236694c722a0b771Timo Sirainen /* split_str_fast() with single separator */
b7b202953ea655bba74ca8b0236694c722a0b771Timo Sirainen /* split_str_slow() with a secondary separator */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* also as suffix */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int j = 0; tests[i].output[j] != NULL; j++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(tests[i].output[j], args[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args2[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args3[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* empty strings */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\n", (const char *const []) { "foo", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "foo\nbar\n", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen { "\nfoo\n\nbar\n\n", (const char *const []) { "foo", "bar", NULL } },
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen const char *const *args, *const *args2, *const *args3;
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args = t_strsplit_spaces(tests[i].input, "\n");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* test also with a secondary nonexistent separator */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args2 = t_strsplit_spaces(tests[i].input, "\r\n");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen /* also as suffix */
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen args3 = t_strsplit_spaces(tests[i].input, "\n\r");
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen for (unsigned int j = 0; tests[i].output[j] != NULL; j++) {
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(tests[i].output[j], args[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args2[j]) == 0, i);
8587f4288b357bd83f2ec72b4197b529cf02220aTimo Sirainen test_assert_idx(null_strcmp(args[j], args3[j]) == 0, i);
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen /* multiple separators */
833fd67ffe6a1832d72b3d310e07345a42a7b6abTimo Sirainen args = t_strsplit_spaces(" , , ,str1 , ,,, , str2 , ", " ,");
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("foo", 'a', 'b'), "foo") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("fooa", 'a', 'b'), "foob") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("afooa", 'a', 'b'), "bfoob") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("", 'a', 'b'), "") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("a", 'a', 'b'), "b") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("aaa", 'a', 'b'), "bbb") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("bbb", 'a', 'b'), "bbb") == 0);
be1749e2421dea6465f9096fd35b28b2500d06e4Timo Sirainen test_assert(strcmp(t_str_replace("aba", 'a', 'b'), "bbb") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_trim("f \t ", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\tfoo ", "\t "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim(" \tfoo\t ", "\t "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\r \tfoo\t \r", "\t \r"), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\r \tfoo foo\t \r", "\t \r"), "foo foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_trim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_ltrim(" \t f", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("foo ", " "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" foo ", " "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\tfoo ", "\t "), "foo ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim(" \tfoo\t ", "\t "), "foo\t ") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\r \tfoo\t \r", "\t \r"), "foo\t \r") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\r \tfoo foo\t \r", "\t \r"), "foo foo\t \r") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_ltrim("\tfoo\tfoo\t", "\t \r"), "foo\tfoo\t") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim("", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim(" ", " "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim(" \t ", "\t "), "") == 0);
e2d420dc4bbb06de56ca225c1d9eb79456c792c5Stephan Bosch test_assert(strcmp(t_str_rtrim("f \t ", "\t "), "f") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo", ""), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("foo ", " "), "foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" foo", " "), " foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" foo ", " "), " foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\tfoo ", "\t "), "\tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim(" \tfoo\t ", "\t "), " \tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\r \tfoo\t \r", "\t \r"), "\r \tfoo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\r \tfoo foo\t \r", "\t \r"), "\r \tfoo foo") == 0);
f43734e772a94c8d2b684b59c212fecf66122d15Stephan Bosch test_assert(strcmp(t_str_rtrim("\tfoo\tfoo\t", "\t \r"), "\tfoo\tfoo") == 0);
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainenstatic const char *const test_strarray_input[] = {
b7324e421e2132cbbf753e6fdbe675bbaecdf929Timo Sirainenstatic const struct {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "!-?", "!-?hello!-?world!-?!-?yay!-?" }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic const char *const test_strarray_input2[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi "", "", "hello", "world", "", "yay", "", NULL
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic struct {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi { "!-?", "!-?!-?hello!-?world!-?!-?yay!-?" }
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic const char *const test_strarray_input3[] = {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvistatic struct {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen unsigned int i;
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen /* empty array -> empty string */
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_assert(strcmp(t_strarray_join(&null, " "), "") == 0);
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
1f35f54096589b0fdb04bfac4d079d06b43f2a74Timo Sirainen test_assert_idx(strcmp(t_strarray_join(test_strarray_input,
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi for (i = 0; i < N_ELEMENTS(test_strarray_outputs2); i++) {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_assert_idx(strcmp(t_strarray_join(test_strarray_input2,
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs2[i].output) == 0, i);
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi for (i = 0; i < N_ELEMENTS(test_strarray_outputs3); i++) {
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_assert_idx(strcmp(t_strarray_join(test_strarray_input3,
16bb1ac73aed6e30be610a2dc4eb9d1719f28559Martti Rannanjärvi test_strarray_outputs3[i].output) == 0, i);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainenstatic void test_p_array_const_string_join(void)
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen unsigned int i;
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen /* empty array -> empty string */
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_assert(strcmp(t_array_const_string_join(&arr, " "), "") == 0);
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen for (i = 0; i < N_ELEMENTS(test_strarray_outputs); i++) {
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen res = p_array_const_string_join(default_pool, &arr,
c8d88cb784cc7f7edcdc97f8b24b243eb1bba7aaTimo Sirainen test_assert_idx(strcmp(res, test_strarray_outputs[i].output) == 0, i);
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen const struct {
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen const char *a, *b;
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(tests); i++) {
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_assert((memcmp(tests[i].a, tests[i].b, len) == 0) ==
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen mem_equals_timing_safe(tests[i].a, tests[i].b, len));
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen test_assert((memcmp(tests[i].a, tests[i].b, len) == 0) ==
9464dc83df5da54457ff0b629d02e543e43d53bfTimo Sirainen mem_equals_timing_safe(tests[i].b, tests[i].a, len));
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 99, 999, 9999, 65535, 65536, 99999, 999999, 9999999,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 99999999, 999999999, 4294967295, 4294967296ULL,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 9999999999999999999ULL,
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen 18446744073709551615ULL
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen char buf[MAX_INT_STRLEN], buf2[MAX_INT_STRLEN];
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen for (unsigned int i = 0; i < N_ELEMENTS(test_input); i++) {
a0876e2befd7c73f909a33d1c1c3271a8d814f9bTimo Sirainen i_snprintf(buf2, sizeof(buf2), "%ju", test_input[i]);