Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\system\t_cobj.cpp
16 // Test CObject, CObjectIx, CObjectCon, CObjectConIx classes
18 // CObject, CObjectIx, CObjectCon, CObjectConIx
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
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
33 // Platforms/Drives/Compatibility:
35 // Assumptions/Requirement/Pre-requisites:
36 // Failures and causes:
37 // Base Port information:
42 #include <e32std_private.h>
44 #include <e32base_private.h>
48 #include "../misc/prbs.h"
54 CObjectCon* iContainer;
58 void CObject::__DbgTest(void* pCObjectDump) const
60 ((TCObjectDump*)pCObjectDump)->iAccessCount=iAccessCount;
61 ((TCObjectDump*)pCObjectDump)->iOwner=iOwner;
62 ((TCObjectDump*)pCObjectDump)->iContainer=iContainer;
63 ((TCObjectDump*)pCObjectDump)->iName=iName;
66 LOCAL_D RTest test(_L("T_COBJ"));
70 LOCAL_D TFullName gFullName;
71 LOCAL_D TFullName gFullName1;
72 LOCAL_D TFullName gFullName2;
74 class RTestHeap : public RAllocator
77 enum TOp {EOpNone=0, EOpAlloc=1, EOpFree=2, EOpReAlloc=3};
87 static RTestHeap* Install();
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();
96 virtual TInt AllocSize(TInt& aTotalAllocSize) const;
97 virtual TInt Available(TInt& aBiggestBlock) const;
98 virtual TInt DebugFunction(TInt aFunc, TAny* a1, TAny* a2);
104 RTestHeap* RTestHeap::Install()
106 RTestHeap* p = new RTestHeap;
108 p->iA = &User::Heap();
109 p->iLastOp.iOp = EOpNone;
114 void RTestHeap::Uninstall()
116 User::SwitchHeap(iA);
120 TInt RTestHeap::GetLastOp(RTestHeap::TOpInfo& aInfo)
122 memcpy(&aInfo, &iLastOp, sizeof(TOpInfo));
123 iLastOp.iOp = EOpNone;
124 return (aInfo.iOp == EOpNone) ? KErrNotFound : KErrNone;
127 TAny* RTestHeap::Alloc(TInt aSize)
129 TAny* p = iA->Alloc(aSize);
130 iLastOp.iOp = EOpAlloc;
132 iLastOp.iIntArg = aSize;
137 void RTestHeap::Free(TAny* aPtr)
139 iLastOp.iOp = EOpFree;
140 iLastOp.iPtrArg = aPtr;
146 TAny* RTestHeap::ReAlloc(TAny* aPtr, TInt aSize, TInt aMode=0)
148 TAny* p = iA->ReAlloc(aPtr,aSize,aMode);
149 iLastOp.iOp = EOpReAlloc;
150 iLastOp.iPtrArg = aPtr;
151 iLastOp.iIntArg = aSize;
156 TInt RTestHeap::AllocLen(const TAny* aCell) const
158 TInt l = iA->AllocLen(aCell);
162 TInt RTestHeap::Compress()
164 TInt l = iA->Compress();
168 void RTestHeap::Reset()
173 TInt RTestHeap::AllocSize(TInt& aTotalAllocSize) const
176 TInt r = iA->AllocSize(s);
181 TInt RTestHeap::Available(TInt& aBiggestBlock) const
184 TInt r = iA->Available(s);
189 TInt RTestHeap::DebugFunction(TInt aFunc, TAny* a1=NULL, TAny* a2=NULL)
191 TInt r = iA->DebugFunction(aFunc, a1, a2);
210 static void GetObjName(TName& aDest,const CObject& aObj);
211 static void GetObjFullName(TFullName& aDest,const CObject& aObj);
215 GLDEF_C void TestCObjects::Test1(void)
217 // Check CObject methods are in the DLL
218 CObject* pCObject=new CObject;
219 CObject* temp=new CObject;
223 gName=pCObject->Name();
224 TPtrC aDesR(_S("a name"));
225 pCObject->SetName(&aDesR);
226 pCObject->SetNameL(&aDesR);
227 gFullName=pCObject->FullName();
229 pCObject->SetOwner(temp);
230 pCObject->Close(); // deletes object when iAccessCount==0
234 GLDEF_C void TestCObjects::Test2(void)
236 // Check CObjectCon methods are in the DLL
237 CObjectCon* pCObjectCon=CObjectCon::NewL();
238 CObject* pCObject=new CObject;
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);
254 GLDEF_C void TestCObjects::Test3(void)
256 // Check the CObjectIx methods are in the DLL
257 CObjectIx* pCObjectIx=CObjectIx::NewL();
259 test(pCObjectIx->At(0)==NULL);
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);
273 TInt r=pCObjectCon->FindByName(find,_L("Name"),name);
274 test(r==KErrNotFound);
276 r=pCObjectCon->FindByFullName(find,_L("Full Name"),fullName);
277 test(r==KErrNotFound);
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);
286 pCObjectIx->At(0, 0);
288 // pCObjectIx->Remove(0);
290 delete pCObjectConIx;
291 // delete pCObjectCon;
292 // pCObject->Close();
295 GLDEF_C void TestCObjects::Test4(void)
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;
305 GLDEF_C void TestCObjects::Test5(void)
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);
314 a->__DbgTest(&CObjectDump);
315 test(CObjectDump.iAccessCount==2);
317 a->__DbgTest(&CObjectDump);
318 test(CObjectDump.iAccessCount==1);
319 TPtrC aDesR(_L("aName"));
322 test(gName==_L("aName"));
323 gFullName=a->FullName();
324 test(gFullName==_L("aName"));
325 TPtrC aDesR2(_L("Owner"));
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()
336 GLDEF_C void TestCObjects::Test6(void)
338 // Test the methods of CObjectCon
339 TCObjectDump dump1, dump2;
341 CObject *pObj1=new CObject,
345 CObjectCon* pCon=CObjectCon::NewL();
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);
352 TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Owner"));
353 pObj1->SetName(&name1);
354 pObj2->SetName(&name2);
355 pObj3->SetName(&name3);
357 test(pCon->Count()==0);
359 test(pCon->Count()==1);
361 test(pCon->Count()==2);
364 test(pCon->FindByName(aFindHandle, _L("xxx"), gName)==KErrNotFound);
366 GetObjName(gName1,*pObj1);
367 test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
368 test(aFindHandle==0);
370 GetObjName(gName1,*pObj1);
371 test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNotFound);
373 GetObjName(gName1,*pObj2);
374 test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNone);
375 test(aFindHandle==1);
378 test(pCon->FindByFullName(aFindHandle, _L("xxx"), gFullName)==KErrNotFound);
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);
387 pObj1->SetOwner(pObj3);
388 pObj2->SetOwner(pObj3);
390 test(pCon->FindByFullName(aFindHandle, _L("xxx"), gFullName)==KErrNotFound);
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);
400 test(pCon->CheckUniqueFullName(pObj3, _L("aname"))==KErrNone);
401 GetObjName(gName1,*pObj1);
402 test(pCon->CheckUniqueFullName(pObj3, gName1)==KErrAlreadyExists);
404 test(pCon->CheckUniqueFullName(pObj3, gName1)==KErrNone);
405 test(pCon->Count()==1);
407 test(pCon->Count()==2);
409 test(pCon->FindByName(aFindHandle, gName1, gName)==KErrNotFound);
411 GetObjFullName(gFullName1,*pObj1);
412 test(pCon->FindByFullName(aFindHandle, gFullName1, gFullName)==KErrNotFound);
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);
424 test(pCon->FindByFullName(aFindHandle, _L("Owner"), gFullName)==KErrNone);
425 test(aFindHandle==1);
428 pObj2->__DbgTest(&dump1);
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);
435 temp->__DbgTest(&dump2);
436 test(dump1.iAccessCount==dump2.iAccessCount && dump1.iOwner==dump2.iOwner &&
437 dump1.iName==dump2.iName &&dump1.iContainer==dump2.iContainer);
441 test(pCon->Count()==0);
444 // Test expansion and shrinking
446 pCon=CObjectCon::NewL();
447 CObject** obj = new CObject*[2048];
450 for (i=0; i<2048; ++i)
452 obj[i] = new CObject;
456 RTestHeap* h = RTestHeap::Install();
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;
465 for (i=1; i<=2048; ++i)
467 pCon->AddL(obj[i-1]);
468 test(pCon->Count()==i);
469 RTestHeap::TOpInfo opi;
470 TInt r = h->GetLastOp(opi);
473 test(r==KErrNotFound);
477 test.Printf(_L("Realloc at %d -> %d\n"), i, opi.iIntArg);
479 test(opi.iOp == RTestHeap::EOpReAlloc);
480 test(opi.iPtrArg == ptr);
483 test(opi.iIntArg == xov*4);
485 test(opi.iResult != 0);
490 for (i=2047; i>=0; --i)
492 pCon->Remove(obj[i]);
493 test(pCon->Count()==i);
494 RTestHeap::TOpInfo opi;
495 TInt r = h->GetLastOp(opi);
498 test(r==KErrNotFound);
502 test.Printf(_L("Realloc at %d -> %d\n"), i, opi.iIntArg);
506 test(opi.iOp == RTestHeap::EOpReAlloc || opi.iOp == RTestHeap::EOpFree);
510 test(opi.iOp = RTestHeap::EOpReAlloc);
511 test(opi.iResult == ptr);
513 test(opi.iPtrArg == ptr);
517 test(opi.iIntArg == sz*4);
521 for (i=0; i<2048; ++i)
529 GLDEF_C void TestCObjects::Test7(void)
531 // Test the methods of CObjectIx
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,
545 TInt aHandle1, aHandle2, aHandle3;
546 TCObjectDump dump1, dump2;
547 TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Obj3"));
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);
556 aHandle1=pIx->AddL(pObj1);
557 aHandle2=pIx->AddL(pObj2);
558 aHandle3=pIx->AddL(pObj3);
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
607 // Removing an object from a CObjectIx calls the objects close method hence in this case
609 //delete pCon1; // The destructor for a container deletes its contained objects
611 delete pConIx; // The CObjectConIx destructor deletes its containers
612 delete pIx; // The CObjectIx destructor calls close on all its objects
615 GLDEF_C void TestCObjects::Test8(void)
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,
629 TPtrC name1(_S("Obj1")), name2(_S("Obj2")), name3(_S("Obj3")), name4(_S("Obj4")), name5(_S("Obj5"));
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);
653 GetObjName(gName1,*pObj1);
654 pCon1->FindByName(tempHandle, gName1, gName);
655 temp=pCon1->At(tempHandle);
656 GetObjName(gName2,*temp);
657 test(gName2==gName1);
659 GetObjFullName(gFullName1,*pObj1);
660 pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
661 temp=pCon1->At(tempHandle);
662 GetObjName(gName2,*temp);
663 test(gName2==gName1);
666 GetObjName(gName1,*pObj2);
667 pCon1->FindByName(tempHandle, gName1, gName);
668 temp=pCon1->At(tempHandle);
669 GetObjName(gName2,*temp);
670 test(gName2==gName1);
672 GetObjFullName(gFullName1,*pObj2);
673 pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
674 temp=pCon1->At(tempHandle);
675 GetObjName(gName2,*temp);
676 test(gName2==gName1);
679 GetObjName(gName1,*pObj3);
680 pCon1->FindByName(tempHandle, gName1, gName);
681 temp=pCon1->At(tempHandle);
682 GetObjName(gName2,*temp);
683 test(gName2==gName1);
685 GetObjFullName(gFullName1,*pObj3);
686 pCon1->FindByFullName(tempHandle, gFullName1, gFullName);
687 temp=pCon1->At(tempHandle);
688 GetObjName(gName2,*temp);
689 test(gName2==gName1);
692 GetObjName(gName1,*pObj4);
693 pCon2->FindByName(tempHandle, gName1, gName);
694 temp=pCon2->At(tempHandle);
695 GetObjName(gName2,*temp);
696 test(gName2==gName1);
698 GetObjFullName(gFullName1,*pObj4);
699 pCon2->FindByFullName(tempHandle, gFullName1, gFullName);
700 temp=pCon2->At(tempHandle);
701 GetObjName(gName2,*temp);
702 test(gName1==gName2);
705 GetObjName(gName1,*pObj5);
706 pCon2->FindByName(tempHandle, gName1, gName);
707 temp=pCon2->At(tempHandle);
708 GetObjName(gName2,*temp);
709 test(gName2==gName1);
711 GetObjFullName(gFullName1,*pObj5);
712 pCon2->FindByFullName(tempHandle, gFullName1, gFullName);
713 temp=pCon2->At(tempHandle);
714 GetObjName(gName2,*temp);
715 test(gName2==gName1);
719 test(pCon1->FindByName(tempHandle, _L("*1"), gName)==KErrNone);
720 GetObjName(gName1,*pObj1);
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)) {};
731 pObj1->Close(); // Deletes it and removes it from its container
734 test(pCon1->FindByName(tempHandle, _L("Obj1"), gName)==KErrNotFound);
736 test(pCon1->FindByName(tempHandle, _L("Obj3"), gName)==KErrNotFound);
738 GetObjName(gName1,*pObj2);
739 test(pCon1->FindByName(tempHandle, gName1, gName)==KErrNone);
742 //CObject* x=new CObject;
743 //x->SetName(_L("xxxx"));
744 //pCon1->FindByFullName(tempHandle, pObj2->FullName(), aFullName);
745 // FullName is set to Xxxx::Obj2
749 GLDEF_C void TestCObjects::GetObjName(TName& aDest,const CObject& aObj)
751 // Utility function to reduce stack usage in functions, and so get rid of __chkstk errors
757 GLDEF_C void TestCObjects::GetObjFullName(TFullName& aDest,const CObject& aObj)
759 // Utility function to reduce stack usage in functions, and so get rid of __chkstk errors
762 aDest=aObj.FullName();
769 CObjectConIx* myConIx=CObjectConIx::NewL();
770 CObjectCon* myCon= myConIx->CreateL();
771 CObjectIx* myIx=CObjectIx::NewL();
772 test(myCon->UniqueID()!=0);
774 for (TInt i=0;i<0x4006; i++)
776 CObject* myObj=new CObject;
778 TInt handle=myIx->AddL(myObj);
779 if ((handle&0xffff0000)==0x3fff0000)
780 test.Printf(_L("Created biggest handle\n"));
782 myIx->Remove(handle);
792 const TInt numObjects=0x600;
793 CObjectConIx* myConIx=CObjectConIx::NewL();
794 CObjectCon* myCon= myConIx->CreateL();
795 // CObjectIx* myIx=CObjectIx::NewL();
802 for (i=0;i<numObjects; i++)
804 CObject* myObj=new CObject;
806 name.Format(_L("CObject%d"),i);
807 myObj->SetName(&name);
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));
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"));
818 const TInt numObjects2=0x600;
819 // CObjectConIx* myConIx2=CObjectConIx::NewL();
820 CObjectCon* myCon2= myConIx->CreateL();
821 // CObjectIx* myIx2=CObjectIx::NewL();
824 for (i=0;i<numObjects2; i++)
826 CObject* myObj=new CObject;
828 name.Format(_L("CObject%d"),i);
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));
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"));
846 const TInt KObjectIndexMask=0x7fff;
848 //Tests CObjectIx class
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);}
867 CObjectConIx* iMyConIx;
871 COBjAndHandle* iObjAndHandle;
874 const TInt KMaxTestObjects = (1<<11);
876 //Runs all tests (methods) of the class
877 void TestCObjectIx::StartL(void)
880 iMyConIx=CObjectConIx::NewL();
881 iMyCon= iMyConIx->CreateL();
882 iObjAndHandle = (COBjAndHandle*) User::AllocL(KMaxTestObjects * sizeof(COBjAndHandle));
885 test.Start(_L("Test CObjectIx"));
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);
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);
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)
903 test.Next(_L("Add and remove objects in random order - performance test..."));
906 startTime = User::NTickCount();
907 for (i=2;i<=KMaxTestObjects; i<<=1)
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"));
915 TestCObjectIx::~TestCObjectIx()
918 delete [] iObjAndHandle;
921 //Adds a number of CObjects to CObjectIx, then removes them in the same order.
922 void TestCObjectIx::Test1(TInt aSize)
925 iMyIx=CObjectIx::NewL();
927 for (i=0; i<aSize; i++) //Add
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);
934 for (i=0; i<aSize; i++) //Remove
935 iMyIx->Remove(iObjAndHandle[i].iHandle);
942 //Adds a number of CObjects to CObjectIx, then removes them in the reverse order.
943 void TestCObjectIx::Test2(TInt aSize)
946 iMyIx=CObjectIx::NewL();
948 for (i=0; i<aSize; i++) //Add
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);
955 for (i=aSize-1; i>=aSize; i--) //Remove
956 iMyIx->Remove(iObjAndHandle[i].iHandle);
963 //Adds and removes random number of CObjects to/from CObjectIx.
964 void TestCObjectIx::Test3(TInt aSize, TBool aPerformanceTest)
967 if(!aPerformanceTest)
968 test.Printf(_L("Testing size %d.The seeds are: S0=%xh S1=%xh\n"), aSize, iSeed[0], iSeed[1]);
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();
974 for (x = 0; x<100; x++)
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 );
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 );
990 //test.Printf(_L("\n"));
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 );
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;
1005 //test.Printf(_L("\n"));
1008 //---Test data consistency---
1009 if(aPerformanceTest) continue;
1012 for (index=0;index<aSize;index++)
1014 if (iObjAndHandle[index].iObject)
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)]);
1028 test (objNum==iMyIx->ActiveCount());
1029 //test.Printf(_L("%d objects in array\n"), objNum);
1036 // Test that things work when the unique ID of the object container grows larger
1038 void TestCObjectConIxL(void)
1040 const TInt KCObjectConLimit = 65535;
1041 _LIT(KAnyMatch, "*");
1045 CObjectConIx* conIx = CObjectConIx::NewL();
1046 CObjectIx* ix = CObjectIx::NewL();
1049 test.Next(_L("Test repeated add/remove of object containers"));
1050 for (i = 0 ; i < KCObjectConLimit * 2 ; ++i)
1052 CObjectCon* con = conIx->CreateL();
1053 CObject* obj = new (ELeave) CObject();
1056 TInt handle = ix->AddL(obj);
1057 test(ix->At(handle) == obj);
1058 test(ix->At(handle, con->UniqueID()) == obj);
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);
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);
1080 test.Next(_L("Test adding maximum possible number of object containers"));
1081 RPointerArray<CObjectCon> cons;
1082 for (i = 0 ; i < KCObjectConLimit ; ++i)
1084 CObjectCon* con = conIx->CreateL();
1087 TRAPD(err, conIx->CreateL());
1088 test(err == KErrOverflow);
1090 test.Next(_L("Test unique IDs are really unique after ID value has wrapped"));
1091 for (i = 100 ; i < KCObjectConLimit ; ++i)
1093 CObjectCon* con = cons[i];
1096 for (i = 100 ; i < 200 ; ++i)
1098 CObjectCon* con = conIx->CreateL();
1101 for (i = 0 ; i < 200 ; ++i)
1104 TInt findHandle = 0;
1105 CObjectCon* con = cons[i];
1107 CObject* obj = new (ELeave) CObject();
1110 test(con->FindByName(findHandle, KAnyMatch, name) == KErrNone);
1111 test(conIx->Lookup(findHandle) == con);
1115 for (i = 0 ; i < 200 ; ++i)
1117 CObjectCon* con = cons[i];
1128 GLDEF_C TInt E32Main()
1131 CTrapCleanup* trapHandler=CTrapCleanup::New();
1132 test(trapHandler!=NULL);
1135 test.Start(_L("TEST METHODS ARE IN THE DLL"));
1139 TestCObjectIx* TCobjectIx = new TestCObjectIx;
1140 TRAPD(ret, TCobjectIx->StartL());
1141 test(KErrNone == ret);
1146 test.Next(_L("Generate lots of handles"));
1149 test.Next(_L("Test CObjectCon is fast enough"));
1153 test.Next(_L("CObject methods"));
1155 test.Next(_L("CObjectCon methods"));
1157 test.Next(_L("CObjectIx methods"));
1159 test.Next(_L("CObjectConIx methods"));
1161 test.Next(_L("TEST THE METHODS"));
1162 test.Next(_L("CObject"));
1164 test.Next(_L("CObjectCon"));
1166 test.Next(_L("CObjectIx"));
1169 //////////////////////////////
1170 // PROPER TESTING STARTS HERE
1171 //////////////////////////////
1172 test.Next(_L("All objects"));
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);