searchengine/cpix/tsrc/cpixunittest/src/utf8.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 <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 
       
    29 const char * Utf8DocsToIndex[5] = {
       
    30     FILE_TEST_CORPUS_PATH "\\fi\\1.txt",
       
    31     FILE_TEST_CORPUS_PATH "\\fi\\2.txt",
       
    32     FILE_TEST_CORPUS_PATH "\\fi\\3.txt",
       
    33     FILE_TEST_CORPUS_PATH "\\fi\\4.txt",
       
    34     NULL
       
    35 };
       
    36 
       
    37 const wchar_t * UcsTermsToSearch[5] = {
       
    38 	L"teetä",
       
    39 	L"näin",
       
    40 	L"hyvä",
       
    41 	L"yötyössä",
       
    42     NULL
       
    43 };
       
    44 
       
    45 void TestUtf8(Itk::TestMgr * testMgr)
       
    46 {
       
    47         cpix_Result
       
    48             result;
       
    49 
       
    50         cpix_IdxDb_dbgScrapAll(&result);
       
    51 
       
    52         ITK_ASSERT(testMgr,
       
    53                    cpix_Succeeded(&result),
       
    54                    "Could not get rid of all test qbac-idx pairs");
       
    55 
       
    56 	std::auto_ptr<FileIdxUtil> util( new FileIdxUtil ); 
       
    57 	
       
    58 	util->init(); 
       
    59 	
       
    60 	cpix_Analyzer* analyzer = cpix_CreateSimpleAnalyzer(&result); 
       
    61 	
       
    62 	if ( cpix_Failed( &result) ) ITK_PANIC("Analyzer could not be created");
       
    63 	
       
    64 	for (int i = 0; Utf8DocsToIndex[i]; i++) 
       
    65 	{
       
    66 		util->indexFile( Utf8DocsToIndex[i], analyzer, testMgr ); 
       
    67 	}
       
    68 
       
    69         util->flush();
       
    70 	
       
    71 	for (int i = 0; UcsTermsToSearch[i]; i++) 
       
    72 	{
       
    73 		cpix_QueryParser
       
    74 			* queryParser = cpix_QueryParser_create(&result,
       
    75 													CONTENTS_FIELD,
       
    76 													analyzer );
       
    77 		if (queryParser == NULL)
       
    78 			{
       
    79 				cpix_Analyzer_destroy( analyzer );
       
    80 				ITK_PANIC("Could not create query parser");
       
    81 			}
       
    82 	
       
    83 		cpix_Query* query = cpix_QueryParser_parse(queryParser,
       
    84 												   UcsTermsToSearch[i]);
       
    85 		if (cpix_Failed(queryParser)
       
    86 			|| query == NULL)
       
    87 			{
       
    88 				cpix_Analyzer_destroy(analyzer);
       
    89 				cpix_ClearError(queryParser);
       
    90 				cpix_QueryParser_destroy(queryParser);
       
    91 				ITK_PANIC("Could not parse query string");
       
    92 			}
       
    93 		cpix_QueryParser_destroy(queryParser);
       
    94 
       
    95 		cpix_Hits
       
    96 			* hits = cpix_IdxDb_search(util->idxDb(),
       
    97 									   query );
       
    98 		
       
    99 		cpix_Query_destroy( query ); 
       
   100 		
       
   101 		util->printHits( hits, testMgr ); 
       
   102 		
       
   103 		cpix_Hits_destroy( hits ); 
       
   104 	}
       
   105 	
       
   106 	cpix_Analyzer_destroy( analyzer ); 
       
   107 }
       
   108 
       
   109 Itk::TesterBase * CreateUtf8Tests()
       
   110 {
       
   111     using namespace Itk;
       
   112 
       
   113     ContextTester
       
   114         * whiteBox = new ContextTester("utf8",
       
   115                                        NULL); // default context
       
   116 
       
   117     whiteBox->add("utf8",
       
   118                   &TestUtf8,
       
   119                   "utf8");
       
   120 
       
   121     // TODO add more
       
   122 
       
   123     return whiteBox;
       
   124 }