os/persistentdata/persistentstorage/dbms/tdbms/t_dbcomp.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
#include <hal.h>
sl@0
    27
sl@0
    28
static RTest TheTest(_L("t_dbcomp"));
sl@0
    29
static RFs   TheFs;
sl@0
    30
sl@0
    31
//T_BENCH.DB file is created by T_BENCH test and is used by the current test (T_COMP).
sl@0
    32
//T_COMP test will delete T_BENCH.DB at the end as it is no more needed.
sl@0
    33
//If you want to rerun T_COMP test again, you have to ensure that T_BENCH.DB file exists -
sl@0
    34
//run T_BENCH test again.
sl@0
    35
TFileName TheBenchDbFileName;
sl@0
    36
TFileName TheCompressedFileName;
sl@0
    37
TFileName TheDecompressedFileName;
sl@0
    38
sl@0
    39
RDbNamedDatabase TheCompDb;
sl@0
    40
RDbNamedDatabase TheCopyDb;
sl@0
    41
RDbTable 		 TheTable1, TheTable2;
sl@0
    42
sl@0
    43
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    44
sl@0
    45
static void CloseAll()
sl@0
    46
	{
sl@0
    47
	TheTable2.Close();
sl@0
    48
	TheTable1.Close();
sl@0
    49
	TheCopyDb.Close();
sl@0
    50
	TheCompDb.Close();
sl@0
    51
	}
sl@0
    52
sl@0
    53
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    54
sl@0
    55
//Delete "aFullName" file.
sl@0
    56
static void DeleteFile(const TDesC& aFullName)
sl@0
    57
	{
sl@0
    58
	RFs fsSession;
sl@0
    59
	TInt err = fsSession.Connect();
sl@0
    60
	if(err == KErrNone)
sl@0
    61
		{
sl@0
    62
		TEntry entry;
sl@0
    63
		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0
    64
			{
sl@0
    65
			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0
    66
			if(err != KErrNone) 
sl@0
    67
				{
sl@0
    68
				TheTest.Printf(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0
    69
				}
sl@0
    70
			err = fsSession.Delete(aFullName);
sl@0
    71
			if(err != KErrNone) 
sl@0
    72
				{
sl@0
    73
				TheTest.Printf(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0
    74
				}
sl@0
    75
			}
sl@0
    76
		fsSession.Close();
sl@0
    77
		}
sl@0
    78
	else
sl@0
    79
		{
sl@0
    80
		TheTest.Printf(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0
    81
		}
sl@0
    82
	}
sl@0
    83
sl@0
    84
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    85
sl@0
    86
static void DestroyTestEnv()
sl@0
    87
	{
sl@0
    88
	CloseAll();
sl@0
    89
	DeleteFile(TheDecompressedFileName);
sl@0
    90
	DeleteFile(TheCompressedFileName);
sl@0
    91
	DeleteFile(TheBenchDbFileName);
sl@0
    92
	TheFs.Close();
sl@0
    93
	}
sl@0
    94
sl@0
    95
///////////////////////////////////////////////////////////////////////////////////////
sl@0
    96
//Tests macros and functions.
sl@0
    97
//If (!aValue) then the test will be panicked, the test data files will be deleted.
sl@0
    98
static void Check(TInt aValue, TInt aLine)
sl@0
    99
	{
sl@0
   100
	if(!aValue)
sl@0
   101
		{
sl@0
   102
		TheTest.Printf(_L("*** Boolean expression evaluated to false!\r\n"));
sl@0
   103
		DestroyTestEnv();
sl@0
   104
		TheTest(EFalse, aLine);
sl@0
   105
		}
sl@0
   106
	}
sl@0
   107
//If (aValue != aExpected) then the test will be panicked, the test data files will be deleted.
sl@0
   108
static void Check(TInt aValue, TInt aExpected, TInt aLine)
sl@0
   109
	{
sl@0
   110
	if(aValue != aExpected)
sl@0
   111
		{
sl@0
   112
		TheTest.Printf(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
sl@0
   113
		DestroyTestEnv();
sl@0
   114
		TheTest(EFalse, aLine);
sl@0
   115
		}
sl@0
   116
	}
sl@0
   117
//Use these to test conditions.
sl@0
   118
#define TEST(arg) ::Check((arg), __LINE__)
sl@0
   119
#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
sl@0
   120
sl@0
   121
//////////////////////////////////////////////////////
sl@0
   122
sl@0
   123
static TInt TheCounterFreq = -10000000;
sl@0
   124
const TInt KMicroSecIn1Sec = 1000000;
sl@0
   125
sl@0
   126
TUint32 CalcTickDiff(TUint32 aStartTicks, TUint32 aEndTicks)
sl@0
   127
	{
sl@0
   128
	TInt64 diffTicks = (TInt64)aEndTicks - (TInt64)aStartTicks;
sl@0
   129
	if(diffTicks < 0)
sl@0
   130
		{
sl@0
   131
		diffTicks = KMaxTUint32 + diffTicks + 1;
sl@0
   132
		}
sl@0
   133
	return (TUint32)diffTicks;
sl@0
   134
	}
sl@0
   135
sl@0
   136
//Prints aFastCount parameter (converted to us)
sl@0
   137
void PrintFcDiffAsUs(const TDesC& aFormatStr, TUint32 aFastCount)
sl@0
   138
	{
sl@0
   139
	if(TheCounterFreq <= 0)
sl@0
   140
		{
sl@0
   141
		TEST2(HAL::Get(HAL::EFastCounterFrequency, TheCounterFreq), KErrNone);
sl@0
   142
		TheTest.Printf(_L("Counter frequency=%d Hz\r\n"), TheCounterFreq);
sl@0
   143
		}
sl@0
   144
	double v = ((double)aFastCount * KMicroSecIn1Sec) / (double)TheCounterFreq;
sl@0
   145
	TInt v2 = (TInt)v;
sl@0
   146
	TheTest.Printf(aFormatStr, v2);
sl@0
   147
	}
sl@0
   148
sl@0
   149
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   150
sl@0
   151
//
sl@0
   152
// Prepare the test directory.
sl@0
   153
//
sl@0
   154
void CreateTestEnv()
sl@0
   155
    {
sl@0
   156
	TInt err = TheFs.Connect();
sl@0
   157
	TheTest(err == KErrNone);
sl@0
   158
sl@0
   159
	err = TheFs.MkDirAll(TheBenchDbFileName);
sl@0
   160
	TEST(err == KErrNone || err == KErrAlreadyExists);
sl@0
   161
	
sl@0
   162
	TUint dummy;
sl@0
   163
	err = TheFs.Att(TheBenchDbFileName, dummy);
sl@0
   164
	if(err != KErrNone)
sl@0
   165
		{
sl@0
   166
		TheTest.Printf(_L("*** The %S file does not exist, err=%d. Run \"T_BENCH\" test first!\r\n"), &TheBenchDbFileName, err);
sl@0
   167
		TEST2(err, KErrNone);
sl@0
   168
		}
sl@0
   169
	}
sl@0
   170
sl@0
   171
///////////////////////////////////////////////////////////////////////////////////////
sl@0
   172
sl@0
   173
//
sl@0
   174
// Compress the database
sl@0
   175
//
sl@0
   176
void CompressL(const TDesC& aSource, const TDesC& aTarget, TBool aCompress)
sl@0
   177
	{
sl@0
   178
	CFileMan* man = CFileMan::NewL(TheFs);
sl@0
   179
	TInt err = man->Copy(aSource, aTarget);
sl@0
   180
	delete man;
sl@0
   181
	User::LeaveIfError(err);
sl@0
   182
	
sl@0
   183
	User::LeaveIfError(TheFs.SetAtt(aTarget, 0, KEntryAttReadOnly));
sl@0
   184
	CFileStore* store = CFileStore::OpenLC(TheFs, aTarget, EFileRead|EFileWrite);
sl@0
   185
	
sl@0
   186
	TUint32 fc = User::FastCounter();
sl@0
   187
	if(aCompress)
sl@0
   188
		{
sl@0
   189
		RDbStoreDatabase::CompressL(*store, store->Root());
sl@0
   190
		}
sl@0
   191
	else
sl@0
   192
		{
sl@0
   193
		RDbStoreDatabase::DecompressL(*store, store->Root());
sl@0
   194
		}
sl@0
   195
	TUint32 time = CalcTickDiff(fc, User::FastCounter());
sl@0
   196
	PrintFcDiffAsUs(_L(" %d us\r\n"), time);
sl@0
   197
	
sl@0
   198
	store->CompactL();
sl@0
   199
	store->CommitL();
sl@0
   200
	
sl@0
   201
	CleanupStack::PopAndDestroy(store);
sl@0
   202
	}
sl@0
   203
sl@0
   204
void CheckTableL(RDbDatabase& aDatabase, RDbDatabase& aCopy, const TDesC& aTable)
sl@0
   205
	{
sl@0
   206
	TheTest.Printf(_L("Processing table %S\n"), &aTable);
sl@0
   207
	
sl@0
   208
	TInt err = TheTable1.Open(aDatabase, aTable, RDbRowSet::EReadOnly);
sl@0
   209
	TEST2(err, KErrNone);
sl@0
   210
	
sl@0
   211
	err = TheTable2.Open(aCopy, aTable, RDbRowSet::EReadOnly);
sl@0
   212
	TEST2(err, KErrNone);
sl@0
   213
	
sl@0
   214
	TInt columns = TheTable1.ColCount();
sl@0
   215
	while(TheTable1.NextL())
sl@0
   216
		{
sl@0
   217
		TheTable1.GetL();
sl@0
   218
		TEST(TheTable2.NextL());
sl@0
   219
		TheTable2.GetL();
sl@0
   220
		for(TInt ii=1;ii<=columns;++ii)
sl@0
   221
			{
sl@0
   222
			if(TDbCol::IsLong(TheTable1.ColType(ii)))
sl@0
   223
				{
sl@0
   224
				TInt len = TheTable1.ColSize(ii);
sl@0
   225
				TEST2(len, TheTable2.ColSize(ii));
sl@0
   226
				RDbColReadStream strm1;
sl@0
   227
				strm1.OpenLC(TheTable1, ii);
sl@0
   228
				RDbColReadStream strm2;
sl@0
   229
				strm2.OpenLC(TheTable2, ii);
sl@0
   230
				TBuf8<512> buf1;
sl@0
   231
				TBuf8<512> buf2;
sl@0
   232
				while(len)
sl@0
   233
					{
sl@0
   234
					TInt block = Min(512, len);
sl@0
   235
					strm1.ReadL(buf1, block);
sl@0
   236
					strm2.ReadL(buf2, block);
sl@0
   237
					TEST(buf1 == buf2);
sl@0
   238
					len -= block;
sl@0
   239
					}
sl@0
   240
				CleanupStack::PopAndDestroy(2);
sl@0
   241
				}
sl@0
   242
			else
sl@0
   243
				{
sl@0
   244
				TEST(TheTable1.ColDes8(ii) == TheTable2.ColDes8(ii));
sl@0
   245
				}
sl@0
   246
			}
sl@0
   247
		}
sl@0
   248
	TheTable2.Close();
sl@0
   249
	TheTable1.Close();
sl@0
   250
	}
sl@0
   251
sl@0
   252
void CheckL(const TDesC& aSource, const TDesC& aTarget)
sl@0
   253
	{
sl@0
   254
	TInt err = TheCompDb.Open(TheFs, aSource, KNullDesC, RDbNamedDatabase::EReadOnly);
sl@0
   255
	TEST2(err, KErrNone);
sl@0
   256
	
sl@0
   257
	err = TheCopyDb.Open(TheFs, aTarget, KNullDesC, RDbNamedDatabase::EReadOnly);
sl@0
   258
	TEST2(err, KErrNone);
sl@0
   259
	
sl@0
   260
	CDbTableNames* tables = TheCompDb.TableNamesL();
sl@0
   261
	for(TInt ii=0;ii<tables->Count();++ii)
sl@0
   262
		{
sl@0
   263
		CheckTableL(TheCompDb, TheCopyDb, (*tables)[ii]);
sl@0
   264
		}
sl@0
   265
	delete tables;
sl@0
   266
	TheCopyDb.Close();
sl@0
   267
	TheCompDb.Close();
sl@0
   268
	}
sl@0
   269
sl@0
   270
/**
sl@0
   271
@SYMTestCaseID          SYSLIB-DBMS-CT-0593
sl@0
   272
@SYMTestCaseDesc        Database compression tests.
sl@0
   273
@SYMTestPriority        Medium
sl@0
   274
@SYMTestActions         Tests for RDbStoreDatabase::CompressL(),RDbStoreDatabase::DecompressL() functions
sl@0
   275
@SYMTestExpectedResults Test must not fail
sl@0
   276
@SYMREQ                 REQ0000
sl@0
   277
*/
sl@0
   278
void Test(const TDesC& aSource, const TDesC& aTarget, TBool aCompress)
sl@0
   279
	{
sl@0
   280
	TRAPD(err, CompressL(aSource, aTarget, aCompress));
sl@0
   281
	TEST2(err, KErrNone);
sl@0
   282
	TheTest.Printf(_L("Checking database"));
sl@0
   283
	TRAP(err, CheckL(aSource, aTarget));
sl@0
   284
	TEST2(err, KErrNone);
sl@0
   285
	}
sl@0
   286
sl@0
   287
void DoTests()
sl@0
   288
	{
sl@0
   289
	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0593 Compressing..."));
sl@0
   290
	Test(TheBenchDbFileName, TheCompressedFileName, ETrue);
sl@0
   291
	
sl@0
   292
	TheTest.Next(_L("Decompressing..."));
sl@0
   293
	Test(TheCompressedFileName, TheDecompressedFileName, EFalse);
sl@0
   294
	}
sl@0
   295
sl@0
   296
//Usage: "t_comp [<drive letter>:]]"
sl@0
   297
TInt E32Main()
sl@0
   298
    {
sl@0
   299
	TheTest.Title();
sl@0
   300
sl@0
   301
	CTrapCleanup* tc = CTrapCleanup::New();
sl@0
   302
	TheTest(tc != NULL);
sl@0
   303
	
sl@0
   304
	TBuf<256> cmdline;
sl@0
   305
	User::CommandLine(cmdline);
sl@0
   306
sl@0
   307
	TParse parse;
sl@0
   308
sl@0
   309
	_LIT(KBenchDbFile, "C:\\DBMS-TST\\T_BENCH.DB");
sl@0
   310
	parse.Set(cmdline, &KBenchDbFile, 0);
sl@0
   311
	TheBenchDbFileName.Copy(parse.FullName());
sl@0
   312
sl@0
   313
	_LIT(KCompressedFile, "C:\\DBMS-TST\\T_COMP.DB1");
sl@0
   314
	parse.Set(cmdline, &KCompressedFile, 0);
sl@0
   315
	TheCompressedFileName.Copy(parse.FullName());
sl@0
   316
sl@0
   317
	_LIT(KDecompressedFile, "C:\\DBMS-TST\\T_COMP.DB2");
sl@0
   318
	parse.Set(cmdline, &KDecompressedFile, 0);
sl@0
   319
	TheDecompressedFileName.Copy(parse.FullName());
sl@0
   320
	
sl@0
   321
	__UHEAP_MARK;
sl@0
   322
	
sl@0
   323
	CreateTestEnv();
sl@0
   324
	DoTests();
sl@0
   325
	DestroyTestEnv();
sl@0
   326
sl@0
   327
	delete tc;
sl@0
   328
sl@0
   329
	__UHEAP_MARKEND;
sl@0
   330
	
sl@0
   331
	TheTest.End();
sl@0
   332
	TheTest.Close();
sl@0
   333
	
sl@0
   334
	return 0;
sl@0
   335
    }