searcher/tsrc/cpixsearchertest/src/cpixboostertester.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 "cpixboostertester.h"
       
    18 #include "ccpixsearcher.h"
       
    19 #include "CSearchDocument.h"
       
    20 #include "CCPixIndexer.h"
       
    21 #include "indevicecfg.h"
       
    22 
       
    23 _LIT( KTestBaseAppClass, "test" );
       
    24 _LIT( KTestQualifiedBaseAppClass, "@c:test" );
       
    25 _LIT( KContentFieldName, "content" ); 
       
    26 
       
    27 void CTestBoost::setUp()
       
    28     {
       
    29     User::LeaveIfError(iSession.Connect());
       
    30     iSession.DefineVolume(KTestQualifiedBaseAppClass, KTestBaseAppClass );
       
    31 
       
    32     iIndexer = CCPixIndexer::NewL(iSession);
       
    33     iIndexer->OpenDatabaseL(KTestQualifiedBaseAppClass);
       
    34 
       
    35     iSearcher = CCPixSearcher::NewL(iSession, _L( "" CONTENTS_FIELD ) );
       
    36     iSearcher->OpenDatabaseL(KTestBaseAppClass);
       
    37     }
       
    38 
       
    39 void CTestBoost::tearDown()
       
    40     {
       
    41     delete iIndexer;
       
    42     delete iSearcher;
       
    43     iSession.Close();
       
    44     }
       
    45 
       
    46 void CTestBoost::testNoBoost()
       
    47     {
       
    48     // 
       
    49     // This is the 'control' test. It is used to assert that 
       
    50     // without tinkering with boosts, the engine would return 
       
    51     // the result with most 'hits' 
       
    52     //
       
    53     iIndexer->ResetL(); 
       
    54 
       
    55     AddDocumentL( 0, _L( "hit hit hit" ), 1.0f );  // first result
       
    56     AddDocumentL( 1, _L( "hit hit miss" ), 1.0f ); // second result
       
    57     AddDocumentL( 2, _L( "hit miss miss" ), 1.0f );// last result
       
    58 
       
    59     if( iSearcher->SearchL( _L( "hit" ), KContentFieldName ) != 3 )
       
    60         User::Leave( KErrNotFound );
       
    61     
       
    62     AssertHitDocumentL( 0, 0 );  // id 1 is first hit
       
    63     AssertHitDocumentL( 1, 1 );  // id 2 is second hit
       
    64     AssertHitDocumentL( 2, 2 );  // id 3 is third hit
       
    65     }
       
    66 
       
    67 
       
    68 void CTestBoost::testBoost() 
       
    69     {
       
    70     //
       
    71     // This is the actual boost test. It is used to show that 
       
    72     // changing boost value actually affects the rank. The 'normal'
       
    73     // document order is reversed with heavy boost usage. 
       
    74     //
       
    75     iIndexer->ResetL(); 
       
    76     
       
    77     AddDocumentL( 0, _L( "hit hit hit" ), 1.0f );    // last result
       
    78     AddDocumentL( 1, _L( "hit hit miss" ), 10.0f );  // second result
       
    79     AddDocumentL( 2, _L( "hit miss miss" ), 100.0f );// first result
       
    80     
       
    81     if( iSearcher->SearchL( _L( "hit" ), KContentFieldName ) != 3 )
       
    82         User::Leave( KErrNotFound );
       
    83     
       
    84     AssertHitDocumentL( 0, 2 ); // id 1 is third hit
       
    85     AssertHitDocumentL( 1, 1 ); // id 2 is second hit
       
    86     AssertHitDocumentL( 2, 0 ); // id 3 is first hit
       
    87     }
       
    88 
       
    89 void CTestBoost::testFieldBoost() 
       
    90     {
       
    91     //
       
    92     // This is the actual boost test. It is used to show that 
       
    93     // changing boost value actually affects the rank. The 'normal'
       
    94     // document order is reversed with heavy boost usage. 
       
    95     //
       
    96     iIndexer->ResetL(); 
       
    97     
       
    98     AddDocumentWithFieldBoostL( 0, _L( "hit hit hit" ), 1.0f );      // last result
       
    99     AddDocumentWithFieldBoostL( 1, _L( "hit hit miss" ), 10.0f );  // second result
       
   100     AddDocumentWithFieldBoostL( 2, _L( "hit miss miss" ), 100.0f );// first result
       
   101     
       
   102     if( iSearcher->SearchL( _L( "hit" ), KContentFieldName ) != 3 )
       
   103         User::Leave( KErrNotFound );
       
   104     
       
   105     AssertHitDocumentL( 0, 2 ); // id 1 is third hit
       
   106     AssertHitDocumentL( 1, 1 ); // id 2 is second hit
       
   107     AssertHitDocumentL( 2, 0 ); // id 3 is first hit
       
   108     }
       
   109 
       
   110 void CTestBoost::AssertHitDocumentL( TInt aIndex, TInt aId )
       
   111     {
       
   112     TBuf<32> buf;
       
   113     TInt err = KErrNone;
       
   114     buf.AppendNum( aId );
       
   115     CSearchDocument* doc = iSearcher->GetDocumentL( aIndex ); 
       
   116     CleanupStack::PushL( doc ); 
       
   117     if( doc->Id().Compare( buf ) )
       
   118         err = KErrNotFound;
       
   119     CleanupStack::PopAndDestroy( doc );
       
   120     User::LeaveIfError( err );
       
   121     }
       
   122 
       
   123 void CTestBoost::AddDocumentL( TInt aId, const TDesC& aContent, TReal aBoost ) 
       
   124     {
       
   125     TBuf<32> buf; 
       
   126     buf.AppendNum( aId );
       
   127     
       
   128     CSearchDocument* doc = CSearchDocument::NewLC( buf, KTestBaseAppClass, aContent ); 
       
   129     
       
   130     doc->SetBoost( aBoost ); 
       
   131     doc->AddFieldL( KContentFieldName, aContent, CDocumentField::EStoreNo | CDocumentField::EIndexTokenized | CDocumentField::EAggregateYes );
       
   132     
       
   133     iIndexer->AddL( *doc ); 
       
   134     iIndexer->FlushL();
       
   135     
       
   136     CleanupStack::PopAndDestroy( doc ); 
       
   137     }
       
   138 
       
   139 void CTestBoost::AddDocumentWithFieldBoostL( TInt aId, const TDesC& aContent, TReal aBoost ) 
       
   140     {
       
   141     TBuf<32> buf; 
       
   142     buf.AppendNum( aId );
       
   143     
       
   144     CSearchDocument* doc = CSearchDocument::NewLC( buf, KTestBaseAppClass, aContent ); 
       
   145     
       
   146     doc->AddFieldL( KContentFieldName, 
       
   147                     aContent, 
       
   148                     CDocumentField::EStoreNo | CDocumentField::EIndexTokenized | CDocumentField::EAggregateYes )
       
   149         .SetBoost( aBoost ); // Boost field instead of document
       
   150     
       
   151     iIndexer->AddL( *doc );
       
   152     iIndexer->FlushL();
       
   153     
       
   154     CleanupStack::PopAndDestroy( doc ); 
       
   155     }