2
Test code for distance functions
4
Copyright 2011 Canonical Ltd.
7
Ted Gould <ted@canonical.com>
9
This program is free software: you can redistribute it and/or modify it
10
under the terms of the GNU General Public License version 3, as published
11
by the Free Software Foundation.
13
This program is distributed in the hope that it will be useful, but
14
WITHOUT ANY WARRANTY; without even the implied warranties of
15
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
16
PURPOSE. See the GNU General Public License for more details.
18
You should have received a copy of the GNU General Public License along
19
with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <glib-object.h>
25
#include "hudsettings.h"
28
/* hardcode some parameters so the test doesn't fail if the user
29
* has bogus things in GSettings.
31
HudSettings hud_settings = {
32
.indicator_penalty = 50,
35
.end_drop_penalty = 1,
41
calculate_distance (const gchar *search, GStrv teststrings, gchar ***matches)
43
HudStringList *list = NULL;
44
HudTokenList *haystack;
49
if (search == NULL || teststrings == NULL) {
53
for (i = 0; teststrings[i]; i++) {
56
tmp = hud_string_list_cons (teststrings[i], list);
57
hud_string_list_unref (list);
61
haystack = hud_token_list_new_from_string_list (list);
62
hud_string_list_unref (list);
64
needle = hud_token_list_new_from_string (search);
65
distance = hud_token_list_distance (haystack, needle, (const HudToken ***) matches);
68
/* These are owned by the tokenlists, so make copies
71
for (i = 0; (*matches)[i]; i++) {
72
const gchar *matchstr;
75
matchstr = hud_token_get_original ((HudToken *) (*matches)[i], &matchlen);
76
(*matches)[i] = g_strndup (matchstr, matchlen);
80
hud_token_list_free (haystack);
81
hud_token_list_free (needle);
86
/* Ensure the base calculation works */
88
test_distance_base (void)
90
gchar * testdata1[] = {"foo", NULL};
91
g_assert(calculate_distance("foo", testdata1, NULL) == 0);
93
gchar * testdata2[] = {"bar", NULL};
94
g_assert(calculate_distance("foo", testdata2, NULL) != 0);
96
g_assert(calculate_distance("foo", NULL, NULL) != 0);
98
g_assert(calculate_distance(NULL, testdata1, NULL) != 0);
103
/* Test a set of strings */
105
test_set (GStrv * teststrings, int num_tests, const gchar * search, int right)
109
for (i = 0; i < num_tests; i++) {
113
if (calculate_distance(search, teststrings[i], NULL) < calculate_distance(search, teststrings[right], NULL)) {
114
gchar * teststr = g_strjoinv(" > ", teststrings[i]);
115
gchar * rightstr = g_strjoinv(" > ", teststrings[right]);
117
g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);
127
/* Ensure the base calculation works */
129
test_distance_subfunction (void)
131
GStrv teststrings[4];
132
gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
133
gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
134
gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
135
gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;
137
test_set(teststrings, 4, "Print Pre", 3);
141
/* Ensure that we can handle some misspelling */
143
test_distance_missspelll (void)
145
GStrv teststrings[4];
146
gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
147
gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
148
gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
149
gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;
151
test_set(teststrings, 4, "Prnt Pr", 3);
152
test_set(teststrings, 4, "Print Preiw", 3);
153
test_set(teststrings, 4, "Prnt Pr", 3);
158
/* Ensure that we can find print with short strings */
160
test_distance_print_issues (void)
162
GStrv teststrings[6];
163
gchar * teststrings0[] = {"File", "New", NULL}; teststrings[0] = teststrings0;
164
gchar * teststrings1[] = {"File", "Open", NULL}; teststrings[1] = teststrings1;
165
gchar * teststrings2[] = {"Edit", "Undo", NULL}; teststrings[2] = teststrings2;
166
gchar * teststrings3[] = {"Help", "About", NULL}; teststrings[3] = teststrings3;
167
gchar * teststrings4[] = {"Help", "Empty", NULL}; teststrings[4] = teststrings4;
168
gchar * teststrings5[] = {"File", "Print...", NULL}; teststrings[5] = teststrings5;
170
test_set(teststrings, 6, "Pr", 5);
171
test_set(teststrings, 6, "Print", 5);
172
test_set(teststrings, 6, "Print...", 5);
177
/* A variety of strings that should have predictable results */
179
test_distance_variety (void)
181
GStrv teststrings[4];
182
gchar * teststrings0[] = {"Date", "House Cleaning", NULL}; teststrings[0] = teststrings0;
183
gchar * teststrings1[] = {"File", "Close Window", NULL}; teststrings[1] = teststrings1;
184
gchar * teststrings2[] = {"Edit", "Keyboard Shortcuts...", NULL}; teststrings[2] = teststrings2;
185
gchar * teststrings3[] = {"Network", "VPN Configuration", "Configure VPN...", NULL}; teststrings[3] = teststrings3;
187
test_set(teststrings, 4, "House", 0);
188
test_set(teststrings, 4, "House C", 0);
189
test_set(teststrings, 4, "House Cle", 0);
190
test_set(teststrings, 4, "House Clean", 0);
191
test_set(teststrings, 4, "Clean House", 0);
196
/* A variety of strings that should have predictable results */
198
test_distance_french_pref (void)
200
GStrv teststrings[3];
201
gchar * teststrings0[] = {"Fichier", "aperçu avant impression", NULL}; teststrings[0] = teststrings0;
202
gchar * teststrings1[] = {"Connexion au réseau...", NULL}; teststrings[1] = teststrings1;
203
gchar * teststrings2[] = {"Edition", "préférences", NULL}; teststrings[2] = teststrings2;
205
test_set(teststrings, 3, "préférences", 2);
206
test_set(teststrings, 3, "pré", 2);
207
test_set(teststrings, 3, "préf", 2);
208
test_set(teststrings, 3, "préfé", 2);
209
test_set(teststrings, 3, "pref", 2);
214
/* Check to make sure the returned hits are not dups and the
217
test_distance_dups (void)
220
gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};
222
g_assert(calculate_distance("ted inf", teststrings, &hits) != 0);
223
g_assert(g_strv_length(hits) == 2);
224
g_assert(g_strcmp0(hits[0], hits[1]) != 0);
231
/* Check to make sure 'Save' matches better than 'Save As...' for "save" */
233
test_distance_extra_terms (void)
235
const gchar *save_as[] = { "File", "Save", "As...", NULL };
236
const gchar *save[] = { "File", "Save", NULL };
238
g_assert_cmpint (calculate_distance ("save", (GStrv) save, NULL),
240
calculate_distance ("save", (GStrv) save_as, NULL));
243
/* Build the test suite */
245
test_distance_suite (void)
247
g_test_add_func ("/hud/distance/base", test_distance_base);
248
g_test_add_func ("/hud/distance/subfunction", test_distance_subfunction);
249
g_test_add_func ("/hud/distance/missspelll", test_distance_missspelll);
250
g_test_add_func ("/hud/distance/print_issues", test_distance_print_issues);
251
g_test_add_func ("/hud/distance/duplicates", test_distance_dups);
252
g_test_add_func ("/hud/distance/variety", test_distance_variety);
253
g_test_add_func ("/hud/distance/french_pref", test_distance_french_pref);
254
g_test_add_func ("/hud/distance/extra_terms", test_distance_extra_terms);
259
main (gint argc, gchar * argv[])
261
g_test_init(&argc, &argv, NULL);
264
test_distance_suite();
266
return g_test_run ();