summaryrefslogtreecommitdiff
path: root/test/fzytest.c
blob: d79ef6922b5289173f68d03ccbbb5dbe55dd4200 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#define _GNU_SOURCE
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>

#include "../config.h"
#include "match.h"
#include "choices.h"

int testsrun = 0, testsfailed = 0, assertionsrun = 0;

#define assert(x)                                                                                  \
	if (++assertionsrun && !(x)) {                                                             \
		fprintf(stderr, "test \"%s\" failed\n   assert(%s) was false\n   at %s:%i\n\n",    \
			__func__, #x, __FILE__, __LINE__);                                         \
		raise(SIGTRAP);                                                                    \
		testsfailed++;                                                                     \
		return;                                                                            \
	}

#define assert_streq(a, b) assert(!strcmp(a, b))

void runtest(void (*test)()) {
	testsrun++;
	test();
}

void test_match() {
	assert(has_match("a", "a"));
	assert(has_match("a", "ab"));
	assert(has_match("a", "ba"));
	assert(has_match("abc", "a|b|c"));

	/* non-match */
	assert(!has_match("a", ""));
	assert(!has_match("a", "b"));
	assert(!has_match("ass", "tags"));

	/* match when query is empty */
	assert(has_match("", ""));
	assert(has_match("", "a"));
}

void test_relative_scores() {
	/* App/Models/Order is better than App/MOdels/zRder  */
	assert(match("amor", "app/models/order") > match("amor", "app/models/zrder"));

	/* App/MOdels/foo is better than App/M/fOo  */
	assert(match("amo", "app/m/foo") < match("amo", "app/models/foo"));

	/* GEMFIle.Lock < GEMFILe  */
	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));

	/* GEMFIle.Lock < GEMFILe  */
	assert(match("gemfil", "Gemfile.lock") < match("gemfil", "Gemfile"));

	/* Prefer shorter matches */
	assert(match("abce", "abcdef") > match("abce", "abc de"));

	/* Prefer shorter candidates */
	assert(match("test", "tests") > match("test", "testing"));

	/* Scores first letter highly */
	assert(match("test", "testing") > match("test", "/testing"));

	/* Prefer shorter matches */
	assert(match("abc", "    a b c ") > match("abc", " a  b  c "));
	assert(match("abc", " a b c    ") > match("abc", " a  b  c "));
}

void test_exact_scores() {
	/* Exact match is SCORE_MAX */
	assert(match("abc", "abc") == SCORE_MAX);
	assert(match("aBc", "abC") == SCORE_MAX);

	/* Empty query always results in SCORE_MIN */
	assert(match("", "") == SCORE_MIN);
	assert(match("", "a") == SCORE_MIN);
	assert(match("", "bb") == SCORE_MIN);

	/* Gaps */
	assert(match("a", "*a") == SCORE_GAP_LEADING);
	assert(match("a", "*ba") == SCORE_GAP_LEADING*2);
	assert(match("a", "**a*") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING);
	assert(match("a", "**a**") == SCORE_GAP_LEADING*2 + SCORE_GAP_TRAILING*2);
	assert(match("aa", "**aa**") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CONSECUTIVE + SCORE_GAP_TRAILING*2);
	assert(match("aa", "**a*a**") == SCORE_GAP_LEADING + SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_GAP_TRAILING + SCORE_GAP_TRAILING);

	/* Consecutive */
	assert(match("aa", "*aa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE);
	assert(match("aaa", "*aaa") == SCORE_GAP_LEADING + SCORE_MATCH_CONSECUTIVE*2);
	assert(match("aaa", "*a*aa") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_CONSECUTIVE);

	/* Slash */
	assert(match("a", "/a") == SCORE_GAP_LEADING + SCORE_MATCH_SLASH);
	assert(match("a", "*/a") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH);
	assert(match("aa", "a/aa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_SLASH + SCORE_MATCH_CONSECUTIVE);

	/* Capital */
	assert(match("a", "bA") == SCORE_GAP_LEADING + SCORE_MATCH_CAPITAL);
	assert(match("a", "baA") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL);
	assert(match("aa", "baAa") == SCORE_GAP_LEADING*2 + SCORE_MATCH_CAPITAL + SCORE_MATCH_CONSECUTIVE);

	/* Dot */
	assert(match("a", ".a") == SCORE_GAP_LEADING + SCORE_MATCH_DOT);
	assert(match("a", "*a.a") == SCORE_GAP_LEADING*3 + SCORE_MATCH_DOT);
	assert(match("a", "*a.a") == SCORE_GAP_LEADING + SCORE_GAP_INNER + SCORE_MATCH_DOT);
}

void test_positions_1() {
	size_t positions[3];
	match_positions("amo", "app/models/foo", positions);
	assert(positions[0] == 0);
	assert(positions[1] == 4);
	assert(positions[2] == 5);
}

void test_positions_2() {
	/*
	 * We should prefer matching the 'o' in order, since it's the beginning
	 * of a word.
	 */
	size_t positions[4];
	match_positions("amor", "app/models/order", positions);
	assert(positions[0] == 0);
	assert(positions[1] == 4);
	assert(positions[2] == 11);
}

void test_positions_3() {
	size_t positions[2];
	match_positions("as", "tags", positions);
	assert(positions[0] == 1);
	assert(positions[1] == 3);
}

void test_positions_4() {
	size_t positions[2];
	match_positions("as", "examples.txt", positions);
	assert(positions[0] == 2);
	assert(positions[1] == 7);
}

void test_positions_5() {
	size_t positions[3];
	match_positions("abc", "a/a/b/c/c", positions);
	assert(positions[0] == 2);
	assert(positions[1] == 4);
	assert(positions[2] == 6);
}

void test_positions_exact() {
	size_t positions[3];
	match_positions("foo", "foo", positions);
	assert(positions[0] == 0);
	assert(positions[1] == 1);
	assert(positions[2] == 2);
}

void test_choices_empty() {
	choices_t choices;
	choices_init(&choices);
	assert(choices.size == 0);
	assert(choices.available == 0);
	assert(choices.selection == 0);

	choices_prev(&choices);
	assert(choices.selection == 0);

	choices_next(&choices);
	assert(choices.selection == 0);

	choices_destroy(&choices);
}

void test_choices_1() {
	choices_t choices;
	choices_init(&choices);
	choices_add(&choices, "tags");

	choices_search(&choices, "");
	assert(choices.available == 1);
	assert(choices.selection == 0);

	choices_search(&choices, "t");
	assert(choices.available == 1);
	assert(choices.selection == 0);

	choices_prev(&choices);
	assert(choices.selection == 0);

	choices_next(&choices);
	assert(choices.selection == 0);

	assert(!strcmp(choices_get(&choices, 0), "tags"));
	assert(choices_get(&choices, 1) == NULL);

	choices_destroy(&choices);
}

void test_choices_2() {
	choices_t choices;
	choices_init(&choices);
	choices_add(&choices, "tags");
	choices_add(&choices, "test");

	/* Empty search */
	choices_search(&choices, "");
	assert(choices.selection == 0);
	assert(choices.available == 2);
	assert_streq(choices_get(&choices, 0), "tags");
	assert_streq(choices_get(&choices, 1), "test");

	choices_next(&choices);
	assert(choices.selection == 1);
	choices_next(&choices);
	assert(choices.selection == 0);

	choices_prev(&choices);
	assert(choices.selection == 1);
	choices_prev(&choices);
	assert(choices.selection == 0);

	/* Filtered search */
	choices_search(&choices, "te");
	assert(choices.available == 1);
	assert(choices.selection == 0);
	assert_streq(choices_get(&choices, 0), "test");

	choices_next(&choices);
	assert(choices.selection == 0);

	choices_prev(&choices);
	assert(choices.selection == 0);

	/* No results */
	choices_search(&choices, "foobar");
	assert(choices.available == 0);
	assert(choices.selection == 0);

	/* Different order due to scoring */
	choices_search(&choices, "ts");
	assert(choices.available == 2);
	assert(choices.selection == 0);
	assert_streq(choices_get(&choices, 0), "test");
	assert_streq(choices_get(&choices, 1), "tags");

	choices_destroy(&choices);
}

void test_choices_without_search() {
	/* Before a search is run, it should return no results */

	choices_t choices;
	choices_init(&choices);

	assert(choices.available == 0);
	assert(choices.selection == 0);
	assert(choices.size == 0);
	assert(choices_get(&choices, 0) == NULL);

	choices_add(&choices, "test");

	assert(choices.available == 0);
	assert(choices.selection == 0);
	assert(choices.size == 1);
	assert(choices_get(&choices, 0) == NULL);

	choices_destroy(&choices);
}

/* Regression test for segfault */
void test_choices_unicode() {
	choices_t choices;
	choices_init(&choices);

	choices_add(&choices, "Edmund Husserl - Méditations cartésiennes - Introduction a la phénoménologie.pdf");
	choices_search(&choices, "e");

	choices_destroy(&choices);
}

void test_choices_large_input() {
	choices_t choices;
	choices_init(&choices);

	int N = 100000;
	char *strings[N];

	for(int i = 0; i < N; i++) {
		asprintf(&strings[i], "%i", i);
		choices_add(&choices, strings[i]);
	}

	choices_search(&choices, "12");

	/* Must match `seq 0 99999 | grep '.*1.*2.*' | wc -l` */
	assert(choices.available == 8146);

	assert_streq(choices_get(&choices, 0), "12")

	for(int i = 0; i < N; i++) {
		free(strings[i]);
	}

	choices_destroy(&choices);
}

void summary() {
	printf("%i tests, %i assertions, %i failures\n", testsrun, assertionsrun, testsfailed);
}

static void ignore_signal(int signum) {
	(void)signum;
}

int main(int argc, char *argv[]) {
	(void)argc;
	(void)argv;

	/* We raise sigtrap on all assertion failures.
	 * If we have no debugger running, we should ignore it */
	signal(SIGTRAP, ignore_signal);

	runtest(test_match);
	runtest(test_relative_scores);
	runtest(test_exact_scores);
	runtest(test_positions_1);
	runtest(test_positions_2);
	runtest(test_positions_3);
	runtest(test_positions_4);
	runtest(test_positions_5);
	runtest(test_positions_exact);

	runtest(test_choices_empty);
	runtest(test_choices_1);
	runtest(test_choices_2);
	runtest(test_choices_without_search);
	runtest(test_choices_unicode);
	runtest(test_choices_large_input);

	summary();

	/* exit 0 if all tests pass */
	return !!testsfailed;
}