test-str-find.c revision 48acc31adebfdd4e4945ee76e1f5259e4b1b6fff
02c335c23bf5fa225a467c19f2c063fb0dc7b8c3Timo Sirainen/* Copyright (c) 2007-2009 Dovecot authors, see the included COPYING file */
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen#include "test-lib.h"
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen#include "str-find.h"
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainenstatic const char *str_find_text = "xababcd";
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainenstatic bool test_str_find_substring(const char *key, int expected_pos)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen{
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen const unsigned char *text = (const unsigned char *)str_find_text;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen const unsigned int text_len = strlen(str_find_text);
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen struct str_find_context *ctx;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen unsigned int i, j, pos, max, offset;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen bool ret;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen
7ef3553585e556f35d5919589cfdc1de3329e4bbTimo Sirainen ctx = str_find_init(pool_datastack_create(), key);
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen /* divide text into every possible block combination and test that
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen it matches */
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen max = 1 << (text_len-1);
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen for (i = 0; i < max; i++) {
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen str_find_reset(ctx);
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen pos = 0; offset = 0; ret = FALSE;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen for (j = 0; j < text_len; j++) {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if ((i & (1 << j)) != 0) {
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen if (str_find_more(ctx, text+pos, j-pos+1)) {
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen ret = TRUE;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen break;
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen }
538303a216166f3526c0ae9658c9978275cfa100Timo Sirainen offset += j-pos + 1;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen pos = j + 1;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (pos != text_len && !ret) {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (str_find_more(ctx, text+pos, j-pos))
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen ret = TRUE;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (expected_pos < 0) {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (ret)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen return FALSE;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen } else {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (!ret)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen return FALSE;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen pos = str_find_get_match_end_pos(ctx) +
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen offset - strlen(key);
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if ((int)pos != expected_pos)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen return FALSE;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen return TRUE;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen}
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainenstruct str_find_input {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen const char *str;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen int pos;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen};
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainenvoid test_str_find(void)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen{
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen static const char *fail_input[] = {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen "xabc",
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen "xabd",
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen "abd"
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen };
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen unsigned int idx, len;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen const char *key, *p;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen unsigned int i;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen bool success = TRUE;
23bdbb7b1831785c6ba6df190f6369da882d2b9dTimo Sirainen
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen for (idx = 0; idx < strlen(str_find_text); idx++) {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen for (len = strlen(str_find_text)-idx; len > 0; len--) {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen /* we'll get a search key for all substrings of text */
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen T_BEGIN {
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen key = t_strndup(str_find_text + idx, len);
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen p = strstr(str_find_text, key);
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen success = test_str_find_substring(key, p - str_find_text);
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen } T_END;
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen if (!success)
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen break;
678d0463849ba777106eb7875f27db07a5d8e3dfTimo Sirainen }
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen }
eac60b7aef3924a611656b184412be1e80b2ed5bTimo Sirainen for (i = 0; i < N_ELEMENTS(fail_input) && success; i++)
eac60b7aef3924a611656b184412be1e80b2ed5bTimo Sirainen success = test_str_find_substring(fail_input[i], -1);
eac60b7aef3924a611656b184412be1e80b2ed5bTimo Sirainen test_out("str_find()", success);
eac60b7aef3924a611656b184412be1e80b2ed5bTimo Sirainen}
e8bdf1be00aec45d0c6dd72ad9c8be02a3dfc778Timo Sirainen