summaryrefslogtreecommitdiff
path: root/src/camel/tests/misc/split.c
blob: 9f7f4185bef777cdd39afd719a4040903668d641 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * This library is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "evolution-data-server-config.h"

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <camel/camel-search-private.h>

#include "camel-test.h"

/* TODO: should put utf8 stuff here too */

static struct {
	const gchar *word;
	gint count;
	struct {
		const gchar *word;
		gint type;
	} splits[5];
} split_tests[] = {
	{ "simple", 1, { { "simple", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "two words", 2, { { "two", CAMEL_SEARCH_WORD_SIMPLE }, {"words" , CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "compl;ex", 1, { { "compl;ex", CAMEL_SEARCH_WORD_COMPLEX } } },
	{ "compl;ex simple", 2, { { "compl;ex", CAMEL_SEARCH_WORD_COMPLEX} , {"simple", CAMEL_SEARCH_WORD_SIMPLE} } },
	{ "\"quoted\"", 1, { { "quoted", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "\"quoted double\"", 1, { { "quoted double", CAMEL_SEARCH_WORD_COMPLEX } } },
	{ "\"quoted double\" compl;ex", 2, { { "quoted double", CAMEL_SEARCH_WORD_COMPLEX }, { "compl;ex", CAMEL_SEARCH_WORD_COMPLEX } } },
	{ "\"quoted gdouble \\\" escaped\"", 1, { { "quoted gdouble \" escaped", CAMEL_SEARCH_WORD_COMPLEX } } },
	{ "\"quoted\\\"double\" \\\" escaped\\\"", 3, { { "quoted\"double", CAMEL_SEARCH_WORD_COMPLEX }, {"\"", CAMEL_SEARCH_WORD_COMPLEX}, { "escaped\"", CAMEL_SEARCH_WORD_COMPLEX } } },
	{ "\\\"escaped", 1, { { "\"escaped", CAMEL_SEARCH_WORD_COMPLEX } } },

};

static struct {
	const gchar *word;
	gint count;
	struct {
		const gchar *word;
		gint type;
	} splits[5];
} simple_tests[] = {
	{ "simple", 1, { {"simple", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "simpleCaSe", 1, { { "simplecase", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "two words", 2, { { "two", CAMEL_SEARCH_WORD_SIMPLE }, { "words", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "two wordscAsE", 2, { { "two", CAMEL_SEARCH_WORD_SIMPLE} ,  { "wordscase", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "compl;ex", 2, { { "compl", CAMEL_SEARCH_WORD_SIMPLE }, { "ex", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "compl;ex simple", 3, { { "compl", CAMEL_SEARCH_WORD_SIMPLE }, { "ex", CAMEL_SEARCH_WORD_SIMPLE }, { "simple", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "\"quoted compl;ex\" simple", 4, { { "quoted", CAMEL_SEARCH_WORD_SIMPLE}, { "compl", CAMEL_SEARCH_WORD_SIMPLE }, { "ex", CAMEL_SEARCH_WORD_SIMPLE }, { "simple", CAMEL_SEARCH_WORD_SIMPLE } } },
	{ "\\\" \"quoted\"compl;ex\" simple", 4, { { "quoted", CAMEL_SEARCH_WORD_SIMPLE}, { "compl", CAMEL_SEARCH_WORD_SIMPLE }, { "ex", CAMEL_SEARCH_WORD_SIMPLE }, { "simple", CAMEL_SEARCH_WORD_SIMPLE } } },
};

gint
main (gint argc,
      gchar **argv)
{
	gint i, j;
	struct _camel_search_words *words, *tmp;

	camel_test_init (argc, argv);

	camel_test_start ("Search splitting");

	for (i = 0; i < G_N_ELEMENTS (split_tests); i++) {
		camel_test_push ("split %d '%s'", i, split_tests[i].word);

		words = camel_search_words_split ((const guchar *) split_tests[i].word);
		check (words != NULL);
		check_msg (words->len == split_tests[i].count, "words->len = %d, count = %d", words->len, split_tests[i].count);

		for (j = 0; j < words->len; j++) {
			check_msg (
				strcmp (split_tests[i].splits[j].word, words->words[j]->word) == 0,
				"'%s' != '%s'", split_tests[i].splits[j].word, words->words[j]->word);
			check (split_tests[i].splits[j].type == words->words[j]->type);
		}

		camel_search_words_free (words);
		camel_test_pull ();
	}

	camel_test_end ();

	camel_test_start ("Search splitting - simple");

	for (i = 0; i < G_N_ELEMENTS (simple_tests); i++) {
		camel_test_push ("simple split %d '%s'", i, simple_tests[i].word);

		tmp = camel_search_words_split ((const guchar *) simple_tests[i].word);
		check (tmp != NULL);

		words = camel_search_words_simple (tmp);
		check (words != NULL);
		check_msg (words->len == simple_tests[i].count, "words->len = %d, count = %d", words->len, simple_tests[i].count);

		for (j = 0; j < words->len; j++) {
			check_msg (
				strcmp (simple_tests[i].splits[j].word, words->words[j]->word) == 0,
				"'%s' != '%s'", simple_tests[i].splits[j].word, words->words[j]->word);
			check (simple_tests[i].splits[j].type == words->words[j]->type);
		}

		camel_search_words_free (words);
		camel_search_words_free (tmp);
		camel_test_pull ();
	}

	camel_test_end ();

	return 0;
}