~jbicha/hud/build-depend-on-valac-not-gir

« back to all changes in this revision

Viewing changes to tests/test-distance.c

Merging the HUD into indicator-appmenu

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
#define DUMP_MATRIX 1
 
26
#include "../service/distance.h"
 
27
#include "../service/distance.c"
 
28
 
 
29
/* Ensure the base calculation works */
 
30
static void
 
31
test_distance_base (void)
 
32
{
 
33
        gchar * testdata1[] = {"foo", NULL};
 
34
        g_assert(calculate_distance("foo", testdata1, NULL) == 0);
 
35
 
 
36
        gchar * testdata2[] = {"bar", NULL};
 
37
        g_assert(calculate_distance("foo", testdata2, NULL) != 0);
 
38
 
 
39
        g_assert(calculate_distance("foo", NULL, NULL) != 0);
 
40
 
 
41
        g_assert(calculate_distance(NULL, testdata1, NULL) != 0);
 
42
 
 
43
        return;
 
44
}
 
45
 
 
46
/* Test a set of strings */
 
47
static void
 
48
test_set (GStrv * teststrings, int num_tests, const gchar * search, int right)
 
49
{
 
50
        int i;
 
51
 
 
52
        for (i = 0; i < num_tests; i++) {
 
53
                if (i == right)
 
54
                        continue;
 
55
 
 
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]);
 
59
 
 
60
                        g_error("Found '%s' with search string '%s' instead of '%s'", teststr, search, rightstr);
 
61
 
 
62
                        g_free(teststr);
 
63
                        g_free(rightstr);
 
64
                }
 
65
        }
 
66
 
 
67
        return;
 
68
}
 
69
 
 
70
/* Ensure the base calculation works */
 
71
static void
 
72
test_distance_subfunction (void)
 
73
{
 
74
        GStrv teststrings[4];
 
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;
 
79
 
 
80
        test_set(teststrings, 4, "Print Pre", 3);
 
81
        return;
 
82
}
 
83
 
 
84
/* Ensure that we can handle some misspelling */
 
85
static void
 
86
test_distance_missspelll (void)
 
87
{
 
88
        GStrv teststrings[4];
 
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;
 
93
 
 
94
        test_set(teststrings, 4, "Prnt Pr", 3);
 
95
        test_set(teststrings, 4, "Print Preiw", 3);
 
96
        test_set(teststrings, 4, "Prnt Pr", 3);
 
97
 
 
98
        return;
 
99
}
 
100
 
 
101
/* Ensure that we can find print with short strings */
 
102
static void
 
103
test_distance_print_issues (void)
 
104
{
 
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;
 
112
 
 
113
        test_set(teststrings, 6, "Pr", 5);
 
114
        test_set(teststrings, 6, "Print", 5);
 
115
        test_set(teststrings, 6, "Print...", 5);
 
116
 
 
117
        return;
 
118
}
 
119
 
 
120
/* Check to make sure the returned hits are not dups and the
 
121
   proper number */
 
122
static void
 
123
test_distance_dups (void)
 
124
{
 
125
        GStrv hits = NULL;
 
126
        gchar * teststrings[] = {"Inflated", "Confluated", "Sublimated", "Sadated", "Situated", "Infatuated", NULL};
 
127
 
 
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);
 
131
 
 
132
        g_strfreev(hits);
 
133
 
 
134
        return;
 
135
}
 
136
 
 
137
/* Build the test suite */
 
138
static void
 
139
test_distance_suite (void)
 
140
{
 
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);
 
146
        return;
 
147
}
 
148
 
 
149
gint
 
150
main (gint argc, gchar * argv[])
 
151
{
 
152
        //gtk_init(&argc, &argv);
 
153
        g_type_init();
 
154
 
 
155
        g_test_init(&argc, &argv, NULL);
 
156
 
 
157
        /* Test suites */
 
158
        test_distance_suite();
 
159
 
 
160
        return g_test_run ();
 
161
}