source: trunk/indexers/mgpp/text/QueryTester.cpp@ 8692

Last change on this file since 8692 was 8692, checked in by kjdon, 19 years ago

Added the changes from Emanuel Dejanu (Simple Words) - mostly efficiency changes. For example, changing i++ to ++i, delete xxx to delete []xxx, some stuff to do with UCArrays...

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.7 KB
Line 
1/**************************************************************************
2 *
3 * QueryTester.cpp --
4 * Copyright (C) 1999 Rodger McNab
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 **************************************************************************/
21
22#include "MGQuery.h"
23
24
25class SetQueryNode : public QueryNode {
26public:
27 QueryResult queryResult;
28
29 SetQueryNode () {}
30 ~SetQueryNode () {}
31
32 void Calculate (IndexData &/*indexData*/, const QueryInfo &queryInfo,
33 QueryResult &result) const {
34 result = queryResult;
35 if (!queryInfo.sortByRank && !queryInfo.needRankInfo)
36 result.ranks.erase (result.ranks.begin(), result.ranks.end());
37 if (!queryInfo.needTermFreqs)
38 result.termFreqs.erase (result.termFreqs.begin(),
39 result.termFreqs.end());
40 }
41 void Free () { queryResult.Clear(); }
42};
43
44
45bool Test1 () {
46 AndQueryNode andNode;
47
48 SetQueryNode *setNode1 = new SetQueryNode;
49 DocNumArray &docSet1 = setNode1->queryResult.docs;
50 RankArray &rankSet1 = setNode1->queryResult.ranks;
51 docSet1.push_back (1); rankSet1.push_back (0.1f);
52 docSet1.push_back (10); rankSet1.push_back (0.2f);
53 docSet1.push_back (15); rankSet1.push_back (0.2f);
54 docSet1.push_back (18); rankSet1.push_back (0.4f);
55 docSet1.push_back (19); rankSet1.push_back (0.5f);
56
57 SetQueryNode *setNode2 = new SetQueryNode;
58 DocNumArray &docSet2 = setNode2->queryResult.docs;
59 RankArray &rankSet2 = setNode2->queryResult.ranks;
60 docSet2.push_back (2); rankSet2.push_back (0.1f);
61 docSet2.push_back (11); rankSet2.push_back (0.2f);
62 docSet2.push_back (12); rankSet2.push_back (0.3f);
63 docSet2.push_back (13); rankSet2.push_back (0.4f);
64 docSet2.push_back (14); rankSet2.push_back (0.5f);
65 docSet2.push_back (15); rankSet2.push_back (0.6f);
66 docSet2.push_back (16); rankSet2.push_back (0.7f);
67 docSet2.push_back (17); rankSet2.push_back (0.8f);
68 docSet2.push_back (19); rankSet2.push_back (0.9f);
69 docSet2.push_back (20); rankSet2.push_back (0.1f);
70 docSet2.push_back (21); rankSet2.push_back (0.2f);
71
72 cout << "\n" << setNode1->queryResult << "AND\n\n"
73 << setNode2->queryResult;
74
75
76 andNode.leftNode = setNode1;
77 andNode.rightNode = setNode2;
78
79 IndexData indexData; // this will not be used anywhere
80 QueryInfo queryInfo;
81 queryInfo.maxDocs = 0;
82 queryInfo.sortByRank = true;
83 queryInfo.needRankInfo = true;
84 queryInfo.needTermFreqs = true;
85
86 QueryResult result;
87
88 andNode.Calculate(indexData, queryInfo, result);
89
90 cout << "EQUALS\n\n" << result << "\n";
91
92 QueryResult resultCompare;
93 DocNumArray &rcDocSet = resultCompare.docs;
94 RankArray &rcRankSet = resultCompare.ranks;
95 rcDocSet.push_back (15); rcRankSet.push_back (0.2+0.6);
96 rcDocSet.push_back (19); rcRankSet.push_back (1.4);
97
98 if (!(result == resultCompare)) {
99 cout << "Test failed!!!\n";
100 cout << resultCompare;
101 return false;
102 }
103
104 return true;
105}
106
107bool Test1b () {
108 AndQueryNode andNode;
109
110 SetQueryNode *setNode1 = new SetQueryNode;
111 DocNumArray &docSet1 = setNode1->queryResult.docs;
112 docSet1.push_back (1);
113 docSet1.push_back (10);
114 docSet1.push_back (15);
115 docSet1.push_back (18);
116 docSet1.push_back (19);
117
118 SetQueryNode *setNode2 = new SetQueryNode;
119 DocNumArray &docSet2 = setNode2->queryResult.docs;
120 docSet2.push_back (2);
121 docSet2.push_back (11);
122 docSet2.push_back (12);
123 docSet2.push_back (13);
124 docSet2.push_back (14);
125 docSet2.push_back (15);
126 docSet2.push_back (16);
127 docSet2.push_back (17);
128 docSet2.push_back (19);
129 docSet2.push_back (20);
130 docSet2.push_back (21);
131
132 cout << "\n" << setNode1->queryResult << "AND\n\n"
133 << setNode2->queryResult;
134
135
136 andNode.leftNode = setNode1;
137 andNode.rightNode = setNode2;
138
139 IndexData indexData; // this will not be used anywhere
140 QueryInfo queryInfo;
141 queryInfo.maxDocs = 0;
142 queryInfo.sortByRank = false;
143 queryInfo.needRankInfo = false;
144 queryInfo.needTermFreqs = true;
145
146 QueryResult result;
147
148 andNode.Calculate(indexData, queryInfo, result);
149
150 cout << "EQUALS\n\n" << result << "\n";
151
152 QueryResult resultCompare;
153 DocNumArray &rcDocSet = resultCompare.docs;
154 rcDocSet.push_back (15);
155 rcDocSet.push_back (19);
156
157 if (!(result == resultCompare)) {
158 cout << "Test failed!!!\n";
159 cout << resultCompare;
160 return false;
161 }
162
163 return true;
164}
165
166bool Test2 () {
167 OrQueryNode orNode;
168
169 SetQueryNode *setNode1 = new SetQueryNode;
170 DocNumArray &docSet1 = setNode1->queryResult.docs;
171 RankArray &rankSet1 = setNode1->queryResult.ranks;
172 docSet1.push_back (1); rankSet1.push_back (0.1f);
173 docSet1.push_back (10); rankSet1.push_back (0.2f);
174 docSet1.push_back (15); rankSet1.push_back (0.2f);
175 docSet1.push_back (18); rankSet1.push_back (0.4f);
176 docSet1.push_back (19); rankSet1.push_back (0.5f);
177
178 SetQueryNode *setNode2 = new SetQueryNode;
179 DocNumArray &docSet2 = setNode2->queryResult.docs;
180 RankArray &rankSet2 = setNode2->queryResult.ranks;
181 docSet2.push_back (2); rankSet2.push_back (0.1f);
182 docSet2.push_back (11); rankSet2.push_back (0.2f);
183 docSet2.push_back (12); rankSet2.push_back (0.3f);
184 docSet2.push_back (13); rankSet2.push_back (0.4f);
185 docSet2.push_back (14); rankSet2.push_back (0.5f);
186 docSet2.push_back (15); rankSet2.push_back (0.6f);
187 docSet2.push_back (16); rankSet2.push_back (0.7f);
188 docSet2.push_back (17); rankSet2.push_back (0.8f);
189 docSet2.push_back (19); rankSet2.push_back (0.9f);
190 docSet2.push_back (20); rankSet2.push_back (0.1f);
191 docSet2.push_back (21); rankSet2.push_back (0.2f);
192
193 cout << "\n" << setNode1->queryResult << "OR\n\n"
194 << setNode2->queryResult;
195
196
197 orNode.leftNode = setNode1;
198 orNode.rightNode = setNode2;
199
200 IndexData indexData; // this will not be used anywhere
201 QueryInfo queryInfo;
202 queryInfo.maxDocs = 0;
203 queryInfo.sortByRank = true;
204 queryInfo.needRankInfo = true;
205 queryInfo.needTermFreqs = true;
206
207 QueryResult result;
208
209 orNode.Calculate(indexData, queryInfo, result);
210
211 cout << "EQUALS\n\n" << result << "\n";
212
213 QueryResult resultCompare;
214 DocNumArray &rcDocSet = resultCompare.docs;
215 RankArray &rcRankSet = resultCompare.ranks;
216 rcDocSet.push_back (1); rcRankSet.push_back (0.1f);
217 rcDocSet.push_back (2); rcRankSet.push_back (0.1f);
218 rcDocSet.push_back (10); rcRankSet.push_back (0.2f);
219 rcDocSet.push_back (11); rcRankSet.push_back (0.2f);
220 rcDocSet.push_back (12); rcRankSet.push_back (0.3f);
221 rcDocSet.push_back (13); rcRankSet.push_back (0.4f);
222 rcDocSet.push_back (14); rcRankSet.push_back (0.5f);
223 rcDocSet.push_back (15); rcRankSet.push_back (0.2f+0.6f);
224 rcDocSet.push_back (16); rcRankSet.push_back (0.7f);
225 rcDocSet.push_back (17); rcRankSet.push_back (0.8f);
226 rcDocSet.push_back (18); rcRankSet.push_back (0.4f);
227 rcDocSet.push_back (19); rcRankSet.push_back (0.9f+0.5f);
228 rcDocSet.push_back (20); rcRankSet.push_back (0.1f);
229 rcDocSet.push_back (21); rcRankSet.push_back (0.2f);
230
231
232 if (!(result == resultCompare)) {
233 cout << "Test failed!!!\n";
234 cout << resultCompare;
235 return false;
236 }
237
238 return true;
239}
240
241bool Test2b () {
242 OrQueryNode orNode;
243
244 SetQueryNode *setNode1 = new SetQueryNode;
245 DocNumArray &docSet1 = setNode1->queryResult.docs;
246 docSet1.push_back (1);
247 docSet1.push_back (10);
248 docSet1.push_back (15);
249 docSet1.push_back (18);
250 docSet1.push_back (19);
251
252 SetQueryNode *setNode2 = new SetQueryNode;
253 DocNumArray &docSet2 = setNode2->queryResult.docs;
254 docSet2.push_back (2);
255 docSet2.push_back (11);
256 docSet2.push_back (12);
257 docSet2.push_back (13);
258 docSet2.push_back (14);
259 docSet2.push_back (15);
260 docSet2.push_back (16);
261 docSet2.push_back (17);
262 docSet2.push_back (19);
263 docSet2.push_back (20);
264 docSet2.push_back (21);
265
266 cout << "\n" << setNode1->queryResult << "OR\n\n"
267 << setNode2->queryResult;
268
269
270 orNode.leftNode = setNode1;
271 orNode.rightNode = setNode2;
272
273 IndexData indexData; // this will not be used anywhere
274 QueryInfo queryInfo;
275 queryInfo.maxDocs = 0;
276 queryInfo.sortByRank = false;
277 queryInfo.needRankInfo = false;
278 queryInfo.needTermFreqs = true;
279
280 QueryResult result;
281
282 orNode.Calculate(indexData, queryInfo, result);
283
284 cout << "EQUALS\n\n" << result << "\n";
285
286 QueryResult resultCompare;
287 DocNumArray &rcDocSet = resultCompare.docs;
288 rcDocSet.push_back (1);
289 rcDocSet.push_back (2);
290 rcDocSet.push_back (10);
291 rcDocSet.push_back (11);
292 rcDocSet.push_back (12);
293 rcDocSet.push_back (13);
294 rcDocSet.push_back (14);
295 rcDocSet.push_back (15);
296 rcDocSet.push_back (16);
297 rcDocSet.push_back (17);
298 rcDocSet.push_back (18);
299 rcDocSet.push_back (19);
300 rcDocSet.push_back (20);
301 rcDocSet.push_back (21);
302
303
304 if (!(result == resultCompare)) {
305 cout << "Test failed!!!\n";
306 cout << resultCompare;
307 return false;
308 }
309
310 return true;
311}
312
313bool Test3 () {
314 NotQueryNode notNode;
315
316 SetQueryNode *setNode1 = new SetQueryNode;
317 DocNumArray &docSet1 = setNode1->queryResult.docs;
318 RankArray &rankSet1 = setNode1->queryResult.ranks;
319 docSet1.push_back (1); rankSet1.push_back (0.1f);
320 docSet1.push_back (10); rankSet1.push_back (0.2f);
321 docSet1.push_back (15); rankSet1.push_back (0.2f);
322 docSet1.push_back (18); rankSet1.push_back (0.4f);
323 docSet1.push_back (19); rankSet1.push_back (0.5f);
324
325 SetQueryNode *setNode2 = new SetQueryNode;
326 DocNumArray &docSet2 = setNode2->queryResult.docs;
327 RankArray &rankSet2 = setNode2->queryResult.ranks;
328 docSet2.push_back (2); rankSet2.push_back (0.1f);
329 docSet2.push_back (11); rankSet2.push_back (0.2f);
330 docSet2.push_back (12); rankSet2.push_back (0.3f);
331 docSet2.push_back (13); rankSet2.push_back (0.4f);
332 docSet2.push_back (14); rankSet2.push_back (0.5f);
333 docSet2.push_back (15); rankSet2.push_back (0.6f);
334 docSet2.push_back (16); rankSet2.push_back (0.7f);
335 docSet2.push_back (17); rankSet2.push_back (0.8f);
336 docSet2.push_back (19); rankSet2.push_back (0.9f);
337 docSet2.push_back (20); rankSet2.push_back (0.1f);
338 docSet2.push_back (21); rankSet2.push_back (0.2f);
339
340 cout << "\n" << setNode1->queryResult << "NOT\n\n"
341 << setNode2->queryResult;
342
343
344 notNode.queryNode = setNode1;
345 notNode.notNode = setNode2;
346
347 IndexData indexData; // this will not be used anywhere
348 QueryInfo queryInfo;
349 queryInfo.maxDocs = 0;
350 queryInfo.sortByRank = true;
351 queryInfo.needRankInfo = true;
352 queryInfo.needTermFreqs = true;
353
354 QueryResult result;
355
356 notNode.Calculate(indexData, queryInfo, result);
357
358 cout << "EQUALS\n\n" << result << "\n";
359
360 QueryResult resultCompare;
361 DocNumArray &rcDocSet = resultCompare.docs;
362 RankArray &rcRankSet = resultCompare.ranks;
363 rcDocSet.push_back (1); rcRankSet.push_back (0.1);
364 rcDocSet.push_back (10); rcRankSet.push_back (0.2);
365 rcDocSet.push_back (18); rcRankSet.push_back (0.4);
366
367
368 if (!(result == resultCompare)) {
369 cout << "Test failed!!!\n";
370 cout << resultCompare;
371 return false;
372 }
373
374 return true;
375}
376
377bool Test3b () {
378 NotQueryNode notNode;
379
380 SetQueryNode *setNode1 = new SetQueryNode;
381 DocNumArray &docSet1 = setNode1->queryResult.docs;
382 docSet1.push_back (1);
383 docSet1.push_back (10);
384 docSet1.push_back (15);
385 docSet1.push_back (18);
386 docSet1.push_back (19);
387
388 SetQueryNode *setNode2 = new SetQueryNode;
389 DocNumArray &docSet2 = setNode2->queryResult.docs;
390 docSet2.push_back (2);
391 docSet2.push_back (11);
392 docSet2.push_back (12);
393 docSet2.push_back (13);
394 docSet2.push_back (14);
395 docSet2.push_back (15);
396 docSet2.push_back (16);
397 docSet2.push_back (17);
398 docSet2.push_back (19);
399 docSet2.push_back (20);
400 docSet2.push_back (21);
401
402 cout << "\n" << setNode1->queryResult << "NOT\n\n"
403 << setNode2->queryResult;
404
405
406 notNode.queryNode = setNode1;
407 notNode.notNode = setNode2;
408
409 IndexData indexData; // this will not be used anywhere
410 QueryInfo queryInfo;
411 queryInfo.maxDocs = 0;
412 queryInfo.sortByRank = false;
413 queryInfo.needRankInfo = false;
414 queryInfo.needTermFreqs = true;
415
416 QueryResult result;
417
418 notNode.Calculate(indexData, queryInfo, result);
419
420 cout << "EQUALS\n\n" << result << "\n";
421
422 QueryResult resultCompare;
423 DocNumArray &rcDocSet = resultCompare.docs;
424 rcDocSet.push_back (1);
425 rcDocSet.push_back (10);
426 rcDocSet.push_back (18);
427
428
429 if (!(result == resultCompare)) {
430 cout << "Test failed!!!\n";
431 cout << resultCompare;
432 return false;
433 }
434
435 return true;
436}
437
438
439int main () {
440 if (!Test1 () ||
441 !Test2 () ||
442 !Test2b () ||
443 !Test3 () ||
444 !Test3b ()) return 1;
445
446 return 0;
447}
Note: See TracBrowser for help on using the repository browser.