persistentstorage/dbms/tdbms/t_dbclient.cpp
changeset 0 08ec8eefde2f
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 #include <e32std.h>
       
    17 #include <d32dbms.h>
       
    18 #include <e32test.h>
       
    19 
       
    20 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    21 
       
    22 LOCAL_D RTest test(_L("t_dbclient"));
       
    23 
       
    24 LOCAL_D const TPtrC KColName(_S("A_column_name"));
       
    25 LOCAL_D TDbColName VarName(_S("Column_A"));
       
    26 
       
    27 const TInt KTestCleanupStack=0x20;
       
    28 const TInt KLeaveError=-4000;
       
    29 const TInt KColCount=26;
       
    30 
       
    31 /**
       
    32 @SYMTestCaseID          SYSLIB-DBMS-CT-0586
       
    33 @SYMTestCaseDesc        Tests for TDbCol class
       
    34 @SYMTestPriority        Medium
       
    35 @SYMTestActions        	Tests for column name and type after creating them.
       
    36 @SYMTestExpectedResults Test must not fail
       
    37 @SYMREQ                 REQ0000
       
    38 */
       
    39 LOCAL_C void TestTDbCol()
       
    40 	{
       
    41 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0586 Testing TDbCol "));
       
    42 	TDbCol c1(KColName,EDbColText8);
       
    43 	test(c1.iName==KColName);
       
    44 	test(c1.iType==EDbColText8);
       
    45 	test(c1.iMaxLength==KDbDefaultTextColLength);
       
    46 	test(c1.iAttributes==0);
       
    47 	TDbCol c2(KColName,EDbColText8,1234);
       
    48 	test(c2.iName==KColName);
       
    49 	test(c2.iType==EDbColText8);
       
    50 	test(c2.iMaxLength==1234);
       
    51 	test(c2.iAttributes==0);
       
    52 	TDbCol c3(KColName,EDbColBit);
       
    53 	test(c3.iName==KColName);
       
    54 	test(c3.iType==EDbColBit);
       
    55 	test(c3.iMaxLength==KDbUndefinedLength);
       
    56 	test(c3.iAttributes==0);
       
    57 	}
       
    58 
       
    59 /**
       
    60 @SYMTestCaseID          SYSLIB-DBMS-CT-0587
       
    61 @SYMTestCaseDesc        CDbColSet class test
       
    62 @SYMTestPriority        Medium
       
    63 @SYMTestActions        	Tests for creation of a CDbColSet column set object
       
    64                         Tests for adding and removing columns to the column set
       
    65 @SYMTestExpectedResults Test must not fail
       
    66 @SYMREQ                 REQ0000
       
    67 */
       
    68 LOCAL_C void TestCDbColSetL()
       
    69 	{
       
    70 	TInt r;
       
    71 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0587 ctor and dtor "));
       
    72 	CDbColSet* c=CDbColSet::NewL();		// assume it will succeed
       
    73 	test(c!=NULL);
       
    74 	test (c->Count()==0);
       
    75 	delete c;
       
    76 	__UHEAP_CHECK(0);
       
    77 #if defined(_DEBUG)
       
    78 	__UHEAP_FAILNEXT(1);
       
    79 	TRAP(r,c=CDbColSet::NewL());
       
    80 	test(r!=KErrNone);
       
    81 	__UHEAP_CHECK(0);
       
    82 #endif
       
    83 	c=CDbColSet::NewLC();
       
    84 	test(c!=NULL);
       
    85 	CleanupStack::PopAndDestroy();
       
    86 	__UHEAP_CHECK(0);
       
    87 	c=CDbColSet::NewL();
       
    88 	TDbCol col(KColName,EDbColText,20);
       
    89 	c->AddL(col);
       
    90 	delete c;
       
    91 	__UHEAP_CHECK(0);
       
    92 	TRAP(r,c=CDbColSet::NewLC();c->AddL(col);User::Leave(KLeaveError););
       
    93 	test(r==KLeaveError);
       
    94 	__UHEAP_CHECK(0);
       
    95 //
       
    96 	test.Next(_L("Add columns"));
       
    97 	c=CDbColSet::NewLC();
       
    98 	TInt ii;
       
    99 	for (ii=1;ii<=KColCount;ii++)
       
   100 		{
       
   101 		VarName[7]=(TUint8)('A'-1+ii);
       
   102 		test(c->Col(VarName)==NULL);
       
   103 		TDbCol column(VarName,EDbColInt32);
       
   104 		c->AddL(column);
       
   105 		test(c->Count()==ii);
       
   106 		test(c->ColNo(VarName)!=KDbNullColNo);
       
   107 		test(c->Col(VarName)!=NULL);
       
   108 		}
       
   109 	test.Next(_L("Check columns: operator[] and ColNo()"));
       
   110 	for (ii=1;ii<=KColCount;ii++)
       
   111 		{
       
   112 		const TDbCol& col=(*c)[ii];
       
   113 		test(c->ColNo(col.iName)==ii);
       
   114 		}
       
   115 	test.Next(_L("Remove columns"));
       
   116 	for (ii=1;ii<=KColCount;ii+=2)
       
   117 		{
       
   118 		VarName[7]=(TUint8)('A'-1+ii);
       
   119 		c->Remove(VarName);
       
   120 		}
       
   121 	test(c->Count()==13);
       
   122 	test.Next(_L("Clear"));
       
   123 	c->Clear();
       
   124 	test(c->Count()==0);
       
   125 	CleanupStack::PopAndDestroy();
       
   126 	__UHEAP_CHECK(0);
       
   127 	test.End();
       
   128 	}
       
   129 
       
   130 /**
       
   131 @SYMTestCaseID          SYSLIB-DBMS-CT-0588
       
   132 @SYMTestCaseDesc        Tests for TDbColSetIter class
       
   133 @SYMTestPriority        Medium
       
   134 @SYMTestActions        	Tests for iterate over the contents of a column set
       
   135 @SYMTestExpectedResults Test must not fail
       
   136 @SYMREQ                 REQ0000
       
   137 */
       
   138 LOCAL_C void TestTDbColSetIterL()
       
   139 	{
       
   140 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0588 Testing TDbColSetIter "));
       
   141 	CDbColSet* c=CDbColSet::NewLC();
       
   142 	TInt ii;
       
   143 	for (ii=0;ii<KColCount;++ii)
       
   144 		{
       
   145 		VarName[7]=(TUint8)('A'-1+ii);
       
   146 		c->AddL(TDbCol(VarName,EDbColInt32));
       
   147 		}
       
   148 	TDbColSetIter iter1(*c);
       
   149 	TDbColSetIter iter2(iter1);
       
   150 	ii=0;
       
   151 	for (;;)
       
   152 		{
       
   153 		test(iter1.Col()==++ii);
       
   154 		test(iter1.operator->()==&iter1.operator*());
       
   155 		test(&*iter1++==&*iter2);
       
   156 		if (!iter1)
       
   157 			break;
       
   158 		test(&*iter1==&*++iter2);
       
   159 		}
       
   160 	test(!++iter2);
       
   161 	test(ii==KColCount);
       
   162 	CleanupStack::PopAndDestroy();
       
   163 	__UHEAP_CHECK(0);
       
   164 	}
       
   165 
       
   166 /**
       
   167 @SYMTestCaseID          SYSLIB-DBMS-CT-0589
       
   168 @SYMTestCaseDesc        Tests for TDbCol,CDbColSet,TDbColSetIter classes
       
   169 @SYMTestPriority        Medium
       
   170 @SYMTestActions        	Executes the tests of TDbCol,CDbColSet,TDbColSetIter
       
   171 @SYMTestExpectedResults Test must not fail
       
   172 @SYMREQ                 REQ0000
       
   173 */
       
   174 LOCAL_C void TestColSetL()
       
   175 	{
       
   176 	test.Next(_L("@SYMTestCaseID:SYSLIB-DBMS-CT-0589 "));
       
   177 	TestTDbCol();
       
   178 	test.Next(_L("Testing CDbColSet"));
       
   179 	TestCDbColSetL();
       
   180 	TestTDbColSetIterL();
       
   181 	test.End();
       
   182 	}
       
   183 
       
   184 /**
       
   185 @SYMTestCaseID          SYSLIB-DBMS-CT-0590
       
   186 @SYMTestCaseDesc        Tests for TDbKeyCol class
       
   187 @SYMTestPriority        Medium
       
   188 @SYMTestActions         Attempts to test for attributes of the key column together.
       
   189 @SYMTestExpectedResults Test must not fail
       
   190 @SYMREQ                 REQ0000
       
   191 */
       
   192 LOCAL_C void TestTDbKeyCol()
       
   193 	{
       
   194 	test.Next(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0590 Testing TDbKeyCol "));
       
   195 	TDbKeyCol c1(KColName);
       
   196 	test(c1.iName==KColName);
       
   197 	test(c1.iOrder==TDbKeyCol::EAsc);
       
   198 	TDbKeyCol c2(KColName,TDbKeyCol::EDesc);
       
   199 	test(c2.iName==KColName);
       
   200 	test(c2.iOrder==TDbKeyCol::EDesc);
       
   201 	}
       
   202 
       
   203 /**
       
   204 @SYMTestCaseID          SYSLIB-DBMS-CT-0591
       
   205 @SYMTestCaseDesc        Tests the CDbKey class
       
   206 @SYMTestPriority        Medium
       
   207 @SYMTestActions         Tests for the new CDbKey creation using NewL and NewLC functions
       
   208 @SYMTestExpectedResults Test must not fail
       
   209 @SYMREQ                 REQ0000
       
   210 */
       
   211 LOCAL_C void TestCDbKeyL()
       
   212 	{
       
   213 	TInt r;
       
   214 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0591 ctor and dtor "));
       
   215 	CDbKey* k=CDbKey::NewL();		// assume it will succeed
       
   216 	test(k!=NULL);
       
   217 	delete k;
       
   218 	__UHEAP_CHECK(0);
       
   219 #if defined(_DEBUG)
       
   220 	__UHEAP_FAILNEXT(1);
       
   221 	TRAP(r,k=CDbKey::NewL());
       
   222 	test(r!=KErrNone);
       
   223 	__UHEAP_CHECK(0);
       
   224 #endif
       
   225 	k=CDbKey::NewLC();
       
   226 	test(k!=NULL);
       
   227 	CleanupStack::PopAndDestroy();
       
   228 	__UHEAP_CHECK(0);
       
   229 	k=CDbKey::NewL();
       
   230 	k->AddL(VarName);
       
   231 	delete k;
       
   232 	__UHEAP_CHECK(0);
       
   233 	TRAP(r,k=CDbKey::NewLC();k->AddL(KColName);User::Leave(KLeaveError););
       
   234 	test(r==KLeaveError);
       
   235 	__UHEAP_CHECK(0);
       
   236 //
       
   237 	test.Next(_L("Add key columns"));
       
   238 	k=CDbKey::NewLC();
       
   239 	TInt ii;
       
   240 	for (ii=1;ii<=KColCount;++ii)
       
   241 		{
       
   242 		VarName[7]=(TUint8)('A'-1+ii);
       
   243 		k->AddL(VarName);
       
   244 		test(k->Count()==ii);
       
   245 		test((*k)[ii-1].iName==VarName);
       
   246 		}
       
   247 	test.Next(_L("Remove key columns"));
       
   248 	for (ii=1;ii<=KColCount;ii+=2)
       
   249 		{
       
   250 		VarName[7]=TUint8('A'-1+ii);
       
   251 		k->Remove(VarName);
       
   252 		}
       
   253 	test(k->Count()==KColCount/2);
       
   254 	test.Next(_L("Clear"));
       
   255 	k->Clear();
       
   256 	test(k->Count()==0);
       
   257 	test.Next(_L("Unique flag"));
       
   258 	test(!k->IsUnique());
       
   259 	k->MakeUnique();
       
   260 	test(k->IsUnique());
       
   261 	k->MakeUnique();
       
   262 	test(k->IsUnique());
       
   263 	CleanupStack::PopAndDestroy();
       
   264 	__UHEAP_CHECK(0);
       
   265 	test.End();
       
   266 	}
       
   267 
       
   268 /**
       
   269 @SYMTestCaseID          SYSLIB-DBMS-CT-0592
       
   270 @SYMTestCaseDesc        Tests the TDbKeyCol,CDbKey classes
       
   271 @SYMTestPriority        Medium
       
   272 @SYMTestActions         Executes the TDbKeyCol,CDbKey tests
       
   273 @SYMTestExpectedResults Test must not fail
       
   274 @SYMREQ                 REQ0000
       
   275 */
       
   276 LOCAL_C void TestKeyL()
       
   277 	{
       
   278 	test.Start(_L(" @SYMTestCaseID:SYSLIB-DBMS-CT-0592 "));
       
   279 	TestTDbKeyCol();
       
   280 	test.Next(_L("Testing CDbKey"));
       
   281 	TestCDbKeyL();
       
   282 	test.End();
       
   283 	}
       
   284 
       
   285 
       
   286 LOCAL_C void setupCleanup()
       
   287 //
       
   288 // Initialise the cleanup stack.
       
   289 //
       
   290     {
       
   291 	TheTrapCleanup=CTrapCleanup::New();
       
   292 	test(TheTrapCleanup!=NULL);
       
   293 	TRAPD(r,\
       
   294 		{\
       
   295 		for (TInt i=KTestCleanupStack;i>0;i--)\
       
   296 			CleanupStack::PushL((TAny*)0);\
       
   297 		CleanupStack::Pop(KTestCleanupStack);\
       
   298 		});
       
   299 	test(r==KErrNone);
       
   300 	}
       
   301 
       
   302 GLDEF_C TInt E32Main()
       
   303 //
       
   304 // Test Client-side objects
       
   305 //
       
   306     {
       
   307 	test.Title();
       
   308 	setupCleanup();
       
   309 	__UHEAP_MARK;
       
   310 //
       
   311 	test.Start(_L("Test the Column Set"));
       
   312 	TRAPD(r,TestColSetL();)
       
   313 	test(r==KErrNone);
       
   314 	__UHEAP_CHECK(0);
       
   315 	test.Next(_L("Test the Key"));
       
   316 	TRAP(r,TestKeyL();)
       
   317 	test(r==KErrNone);
       
   318 	test.End();
       
   319 //
       
   320 	__UHEAP_MARKEND;
       
   321 	delete TheTrapCleanup;
       
   322 	test.Close();
       
   323 	return 0;
       
   324     }