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