searchengine/cpix/tsrc/cpixunittest/src/utf8path.cpp
changeset 0 671dee74050a
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 <wchar.h>
       
    18 #include <stddef.h>
       
    19 
       
    20 #include <iostream>
       
    21 
       
    22 #include "cpixidxdb.h"
       
    23 
       
    24 #include "itk.h"
       
    25 
       
    26 #include "config.h"
       
    27 #include "testutils.h"
       
    28 #include "setupsentry.h"
       
    29 
       
    30 
       
    31 struct TestPathAndContent
       
    32 {
       
    33     const char    * utf8Dir_;
       
    34     const char    * utf8File_;
       
    35     const char    * utf8Content_;
       
    36 };
       
    37 
       
    38 
       
    39 const struct TestPathAndContent TestPathAndContents[] = 
       
    40     {
       
    41         { "english 1\\",
       
    42           "great ideas.txt",
       
    43           "Some great ideas never catch on. London. Foobar."
       
    44         },
       
    45 
       
    46         { "russian 2\\",
       
    47           "Русский язык.txt",
       
    48           "Подарок обошелся в $12 тысяч Голливудский актер Брэд Питт попал в аварию в Лос-Анджелесе. Moscow. Foobar."
       
    49         },
       
    50 
       
    51         { "chinese 3\\",
       
    52           "中文.txt",
       
    53           "解放军今年演习强度密度\"罕见\" 开放透明度空前 高清 Beijing. Foobar."
       
    54         },
       
    55 
       
    56         { "japanese 4\\",
       
    57           "日本語.txt",
       
    58           "10月25日(日曜日)、バグダッドにおいて連続爆発テロが発生し、 Tokyo. Foobar."
       
    59         },
       
    60 
       
    61         { "finnish 5\\",
       
    62           "äÄöÖ.txt",
       
    63           "Syksyllä satoi vettä. Helsinki. Foobar."
       
    64         },
       
    65 
       
    66         { NULL,
       
    67           NULL,
       
    68           NULL
       
    69         },
       
    70     };
       
    71 
       
    72 
       
    73 
       
    74 
       
    75 
       
    76 class Utf8PathContext : public Itk::ITestContext, public Cpt::IFileVisitor
       
    77 {
       
    78 private:
       
    79     FileIdxUtil             * util_;
       
    80     cpix_Analyzer           * analyzer_;
       
    81     cpix_Query              * query_;
       
    82 
       
    83     cpix_QueryParser        * queryParser_;
       
    84 
       
    85     Itk::TestMgr            * testMgr_;
       
    86 
       
    87 
       
    88 
       
    89     static const char         BaseDir_[];
       
    90 
       
    91 
       
    92 public:
       
    93 
       
    94     //
       
    95     // From ITestContext
       
    96     //
       
    97     virtual void setup() throw (Itk::PanicExc)
       
    98     {
       
    99         SetupSentry
       
   100             ss(*this);
       
   101 
       
   102         cpix_Result
       
   103             result;
       
   104 
       
   105         cpix_IdxDb_dbgScrapAll(&result);
       
   106 
       
   107         using namespace std;
       
   108 
       
   109         util_ = new FileIdxUtil;
       
   110         util_->init();
       
   111         
       
   112         analyzer_ = cpix_CreateSimpleAnalyzer(&result);
       
   113         if (analyzer_ == NULL)
       
   114             {
       
   115                 ITK_PANIC("Could not create analyzer");
       
   116             }
       
   117 
       
   118         queryParser_ = cpix_QueryParser_create(&result,
       
   119                                                CONTENTS_FIELD,
       
   120                                                analyzer_);
       
   121         if (queryParser_ == NULL)
       
   122             {
       
   123                 ITK_PANIC("Could not create query parser");
       
   124             }
       
   125         
       
   126         query_ = cpix_QueryParser_parse(queryParser_,
       
   127                                         L"foobar");
       
   128         if (cpix_Failed(queryParser_)
       
   129             || query_ == NULL)
       
   130             {
       
   131                 ITK_PANIC("Could not parse query string");
       
   132             }
       
   133 
       
   134         ss.setupComplete();
       
   135     }
       
   136     
       
   137 
       
   138     virtual void tearDown() throw ()
       
   139     {
       
   140         cleanup();
       
   141     }
       
   142 
       
   143 
       
   144     Utf8PathContext()
       
   145         : util_(NULL),
       
   146           analyzer_(NULL),
       
   147           query_(NULL),
       
   148           queryParser_(NULL),
       
   149           testMgr_(NULL)
       
   150     {
       
   151         ;
       
   152     }
       
   153 
       
   154 
       
   155     ~Utf8PathContext()
       
   156     {
       
   157         cleanup();
       
   158     }
       
   159 
       
   160 
       
   161     //
       
   162     // from Cpt::IFileVisitor
       
   163     //
       
   164     virtual bool visitFile(const char * path)
       
   165     {
       
   166         bool
       
   167             goOn = true;
       
   168 
       
   169         util_->indexFile(path,
       
   170                          analyzer_,
       
   171                          testMgr_);
       
   172 
       
   173         return goOn;
       
   174     }
       
   175     
       
   176     
       
   177     virtual DirVisitResult visitDirPre(const char * /*path*/)
       
   178     {
       
   179         return IFV_CONTINUE;
       
   180     }
       
   181 
       
   182 
       
   183     virtual bool visitDirPost(const char * /*path*/)
       
   184     {
       
   185         return true;
       
   186     }
       
   187 
       
   188 
       
   189 
       
   190     /**
       
   191      * We have to generate text files manually, because even if
       
   192      * Symbian itself supports unicode, its build tools like bldmake
       
   193      * etc do not, so we can't even export test files etc. DAMN
       
   194      * SYMBIAN!
       
   195      */
       
   196     void testCreateUtf8PathFiles(Itk::TestMgr * mgr)
       
   197     {
       
   198         printf("Creating files with utf8 paths and content\n");
       
   199 
       
   200         using namespace std;
       
   201 
       
   202         for (const struct TestPathAndContent * tpac = TestPathAndContents;
       
   203              tpac->utf8Dir_ != NULL;
       
   204              ++tpac)
       
   205             {
       
   206                 string
       
   207                     utf8Path(BaseDir_);
       
   208 
       
   209                 utf8Path += tpac->utf8Dir_;
       
   210 
       
   211                 int
       
   212                     result = Cpt::mkdirs(utf8Path.c_str(), 0666);
       
   213 
       
   214                 ITK_ASSERT(mgr,
       
   215                            result == 0,
       
   216                            "Could not create base dir %s",
       
   217                            utf8Path.c_str());
       
   218 
       
   219                 utf8Path += tpac->utf8File_;
       
   220 
       
   221                 ofstream
       
   222                     ofs(utf8Path.c_str());
       
   223 
       
   224                 ofs << tpac->utf8Content_;
       
   225 
       
   226                 ofs.flush();
       
   227 
       
   228                 printf("Created file at : %s\n",
       
   229                        utf8Path.c_str());
       
   230             }
       
   231     }
       
   232 
       
   233 
       
   234     void testHarvesting(Itk::TestMgr * mgr)
       
   235     {
       
   236         printf("Harvesting files with utf8 paths and content\n");
       
   237 
       
   238         testMgr_ = mgr;
       
   239         Cpt::traverse(BaseDir_,
       
   240                       this);
       
   241         util_->flush();
       
   242     }
       
   243 
       
   244     
       
   245     void testSearching(Itk::TestMgr * mgr)
       
   246     {
       
   247         printf("Searching files with utf8 paths and content\n");
       
   248 
       
   249         using namespace Itk;
       
   250 
       
   251         cpix_Hits
       
   252             * hits = cpix_IdxDb_search(util_->idxDb(),
       
   253                                        query_);
       
   254         
       
   255         if (cpix_Failed(util_->idxDb()))
       
   256             {
       
   257                 ITK_EXPECT(mgr,
       
   258                            false,
       
   259                            "Failed to search index");
       
   260                 cpix_ClearError(util_->idxDb());
       
   261             }
       
   262         else
       
   263             {
       
   264                 util_->printHits(hits,
       
   265                                  mgr);
       
   266 
       
   267                 cpix_Hits_destroy(hits);
       
   268             }
       
   269     }
       
   270 
       
   271 
       
   272 private:
       
   273     //
       
   274     // private methods
       
   275     //
       
   276 
       
   277     void cleanup()
       
   278     {
       
   279         delete util_;
       
   280         util_ = NULL;
       
   281 
       
   282         cpix_Analyzer_destroy(analyzer_);
       
   283         analyzer_ = NULL;
       
   284 
       
   285         cpix_Query_destroy(query_);
       
   286         query_ = NULL;
       
   287 
       
   288         cpix_QueryParser_destroy(queryParser_);
       
   289         queryParser_ = NULL;
       
   290     }
       
   291 
       
   292 
       
   293 };
       
   294 
       
   295 
       
   296 const char Utf8PathContext::BaseDir_[] = "c:\\Data\\cpixunittest\\_txt\\";
       
   297 
       
   298 
       
   299 
       
   300 Itk::TesterBase * CreateUtf8PathTests()
       
   301 {
       
   302     using namespace Itk;
       
   303 
       
   304     Utf8PathContext
       
   305         * ctxt = new Utf8PathContext;
       
   306 
       
   307     ContextTester
       
   308         * utf8Path = new ContextTester("utf8path",
       
   309                                        ctxt);
       
   310     
       
   311 #define TEST "createUtf8PathFiles"
       
   312     utf8Path->add(TEST,
       
   313                   ctxt,
       
   314                   &Utf8PathContext::testCreateUtf8PathFiles,
       
   315                   TEST);
       
   316 #undef TEST
       
   317 
       
   318 #define TEST "harvest"
       
   319     utf8Path->add(TEST,
       
   320                   ctxt,
       
   321                   &Utf8PathContext::testHarvesting,
       
   322                   TEST);
       
   323 #undef TEST
       
   324 
       
   325 #define TEST "search"
       
   326     utf8Path->add(TEST,
       
   327                   ctxt,
       
   328                   &Utf8PathContext::testSearching,
       
   329                   TEST);
       
   330 #undef TEST
       
   331     
       
   332     // TODO add more
       
   333 
       
   334     return utf8Path;
       
   335 }
       
   336