os/persistentdata/persistentstorage/dbms/pcdbms/tdbms/src/t_limit.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 <e32test.h>
    24 #include <s32file.h>
    25 
    26 #include "crccheck.h"
    27 
    28 #undef __UHEAP_MARK
    29 #define __UHEAP_MARK
    30 #undef __UHEAP_MARKEND
    31 #define __UHEAP_MARKEND
    32 
    33 LOCAL_D TDBMS_CRCChecks TheCrcChecker;
    34 
    35 #ifndef __linux__ //No CRC test on LINUX
    36 #ifdef __TOOLS2__
    37 const TPtrC	KCrcRecord=_L("\\epoc32\\winscw\\c\\dbms-tst\\T_LIMIT.CRC");
    38 #else
    39 const TPtrC	KCrcRecord=_L("C:\\dbms-tst\\T_LIMIT.CRC");
    40 #endif
    41 #endif
    42 
    43 
    44 LOCAL_D CTrapCleanup* TheTrapCleanup;
    45 LOCAL_D RFs TheFs;
    46 LOCAL_D RDbs TheDbs;
    47 LOCAL_D RDbNamedDatabase TheDatabase;
    48 LOCAL_D RDbTable TheTable;
    49 LOCAL_D RDbView TheView;
    50 
    51 const TInt KTestCleanupStack=0x20;
    52 
    53 #ifdef __TOOLS2__
    54 const TPtrC KTestDatabase=_L(".\\dbms-tst\\T_LIMIT.DB");
    55 #else
    56 const TPtrC KTestDatabase=_L("C:\\dbms-tst\\T_LIMIT.DB");
    57 #endif
    58 
    59 const TPtrC KTableName(_S("TestTable"));
    60 
    61 const TPtrC KColFormat=_L("c%d");
    62 
    63 LOCAL_D RTest test(_L("T_LIMIT - testing table limits"));
    64 
    65 const TInt KRecordLimit=8200;
    66 const TInt KMinInlineLimit=16;
    67 const TInt KMaxInlineLimit=255;
    68 
    69 // expected maxima for record structure
    70 const TInt KMaxColInt64NN=1025;
    71 const TInt KMaxColText8=32;
    72 const TInt KMaxColText16=16;
    73 const TInt KMaxColLongText8=504;
    74 
    75 
    76 LOCAL_C TBool FitBlob(TInt aCount)
    77 //
    78 // Matches heuristics in DBMS
    79 //
    80 	{
    81 	TInt used=(aCount*(2+(KMinInlineLimit<<3))+7)>>3;
    82 	return used<=KRecordLimit;
    83 	}
    84 
    85 LOCAL_C TInt InlineLimit(TInt aCount)
    86 //
    87 // Matches heuristics in DBMS
    88 //
    89 	{
    90 	TInt used=(aCount*(2+(KMinInlineLimit<<3))+7)>>3;
    91 	TInt space=(KRecordLimit-used);//>>1;
    92 	TInt inl=space/aCount+KMinInlineLimit-1;
    93 	return Min(inl,KMaxInlineLimit);
    94 	}
    95 
    96 LOCAL_C void OpenDatabase()
    97 //
    98 // Open the database
    99 //
   100 	{
   101 	test (TheDatabase.Open(TheDbs,KTestDatabase)==KErrNone);
   102 	}
   103 
   104 LOCAL_C void CloseDatabase()
   105 	{
   106 	TheDatabase.Close();
   107 	TheCrcChecker.GenerateCrcL(KTestDatabase);
   108 	}
   109 
   110 LOCAL_C void CreateDatabase()
   111 //
   112 // Create the database-in-a-store
   113 //
   114 	{
   115 	test (TheDatabase.Replace(TheFs,KTestDatabase)==KErrNone);
   116 	CloseDatabase();
   117 	OpenDatabase();
   118 	}
   119 
   120 LOCAL_C void DestroyDatabase()
   121 	{
   122 	test (TheDatabase.Destroy()==KErrNone);
   123 	}
   124 
   125 LOCAL_C CDbColSet* SetLC(TDbCol& aCol,TInt aCount)
   126 	{
   127 	CDbColSet* set=CDbColSet::NewLC();
   128 	TDbColName name;
   129 	while (--aCount>=0)
   130 		{
   131 		name.Format(KColFormat,aCount);
   132 		aCol.iName=name;
   133 		set->AddL(aCol);
   134 		}
   135 	return set;
   136 	}
   137 
   138 LOCAL_C TBool TestL(TDbCol& aCol,TInt aCount)
   139 	{
   140 	test.Printf(_L("\rtesting %d    "),aCount);
   141 	CDbColSet* set=SetLC(aCol,aCount);
   142 	TInt r;
   143 	r=TheDatabase.CreateTable(KTableName,*set);
   144 	if (r==KErrNone)
   145 		{
   146 		CDbColSet* comp=TheDatabase.ColSetL(KTableName);
   147 		test (comp->Count()==aCount);
   148 		delete comp;
   149 		}
   150 	CleanupStack::PopAndDestroy();
   151 	if (r==KErrTooBig)
   152 		return EFalse;
   153 	test (r==KErrNone);
   154 	test (TheDatabase.DropTable(KTableName)==KErrNone);
   155 	return ETrue;
   156 	}
   157 
   158 /**
   159 See how many columns of this sort can be used
   160 
   161 @SYMTestCaseID          SYSLIB-DBMS-CT-0631
   162 @SYMTestCaseDesc        Tests for maximum limits on a Table 
   163 @SYMTestPriority        Medium
   164 @SYMTestActions         Tests for creating a table with maximum number of columns 
   165 @SYMTestExpectedResults Test must not fail
   166 @SYMREQ                 REQ0000
   167 */
   168 LOCAL_C TInt TestTypeL(TDbCol& aCol)
   169 	{
   170 	CreateDatabase();
   171 	TInt ii=1;
   172 	for (;;)
   173 		{
   174 		if (!TestL(aCol,ii))
   175 			break;
   176 		ii<<=1;
   177 		}
   178 	TInt lim=ii>>=1;
   179 	test (lim>0);
   180 	while ((ii>>=1)>0)
   181 		{	// ok<=max<ok+ii*2
   182 		if (TestL(aCol,lim+ii))
   183 			lim+=ii;
   184 		}
   185 	DestroyDatabase();
   186 	test.Printf(_L("\r   create %d     \n"),lim);
   187 	return lim;
   188 	}
   189 
   190 LOCAL_C void StretchRecordL()
   191 	{
   192 	CreateDatabase();
   193 	TDbCol col;
   194 	col.iType=EDbColLongText8;
   195 	col.iMaxLength=KDbUndefinedLength;
   196 	col.iAttributes=0;
   197 	for (TInt ii=4;FitBlob(ii);ii+=4)
   198 		{
   199 		test.Printf(_L("\rtesting %d    "),ii);
   200 		CDbColSet* set=SetLC(col,ii);
   201 		if (ii==4)
   202 			test (TheDatabase.CreateTable(KTableName,*set)==KErrNone);
   203 		else
   204 			test (TheDatabase.AlterTable(KTableName,*set)==KErrNone);
   205 		CleanupStack::PopAndDestroy();
   206 		test (TheTable.Open(TheDatabase,KTableName)==KErrNone);
   207 		if (ii==4)
   208 			TheTable.InsertL();
   209 		else
   210 			{
   211 			TheTable.FirstL();
   212 			TheTable.UpdateL();
   213 			}
   214 		TBuf8<256> buf;
   215 		buf.Fill('-',InlineLimit(ii)/*>>1*/);
   216 		TPtrC8 ptr((const TUint8*)buf.Ptr(),buf.Size());
   217 		TheTable.SetColL(ii-3,ptr);
   218 		TheTable.SetColL(ii-2,ptr);
   219 		TheTable.SetColL(ii-1,ptr);
   220 		TheTable.SetColL(ii,ptr);
   221 		TheTable.PutL();
   222 		TheTable.Close();
   223 //		if ((ii&0x1c)==0)
   224 //			test (TheDatabase.Compact()==KErrNone);
   225 		}
   226 	test (TheDatabase.Compact()==KErrNone);
   227 	test.Printf(_L("\n"));
   228 	CloseDatabase();
   229 	}
   230 
   231 LOCAL_C void doMain()
   232 	{
   233 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0631 TInt64 NOT NULL "));
   234 	TDbCol col;
   235 	col.iType=EDbColInt64;
   236 	col.iMaxLength=KDbUndefinedLength;
   237 	col.iAttributes=TDbCol::ENotNull;
   238 	test (TestTypeL(col)==KMaxColInt64NN);
   239 	test.Next(_L("Text8"));
   240 	col.iType=EDbColText8;
   241 	col.iAttributes=0;
   242 	test (TestTypeL(col)==KMaxColText8);
   243 	test.Next(_L("Text16"));
   244 	col.iType=EDbColText16;
   245 	test (TestTypeL(col)==KMaxColText16);
   246 	test.Next(_L("LongText8"));
   247 	col.iType=EDbColLongText8;
   248 	test (TestTypeL(col)==KMaxColLongText8);
   249 	test.Next(_L("Stretching the record"));
   250 	StretchRecordL();
   251 	}
   252 
   253 LOCAL_C void setupTestDirectory()
   254 //
   255 // Prepare the test directory.
   256 //
   257     {
   258 	TInt r=TheFs.Connect();
   259 	test(r==KErrNone);
   260 //
   261 	r=TheFs.MkDir(KTestDatabase);
   262 	test(r==KErrNone || r==KErrAlreadyExists);
   263 	}
   264 
   265 LOCAL_C void setupCleanup()
   266 //
   267 // Initialise the cleanup stack.
   268 //
   269     {
   270 	TheTrapCleanup=CTrapCleanup::New();
   271 	test(TheTrapCleanup!=NULL);
   272 	TRAPD(r,\
   273 		{\
   274 		for (TInt i=KTestCleanupStack;i>0;i--)\
   275 			CleanupStack::PushL((TAny*)0);\
   276 		CleanupStack::Pop(KTestCleanupStack);\
   277 		});
   278 	test(r==KErrNone);
   279 	}
   280 
   281 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
   282 	{
   283 	RFs fsSession;
   284 	TInt err = fsSession.Connect();
   285 	if(err == KErrNone)
   286 		{
   287 		TEntry entry;
   288 		if(fsSession.Entry(aFullName, entry) == KErrNone)
   289 			{
   290 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
   291 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
   292 			if(err != KErrNone) 
   293 				{
   294 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
   295 				}
   296 			err = fsSession.Delete(aFullName);
   297 			if(err != KErrNone) 
   298 				{
   299 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
   300 				}
   301 			}
   302 		fsSession.Close();
   303 		}
   304 	else
   305 		{
   306 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
   307 		}
   308 	}
   309 
   310 GLDEF_C TInt E32Main()
   311     {
   312 	test.Title();
   313 	setupTestDirectory();
   314 	setupCleanup();
   315 #ifndef __TOOLS2__
   316 	test (TheDbs.Connect()==KErrNone);
   317 #endif
   318 	__UHEAP_MARK;
   319 //
   320 	TRAPD(r,doMain();)
   321 	test(r==KErrNone);
   322 
   323 	test.Printf(_L("Waiting for server exit\n"));
   324 	const TUint KExitDelay=6*0x100000;	// ~6 seconds
   325 	User::After(KExitDelay);
   326 	
   327 	::DeleteDataFile(KTestDatabase);	//deletion of data files must be done before call to end - DEF047652
   328 #ifndef __linux__
   329 	TInt err;
   330 #ifndef __TOOLS2__ 
   331 	TRAPD(lc, err = TheCrcChecker.DumpCrcRecordsL(KCrcRecord));
   332 	test(err==KErrNone);
   333 	test(lc==KErrNone);
   334 #else
   335 	TRAPD(lc, err = TheCrcChecker.ValidateCrcRecordsL(KCrcRecord));
   336 	TPtrC errmsg;
   337 	TheCrcChecker.ErrorReportL(err, errmsg);
   338 	RDebug::Print(errmsg);
   339 	test(err==KErrNone || err==TDBMS_CRCChecks::ECrcCheckOk);
   340 #endif
   341 #endif
   342 	test.End();
   343 //
   344 	__UHEAP_MARKEND;
   345 	delete TheTrapCleanup;
   346 
   347 #ifndef __TOOLS2__
   348 	TheDbs.Close();
   349 #endif
   350 	TheFs.Close();
   351 	test.Close();
   352 	return 0;
   353     }