forked from wmorgan/whistlepig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-search.c
407 lines (303 loc) · 10.3 KB
/
test-search.c
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "test.h"
#include "query.h"
#include "query-parser.h"
#include "index.h"
#define INDEX_PATH "/tmp/index-test"
RAISING_STATIC(add_string(wp_index* index, const char* string)) {
uint64_t doc_id;
wp_entry* entry = wp_entry_new();
RELAY_ERROR(wp_entry_add_string(entry, "body", string));
RELAY_ERROR(wp_index_add_entry(index, entry, &doc_id));
RELAY_ERROR(wp_entry_free(entry));
return NO_ERROR;
}
wp_error* setup(wp_index** index) {
RELAY_ERROR(wp_index_delete(INDEX_PATH));
RELAY_ERROR(wp_index_create(index, INDEX_PATH));
RELAY_ERROR(add_string(*index, "one two three"));
RELAY_ERROR(add_string(*index, "two three four"));
RELAY_ERROR(add_string(*index, "three four five"));
return NO_ERROR;
}
wp_error* shutdown(wp_index* index) {
RELAY_ERROR(wp_index_free(index));
RELAY_ERROR(wp_index_delete(INDEX_PATH));
return NO_ERROR;
}
#define RUN_QUERY(q) \
RELAY_ERROR(wp_query_parse(q, "body", &query)); \
RELAY_ERROR(wp_index_setup_query(index, query)); \
RELAY_ERROR(wp_index_run_query(index, query, 10, &num_results, &results[0])); \
RELAY_ERROR(wp_index_teardown_query(index, query)); \
wp_query_free(query); \
TEST(conjunctions) {
wp_index* index;
wp_query* query;
uint64_t results[10];
uint32_t num_results;
RELAY_ERROR(setup(&index));
RUN_QUERY("one two");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("three five");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
RUN_QUERY("one five");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("three");
ASSERT_EQUALS_UINT(3, num_results);
RUN_QUERY("one");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("one one one");
ASSERT_EQUALS_UINT(1, num_results);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(disjunctions) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
RUN_QUERY("one OR two");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
ASSERT_EQUALS_UINT64(1, results[1]);
RUN_QUERY("one OR two OR three");
ASSERT_EQUALS_UINT(3, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
ASSERT_EQUALS_UINT64(1, results[2]);
RUN_QUERY("three OR two OR one");
ASSERT_EQUALS_UINT(3, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
ASSERT_EQUALS_UINT64(1, results[2]);
RUN_QUERY("one OR one OR one");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("six OR one");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("six OR nine");
ASSERT_EQUALS_UINT(0, num_results);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(phrases) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
RUN_QUERY("\"one\"");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("\"one two\"");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("\"one two three\"");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("\"one two three four\"");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("\"three four five\"");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
RUN_QUERY("\"two three\"");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
ASSERT_EQUALS_UINT64(1, results[1]);
RUN_QUERY("\"two two\"");
ASSERT_EQUALS_UINT(0, num_results);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(combinations) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
RUN_QUERY("two (three OR one)");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
ASSERT_EQUALS_UINT64(1, results[1]);
RUN_QUERY("two (three OR four)");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
ASSERT_EQUALS_UINT64(1, results[1]);
RUN_QUERY("two (three OR four OR five)");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
ASSERT_EQUALS_UINT64(1, results[1]);
RUN_QUERY("\"two three\" OR four");
ASSERT_EQUALS_UINT(3, num_results);
RUN_QUERY("\"two three\" one");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RUN_QUERY("three -one");
ASSERT_EQUALS_UINT(2, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(resumability) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
// query matches three docs, one at a time
RELAY_ERROR(wp_query_parse("three", "body", &query));
RELAY_ERROR(wp_index_setup_query(index, query));
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(2, results[0]);
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(0, num_results);
RELAY_ERROR(wp_index_teardown_query(index, query));
wp_query_free(query);
// query matches one doc, one at a time
RELAY_ERROR(wp_query_parse("one", "body", &query));
RELAY_ERROR(wp_index_setup_query(index, query));
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(1, results[0]);
RELAY_ERROR(wp_index_run_query(index, query, 1, &num_results, &results[0]));
ASSERT_EQUALS_UINT(0, num_results);
RELAY_ERROR(wp_index_teardown_query(index, query));
wp_query_free(query);
return NO_ERROR;
}
// found a bug in the phrase matching that this captures
TEST(phrases_against_multiple_matches_in_doc) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
// add another doc
RELAY_ERROR(add_string(index, "five four five six"));
RUN_QUERY("\"four five six\"");
ASSERT_EQUALS_UINT(1, num_results);
ASSERT_EQUALS_UINT64(4, results[0]);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(queries_against_an_empty_index) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(wp_index_delete(INDEX_PATH));
RELAY_ERROR(wp_index_create(&index, INDEX_PATH));
RUN_QUERY("hello");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("\"four five\"");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("four OR five");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("-hello");
ASSERT_EQUALS_UINT(0, num_results);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(fielded_queries) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
uint64_t doc_id;
wp_entry* entry;
RELAY_ERROR(wp_index_delete(INDEX_PATH));
RELAY_ERROR(wp_index_create(&index, INDEX_PATH));
entry = wp_entry_new();
RELAY_ERROR(wp_entry_add_string(entry, "subject", "hello there bob"));
RELAY_ERROR(wp_entry_add_string(entry, "from", "joe blow"));
RELAY_ERROR(wp_entry_add_string(entry, "to", "bob bobson"));
RELAY_ERROR(wp_entry_add_string(entry, "body", "hi bob how are you? i am fine. your friend, joe"));
RELAY_ERROR(wp_index_add_entry(index, entry, &doc_id));
RELAY_ERROR(wp_entry_free(entry));
entry = wp_entry_new();
RELAY_ERROR(wp_entry_add_string(entry, "subject", "hello i'm on vacation"));
RELAY_ERROR(wp_entry_add_string(entry, "from", "joe blow"));
RELAY_ERROR(wp_entry_add_string(entry, "to", "harry harrison"));
RELAY_ERROR(wp_entry_add_string(entry, "body", "hi harry i am going to vacation now bye"));
RELAY_ERROR(wp_index_add_entry(index, entry, &doc_id));
RELAY_ERROR(wp_entry_free(entry));
RUN_QUERY("subject:there");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("body:there");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("there");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("subject:hello");
ASSERT_EQUALS_UINT(2, num_results);
RUN_QUERY("body:hello");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("hello");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("subject:vacation body:harry");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("subject:vacation harry");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("subject:vacation -body:harry");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("subject:hello -body:harry");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("subject:hello body:\"i am going\"");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("-subject:hello body:\"i am going\"");
ASSERT_EQUALS_UINT(0, num_results);
return NO_ERROR;
}
TEST(utf8_chars) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
RELAY_ERROR(add_string(index, "hello 我 能 吞下 玻璃 而 不 傷 身體 。")); // pre-tokenized for your pleasure
RUN_QUERY("我");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("吞下");
ASSERT_EQUALS_UINT(1, num_results);
RUN_QUERY("\"不 傷 身體\"");
ASSERT_EQUALS_UINT(1, num_results);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}
TEST(every_selector) {
wp_index* index;
uint64_t results[10];
uint32_t num_results;
wp_query* query;
RELAY_ERROR(setup(&index));
RUN_QUERY("*");
ASSERT_EQUALS_UINT(3, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
ASSERT_EQUALS_UINT64(1, results[2]);
RUN_QUERY("-*");
ASSERT_EQUALS_UINT(0, num_results);
RUN_QUERY("* OR four");
ASSERT_EQUALS_UINT(3, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
ASSERT_EQUALS_UINT64(1, results[2]);
RUN_QUERY("* OR asdfasefs");
ASSERT_EQUALS_UINT(3, num_results);
ASSERT_EQUALS_UINT64(3, results[0]);
ASSERT_EQUALS_UINT64(2, results[1]);
ASSERT_EQUALS_UINT64(1, results[2]);
RELAY_ERROR(shutdown(index));
return NO_ERROR;
}