summaryrefslogtreecommitdiffstats
path: root/src/lib/test-str-find.c
blob: a6a6340a287a58a3eef26cad3d09902e9aab3fbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Copyright (c) 2007-2018 Dovecot authors, see the included COPYING file */

#include "test-lib.h"
#include "str-find.h"

static const char *str_find_text = "xababcd";

static bool test_str_find_substring(const char *key, int expected_pos)
{
	const unsigned char *text = (const unsigned char *)str_find_text;
	const unsigned int text_len = strlen(str_find_text);
	struct str_find_context *ctx;
	unsigned int i, j, pos, max, offset;
	bool ret;

	ctx = str_find_init(pool_datastack_create(), key);
	/* divide text into every possible block combination and test that
	   it matches */
	i_assert(text_len > 0);
	max = 1U << (text_len-1);
	for (i = 0; i < max; i++) {
		str_find_reset(ctx);
		pos = 0; offset = 0; ret = FALSE;
		for (j = 0; j < text_len; j++) {
			if ((i & (1 << j)) != 0) {
				if (str_find_more(ctx, text+pos, j-pos+1)) {
					ret = TRUE;
					break;
				}
				offset += j-pos + 1;
				pos = j + 1;
			}
		}
		if (pos != text_len && !ret) {
			if (str_find_more(ctx, text+pos, j-pos))
				ret = TRUE;
		}
		if (expected_pos < 0) {
			if (ret)
				return FALSE;
		} else {
			if (!ret)
				return FALSE;

			pos = str_find_get_match_end_pos(ctx) +
				offset - strlen(key);
			if ((int)pos != expected_pos)
				return FALSE;
		}
	}
	return TRUE;
}

struct str_find_input {
	const char *str;
	int pos;
};

void test_str_find(void)
{
	static const char *fail_input[] = {
		"xabc",
		"xabd",
		"abd"
	};
	unsigned int idx, len;
	const char *key, *p;
	unsigned int i;
	bool success = TRUE;

	for (idx = 0; idx < strlen(str_find_text); idx++) {
		for (len = strlen(str_find_text)-idx; len > 0; len--) {
			/* we'll get a search key for all substrings of text */
			T_BEGIN {
				key = t_strndup(str_find_text + idx, len);
				p = strstr(str_find_text, key);
				success = test_str_find_substring(key, p - str_find_text);
			} T_END;
			if (!success)
				break;
		}
	}
	for (i = 0; i < N_ELEMENTS(fail_input) && success; i++)
		success = test_str_find_substring(fail_input[i], -1);
	test_out("str_find()", success);
}