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