os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_comp.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
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_COMP.CRC");
sl@0
    39
#else
sl@0
    40
const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_COMP.CRC");
sl@0
    41
#endif
sl@0
    42
#endif
sl@0
    43
sl@0
    44
LOCAL_D RTest test(_L("T_COMP"));
sl@0
    45
LOCAL_D CTrapCleanup* TheTrapCleanup;
sl@0
    46
LOCAL_D RFs TheFs;
sl@0
    47
sl@0
    48
const TInt KTestCleanupStack=0x20;
sl@0
    49
sl@0
    50
#ifdef __TOOLS2__
sl@0
    51
const TPtrC KTestDir=_L(".\\dbms-tst\\");
sl@0
    52
#else
sl@0
    53
const TPtrC KTestDir=_L("C:\\dbms-tst\\");
sl@0
    54
#endif
sl@0
    55
sl@0
    56
//T_BENCH.DB file is created by T_BENCH test and is used by the current test (T_COMP).
sl@0
    57
//T_COMP test will delete T_BENCH.DB at the end as it is no more needed. 
sl@0
    58
//If you want to rerun T_COMP test again, you have to ensure that T_BENCH.DB file exists - 
sl@0
    59
//run T_BENCH test again.
sl@0
    60
_LIT(KTestFile,"T_BENCH.DB");
sl@0
    61
sl@0
    62
_LIT(KRomTestFile,"Z:\\TEST\\T_COMP.DAT");
sl@0
    63
_LIT(KCompressedFile,"T_COMP.DB1");
sl@0
    64
_LIT(KDecompressedFile,"T_COMP.DB2");
sl@0
    65
sl@0
    66
const TStreamId KHelpId=2;
sl@0
    67
sl@0
    68
//
sl@0
    69
// Compress the database
sl@0
    70
//
sl@0
    71
LOCAL_C void CompressL(const TDesC& aSource,const TDesC& aTarget,TBool aCompress)
sl@0
    72
	{
sl@0
    73
#ifndef __TOOLS2__
sl@0
    74
	CFileMan* man=CFileMan::NewL(TheFs);
sl@0
    75
	TInt r=man->Copy(aSource,aTarget);
sl@0
    76
	delete man;
sl@0
    77
	User::LeaveIfError(r);
sl@0
    78
#else
sl@0
    79
	TInt r;
sl@0
    80
    RFile f1;
sl@0
    81
    r = f1.Open(TheFs, aSource, EFileStreamText|EFileShareReadersOnly);
sl@0
    82
    test(r==KErrNone);
sl@0
    83
    RFile f2;
sl@0
    84
    r=f2.Replace(TheFs, aTarget, EFileWrite);
sl@0
    85
    test(r==KErrNone);
sl@0
    86
sl@0
    87
    TBuf8<512> copyBuf;
sl@0
    88
	TInt rem;
sl@0
    89
	r=f1.Size(rem);
sl@0
    90
	test(r==KErrNone);
sl@0
    91
	TInt pos=0;
sl@0
    92
	while (rem)
sl@0
    93
		{
sl@0
    94
		TInt s=Min(rem,copyBuf.MaxSize());
sl@0
    95
		r=f1.Read(pos,copyBuf,s);
sl@0
    96
		test(r==KErrNone);
sl@0
    97
		test(copyBuf.Length()==s);
sl@0
    98
		r=f2.Write(pos,copyBuf,s);
sl@0
    99
		test(r==KErrNone);
sl@0
   100
		pos+=s;
sl@0
   101
		rem-=s;
sl@0
   102
		}
sl@0
   103
	f1.Close();
sl@0
   104
	f2.Close();	
sl@0
   105
#endif
sl@0
   106
#ifndef __TOOLS2__
sl@0
   107
	User::LeaveIfError(TheFs.SetAtt(aTarget,0,KEntryAttReadOnly));
sl@0
   108
#endif
sl@0
   109
	CFileStore* store=CFileStore::OpenLC(TheFs,aTarget,EFileRead|EFileWrite);
sl@0
   110
	TUint t=User::TickCount();
sl@0
   111
	if (aCompress)
sl@0
   112
		RDbStoreDatabase::CompressL(*store,store->Root());
sl@0
   113
	else
sl@0
   114
		RDbStoreDatabase::DecompressL(*store,store->Root());
sl@0
   115
	t=User::TickCount()-t;
sl@0
   116
	test.Printf(_L("%d ticks\r\n"),t);
sl@0
   117
	store->CompactL();
sl@0
   118
	store->CommitL();
sl@0
   119
	CleanupStack::PopAndDestroy();	// store
sl@0
   120
	TInt err;
sl@0
   121
	TRAPD(lc, err = TheCrcChecker.GenerateCrcL(aTarget));
sl@0
   122
	test(err == KErrNone);
sl@0
   123
	test(lc == KErrNone);
sl@0
   124
	}
sl@0
   125
sl@0
   126
LOCAL_C void CheckTableL(RDbDatabase& aDatabase,RDbDatabase& aCopy,const TDesC& aTable)
sl@0
   127
	{
sl@0
   128
	test.Printf(_L("Processing table %S\n"),&aTable);
sl@0
   129
	RDbTable table;
sl@0
   130
	test (table.Open(aDatabase,aTable,table.EReadOnly)==KErrNone);
sl@0
   131
	RDbTable copy;
sl@0
   132
	test (copy.Open(aCopy,aTable,table.EReadOnly)==KErrNone);
sl@0
   133
	TInt columns=table.ColCount();
sl@0
   134
	while (table.NextL())
sl@0
   135
		{
sl@0
   136
		table.GetL();
sl@0
   137
		test (copy.NextL());
sl@0
   138
		copy.GetL();
sl@0
   139
		for (TInt ii=1;ii<=columns;++ii)
sl@0
   140
			{
sl@0
   141
			if (TDbCol::IsLong(table.ColType(ii)))
sl@0
   142
				{
sl@0
   143
				TInt len=table.ColSize(ii);
sl@0
   144
				test (len==copy.ColSize(ii));
sl@0
   145
				RDbColReadStream strm1;
sl@0
   146
				strm1.OpenLC(table,ii);
sl@0
   147
				RDbColReadStream strm2;
sl@0
   148
				strm2.OpenLC(copy,ii);
sl@0
   149
				TBuf8<512> buf1;
sl@0
   150
				TBuf8<512> buf2;
sl@0
   151
				while (len)
sl@0
   152
					{
sl@0
   153
					TInt block=Min(512,len);
sl@0
   154
					strm1.ReadL(buf1,block);
sl@0
   155
					strm2.ReadL(buf2,block);
sl@0
   156
					test (buf1==buf2);
sl@0
   157
					len-=block;
sl@0
   158
					}
sl@0
   159
				CleanupStack::PopAndDestroy(2);
sl@0
   160
				}
sl@0
   161
			else
sl@0
   162
				{
sl@0
   163
				test (table.ColDes8(ii)==copy.ColDes8(ii));
sl@0
   164
				}
sl@0
   165
			}
sl@0
   166
		}
sl@0
   167
	table.Close();
sl@0
   168
	copy.Close();
sl@0
   169
	}
sl@0
   170
sl@0
   171
LOCAL_C void CheckL(const TDesC& aSource,const TDesC& aTarget)
sl@0
   172
	{
sl@0
   173
	RDbNamedDatabase comp;
sl@0
   174
	RDbNamedDatabase copy;
sl@0
   175
	test (comp.Open(TheFs,aSource,TPtrC(),comp.EReadOnly)==KErrNone);
sl@0
   176
	test (copy.Open(TheFs,aTarget,TPtrC(),copy.EReadOnly)==KErrNone);
sl@0
   177
	CDbTableNames* tables=comp.TableNamesL();
sl@0
   178
	for (TInt ii=0;ii<tables->Count();++ii)
sl@0
   179
		CheckTableL(comp,copy,(*tables)[ii]);
sl@0
   180
	delete tables;
sl@0
   181
	copy.Close();
sl@0
   182
	comp.Close();
sl@0
   183
	}
sl@0
   184
sl@0
   185
/**
sl@0
   186
@SYMTestCaseID          SYSLIB-DBMS-CT-0593
sl@0
   187
@SYMTestCaseDesc        Database compression tests.
sl@0
   188
@SYMTestPriority        Medium
sl@0
   189
@SYMTestActions         Tests for RDbStoreDatabase::CompressL(),RDbStoreDatabase::DecompressL() functions 
sl@0
   190
@SYMTestExpectedResults Test must not fail
sl@0
   191
@SYMREQ                 REQ0000
sl@0
   192
*/
sl@0
   193
LOCAL_C void Test(const TDesC& aSource,const TDesC& aTarget,TBool aCompress)
sl@0
   194
	{
sl@0
   195
	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0593 Converting database "));
sl@0
   196
	TRAPD(r,CompressL(aSource,aTarget,aCompress));
sl@0
   197
	test (r==KErrNone);
sl@0
   198
	test.Next(_L("Checking database"));
sl@0
   199
	TRAP(r,CheckL(aSource,aTarget));
sl@0
   200
	test (r==KErrNone);
sl@0
   201
	test.End();
sl@0
   202
	}
sl@0
   203
sl@0
   204
//
sl@0
   205
// Prepare the test directory.
sl@0
   206
//
sl@0
   207
LOCAL_C void setupTestDirectory()
sl@0
   208
    {
sl@0
   209
	TInt r=TheFs.Connect();
sl@0
   210
	test(r==KErrNone);
sl@0
   211
//
sl@0
   212
	r=TheFs.MkDir(KTestDir);
sl@0
   213
	test(r==KErrNone || r==KErrAlreadyExists);
sl@0
   214
	r=TheFs.SetSessionPath(KTestDir);
sl@0
   215
	test(r==KErrNone);
sl@0
   216
// On TOOLS2 - RFs::SetSessionPath() will affect all RFs Sessions, 
sl@0
   217
// the two RFs need same session path anyway
sl@0
   218
#ifdef __WINSCW__ 
sl@0
   219
	r=TheCrcChecker.SetSessionPath(KTestDir);
sl@0
   220
	test(r==KErrNone);
sl@0
   221
#endif
sl@0
   222
	}
sl@0
   223
sl@0
   224
//
sl@0
   225
// Initialise the cleanup stack.
sl@0
   226
//
sl@0
   227
LOCAL_C void setupCleanup()
sl@0
   228
    {
sl@0
   229
	TheTrapCleanup=CTrapCleanup::New();
sl@0
   230
	test(TheTrapCleanup!=NULL);
sl@0
   231
	TRAPD(r,\
sl@0
   232
		{\
sl@0
   233
		for (TInt i=KTestCleanupStack;i>0;i--)\
sl@0
   234
			CleanupStack::PushL((TAny*)0);\
sl@0
   235
		CleanupStack::Pop(KTestCleanupStack);\
sl@0
   236
		});
sl@0
   237
	test(r==KErrNone);
sl@0
   238
	}
sl@0
   239
sl@0
   240
LOCAL_C void DeleteDataFile(const TDesC& aFullName)
sl@0
   241
	{
sl@0
   242
	RFs fsSession;
sl@0
   243
	TInt err = fsSession.Connect();
sl@0
   244
	if(err == KErrNone)
sl@0
   245
		{
sl@0
   246
		TEntry entry;
sl@0
   247
		if(fsSession.Entry(aFullName, entry) == KErrNone)
sl@0
   248
			{
sl@0
   249
			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
sl@0
   250
			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
sl@0
   251
			if(err != KErrNone) 
sl@0
   252
				{
sl@0
   253
				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
sl@0
   254
				}
sl@0
   255
			err = fsSession.Delete(aFullName);
sl@0
   256
			if(err != KErrNone) 
sl@0
   257
				{
sl@0
   258
				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
sl@0
   259
				}
sl@0
   260
			}
sl@0
   261
		fsSession.Close();
sl@0
   262
		}
sl@0
   263
	else
sl@0
   264
		{
sl@0
   265
		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
sl@0
   266
		}
sl@0
   267
	}
sl@0
   268
sl@0
   269
//
sl@0
   270
// Test streaming conversions.
sl@0
   271
//
sl@0
   272
GLDEF_C TInt E32Main()
sl@0
   273
    {
sl@0
   274
	__UHEAP_MARK;
sl@0
   275
	test.Title();
sl@0
   276
	setupTestDirectory();
sl@0
   277
	setupCleanup();
sl@0
   278
//
sl@0
   279
	test.Start(_L("Compressing..."));
sl@0
   280
// Don't use the rom test file.
sl@0
   281
/*
sl@0
   282
	TUint dummy;
sl@0
   283
    TInt err = TheFs.Att(KRomTestFile,dummy);
sl@0
   284
	if (err==KErrNone)
sl@0
   285
		Test(KRomTestFile,KCompressedFile,ETrue);
sl@0
   286
	else
sl@0
   287
		Test(KTestFile,KCompressedFile,ETrue);
sl@0
   288
*/
sl@0
   289
	Test(KTestFile,KCompressedFile,ETrue);
sl@0
   290
	test.Next(_L("Decompressing..."));
sl@0
   291
	Test(KCompressedFile,KDecompressedFile,EFalse);
sl@0
   292
	
sl@0
   293
//
sl@0
   294
sl@0
   295
#ifndef __TOOLS2__
sl@0
   296
	_LIT(KTestDbName,"C:\\dbms-tst\\T_BENCH.DB");
sl@0
   297
	::DeleteDataFile(KTestDbName);
sl@0
   298
	_LIT(KTestDbName1,"C:\\dbms-tst\\T_COMP.DB1");
sl@0
   299
	::DeleteDataFile(KTestDbName1);
sl@0
   300
	_LIT(KTestDbName2,"C:\\dbms-tst\\T_COMP.DB2");
sl@0
   301
	::DeleteDataFile(KTestDbName2);
sl@0
   302
#else
sl@0
   303
	_LIT(KTestDbName,"T_BENCH.DB");
sl@0
   304
	::DeleteDataFile(KTestDbName);
sl@0
   305
	_LIT(KTestDbName1,"T_COMP.DB1");
sl@0
   306
	::DeleteDataFile(KTestDbName1);
sl@0
   307
	_LIT(KTestDbName2,"T_COMP.DB2");
sl@0
   308
	::DeleteDataFile(KTestDbName2);
sl@0
   309
#endif
sl@0
   310
sl@0
   311
#ifndef __linux__
sl@0
   312
#ifdef CRC_COMP  // Exclude Crc checking for this test. It's out of scope.
sl@0
   313
	TInt err;
sl@0
   314
#ifndef __TOOLS2__
sl@0
   315
	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
sl@0
   316
	test(err==KErrNone);
sl@0
   317
	test(lc==KErrNone);
sl@0
   318
#else
sl@0
   319
	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
sl@0
   320
	TPtrC errmsg;
sl@0
   321
	TheCrcChecker.ErrorReportL(err, errmsg);
sl@0
   322
	RDebug::Print(errmsg);
sl@0
   323
	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
sl@0
   324
#endif
sl@0
   325
#endif
sl@0
   326
#endif
sl@0
   327
sl@0
   328
	delete TheTrapCleanup;
sl@0
   329
	test.End(); //call to end must be after deletion of files - DEF047652
sl@0
   330
	
sl@0
   331
	TheFs.Close();
sl@0
   332
	test.Close();
sl@0
   333
	__UHEAP_MARKEND;
sl@0
   334
	return 0;
sl@0
   335
    }