searchengine/cpix/tsrc/cpixunittest/src/domainselectiontests.cpp
changeset 0 671dee74050a
child 3 ae3f1779f6da
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     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 <memory>
       
    22 #include <iostream>
       
    23 #include <list>
       
    24 #include <map>
       
    25 #include <vector>
       
    26 
       
    27 #include "cpixfstools.h"
       
    28 
       
    29 #include "itk.h"
       
    30 
       
    31 #include "cpixidxdb.h"
       
    32 #include "cpixsearch.h"
       
    33 #include "idxdbmgr.h" // from private folder: whitebox testing
       
    34 
       
    35 #include "config.h"
       
    36 #include "suggestion.h"
       
    37 #include "testutils.h"
       
    38 #include "testcorpus.h"
       
    39 #include "setupsentry.h"
       
    40 
       
    41 
       
    42 struct MatchTest
       
    43 {
       
    44     const char * domainSelector_;
       
    45     const char * baseAppClass_;
       
    46 };
       
    47 
       
    48 
       
    49 
       
    50 void testDomainSelection(Itk::TestMgr    * testMgr,
       
    51                          const MatchTest * matchTest,
       
    52                          bool              shouldMatch)
       
    53 {
       
    54     for (; matchTest->domainSelector_ != NULL; ++matchTest)
       
    55         {
       
    56             bool
       
    57                 matched =  Cpix::IdxDbMgr::match(matchTest->domainSelector_,
       
    58                                                  matchTest->baseAppClass_);
       
    59             ITK_EXPECT(testMgr,
       
    60                        matched == shouldMatch,
       
    61                        "Domain selector '%s' and baseappclass: '%s' "
       
    62                        "should %s match",
       
    63                        matchTest->domainSelector_,
       
    64                        matchTest->baseAppClass_,
       
    65                        (shouldMatch ? "" : "NOT"));
       
    66         }
       
    67 }
       
    68 
       
    69 
       
    70 
       
    71 const MatchTest PositiveMatchTests[] = {
       
    72     // multiple volume cases
       
    73     { "root file",                       "@c:root file" },
       
    74     { "root file",                       "@d:root file" },
       
    75     { "@c:root file",                    "@c:root file" },
       
    76     { "@d:root file",                    "@d:root file" },
       
    77 
       
    78     // generic/widening search cases
       
    79     { "root",                            "@c:root file" },
       
    80     { "root",                            "@d:root file" },
       
    81     { "root",                            "@0:root msg phone sms" },
       
    82     { "root msg",                        "@0:root msg phone sms" },
       
    83     { "@c:root",                         "@c:root file" },
       
    84     { "@d:root",                         "@d:root file" },
       
    85 
       
    86     // selecting a list of volumes to use
       
    87     { "@FIN:root maps,@GBR:root maps",   "@FIN:root maps" },
       
    88     { "@FIN:root maps,@GBR:root maps",   "@GBR:root maps" },
       
    89     { "root file,root msg phone sms",    "@c:root file" },
       
    90     { "root file,root msg phone sms",    "@d:root file" },
       
    91     { "root file,root msg phone sms",    "@0:root msg phone sms" },
       
    92     { "@c:root file,root msg phone sms", "@c:root file" },
       
    93     { "@c:root file,root msg phone sms", "@0:root msg phone sms" },
       
    94 
       
    95     // end
       
    96     { NULL,                              NULL }
       
    97 };
       
    98 
       
    99 
       
   100 void testPositiveDomainSelection(Itk::TestMgr * testMgr)
       
   101 {
       
   102     testDomainSelection(testMgr,
       
   103                         PositiveMatchTests,
       
   104                         true); // should match
       
   105 }
       
   106 
       
   107 
       
   108 const MatchTest NegativeMatchTests[] = {
       
   109     { "root file",                       "@0:root msg phone sms" },
       
   110     { "@c:root file",                    "@0:root msg phone sms" },
       
   111     { "root file",                       "@FIN:root maps" },
       
   112     { "@c:root file",                    "@FIN:root maps" },
       
   113 
       
   114     // end
       
   115     { NULL,                              NULL }
       
   116 };
       
   117 
       
   118 
       
   119 void testNegativeDomainSelection(Itk::TestMgr * testMgr)
       
   120 {
       
   121     testDomainSelection(testMgr,
       
   122                         NegativeMatchTests,
       
   123                         false); // should NOT match
       
   124 }
       
   125 
       
   126 
       
   127 
       
   128 class DomainSelectionContext : public Itk::ITestContext
       
   129 {
       
   130     cpix_Analyzer              * analyzer_;
       
   131     cpix_Query                 * query_;
       
   132     cpix_Query                 * termsQuery_;
       
   133     cpix_Query                 * dumpQuery_;
       
   134 
       
   135     cpix_QueryParser           * queryParser_;
       
   136 
       
   137     std::list<cpix_IdxSearcher*> searchers_;
       
   138 
       
   139     static const char          * domainSpecifiers_[];
       
   140     static const MVFTest         filesAndVols_[];
       
   141 
       
   142 public:
       
   143     virtual void setup() throw (Itk::PanicExc)
       
   144     {
       
   145         SetupSentry
       
   146             ss(*this);
       
   147 
       
   148         cpix_Result
       
   149             result;
       
   150 
       
   151         cpix_IdxDb_dbgScrapAll(&result);
       
   152         if (cpix_Failed(&result))
       
   153             {
       
   154                 cpix_LogLevel
       
   155                     ll = cpix_setLogLevel(CPIX_LL_TRACE);
       
   156                 cpix_dbgDumpState();
       
   157                 cpix_setLogLevel(ll);
       
   158                 ITK_PANIC("Could not dbg scrapp all indexes");
       
   159             }
       
   160 
       
   161         analyzer_ = cpix_CreateSimpleAnalyzer(&result);
       
   162         if (analyzer_ == NULL)
       
   163             {
       
   164                 ITK_PANIC("Could not create analyzer");
       
   165             }
       
   166 
       
   167 
       
   168         queryParser_ = cpix_QueryParser_create(&result,
       
   169                                                LCPIX_DEFAULT_FIELD,
       
   170                                                analyzer_);
       
   171         if (queryParser_ == NULL)
       
   172             {
       
   173                 ITK_PANIC("Could not create query parser");
       
   174             }
       
   175 
       
   176         query_ = cpix_QueryParser_parse(queryParser_,
       
   177                                         L"happ*");
       
   178         if (cpix_Failed(queryParser_))
       
   179             {
       
   180                 ITK_PANIC("Could not create query parser");
       
   181             }
       
   182 
       
   183         // terms (suggestions) query using unified search API
       
   184         termsQuery_ = cpix_QueryParser_parse(queryParser_,
       
   185                                              L"$terms<50>(ha*)");
       
   186 
       
   187         if (cpix_Failed(queryParser_)
       
   188             || termsQuery_ == NULL)
       
   189             {
       
   190                 ITK_PANIC("Could not parse terms query string");
       
   191             }
       
   192 
       
   193         dumpQuery_ = cpix_QueryParser_parse(queryParser_,
       
   194                                             L"*");
       
   195 
       
   196         if (cpix_Failed(queryParser_))
       
   197             {
       
   198                 ITK_PANIC("Could not parse terms query string");
       
   199             }
       
   200 
       
   201         ss.setupComplete();
       
   202     }
       
   203 
       
   204 
       
   205     virtual void tearDown() throw()
       
   206     {
       
   207         cleanup();
       
   208     }
       
   209 
       
   210 
       
   211     DomainSelectionContext()
       
   212         : analyzer_(NULL),
       
   213           query_(NULL),
       
   214           termsQuery_(NULL),
       
   215           dumpQuery_(NULL),
       
   216           queryParser_(NULL)
       
   217     {
       
   218         ;
       
   219     }
       
   220 
       
   221 
       
   222     virtual ~DomainSelectionContext()
       
   223     {
       
   224         cleanup();
       
   225     }
       
   226 
       
   227 
       
   228     void testCreateIdxs(Itk::TestMgr * testMgr)
       
   229     {
       
   230         const MVFTest
       
   231             * mvfTest = filesAndVols_;
       
   232 
       
   233         for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest)
       
   234             {
       
   235                 using namespace std;
       
   236 
       
   237                 printf("Creating volume %s ...\n",
       
   238                        mvfTest->qualifiedBaseAppClass_);
       
   239 
       
   240                 {
       
   241                     auto_ptr<VolumeFileIdxUtil>
       
   242                         vfiUtil(new VolumeFileIdxUtil(mvfTest));
       
   243                     vfiUtil->init();
       
   244 
       
   245                     vfiUtil->indexFile(mvfTest->textFilePath_,
       
   246                                        analyzer_,
       
   247                                        testMgr);
       
   248                 }
       
   249 
       
   250                 printf("... created volume %s\n",
       
   251                        mvfTest->qualifiedBaseAppClass_);
       
   252             }
       
   253 
       
   254         printf("Indexing some SMS-es\n");
       
   255 
       
   256         LineTestCorpusRef
       
   257             corpus(DEFAULT_TEST_CORPUS_PATH);
       
   258         std::auto_ptr<SmsIdxUtil>
       
   259             siUtil(new SmsIdxUtil);
       
   260         siUtil->init();
       
   261         
       
   262 
       
   263         for (size_t i = 0; i < 200; ++i)
       
   264             {
       
   265                 std::wstring
       
   266                     body = corpus.item(i);
       
   267                 siUtil->indexSms(i,
       
   268                                body.c_str(),
       
   269                                analyzer_,
       
   270                                testMgr);
       
   271                 if ((i % 5) == 0)
       
   272                     {
       
   273                         ITK_DBGMSG(testMgr,
       
   274                                    ".");
       
   275                     }
       
   276             }
       
   277         siUtil->flush();
       
   278 
       
   279         searchAll(testMgr);
       
   280         suggestAll(testMgr);
       
   281         dumpAll(testMgr);
       
   282     }
       
   283 
       
   284     
       
   285     void unmountC(Itk::TestMgr * testMgr)
       
   286     {
       
   287         unmount(filesAndVols_[0].qualifiedBaseAppClass_);
       
   288         
       
   289         searchAll(testMgr);
       
   290         suggestAll(testMgr);
       
   291     }
       
   292 
       
   293 
       
   294     void unmountE(Itk::TestMgr * testMgr)
       
   295     {
       
   296         unmount(filesAndVols_[2].qualifiedBaseAppClass_);
       
   297 
       
   298         searchAll(testMgr);
       
   299         suggestAll(testMgr);
       
   300     }
       
   301 
       
   302 
       
   303     void unmountD(Itk::TestMgr * testMgr)
       
   304     {
       
   305         unmount(filesAndVols_[1].qualifiedBaseAppClass_);
       
   306 
       
   307         searchAll(testMgr);
       
   308         suggestAll(testMgr);
       
   309     }
       
   310 
       
   311 
       
   312     void mountC(Itk::TestMgr * testMgr)
       
   313     {
       
   314         mount(testMgr,
       
   315               filesAndVols_[0].qualifiedBaseAppClass_,
       
   316               filesAndVols_[0].idxDbPath_);
       
   317 
       
   318         searchAll(testMgr);
       
   319         suggestAll(testMgr);
       
   320     }
       
   321 
       
   322     void mountE(Itk::TestMgr * testMgr)
       
   323     {
       
   324         mount(testMgr,
       
   325               filesAndVols_[2].qualifiedBaseAppClass_,
       
   326               filesAndVols_[2].idxDbPath_);
       
   327 
       
   328         searchAll(testMgr);
       
   329         suggestAll(testMgr);
       
   330     }
       
   331                
       
   332 
       
   333     void mountD(Itk::TestMgr * testMgr)
       
   334     {
       
   335         mount(testMgr,
       
   336               filesAndVols_[1].qualifiedBaseAppClass_,
       
   337               filesAndVols_[1].idxDbPath_);
       
   338 
       
   339         searchAll(testMgr);
       
   340         suggestAll(testMgr);
       
   341     }
       
   342 
       
   343 
       
   344 private:
       
   345 
       
   346     
       
   347     void unmount(const char   * baseAppClass)
       
   348     {
       
   349         printf("Unmounting volume %s ...\n",
       
   350                baseAppClass);
       
   351         cpix_IdxDb_undefineVolume(baseAppClass);
       
   352         printf("... unmounted volume %s.\n",
       
   353                baseAppClass);
       
   354     }
       
   355 
       
   356 
       
   357     void mount(Itk::TestMgr * testMgr,
       
   358                const char   * baseAppClass,
       
   359                const char   * idxDbPath)
       
   360     {
       
   361         printf("mounting volume %s/%s ...\n",
       
   362                baseAppClass,
       
   363                idxDbPath);
       
   364         cpix_Result
       
   365             result;
       
   366         cpix_IdxDb_defineVolume(&result,
       
   367                                 baseAppClass,
       
   368                                 idxDbPath);
       
   369         ITK_ASSERT(testMgr,
       
   370                    cpix_Succeeded(&result),
       
   371                    "Failed to mount volume %s/%s",
       
   372                    baseAppClass,
       
   373                    idxDbPath);
       
   374         printf("... mounted volume %s/%s.\n",
       
   375                baseAppClass,
       
   376                idxDbPath);
       
   377     }
       
   378 
       
   379 
       
   380     void createSearchersIfNecessary(Itk::TestMgr * testMgr)
       
   381     {
       
   382         if (searchers_.size() > 0)
       
   383             {
       
   384                 return;
       
   385             }
       
   386 
       
   387         const char
       
   388             * * domainSpecifierPtr = domainSpecifiers_;
       
   389         for (; *domainSpecifierPtr != NULL; ++domainSpecifierPtr)
       
   390             {
       
   391                 cpix_Result
       
   392                     result;
       
   393 
       
   394                 cpix_IdxSearcher
       
   395                     * searcher = cpix_IdxSearcher_openDb(&result,
       
   396                                                          *domainSpecifierPtr);
       
   397 
       
   398                 ITK_ASSERT(testMgr,
       
   399                            cpix_Succeeded(&result),
       
   400                            "Could not create searcher for '%s'",
       
   401                            *domainSpecifierPtr);
       
   402                 searchers_.push_back(searcher);
       
   403             }
       
   404     }
       
   405 
       
   406     
       
   407     void unifiedSearchAll(Itk::TestMgr * testMgr,
       
   408                           cpix_Query   * query,
       
   409                           const char   * msg,
       
   410                           void        (* printer)(cpix_Hits*, Itk::TestMgr*))
       
   411     {
       
   412         createSearchersIfNecessary(testMgr);
       
   413 
       
   414         printf("\n\n%s\n",
       
   415                msg);
       
   416 
       
   417         size_t
       
   418             idx = 0;
       
   419 
       
   420         std::list<cpix_IdxSearcher*>::iterator
       
   421             i = searchers_.begin(),
       
   422             end = searchers_.end();
       
   423         for (; i != end; ++i)
       
   424             {
       
   425                 printf("Searching with domain selector '%s':\n",
       
   426                        domainSpecifiers_[idx]);
       
   427 
       
   428                 cpix_Hits
       
   429                     * hits = cpix_IdxSearcher_search(*i,
       
   430                                                      query);
       
   431                 ITK_EXPECT(testMgr,
       
   432                            cpix_Succeeded(*i),
       
   433                            "Could not search idx searcher (%s)",
       
   434                            domainSpecifiers_[idx]);
       
   435                 if (cpix_Succeeded(*i))
       
   436                     {
       
   437                         /* 
       
   438                         PrintHits(hits,
       
   439                                   testMgr);
       
   440                         */
       
   441                         printer(hits,
       
   442                                 testMgr);
       
   443                     }
       
   444                 cpix_Hits_destroy(hits);
       
   445                 printf("\n");
       
   446 
       
   447                 ++idx;
       
   448             }
       
   449     }
       
   450     
       
   451 
       
   452 
       
   453     void searchAll(Itk::TestMgr * testMgr)
       
   454     { 
       
   455        unifiedSearchAll(testMgr,
       
   456                          query_,
       
   457                          "And now searching with all searchers",
       
   458                          &PrintHits);
       
   459     }
       
   460 
       
   461 
       
   462     void suggestAll(Itk::TestMgr * testMgr)
       
   463     {
       
   464         unifiedSearchAll(testMgr,
       
   465                          termsQuery_,
       
   466                          "And now suggesting for 'ha*' with all searchers",
       
   467                          &Suggestion::printSuggestions);
       
   468     }
       
   469 
       
   470 
       
   471     void dumpAll(Itk::TestMgr * testMgr)
       
   472     {
       
   473         unifiedSearchAll(testMgr,
       
   474                          dumpQuery_,
       
   475                          "And now dumping with all searchers",
       
   476                          &PrintHits);
       
   477     }
       
   478 
       
   479 
       
   480     void cleanup()
       
   481     {
       
   482         cpix_Analyzer_destroy(analyzer_);
       
   483         analyzer_ = NULL;
       
   484 
       
   485         cpix_Query_destroy(query_);
       
   486         query_ = NULL;
       
   487 
       
   488         cpix_Query_destroy(termsQuery_);
       
   489         termsQuery_ = NULL;
       
   490 
       
   491         cpix_Query_destroy(dumpQuery_);
       
   492         dumpQuery_ = NULL;
       
   493 
       
   494         cpix_QueryParser_destroy(queryParser_);
       
   495         queryParser_ = NULL;
       
   496 
       
   497         std::list<cpix_IdxSearcher*>::iterator
       
   498             i = searchers_.begin(),
       
   499             end = searchers_.end();
       
   500         for (; i != end; ++i)
       
   501             {
       
   502                 cpix_IdxSearcher_releaseDb(*i);
       
   503             }
       
   504         searchers_.clear();
       
   505     }
       
   506 };
       
   507 
       
   508 
       
   509 const MVFTest DomainSelectionContext::filesAndVols_[] = {
       
   510     {
       
   511         FILE_TEST_CORPUS_PATH "\\en\\1.txt",
       
   512         "@c:root file",
       
   513         CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\c",
       
   514     },
       
   515 
       
   516     {
       
   517         FILE_TEST_CORPUS_PATH "\\en\\2.txt",
       
   518         "@d:root file",
       
   519         CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\d",
       
   520     },
       
   521 
       
   522     {
       
   523         FILE_TEST_CORPUS_PATH "\\en\\3.txt",
       
   524         "@e:root file",
       
   525         CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\e",
       
   526     },
       
   527 
       
   528     {
       
   529         FILE_TEST_CORPUS_PATH "\\en\\4.txt",
       
   530         "@f:root file",
       
   531         CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\f",
       
   532     },
       
   533 
       
   534     {
       
   535         NULL,
       
   536         NULL,
       
   537         NULL
       
   538     },
       
   539 };
       
   540 
       
   541 
       
   542 const char * DomainSelectionContext::domainSpecifiers_[] = {
       
   543     "root",
       
   544     "root msg",
       
   545     "@d:root file,root msg phone sms",
       
   546     "@d:root file,@f:root file",
       
   547     NULL
       
   548 };
       
   549 
       
   550 
       
   551 
       
   552 
       
   553 // TODO implement actual test cases for generic / widening search
       
   554 // (defining volumes and other stuff and searching them) 
       
   555 
       
   556 // TODO implement actual test cases for selecting list of app classes
       
   557 // to use
       
   558 
       
   559 
       
   560 
       
   561 
       
   562 
       
   563 Itk::TesterBase * CreateDomainSelectionTests()
       
   564 {
       
   565     using namespace Itk;
       
   566 
       
   567     SuiteTester
       
   568         * domainTester = new SuiteTester("domain");
       
   569 
       
   570     // some white box test testing matcing logic
       
   571 
       
   572     SuiteTester
       
   573         * whiteBox = new SuiteTester("whitebox");
       
   574 
       
   575 #define TEST "positive"
       
   576     whiteBox->add(TEST,
       
   577                   &testPositiveDomainSelection);
       
   578 #undef TEST
       
   579 
       
   580 #define TEST "negative"
       
   581     whiteBox->add(TEST,
       
   582                   &testNegativeDomainSelection);
       
   583 #undef TEST
       
   584 
       
   585 
       
   586     domainTester->add(whiteBox);
       
   587 
       
   588     // some blackbox tests doing idx manipulation & searches
       
   589     
       
   590     DomainSelectionContext
       
   591         * domainSelectionContext = new DomainSelectionContext();
       
   592     ContextTester
       
   593         * contextTester = new ContextTester("selection",
       
   594                                             domainSelectionContext);
       
   595 
       
   596 #define TEST "createIdxs"
       
   597     contextTester->add(TEST,
       
   598                        domainSelectionContext,
       
   599                        &DomainSelectionContext::testCreateIdxs,
       
   600                        TEST);
       
   601 #undef TEST
       
   602 
       
   603 
       
   604 #define TEST "unmountC"
       
   605     contextTester->add(TEST,
       
   606                        domainSelectionContext,
       
   607                        &DomainSelectionContext::unmountC,
       
   608                        TEST);
       
   609 #undef TEST
       
   610 
       
   611 #define TEST "unmountE"
       
   612     contextTester->add(TEST,
       
   613                        domainSelectionContext,
       
   614                        &DomainSelectionContext::unmountE,
       
   615                        TEST);
       
   616 #undef TEST
       
   617 
       
   618 #define TEST "unmountD"
       
   619     contextTester->add(TEST,
       
   620                        domainSelectionContext,
       
   621                        &DomainSelectionContext::unmountD,
       
   622                        TEST);
       
   623 #undef TEST
       
   624 
       
   625 #define TEST "mountC"
       
   626     contextTester->add(TEST,
       
   627                        domainSelectionContext,
       
   628                        &DomainSelectionContext::mountC,
       
   629                        TEST);
       
   630 #undef TEST
       
   631 
       
   632 #define TEST "mountE"
       
   633     contextTester->add(TEST,
       
   634                        domainSelectionContext,
       
   635                        &DomainSelectionContext::mountE,
       
   636                        TEST);
       
   637 #undef TEST
       
   638 
       
   639 #define TEST "mountD"
       
   640     contextTester->add(TEST,
       
   641                        domainSelectionContext,
       
   642                        &DomainSelectionContext::mountD,
       
   643                        TEST);
       
   644 #undef TEST
       
   645 
       
   646     domainTester->add(contextTester);
       
   647 
       
   648     // ... add more
       
   649 
       
   650     return domainTester;
       
   651 }
       
   652 
       
   653