os/persistentdata/persistentstorage/dbms/tdbms/t_dbindex.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <d32dbms.h>
    17 #include <s32file.h>
    18 #include <e32test.h>
    19 #include <e32math.h>
    20 #include "D32TABLE.H"
    21 
    22 // MSVC++ up to 5.0 has problems with expanding inline functions
    23 // This disables the mad warnings for the whole project
    24 #if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
    25 #pragma warning(disable : 4710)			// function not expanded. MSVC 5.0 is stupid
    26 #endif
    27 
    28 LOCAL_D RTest test(_L("t_dbindex - Test DBMS indexing and ordering"));
    29 LOCAL_D CTrapCleanup* TheTrapCleanup;
    30 LOCAL_D RDbs TheDbs;
    31 LOCAL_D RDbNamedDatabase TheDatabase;
    32 LOCAL_D RDbTable TheTable;
    33 LOCAL_D RDbView TheView;
    34 LOCAL_D RFs TheFs;
    35 LOCAL_D TBuf<0x200> TheBuf;
    36 
    37 const TInt KTestCleanupStack=0x20;
    38 const TPtrC KTestDatabase=_L("C:\\DBMS-TST\\T_INDEX.DB");
    39 const TPtrC KTableName(_S("Table"));
    40 const TPtrC KIndexName(_S("index"));
    41 const TPtrC KIndexTwo(_S("index_two"));
    42 const TPtrC KColumnName(_S("column"));
    43 const TPtrC KColumnTwo(_S("column2"));
    44 
    45 const TPtrC KTableName1(_S("Table1"));
    46 const TPtrC KColumnName1(_S("column1"));
    47 const TPtrC KIndexName1(_S("index1"));
    48 
    49 const TPtrC KSelectOrdered(_L("select column from table order by column"));
    50 
    51 #define elementsof(array) (sizeof(array)/sizeof(array[0]))
    52 
    53 void Check(TInt aValue,TInt aExpected,TInt aLine)
    54 	{
    55 	if (aValue!=aExpected)
    56 		{
    57 		test.Printf(_L("*** Expected %d: got %d\r\n"),aExpected,aValue);
    58 		test.operator()(EFalse,aLine);
    59 		}
    60 	}
    61 #define test2(a,b) Check(a,b,__LINE__)
    62 
    63 //
    64 // Open the database (shared access) (SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version)
    65 //
    66 LOCAL_C void OpenDatabase()
    67 	{
    68 	TInt r=TheDatabase.Open(TheDbs,KTestDatabase);
    69 	test (r==KErrNone);
    70 	}
    71 
    72 //
    73 // Create the database (SYMBIAN_REMOVE_TRIVIAL_ENCRYPTION version)
    74 //
    75 LOCAL_C void CreateDatabase()
    76 	{
    77 	TInt r=TheDatabase.Replace(TheFs,KTestDatabase);
    78 	test (r==KErrNone);
    79 	TheDatabase.Close();
    80 	OpenDatabase();
    81 	}
    82 
    83 
    84 LOCAL_C void CloseDatabase()
    85 	{
    86 	TheDatabase.Close();
    87 	}
    88 
    89 /**
    90 @SYMTestCaseID          SYSLIB-DBMS-CT-0618
    91 @SYMTestCaseDesc        Tests for RDbNamedDatabase functionality
    92 @SYMTestPriority        Medium
    93 @SYMTestActions         Tests for RDbNamedDatabase::CreateIndex()
    94 @SYMTestExpectedResults Test must not fail
    95 @SYMREQ                 REQ0000
    96 */
    97 LOCAL_C void TestIndexBuildL()
    98 	{
    99 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0618 Create table "));
   100 	TheDatabase.Begin();
   101 	CDbColSet *cs=CDbColSet::NewLC();
   102 	cs->AddL(TDbCol(KColumnName,EDbColInt32));
   103 	cs->AddL(TDbCol(KColumnTwo,EDbColInt32));
   104 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
   105 	CleanupStack::PopAndDestroy();
   106 	test.Next(_L("create indices"));
   107 	CDbKey *key=CDbKey::NewLC();
   108 	key->AddL(TDbKeyCol(KColumnName));
   109 	key->MakeUnique();
   110 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
   111 	key->Clear();
   112 	key->AddL(TDbKeyCol(KColumnTwo));
   113 	key->MakeUnique();
   114 	test(TheDatabase.CreateIndex(KIndexTwo,KTableName,*key)==KErrNone);
   115 	CleanupStack::PopAndDestroy();
   116 	test.Next(_L("Populate table"));
   117 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   118 	TheTable.InsertL();
   119 	TheTable.SetColL(1,1);
   120 	TheTable.SetColL(2,-1);
   121 	TheTable.PutL();
   122 	TheTable.InsertL();
   123 	TheTable.SetColL(1,2);
   124 	TheTable.SetColL(2,-2);
   125 	TheTable.PutL();
   126 	test(TheDatabase.Commit()==KErrNone);
   127 	test.Next(_L("Check order"));
   128 	test(TheTable.SetNoIndex()==KErrNone);
   129 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   130 	test(TheTable.CountL()==2);
   131 	test(TheTable.NextL());
   132 	TheTable.GetL();
   133 	test(TheTable.ColInt(1)==1);
   134 	test(TheTable.NextL());
   135 	TheTable.GetL();
   136 	test(TheTable.ColInt(1)==2);
   137 	test(!TheTable.NextL());
   138 	test(TheTable.SetIndex(KIndexTwo)==KErrNone);
   139 	test(TheTable.CountL()==2);
   140 	test(TheTable.NextL());
   141 	TheTable.GetL();
   142 	test(TheTable.ColInt(1)==2);
   143 	test(TheTable.NextL());
   144 	TheTable.GetL();
   145 	test(TheTable.ColInt(1)==1);
   146 	test(!TheTable.NextL());
   147  	test(TheTable.SetNoIndex()==KErrNone);
   148 	test(TheTable.CountL()==2);
   149 	test(TheTable.NextL());
   150 	test(TheTable.NextL());
   151 	test(!TheTable.NextL());
   152 	TheTable.Close();
   153 	test.Next(_L("Drop indices"));
   154 	test(TheDatabase.DropIndex(KIndexTwo,KTableName)==KErrNone);
   155 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   156 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   157 	test(TheTable.SetIndex(KIndexTwo)!=KErrNone);
   158 	TheTable.Close();
   159 	test(TheDatabase.DropIndex(KIndexName,KTableName)==KErrNone);
   160 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   161 	test(TheTable.SetIndex(KIndexName)!=KErrNone);
   162 	test(TheTable.SetIndex(KIndexTwo)!=KErrNone);
   163 	TheTable.Close();
   164 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   165 	test.End();
   166 	}
   167 
   168 LOCAL_C TInt CountRowsL()
   169 	{
   170 	TInt count=0;
   171 	while (TheTable.NextL())
   172 		++count;
   173 	return count;
   174 	}
   175 
   176 /**
   177 @SYMTestCaseID          SYSLIB-DBMS-CT-0619
   178 @SYMTestCaseDesc        RDbNamedDatabase::Execute() function test
   179 @SYMTestPriority        Medium
   180 @SYMTestActions         Tests for CREATE TABLE,CREATE INDEX and CREATE UNIQUE INDEX query
   181 @SYMTestExpectedResults Test must not fail
   182 @SYMREQ                 REQ0000
   183 */
   184 LOCAL_C void TestPersistenceL()
   185 	{
   186 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0619 Create table "));
   187 	TheDatabase.Begin();
   188 	test(TheDatabase.Execute(_L("CREATE TABLE Table (column CHAR(120) NOT NULL)"))==KErrNone);
   189 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   190 	TheTable.InsertL();
   191 	TheTable.SetColL(1,_L("a"));
   192 	TheTable.PutL();
   193 	TheTable.InsertL();
   194 	TheTable.SetColL(1,_L("b"));
   195 	TheTable.PutL();
   196 	TheTable.InsertL();
   197 	TheTable.SetColL(1,_L("c"));
   198 	TheTable.PutL();
   199 	TheTable.InsertL();
   200 	TheTable.SetColL(1,_L("d"));
   201 	TheTable.PutL();
   202 	TheTable.InsertL();
   203 	TheTable.SetColL(1,_L("e"));
   204 	TheTable.PutL();
   205 	TheTable.Close();
   206 	test(TheDatabase.Commit()==KErrNone);
   207 	test.Next(_L("Build indices"));
   208 	test (TheDatabase.Execute(_L("CREATE INDEX index ON table (column ASC)"))==KErrNone);
   209 	test (TheDatabase.Execute(_L("CREATE UNIQUE INDEX index_two ON table (column DESC)"))==KErrNone);
   210 	test.Next(_L("Close and re-open database"));
   211 	CloseDatabase();
   212 	OpenDatabase();
   213 	test.Next(_L("Check indices"));
   214 	CDbKey* key=TheDatabase.KeyL(KIndexName,KTableName);
   215 	test (key->Count()==1);
   216 	test (key->Comparison()==EDbCompareNormal);
   217 	test (!key->IsUnique());
   218 	test ((*key)[0].iName.CompareF(KColumnName)==0);
   219 	test ((*key)[0].iOrder==TDbKeyCol::EAsc);
   220 	delete key;
   221 	key=TheDatabase.KeyL(KIndexTwo,KTableName);
   222 	test (key->Count()==1);
   223 	test (key->Comparison()==EDbCompareNormal);
   224 	test (key->IsUnique());
   225 	test ((*key)[0].iName.CompareF(KColumnName)==0);
   226 	test ((*key)[0].iOrder==TDbKeyCol::EDesc);
   227 	delete key;
   228 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   229 	test(TheTable.SetNoIndex()==KErrNone);
   230 	test(CountRowsL()==5);
   231 	test(TheTable.CountL()==5);
   232 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   233 	test(TheTable.CountL()==5);
   234 	test(CountRowsL()==5);
   235 	test(TheTable.SetIndex(KIndexTwo)==KErrNone);
   236 	test(TheTable.CountL()==5);
   237 	test(CountRowsL()==5);
   238 	TheTable.Close();
   239 	test.Next(_L("Drop indices"));
   240 	TheDatabase.Begin();
   241 	test (TheDatabase.Execute(_L("DROP INDEX index_two FROM table"))==KErrNone);
   242 	test (TheDatabase.Execute(_L("DROP INDEX index FROM table"))==KErrNone);
   243 	test (TheDatabase.Execute(_L("DROP TABLE table"))==KErrNone);
   244 	test(TheDatabase.Commit()==KErrNone);
   245 	test.End();
   246 	}
   247 
   248 LOCAL_C void BuildTablePrologL(TDbColType aType,TInt aAttribs=0)
   249 	{
   250 	TheDatabase.Begin();
   251 	CDbColSet *cs=CDbColSet::NewLC();
   252 	TDbCol col(KColumnName,aType);
   253 	col.iAttributes=aAttribs;
   254 	cs->AddL(col);
   255 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
   256 	CleanupStack::PopAndDestroy();
   257 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   258 	}
   259 
   260 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TInt& aValue)
   261 	{aSet.SetColL(col,aValue);}
   262 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TUint& aValue)
   263 	{aSet.SetColL(col,aValue);}
   264 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TInt64& aValue)
   265 	{aSet.SetColL(col,aValue);}
   266 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TReal32& aValue)
   267 	{aSet.SetColL(col,aValue);}
   268 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TReal64& aValue)
   269 	{aSet.SetColL(col,aValue);}
   270 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TTime& aValue)
   271 	{aSet.SetColL(col,aValue);}
   272 inline void SetColL(RDbRowSet& aSet,TDbColNo col,const TPtrC& aValue)
   273 	{aSet.SetColL(col,aValue);}
   274 
   275 template <class T>
   276 void BuildTableL(TDbColType aType,TInt aAttribs,T aValues[],TInt aCount)
   277 	{
   278 	BuildTablePrologL(aType,aAttribs);
   279 	for (TInt ii=0;ii<aCount;++ii)
   280 		{
   281 		TheTable.InsertL();
   282 		SetColL(TheTable,1,aValues[ii]);
   283 		TheTable.PutL();
   284 		}
   285 	test(TheDatabase.Commit()==KErrNone);
   286 	test (TheTable.CountL()==aCount);
   287 	TheTable.Close();
   288 	}
   289 
   290 /**
   291 @SYMTestCaseID          SYSLIB-DBMS-CT-0620
   292 @SYMTestCaseDesc        Setting the specified index as the active index for a table test
   293 @SYMTestPriority        Medium
   294 @SYMTestActions         Create an index and set as active index for the table.
   295                         Tests for RDbNamedDatabase::CreateIndex(),RDbNamedDatabase::Commit()
   296 						RDbTable::Open(),RDbTable::SetIndex() functions.
   297 @SYMTestExpectedResults Test must not fail
   298 @SYMREQ                 REQ0000
   299 */
   300 LOCAL_C void BuildIndexL(TDbTextComparison aComparison=EDbCompareNormal,TInt aLength=KDbUndefinedLength)
   301 	{
   302 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0620 Test index order "));
   303 	TheDatabase.Begin();
   304 	CDbKey *key=CDbKey::NewLC();
   305 	key->AddL(TDbKeyCol(KColumnName,aLength));
   306 	key->MakeUnique();
   307 	key->SetComparison(aComparison);
   308 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
   309 	CleanupStack::PopAndDestroy();
   310 	test (TheDatabase.Commit()==KErrNone);
   311 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   312 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   313 	}
   314 
   315 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TInt& aValue)
   316 	{aValue=aSet.ColInt(col);}
   317 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TUint& aValue)
   318 	{aValue=aSet.ColUint(col);}
   319 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TInt64& aValue)
   320 	{aValue=aSet.ColInt64(col);}
   321 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TReal32& aValue)
   322 	{aValue=aSet.ColReal32(col);}
   323 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TReal64& aValue)
   324 	{aValue=aSet.ColReal64(col);}
   325 inline void GetCol(RDbRowSet& aSet,TDbColNo col,TTime& aValue)
   326 	{aValue=aSet.ColTime(col);}
   327 
   328 /**
   329 @SYMTestCaseID          SYSLIB-DBMS-CT-0621
   330 @SYMTestCaseDesc        RDbRowSet ordering test
   331 @SYMTestPriority        Medium
   332 @SYMTestActions         Reorder the row set data with RDbRowSet::GetL(),SetL() functions.
   333 @SYMTestExpectedResults Test must not fail
   334 @SYMREQ                 REQ0000
   335 */
   336 template <class T>
   337 void TestOrderingL(RDbRowSet& aSet,T,TInt aCount)
   338 	{
   339 	test.Next( _L( " @SYMTestCaseID:SYSLIB-DBMS-CT-0621 " ) );
   340 	test(aSet.CountL()==aCount);
   341 	TInt count=0;
   342 	if (aSet.FirstL())
   343 		{
   344 		++count;
   345 		aSet.GetL();
   346 		T last;
   347 		GetCol(aSet,1,last);
   348 		while (aSet.NextL())
   349 			{
   350 			++count;
   351 			aSet.GetL();
   352 			T current;
   353 			GetCol(aSet,1,current);
   354 			test(last<current);
   355 			last=current;
   356 			}
   357 		}
   358 	test(count==aCount);
   359 	}
   360 
   361 /**
   362 @SYMTestCaseID          SYSLIB-DBMS-CT-0622
   363 @SYMTestCaseDesc        RDbTable::SeekL() function test
   364 @SYMTestPriority        Medium
   365 @SYMTestActions         Tests for the retrieved column value
   366 @SYMTestExpectedResults Test must not fail
   367 @SYMREQ                 REQ0000
   368 */
   369 template <class T>
   370 void TestSeekL(RDbTable& aTable,T,const T aValues[],TInt aCount)
   371 	{
   372 	test.Next( _L( " @SYMTestCaseID:SYSLIB-DBMS-CT-0622 Test index seeking " ) );
   373 	for (TInt ii=0;ii<aCount;++ii)
   374 		{
   375 		test(aTable.SeekL(aValues[ii]));
   376 		aTable.GetL();
   377 		T val;
   378 		GetCol(aTable,1,val);
   379 		test(aValues[ii]==val);
   380 		}
   381 	}
   382 
   383 /**
   384 aVal argument is used in the test functions (TestType & TestOrdering) and has no meaning outside them.
   385 It is used only to avoid some compiler varnings and to determine the correct template type
   386 
   387 @SYMTestCaseID          SYSLIB-DBMS-CT-0623
   388 @SYMTestCaseDesc        Tests for RDbTable,RDbRowSet classes
   389 @SYMTestPriority        Medium
   390 @SYMTestActions         Call up Test for table ordering and index seeking functions
   391 @SYMTestExpectedResults Test must not fail
   392 @SYMREQ                 REQ0000
   393 */
   394 template <class T>
   395 void TestTypeL( TDbColType aType, TInt aAttribs, const T aValues[], TInt aCount )
   396 	{
   397 	test.Start( _L( " @SYMTestCaseID:SYSLIB-DBMS-CT-0623 Build table " ) );
   398 	BuildTableL( aType, aAttribs, aValues, aCount );
   399 	T t(0);
   400 //
   401 	test.Next( _L( "Test ORDER BY ordering" ) );
   402 	test( TheView.Prepare( TheDatabase, KSelectOrdered ) == KErrNone );
   403 	test( TheView.EvaluateAll() == KErrNone );
   404 	TestOrderingL( TheView, t, aCount );
   405 	TheView.Close();
   406 //
   407 	BuildIndexL();
   408 	TestOrderingL( TheTable, t, aCount );
   409 //
   410 	TestSeekL( TheTable, t, aValues, aCount );
   411 	TheTable.Close();
   412 //
   413 	test( TheDatabase.DropTable( KTableName ) == KErrNone );
   414 	test.End();
   415 	}
   416 /**
   417 @SYMTestCaseID          SYSLIB-DBMS-CT-1322
   418 @SYMTestCaseDesc        Text ordering test
   419 @SYMTestPriority        Medium
   420 @SYMTestActions         Tests for RDbRowSet::Next(),RDbRowSet::GetL(),RDbRowSet::ColDes() functions
   421 @SYMTestExpectedResults Test must not fail
   422 @SYMREQ                 REQ0000
   423 */
   424 void TestTextOrderingL(RDbRowSet& aSet,TInt aCount,const TTextOps& aComp)
   425 	{
   426 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-1322 "));
   427 	test(aSet.CountL()==aCount);
   428 	TInt count=0;
   429 	while (aSet.NextL())
   430 		{
   431 		aSet.GetL();
   432 		TPtrC current=aSet.ColDes(1);
   433 		if (count++>0)
   434 			test(aComp.Compare(TheBuf,current)<0);
   435 		TheBuf=current;
   436 		}
   437 	test(count==aCount);
   438 	}
   439 
   440 /**
   441 @SYMTestCaseID          SYSLIB-DBMS-CT-1323
   442 @SYMTestCaseDesc        Tests for RDbView,RDbTable classes
   443 @SYMTestPriority        Medium
   444 @SYMTestActions         Wrapper function to call up for text ordering and text indexing tests
   445 @SYMTestExpectedResults Test must not fail
   446 @SYMREQ                 REQ0000
   447 */
   448 void TestTextL(const TPtrC aValues[],TInt aCount,TDbTextComparison aComparison,TInt aLength=KDbUndefinedLength)
   449 	{
   450 	const TTextOps& comp=TTextOps::Ops(aComparison);
   451 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-1323 Build table "));
   452 	BuildTableL(EDbColText,0,aValues,aCount);
   453 //
   454 	test.Next(_L("Test ORDER BY ordering"));
   455 	test (TheView.Prepare(TheDatabase,TDbQuery(KSelectOrdered,aComparison))==KErrNone);
   456 	test (TheView.EvaluateAll()==KErrNone);
   457 	TestTextOrderingL(TheView,aCount,comp);
   458 	TheView.Close();
   459 //
   460 	BuildIndexL(aComparison,aLength);
   461 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   462 	TestTextOrderingL(TheTable,aCount,comp);
   463 //
   464 	test.Next(_L("Test index seeking"));
   465 	for (TInt ii=0;ii<aCount;++ii)
   466 		{
   467 		test(TheTable.SeekL(aValues[ii]));
   468 		TheTable.GetL();
   469 		test(comp.Compare(aValues[ii],TheTable.ColDes(1))==0);
   470 		}
   471 //
   472 	TheTable.Close();
   473 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   474 	test.End();
   475 	}
   476 
   477 void TestLongTextOrderingL(RDbRowSet& aSet,TInt aCount,const TTextOps& aComp)
   478 	{
   479 	test(aSet.CountL()==aCount);
   480 	TInt count=0;
   481 	HBufC* prev=0;
   482 	while (aSet.NextL())
   483 		{
   484 		aSet.GetL();
   485 		TInt len=aSet.ColLength(1);
   486 		HBufC* buf=HBufC::NewL(len);
   487 		RDbColReadStream s;
   488 		s.OpenLC(aSet,1);
   489 		TPtr des(buf->Des());
   490 		s.ReadL(des,len);
   491 		CleanupStack::PopAndDestroy();
   492 		if (count++)
   493 			test (aComp.Compare(*buf,*prev)>=0);
   494 		delete prev;
   495 		prev=buf;
   496 		}
   497 	delete prev;
   498 	test(count==aCount);
   499 	}
   500 
   501 LOCAL_C void OrderByLongTextL(TInt aCount,TDbTextComparison aComparison)
   502 	{
   503 	test (TheView.Prepare(TheDatabase,TDbQuery(KSelectOrdered,aComparison))==KErrNone);
   504 	test (TheView.EvaluateAll()==KErrNone);
   505 	TestLongTextOrderingL(TheView,aCount,TTextOps::Ops(aComparison));
   506 	TheView.Close();
   507 	}
   508 
   509 LOCAL_C void TestLongTextL(const TPtrC aValues[],TInt aCount,TDbTextComparison aComparison,TInt aLength=KDbUndefinedLength)
   510 	{
   511 	const TTextOps& comp=TTextOps::Ops(aComparison);
   512 	test.Start(_L("Build table"));
   513 	BuildTableL(EDbColLongText,0,aValues,aCount);
   514 //
   515 	test.Next(_L("Test ORDER BY ordering"));
   516 	OrderByLongTextL(aCount,aComparison);
   517 //
   518 	BuildIndexL(aComparison,aLength);
   519 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   520 	TestLongTextOrderingL(TheTable,aCount,comp);
   521 //
   522 	test.Next(_L("Test index seeking"));
   523 	for (TInt ii=0;ii<aCount;++ii)
   524 		{
   525 		test(TheTable.SeekL(aValues[ii]));
   526 		TheTable.GetL();
   527 		RDbColReadStream strm;
   528 		strm.OpenLC(TheTable,1);
   529 		strm.ReadL(TheBuf,TheTable.ColLength(1));
   530 		CleanupStack::PopAndDestroy();
   531 		test(comp.Compare(aValues[ii],TheBuf)==0);
   532 		}
   533 	TheTable.Close();
   534 //
   535 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   536 	test.End();
   537 	}
   538 
   539 TUint const KBitValues[]={0,1};
   540 TInt const KInt8Values[]={0,KMinTInt8+1,1,KMaxTInt8,2,-3,-1,KMaxTInt8-1,KMinTInt8,-40};
   541 TInt const KInt16Values[]={0,KMinTInt16+1,1,KMaxTInt16,2,-3,-1,KMaxTInt16-1,KMinTInt16,-4000};
   542 TInt const KInt32Values[]={0,KMinTInt32+1,1,KMaxTInt32,2,-3,-1,KMaxTInt32-1,KMinTInt32,-40000000};
   543 TInt const KInt32Count=sizeof(KInt32Values)/sizeof(KInt32Values[0]);
   544 TUint const KUint8Values[]={0,1,KMaxTUint8,2,(KMaxTUint8+1)/2,(KMaxTUint8-1)/2,KMaxTUint8-1,40};
   545 TUint const KUint16Values[]={0,1,KMaxTUint16,2,(KMaxTUint16+1)/2,(KMaxTUint16-1)/2,KMaxTUint16-1,4000};
   546 TUint const KUint32Values[]={0,1,KMaxTUint32,2,KMaxTUint32/2+1,KMaxTUint32/2,KMaxTUint32-1,40000000};
   547 //TReal32 const KReal32Values[]={0.0f,1.0f,KMaxTReal32,KMinTReal32,-1.0f,-KMaxTReal32,-4e20f,-KMinTReal32};
   548 //TReal64 const KReal64Values[]={0.0,1.0,KMaxTReal64,KMinTReal64,-1.0,-KMaxTReal64,-4e200,-KMinTReal64};
   549 TReal32 const KReal32Values[]={0.0f,1.0f,1e37f,1e-37f,-1.0f,-1e37f,-4e20f,-1e-37f};
   550 TReal64 const KReal64Values[]={0.0,1.0,KMaxTReal64,KMinTReal64,-1.0,-KMaxTReal64,-4e200,-KMinTReal64};
   551 TInt64 const KInt64Values[]=
   552 	{
   553 	0,
   554 	MAKE_TINT64(0x80000000u,0x1u),
   555 	1,
   556 	MAKE_TINT64(0x7fffffffu,0xffffffffu),
   557 	2,
   558 	-3,
   559 	-1,
   560 	MAKE_TINT64(0x7fffffffu,0xfffffffeu),
   561 	MAKE_TINT64(0x80000000u,0x0u),
   562 	-40
   563 	};
   564 TTime const KTimeValues[]=
   565 	{
   566 	Time::MaxTTime(),
   567 	TInt64(0),
   568 	TDateTime(1970,EJanuary,0,0,0,0,0),
   569 	Time::MinTTime(),
   570 	TDateTime(2049,EDecember,30,23,59,59,999999),
   571 	TDateTime(1996,EJuly,8,17,45,0,0)
   572 	};
   573 TPtrC const KTextValues[]=
   574 	{
   575 	_S(""),
   576 	_S("10"),
   577 	_S("zyx"),
   578 	_S("0"),
   579 	_S("abcd"),
   580 	_S("abcdefg"),
   581 	_S("ABCDE"),
   582 	_S("Z")
   583 	};
   584 TPtrC const KLongTextValues[]=
   585 	{
   586 	_S("this blob will be inlined"),
   587 	_S(""),
   588 	_S("that blob was null"),
   589 	_S("An example of an out-of-line blob in an index! LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8-LongText8")
   590 	};
   591 const TInt KLongTextLimit=30;
   592 
   593 /**
   594 @SYMTestCaseID          SYSLIB-DBMS-CT-0624
   595 @SYMTestCaseDesc        Wrapper function testing for Indexing with different integer sizes and Text .
   596 @SYMTestPriority        Medium
   597 @SYMTestActions         Tests for indexing
   598 @SYMTestExpectedResults Test must not fail
   599 @SYMREQ                 REQ0000
   600 */
   601 LOCAL_C void TestTypesL()
   602 	{
   603 	#define ARRAY_SIZE(a) TInt(sizeof(a)/sizeof(a[0]))
   604 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0624 Indexing Bit "));
   605 	TestTypeL(EDbColBit,TDbCol::ENotNull,KBitValues,ARRAY_SIZE(KBitValues));
   606 	test.Next(_L("Indexing Int8"));
   607 	TestTypeL(EDbColInt8,TDbCol::ENotNull,KInt8Values,ARRAY_SIZE(KInt8Values));
   608 	test.Next(_L("Indexing Int16"));
   609 	TestTypeL(EDbColInt16,TDbCol::ENotNull,KInt16Values,ARRAY_SIZE(KInt16Values));
   610 	test.Next(_L("Indexing Int32"));
   611 	TestTypeL(EDbColInt32,TDbCol::ENotNull,KInt32Values,ARRAY_SIZE(KInt32Values));
   612 	test.Next(_L("Indexing Uint8"));
   613 	TestTypeL(EDbColUint8,TDbCol::ENotNull,KUint8Values,ARRAY_SIZE(KUint8Values));
   614 	test.Next(_L("Indexing Uint16"));
   615 	TestTypeL(EDbColUint16,TDbCol::ENotNull,KUint16Values,ARRAY_SIZE(KUint16Values));
   616 	test.Next(_L("Indexing Uint32"));
   617 	TestTypeL(EDbColUint32,TDbCol::ENotNull,KUint32Values,ARRAY_SIZE(KUint32Values));
   618 	test.Next(_L("Indexing Real32"));
   619 	TestTypeL(EDbColReal32,TDbCol::ENotNull,KReal32Values,ARRAY_SIZE(KReal32Values));
   620 	test.Next(_L("Indexing Real64"));
   621 	TestTypeL(EDbColReal64,TDbCol::ENotNull,KReal64Values,ARRAY_SIZE(KReal64Values));
   622 	test.Next(_L("Indexing Int64"));
   623 	TestTypeL(EDbColInt64,TDbCol::ENotNull,KInt64Values,ARRAY_SIZE(KInt64Values));
   624 	test.Next(_L("Indexing Time"));
   625 	TestTypeL(EDbColDateTime,TDbCol::ENotNull,KTimeValues,ARRAY_SIZE(KTimeValues));
   626 	test.Next(_L("Indexing Text (Normal)"));
   627 	TestTextL(KTextValues,ARRAY_SIZE(KTextValues),EDbCompareNormal);
   628 	test.Next(_L("Indexing Text (Folded)"));
   629 	TestTextL(KTextValues,ARRAY_SIZE(KTextValues),EDbCompareFolded);
   630 	test.Next(_L("Indexing Text (Collated)"));
   631 	TestTextL(KTextValues,ARRAY_SIZE(KTextValues),EDbCompareCollated);
   632 	test.Next(_L("Indexing Text (Normal, truncated)"));
   633 	TestTextL(KTextValues,ARRAY_SIZE(KTextValues),EDbCompareNormal,5);
   634 	test.Next(_L("Indexing LongText (Normal)"));
   635 	TestLongTextL(KLongTextValues,ARRAY_SIZE(KLongTextValues),EDbCompareNormal,KLongTextLimit);
   636 	test.Next(_L("Indexing LongText (Folded)"));
   637 	TestLongTextL(KLongTextValues,ARRAY_SIZE(KLongTextValues),EDbCompareFolded,KLongTextLimit);
   638 	test.Next(_L("Indexing LongText (Collated)"));
   639 	TestLongTextL(KLongTextValues,ARRAY_SIZE(KLongTextValues),EDbCompareCollated,KLongTextLimit);
   640 	test.End();
   641 	}
   642 
   643 const TInt KBlobKeyTruncated=32/sizeof(TText);
   644 const TInt KBlobKeyMaxInline=255/sizeof(TText);
   645 const TInt KBlobKeyCompare=512/sizeof(TText);
   646 
   647 const TInt KMemoTestLengths[]=
   648 	{
   649 	0,
   650 	1,
   651 	KBlobKeyTruncated-1,
   652 	KBlobKeyTruncated,
   653 	KBlobKeyTruncated+1,
   654 	KBlobKeyMaxInline,
   655 	KBlobKeyMaxInline+1,
   656 	KBlobKeyCompare-1,
   657 	KBlobKeyCompare,
   658 	KBlobKeyCompare+1,
   659 	5000
   660 	};
   661 
   662 void TestMemoTableL(CDbColSet& aBaseSet)
   663 	{
   664 	test.Start(_L("create the table"));
   665 	aBaseSet.AddL(TDbCol(KColumnName,EDbColLongText));
   666 	TheDatabase.Begin();
   667 	TInt r=TheDatabase.CreateTable(KTableName,aBaseSet);
   668 	test (r==KErrNone);
   669 //
   670 	test.Next(_L("add the rows"));
   671 	r=TheView.Prepare(TheDatabase,KSelectOrdered,RDbView::EInsertOnly);
   672 	test (r==KErrNone);
   673 	r=TheView.EvaluateAll();
   674 	test (r==KErrNone);
   675 	HBufC* prev=0;
   676 	TInt count=0;
   677 	for (TUint ii=0;ii<elementsof(KMemoTestLengths);++ii)
   678 		{
   679 		TInt size=KMemoTestLengths[ii];
   680 		HBufC* buf=HBufC::NewL(size);
   681 		if (size>0)
   682 			{
   683 			TPtr des(buf->Des());
   684 			des.Copy(*prev);
   685 			des.AppendFill('b',size-prev->Length());
   686 			delete prev;
   687 			TheView.InsertL();
   688 			TheView.SetColL(1,*buf);
   689 			TheView.PutL();
   690 			++count;
   691 			des[size-1]='a';
   692 			}
   693 		TheView.InsertL();
   694 		TheView.SetColL(1,*buf);
   695 		TheView.PutL();
   696 		++count;
   697 		prev=buf;
   698 		}
   699 	delete prev;
   700 	TheView.Close();
   701 	r=TheDatabase.Commit();
   702 	test (r==KErrNone);
   703 //
   704 	test.Next(_L("Normal order"));
   705 	OrderByLongTextL(count,EDbCompareNormal);
   706 	test.Next(_L("Folded order"));
   707 	OrderByLongTextL(count,EDbCompareFolded);
   708 	test.Next(_L("Collated order"));
   709 	OrderByLongTextL(count,EDbCompareCollated);
   710 //
   711 	r=TheDatabase.DropTable(KTableName);
   712 	test (r==KErrNone);
   713 	test.End();
   714 	}
   715 
   716 /**
   717 @SYMTestCaseID          SYSLIB-DBMS-CT-0625
   718 @SYMTestCaseDesc        Tests for ordering by longtext
   719 @SYMTestPriority        Medium
   720 @SYMTestActions         Tests for CDbColSet
   721 @SYMTestExpectedResults Test must not fail
   722 @SYMREQ                 REQ0000
   723 */
   724 LOCAL_C void TestOrderByLongTextL()
   725 	{
   726 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0625 Maximum Inline Limit "));
   727 	CDbColSet* set=CDbColSet::NewLC();
   728 	TestMemoTableL(*set);
   729 //
   730 	test.Next(_L("Reduced Inline limit [<32]"));
   731 	set->Clear();
   732 	TDbCol col;
   733 	col.iType=EDbColText8;
   734 	col.iMaxLength=255;
   735 	col.iAttributes=0;
   736 	TDbColName name;
   737 	for (TInt ii=0;ii<32;++ii)
   738 		{
   739 		name.Format(_L("col%d"),ii);
   740 		col.iName=name;
   741 		if (ii==31)
   742 			col.iMaxLength=255-20;
   743 		set->AddL(col);
   744 		}
   745 	TestMemoTableL(*set);
   746 	CleanupStack::PopAndDestroy();
   747 	test.End();
   748 	}
   749 
   750 /**
   751 @SYMTestCaseID          SYSLIB-DBMS-CT-0626
   752 @SYMTestCaseDesc        Tests for reverse ordering in indexes
   753 @SYMTestPriority        Medium
   754 @SYMTestActions         Tests for reversing the indexes
   755 @SYMTestExpectedResults Test must not fail
   756 @SYMREQ                 REQ0000
   757 */
   758 LOCAL_C void TestReverseL()
   759 	{
   760 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0626 Create table and index "));
   761 	TheDatabase.Begin();
   762 	CDbColSet *cs=CDbColSet::NewLC();
   763 	cs->AddL(TDbCol(KColumnName,EDbColInt32));
   764 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
   765 	CleanupStack::PopAndDestroy();
   766 	CDbKey *key=CDbKey::NewLC();
   767 	key->AddL(TDbKeyCol(KColumnName,TDbKeyCol::EDesc));
   768 	key->MakeUnique();
   769 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
   770 	CleanupStack::PopAndDestroy();
   771 	test(TheDatabase.Commit()==KErrNone);
   772 	test.Next(_L("Add records"));
   773 	TheDatabase.Begin();
   774 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   775 	for (TInt ii=0;ii<KInt32Count;++ii)
   776 		{
   777 		TheTable.InsertL();
   778 		TheTable.SetColL(1,KInt32Values[ii]);
   779 		TheTable.PutL();
   780 		}
   781 	test(TheDatabase.Commit()==KErrNone);
   782 	test(TheTable.CountL()==KInt32Count);
   783 	test.Next(_L("Check order"));
   784 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   785 	test(TheTable.CountL()==KInt32Count);
   786 	TInt count=0;
   787 	if (TheTable.FirstL())
   788 		{
   789 		++count;
   790 		TheTable.GetL();
   791 		TInt32 last=TheTable.ColInt(1);
   792 		while (TheTable.NextL())
   793 			{
   794 			++count;
   795 			TheTable.GetL();
   796 			TInt32 current=TheTable.ColInt(1);
   797 			test(last>current);
   798 			last=current;
   799 			}
   800 		}
   801 	test(count==KInt32Count);
   802 	TheTable.Close();
   803 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   804 	test.End();
   805 	}
   806 
   807 /**
   808 @SYMTestCaseID          SYSLIB-DBMS-CT-0627
   809 @SYMTestCaseDesc        Tests for multi-column keys
   810 @SYMTestPriority        Medium
   811 @SYMTestActions         Tests for mutliple column keys
   812 @SYMTestExpectedResults Test must not fail
   813 @SYMREQ                 REQ0000
   814 */
   815 LOCAL_C void TestMultiL()
   816 	{
   817 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0627 Create table and index "));
   818 	TheDatabase.Begin();
   819 	CDbColSet *cs=CDbColSet::NewLC();
   820 	TDbCol col(KColumnName,EDbColInt32);
   821 	col.iAttributes=TDbCol::ENotNull;
   822 	cs->AddL(col);
   823 	col.iName=KColumnTwo;
   824 	cs->AddL(col);
   825 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
   826 	CleanupStack::PopAndDestroy();
   827 	CDbKey *key=CDbKey::NewLC();
   828 	key->AddL(TDbKeyCol(KColumnTwo));
   829 	key->AddL(TDbKeyCol(KColumnName,TDbKeyCol::EDesc));
   830 	key->MakeUnique();
   831 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
   832 	CleanupStack::PopAndDestroy();
   833 	test(TheDatabase.Commit()==KErrNone);
   834 	test.Next(_L("Add records"));
   835 	TheDatabase.Begin();
   836 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   837 	for (TInt ii=0;ii<KInt32Count;++ii)
   838 		{
   839 		for (TInt jj=0;jj<KInt32Count;++jj)
   840 			{
   841 			TheTable.InsertL();
   842 			TheTable.SetColL(1,KInt32Values[ii]);
   843 			TheTable.SetColL(2,KInt32Values[jj]);
   844 			TheTable.PutL();
   845 			}
   846 		}
   847 	test(TheDatabase.Commit()==KErrNone);
   848 	test.Next(_L("Check order"));
   849 	test(TheTable.CountL()==KInt32Count*KInt32Count);
   850 	test(TheTable.SetIndex(KIndexName)==KErrNone);
   851 	test(TheTable.CountL()==KInt32Count*KInt32Count);
   852 	TInt count=0;
   853 	if (TheTable.FirstL())
   854 		{
   855 		++count;
   856 		TheTable.GetL();
   857 		TInt32 lastOne=TheTable.ColInt(1);
   858 		TInt32 lastTwo=TheTable.ColInt(2);
   859 		while (TheTable.NextL())
   860 			{
   861 			++count;
   862 			TheTable.GetL();
   863 			TInt32 currentOne=TheTable.ColInt(1);
   864 			TInt32 currentTwo=TheTable.ColInt(2);
   865 			test(lastTwo<currentTwo||(lastTwo==currentTwo&&lastOne>currentOne));
   866 			lastOne=currentOne;
   867 			lastTwo=currentTwo;
   868 			}
   869 		}
   870 	test(count==KInt32Count*KInt32Count);
   871 	TheTable.Close();
   872 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   873 	test.End();
   874 	}
   875 
   876 /**
   877 @SYMTestCaseID          SYSLIB-DBMS-CT-0628
   878 @SYMTestCaseDesc        Tests duplicates/unique constraints
   879 @SYMTestPriority        Medium
   880 @SYMTestActions         Tests for adding duplicate entries
   881 @SYMTestExpectedResults Test must not fail
   882 @SYMREQ                 REQ0000
   883 */
   884 LOCAL_C void TestDuplicatesL()
   885 	{
   886 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0628 Create table and indices "));
   887 	BuildTableL(EDbColInt32,TDbCol::ENotNull,KInt32Values,KInt32Count);
   888 	BuildIndexL();
   889 	TheTable.Close();
   890 	CDbKey* key=CDbKey::NewLC();
   891 	key->AddL(KColumnName);
   892 	test(TheDatabase.CreateIndex(KIndexTwo,KTableName,*key)==KErrNone);
   893 	CleanupStack::PopAndDestroy();
   894 	test.Next(_L("Attempt to add/update duplicate entry"));
   895 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   896 	TheTable.InsertL();
   897 	TheTable.SetColL(1,0);
   898 	TRAPD(r,TheTable.PutL());
   899 	test(r!=KErrNone);
   900 	TheTable.Cancel();
   901 	TheTable.LastL();
   902 	TheTable.UpdateL();
   903 	TheTable.SetColL(1,0);
   904 	TRAP(r,TheTable.PutL());
   905 	test(r!=KErrNone);
   906 	TheTable.Cancel();
   907 	test.Next(_L("Remove unique index"));
   908 	TheTable.Close();
   909 	test(TheDatabase.DropIndex(KIndexName,KTableName)==KErrNone);
   910 	test.Next(_L("Attempt to update/add duplicate entry"));
   911 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   912 	TheTable.LastL();
   913 	TheTable.UpdateL();
   914 	TheTable.SetColL(1,0);
   915 	TRAP(r,TheTable.PutL());
   916 	test(r==KErrNone);
   917 	TheTable.InsertL();
   918 	TheTable.SetColL(1,0);
   919 	TRAP(r,TheTable.PutL());
   920 	test(r==KErrNone);
   921 	test.Next(_L("Check order"));
   922 	test(TheTable.CountL()==KInt32Count+1);
   923 	test(TheTable.SetIndex(KIndexTwo)==KErrNone);
   924 	test(TheTable.CountL()==KInt32Count+1);
   925 	TInt count=0;
   926 	if (TheTable.FirstL())
   927 		{
   928 		++count;
   929 		TheTable.GetL();
   930 		TInt32 last=TheTable.ColInt(1);
   931 		while (TheTable.NextL())
   932 			{
   933 			++count;
   934 			TheTable.GetL();
   935 			TInt32 current=TheTable.ColInt(1);
   936 			test(last<=current);	// duplicates present
   937 			last=current;
   938 			}
   939 		}
   940 	test(count==KInt32Count+1);
   941 	test.Next(_L("Try to create unique index"));
   942 	TheTable.Close();
   943 	key=CDbKey::NewLC();
   944 	key->AddL(KColumnName);
   945 	key->MakeUnique();
   946 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)!=KErrNone);
   947 	CleanupStack::PopAndDestroy();
   948 	test(TheDatabase.DropTable(KTableName)==KErrNone);
   949 	test.End();
   950 	}
   951 
   952 struct Pair { TInt i1,i2; };
   953 struct TSeekingTest
   954 	{
   955 	Pair iSeek;
   956 	Pair iResults[5];
   957 	};
   958 
   959 // seek pair: results for <,<=,=,>=,>
   960 static TSeekingTest const SeekingTests[]=
   961 	{
   962 	{{0,0},{{-1,-1},{-1,-1},{-1,-1},{100,10},{100,10}}},
   963 	{{0,-1},{{-1,-1},{-1,-1},{-1,-1},{100,10},{100,10}}},
   964 	{{100,0},{{-1,-1},{-1,-1},{-1,-1},{100,10},{100,10}}},
   965 	{{100,10},{{-1,-1},{100,10},{100,10},{100,10},{100,20}}},
   966 	{{100,55},{{100,50},{100,50},{-1,-1},{100,60},{100,60}}},
   967 	{{100,60},{{100,50},{100,60},{100,60},{100,60},{100,70}}},
   968 	{{100,100},{{100,90},{100,100},{100,100},{100,100},{200,10}}},
   969 	{{100,110},{{100,100},{100,100},{-1,-1},{200,10},{200,10}}},
   970 	{{100,-1},{{-1,-1},{100,100},{100,10},{100,10},{200,10}}},
   971 	{{500,-1},{{400,100},{500,100},{500,10},{500,10},{600,10}}},
   972 	{{550,50},{{500,100},{500,100},{-1,-1},{600,10},{600,10}}},
   973 	{{550,-1},{{500,100},{500,100},{-1,-1},{600,10},{600,10}}},
   974 	{{1000,0},{{900,100},{900,100},{-1,-1},{1000,10},{1000,10}}},
   975 	{{1000,10},{{900,100},{1000,10},{1000,10},{1000,10},{1000,20}}},
   976 	{{1000,100},{{1000,90},{1000,100},{1000,100},{1000,100},{-1,-1}}},
   977 	{{1000,110},{{1000,100},{1000,100},{-1,-1},{-1,-1},{-1,-1}}},
   978 	{{1000,-1},{{900,100},{1000,100},{1000,10},{1000,10},{-1,-1}}},
   979 	{{1100,0},{{1000,100},{1000,100},{-1,-1},{-1,-1},{-1,-1}}},
   980 	{{1100,-1},{{1000,100},{1000,100},{-1,-1},{-1,-1},{-1,-1}}}
   981 	};
   982 
   983 /**
   984 @SYMTestCaseID          SYSLIB-DBMS-CT-0629
   985 @SYMTestCaseDesc        Tests for seeking on indexes
   986 @SYMTestPriority        Medium
   987 @SYMTestActions         Tests for SeekL and GetL functions
   988 @SYMTestExpectedResults Test must not fail
   989 @SYMREQ                 REQ0000
   990 */
   991 LOCAL_C void TestSeekingL()
   992 	{
   993 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0629 Create table and index "));
   994 	TheDatabase.Begin();
   995 	CDbColSet *cs=CDbColSet::NewLC();
   996 	TDbCol col(KColumnName,EDbColInt32);
   997 	col.iAttributes=TDbCol::ENotNull;
   998 	cs->AddL(col);
   999 	col.iName=KColumnTwo;
  1000 	cs->AddL(col);
  1001 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
  1002 	CleanupStack::PopAndDestroy();
  1003 	CDbKey *key=CDbKey::NewLC();
  1004 	key->AddL(KColumnName).AddL(KColumnTwo);
  1005 	key->MakeUnique();
  1006 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
  1007 	CleanupStack::PopAndDestroy();
  1008 	test(TheDatabase.Commit()==KErrNone);
  1009 	test.Next(_L("Add records"));
  1010 	TheDatabase.Begin();
  1011 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
  1012 	for (TInt ii=100;ii<=1000;ii+=100)
  1013 		{
  1014 		for (TInt jj=10;jj<=100;jj+=10)
  1015 			{
  1016 			TheTable.InsertL();
  1017 			TheTable.SetColL(1,ii);
  1018 			TheTable.SetColL(2,jj);
  1019 			TheTable.PutL();
  1020 			}
  1021 		}
  1022 	test(TheDatabase.Commit()==KErrNone);
  1023 	test(TheTable.CountL()==100);
  1024 	test(TheTable.SetIndex(KIndexName)==KErrNone);
  1025 	test(TheTable.CountL()==100);
  1026 	test.Next(_L("Seeking"));
  1027 	const TSeekingTest* stest=SeekingTests;
  1028 	const TSeekingTest* const end=stest+sizeof(SeekingTests)/sizeof(SeekingTests[0])-1;
  1029 	for (;stest<=end;++stest)
  1030 		{
  1031 		TDbSeekMultiKey<2> key;
  1032 		key.Add(stest->iSeek.i1);
  1033 		if (stest->iSeek.i2>=0)
  1034 			key.Add(stest->iSeek.i2);
  1035 		const Pair* results=stest->iResults;
  1036 		for (TInt ii=RDbTable::ELessThan;ii<=RDbTable::EGreaterThan;++results,++ii)
  1037 			{
  1038 			if (TheTable.SeekL(key,RDbTable::TComparison(ii)))
  1039 				{
  1040 				test(results->i1>=0);
  1041 				TRAPD(errCode, TheTable.GetL());
  1042 				test(errCode==KErrNone);
  1043 				test(TheTable.ColInt(1)==results->i1);
  1044 				test(TheTable.ColInt(2)==results->i2);
  1045 				}
  1046 			else
  1047 				test(results->i1<0);
  1048 			}
  1049 		}
  1050 	TheTable.Close();
  1051 	test(TheDatabase.DropTable(KTableName)==KErrNone);
  1052 	test.End();
  1053 	}
  1054 
  1055 /**
  1056 @SYMTestCaseID          SYSLIB-DBMS-CT-0630
  1057 @SYMTestCaseDesc        Tests for defect,index creation and set index operations
  1058 @SYMTestPriority        Medium
  1059 @SYMTestActions         Tests for creating and setting index operations
  1060 @SYMTestExpectedResults Test must not fail
  1061 @SYMREQ                 REQ0000
  1062 */
  1063 LOCAL_C void TestInequalityErrorL()
  1064 	{
  1065 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0630 Create table "));
  1066 	TheDatabase.Begin();
  1067 	CDbColSet *cs=CDbColSet::NewLC();
  1068 	cs->AddL(TDbCol(KColumnName,EDbColInt32));
  1069 	test(TheDatabase.CreateTable(KTableName,*cs)==KErrNone);
  1070 	CleanupStack::PopAndDestroy();
  1071 //
  1072 	test.Next(_L("create indices"));
  1073 	CDbKey *key=CDbKey::NewLC();
  1074 	key->AddL(TDbKeyCol(KColumnName));
  1075 	test(TheDatabase.CreateIndex(KIndexName,KTableName,*key)==KErrNone);
  1076 	CleanupStack::PopAndDestroy();
  1077 //
  1078 	test.Next(_L("Populate table"));
  1079 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
  1080 	for(TInt ii=0; ii<=130;++ii)
  1081 		{
  1082 		TheTable.InsertL();
  1083 		TheTable.SetColL(1,ii);
  1084 		TheTable.PutL();
  1085 		}
  1086 //
  1087 	test.Next(_L("Checks"));
  1088 	test(TheTable.SetIndex(KIndexName)==KErrNone);
  1089 //
  1090 // We need to delete a row in this node to get to the correct inequality condition
  1091 	test.Next(_L("Delete row 90"));
  1092 	test(TheTable.SeekL(TDbSeekKey(90)));
  1093 	TheTable.GetL();
  1094 	TheTable.DeleteL();
  1095 // Now delete last row on node which should be 94
  1096 	test.Next(_L("Delete row 94"));
  1097 	test(TheTable.SeekL(TDbSeekKey(94)));
  1098 	TheTable.GetL();
  1099 	TheTable.DeleteL();
  1100 	TheTable.EndL();
  1101 	test(!TheTable.SeekL(TDbSeekKey(94)));
  1102 //
  1103 	test.Next(_L("Insert row 94"));
  1104 	TheTable.InsertL();
  1105 	TheTable.SetColL(1,94);
  1106 	TheTable.PutL();
  1107 //
  1108 	test.Next(_L("now try and find it"));
  1109 	test(TheTable.SeekL(TDbSeekKey(94))); //prior to defect fix this line failed.
  1110 //
  1111 	TheDatabase.Commit();
  1112 	TheTable.Close();
  1113 	test(TheDatabase.DropTable(KTableName)==KErrNone);
  1114 	test.End();
  1115 	}
  1116 
  1117 /**
  1118 @SYMTestCaseID          PDS-DBMS-CT-4006
  1119 @SYMTestCaseDesc        Tests for add dbseekkey operations
  1120 @SYMTestPriority        High
  1121 @SYMTestActions         Create a DbSeekKey then add an index to it
  1122 @SYMTestExpectedResults Verify that seeking with key works
  1123 @SYMDEF                 DEF135710
  1124 */
  1125 LOCAL_C void TestAddSeekKeyL()
  1126 	{
  1127 	test.Start(_L(" @SYMTestCaseID:PDS-DBMS-CT-4006 Tests for add dbseekkey operations"));
  1128 	
  1129 	CreateDatabase();
  1130 	TInt err = TheDatabase.Begin();
  1131 	test2(err, KErrNone);
  1132 	
  1133 	CDbColSet *cs=CDbColSet::NewLC();
  1134 	cs->AddL(TDbCol(KColumnName1,EDbColText8));
  1135 	err = TheDatabase.CreateTable(KTableName1,*cs);
  1136 	test2(err , KErrNone);
  1137 	CleanupStack::PopAndDestroy();
  1138 
  1139 	test.Next(_L("Create Indices"));
  1140 	CDbKey *key=CDbKey::NewLC();
  1141 	key->AddL(TDbKeyCol(KColumnName1));
  1142 	err = TheDatabase.CreateIndex(KIndexName1,KTableName1,*key);
  1143 	test2(err, KErrNone);
  1144 	CleanupStack::PopAndDestroy();
  1145 
  1146 	test.Next(_L("Populate Table"));
  1147 	TBuf8<7> buf(_L8("testKey"));
  1148 	err = TheTable.Open(TheDatabase,KTableName1);
  1149 	test2(err, KErrNone);
  1150 	for(TInt i = 0; i <= 10; ++i)
  1151 		{
  1152 		TheTable.InsertL();
  1153 		TheTable.SetColL(1,buf);
  1154 		TheTable.PutL();
  1155 		}
  1156 	
  1157 	test.Next(_L("Set Index"));
  1158 	err = TheTable.SetIndex(KIndexName1);
  1159 	test2(err, KErrNone);
  1160 
  1161 	test.Next(_L("Testing Add Function"));
  1162 	TDbSeekKey testkey;
  1163 	testkey.Add(buf);
  1164 	TBool r = TheTable.SeekL(testkey);
  1165 	test2(r, ETrue);
  1166 	
  1167 	err = TheDatabase.Commit();
  1168 	test2(err, KErrNone);
  1169 	
  1170 	TheTable.Close();
  1171 	test.End();
  1172 	}
  1173 
  1174 //
  1175 // Test the database definition and enquiry functions
  1176 //
  1177 LOCAL_C void TestIndexesL()
  1178 	{
  1179 	test.Start(_L("Create Database"));
  1180 	CreateDatabase();
  1181 	test.Next(_L("Test index build and drop"));
  1182 	TestIndexBuildL();
  1183 	test.Next(_L("Test index persistence"));
  1184 	TestPersistenceL();
  1185 	test.Next(_L("Testing index key types"));
  1186 	TestTypesL();
  1187 	test.Next(_L("Testing LongText ORDER BY"));
  1188 	TestOrderByLongTextL();
  1189 	test.Next(_L("Testing reverse ordering"));
  1190 	TestReverseL();
  1191 	test.Next(_L("Testing multi column keys"));
  1192 	TestMultiL();
  1193 	test.Next(_L("Testing duplicate/unqiue"));
  1194 	TestDuplicatesL();
  1195 	test.Next(_L("Testing seeking"));
  1196 	TestSeekingL();
  1197 	test.Next(_L("Testing incorrect inequaltiy condition in store btree"));
  1198 	TestInequalityErrorL();
  1199 	CloseDatabase();
  1200 	test.End();
  1201 	}
  1202 
  1203 //
  1204 // Prepare the test directory.
  1205 //
  1206 LOCAL_C void setupTestDirectory()
  1207     {
  1208 	TInt r=TheFs.Connect();
  1209 	test(r==KErrNone);
  1210 //
  1211 	r=TheFs.MkDir(KTestDatabase);
  1212 	test(r==KErrNone || r==KErrAlreadyExists);
  1213 	}
  1214 
  1215 //
  1216 // Initialise the cleanup stack.
  1217 //
  1218 LOCAL_C void setupCleanup()
  1219     {
  1220 	TheTrapCleanup=CTrapCleanup::New();
  1221 	test(TheTrapCleanup!=NULL);
  1222 	TRAPD(r,\
  1223 		{\
  1224 		for (TInt i=KTestCleanupStack;i>0;i--)\
  1225 			CleanupStack::PushL((TAny*)0);\
  1226 		CleanupStack::Pop(KTestCleanupStack);\
  1227 		});
  1228 	test(r==KErrNone);
  1229 	}
  1230 
  1231 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
  1232 	{
  1233 	RFs fsSession;
  1234 	TInt err = fsSession.Connect();
  1235 	if(err == KErrNone)
  1236 		{
  1237 		TEntry entry;
  1238 		if(fsSession.Entry(aFullName, entry) == KErrNone)
  1239 			{
  1240 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
  1241 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
  1242 			if(err != KErrNone)
  1243 				{
  1244 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
  1245 				}
  1246 			err = fsSession.Delete(aFullName);
  1247 			if(err != KErrNone)
  1248 				{
  1249 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
  1250 				}
  1251 			}
  1252 		fsSession.Close();
  1253 		}
  1254 	else
  1255 		{
  1256 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
  1257 		}
  1258 	}
  1259 
  1260 //
  1261 // Test streaming conversions.
  1262 //
  1263 GLDEF_C TInt E32Main()
  1264     {
  1265 	test.Title();
  1266 	setupTestDirectory();
  1267 	setupCleanup();
  1268 	__UHEAP_MARK;
  1269 //
  1270 	TInt r=TheDbs.Connect();
  1271 	test (r==KErrNone);
  1272 	TheDbs.ResourceMark();
  1273 	test.Start(_L("Standard database"));
  1274 	TRAP(r,TestIndexesL();)
  1275 	test(r==KErrNone);
  1276 	test.Next(_L("Secure database"));
  1277 	TRAP(r,TestIndexesL();)
  1278 	test(r==KErrNone);
  1279 	TRAP(r, TestAddSeekKeyL());
  1280 	test(r==KErrNone);
  1281 	CloseDatabase();
  1282 
  1283 	::DeleteDataFile(KTestDatabase);	//deletion of data files must be before call to end - DEF047652
  1284 
  1285 	test.End();
  1286 	TheDbs.ResourceCheck();
  1287 	TheDbs.Close();
  1288 
  1289 	__UHEAP_MARKEND;
  1290 	delete TheTrapCleanup;
  1291 
  1292 	TheFs.Close();
  1293 	test.Close();
  1294 	return 0;
  1295     }