kerneltest/e32test/system/t_cobj.cpp
changeset 0 a41df078684a
child 231 75252ea6123b
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 1995-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 the License "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 // e32test\system\t_cobj.cpp
       
    15 // Overview:
       
    16 // Test CObject, CObjectIx, CObjectCon, CObjectConIx classes
       
    17 // API Information:
       
    18 // CObject, CObjectIx, CObjectCon, CObjectConIx
       
    19 // Details:
       
    20 // - Test the CObjectIx class by adding and removing CObject objects
       
    21 // in the same order, in reverse order and in random order.
       
    22 // - Test adding many CObject objects to a CObjectConIx and test that
       
    23 // the returned handle is as expected.
       
    24 // - Perform a speed test on CObjectCon for both named and unnamed
       
    25 // objects.
       
    26 // - Check that the CObject, CObjectCon, CObjectIx and CObjectConIx methods 
       
    27 // are in the DLL by calling each one.
       
    28 // - Test the CObject, CObjectCon, CObjectIx and CObjectConIx methods and
       
    29 // verify the results are as expected.
       
    30 // - Test all the objects together: create two containers, find objects by 
       
    31 // name in a variety of ways, delete objects and verify the results are 
       
    32 // as expected.
       
    33 // Platforms/Drives/Compatibility:
       
    34 // All.
       
    35 // Assumptions/Requirement/Pre-requisites:
       
    36 // Failures and causes:
       
    37 // Base Port information:
       
    38 // 
       
    39 //
       
    40 
       
    41 #include <e32std.h>
       
    42 #include <e32std_private.h>
       
    43 #include <e32base.h>
       
    44 #include <e32base_private.h>
       
    45 #include <e32test.h>
       
    46 #include <e32svr.h>
       
    47 #include <e32ver.h>
       
    48 #include "../misc/prbs.h"
       
    49 
       
    50 struct TCObjectDump
       
    51 	{
       
    52 	TInt 		iAccessCount;
       
    53 	CObject*	iOwner;
       
    54 	CObjectCon*	iContainer;
       
    55 	HBufC*		iName;
       
    56 	};
       
    57 
       
    58 void CObject::__DbgTest(void* pCObjectDump) const
       
    59 	{
       
    60 	((TCObjectDump*)pCObjectDump)->iAccessCount=iAccessCount;
       
    61 	((TCObjectDump*)pCObjectDump)->iOwner=iOwner;
       
    62 	((TCObjectDump*)pCObjectDump)->iContainer=iContainer;
       
    63 	((TCObjectDump*)pCObjectDump)->iName=iName;
       
    64 	}
       
    65 
       
    66 LOCAL_D RTest test(_L("T_COBJ"));
       
    67 LOCAL_D	TName gName;
       
    68 LOCAL_D	TName gName1;
       
    69 LOCAL_D	TName gName2;
       
    70 LOCAL_D	TFullName gFullName;
       
    71 LOCAL_D	TFullName gFullName1;
       
    72 LOCAL_D	TFullName gFullName2;
       
    73 
       
    74 class RTestHeap : public RAllocator
       
    75 	{
       
    76 public:
       
    77 	enum TOp {EOpNone=0, EOpAlloc=1, EOpFree=2, EOpReAlloc=3};
       
    78 
       
    79 	struct TOpInfo
       
    80 		{
       
    81 		TInt	iOp;
       
    82 		TAny*	iPtrArg;
       
    83 		TInt	iIntArg;
       
    84 		TAny*	iResult;
       
    85 		};
       
    86 public:
       
    87 	static RTestHeap* Install();
       
    88 	void Uninstall();
       
    89 	TInt GetLastOp(TOpInfo& aInfo);
       
    90 	virtual TAny* Alloc(TInt aSize);
       
    91 	virtual void Free(TAny* aPtr);
       
    92 	virtual TAny* ReAlloc(TAny* aPtr, TInt aSize, TInt aMode);
       
    93 	virtual TInt AllocLen(const TAny* aCell) const;
       
    94 	virtual TInt Compress();
       
    95 	virtual void Reset();
       
    96 	virtual TInt AllocSize(TInt& aTotalAllocSize) const;
       
    97 	virtual TInt Available(TInt& aBiggestBlock) const;
       
    98 	virtual TInt DebugFunction(TInt aFunc, TAny* a1, TAny* a2);
       
    99 public:
       
   100 	RAllocator* iA;
       
   101 	TOpInfo iLastOp;
       
   102 	};
       
   103 
       
   104 RTestHeap* RTestHeap::Install()
       
   105 	{
       
   106 	RTestHeap* p = new RTestHeap;
       
   107 	test(p!=0);
       
   108 	p->iA = &User::Heap();
       
   109 	p->iLastOp.iOp = EOpNone;
       
   110 	User::SwitchHeap(p);
       
   111 	return p;
       
   112 	}
       
   113 
       
   114 void RTestHeap::Uninstall()
       
   115 	{
       
   116 	User::SwitchHeap(iA);
       
   117 	delete this;
       
   118 	}
       
   119 
       
   120 TInt RTestHeap::GetLastOp(RTestHeap::TOpInfo& aInfo)
       
   121 	{
       
   122 	memcpy(&aInfo, &iLastOp, sizeof(TOpInfo));
       
   123 	iLastOp.iOp = EOpNone;
       
   124 	return (aInfo.iOp == EOpNone) ? KErrNotFound : KErrNone;
       
   125 	}
       
   126 
       
   127 TAny* RTestHeap::Alloc(TInt aSize)
       
   128 	{
       
   129 	TAny* p = iA->Alloc(aSize);
       
   130 	iLastOp.iOp = EOpAlloc;
       
   131 	iLastOp.iPtrArg = 0;
       
   132 	iLastOp.iIntArg = aSize;
       
   133 	iLastOp.iResult = p;
       
   134 	return p;
       
   135 	}
       
   136 
       
   137 void RTestHeap::Free(TAny* aPtr)
       
   138 	{
       
   139 	iLastOp.iOp = EOpFree;
       
   140 	iLastOp.iPtrArg = aPtr;
       
   141 	iLastOp.iIntArg = 0;
       
   142 	iLastOp.iResult = 0;
       
   143 	iA->Free(aPtr);
       
   144 	}
       
   145 
       
   146 TAny* RTestHeap::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode=0)
       
   147 	{
       
   148 	TAny* p = iA->ReAlloc(aPtr,aSize,aMode);
       
   149 	iLastOp.iOp = EOpReAlloc;
       
   150 	iLastOp.iPtrArg = aPtr;
       
   151 	iLastOp.iIntArg = aSize;
       
   152 	iLastOp.iResult = p;
       
   153 	return p;
       
   154 	}
       
   155 
       
   156 TInt RTestHeap::AllocLen(const TAny* aCell) const
       
   157 	{
       
   158 	TInt l = iA->AllocLen(aCell);
       
   159 	return l;
       
   160 	}
       
   161 
       
   162 TInt RTestHeap::Compress()
       
   163 	{
       
   164 	TInt l = iA->Compress();
       
   165 	return l;
       
   166 	}
       
   167 
       
   168 void RTestHeap::Reset()
       
   169 	{
       
   170 	iA->Reset();
       
   171 	}
       
   172 
       
   173 TInt RTestHeap::AllocSize(TInt& aTotalAllocSize) const
       
   174 	{
       
   175 	TInt s;
       
   176 	TInt r = iA->AllocSize(s);
       
   177 	aTotalAllocSize = s;
       
   178 	return r;
       
   179 	}
       
   180 
       
   181 TInt RTestHeap::Available(TInt& aBiggestBlock) const
       
   182 	{
       
   183 	TInt s;
       
   184 	TInt r = iA->Available(s);
       
   185 	aBiggestBlock = s;
       
   186 	return r;
       
   187 	}
       
   188 
       
   189 TInt RTestHeap::DebugFunction(TInt aFunc, TAny* a1=NULL, TAny* a2=NULL)
       
   190 	{
       
   191 	TInt r = iA->DebugFunction(aFunc, a1, a2);
       
   192 	return r;
       
   193 	}
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 class TestCObjects
       
   199 	{
       
   200 public:
       
   201 	void Test1(void);
       
   202 	void Test2(void);
       
   203 	void Test3(void);
       
   204 	void Test4(void);
       
   205 	void Test5(void);
       
   206 	void Test6(void);
       
   207 	void Test7(void);
       
   208 	void Test8(void);
       
   209 private:
       
   210 	static void GetObjName(TName& aDest,const CObject& aObj);
       
   211 	static void GetObjFullName(TFullName& aDest,const CObject& aObj);
       
   212 	};
       
   213 
       
   214 
       
   215 GLDEF_C void TestCObjects::Test1(void)
       
   216 	{
       
   217 	// Check CObject methods are in the DLL
       
   218 	CObject* pCObject=new CObject; 
       
   219 	CObject* temp=new CObject;
       
   220 
       
   221 	pCObject->Open();
       
   222 	pCObject->Close();
       
   223 	gName=pCObject->Name();
       
   224 	TPtrC aDesR(_S("a name"));
       
   225 	pCObject->SetName(&aDesR);
       
   226 	pCObject->SetNameL(&aDesR);
       
   227 	gFullName=pCObject->FullName();
       
   228 	pCObject->Owner();
       
   229 	pCObject->SetOwner(temp);
       
   230 	pCObject->Close(); // deletes object when iAccessCount==0
       
   231 	temp->Close();
       
   232 	}
       
   233 
       
   234 GLDEF_C void TestCObjects::Test2(void)
       
   235 	{
       
   236 	// Check CObjectCon methods are in the DLL
       
   237 	CObjectCon* pCObjectCon=CObjectCon::NewL();
       
   238 	CObject* pCObject=new CObject;
       
   239 	TInt aFindHandle=0;
       
   240 	
       
   241 	pCObjectCon->AddL(pCObject);
       
   242 	pCObjectCon->At(aFindHandle);
       
   243 	User::ValidateName(_L("a name"));
       
   244 	pCObjectCon->CheckUniqueFullName(pCObject, _L("a name"));
       
   245 	pCObjectCon->FindByName(aFindHandle, _L("a name"), gName); 
       
   246 	pCObjectCon->FindByFullName(aFindHandle, _L("a Name"), gFullName);
       
   247 	pCObjectCon->UniqueID();
       
   248 	pCObjectCon->Count();
       
   249 	pCObjectCon->Remove(pCObject); 
       
   250 	pCObject->Close();
       
   251 	delete pCObjectCon;
       
   252 	}
       
   253 
       
   254 GLDEF_C void TestCObjects::Test3(void)
       
   255 	{	
       
   256 	// Check the CObjectIx methods are in the DLL		  
       
   257 	CObjectIx* pCObjectIx=CObjectIx::NewL();
       
   258 	pCObjectIx->Count();
       
   259 	test(pCObjectIx->At(0)==NULL);
       
   260 	delete pCObjectIx;
       
   261 	pCObjectIx=CObjectIx::NewL();
       
   262 	test(pCObjectIx->Count()==0);
       
   263 	CObjectConIx* pCObjectConIx=CObjectConIx::NewL();
       
   264 	delete pCObjectConIx;
       
   265 	pCObjectConIx=CObjectConIx::NewL();
       
   266 	CObjectCon* pCObjectCon=pCObjectConIx->CreateL();
       
   267 	test(pCObjectCon->Count()==0);
       
   268 	CObject* pCObject= new CObject;
       
   269 	test(pCObject!=NULL);
       
   270 
       
   271 	TInt find;
       
   272 	TName name;
       
   273 	TInt r=pCObjectCon->FindByName(find,_L("Name"),name);
       
   274 	test(r==KErrNotFound);
       
   275 	TFullName fullName;
       
   276 	r=pCObjectCon->FindByFullName(find,_L("Full Name"),fullName);
       
   277 	test(r==KErrNotFound);
       
   278 
       
   279 
       
   280 	pCObjectCon->AddL(pCObject);
       
   281 	test(pCObjectCon->Count()==1);
       
   282 	test(pCObjectIx->Count(pCObject)==0);
       
   283 	pCObjectIx->AddL(pCObject); 
       
   284 	test(pCObjectIx->Count(pCObject)==1);
       
   285 	pCObjectIx->Count();
       
   286 	pCObjectIx->At(0, 0);
       
   287 	pCObjectIx->At(0);
       
   288 //	pCObjectIx->Remove(0);
       
   289 	delete pCObjectIx;
       
   290 	delete pCObjectConIx;
       
   291 //	delete pCObjectCon;
       
   292 //	pCObject->Close();
       
   293 	}
       
   294 
       
   295 GLDEF_C void TestCObjects::Test4(void)
       
   296 	{
       
   297 	// Check CObjectConIx methods are in the DLL
       
   298 	CObjectConIx* pCObjectConIx=CObjectConIx::NewL();
       
   299 	CObjectCon* pCObjectCon=pCObjectConIx->CreateL();
       
   300 	pCObjectConIx->Lookup(0);
       
   301 	pCObjectConIx->Remove(pCObjectCon);
       
   302 	delete pCObjectConIx;
       
   303 	}
       
   304 
       
   305 GLDEF_C void TestCObjects::Test5(void)
       
   306 	{
       
   307 	// Test the methods of the CObject class
       
   308 	TCObjectDump CObjectDump;
       
   309 	CObject* a=new CObject;
       
   310 	CObject* b=new CObject;
       
   311 	a->__DbgTest(&CObjectDump);
       
   312 	test(CObjectDump.iAccessCount==1 && CObjectDump.iOwner==NULL && CObjectDump.iContainer==NULL);
       
   313 	a->Open();
       
   314 	a->__DbgTest(&CObjectDump);
       
   315 	test(CObjectDump.iAccessCount==2);
       
   316 	a->Close();
       
   317 	a->__DbgTest(&CObjectDump);
       
   318 	test(CObjectDump.iAccessCount==1);
       
   319 	TPtrC aDesR(_L("aName"));
       
   320 	a->SetName(&aDesR);
       
   321     gName=a->Name();
       
   322 	test(gName==_L("aName"));
       
   323     gFullName=a->FullName();
       
   324 	test(gFullName==_L("aName"));
       
   325 	TPtrC aDesR2(_L("Owner"));
       
   326 	b->SetName(&aDesR2);
       
   327 	a->SetOwner(b);
       
   328 	test(a->Owner()==b);
       
   329     gFullName=a->FullName();
       
   330 	test(gFullName==_L("Owner::aName"));
       
   331 	a->Close();  // Calls the destructor via the call to delete
       
   332 // To Do: set iContainer to something then call Close() 
       
   333 	}
       
   334 
       
   335 
       
   336 GLDEF_C void TestCObjects::Test6(void)
       
   337 	{
       
   338 	// Test the methods of CObjectCon
       
   339 	TCObjectDump dump1, dump2;
       
   340 	TInt aFindHandle=0;
       
   341 	CObject	*pObj1=new CObject,
       
   342 			*pObj2=new CObject,
       
   343 			*pObj3=new CObject,
       
   344 			*temp;
       
   345 	CObjectCon* pCon=CObjectCon::NewL();
       
   346 
       
   347 	test(User::ValidateName(_L("xxx*xxx"))==KErrBadName);
       
   348 	test(User::ValidateName(_L("xxx?xxx"))==KErrBadName);
       
   349 	test(User::ValidateName(_L("xxx:xxx"))==KErrBadName);
       
   350 	test(User::ValidateName(_L("xxxxxxx"))==KErrNone);
       
   351 
       
   352 	TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Owner"));
       
   353 	pObj1->SetName(&name1);
       
   354 	pObj2->SetName(&name2);
       
   355 	pObj3->SetName(&name3);
       
   356 
       
   357 	test(pCon->Count()==0);
       
   358 	pCon->AddL(pObj1);
       
   359 	test(pCon->Count()==1);
       
   360 	pCon->AddL(pObj2);
       
   361 	test(pCon->Count()==2);
       
   362 
       
   363 	aFindHandle=0;
       
   364 	test(pCon->FindByName(aFindHandle, _L("xxx"), gName)==KErrNotFound);
       
   365 	aFindHandle=0;
       
   366 	GetObjName(gName1,*pObj1);
       
   367 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
       
   368 	test(aFindHandle==0);
       
   369 	aFindHandle=2;
       
   370 	GetObjName(gName1,*pObj1);
       
   371 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNotFound);
       
   372 	aFindHandle=0;
       
   373 	GetObjName(gName1,*pObj2);
       
   374 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
       
   375 	test(aFindHandle==1);
       
   376 
       
   377 	aFindHandle=0;
       
   378 	test(pCon->FindByFullName(aFindHandle, _L("xxx"), gFullName)==KErrNotFound);
       
   379 	aFindHandle=0;
       
   380 	GetObjName(gName1,*pObj1);
       
   381 	test(pCon->FindByFullName(aFindHandle, gName1, gFullName)==KErrNone);
       
   382 	test(aFindHandle==0);
       
   383 	GetObjName(gName1,*pObj2);
       
   384 	test(pCon->FindByFullName(aFindHandle, gName1, gFullName)==KErrNone);
       
   385 	test(aFindHandle==1);
       
   386 
       
   387 	pObj1->SetOwner(pObj3);
       
   388 	pObj2->SetOwner(pObj3);
       
   389 	aFindHandle=0;
       
   390 	test(pCon->FindByFullName(aFindHandle, _L("xxx"), gFullName)==KErrNotFound);
       
   391 	aFindHandle=0;
       
   392 	GetObjFullName(gFullName1,*pObj1);
       
   393 	test(pCon->FindByFullName(aFindHandle, gFullName1, gFullName)==KErrNone);
       
   394 	test(aFindHandle==0);
       
   395 	GetObjFullName(gFullName1,*pObj2);
       
   396 	test(pCon->FindByFullName(aFindHandle, gFullName1, gFullName)==KErrNone);
       
   397 	test(aFindHandle==1);
       
   398 
       
   399 
       
   400 	test(pCon->CheckUniqueFullName(pObj3, _L("aname"))==KErrNone);
       
   401 	GetObjName(gName1,*pObj1);
       
   402 	test(pCon->CheckUniqueFullName(pObj3, gName1)==KErrAlreadyExists);
       
   403 	pCon->Remove(pObj1);
       
   404 	test(pCon->CheckUniqueFullName(pObj3, gName1)==KErrNone);
       
   405 	test(pCon->Count()==1);
       
   406 	pCon->AddL(pObj3);
       
   407 	test(pCon->Count()==2);
       
   408 	aFindHandle=0;
       
   409 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNotFound);
       
   410 	aFindHandle=0;
       
   411 	GetObjFullName(gFullName1,*pObj1);
       
   412 	test(pCon->FindByFullName(aFindHandle, gFullName1, gFullName)==KErrNotFound);
       
   413 	aFindHandle=0;
       
   414 	GetObjName(gName1,*pObj2);
       
   415 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
       
   416 	test(aFindHandle==0);
       
   417 	GetObjFullName(gFullName1,*pObj2);
       
   418 	test(pCon->FindByFullName(aFindHandle, gFullName1, gFullName)==KErrNone);
       
   419 	test(aFindHandle==0);
       
   420 	GetObjName(gName1,*pObj3);
       
   421 	test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
       
   422 	test(aFindHandle==1);
       
   423 	aFindHandle=0;
       
   424 	test(pCon->FindByFullName(aFindHandle, _L("Owner"), gFullName)==KErrNone);
       
   425 	test(aFindHandle==1);
       
   426 
       
   427 
       
   428 	pObj2->__DbgTest(&dump1);
       
   429 	temp=pCon->At(0);
       
   430 	temp->__DbgTest(&dump2);
       
   431 	test(dump1.iAccessCount==dump2.iAccessCount  && dump1.iOwner==dump2.iOwner && 
       
   432 			dump1.iName==dump2.iName &&dump1.iContainer==dump2.iContainer); 
       
   433 	pObj3->__DbgTest(&dump1);
       
   434 	temp=pCon->At(1);
       
   435 	temp->__DbgTest(&dump2);
       
   436 	test(dump1.iAccessCount==dump2.iAccessCount  && dump1.iOwner==dump2.iOwner && 
       
   437 			dump1.iName==dump2.iName &&dump1.iContainer==dump2.iContainer); 
       
   438 
       
   439 	pCon->Remove(pObj2);
       
   440 	pCon->Remove(pObj3);
       
   441 	test(pCon->Count()==0);
       
   442 	delete pCon;
       
   443 
       
   444 	// Test expansion and shrinking
       
   445 	__UHEAP_MARK;
       
   446 	pCon=CObjectCon::NewL();
       
   447 	CObject** obj = new CObject*[2048];
       
   448 	test(obj!=0);
       
   449 	TInt i;
       
   450 	for (i=0; i<2048; ++i)
       
   451 		{
       
   452 		obj[i] = new CObject;
       
   453 		test(obj[i] != 0);
       
   454 		}
       
   455 
       
   456 	RTestHeap* h = RTestHeap::Install();
       
   457 
       
   458 	const TInt xov_values[] = {0,8,12,16,24,32,48,64,96,128,192,256,384,512,768,1024,1536,2048};
       
   459 	const TInt xov2_values[] = {1472, 960, 704, 448, 320, 192, 128, 96, 64, 48, 32, 24, 16, 12, 8, 6, 0};
       
   460 	const TInt* xov_ptr = xov_values;
       
   461 	const TInt* xov2_ptr = xov2_values;
       
   462 	TInt sz = 0;
       
   463 	TInt xov = 0;
       
   464 	TAny* ptr = 0;
       
   465 	for (i=1; i<=2048; ++i)
       
   466 		{
       
   467 		pCon->AddL(obj[i-1]);
       
   468 		test(pCon->Count()==i);
       
   469 		RTestHeap::TOpInfo opi;
       
   470 		TInt r = h->GetLastOp(opi);
       
   471 		if (i*4 <= sz)
       
   472 			{
       
   473 			test(r==KErrNotFound);
       
   474 			continue;
       
   475 			}
       
   476 		test(r==KErrNone);
       
   477 		test.Printf(_L("Realloc at %d -> %d\n"), i, opi.iIntArg);
       
   478 		test(i-1 == xov);
       
   479 		test(opi.iOp == RTestHeap::EOpReAlloc);
       
   480 		test(opi.iPtrArg == ptr);
       
   481 		// recalc xov
       
   482 		xov = *++xov_ptr;
       
   483 		test(opi.iIntArg == xov*4);
       
   484 		sz = xov*4;
       
   485 		test(opi.iResult != 0);
       
   486 		ptr = opi.iResult;
       
   487 		}
       
   488 
       
   489 	xov = *xov2_ptr;
       
   490 	for (i=2047; i>=0; --i)
       
   491 		{
       
   492 		pCon->Remove(obj[i]);
       
   493 		test(pCon->Count()==i);
       
   494 		RTestHeap::TOpInfo opi;
       
   495 		TInt r = h->GetLastOp(opi);
       
   496 		if (i>xov)
       
   497 			{
       
   498 			test(r==KErrNotFound);
       
   499 			continue;
       
   500 			}
       
   501 		test(r==KErrNone);
       
   502 		test.Printf(_L("Realloc at %d -> %d\n"), i, opi.iIntArg);
       
   503 		test(i == xov);
       
   504 		if (i==0)
       
   505 			{
       
   506 			test(opi.iOp == RTestHeap::EOpReAlloc || opi.iOp == RTestHeap::EOpFree);
       
   507 			}
       
   508 		else
       
   509 			{
       
   510 			test(opi.iOp = RTestHeap::EOpReAlloc);
       
   511 			test(opi.iResult == ptr);
       
   512 			}
       
   513 		test(opi.iPtrArg == ptr);
       
   514 		// recalc xov
       
   515 		xov = *++xov2_ptr;
       
   516 		sz = *--xov_ptr;
       
   517 		test(opi.iIntArg == sz*4);
       
   518 		}
       
   519 
       
   520 	delete pCon;
       
   521 	for (i=0; i<2048; ++i)
       
   522 		obj[i]->Close();
       
   523 	delete[] obj;
       
   524 	h->Uninstall();
       
   525 	__UHEAP_MARKEND;
       
   526 	}
       
   527 
       
   528 
       
   529 GLDEF_C void TestCObjects::Test7(void)
       
   530 	{
       
   531 	// Test the methods of CObjectIx  
       
   532 
       
   533 	// Before an object can be added to a ConIx it first has to be added to a container because the
       
   534 	// CObjectIx::Add method references theObject->iContainer->iName. But theObject->iContainer field
       
   535 	// is only set (within the CObjectCon::Add method) IF the container has first been created by the
       
   536 	// CObjectConIx::Create method, otherwise it is NULL and adding it to a CObjectIx will fail.
       
   537 	CObjectIx		*pIx=CObjectIx::NewL();
       
   538 	CObjectConIx 	*pConIx=CObjectConIx::NewL();
       
   539 	CObjectCon 		*pCon1=pConIx->CreateL(),
       
   540 					*pCon2=pConIx->CreateL();
       
   541 	CObject 		*pObj1=new CObject, 
       
   542 					*pObj2=new CObject, 
       
   543 					*pObj3=new CObject,
       
   544 					*temp;
       
   545 	TInt aHandle1, aHandle2, aHandle3;
       
   546 	TCObjectDump dump1, dump2;	  
       
   547 	TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Obj3"));
       
   548 
       
   549 	// Create two containers with Obj1 and Obj2 in the first and Obj3 in the second
       
   550 	pObj1->SetName(&name1);
       
   551 	pObj2->SetName(&name2);
       
   552 	pObj3->SetName(&name3);			   
       
   553 	pCon1->AddL(pObj1);
       
   554 	pCon1->AddL(pObj2);
       
   555 	pCon2->AddL(pObj3);
       
   556 	aHandle1=pIx->AddL(pObj1);
       
   557 	aHandle2=pIx->AddL(pObj2);
       
   558 	aHandle3=pIx->AddL(pObj3); 
       
   559 	// At(TInt aHandle)
       
   560 	pObj1->__DbgTest(&dump1);
       
   561 	temp=pIx->At(aHandle1);	
       
   562 	temp->__DbgTest(&dump2);
       
   563 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   564 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName); 
       
   565 	pObj2->__DbgTest(&dump1);
       
   566 	temp=pIx->At(aHandle2);
       
   567 	temp->__DbgTest(&dump2);
       
   568 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   569 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName); 
       
   570 	pObj3->__DbgTest(&dump1);
       
   571 	temp=pIx->At(aHandle3);
       
   572 	temp->__DbgTest(&dump2);
       
   573 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   574 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName);
       
   575 	// At(TInt aHandle, TInt aUniqueID);
       
   576 	pObj1->__DbgTest(&dump1);
       
   577 	temp=pIx->At(aHandle1, 1);	
       
   578 	temp->__DbgTest(&dump2);
       
   579 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   580 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName); 
       
   581 	pObj2->__DbgTest(&dump1);
       
   582 	temp=pIx->At(aHandle2, 1);
       
   583 	temp->__DbgTest(&dump2);
       
   584 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   585 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName); 
       
   586 	pObj3->__DbgTest(&dump1);
       
   587 	temp=pIx->At(aHandle3, 2);
       
   588 	temp->__DbgTest(&dump2);
       
   589 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   590 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName);
       
   591 	// Remove(Tint aHandle)
       
   592 	pIx->Remove(aHandle2);
       
   593 	pObj1->__DbgTest(&dump1);
       
   594 	temp=pIx->At(aHandle1);
       
   595 	temp->__DbgTest(&dump2);
       
   596 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   597 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName); 
       
   598 	pObj3->__DbgTest(&dump1);
       
   599 	temp=pIx->At(aHandle3);
       
   600 	temp->__DbgTest(&dump2);
       
   601 	test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
       
   602 		 dump1.iContainer==dump2.iContainer && dump1.iName==dump2.iName);
       
   603 	pIx->Remove(aHandle1);
       
   604 	pIx->Remove(aHandle3);
       
   605 	// pIx->Remove(aHandle3); this will cause a crash
       
   606 
       
   607 	// Removing an object from a CObjectIx calls the objects close method hence in this case
       
   608 	// deleting it
       
   609 	//delete pCon1;	// The destructor for a container deletes its contained objects
       
   610 	//delete pCon2;	
       
   611 	delete pConIx; // The CObjectConIx destructor deletes its containers
       
   612 	delete pIx; // The CObjectIx destructor calls close on all its objects
       
   613 	}
       
   614 
       
   615 GLDEF_C void TestCObjects::Test8(void)
       
   616 	{
       
   617 	// Test all objects together
       
   618 	CObjectIx		*pIx=CObjectIx::NewL();
       
   619 	CObjectConIx 	*pConIx=CObjectConIx::NewL();
       
   620 	CObjectCon 		*pCon1=pConIx->CreateL(),
       
   621 					*pCon2=pConIx->CreateL();
       
   622 	CObject 		*pObj1=new CObject, 
       
   623 					*pObj2=new CObject, 
       
   624 					*pObj3=new CObject,
       
   625 					*pObj4=new CObject,
       
   626 					*pObj5=new CObject,
       
   627 					*temp;
       
   628 	TInt tempHandle;
       
   629 	TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Obj3")), name4(_S("Obj4")), name5(_S("Obj5"));
       
   630 
       
   631 	// Create two containers with Obj1,Obj2, Obj3 in the first and Obj4 and Obj5 in the second
       
   632 	// Obj1 owns Obj2 which owns Obj3
       
   633 	pObj1->SetName(&name1);
       
   634 	pObj2->SetName(&name2);
       
   635 	pObj2->SetOwner(pObj1);
       
   636 	pObj3->SetName(&name3);
       
   637 	pObj3->SetOwner(pObj2);	
       
   638 	pObj4->SetName(&name4);		   
       
   639 	pObj5->SetName(&name5);
       
   640 	pCon1->AddL(pObj1);
       
   641 	pCon1->AddL(pObj2);
       
   642 	pCon1->AddL(pObj3);
       
   643 	pCon2->AddL(pObj4);
       
   644 	pCon2->AddL(pObj5);
       
   645 	pIx->AddL(pObj1);
       
   646 	pIx->AddL(pObj2);
       
   647 	pIx->AddL(pObj3); 
       
   648 	pIx->AddL(pObj4);
       
   649 	pIx->AddL(pObj5);
       
   650 
       
   651 
       
   652 	tempHandle=0;
       
   653 	GetObjName(gName1,*pObj1);
       
   654 	pCon1->FindByName(tempHandle, gName1, gName);
       
   655 	temp=pCon1->At(tempHandle);
       
   656 	GetObjName(gName2,*temp);
       
   657 	test(gName2==gName1);
       
   658 	tempHandle=0;
       
   659 	GetObjFullName(gFullName1,*pObj1);
       
   660 	pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
       
   661 	temp=pCon1->At(tempHandle);
       
   662 	GetObjName(gName2,*temp);
       
   663 	test(gName2==gName1);
       
   664 
       
   665 	tempHandle=0;
       
   666 	GetObjName(gName1,*pObj2);
       
   667 	pCon1->FindByName(tempHandle, gName1, gName);
       
   668 	temp=pCon1->At(tempHandle);
       
   669 	GetObjName(gName2,*temp);
       
   670 	test(gName2==gName1);
       
   671 	tempHandle=0;
       
   672 	GetObjFullName(gFullName1,*pObj2);
       
   673 	pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
       
   674 	temp=pCon1->At(tempHandle);
       
   675 	GetObjName(gName2,*temp);
       
   676 	test(gName2==gName1);
       
   677 
       
   678 	tempHandle=0;
       
   679 	GetObjName(gName1,*pObj3);
       
   680 	pCon1->FindByName(tempHandle, gName1, gName);
       
   681 	temp=pCon1->At(tempHandle);
       
   682 	GetObjName(gName2,*temp);
       
   683 	test(gName2==gName1);
       
   684 	tempHandle=0;
       
   685 	GetObjFullName(gFullName1,*pObj3);
       
   686 	pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
       
   687 	temp=pCon1->At(tempHandle);
       
   688 	GetObjName(gName2,*temp);
       
   689 	test(gName2==gName1);
       
   690 
       
   691 	tempHandle=0;
       
   692 	GetObjName(gName1,*pObj4);
       
   693 	pCon2->FindByName(tempHandle, gName1, gName);
       
   694 	temp=pCon2->At(tempHandle);
       
   695 	GetObjName(gName2,*temp);
       
   696 	test(gName2==gName1);
       
   697 	tempHandle=0;
       
   698 	GetObjFullName(gFullName1,*pObj4);
       
   699 	pCon2->FindByFullName(tempHandle, gFullName1, gFullName);
       
   700 	temp=pCon2->At(tempHandle);
       
   701 	GetObjName(gName2,*temp);
       
   702 	test(gName1==gName2);
       
   703 
       
   704 	tempHandle=0;
       
   705 	GetObjName(gName1,*pObj5);
       
   706 	pCon2->FindByName(tempHandle, gName1, gName);
       
   707 	temp=pCon2->At(tempHandle);
       
   708     GetObjName(gName2,*temp);
       
   709 	test(gName2==gName1);
       
   710 	tempHandle=0;
       
   711 	GetObjFullName(gFullName1,*pObj5);
       
   712 	pCon2->FindByFullName(tempHandle, gFullName1, gFullName);
       
   713 	temp=pCon2->At(tempHandle);
       
   714     GetObjName(gName2,*temp);
       
   715 	test(gName2==gName1);
       
   716 
       
   717 		
       
   718 	tempHandle=0;
       
   719 	test(pCon1->FindByName(tempHandle, _L("*1"), gName)==KErrNone);
       
   720 	GetObjName(gName1,*pObj1);
       
   721 	test(gName==gName1);
       
   722 	tempHandle=0;
       
   723 	test(pCon1->FindByFullName(tempHandle, _L("*::*"), gFullName)==KErrNone);
       
   724 	GetObjFullName(gFullName1,*pObj2);
       
   725 	test(gFullName==gFullName1);
       
   726     GetObjName(gName1,*pObj3);
       
   727 	for(tempHandle=0;gName!=gName1;pCon1->FindByName(tempHandle, _L("????"),gName)) {};
       
   728 	test(gName==gName1);
       
   729 
       
   730 
       
   731 	pObj1->Close();  // Deletes it and removes it from its container
       
   732 	pObj3->Close();
       
   733 	tempHandle=0;
       
   734 	test(pCon1->FindByName(tempHandle, _L("Obj1"), gName)==KErrNotFound);
       
   735 	tempHandle=0;
       
   736 	test(pCon1->FindByName(tempHandle, _L("Obj3"), gName)==KErrNotFound);
       
   737 	tempHandle=0;
       
   738 	GetObjName(gName1,*pObj2);
       
   739 	test(pCon1->FindByName(tempHandle, gName1, gName)==KErrNone);
       
   740 	test(gName==gName1);
       
   741 	tempHandle=0;
       
   742 	//CObject* x=new CObject;
       
   743 	//x->SetName(_L("xxxx"));
       
   744 	//pCon1->FindByFullName(tempHandle, pObj2->FullName(), aFullName);
       
   745 	// FullName is set to Xxxx::Obj2
       
   746 
       
   747 	}
       
   748 
       
   749 GLDEF_C void TestCObjects::GetObjName(TName& aDest,const CObject& aObj)
       
   750 //
       
   751 // Utility function to reduce stack usage in functions, and so get rid of __chkstk errors
       
   752 //
       
   753 	{
       
   754 	aDest=aObj.Name();
       
   755 	}
       
   756 
       
   757 GLDEF_C void TestCObjects::GetObjFullName(TFullName& aDest,const CObject& aObj)
       
   758 //
       
   759 // Utility function to reduce stack usage in functions, and so get rid of __chkstk errors
       
   760 //
       
   761 	{
       
   762 	aDest=aObj.FullName();
       
   763 	}
       
   764 
       
   765 void testHandles()
       
   766 
       
   767 	{
       
   768 
       
   769 	CObjectConIx* myConIx=CObjectConIx::NewL();
       
   770 	CObjectCon* myCon= myConIx->CreateL();
       
   771 	CObjectIx* myIx=CObjectIx::NewL();
       
   772 	test(myCon->UniqueID()!=0);
       
   773 
       
   774 	for (TInt i=0;i<0x4006; i++)
       
   775 		{
       
   776 		CObject* myObj=new CObject;
       
   777 		myCon->AddL(myObj);
       
   778 		TInt handle=myIx->AddL(myObj);
       
   779 		if ((handle&0xffff0000)==0x3fff0000)
       
   780 			test.Printf(_L("Created biggest handle\n"));
       
   781 		test (handle!=0);
       
   782 		myIx->Remove(handle);
       
   783 		}
       
   784 	delete myIx;
       
   785 	delete myConIx;
       
   786 	}
       
   787 
       
   788 void testSpeed()
       
   789 
       
   790 	{
       
   791 
       
   792 	const TInt numObjects=0x600;
       
   793 	CObjectConIx* myConIx=CObjectConIx::NewL();
       
   794 	CObjectCon* myCon= myConIx->CreateL();
       
   795 //	CObjectIx* myIx=CObjectIx::NewL();
       
   796 	TTime start;
       
   797 	TTime end;
       
   798 	TInt64 diff;
       
   799 	TInt i;
       
   800 
       
   801 	start.HomeTime();
       
   802 	for (i=0;i<numObjects; i++)
       
   803 		{
       
   804 		CObject* myObj=new CObject;
       
   805 		TName name;
       
   806 		name.Format(_L("CObject%d"),i);
       
   807 		myObj->SetName(&name);
       
   808 		myCon->AddL(myObj);
       
   809 		}
       
   810 	end.HomeTime();
       
   811 	diff=(end.Int64()-start.Int64())/100000;
       
   812 	test.Printf(_L("Time for 0x%08x named objects = %d tenths of secs\n"),numObjects,I64INT(diff));
       
   813 
       
   814 // If run as a manual test (when enhancements to RTEST arrive)
       
   815 //	test.Printf(_L("Press a key to continue with these spammy tests\n"));
       
   816 //	test.Getch();
       
   817 
       
   818 	const TInt numObjects2=0x600;
       
   819 //	CObjectConIx* myConIx2=CObjectConIx::NewL();
       
   820 	CObjectCon* myCon2= myConIx->CreateL();
       
   821 //	CObjectIx* myIx2=CObjectIx::NewL();
       
   822 
       
   823 	start.HomeTime();
       
   824 	for (i=0;i<numObjects2; i++)
       
   825 		{
       
   826 		CObject* myObj=new CObject;
       
   827 		TName name;
       
   828 		name.Format(_L("CObject%d"),i);
       
   829 		myCon2->AddL(myObj);
       
   830 		}
       
   831 	end.HomeTime();
       
   832 	diff=(end.Int64()-start.Int64())/100000;
       
   833 	test.Printf(_L("Time for 0x%08x unnamed objects = %d tenths of secs\n"),numObjects2,I64INT(diff));
       
   834 
       
   835 
       
   836 // If run as a manual test (when enhancements to RTEST arrive)
       
   837 //  test.Printf(_L("Press a key to continue with these spammy tests\n"));
       
   838 //  test.Getch();
       
   839 	
       
   840 	//	delete myIx;
       
   841 //	delete myConIx;
       
   842 	}
       
   843 
       
   844 
       
   845 
       
   846 const TInt KObjectIndexMask=0x7fff;
       
   847 
       
   848 //Tests CObjectIx class
       
   849 class TestCObjectIx
       
   850 	{
       
   851 public:
       
   852 	void StartL(void);
       
   853 	~TestCObjectIx();
       
   854 private:
       
   855 	void Test1(TInt aSize);
       
   856 	void Test2(TInt aSize);
       
   857 	void Test3(TInt aSize, TBool aPerformanceTest);
       
   858 	inline TInt Index(TInt aHandle)	{return(aHandle&KObjectIndexMask);}
       
   859 
       
   860 private:
       
   861 	struct COBjAndHandle
       
   862 		{
       
   863 		CObject* iObject;
       
   864 		TInt	 iHandle;
       
   865 		};
       
   866 
       
   867 	CObjectConIx* iMyConIx;
       
   868 	CObjectCon*   iMyCon;
       
   869 	CObjectIx*    iMyIx;
       
   870 	TUint iSeed[2];
       
   871 	COBjAndHandle* iObjAndHandle;	
       
   872 	};
       
   873 
       
   874 const TInt KMaxTestObjects = (1<<11);
       
   875 
       
   876 //Runs all tests (methods) of the class
       
   877 void TestCObjectIx::StartL(void)
       
   878 {
       
   879 	TInt i;
       
   880 	iMyConIx=CObjectConIx::NewL();
       
   881 	iMyCon= iMyConIx->CreateL();
       
   882 	iObjAndHandle = (COBjAndHandle*) User::AllocL(KMaxTestObjects * sizeof(COBjAndHandle));
       
   883 
       
   884 	test.Title();
       
   885 	test.Start(_L("Test CObjectIx"));
       
   886 
       
   887 	test.Next(_L("Add and remove objects in the same order..."));
       
   888 	TUint32 startTime = User::NTickCount();
       
   889 	for (i=1;i<2000;i=i+10) Test1(i);
       
   890 	test.Printf(_L("...done in %d msec\n"), User::NTickCount()-startTime);
       
   891 
       
   892 	test.Next(_L("Add and remove objects in the reverse order..."));
       
   893 	startTime = User::NTickCount();
       
   894 	for (i=1;i<2000;i=i+10) Test2(i);
       
   895 	test.Printf(_L("...done in %d msec\n"), User::NTickCount()-startTime);
       
   896 
       
   897 	test.Next(_L("Add and remove objects in random order..."));
       
   898 	iSeed[0]=User::TickCount();
       
   899 	test.Printf(_L("The initial seed for the random function is:  S0=%xh S1=%xh\n"), iSeed[0], iSeed[1]);
       
   900 	for (i=2;i<=KMaxTestObjects/2; i<<=1)
       
   901 		Test3(i-1, EFalse);
       
   902 
       
   903 	test.Next(_L("Add and remove objects in random order - performance test..."));
       
   904 	iSeed[0]=0;
       
   905 	iSeed[1]=1;
       
   906 	startTime = User::NTickCount();
       
   907 	for (i=2;i<=KMaxTestObjects; i<<=1)
       
   908 		Test3(i-1, ETrue);
       
   909 	test.Printf(_L("...done in %d msec\n"), User::NTickCount()-startTime);
       
   910 	test.Printf(_L("CAUTION: Test changed in May 2005. Comparison of timings with those before then are not valid.\n"));
       
   911 	
       
   912 	test.End();
       
   913 }
       
   914 
       
   915 TestCObjectIx::~TestCObjectIx()
       
   916 	{
       
   917 	delete iMyConIx;
       
   918 	delete [] iObjAndHandle;
       
   919 	}
       
   920 
       
   921 //Adds a number of CObjects to CObjectIx, then removes them in the same order.
       
   922 void TestCObjectIx::Test1(TInt aSize)
       
   923 {
       
   924 	TInt i;	
       
   925 	iMyIx=CObjectIx::NewL();
       
   926 
       
   927 	for (i=0; i<aSize; i++) //Add
       
   928 		{
       
   929 		if( (iObjAndHandle[i].iObject = new CObject) == NULL) User::Leave(KErrNoMemory);
       
   930 		iMyCon->AddL(iObjAndHandle[i].iObject);
       
   931 		iObjAndHandle[i].iHandle = iMyIx->AddL(iObjAndHandle[i].iObject);
       
   932 		}
       
   933 
       
   934 	for (i=0; i<aSize; i++)		//Remove
       
   935 		iMyIx->Remove(iObjAndHandle[i].iHandle);
       
   936 
       
   937 	delete iMyIx;
       
   938 	iMyIx=0;
       
   939 }
       
   940 
       
   941 
       
   942 //Adds a number of CObjects to CObjectIx, then removes them in the reverse order.
       
   943 void TestCObjectIx::Test2(TInt aSize)
       
   944 {
       
   945 	TInt i;	
       
   946 	iMyIx=CObjectIx::NewL();
       
   947 
       
   948 	for (i=0; i<aSize; i++)		//Add
       
   949 		{
       
   950 		if( (iObjAndHandle[i].iObject = new CObject) == NULL) User::Leave(KErrNoMemory);
       
   951 		iMyCon->AddL(iObjAndHandle[i].iObject);
       
   952 		iObjAndHandle[i].iHandle = iMyIx->AddL(iObjAndHandle[i].iObject);
       
   953 		}
       
   954 
       
   955 	for (i=aSize-1; i>=aSize; i--)		//Remove
       
   956 		iMyIx->Remove(iObjAndHandle[i].iHandle);
       
   957 
       
   958 	delete iMyIx;
       
   959 	iMyIx=0;
       
   960 }
       
   961 
       
   962 
       
   963 //Adds and removes random number of CObjects to/from CObjectIx.
       
   964 void TestCObjectIx::Test3(TInt aSize, TBool aPerformanceTest)
       
   965 {
       
   966 	TInt index, x;
       
   967 	if(!aPerformanceTest) 
       
   968 		test.Printf(_L("Testing size %d.The seeds are:  S0=%xh S1=%xh\n"), aSize, iSeed[0], iSeed[1]);
       
   969 	
       
   970 	//---Create & init the objects we need
       
   971 	for (x=0; x<aSize; x++) iObjAndHandle[x].iObject = NULL; //initialize the array
       
   972 	iMyIx=CObjectIx::NewL();
       
   973 
       
   974 	for (x = 0; x<100; x++)
       
   975 		{
       
   976 		
       
   977 		//---Add the random number of objects (in random order)---
       
   978 		TInt toAdd=Random(iSeed)%(aSize-iMyIx->ActiveCount()+1);
       
   979 		//test.Printf(_L("Adding %d objects\n"), toAdd );
       
   980 		while (toAdd--)
       
   981 			{
       
   982 			index=Random(iSeed)%aSize;
       
   983 			while (iObjAndHandle[index].iObject) //Find the next NULL pointer 
       
   984 				{  ++index; if(index>=aSize) index=0; }
       
   985 			if( (iObjAndHandle[index].iObject = new CObject) == NULL) User::Leave(KErrNoMemory);
       
   986 			iMyCon->AddL(iObjAndHandle[index].iObject);
       
   987 			iObjAndHandle[index].iHandle = iMyIx->AddL(iObjAndHandle[index].iObject);
       
   988 			//test.Printf(_L("%d(%d) "), index,iObjAndHandle[index].iHandle & 0x7fff  );
       
   989 			}
       
   990 		//test.Printf(_L("\n"));
       
   991 
       
   992 
       
   993 		//---Remove the random number of objects (in random order)---
       
   994 		TInt toRemove=Random(iSeed)%(iMyIx->ActiveCount()+1);
       
   995 		//test.Printf(_L("Removing %d objects: "), toRemove );
       
   996 		while (toRemove--)
       
   997 			{
       
   998 			index=Random(iSeed)%aSize;
       
   999 			while (!iObjAndHandle[index].iObject) //Find the next non-NULL pointer 
       
  1000 				{  ++index; if(index>=aSize) index=0; }
       
  1001 			//test.Printf(_L("%d(%d) "), index,iObjAndHandle[index].iHandle & 0x7fff  );
       
  1002 			iMyIx->Remove(iObjAndHandle[index].iHandle);
       
  1003 			iObjAndHandle[index].iObject=NULL;
       
  1004 			}
       
  1005 		//test.Printf(_L("\n"));
       
  1006 
       
  1007 
       
  1008 		//---Test data consistency---
       
  1009 		if(aPerformanceTest) continue;
       
  1010 
       
  1011 		TInt objNum=0;
       
  1012 		for (index=0;index<aSize;index++) 
       
  1013 			{
       
  1014 			if (iObjAndHandle[index].iObject)
       
  1015 				{
       
  1016 				objNum++;
       
  1017 				//Test At(TInt aHandle) method
       
  1018 				test(iObjAndHandle[index].iObject == iMyIx->At(iObjAndHandle[index].iHandle));
       
  1019 				//Test Count(CObject* aObject) method
       
  1020 				test(1==iMyIx->Count(iObjAndHandle[index].iObject));
       
  1021 				//Test At(CObject* aObject) method
       
  1022 				test(iObjAndHandle[index].iHandle==iMyIx->At(iObjAndHandle[index].iObject));
       
  1023 				//Test operator[](TInt index) method
       
  1024 				test(iObjAndHandle[index].iObject==(*iMyIx)[Index(iObjAndHandle[index].iHandle)]);
       
  1025 				}
       
  1026 			}
       
  1027 	
       
  1028 		test (objNum==iMyIx->ActiveCount());
       
  1029 		//test.Printf(_L("%d objects in array\n"), objNum);
       
  1030 		}
       
  1031 
       
  1032 	delete iMyIx;
       
  1033 	iMyIx=NULL;
       
  1034 }
       
  1035 
       
  1036 // Test that things work when the unique ID of the object container grows larger
       
  1037 // than 15 bits
       
  1038 void TestCObjectConIxL(void)
       
  1039 	{
       
  1040 	const TInt KCObjectConLimit = 65535;
       
  1041 	_LIT(KAnyMatch, "*");
       
  1042 	
       
  1043    	__UHEAP_MARK;
       
  1044 	
       
  1045 	CObjectConIx* conIx = CObjectConIx::NewL();
       
  1046 	CObjectIx* ix = CObjectIx::NewL();
       
  1047 	TInt i;
       
  1048 
       
  1049 	test.Next(_L("Test repeated add/remove of object containers"));
       
  1050 	for (i = 0 ; i < KCObjectConLimit * 2 ; ++i)
       
  1051 		{
       
  1052 		CObjectCon* con = conIx->CreateL();
       
  1053 		CObject* obj = new (ELeave) CObject();
       
  1054 		con->AddL(obj);
       
  1055 		
       
  1056 		TInt handle = ix->AddL(obj);
       
  1057 		test(ix->At(handle) == obj);
       
  1058 		test(ix->At(handle, con->UniqueID()) == obj);
       
  1059 
       
  1060 		TName name;
       
  1061 		TInt findHandle = 0;
       
  1062 		test(con->FindByName(findHandle, KAnyMatch, name) == KErrNone);
       
  1063 		test(con->AtL(findHandle) == obj);
       
  1064 		test(con->At(findHandle) == obj);
       
  1065 		test(conIx->Lookup(findHandle) == con);
       
  1066 		test(con->FindByName(findHandle, KAnyMatch, name) == KErrNotFound);
       
  1067 
       
  1068 		TFullName fullName;
       
  1069 		findHandle = 0;
       
  1070 		test(con->FindByFullName(findHandle, KAnyMatch, fullName) == KErrNone);
       
  1071 		test(con->AtL(findHandle) == obj);
       
  1072 		test(con->At(findHandle) == obj);
       
  1073 		test(conIx->Lookup(findHandle) == con);
       
  1074 		test(con->FindByFullName(findHandle, KAnyMatch, fullName) == KErrNotFound);
       
  1075 		
       
  1076 		ix->Remove(handle);
       
  1077 		conIx->Remove(con);
       
  1078 		}
       
  1079 	
       
  1080 	test.Next(_L("Test adding maximum possible number of object containers"));
       
  1081 	RPointerArray<CObjectCon> cons;
       
  1082 	for (i = 0 ; i < KCObjectConLimit ; ++i)
       
  1083 		{
       
  1084 		CObjectCon* con = conIx->CreateL();
       
  1085 		cons.AppendL(con);
       
  1086 		}
       
  1087 	TRAPD(err, conIx->CreateL());
       
  1088 	test(err == KErrOverflow);
       
  1089 	
       
  1090 	test.Next(_L("Test unique IDs are really unique after ID value has wrapped"));
       
  1091 	for (i = 100 ; i < KCObjectConLimit ; ++i)
       
  1092 		{
       
  1093 		CObjectCon* con = cons[i];
       
  1094 		conIx->Remove(con);		
       
  1095 		}
       
  1096 	for (i = 100 ; i < 200 ; ++i)
       
  1097 		{
       
  1098 		CObjectCon* con = conIx->CreateL();
       
  1099 		cons[i] = con;
       
  1100 		}
       
  1101 	for (i = 0 ; i < 200 ; ++i)
       
  1102 		{
       
  1103 		TName name;
       
  1104 		TInt findHandle = 0;
       
  1105 		CObjectCon* con = cons[i];
       
  1106 
       
  1107 		CObject* obj = new (ELeave) CObject();
       
  1108 		con->AddL(obj);
       
  1109 
       
  1110 		test(con->FindByName(findHandle, KAnyMatch, name) == KErrNone);
       
  1111 		test(conIx->Lookup(findHandle) == con);
       
  1112 
       
  1113 		obj->Close();
       
  1114 		}
       
  1115 	for (i = 0 ; i < 200 ; ++i)
       
  1116 		{
       
  1117 		CObjectCon* con = cons[i];
       
  1118 		conIx->Remove(con);
       
  1119 		}
       
  1120 	cons.Close();
       
  1121 
       
  1122 	delete ix;
       
  1123 	delete conIx;
       
  1124 	
       
  1125    	__UHEAP_MARKEND;
       
  1126 	}
       
  1127 
       
  1128 GLDEF_C TInt E32Main()
       
  1129     {
       
  1130 
       
  1131    	CTrapCleanup* trapHandler=CTrapCleanup::New();
       
  1132    	test(trapHandler!=NULL);
       
  1133 
       
  1134 	test.Title();
       
  1135 	test.Start(_L("TEST METHODS ARE IN THE DLL"));
       
  1136 
       
  1137    	__UHEAP_MARK;
       
  1138 
       
  1139 	TestCObjectIx* TCobjectIx = new TestCObjectIx;
       
  1140    	TRAPD(ret, TCobjectIx->StartL()); 
       
  1141 	test(KErrNone == ret);
       
  1142 	delete TCobjectIx;
       
  1143 
       
  1144    	__UHEAP_MARKEND;
       
  1145 
       
  1146 	test.Next(_L("Generate lots of handles"));
       
  1147 	testHandles();
       
  1148 
       
  1149 	test.Next(_L("Test CObjectCon is fast enough"));
       
  1150 	testSpeed();
       
  1151 
       
  1152 	TestCObjects T;
       
  1153 	test.Next(_L("CObject methods"));
       
  1154 	T.Test1();
       
  1155 	test.Next(_L("CObjectCon methods"));
       
  1156 	T.Test2();
       
  1157 	test.Next(_L("CObjectIx methods"));
       
  1158 	T.Test3();
       
  1159 	test.Next(_L("CObjectConIx methods"));
       
  1160 	T.Test4();
       
  1161 	test.Next(_L("TEST THE METHODS"));
       
  1162 	test.Next(_L("CObject"));
       
  1163 	T.Test5();
       
  1164 	test.Next(_L("CObjectCon"));
       
  1165 	T.Test6();
       
  1166 	test.Next(_L("CObjectIx"));
       
  1167 	T.Test7();
       
  1168 
       
  1169 	//////////////////////////////
       
  1170 	// PROPER TESTING STARTS HERE
       
  1171 	//////////////////////////////
       
  1172 	test.Next(_L("All objects"));
       
  1173 	T.Test8();
       
  1174 	
       
  1175 	test.Next(_L("CObjectConIx"));
       
  1176 	TRAPD(err, TestCObjectConIxL());
       
  1177 	if (err != KErrNone)
       
  1178 		test.Printf(_L("TestCObjectConIxL left with %d\n"), err);
       
  1179 	test(err == KErrNone);
       
  1180 
       
  1181 	test.End();
       
  1182 
       
  1183    	delete trapHandler;
       
  1184    	return(KErrNone);
       
  1185 	}