~bregma/indicator-appmenu/lp-1199877

« back to all changes in this revision

Viewing changes to tests/test-distance.c

  • Committer: Charles Kerr
  • Date: 2012-12-03 17:40:45 UTC
  • Revision ID: charles.kerr@canonical.com-20121203174045-cq45hp4y3dksdwut
revert r223 as per didrocks' request

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Test code for distance functions
 
3
 
 
4
Copyright 2011 Canonical Ltd.
 
5
 
 
6
Authors:
 
7
    Ted Gould <ted@canonical.com>
 
8
 
 
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.
 
12
 
 
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.
 
17
 
 
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/>.
 
20
*/
 
21
 
 
22
#include <glib.h>
 
23
#include <glib-object.h>
 
24
 
 
25
#include "hudsettings.h"
 
26
#include "hudtoken.h"
 
27
 
 
28
/* hardcode some parameters so the test doesn't fail if the user
 
29
 * has bogus things in GSettings.
 
30
 */
 
31
HudSettings hud_settings = {
 
32
        .indicator_penalty = 50,
 
33
        .add_penalty = 10,
 
34
        .drop_penalty = 10,
 
35
        .end_drop_penalty = 1,
 
36
        .swap_penalty = 15,
 
37
        .max_distance = 30
 
38
};
 
39
 
 
40
static guint
 
41
calculate_distance (const gchar *search, GStrv teststrings, gchar ***matches)
 
42
{
 
43
        HudStringList *list = NULL;
 
44
        HudTokenList *haystack;
 
45
        HudTokenList *needle;
 
46
        guint distance;
 
47
        gint i;
 
48
 
 
49
        if (search == NULL || teststrings == NULL) {
 
50
                return G_MAXINT;
 
51
        }
 
52
 
 
53
        for (i = 0; teststrings[i]; i++) {
 
54
                HudStringList *tmp;
 
55
 
 
56
                tmp = hud_string_list_cons (teststrings[i], list);
 
57
                hud_string_list_unref (list);
 
58
                list = tmp;
 
59
        }
 
60
 
 
61
        haystack = hud_token_list_new_from_string_list (list);
 
62
        hud_string_list_unref (list);
 
63
 
 
64
        needle = hud_token_list_new_from_string (search);
 
65
        distance = hud_token_list_distance (haystack, needle, (const HudToken ***) matches);
 
66
 
 
67
        if (matches) {
 
68
                /* These are owned by the tokenlists, so make copies
 
69
                 * before freeing.
 
70
                 */
 
71
                for (i = 0; (*matches)[i]; i++) {
 
72
                        const gchar *matchstr;
 
73
                        guint matchlen;
 
74
 
 
75
                        matchstr = hud_token_get_original ((HudToken *) (*matches)[i], &matchlen);
 
76
                        (*matches)[i] = g_strndup (matchstr, matchlen);
 
77
                }
 
78
        }
 
79
 
 
80
        hud_token_list_free (haystack);
 
81
        hud_token_list_free (needle);
 
82
 
 
83
        return distance;
 
84
}
 
85
 
 
86
/* Ensure the base calculation works */
 
87
static void
 
88
test_distance_base (void)
 
89
{
 
90
        gchar * testdata1[] = {"foo", NULL};
 
91
        g_assert(calculate_distance("foo", testdata1, NULL) == 0);
 
92
 
 
93
        gchar * testdata2[] = {"bar", NULL};
 
94
        g_assert(calculate_distance("foo", testdata2, NULL) != 0);
 
95
 
 
96
        g_assert(calculate_distance("foo", NULL, NULL) != 0);
 
97
 
 
98
        g_assert(calculate_distance(NULL, testdata1, NULL) != 0);
 
99
 
 
100
        return;
 
101
}
 
102
 
 
103
/* Test a set of strings */
 
104
static void
 
105
test_set (GStrv * teststrings, int num_tests, const gchar * search, int right)
 
106
{
 
107
        int i;
 
108
 
 
109
        for (i = 0; i < num_tests; i++) {
 
110
                if (i == right)
 
111
                        continue;
 
112
 
 
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]);
 
116
 
 
117
                        g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);
 
118
 
 
119
                        g_free(teststr);
 
120
                        g_free(rightstr);
 
121
                }
 
122
        }
 
123
 
 
124
        return;
 
125
}
 
126
 
 
127
/* Ensure the base calculation works */
 
128
static void
 
129
test_distance_subfunction (void)
 
130
{
 
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;
 
136
 
 
137
        test_set(teststrings, 4, "Print Pre", 3);
 
138
        return;
 
139
}
 
140
 
 
141
/* Ensure that we can handle some misspelling */
 
142
static void
 
143
test_distance_missspelll (void)
 
144
{
 
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;
 
150
 
 
151
        test_set(teststrings, 4, "Prnt Pr", 3);
 
152
        test_set(teststrings, 4, "Print Preiw", 3);
 
153
        test_set(teststrings, 4, "Prnt Pr", 3);
 
154
 
 
155
        return;
 
156
}
 
157
 
 
158
/* Ensure that we can find print with short strings */
 
159
static void
 
160
test_distance_print_issues (void)
 
161
{
 
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;
 
169
 
 
170
        test_set(teststrings, 6, "Pr", 5);
 
171
        test_set(teststrings, 6, "Print", 5);
 
172
        test_set(teststrings, 6, "Print...", 5);
 
173
 
 
174
        return;
 
175
}
 
176
 
 
177
/* A variety of strings that should have predictable results */
 
178
static void
 
179
test_distance_variety (void)
 
180
{
 
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;
 
186
 
 
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);
 
192
 
 
193
        return;
 
194
}
 
195
 
 
196
/* A variety of strings that should have predictable results */
 
197
static void
 
198
test_distance_french_pref (void)
 
199
{
 
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;
 
204
 
 
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);
 
210
 
 
211
        return;
 
212
}
 
213
 
 
214
/* Check to make sure the returned hits are not dups and the
 
215
   proper number */
 
216
static void
 
217
test_distance_dups (void)
 
218
{
 
219
        GStrv hits = NULL;
 
220
        gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};
 
221
 
 
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);
 
225
 
 
226
        g_strfreev(hits);
 
227
 
 
228
        return;
 
229
}
 
230
 
 
231
/* Check to make sure 'Save' matches better than 'Save As...' for "save" */
 
232
static void
 
233
test_distance_extra_terms (void)
 
234
{
 
235
  const gchar *save_as[] = { "File", "Save", "As...", NULL };
 
236
  const gchar *save[] = { "File", "Save", NULL };
 
237
 
 
238
  g_assert_cmpint (calculate_distance ("save", (GStrv) save, NULL),
 
239
                   <,
 
240
                   calculate_distance ("save", (GStrv) save_as, NULL));
 
241
}
 
242
 
 
243
/* Build the test suite */
 
244
static void
 
245
test_distance_suite (void)
 
246
{
 
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);
 
255
        return;
 
256
}
 
257
 
 
258
gint
 
259
main (gint argc, gchar * argv[])
 
260
{
 
261
        //gtk_init(&argc, &argv);
 
262
        g_type_init();
 
263
 
 
264
        g_test_init(&argc, &argv, NULL);
 
265
 
 
266
        /* Test suites */
 
267
        test_distance_suite();
 
268
 
 
269
        return g_test_run ();
 
270
}