searchengine/cpix/tsrc/cpixunittest/src/prematureqrytests.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 #include "testutils.h"
       
    18 #include "testcorpus.h"
       
    19 #include "config.h"
       
    20 #include "itk.h"
       
    21 #include "setupsentry.h"
       
    22 
       
    23 #include <iostream>
       
    24 
       
    25 #include "cpixsearch.h"
       
    26 
       
    27 
       
    28 namespace
       
    29 {
       
    30     const MVFTest FilesAndVols[] = {
       
    31         {
       
    32             FILE_TEST_CORPUS_PATH "\\en\\1.txt",
       
    33             "@c:root file",
       
    34             CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\c",
       
    35         },
       
    36 
       
    37         {
       
    38             FILE_TEST_CORPUS_PATH "\\en\\2.txt",
       
    39             "@d:root file",
       
    40             CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\d",
       
    41         },
       
    42 
       
    43         {
       
    44             FILE_TEST_CORPUS_PATH "\\en\\3.txt",
       
    45             "@e:root file",
       
    46             CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\e",
       
    47         },
       
    48 
       
    49         {
       
    50             FILE_TEST_CORPUS_PATH "\\en\\4.txt",
       
    51             "@f:root file",
       
    52             CPIX_TEST_INDEVICE_INDEXDB_PHMEM CPIX_FILE_IDXDB "_\\f",
       
    53         },
       
    54 
       
    55         {
       
    56             NULL,
       
    57             NULL,
       
    58             NULL
       
    59         },
       
    60     };
       
    61 }
       
    62 
       
    63 
       
    64 class PrematureQryContext : public Itk::ITestContext
       
    65 {
       
    66 private:
       
    67     cpix_IdxDb         * idxDb_;
       
    68     cpix_IdxSearcher   * idxSearcher_;
       
    69 
       
    70     cpix_Analyzer      * analyzer_;
       
    71     cpix_QueryParser   * queryParser_;
       
    72     cpix_Query         * query_;
       
    73 
       
    74 
       
    75 public:
       
    76 
       
    77     //
       
    78     // from interface Itk::ITestContext
       
    79     //
       
    80     virtual void setup() throw (Itk::PanicExc)
       
    81     {
       
    82         SetupSentry
       
    83             ss(*this);
       
    84 
       
    85         cpix_Result
       
    86             result;
       
    87 
       
    88         cpix_IdxDb_dbgScrapAll(&result);
       
    89         if (cpix_Failed(&result))
       
    90             {
       
    91                 ITK_PANIC("Could not dbg scrapp all indexes");
       
    92             }
       
    93 
       
    94         analyzer_ = cpix_CreateSimpleAnalyzer(&result);
       
    95         if (cpix_Failed(&result))
       
    96             {
       
    97                 ITK_PANIC("Could not create analyzer instance");
       
    98             }
       
    99 
       
   100         queryParser_ = cpix_QueryParser_create(&result,
       
   101                                                LCPIX_DEFAULT_FIELD,
       
   102                                                analyzer_);
       
   103         if (cpix_Failed(&result))
       
   104             {
       
   105                 ITK_PANIC("Could not create query parser");
       
   106             }
       
   107 
       
   108         query_ = cpix_QueryParser_parse(queryParser_,
       
   109                                         L"ha*");
       
   110         if (cpix_Failed(queryParser_))
       
   111             {
       
   112                 ITK_PANIC("Could not create query");
       
   113             }
       
   114 
       
   115         for (const MVFTest * test = FilesAndVols;
       
   116              test->qualifiedBaseAppClass_ != NULL;
       
   117              ++test)
       
   118             {
       
   119                 bool
       
   120                     isThere = Cpt::directoryexists(test->idxDbPath_);
       
   121 
       
   122                 if (isThere)
       
   123                     {
       
   124                         int
       
   125                             result = Cpt::removeall(test->idxDbPath_);
       
   126                         
       
   127                         if (result != 0)
       
   128                             {
       
   129                                 ITK_PANIC("Could not purge index path %s",
       
   130                                           test->idxDbPath_);
       
   131                             }
       
   132                     }
       
   133             }
       
   134 
       
   135         ss.setupComplete();
       
   136     }    
       
   137 
       
   138 
       
   139     virtual void tearDown() throw()
       
   140     {
       
   141         cleanup();
       
   142     }
       
   143 
       
   144 
       
   145     //
       
   146     // lifetime mgmt
       
   147     //
       
   148     virtual ~PrematureQryContext()
       
   149     {
       
   150         cleanup();
       
   151     }
       
   152     
       
   153 
       
   154     
       
   155     PrematureQryContext()
       
   156         : idxDb_(NULL),
       
   157           idxSearcher_(NULL),
       
   158           analyzer_(NULL),
       
   159           queryParser_(NULL),
       
   160           query_(NULL)
       
   161     {
       
   162         ;
       
   163     }
       
   164 
       
   165 
       
   166     //
       
   167     // test functions
       
   168     //
       
   169     void testBeforeVolDefs(Itk::TestMgr * mgr)
       
   170     {
       
   171         printf("Before volume definitions\n");
       
   172 
       
   173         qry(mgr,
       
   174             false);
       
   175     }
       
   176 
       
   177 
       
   178     void testBeforeContentDefs(Itk::TestMgr * mgr)
       
   179     {
       
   180         printf("Defining volumes\n");
       
   181 
       
   182         cpix_Result
       
   183             result;
       
   184 
       
   185         for (const MVFTest * test = FilesAndVols;
       
   186              test->qualifiedBaseAppClass_ != NULL;
       
   187              ++test)
       
   188             {
       
   189                 cpix_IdxDb_defineVolume(&result,
       
   190                                         test->qualifiedBaseAppClass_,
       
   191                                         test->idxDbPath_);
       
   192                 ITK_ASSERT(mgr,
       
   193                            cpix_Succeeded(&result),
       
   194                            "Failed to define volume %s/%s",
       
   195                            test->qualifiedBaseAppClass_,
       
   196                            test->idxDbPath_);
       
   197             }
       
   198 
       
   199         printf("Defined volumes, but there are no indexes created yet\n");
       
   200 
       
   201         qry(mgr,
       
   202             true);
       
   203     }
       
   204 
       
   205 
       
   206     void testReady(Itk::TestMgr * mgr)
       
   207     {
       
   208         printf("Defining contents\n");
       
   209 
       
   210         const MVFTest
       
   211             * mvfTest = FilesAndVols;
       
   212 
       
   213         for (; mvfTest->qualifiedBaseAppClass_ != NULL; ++mvfTest)
       
   214             {
       
   215                 using namespace std;
       
   216 
       
   217                 printf("Creating volume %s ...\n",
       
   218                        mvfTest->qualifiedBaseAppClass_);
       
   219 
       
   220                 {
       
   221                     auto_ptr<VolumeFileIdxUtil>
       
   222                         vfiUtil(new VolumeFileIdxUtil(mvfTest));
       
   223                     vfiUtil->init();
       
   224 
       
   225                     vfiUtil->indexFile(mvfTest->textFilePath_,
       
   226                                        analyzer_,
       
   227                                        mgr);
       
   228                     vfiUtil->flush();
       
   229                 }
       
   230 
       
   231                 printf("... created volume %s\n",
       
   232                        mvfTest->qualifiedBaseAppClass_);
       
   233             }
       
   234 
       
   235         qry(mgr,
       
   236             true);
       
   237     }
       
   238 
       
   239 
       
   240 private:
       
   241     void cleanup()
       
   242     {
       
   243         cpix_IdxDb_releaseDb(idxDb_);
       
   244         idxDb_ = NULL;
       
   245 
       
   246         cpix_IdxSearcher_releaseDb(idxSearcher_);
       
   247         idxSearcher_ = NULL;
       
   248 
       
   249         cpix_Query_destroy(query_);
       
   250         query_ = NULL;
       
   251 
       
   252         cpix_QueryParser_destroy(queryParser_);
       
   253         queryParser_ = NULL;
       
   254 
       
   255         cpix_Analyzer_destroy(analyzer_);
       
   256         analyzer_ = NULL;
       
   257     }
       
   258 
       
   259 
       
   260     void qryIdx(Itk::TestMgr * mgr,
       
   261                 bool           shouldSucceed)
       
   262     {
       
   263         printf("Trying to search with cpix_IdxDb instance\n");
       
   264 
       
   265         cpix_Result
       
   266             result;
       
   267         bool
       
   268             succeeded;
       
   269 
       
   270         if (idxDb_ == NULL)
       
   271             {
       
   272                 idxDb_ = cpix_IdxDb_openDb(&result,
       
   273                                            "@c:root file",
       
   274                                            cpix_IDX_OPEN);
       
   275                                            
       
   276                 succeeded = cpix_Succeeded(&result);
       
   277 
       
   278                 if (!succeeded)
       
   279                     {
       
   280                         goto exit;
       
   281                     }
       
   282             }
       
   283 
       
   284         cpix_Hits
       
   285             * hits = cpix_IdxDb_search(idxDb_,
       
   286                                        query_);
       
   287 
       
   288         succeeded = cpix_Succeeded(idxDb_);
       
   289         
       
   290         if (!succeeded)
       
   291             {
       
   292                 goto exit;
       
   293             }
       
   294 
       
   295         PrintHits(hits,
       
   296                   mgr);
       
   297 
       
   298         cpix_Hits_destroy(hits);
       
   299 
       
   300     exit:
       
   301         ITK_EXPECT(mgr,
       
   302                    (shouldSucceed && succeeded) 
       
   303                    || (!shouldSucceed && !succeeded),
       
   304                    "Querying idx should %s have succeeded and it did %s",
       
   305                    shouldSucceed ? "" : "not",
       
   306                    succeeded ? "" : "not");
       
   307     }
       
   308 
       
   309 
       
   310     void qrySearcher(Itk::TestMgr * mgr,
       
   311                      bool           shouldSucceed)
       
   312     {
       
   313         printf("Trying to search with cpix_IdxSearcher instance\n");
       
   314 
       
   315         cpix_Result
       
   316             result;
       
   317         bool
       
   318             succeeded;
       
   319 
       
   320         if (idxSearcher_ == NULL)
       
   321             {
       
   322                 idxSearcher_ = cpix_IdxSearcher_openDb(&result,
       
   323                                                        "root");
       
   324                                            
       
   325                 succeeded = cpix_Succeeded(&result);
       
   326 
       
   327                 if (!succeeded)
       
   328                     {
       
   329                         goto exit;
       
   330                     }
       
   331             }
       
   332 
       
   333         cpix_Hits
       
   334             * hits = cpix_IdxSearcher_search(idxSearcher_,
       
   335                                              query_);
       
   336 
       
   337         succeeded = cpix_Succeeded(idxSearcher_);
       
   338         
       
   339         if (!succeeded)
       
   340             {
       
   341                 goto exit;
       
   342             }
       
   343 
       
   344         PrintHits(hits,
       
   345                   mgr);
       
   346 
       
   347         cpix_Hits_destroy(hits);
       
   348 
       
   349     exit:
       
   350         ITK_EXPECT(mgr,
       
   351                    (shouldSucceed && succeeded) 
       
   352                    || (!shouldSucceed && !succeeded),
       
   353                    "Querying searcher should %s have succeeded and it did %s",
       
   354                    shouldSucceed ? "" : "not",
       
   355                    succeeded ? "" : "not");
       
   356     }
       
   357 
       
   358 
       
   359 
       
   360     void qry(Itk::TestMgr * mgr,
       
   361              bool           shouldSucceed)
       
   362     {
       
   363         qryIdx(mgr,
       
   364                shouldSucceed);
       
   365         qrySearcher(mgr,
       
   366                     shouldSucceed);
       
   367     }
       
   368 
       
   369 
       
   370 
       
   371 };
       
   372 
       
   373 
       
   374 
       
   375 Itk::TesterBase * CreatePrematureQryTests()
       
   376 {
       
   377     using namespace Itk;
       
   378 
       
   379     PrematureQryContext
       
   380         * pqc = new PrematureQryContext();
       
   381     ContextTester
       
   382         * contextTester = new ContextTester("prematureqry",
       
   383                                             pqc);
       
   384 
       
   385 #define TEST "beforeVolDefs"
       
   386     contextTester->add(TEST,
       
   387                        pqc,
       
   388                        &PrematureQryContext::testBeforeVolDefs,
       
   389                        TEST);
       
   390 #undef TEST
       
   391 
       
   392 #define TEST "beforeContentDefs"
       
   393     contextTester->add(TEST,
       
   394                        pqc,
       
   395                        &PrematureQryContext::testBeforeContentDefs,
       
   396                        TEST);
       
   397 #undef TEST
       
   398     
       
   399 #define TEST "ready"
       
   400     contextTester->add(TEST,
       
   401                        pqc,
       
   402                        &PrematureQryContext::testReady,
       
   403                        TEST);
       
   404 #undef TEST
       
   405 
       
   406     return contextTester;
       
   407 }