searchengine/cpix/tsrc/cpixunittest/src/aggregatetests.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 
       
    33 #include "testcorpus.h"
       
    34 
       
    35 
       
    36 cpix_FieldDesc MultiFieldSchema[] = {
       
    37     
       
    38     // filter field
       
    39     {
       
    40         LCPIX_FILTERID_FIELD,                  // name_
       
    41         cpix_STORE_YES | cpix_INDEX_NO,       // cfg_
       
    42     },
       
    43 
       
    44     // dummy field 
       
    45     {
       
    46         L"dummy",                             // name_
       
    47         cpix_STORE_YES | cpix_INDEX_TOKENIZED // cfg_
       
    48     },
       
    49     {
       
    50         L"dummy2",                            // name_
       
    51         cpix_STORE_YES | cpix_INDEX_NO | cpix_AGGREGATE_YES// cfg_
       
    52     },
       
    53     {
       
    54         L"dummy3",                            // name_
       
    55         cpix_STORE_YES | cpix_INDEX_TOKENIZED | cpix_AGGREGATE_NO // cfg_
       
    56     }
       
    57 };
       
    58 
       
    59 
       
    60 const wchar_t * DummyWords[] = {
       
    61     L"happy words",
       
    62     L"look at you",
       
    63     L"happening there",
       
    64     L"important happiness"
       
    65 };
       
    66 
       
    67 // NOTE must be of the same size (or bigger) as DummyWords
       
    68 const wchar_t * Dummy2Words[] = {
       
    69     L"christmas easter",
       
    70     L"summer's midnight eve",
       
    71     L"thanksgiving",
       
    72     L"vappu"
       
    73 };
       
    74 
       
    75 // NOTE must be of the same size (or bigger) as DummyWords
       
    76 const wchar_t * Dummy3Words[] = {
       
    77     L"rabid happening",
       
    78     L"look, important and happy robot",
       
    79     L"christmas for ever",
       
    80     L"christmas is an important and happy happening"
       
    81 };
       
    82 
       
    83 
       
    84 class MultiFieldIdxUtil : public FileIdxUtil
       
    85 {
       
    86 public:
       
    87     virtual ~MultiFieldIdxUtil() throw ()
       
    88     {
       
    89         ;
       
    90     }
       
    91 
       
    92 
       
    93     void indexMultiField(const char     * path,
       
    94                          cpix_Analyzer  * analyzer,
       
    95                          Itk::TestMgr   * testMgr)
       
    96     {
       
    97         wchar_t
       
    98             wpath[256];
       
    99 
       
   100         size_t
       
   101             res = mbstowcs(wpath,
       
   102                            path,
       
   103                            sizeof(wpath) / sizeof(wchar_t) - 1);
       
   104         ITK_ASSERT(testMgr,
       
   105                    //res >= 0,
       
   106 				   1,
       
   107                    "mbstowcs failed, errno: %d",
       
   108                    errno);
       
   109         wpath[sizeof(wpath) / sizeof(wchar_t) - 1] = wchar_t(0);
       
   110 
       
   111 
       
   112         static int
       
   113             dummyWordIdx = 0;
       
   114 
       
   115         const wchar_t *fields[4] = {
       
   116             LCPIX_FILEPARSER_FID,      // filter id field
       
   117             DummyWords[dummyWordIdx],  // dummy field
       
   118             Dummy2Words[dummyWordIdx], // dummy2 field
       
   119             Dummy3Words[dummyWordIdx] // dummy3 field
       
   120         };
       
   121 
       
   122         ++dummyWordIdx;
       
   123         if (dummyWordIdx == sizeof(DummyWords) / sizeof(wchar_t*))
       
   124             {
       
   125                 dummyWordIdx = 0;
       
   126             }
       
   127 
       
   128         // NOTE: app class, excerpt and mime type have to be defined
       
   129         // here, but this is using the file parser functionality
       
   130         // inside Cpix, which re-defines these anyway
       
   131         cpix_IdxDb_add2(idxDb(),
       
   132                         schemaId(),
       
   133                         wpath,                   // docUid,
       
   134                         NULL,                    // app class
       
   135                         NULL,                    // excerpt
       
   136                         NULL,                    // mime type
       
   137                         (const wchar_t**)fields, // fields
       
   138                         analyzer);
       
   139 
       
   140         if (cpix_Failed(idxDb()))
       
   141             {
       
   142                 wchar_t
       
   143                     report[256];
       
   144                 cpix_Error_report(idxDb()->err_,
       
   145                                   report,
       
   146                                   sizeof(report));
       
   147                 ITK_ASSERT(testMgr,
       
   148                            false,
       
   149                            "Failed to add document %S: %S",
       
   150                            wpath,
       
   151                            report);
       
   152                 cpix_ClearError(idxDb());
       
   153             }
       
   154         else
       
   155             {
       
   156                 wpath[0] = '!';
       
   157                 ITK_MSG(testMgr,
       
   158                         "Indexed file (multifield): %S",
       
   159                         wpath);
       
   160             }
       
   161     }
       
   162 
       
   163 
       
   164 protected:
       
   165     virtual void printHit(cpix_Document  * doc,
       
   166                           Itk::TestMgr   * testMgr)
       
   167     {
       
   168         using namespace std;
       
   169         
       
   170         std::wstring
       
   171             idStr(getIdStr(doc,
       
   172                            testMgr));
       
   173         
       
   174         fprintf(stdout,
       
   175                 "DOC (multifield) (%S):\n",
       
   176                 idStr.c_str());
       
   177         
       
   178         cpix_DocFieldEnum
       
   179             * dfe = cpix_Document_fields(doc);
       
   180         if (cpix_Succeeded(doc))
       
   181             {
       
   182                 cpix_Field
       
   183                     field;
       
   184 
       
   185                 while (cpix_DocFieldEnum_hasMore(dfe))
       
   186                     {
       
   187                         cpix_DocFieldEnum_next(dfe,
       
   188                                                &field);
       
   189 
       
   190                         const wchar_t
       
   191                             * name = cpix_Field_name(&field);
       
   192 
       
   193                         if (wcscmp(name, LCPIX_DOCUID_FIELD) == 0)
       
   194                             {
       
   195                                 continue;
       
   196                             }
       
   197 
       
   198                         fprintf(stdout,
       
   199                                 " o %S: ",
       
   200                                 name);
       
   201 
       
   202                         bool
       
   203                             binary = static_cast<bool>(cpix_Field_isBinary(&field));
       
   204 
       
   205                         fprintf(stdout,
       
   206                                 "%S\n",
       
   207                                 (binary ? 
       
   208                                  L"(binary)" 
       
   209                                  : cpix_Field_stringValue(&field)
       
   210                                  )
       
   211                                 );
       
   212                     }
       
   213 
       
   214                 cpix_DocFieldEnum_destroy(dfe);
       
   215             }
       
   216         else
       
   217             {
       
   218                 wchar_t
       
   219                     report[128];
       
   220                 cpix_Error_report(doc->err_,
       
   221                                   report,
       
   222                                   sizeof(report)/sizeof(wchar_t));
       
   223                 ITK_EXPECT(testMgr,
       
   224                            false,
       
   225                            "Could not create doc field enum: %S",
       
   226                            report);
       
   227                 cpix_ClearError(doc);
       
   228             }
       
   229     }
       
   230 
       
   231 
       
   232     virtual SchemaId addSchema() throw (Itk::PanicExc)
       
   233     {
       
   234         return cpix_IdxDb_addSchema(idxDb(),
       
   235                                     MultiFieldSchema,
       
   236                                     sizeof(MultiFieldSchema)/sizeof(cpix_FieldDesc));    
       
   237     }
       
   238 
       
   239 };
       
   240 
       
   241 
       
   242 
       
   243 class MultiFieldContext : public Itk::ITestContext, public Cpt::IFileVisitor
       
   244 {
       
   245 protected:
       
   246     MultiFieldIdxUtil     * util_;
       
   247     cpix_Analyzer         * analyzer_;
       
   248     cpix_QueryParser      * queryParser_;
       
   249 
       
   250     Itk::TestMgr          * testMgr_;
       
   251 
       
   252 
       
   253 public:
       
   254 
       
   255     //
       
   256     // From ITestContext
       
   257     //
       
   258     virtual void setup() throw (Itk::PanicExc)
       
   259     {
       
   260         SetupSentry
       
   261             ss(*this);
       
   262 
       
   263         cpix_Result
       
   264             result;
       
   265 
       
   266         cpix_IdxDb_dbgScrapAll(&result);
       
   267 
       
   268         using namespace std;
       
   269 
       
   270         util_ = new MultiFieldIdxUtil;
       
   271         util_->init();
       
   272         
       
   273         analyzer_ = cpix_CreateSimpleAnalyzer(&result);
       
   274 
       
   275         if (analyzer_ == NULL)
       
   276             {
       
   277                 ITK_PANIC("Could not create analyzer");
       
   278             }
       
   279 
       
   280         queryParser_ = cpix_QueryParser_create(&result,
       
   281                                                LCPIX_DEFAULT_FIELD,
       
   282                                                analyzer_);
       
   283 
       
   284         if (queryParser_ == NULL)
       
   285             {
       
   286                 ITK_PANIC("Could not create query parser");
       
   287             }
       
   288 
       
   289         ss.setupComplete();
       
   290     }
       
   291 
       
   292 
       
   293     virtual void tearDown() throw ()
       
   294     {
       
   295         cleanup();
       
   296     }
       
   297 
       
   298 
       
   299     virtual ~MultiFieldContext()
       
   300     {
       
   301         cleanup();
       
   302     }
       
   303 
       
   304 
       
   305     //
       
   306     // from Cpt::IFileVisitor
       
   307     //
       
   308     virtual bool visitFile(const char * path)
       
   309     {
       
   310         bool
       
   311             goOn = true;
       
   312 
       
   313         util_->indexMultiField(path,
       
   314                                analyzer_,
       
   315                                testMgr_);
       
   316         
       
   317         return goOn;
       
   318     }
       
   319 
       
   320 
       
   321     virtual DirVisitResult visitDirPre(const char * /* path */)
       
   322     {
       
   323         return IFV_CONTINUE;
       
   324     }
       
   325 
       
   326 
       
   327     virtual bool visitDirPost(const char * /* path */)
       
   328     {
       
   329         return true;
       
   330     }
       
   331 
       
   332 
       
   333     MultiFieldContext()
       
   334         : util_(NULL),
       
   335           analyzer_(NULL),
       
   336           queryParser_(NULL),
       
   337           testMgr_(NULL)
       
   338     {
       
   339         ;
       
   340     }
       
   341 
       
   342 
       
   343     //
       
   344     // Test operations
       
   345     //
       
   346     void testAddFiles(Itk::TestMgr * testMgr)
       
   347     {
       
   348         testMgr_ = testMgr;
       
   349 
       
   350         Cpt::traverse(FILE_TEST_CORPUS_PATH "\\en",
       
   351                       this);
       
   352 
       
   353         util_->flush();
       
   354     }
       
   355 
       
   356 
       
   357     void testSearchForHappy(Itk::TestMgr * testMgr)
       
   358     {
       
   359         const wchar_t
       
   360             * word = L"happy";
       
   361 
       
   362         testSearchFor(testMgr,
       
   363                       word);
       
   364     }
       
   365 
       
   366     void testSearchForImportant(Itk::TestMgr * testMgr)
       
   367     {
       
   368         const wchar_t
       
   369             * word = L"important";
       
   370 
       
   371         testSearchFor(testMgr,
       
   372                       word);
       
   373     }
       
   374 
       
   375     void testSearchForHappening(Itk::TestMgr * testMgr)
       
   376     {
       
   377         const wchar_t
       
   378             * word = L"happening";
       
   379 
       
   380         testSearchFor(testMgr,
       
   381                       word);
       
   382     }
       
   383 
       
   384     void testSearchForLook(Itk::TestMgr * testMgr)
       
   385     {
       
   386         const wchar_t
       
   387             * word = L"look";
       
   388 
       
   389         testSearchFor(testMgr,
       
   390                       word);
       
   391     }
       
   392 
       
   393     void testSearchForChristmas(Itk::TestMgr * testMgr)
       
   394     {
       
   395         const wchar_t
       
   396             * word = L"christmas";
       
   397 
       
   398         testSearchFor(testMgr,
       
   399                       word);
       
   400     }
       
   401 
       
   402 
       
   403 private:
       
   404     //
       
   405     // private methods
       
   406     //
       
   407     void testSearchFor(Itk::TestMgr  * testMgr,
       
   408                        const wchar_t * word)
       
   409     {
       
   410         wchar_t
       
   411             report[512];
       
   412 
       
   413         cpix_Query
       
   414             * query = cpix_QueryParser_parse(queryParser_,
       
   415                                              word);
       
   416 
       
   417         if (cpix_Succeeded(queryParser_))
       
   418             {
       
   419                 cpix_Hits
       
   420                     * hits = cpix_IdxDb_search(util_->idxDb(),
       
   421                                                query);
       
   422 
       
   423                 if (cpix_Succeeded(util_->idxDb()))
       
   424                     {
       
   425                         util_->printHits(hits,
       
   426                                          testMgr);
       
   427                     }
       
   428                 else
       
   429                     {
       
   430                         cpix_Error_report(util_->idxDb()->err_,
       
   431                                           report,
       
   432                                           sizeof(report)/sizeof(wchar_t));
       
   433                         ITK_EXPECT(testMgr,
       
   434                                    false,
       
   435                                    "Failed to search: %S",
       
   436                                    report);
       
   437                         cpix_ClearError(util_->idxDb());
       
   438                     }
       
   439 
       
   440                 cpix_Hits_destroy(hits);
       
   441             }
       
   442         else
       
   443             {
       
   444                 cpix_Error_report(query->err_,
       
   445                                   report,
       
   446                                   sizeof(report)/sizeof(wchar_t));
       
   447 
       
   448                 ITK_EXPECT(testMgr,
       
   449                            false,
       
   450                            "Failed to parse '%S': %S",
       
   451                            word,
       
   452                            report);
       
   453 
       
   454                 cpix_ClearError(query);
       
   455             }
       
   456         
       
   457         cpix_Query_destroy(query);
       
   458     }
       
   459 
       
   460 
       
   461     void cleanup()
       
   462     {
       
   463         delete util_;
       
   464         util_ = NULL;
       
   465 
       
   466         cpix_Analyzer_destroy(analyzer_);
       
   467         analyzer_ = NULL;
       
   468 
       
   469         cpix_QueryParser_destroy(queryParser_);
       
   470         queryParser_ = NULL;
       
   471     }
       
   472 
       
   473 };
       
   474 
       
   475 
       
   476 Itk::TesterBase * CreateAggregateTests()
       
   477 {
       
   478     using namespace Itk;
       
   479 
       
   480     MultiFieldContext
       
   481         * context = new MultiFieldContext;
       
   482 
       
   483     ContextTester
       
   484         * contextTester = new ContextTester("aggregate",
       
   485                                             context);
       
   486 
       
   487 
       
   488 #define TEST "adding"
       
   489     contextTester->add(TEST,
       
   490                        context,
       
   491                        &MultiFieldContext::testAddFiles,
       
   492                        TEST);
       
   493 #undef TEST
       
   494 
       
   495 #define TEST "searchHappy"
       
   496     contextTester->add(TEST,
       
   497                        context,
       
   498                        &MultiFieldContext::testSearchForHappy,
       
   499                        TEST);
       
   500 #undef TEST
       
   501 
       
   502 #define TEST "searchImportant"
       
   503     contextTester->add(TEST,
       
   504                        context,
       
   505                        &MultiFieldContext::testSearchForImportant,
       
   506                        TEST);
       
   507 #undef TEST
       
   508 
       
   509 #define TEST "searchHappening"
       
   510     contextTester->add(TEST,
       
   511                        context,
       
   512                        &MultiFieldContext::testSearchForHappening,
       
   513                        TEST);
       
   514 #undef TEST
       
   515 
       
   516 #define TEST "searchLook"
       
   517     contextTester->add(TEST,
       
   518                        context,
       
   519                        &MultiFieldContext::testSearchForLook,
       
   520                        TEST);
       
   521 #undef TEST
       
   522 
       
   523 #define TEST "searchChristmas"
       
   524     contextTester->add(TEST,
       
   525                        context,
       
   526                        &MultiFieldContext::testSearchForChristmas,
       
   527                        TEST);
       
   528 #undef TEST
       
   529 
       
   530 
       
   531     // TODO add more tests to suite
       
   532 
       
   533     return contextTester;    
       
   534 }