~ted/indicator-appmenu/hud

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Test code for distance functions

Copyright 2011 Canonical Ltd.

Authors:
    Ted Gould <ted@canonical.com>

This program is free software: you can redistribute it and/or modify it 
under the terms of the GNU General Public License version 3, as published 
by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but 
WITHOUT ANY WARRANTY; without even the implied warranties of 
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along 
with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <glib.h>
#include <glib-object.h>

#define DUMP_MATRIX 1
#include "distance.h"
#include "distance.c"
#include "utils.c"

/* Ensure the base calculation works */
static void
test_distance_base (void)
{
	gchar * testdata1[] = {"foo", NULL};
	g_assert(calculate_distance("foo", testdata1, NULL) == 0);

	gchar * testdata2[] = {"bar", NULL};
	g_assert(calculate_distance("foo", testdata2, NULL) != 0);

	g_assert(calculate_distance("foo", NULL, NULL) != 0);

	g_assert(calculate_distance(NULL, testdata1, NULL) != 0);

	return;
}

/* Test a set of strings */
static void
test_set (GStrv * teststrings, int num_tests, const gchar * search, int right)
{
	int i;

	for (i = 0; i < num_tests; i++) {
		if (i == right)
			continue;

		if (calculate_distance(search, teststrings[i], NULL) < calculate_distance(search, teststrings[right], NULL)) {
			gchar * teststr = g_strjoinv(" > ", teststrings[i]);
			gchar * rightstr = g_strjoinv(" > ", teststrings[right]);

			g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);

			g_free(teststr);
			g_free(rightstr);
		}
	}

	return;
}

/* Ensure the base calculation works */
static void
test_distance_subfunction (void)
{
	GStrv teststrings[4];
	gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
	gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
	gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
	gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;

	test_set(teststrings, 4, "Print Pre", 3);
	return;
}

/* Ensure that we can handle some misspelling */
static void
test_distance_missspelll (void)
{
	GStrv teststrings[4];
	gchar * teststrings0[] = {"File", "Open", NULL}; teststrings[0] = teststrings0;
	gchar * teststrings1[] = {"File", "New", NULL}; teststrings[1] = teststrings1;
	gchar * teststrings2[] = {"File", "Print", NULL}; teststrings[2] = teststrings2;
	gchar * teststrings3[] = {"File", "Print Preview", NULL}; teststrings[3] = teststrings3;

	test_set(teststrings, 4, "Prnt Pr", 3);
	test_set(teststrings, 4, "Print Preiw", 3);
	test_set(teststrings, 4, "Prnt Pr", 3);

	return;
}

/* Ensure that we can find print with short strings */
static void
test_distance_print_issues (void)
{
	GStrv teststrings[6];
	gchar * teststrings0[] = {"File", "New", NULL}; teststrings[0] = teststrings0;
	gchar * teststrings1[] = {"File", "Open", NULL}; teststrings[1] = teststrings1;
	gchar * teststrings2[] = {"Edit", "Undo", NULL}; teststrings[2] = teststrings2;
	gchar * teststrings3[] = {"Help", "About", NULL}; teststrings[3] = teststrings3;
	gchar * teststrings4[] = {"Help", "Empty", NULL}; teststrings[4] = teststrings4;
	gchar * teststrings5[] = {"File", "Print...", NULL}; teststrings[5] = teststrings5;

	test_set(teststrings, 6, "Pr", 5);
	test_set(teststrings, 6, "Print", 5);
	test_set(teststrings, 6, "Print...", 5);

	return;
}

/* A variety of strings that should have predictable results */
static void
test_distance_variety (void)
{
	GStrv teststrings[4];
	gchar * teststrings0[] = {"Date", "House Cleaning", NULL}; teststrings[0] = teststrings0;
	gchar * teststrings1[] = {"File", "Close Window", NULL}; teststrings[1] = teststrings1;
	gchar * teststrings2[] = {"Edit", "Keyboard Shortcuts...", NULL}; teststrings[2] = teststrings2;
	gchar * teststrings3[] = {"Network", "VPN Configuration", "Configure VPN...", NULL}; teststrings[3] = teststrings3;

	test_set(teststrings, 4, "House", 0);
	test_set(teststrings, 4, "House C", 0);
	test_set(teststrings, 4, "House Cle", 0);
	test_set(teststrings, 4, "House Clean", 0);
	test_set(teststrings, 4, "Clean House", 0);

	return;
}

/* Check to make sure the returned hits are not dups and the
   proper number */
static void
test_distance_dups (void)
{
	GStrv hits = NULL;
	gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};

	g_assert(calculate_distance("ted inf", teststrings, &hits) != 0);
	g_assert(g_strv_length(hits) == 2);
	g_assert(g_strcmp0(hits[0], hits[1]) != 0);

	g_strfreev(hits);

	return;
}

/* Build the test suite */
static void
test_distance_suite (void)
{
	g_test_add_func ("/hud/distance/base",          test_distance_base);
	g_test_add_func ("/hud/distance/subfunction",   test_distance_subfunction);
	g_test_add_func ("/hud/distance/missspelll",    test_distance_missspelll);
	g_test_add_func ("/hud/distance/print_issues",  test_distance_print_issues);
	g_test_add_func ("/hud/distance/duplicates",    test_distance_dups);
	g_test_add_func ("/hud/distance/variety",       test_distance_variety);
	return;
}

gint
main (gint argc, gchar * argv[])
{
	//gtk_init(&argc, &argv);
	g_type_init();

	g_test_init(&argc, &argv, NULL);

	/* Test suites */
	test_distance_suite();

	return g_test_run ();
}