|
1 /* |
|
2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 #include <wchar.h> |
|
19 #include <stddef.h> |
|
20 |
|
21 #include <iostream> |
|
22 |
|
23 #include "cpixfstools.h" |
|
24 |
|
25 #include "itk.h" |
|
26 |
|
27 #include "cpixidxdb.h" |
|
28 |
|
29 #include "config.h" |
|
30 #include "testutils.h" |
|
31 |
|
32 #include "testcorpus.h" |
|
33 #include "setupsentry.h" |
|
34 |
|
35 |
|
36 class PlainMVContext : public Itk::ITestContext |
|
37 { |
|
38 cpix_Analyzer * analyzer_; |
|
39 cpix_QueryParser * queryParser_; |
|
40 |
|
41 const MVFTest * mvfTests_; |
|
42 |
|
43 public: |
|
44 |
|
45 PlainMVContext(const MVFTest * mvfTests) |
|
46 : analyzer_(NULL), |
|
47 queryParser_(NULL), |
|
48 mvfTests_(mvfTests) |
|
49 { |
|
50 ; |
|
51 } |
|
52 |
|
53 |
|
54 virtual void setup() throw (Itk::PanicExc) |
|
55 { |
|
56 SetupSentry |
|
57 ss(*this); |
|
58 |
|
59 cpix_Result |
|
60 result; |
|
61 |
|
62 cpix_IdxDb_dbgScrapAll(&result); |
|
63 |
|
64 analyzer_ = cpix_CreateSimpleAnalyzer(&result); |
|
65 if (analyzer_ == NULL) |
|
66 { |
|
67 ITK_PANIC("Could not create analyzer"); |
|
68 } |
|
69 |
|
70 |
|
71 queryParser_ = cpix_QueryParser_create(&result, |
|
72 CONTENTS_FIELD, |
|
73 analyzer_); |
|
74 if (queryParser_ == NULL) |
|
75 { |
|
76 ITK_PANIC("Could not create query parser"); |
|
77 } |
|
78 |
|
79 ss.setupComplete(); |
|
80 } |
|
81 |
|
82 |
|
83 virtual void tearDown() throw () |
|
84 { |
|
85 cleanup(); |
|
86 } |
|
87 |
|
88 |
|
89 virtual ~PlainMVContext() |
|
90 { |
|
91 cleanup(); |
|
92 } |
|
93 |
|
94 |
|
95 void testAddFiles(Itk::TestMgr * testMgr) |
|
96 { |
|
97 const MVFTest |
|
98 * mvfTest = mvfTests_; |
|
99 |
|
100 for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest) |
|
101 { |
|
102 using namespace std; |
|
103 |
|
104 printf("Creating volume %s ...\n", |
|
105 mvfTest->qualifiedBaseAppClass_); |
|
106 |
|
107 { |
|
108 auto_ptr<VolumeFileIdxUtil> |
|
109 util(new VolumeFileIdxUtil(mvfTest)); |
|
110 util->init(); |
|
111 |
|
112 util->indexFile(mvfTest->textFilePath_, |
|
113 analyzer_, |
|
114 testMgr); |
|
115 } |
|
116 |
|
117 printf("... created volume %s\n", |
|
118 mvfTest->qualifiedBaseAppClass_); |
|
119 } |
|
120 } |
|
121 |
|
122 |
|
123 void testSearchAllFiles(Itk::TestMgr * testMgr) |
|
124 { |
|
125 cpix_Result |
|
126 result; |
|
127 |
|
128 cpix_IdxSearcher |
|
129 * searcher = cpix_IdxSearcher_openDb(&result, |
|
130 FILEAPPCLASS); |
|
131 |
|
132 ITK_ASSERT(testMgr, |
|
133 cpix_Succeeded(&result), |
|
134 "Failed to search multi volume"); |
|
135 |
|
136 cpix_Query |
|
137 * query = cpix_QueryParser_parse(queryParser_, |
|
138 L"happ*"); |
|
139 |
|
140 if (cpix_Failed(queryParser_)) |
|
141 { |
|
142 cpix_IdxSearcher_releaseDb(searcher); |
|
143 } |
|
144 |
|
145 ITK_ASSERT(testMgr, |
|
146 cpix_Succeeded(queryParser_), |
|
147 "Could not parse 'happ*'"); |
|
148 |
|
149 cpix_Hits |
|
150 * hits = cpix_IdxSearcher_search(searcher, |
|
151 query); |
|
152 |
|
153 if (cpix_Failed(searcher)) |
|
154 { |
|
155 cpix_IdxSearcher_releaseDb(searcher); |
|
156 cpix_Query_destroy(query); |
|
157 } |
|
158 |
|
159 ITK_ASSERT(testMgr, |
|
160 cpix_Succeeded(searcher), |
|
161 "Could not perform (multi)search"); |
|
162 |
|
163 PrintHits(hits, |
|
164 testMgr); |
|
165 |
|
166 cpix_Hits_destroy(hits); |
|
167 cpix_IdxSearcher_releaseDb(searcher); |
|
168 cpix_Query_destroy(query); |
|
169 } |
|
170 |
|
171 |
|
172 private: |
|
173 void cleanup() |
|
174 { |
|
175 cpix_Analyzer_destroy(analyzer_); |
|
176 analyzer_ = NULL; |
|
177 |
|
178 cpix_QueryParser_destroy(queryParser_); |
|
179 queryParser_ = NULL; |
|
180 } |
|
181 }; |
|
182 |
|
183 |
|
184 |
|
185 Itk::TesterBase * CreateMVPlainTests(const MVFTest * mvfTests) |
|
186 { |
|
187 using namespace Itk; |
|
188 |
|
189 PlainMVContext |
|
190 * context = new PlainMVContext(mvfTests); |
|
191 |
|
192 ContextTester |
|
193 * contextTester = new ContextTester("plain", |
|
194 context); |
|
195 |
|
196 #define TEST "adding" |
|
197 contextTester->add(TEST, |
|
198 context, |
|
199 &PlainMVContext::testAddFiles, |
|
200 TEST); |
|
201 #undef TEST |
|
202 |
|
203 #define TEST "searchingAll" |
|
204 contextTester->add(TEST, |
|
205 context, |
|
206 &PlainMVContext::testSearchAllFiles, |
|
207 TEST); |
|
208 #undef TEST |
|
209 |
|
210 // ... add more |
|
211 |
|
212 return contextTester; |
|
213 } |
|
214 |
|
215 |
|
216 |
|
217 class DynMVContext : public Itk::ITestContext |
|
218 { |
|
219 cpix_Analyzer * analyzer_; |
|
220 cpix_Query * query_; |
|
221 cpix_IdxSearcher * searcher_; |
|
222 cpix_QueryParser * queryParser_; |
|
223 |
|
224 const MVFTest * mvfTests_; |
|
225 |
|
226 public: |
|
227 |
|
228 DynMVContext(const MVFTest * mvfTests) |
|
229 : analyzer_(NULL), |
|
230 query_(NULL), |
|
231 searcher_(NULL), |
|
232 queryParser_(NULL), |
|
233 mvfTests_(mvfTests) |
|
234 { |
|
235 ; |
|
236 } |
|
237 |
|
238 |
|
239 virtual void setup() throw (Itk::PanicExc) |
|
240 { |
|
241 SetupSentry |
|
242 ss(*this); |
|
243 |
|
244 cpix_Result |
|
245 result; |
|
246 |
|
247 cpix_IdxDb_dbgScrapAll(&result); |
|
248 |
|
249 analyzer_ = cpix_CreateSimpleAnalyzer(&result); |
|
250 if (analyzer_ == NULL) |
|
251 { |
|
252 ITK_PANIC("Could not create analyzer"); |
|
253 } |
|
254 |
|
255 |
|
256 queryParser_ = cpix_QueryParser_create(&result, |
|
257 CONTENTS_FIELD, |
|
258 analyzer_); |
|
259 if (queryParser_ == NULL) |
|
260 { |
|
261 ITK_PANIC("Could not create query parser"); |
|
262 } |
|
263 |
|
264 query_ = cpix_QueryParser_parse(queryParser_, |
|
265 L"happ*"); |
|
266 if (cpix_Failed(queryParser_)) |
|
267 { |
|
268 ITK_PANIC("Could not create query parser"); |
|
269 } |
|
270 |
|
271 ss.setupComplete(); |
|
272 } |
|
273 |
|
274 |
|
275 virtual void tearDown() throw() |
|
276 { |
|
277 cleanup(); |
|
278 } |
|
279 |
|
280 |
|
281 virtual ~DynMVContext() |
|
282 { |
|
283 cleanup(); |
|
284 } |
|
285 |
|
286 |
|
287 void testCreatingVolumes(Itk::TestMgr * testMgr) |
|
288 { |
|
289 const MVFTest |
|
290 * mvfTest = mvfTests_; |
|
291 |
|
292 for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest) |
|
293 { |
|
294 using namespace std; |
|
295 |
|
296 printf("Creating volume %s ...\n", |
|
297 mvfTest->qualifiedBaseAppClass_); |
|
298 |
|
299 { |
|
300 auto_ptr<VolumeFileIdxUtil> |
|
301 util(new VolumeFileIdxUtil(mvfTest)); |
|
302 util->init(); |
|
303 |
|
304 util->indexFile(mvfTest->textFilePath_, |
|
305 analyzer_, |
|
306 testMgr); |
|
307 util->flush(); |
|
308 } |
|
309 |
|
310 printf("... created volume %s\n", |
|
311 mvfTest->qualifiedBaseAppClass_); |
|
312 |
|
313 // after the first volume has been defined, there is |
|
314 // something to search on |
|
315 createSearcherIfNecessary(testMgr); |
|
316 searchAll(testMgr); |
|
317 } |
|
318 } |
|
319 |
|
320 |
|
321 void testUnmountingVolumes(Itk::TestMgr * testMgr) |
|
322 { |
|
323 const MVFTest |
|
324 * mvfTest = mvfTests_; |
|
325 |
|
326 for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest) |
|
327 { |
|
328 using namespace std; |
|
329 |
|
330 searchAll(testMgr); |
|
331 |
|
332 printf("Undefining (unmounting) volume %s ...\n", |
|
333 mvfTest->qualifiedBaseAppClass_); |
|
334 |
|
335 cpix_IdxDb_undefineVolume(mvfTest->qualifiedBaseAppClass_); |
|
336 |
|
337 printf("... undefined (unmounted) volume %s.\n", |
|
338 mvfTest->qualifiedBaseAppClass_); |
|
339 } |
|
340 |
|
341 searchAll(testMgr); |
|
342 } |
|
343 |
|
344 |
|
345 void testMountingVolumes(Itk::TestMgr * testMgr) |
|
346 { |
|
347 const MVFTest |
|
348 * mvfTest = mvfTests_; |
|
349 |
|
350 for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest) |
|
351 { |
|
352 using namespace std; |
|
353 |
|
354 searchAll(testMgr); |
|
355 |
|
356 printf("Defining (mounting) volume %s ...\n", |
|
357 mvfTest->qualifiedBaseAppClass_); |
|
358 |
|
359 cpix_Result |
|
360 result; |
|
361 |
|
362 cpix_IdxDb_defineVolume(&result, |
|
363 mvfTest->qualifiedBaseAppClass_, |
|
364 mvfTest->idxDbPath_); |
|
365 |
|
366 ITK_ASSERT(testMgr, |
|
367 cpix_Succeeded(&result), |
|
368 "Could not define (mount) volume %s", |
|
369 mvfTest->qualifiedBaseAppClass_); |
|
370 |
|
371 printf("... defined (mounted) volume %s.\n", |
|
372 mvfTest->qualifiedBaseAppClass_); |
|
373 } |
|
374 |
|
375 searchAll(testMgr); |
|
376 } |
|
377 |
|
378 |
|
379 private: |
|
380 |
|
381 void createSearcherIfNecessary(Itk::TestMgr * testMgr) |
|
382 { |
|
383 if (searcher_ == NULL) |
|
384 { |
|
385 cpix_Result |
|
386 result; |
|
387 searcher_ = cpix_IdxSearcher_openDb(&result, |
|
388 FILEAPPCLASS); |
|
389 |
|
390 ITK_ASSERT(testMgr, |
|
391 cpix_Succeeded(&result), |
|
392 "Could not create idx searcher"); |
|
393 } |
|
394 } |
|
395 |
|
396 |
|
397 void searchAll(Itk::TestMgr * testMgr) |
|
398 { |
|
399 printf("\nAnd now searching all 'root file'!\n"); |
|
400 |
|
401 cpix_Hits |
|
402 * hits = cpix_IdxSearcher_search(searcher_, |
|
403 query_); |
|
404 |
|
405 if (cpix_Failed(searcher_)) |
|
406 { |
|
407 cpix_IdxSearcher_releaseDb(searcher_); |
|
408 } |
|
409 |
|
410 ITK_ASSERT(testMgr, |
|
411 cpix_Succeeded(searcher_), |
|
412 "Could not perform (multi)search"); |
|
413 |
|
414 PrintHits(hits, |
|
415 testMgr); |
|
416 |
|
417 cpix_Hits_destroy(hits); |
|
418 } |
|
419 |
|
420 |
|
421 void cleanup() |
|
422 { |
|
423 cpix_Analyzer_destroy(analyzer_); |
|
424 analyzer_ = NULL; |
|
425 |
|
426 cpix_Query_destroy(query_); |
|
427 query_ = NULL; |
|
428 |
|
429 cpix_IdxSearcher_releaseDb(searcher_); |
|
430 searcher_ = NULL; |
|
431 |
|
432 cpix_QueryParser_destroy(queryParser_); |
|
433 queryParser_ = NULL; |
|
434 } |
|
435 }; |
|
436 |
|
437 |
|
438 |
|
439 Itk::TesterBase * CreateMVDynTests(const MVFTest * mvfTests) |
|
440 { |
|
441 using namespace Itk; |
|
442 |
|
443 DynMVContext |
|
444 * context = new DynMVContext(mvfTests); |
|
445 |
|
446 ContextTester |
|
447 * contextTester = new ContextTester("dyn", |
|
448 context); |
|
449 |
|
450 #define TEST "creatingVols" |
|
451 contextTester->add(TEST, |
|
452 context, |
|
453 &DynMVContext::testCreatingVolumes, |
|
454 TEST); |
|
455 #undef TEST |
|
456 |
|
457 #define TEST "unmountingVols" |
|
458 contextTester->add(TEST, |
|
459 context, |
|
460 &DynMVContext::testUnmountingVolumes, |
|
461 TEST); |
|
462 #undef TEST |
|
463 |
|
464 #define TEST "mountingVols" |
|
465 contextTester->add(TEST, |
|
466 context, |
|
467 &DynMVContext::testMountingVolumes, |
|
468 TEST); |
|
469 #undef TEST |
|
470 |
|
471 // ... add more |
|
472 |
|
473 return contextTester; |
|
474 } |
|
475 |
|
476 |
|
477 struct MVFTest MVFTests[] = { |
|
478 |
|
479 { |
|
480 FILE_TEST_CORPUS_PATH "\\en\\1.txt", |
|
481 "@c:root file", |
|
482 CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\c", |
|
483 }, |
|
484 |
|
485 { |
|
486 FILE_TEST_CORPUS_PATH "\\en\\2.txt", |
|
487 "@d:root file", |
|
488 CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\d", |
|
489 }, |
|
490 |
|
491 { |
|
492 FILE_TEST_CORPUS_PATH "\\en\\3.txt", |
|
493 "@e:root file", |
|
494 CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\e", |
|
495 }, |
|
496 |
|
497 { |
|
498 FILE_TEST_CORPUS_PATH "\\en\\4.txt", |
|
499 "@f:root file", |
|
500 CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\f", |
|
501 }, |
|
502 |
|
503 { |
|
504 NULL, |
|
505 NULL, |
|
506 NULL |
|
507 }, |
|
508 }; |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 Itk::TesterBase * CreateMultiVolumeTests() |
|
514 { |
|
515 using namespace Itk; |
|
516 |
|
517 SuiteTester |
|
518 * multiVolume = new SuiteTester("multivolume"); |
|
519 |
|
520 multiVolume->add(CreateMVPlainTests(MVFTests)); |
|
521 multiVolume->add(CreateMVDynTests(MVFTests)); |
|
522 |
|
523 // TODO add more tests to suite |
|
524 |
|
525 return multiVolume; |
|
526 } |
|
527 |
|
528 |
|
529 |