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>
26
#include "../service/distance.h"
27
#include "../service/distance.c"
29
/* Ensure the base calculation works */
31
test_distance_base (void)
33
gchar * testdata1[] = {"foo", NULL};
34
g_assert(calculate_distance("foo", testdata1, NULL) == 0);
36
gchar * testdata2[] = {"bar", NULL};
37
g_assert(calculate_distance("foo", testdata2, NULL) != 0);
39
g_assert(calculate_distance("foo", NULL, NULL) != 0);
41
g_assert(calculate_distance(NULL, testdata1, NULL) != 0);
46
/* Test a set of strings */
48
test_set (GStrv * teststrings, int num_tests, const gchar * search, int right)
52
for (i = 0; i < num_tests; i++) {
56
if (calculate_distance(search, teststrings[i], NULL) < calculate_distance(search, teststrings[right], NULL)) {
57
gchar * teststr = g_strjoinv(" > ", teststrings[i]);
58
gchar * rightstr = g_strjoinv(" > ", teststrings[right]);
60
g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);
70
/* Ensure the base calculation works */
72
test_distance_subfunction (void)
75
gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
76
gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
77
gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
78
gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;
80
test_set(teststrings, 4, "Print Pre", 3);
84
/* Ensure that we can handle some misspelling */
86
test_distance_missspelll (void)
89
gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
90
gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
91
gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
92
gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;
94
test_set(teststrings, 4, "Prnt Pr", 3);
95
test_set(teststrings, 4, "Print Preiw", 3);
96
test_set(teststrings, 4, "Prnt Pr", 3);
101
/* Ensure that we can find print with short strings */
103
test_distance_print_issues (void)
105
GStrv teststrings[6];
106
gchar * teststrings0[] = {"File", "New", NULL}; teststrings[0] = teststrings0;
107
gchar * teststrings1[] = {"File", "Open", NULL}; teststrings[1] = teststrings1;
108
gchar * teststrings2[] = {"Edit", "Undo", NULL}; teststrings[2] = teststrings2;
109
gchar * teststrings3[] = {"Help", "About", NULL}; teststrings[3] = teststrings3;
110
gchar * teststrings4[] = {"Help", "Empty", NULL}; teststrings[4] = teststrings4;
111
gchar * teststrings5[] = {"File", "Print...", NULL}; teststrings[5] = teststrings5;
113
test_set(teststrings, 6, "Pr", 5);
114
test_set(teststrings, 6, "Print", 5);
115
test_set(teststrings, 6, "Print...", 5);
120
/* Check to make sure the returned hits are not dups and the
123
test_distance_dups (void)
126
gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};
128
g_assert(calculate_distance("ted inf", teststrings, &hits) != 0);
129
g_assert(g_strv_length(hits) == 2);
130
g_assert(g_strcmp0(hits[0], hits[1]) != 0);
137
/* Build the test suite */
139
test_distance_suite (void)
141
g_test_add_func ("/hud/distance/base", test_distance_base);
142
g_test_add_func ("/hud/distance/subfunction", test_distance_subfunction);
143
g_test_add_func ("/hud/distance/missspelll", test_distance_missspelll);
144
g_test_add_func ("/hud/distance/print_issues", test_distance_print_issues);
145
g_test_add_func ("/hud/distance/duplicates", test_distance_dups);
150
main (gint argc, gchar * argv[])
152
//gtk_init(&argc, &argv);
155
g_test_init(&argc, &argv, NULL);
158
test_distance_suite();
160
return g_test_run ();