searchengine/cpix/tsrc/cpixunittest/src/querytest.cpp
author Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
Mon, 03 May 2010 13:33:22 +0300
changeset 1 6f2c1c46032b
child 2 6c1a2771f4b7
permissions -rw-r--r--
Revision: 201015 Kit: 201018

/*
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: 
*
*/


#include <wchar.h>
#include <stddef.h>

#include <iostream>
#include <sstream>
#include "indevicecfg.h"

#include "cpixidxdb.h"

#include "itk.h"
//#include "xmllog.h"
#include "config.h"
#include "testutils.h"
#include "suggestion.h"


const char * docsToIndex[5] = {
CORPUS_PATH "\\query\\query1.txt",
CORPUS_PATH "\\query\\query2.txt",
CORPUS_PATH "\\query\\query3.txt",
CORPUS_PATH "\\query\\query4.txt",
NULL
};

void setupPlainQuery(Itk::TestMgr * testMgr)
    {
    cpix_Result
            result;

        cpix_IdxDb_dbgScrapAll(&result);
        ITK_ASSERT(testMgr,
                       cpix_Succeeded(&result),
                       "Could not get rid of all test qbac-idx pairs");
        std::auto_ptr<FileIdxUtil> util( new FileIdxUtil ); 
        util->init(true); 
        cpix_Analyzer* analyzer = cpix_Analyzer_create(&result, L"standard"); 
        if ( cpix_Failed( &result) ) ITK_PANIC("Analyzer could not be created");
        for (int i = 0; docsToIndex[i]; i++) 
            {
        util->indexFile( docsToIndex[i], analyzer, testMgr );
            }
        util->flush();
        cpix_Analyzer_destroy(analyzer);
    }

void setupPrefixQuery(Itk::TestMgr * testMgr)
    {
    cpix_Result
            result;

        cpix_IdxDb_dbgScrapAll(&result);
        ITK_ASSERT(testMgr,
                       cpix_Succeeded(&result),
                       "Could not get rid of all test qbac-idx pairs");
        std::auto_ptr<FileIdxUtil> util( new FileIdxUtil ); 
        util->init(true); 
        cpix_Analyzer* analyzer = cpix_Analyzer_create(&result, L"standard"); 
        if ( cpix_Failed( &result) ) ITK_PANIC("Analyzer could not be created");
       
        util->indexFile( CORPUS_PATH "\\query\\query5.txt", analyzer, testMgr );
       
        util->flush();
        util->indexFile( CORPUS_PATH "\\query\\query6.txt", analyzer, testMgr );
               
        util->flush();
        cpix_Analyzer_destroy(analyzer);
    }

void testQuery(Itk::TestMgr * testMgr, const wchar_t *qryStr, int hitLen)
{
cpix_Result
        result;
int32_t hitsLength  = 0;
   
    std::auto_ptr<FileIdxUtil> util( new FileIdxUtil ); 
    
    util->init(false); 
    cpix_Analyzer* analyzer = cpix_Analyzer_create(&result, L"standard"); 
    if ( cpix_Failed( &result) ) ITK_PANIC("Analyzer could not be created");
        cpix_QueryParser
            * queryParser = cpix_QueryParser_create(&result,
                                                    LCPIX_DEFAULT_FIELD,
                                                    analyzer );
        if (queryParser == NULL)
            {
                cpix_Analyzer_destroy( analyzer );
                ITK_PANIC("Could not create query parser");
            }
        

    
        cpix_Query* query = cpix_QueryParser_parse(queryParser,
                                                   qryStr);
        if (cpix_Failed(queryParser)
            || query == NULL)
            {
                cpix_Analyzer_destroy(analyzer);
                cpix_ClearError(queryParser);
                cpix_QueryParser_destroy(queryParser);
                ITK_PANIC("Could not parse query string");
            }
        cpix_QueryParser_destroy(queryParser);

        cpix_Hits
            * hits = cpix_IdxDb_search(util->idxDb(),
                                       query );
        
        if (cpix_Succeeded(hits))
                {
                hitsLength = cpix_Hits_length(hits);
                cpix_Hits_destroy( hits );
                }
                if(hitsLength == hitLen )
                    {
                   
                    ITK_MSG(testMgr, "Query %S, returned %d hits. Passed \n",qryStr,hitsLength );
                    }
                else
                    {
                   
                    ITK_MSG(testMgr, "Query %S, didnt return expected hits. Expected is %d hits. Returned is %d. Failed \n",qryStr,hitLen,hitsLength);
                    }
                    
        cpix_Query_destroy( query ); 
        
        cpix_Analyzer_destroy(analyzer);
}



void CreatePlainQueryTest(Itk::TestMgr * testMgr) 
{
    bool val = true;
    setupPlainQuery(testMgr);
    testQuery(testMgr,L"Nokia", 2);
    testQuery(testMgr,L"iNdia", 1);
    testQuery(testMgr,L"\"London Finland\"", 1);
    testQuery(testMgr,L"Contents:Nokia", 2);
    testQuery(testMgr,L"Contents:Nokia AND country", 1);
    testQuery(testMgr,L"Contents:Nokia && country", 1);
    testQuery(testMgr,L"Nokia Selvaraj", 3);
    testQuery(testMgr,L"Lo?don", 1);
    testQuery(testMgr,L"countr?", 1);
    testQuery(testMgr,L"country?", 0);
    testQuery(testMgr,L"?india", 0);
    testQuery(testMgr,L"nok*", 2);
    testQuery(testMgr,L"count?ry", 0);
    testQuery(testMgr,L"roam~", 2);
    testQuery(testMgr,L"ro~am", 0);
    testQuery(testMgr,L"\"london country\"~10", 1);
    testQuery(testMgr,L"\"nokia country\"~2", 0);
    testQuery(testMgr,L"nokia basker", 3);
    testQuery(testMgr,L"Nokia^5 basker", 3);
    testQuery(testMgr,L"Nokia basker^5", 3);
    testQuery(testMgr,L"Nokia || basker", 3);
    testQuery(testMgr,L"Nokia OR basker", 3);
    testQuery(testMgr,L"Nokia AND basker", 0);
    testQuery(testMgr,L"Nokia && basker", 0);
    testQuery(testMgr,L"+nokia country", 2);
    testQuery(testMgr,L"+nokia roam", 2);
    testQuery(testMgr,L"Nokia !country", 1);
    testQuery(testMgr,L"nokia NOT country", 1);
    testQuery(testMgr,L"nokia NOT basker", 2);
    testQuery(testMgr,L"NOT India", 1);
    testQuery(testMgr,L"(india OR Mobile) AND Nokia", 2);
    testQuery(testMgr,L"(india OR Mobile) AND Country", 1);
  //  create_xml(val,__func__,__FILE__,__LINE__);
}

void CreatePrefixQueryTest(Itk::TestMgr * testMgr) 
{
    bool val = true;
    setupPrefixQuery(testMgr);
    testQuery(testMgr,L"$prefix(\"new-notes\")", 1);
    testQuery(testMgr,L"$prefix(\"notes\")", 1);
    testQuery(testMgr,L"$prefix(\"new\")", 1);
    testQuery(testMgr,L"$prefix(\"-india\")", 1);
    testQuery(testMgr,L"$prefix(\"tamil-nadu\")", 1);
    testQuery(testMgr,L"$prefix(\"testing\")", 2);
    testQuery(testMgr,L"$prefix(\"*shankar\")", 2);
    testQuery(testMgr,L"$prefix(\"Ani*rban\")", 1);
    testQuery(testMgr,L"$prefix(\"kumar*\")", 1);
    testQuery(testMgr,L"$prefix(\"carrot^\")", 1);
    testQuery(testMgr,L"$prefix(\"carrot\")", 1);
    testQuery(testMgr,L"$prefix(\"ani\")", 1);
    testQuery(testMgr,L"$prefix(\"question\")", 1);
    testQuery(testMgr,L"$prefix(\"question?\")", 1);
    testQuery(testMgr,L"$prefix(\"|pipe\")", 1);
    testQuery(testMgr,L"$prefix(\"&&ambersend\")", 1);
    testQuery(testMgr,L"$prefix(\"!=Exclamation\")", 1);
    testQuery(testMgr,L"$prefix(\":colon\")", 1);
    testQuery(testMgr,L"$prefix(\"http:\\www.nokia.com\")", 1);
    testQuery(testMgr,L"$prefix(\"%percentage\")", 1);
    testQuery(testMgr,L"$prefix(\"(testing)\")", 2);
    testQuery(testMgr,L"$prefix(\"mail-id\")", 1);
    testQuery(testMgr,L"$prefix(\"mail id\")", 1);
    testQuery(testMgr,L"$prefix(\"shankar.rajendran@nokia.com\")", 1);
    testQuery(testMgr,L"$prefix(\"~tild\")", 1);
    testQuery(testMgr,L"$prefix(\"shankar\")", 2);
    testQuery(testMgr,L"$prefix(\"`singlequote\")", 1);
    testQuery(testMgr,L"$prefix(\"singlequote\")", 1);
    testQuery(testMgr,L"$prefix(\"\"doublequote\")", 1);
    testQuery(testMgr,L"$prefix(\"doublequote\")", 1);
    testQuery(testMgr,L"$prefix(\";semicolon\")", 1);
    testQuery(testMgr,L"$prefix(\"/slash\")", 1);
    testQuery(testMgr,L"$prefix(\"slash\")", 1);
    testQuery(testMgr,L"$prefix(\"\\backslash\")", 1);
    testQuery(testMgr,L"$prefix(\"backslash\")", 1);
    testQuery(testMgr,L"$prefix(\"[squarebracket]\")", 1);
    testQuery(testMgr,L"$prefix(\"{flowerbracket}\")", 1);
    testQuery(testMgr,L"$prefix(\"<lessthan\")", 1);
    testQuery(testMgr,L"$prefix(\">greaterthan\")", 1);
    testQuery(testMgr,L"$prefix(\"worked for motorola .\")", 1);
    
}

Itk::TesterBase * CreateQueryTests()
{
    using namespace Itk;

    ContextTester
        * qryTests = new ContextTester("Query Tests", NULL);

    qryTests->add("PlainQueryTest", &CreatePlainQueryTest);
    qryTests->add("PrefixQueryTest", &CreatePrefixQueryTest);
    
    return qryTests;
}