searchengine/cpix/tsrc/cpixunittest/src/whiteboxtests.cpp
changeset 24 65456528cac2
parent 14 8bd192d47aaa
equal deleted inserted replaced
23:d4d56f5e7c55 24:65456528cac2
    40 
    40 
    41 void TestBaseAppClassCollision(Itk::TestMgr * testMgr)
    41 void TestBaseAppClassCollision(Itk::TestMgr * testMgr)
    42 {
    42 {
    43     cpix_Result
    43     cpix_Result
    44         result;
    44         result;
    45     char *xml_file = (char*)__FUNCTION__;
       
    46     assert_failed = 0;
       
    47     cpix_IdxDb_defineVolume(&result,
    45     cpix_IdxDb_defineVolume(&result,
    48                             SMS_QBASEAPPCLASS,
    46                             SMS_QBASEAPPCLASS,
    49                             NULL);
    47                             NULL);
    50     if(!cpix_Succeeded(&result))
    48     if(!cpix_Succeeded(&result))
    51         assert_failed = 1;        
    49   
    52     ITK_ASSERT(testMgr,
    50     ITK_ASSERT(testMgr,
    53                cpix_Succeeded(&result),
    51                cpix_Succeeded(&result),
    54                "Definition of volume (%s, %s) failed.",
    52                "Definition of volume (%s, %s) failed.",
    55                SMS_QBASEAPPCLASS,
    53                SMS_QBASEAPPCLASS,
    56                "<default>");
    54                "<default>");
    57 
    55 
    58     cpix_IdxDb_defineVolume(&result,
    56     cpix_IdxDb_defineVolume(&result,
    59                             SMS_QBASEAPPCLASS,
    57                             SMS_QBASEAPPCLASS,
    60                             NULL);
    58                             NULL);
    61     if(!cpix_Succeeded(&result))
    59     if(!cpix_Succeeded(&result))
    62         assert_failed = 1; 
    60 
    63     ITK_ASSERT(testMgr,
    61     ITK_ASSERT(testMgr,
    64                cpix_Succeeded(&result),
    62                cpix_Succeeded(&result),
    65                "Re-definition of identical volume (%s, %s) failed.",
    63                "Re-definition of identical volume (%s, %s) failed.",
    66                SMS_QBASEAPPCLASS,
    64                SMS_QBASEAPPCLASS,
    67                "<default>");
    65                "<default>");
    70         * dummyPath = "\\dummy\\path";
    68         * dummyPath = "\\dummy\\path";
    71 
    69 
    72     cpix_IdxDb_defineVolume(&result,
    70     cpix_IdxDb_defineVolume(&result,
    73                             SMS_QBASEAPPCLASS,
    71                             SMS_QBASEAPPCLASS,
    74                             dummyPath);
    72                             dummyPath);
    75     if(!cpix_Succeeded(&result))
    73 
    76         assert_failed = 1; 
    74          
    77     ITK_ASSERT(testMgr,
    75     ITK_ASSERT(testMgr,
    78                cpix_Failed(&result),
    76                cpix_Failed(&result),
    79                "Volume (%s, %s) definition should have failed.",
    77                "Volume (%s, %s) definition should have failed.",
    80                SMS_QBASEAPPCLASS,
    78                SMS_QBASEAPPCLASS,
    81                dummyPath);
    79                dummyPath);
    82 
    80 
    83     cpix_IdxDb_undefineVolume("@0:root foo bar");
    81     cpix_IdxDb_undefineVolume("@0:root foo bar");
    84     testResultXml(xml_file);
    82    
    85 }
    83 }
    86 
    84 
    87 
    85 
    88 #define DUMMY_QBASEAPPCLASS "@0:root dummy"
    86 #define DUMMY_QBASEAPPCLASS "@0:root dummy"
    89 #define DUMMY2_QBASEAPPCLASS "@0:root dummy2"
    87 #define DUMMY2_QBASEAPPCLASS "@0:root dummy2"
    92 
    90 
    93 void TestIdxDbPathCollision(Itk::TestMgr * testMgr)
    91 void TestIdxDbPathCollision(Itk::TestMgr * testMgr)
    94 {
    92 {
    95     cpix_Result
    93     cpix_Result
    96         result;
    94         result;
    97     char *xml_file = (char*)__FUNCTION__;
       
    98     assert_failed = 0;
       
    99     cpix_IdxDb_defineVolume(&result,
    95     cpix_IdxDb_defineVolume(&result,
   100                       DUMMY_QBASEAPPCLASS,
    96                       DUMMY_QBASEAPPCLASS,
   101                       DUMMY_IDXDBPATH);
    97                       DUMMY_IDXDBPATH);
   102     if(!cpix_Succeeded(&result))
    98 
   103         assert_failed = 1;
    99         
   104     ITK_ASSERT(testMgr,
   100     ITK_ASSERT(testMgr,
   105                cpix_Succeeded(&result),
   101                cpix_Succeeded(&result),
   106                "Defining volume %s, %s failed.",
   102                "Defining volume %s, %s failed.",
   107                DUMMY_QBASEAPPCLASS,
   103                DUMMY_QBASEAPPCLASS,
   108                DUMMY_IDXDBPATH);
   104                DUMMY_IDXDBPATH);
   109 
   105 
   110     cpix_IdxDb_defineVolume(&result,
   106     cpix_IdxDb_defineVolume(&result,
   111                       DUMMY2_QBASEAPPCLASS,
   107                       DUMMY2_QBASEAPPCLASS,
   112                       DUMMY_IDXDBPATH);
   108                       DUMMY_IDXDBPATH);
   113     if(!cpix_Succeeded(&result))
   109 
   114         assert_failed = 1;
   110         
   115     ITK_ASSERT(testMgr,
   111     ITK_ASSERT(testMgr,
   116                cpix_Failed(&result),
   112                cpix_Failed(&result),
   117                "Defining volume %s, %s should have failed.",
   113                "Defining volume %s, %s should have failed.",
   118                DUMMY2_QBASEAPPCLASS,
   114                DUMMY2_QBASEAPPCLASS,
   119                DUMMY_IDXDBPATH);
   115                DUMMY_IDXDBPATH);
   120     testResultXml(xml_file);
   116    
   121 }
   117 }
   122 
   118 
   123 
   119 
   124 
   120 
   125 void TestScrapAll(Itk::TestMgr * testMgr)
   121 void TestScrapAll(Itk::TestMgr * testMgr)
   126 {
   122 {
   127     cpix_Result
   123     cpix_Result
   128         result;
   124         result;
   129     char *xml_file = (char*)__FUNCTION__;
       
   130     assert_failed = 0;
       
   131 
   125 
   132     cpix_IdxDb_defineVolume(&result,
   126     cpix_IdxDb_defineVolume(&result,
   133                             SMS_QBASEAPPCLASS,
   127                             SMS_QBASEAPPCLASS,
   134                             NULL);
   128                             NULL);
   135     if (cpix_Succeeded(&result))
   129     if (cpix_Succeeded(&result))
   139                                             SMS_QBASEAPPCLASS,
   133                                             SMS_QBASEAPPCLASS,
   140                                             cpix_IDX_CREATE);
   134                                             cpix_IDX_CREATE);
   141 
   135 
   142             if (cpix_Succeeded(&result))
   136             if (cpix_Succeeded(&result))
   143                 {
   137                 {
   144             assert_failed = 1;
   138             
   145                     cpix_IdxDb_releaseDb(idxDb);
   139                     cpix_IdxDb_releaseDb(idxDb);
   146                 }
   140                 }
   147         }
   141         }
   148 
   142 
   149     ITK_ASSERT(testMgr,
   143     ITK_ASSERT(testMgr,
   161     
   155     
   162     if (cpix_Succeeded(&result))
   156     if (cpix_Succeeded(&result))
   163         {
   157         {
   164             cpix_IdxDb_releaseDb(idxDb);
   158             cpix_IdxDb_releaseDb(idxDb);
   165         }
   159         }
   166     else
   160 
   167         {
       
   168             assert_failed = 1;
       
   169         }
       
   170 
   161 
   171     ITK_ASSERT(testMgr,
   162     ITK_ASSERT(testMgr,
   172                cpix_Failed(&result),
   163                cpix_Failed(&result),
   173                "Opening IdxDb(%s) should have failed now.",
   164                "Opening IdxDb(%s) should have failed now.",
   174                SMS_QBASEAPPCLASS);
   165                SMS_QBASEAPPCLASS);
   175     testResultXml(xml_file);
   166    
   176 }
   167 }
   177 
   168 
   178 const wchar_t * QryStrings[] = {
   169 const wchar_t * QryStrings[] = {
   179 
   170 
   180     // should pass:
   171     // should pass:
   201 
   192 
   202 
   193 
   203 void TestUnifiedSearchParse(Itk::TestMgr * )
   194 void TestUnifiedSearchParse(Itk::TestMgr * )
   204 {
   195 {
   205     using namespace std;
   196     using namespace std;
   206     char *xml_file = (char*)__FUNCTION__;
       
   207     assert_failed = 0;
       
   208     printf("Whitebox testing parsing of unified search syntax\n\n");
   197     printf("Whitebox testing parsing of unified search syntax\n\n");
   209 
   198 
   210     const wchar_t
   199     const wchar_t
   211         ** p = QryStrings;
   200         ** p = QryStrings;
   212 
   201 
   235 
   224 
   236                 printf("\to inner qry: '%S'\n",
   225                 printf("\to inner qry: '%S'\n",
   237                        qc.innerQryStr_.c_str());
   226                        qc.innerQryStr_.c_str());
   238                 
   227                 
   239             } catch (CpixExc & cpixExc) {
   228             } catch (CpixExc & cpixExc) {
   240                 assert_failed = 1;
   229                 
   241                 printf("Failed to parse: %S\n",
   230                 printf("Failed to parse: %S\n",
   242                        cpixExc.wWhat());
   231                        cpixExc.wWhat());
   243             } catch (...) {
   232             } catch (...) {
   244                 assert_failed = 1;
       
   245                 printf("Failed t parse: for unknown reasons\n");
   233                 printf("Failed t parse: for unknown reasons\n");
   246             }
   234             }
   247         }
   235         }
   248     testResultXml(xml_file);
   236    
   249 }
   237 }
   250 
   238 
   251 
   239 
   252 class PageContext : public Itk::ITestContext
   240 class PageContext : public Itk::ITestContext
   253 {
   241 {
   332     }
   320     }
   333 
   321 
   334 
   322 
   335     void testAddSome(Itk::TestMgr  * mgr)
   323     void testAddSome(Itk::TestMgr  * mgr)
   336     {
   324     {
   337         char *xml_file = (char*)__FUNCTION__;
       
   338         assert_failed = 0;
       
   339         std::wstring
   325         std::wstring
   340             firstBody,
   326             firstBody,
   341             secondBody;
   327             secondBody;
   342 
   328 
   343         for (int i = 0; i < expectedHitCount_; ++i)
   329         for (int i = 0; i < expectedHitCount_; ++i)
   360                         ITK_DBGMSG(mgr,
   346                         ITK_DBGMSG(mgr,
   361                                    ".");
   347                                    ".");
   362                     }
   348                     }
   363             }
   349             }
   364         util_->flush();
   350         util_->flush();
   365         testResultXml(xml_file);
   351        
   366     }
   352     }
   367 
   353 
   368 
   354 
   369     void testSearchPages(Itk::TestMgr * mgr)
   355     void testSearchPages(Itk::TestMgr * mgr)
   370     {
   356     {
   371         cpix_Hits
   357         cpix_Hits
   372             * hits = cpix_IdxDb_search(util_->idxDb(),
   358             * hits = cpix_IdxDb_search(util_->idxDb(),
   373                                        query_);
   359                                        query_);
   374         char *xml_file = (char*)__FUNCTION__;
       
   375         assert_failed = 0;
       
   376         if (cpix_Failed(util_->idxDb()))
   360         if (cpix_Failed(util_->idxDb()))
   377             {
   361             {
   378                 ITK_EXPECT(mgr,
   362                 ITK_EXPECT(mgr,
   379                            false,
   363                            false,
   380                            "Failed to search index");
   364                            "Failed to search index");
   381                 cpix_ClearError(util_->idxDb());
   365                 cpix_ClearError(util_->idxDb());
   382                 assert_failed = 1;
   366                 
   383             }
   367             }
   384         else
   368         else
   385             {
   369             {
   386                 int32_t
   370                 int32_t
   387                     hitsLength = cpix_Hits_length(hits);
   371                     hitsLength = cpix_Hits_length(hits);
   389                 ITK_EXPECT(mgr,
   373                 ITK_EXPECT(mgr,
   390                            expectedHitCount_ == hitsLength,
   374                            expectedHitCount_ == hitsLength,
   391                            "Did not get expected hitcounts (%d), but %d",
   375                            "Did not get expected hitcounts (%d), but %d",
   392                            expectedHitCount_,
   376                            expectedHitCount_,
   393                            hitsLength);
   377                            hitsLength);
   394                 if(expectedHitCount_ != hitsLength)
   378 
   395                     {
       
   396                     assert_failed = 1;
       
   397                     }
       
   398                 
   379                 
   399                 printf("Got %d hits\n",
   380                 printf("Got %d hits\n",
   400                        hitsLength);
   381                        hitsLength);
   401 
   382 
   402                 search(hits,
   383                 search(hits,
   420                        mgr);
   401                        mgr);
   421 
   402 
   422             }
   403             }
   423         
   404         
   424         cpix_Hits_destroy( hits );  
   405         cpix_Hits_destroy( hits );  
   425         testResultXml(xml_file);
   406        
   426     }
   407     }
   427 
   408 
   428 
   409 
   429 private:
   410 private:
   430     //
   411     //
  1023 
  1004 
  1024     void testStartStage(Itk::TestMgr * mgr)
  1005     void testStartStage(Itk::TestMgr * mgr)
  1025     {
  1006     {
  1026         using namespace lucene::index;
  1007         using namespace lucene::index;
  1027         using namespace std;
  1008         using namespace std;
  1028         char *xml_file = (char*)__FUNCTION__;
       
  1029           assert_failed = 0;
       
  1030         printf("Start state of index:\n");
  1009         printf("Start state of index:\n");
  1031 
  1010 
  1032         {
  1011         {
  1033             auto_ptr<IndexReader>
  1012             auto_ptr<IndexReader>
  1034                 reader(IndexReader::open(deltaIdxDbPath_.c_str()));
  1013                 reader(IndexReader::open(deltaIdxDbPath_.c_str()));
  1049 
  1028 
  1050         if (haveDels_)
  1029         if (haveDels_)
  1051             {
  1030             {
  1052                 if (!reader_.isOpen())
  1031                 if (!reader_.isOpen())
  1053                     {
  1032                     {
  1054                 assert_failed = 1;
  1033                 
  1055                         ITK_PANIC("Reader should be open");
  1034                         ITK_PANIC("Reader should be open");
  1056                     }
  1035                     }
  1057 
  1036 
  1058                 for (size_t i = 0; 
  1037                 for (size_t i = 0; 
  1059                      i < sizeof(DeltaSmsesToDelete) / sizeof(wchar_t*);
  1038                      i < sizeof(DeltaSmsesToDelete) / sizeof(wchar_t*);
  1068                         
  1047                         
  1069                         ITK_EXPECT(mgr,
  1048                         ITK_EXPECT(mgr,
  1070                                    result == 1,
  1049                                    result == 1,
  1071                                    "Should have deleted exactly one doc");
  1050                                    "Should have deleted exactly one doc");
  1072                         if(result != 0)
  1051                         if(result != 0)
  1073                             assert_failed = 1;
  1052                             
  1074                         printf("... deleted doc %S\n",
  1053                         printf("... deleted doc %S\n",
  1075                                DeltaSmsesToDelete[i]);
  1054                                DeltaSmsesToDelete[i]);
  1076                     }
  1055                     }
  1077             }
  1056             }
  1078         testResultXml(xml_file);
  1057        
  1079     }
  1058     }
  1080 
  1059 
  1081 
  1060 
  1082     
  1061     
  1083     struct TestCommitStageExc
  1062     struct TestCommitStageExc
  1123 
  1102 
  1124     void testCommitToDisk(Itk::TestMgr * mgr)
  1103     void testCommitToDisk(Itk::TestMgr * mgr)
  1125     {
  1104     {
  1126         using namespace lucene::search;
  1105         using namespace lucene::search;
  1127         using namespace Cpix::Impl;
  1106         using namespace Cpix::Impl;
  1128         char *xml_file = (char*)__FUNCTION__;
       
  1129           assert_failed = 0;
       
  1130         // The TestCommitStageAction instance we give is to emulate
  1107         // The TestCommitStageAction instance we give is to emulate
  1131         // interrupting the committing-to-disk process at different
  1108         // interrupting the committing-to-disk process at different
  1132         // stages, and see if we can recover whatever information can
  1109         // stages, and see if we can recover whatever information can
  1133         // be from whatever has been committed to disk up to that
  1110         // be from whatever has been committed to disk up to that
  1134         // point. The interrupt is done by throwing a test exception
  1111         // point. The interrupt is done by throwing a test exception
  1166                                 ITK_EXPECT(mgr,
  1143                                 ITK_EXPECT(mgr,
  1167                                            Cpt::directoryexists(deltaIdxDbPath.c_str()),
  1144                                            Cpt::directoryexists(deltaIdxDbPath.c_str()),
  1168                                            "Complete, committed, updated idx dir should exist (%s): %s",
  1145                                            "Complete, committed, updated idx dir should exist (%s): %s",
  1169                                            test_.c_str(),
  1146                                            test_.c_str(),
  1170                                            deltaIdxDbPath.c_str());
  1147                                            deltaIdxDbPath.c_str());
  1171                                 if(!Cpt::directoryexists(deltaIdxDbPath.c_str()))
  1148 
  1172                                     {
       
  1173                                     assert_failed = 1;
       
  1174                                     }
       
  1175                             }
  1149                             }
  1176                     }
  1150                     }
  1177                 else
  1151                 else
  1178                     {
  1152                     {
  1179                 assert_failed = 1;
  1153                 
  1180                         ITK_EXPECT(mgr,
  1154                         ITK_EXPECT(mgr,
  1181                                    false,
  1155                                    false,
  1182                                    "Should have been interrupted at stage %d",
  1156                                    "Should have been interrupted at stage %d",
  1183                                    targetCommitStage_);
  1157                                    targetCommitStage_);
  1184                     }
  1158                     }
  1185             }
  1159             }
  1186         catch (TestCommitStageExc & exc)
  1160         catch (TestCommitStageExc & exc)
  1187             {
  1161             {
  1188         assert_failed = 1;
  1162         
  1189                 if (exc.commitStage_ == targetCommitStage_)
  1163                 if (exc.commitStage_ == targetCommitStage_)
  1190                     {
  1164                     {
  1191                         properlyInterrupted = true;
  1165                         properlyInterrupted = true;
  1192                     }
  1166                     }
  1193             }
  1167             }
  1203         ITK_EXPECT(mgr,
  1177         ITK_EXPECT(mgr,
  1204                    (insertBuf_ == NULL 
  1178                    (insertBuf_ == NULL 
  1205                     || insertBuf_->isEmpty()
  1179                     || insertBuf_->isEmpty()
  1206                     || targetCommitStage_ < CS_IBUF_COMMITTING),
  1180                     || targetCommitStage_ < CS_IBUF_COMMITTING),
  1207                    "Expected the (test) insert buffer to be closed");
  1181                    "Expected the (test) insert buffer to be closed");
  1208         if(!properlyInterrupted || (haveReader_ || reader_.isOpen() ) ||  !((insertBuf_ == NULL 
  1182   
  1209                 || insertBuf_->isEmpty() || targetCommitStage_ < CS_IBUF_COMMITTING)))
  1183        
  1210             {
       
  1211             assert_failed = 1;
       
  1212             }
       
  1213         testResultXml(xml_file);
       
  1214     }
  1184     }
  1215 
  1185 
  1216 
  1186 
  1217     void testRecoveredStage(Itk::TestMgr * )
  1187     void testRecoveredStage(Itk::TestMgr * )
  1218     {
  1188     {
  1219         char *xml_file = (char*)__FUNCTION__;
       
  1220           assert_failed = 0;
       
  1221         if (reader_.isOpen())
  1189         if (reader_.isOpen())
  1222             {
  1190             {
  1223                 printf("Reader (either the original or the re-created):\n");
  1191                 printf("Reader (either the original or the re-created):\n");
  1224                 searchAndPrint(reader_.getReader());
  1192                 searchAndPrint(reader_.getReader());
  1225                 reader_.close(NULL);
  1193                 reader_.close(NULL);
  1232                                               &reader_);
  1200                                               &reader_);
  1233 
  1201 
  1234         printf("The recovered state of index:\n");
  1202         printf("The recovered state of index:\n");
  1235 
  1203 
  1236         searchAndPrint(reader_.getReader());
  1204         searchAndPrint(reader_.getReader());
  1237         testResultXml(xml_file);
  1205        
  1238     }
  1206     }
  1239 
  1207 
  1240 
  1208 
  1241 private:
  1209 private:
  1242     void cleanup()
  1210     void cleanup()