source: trunk/gsdl/src/mgpp/text/QueryTester.cpp@ 855

Last change on this file since 855 was 855, checked in by sjboddie, 24 years ago

Rodgers new C++ mg

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