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