searchengine/cpix/tsrc/cpixunittest/src/flushtests.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 <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 #include "setupsentry.h"
       
    32 #include "testcorpus.h"
       
    33 
       
    34 
       
    35 
       
    36 class FlushContext : public Itk::ITestContext, public Cpt::IFileVisitor
       
    37 {
       
    38 private:
       
    39     SmsIdxUtil           * util_;
       
    40     cpix_Analyzer        * analyzer_;
       
    41 
       
    42     LineTestCorpusRef      corpus_;
       
    43 
       
    44 public:
       
    45 
       
    46 
       
    47     virtual void setup() throw (Itk::PanicExc)
       
    48     {
       
    49         SetupSentry
       
    50             ss(*this);
       
    51 
       
    52         cpix_Result
       
    53             result;
       
    54 
       
    55         cpix_IdxDb_dbgScrapAll(&result);
       
    56 
       
    57         if (cpix_Failed(&result))
       
    58             {
       
    59                 ITK_PANIC("Could not dbg scrapp all indexes");
       
    60             }
       
    61         
       
    62         using namespace std;
       
    63 
       
    64         util_ = new SmsIdxUtil;
       
    65         util_->init();
       
    66         
       
    67         analyzer_ = cpix_CreateSimpleAnalyzer(&result);
       
    68         if (analyzer_ == NULL)
       
    69             {
       
    70                 ITK_PANIC("Could not create analyzer");
       
    71             }
       
    72 
       
    73         ss.setupComplete();
       
    74     }
       
    75 
       
    76 
       
    77     virtual void tearDown() throw ()
       
    78     {
       
    79         cleanup();
       
    80     }
       
    81 
       
    82 
       
    83     FlushContext()
       
    84         : util_(NULL),
       
    85           analyzer_(NULL),
       
    86           corpus_(DEFAULT_TEST_CORPUS_PATH)
       
    87     {
       
    88         ;
       
    89     }
       
    90 
       
    91 
       
    92     virtual ~FlushContext()
       
    93     {
       
    94         cleanup();
       
    95     }
       
    96  
       
    97 
       
    98 
       
    99     //
       
   100     // from Cpt::IFileVisitor
       
   101     //
       
   102     virtual bool visitFile(const char * path)
       
   103     {
       
   104         bool
       
   105             goOn = true;
       
   106 
       
   107         printf("  # index file %s\n",
       
   108                path);
       
   109 
       
   110         return goOn;
       
   111     }
       
   112     
       
   113     
       
   114     virtual DirVisitResult visitDirPre(const char * /*path*/)
       
   115     {
       
   116         return IFV_CONTINUE;
       
   117     }
       
   118 
       
   119 
       
   120     virtual bool visitDirPost(const char * /*path*/)
       
   121     {
       
   122         return true;
       
   123     }
       
   124 
       
   125 
       
   126 
       
   127     void testEmptyIndex(Itk::TestMgr * testMgr)
       
   128     {
       
   129         checkIndexFiles(testMgr);
       
   130     }
       
   131 
       
   132     
       
   133     void testAddingBy512B(Itk::TestMgr * testMgr)
       
   134     {
       
   135         setMaxInsertBufferSize(testMgr,
       
   136                          512); // 512 B buffer
       
   137         addItems(testMgr,
       
   138                  10);
       
   139     }
       
   140 
       
   141 
       
   142     void testAddingBy10KB(Itk::TestMgr * testMgr)
       
   143     {
       
   144         setMaxInsertBufferSize(testMgr,
       
   145                                10*1024); // 10 KB buffer
       
   146         addItems(testMgr,
       
   147                  40);
       
   148     }
       
   149 
       
   150 
       
   151     void testFlushing(Itk::TestMgr * testMgr)
       
   152     {
       
   153         addItems(testMgr,
       
   154                  15);
       
   155         flush(testMgr);
       
   156         checkIndexFiles(testMgr);
       
   157     }
       
   158 
       
   159 
       
   160 private:
       
   161 
       
   162     // utilities
       
   163     void checkIndexFiles(Itk::TestMgr * )
       
   164     {
       
   165         Cpt::traverse(DEFAULT_CPIX_DIR "indexing\\indexdb\\root\\msg\\phone\\sms",
       
   166                       this);
       
   167     }
       
   168 
       
   169 
       
   170     void addItems(Itk::TestMgr * testMgr,
       
   171                   size_t         num)
       
   172     {
       
   173         using namespace Itk;
       
   174 
       
   175         for (size_t i = 0; i < num; ++i)
       
   176             {
       
   177                 std::wstring
       
   178                     body = corpus_.item(i);
       
   179                 util_->indexSms(i,
       
   180                                 body.c_str(),
       
   181                                 analyzer_,
       
   182                                 testMgr);
       
   183                 printf("Added one more item\n");
       
   184                 checkIndexFiles(testMgr);
       
   185             }
       
   186     }
       
   187 
       
   188     
       
   189     void setMaxInsertBufferSize(Itk::TestMgr * testMgr,
       
   190                                 size_t         maxInsertBufferSize)
       
   191     {
       
   192         cpix_IdxDb_setMaxInsertBufSize(util_->idxDb(),
       
   193                                        maxInsertBufferSize);
       
   194         
       
   195         // casted to be printable with %d on device
       
   196         int trunkated = static_cast<int>(maxInsertBufferSize);
       
   197         
       
   198         ITK_ASSERT(testMgr,
       
   199                    cpix_Succeeded(util_->idxDb()),
       
   200                    "Could not set the maxInsertBufferSize to %d",
       
   201                    trunkated); 
       
   202         printf("Set max insert buffer size to %d\n",
       
   203         	   trunkated);
       
   204     }
       
   205 
       
   206     
       
   207     void flush(Itk::TestMgr * testMgr)
       
   208     {
       
   209         cpix_IdxDb_flush(util_->idxDb());
       
   210         ITK_ASSERT(testMgr,
       
   211                    cpix_Succeeded(util_->idxDb()),
       
   212                    "Could not flush index database");
       
   213         printf("Flushed index database\n");
       
   214     }
       
   215     
       
   216 
       
   217 private:
       
   218     //
       
   219     // private methods
       
   220     //
       
   221 
       
   222     void cleanup()
       
   223     {
       
   224         delete util_;
       
   225         util_ = NULL;
       
   226 
       
   227         cpix_Analyzer_destroy(analyzer_);
       
   228         analyzer_ = NULL;
       
   229     }
       
   230 
       
   231 
       
   232 };
       
   233 
       
   234 
       
   235 
       
   236 Itk::TesterBase * CreateFlushTests()
       
   237 {
       
   238     using namespace Itk;
       
   239 
       
   240     FlushContext
       
   241         * flushContext = new FlushContext();
       
   242     ContextTester
       
   243         * flushTests = new ContextTester("flush",
       
   244                                          flushContext);
       
   245 
       
   246 #define TEST "emptyIndex"
       
   247     flushTests->add(TEST,
       
   248                     flushContext,
       
   249                     &FlushContext::testEmptyIndex,
       
   250                     TEST);
       
   251 #undef TEST
       
   252 
       
   253 
       
   254 #define TEST "addingBy512B"
       
   255     flushTests->add(TEST,
       
   256                     flushContext,
       
   257                     &FlushContext::testAddingBy512B,
       
   258                     TEST);
       
   259 #undef TEST
       
   260 
       
   261 	#define TEST "addingBy10KB"
       
   262     flushTests->add(TEST,
       
   263                     flushContext,
       
   264                     &FlushContext::testAddingBy10KB,
       
   265                     TEST);
       
   266 #undef TEST
       
   267 
       
   268 #define TEST "flushing"
       
   269     flushTests->add(TEST,
       
   270                     flushContext,
       
   271                     &FlushContext::testFlushing,
       
   272                     TEST);
       
   273 #undef TEST
       
   274 
       
   275     
       
   276 
       
   277     // TODO add more tests to suite
       
   278 
       
   279     return flushTests;
       
   280 }