os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_trans.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 // MSVC++ up to 5.0 has problems with expanding inline functions
    17 // This disables the mad warnings for the whole project
    18 #if defined(NDEBUG) && defined(__VC32__) && _MSC_VER<=1100
    19 #pragma warning(disable : 4710)			// function not expanded. MSVC 5.0 is stupid
    20 #endif
    21 
    22 #include <d32dbms.h>
    23 #include <s32file.h>
    24 #include <e32test.h>
    25 #include <e32math.h>
    26 
    27 #include "crccheck.h"
    28 
    29 #undef __UHEAP_MARK
    30 #define __UHEAP_MARK
    31 #undef __UHEAP_MARKEND
    32 #define __UHEAP_MARKEND
    33 
    34 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    35 
    36 #ifndef __linux__ //No CRC test on LINUX
    37 #ifdef __TOOLS2__
    38 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_TRANS.CRC");
    39 #else
    40 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_TRANS.CRC");
    41 #endif
    42 #endif
    43 
    44 LOCAL_D RTest test(_L("T_TRANS - Test DBMS transactions"));
    45 LOCAL_D CTrapCleanup* TheTrapCleanup;
    46 LOCAL_D RDbTable TheTable;
    47 LOCAL_D RFs TheFs;
    48 #ifndef __TOOLS2__
    49 LOCAL_D RDbs TheDbs;
    50 #endif
    51 LOCAL_D RDbNamedDatabase TheDatabase;
    52 
    53 const TInt KTestCleanupStack=0x20;
    54 
    55 #ifdef __TOOLS2__
    56 const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_TRANS.DB");
    57 #else
    58 const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_TRANS.DB");
    59 #endif
    60 
    61 const TPtrC KTableName(_S("table"));
    62 const TPtrC KIndexInt=_S("int");
    63 const TPtrC KIndexText=_S("text");
    64 const TPtrC KColumnInt=_S("int");
    65 const TPtrC KColumnText=_S("text");
    66 const TPtrC KColumnComment=_S("comment");
    67 const TPtrC KCommentValue=_S("abcdefghijklmnopqrstuvwxyz");
    68 const TInt KRecords=2000;
    69 
    70 class Progress
    71 	{
    72 	enum {ETotal=32};
    73 public:
    74 	Progress(TInt aCount);
    75 	void Next(TInt aStep);
    76 private:
    77 	TInt iCount;
    78 	TInt iPos;
    79 	};
    80 
    81 Progress::Progress(TInt aCount)
    82 	: iCount(aCount),iPos(0)
    83 	{}
    84 
    85 void Progress::Next(TInt aStep)
    86 	{
    87 	TInt next=(ETotal*(iCount-aStep))/iCount;
    88 	if (next!=iPos)
    89 		{
    90 		test.Printf(_L("."));
    91 		iPos=next;
    92 		}
    93 	if (aStep==0)
    94 		test.Printf(_L("\n"));
    95 	}
    96 
    97 LOCAL_C void ProgressInc(RDbIncremental& inc,TInt aCount)
    98 	{
    99 	Progress progress(aCount);
   100 	while (aCount)
   101 		{
   102 		inc.Next(aCount);
   103 		progress.Next(aCount);
   104 		}
   105 	inc.Close();
   106 	}
   107 
   108 LOCAL_C void CreateDatabaseL()
   109 //
   110 // Create the database
   111 //
   112 	{
   113 	TInt r=TheDatabase.Replace(TheFs,KTestDatabase);
   114 	test (r==KErrNone);
   115 	}
   116 
   117 LOCAL_C void OpenDatabase()
   118 //
   119 // Create the database
   120 //
   121 	{
   122 	TInt r=TheDatabase.Open(TheFs,KTestDatabase);
   123 	test (r==KErrNone);
   124 	}
   125 
   126 LOCAL_C void CloseDatabaseL()
   127 	{
   128 	TheDatabase.Close();
   129 	TheCrcChecker.GenerateCrcL(KTestDatabase);
   130 	}
   131 
   132 LOCAL_C void CreateTable()
   133 	{
   134 	TInt r=TheDatabase.Execute(_L("create table table (int integer,text varchar(8),comment varchar)"));
   135 	test (r==KErrNone);
   136 	}
   137 
   138 LOCAL_C void WriteRecords(TInt aCount)
   139 	{
   140 	Progress write(aCount);
   141 	TDbColNo cInt,cText,cComment;
   142 	CDbColSet* set=TheTable.ColSetL();
   143 	cInt=set->ColNo(KColumnInt);
   144 	cText=set->ColNo(KColumnText);
   145 	cComment=set->ColNo(KColumnComment);
   146 	delete set;
   147 	TBuf<10> text;
   148 	TInt jj=0;
   149 	for (TInt ii=0;ii<aCount;++ii)
   150 		{
   151 		TheTable.InsertL();
   152 		jj=(jj+23);
   153 		if (jj>=aCount)
   154 			jj-=aCount;
   155 		TheTable.SetColL(cInt,jj);
   156 		text.Num(jj);
   157 		TheTable.SetColL(cText,text);
   158 		TheTable.SetColL(cComment,KCommentValue);
   159 		TheTable.PutL();
   160 		write.Next(aCount-ii-1);
   161 		}
   162 	}
   163 
   164 LOCAL_C TUint FileSize()
   165 	{
   166 #ifndef __TOOLS2__
   167 	TEntry entry;
   168 	test(TheFs.Entry(KTestDatabase,entry)==KErrNone);
   169 	return entry.iSize;
   170 #else
   171 	RFile myfile;
   172 	test(myfile.Open(TheFs, KTestDatabase, EFileRead) == KErrNone);
   173 	TInt size;
   174 	test(myfile.Size(size) == KErrNone);
   175 	myfile.Close();
   176 	return size;
   177 #endif
   178 	}
   179 
   180 LOCAL_C void BuildTable(TInt aCount,TBool aTransactions,TUint& aTime,TUint& aSize)
   181 	{
   182 	TUint size=FileSize();
   183 	TUint time=User::TickCount();
   184 	CreateTable();
   185 	if (aTransactions)
   186 		TheDatabase.Begin();
   187 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   188 	WriteRecords(aCount);
   189 	if (aTransactions)
   190 		test(TheDatabase.Commit()==KErrNone);
   191 	TheTable.Close();
   192 	aTime=User::TickCount()-time;
   193 	aSize=FileSize()-size;
   194 	}
   195 
   196 LOCAL_C void Execute(const TDesC& aSql)
   197 	{
   198 	RDbIncremental inc;
   199 	TInt step;
   200 	test(inc.Execute(TheDatabase,aSql,step)==KErrNone);
   201 	ProgressInc(inc,step);
   202 	}
   203 
   204 LOCAL_C void BreakIndex()
   205 	{
   206 	TheDatabase.Begin();
   207 	test(TheTable.Open(TheDatabase,KTableName)==KErrNone);
   208 	TheTable.InsertL();
   209 	TheTable.SetColL(1,-1);
   210 	TheTable.PutL();
   211 	TheTable.Close();
   212 	TheDatabase.Rollback();
   213 	test(TheDatabase.IsDamaged());
   214 	}
   215 
   216 LOCAL_C void Recover()
   217 	{
   218 	RDbIncremental rec;
   219 	TInt step;
   220 	test(rec.Recover(TheDatabase,step)==KErrNone);
   221 	ProgressInc(rec,step);
   222 	test(!TheDatabase.IsDamaged());
   223 	}
   224 
   225 /**
   226 @SYMTestCaseID          SYSLIB-DBMS-CT-0637
   227 @SYMTestCaseDesc        Streaming conversions test
   228 @SYMTestPriority        Medium
   229 @SYMTestActions         Test the database definition and enquiry functions 
   230 @SYMTestExpectedResults Test must not fail
   231 @SYMREQ                 REQ0000
   232 */
   233 LOCAL_C void Test()
   234 	{
   235 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0637 Build without transactions "));
   236 	CreateDatabaseL();
   237 	TUint time1,size1;
   238 	BuildTable(KRecords,EFalse,time1,size1);
   239 	CloseDatabaseL();
   240 	test.Next(_L("Build with transactions"));
   241 	CreateDatabaseL();
   242 	TUint time2,size2;
   243 	BuildTable(KRecords,ETrue,time2,size2);
   244 	test.Printf(_L("Transaction performance: time %4.2f, size %4.2f\n"),TReal(time1)/TReal(time2),TReal(size1)/TReal(size2));
   245 	test.Next(_L("Build Int index"));
   246 	Execute(_L("create unique index int on table (int)"));
   247 	test.Next(_L("Break index"));
   248 	BreakIndex();
   249 	test.Next(_L("Build Text index"));
   250 	Execute(_L("create unique index text on table (text)"));
   251 	test.Next(_L("Recover"));
   252 	test (TheDatabase.IsDamaged());
   253 	CloseDatabaseL();
   254 	OpenDatabase();
   255 	test (TheDatabase.IsDamaged());
   256 	Recover();
   257 	test.Next(_L("Drop table"));
   258 	Execute(_L("drop table table"));
   259 	CloseDatabaseL();
   260 	}
   261 
   262 LOCAL_C void setupTestDirectory()
   263 //
   264 // Prepare the test directory.
   265 //
   266     {
   267 	TInt r=TheFs.Connect();
   268 	test(r==KErrNone);
   269 //
   270 	r=TheFs.MkDir(KTestDatabase);
   271 	test(r==KErrNone || r==KErrAlreadyExists);
   272 	}
   273 
   274 LOCAL_C void setupCleanup()
   275 //
   276 // Initialise the cleanup stack.
   277 //
   278     {
   279 	TheTrapCleanup=CTrapCleanup::New();
   280 	test(TheTrapCleanup!=NULL);
   281 	TRAPD(r,\
   282 		{\
   283 		for (TInt i=KTestCleanupStack;i>0;i--)\
   284 			CleanupStack::PushL((TAny*)0);\
   285 		CleanupStack::Pop(KTestCleanupStack);\
   286 		});
   287 	test(r==KErrNone);
   288 	}
   289 
   290 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   291 	{
   292 	RFs fsSession;
   293 	TInt err = fsSession.Connect();
   294 	if(err == KErrNone)
   295 		{
   296 		TEntry entry;
   297 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   298 			{
   299 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   300 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   301 			if(err != KErrNone) 
   302 				{
   303 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   304 				}
   305 			err = fsSession.Delete(aFullName);
   306 			if(err != KErrNone) 
   307 				{
   308 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   309 				}
   310 			}
   311 		fsSession.Close();
   312 		}
   313 	else
   314 		{
   315 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   316 		}
   317 	}
   318 
   319 
   320 GLDEF_C TInt E32Main()
   321     {
   322 	test.Title();
   323 	setupTestDirectory();
   324 	setupCleanup();
   325 	__UHEAP_MARK;
   326 
   327 	TRAPD(r,Test();)
   328 	test(r==KErrNone);
   329 
   330 	test.Printf(_L("Waiting for server exit\n"));
   331 	const TUint KExitDelay=6*0x100000;	// ~6 seconds
   332 	User::After(KExitDelay);
   333 	
   334 	//deletion of data files must be done before call to End() - DEF047652
   335 	::DeleteDataFile(KTestDatabase);
   336 
   337 #ifndef __linux__
   338 	TInt err;
   339 #ifndef __TOOLS2__
   340 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
   341 	test(err==KErrNone);
   342 	test(lc==KErrNone);
   343 #else
   344 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
   345 	TPtrC errmsg;
   346 	TheCrcChecker.ErrorReportL(err, errmsg);
   347 	RDebug::Print(errmsg);
   348 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
   349 #endif
   350 #endif
   351 
   352 	test.End();
   353 //
   354 	__UHEAP_MARKEND;
   355 
   356 	delete TheTrapCleanup;
   357 	TheFs.Close();
   358 	test.Close();
   359 	return 0;
   360     }