sl@0: // Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of the License "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // e32test\system\t_ref.cpp sl@0: // Overview: sl@0: // Test the methods of the RRef class. sl@0: // API Information: sl@0: // RRef sl@0: // Details: sl@0: // - Check for the existence of all RRef methods. sl@0: // - Test and verify the results of the RRef constructors. sl@0: // - Test and verify the results of the RRef assignment methods. sl@0: // - Test and verify the results of the RRef smart pointer methods. sl@0: // - Attempt to allocate all available heap space, verify that it sl@0: // cause a leave. sl@0: // - Test and verify the results of the RRef allocation methods. sl@0: // Platforms/Drives/Compatibility: sl@0: // All. sl@0: // Assumptions/Requirement/Pre-requisites: sl@0: // Failures and causes: sl@0: // Base Port information: sl@0: // sl@0: // sl@0: sl@0: #include sl@0: sl@0: LOCAL_D RTest test(_L("T_REF")); sl@0: sl@0: struct SObj sl@0: { sl@0: TInt iInt; sl@0: TBuf<0x100> aBuf; sl@0: }; sl@0: sl@0: void Test1() sl@0: // All methods sl@0: { sl@0: sl@0: __UHEAP_MARK; sl@0: SObj anObj; sl@0: anObj.iInt=10; sl@0: anObj.aBuf=_L("Hello World"); sl@0: RRef aRef1; sl@0: aRef1.Alloc(anObj); sl@0: RRef aRef2(aRef1); sl@0: aRef2=aRef1; sl@0: aRef2.AllocL(anObj); sl@0: test(aRef1->iInt==10); sl@0: test(((SObj*)aRef1)->aBuf==_L("Hello World")); sl@0: aRef1.Free(); sl@0: aRef2.Free(); sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: void Test2() sl@0: // Constructors sl@0: { sl@0: __UHEAP_MARK; sl@0: __UHEAP_MARK; sl@0: SObj anObj; sl@0: anObj.iInt=10; sl@0: anObj.aBuf=_L("Hello World"); sl@0: RRef aRef1; sl@0: aRef1.Alloc(anObj); sl@0: RRef aRef2(aRef1); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: test(aRef2->iInt==10); sl@0: test(aRef2->aBuf==_L("Hello World")); sl@0: aRef1.Free(); sl@0: __UHEAP_MARKENDC(1); sl@0: aRef2.Free(); sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: void Test3() sl@0: // Assignment methods sl@0: { sl@0: __UHEAP_MARK; sl@0: __UHEAP_MARK; sl@0: SObj anObj; sl@0: anObj.iInt=10; sl@0: anObj.aBuf=_L("Hello World"); sl@0: RRef aRef1; sl@0: RRef aRef2; sl@0: aRef1.Alloc(anObj); sl@0: aRef2=aRef1; sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: test(aRef2->iInt==10); sl@0: test(aRef2->aBuf==_L("Hello World")); sl@0: aRef1.Free(); sl@0: __UHEAP_MARKENDC(1); sl@0: aRef2.Free(); sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: void Test4() sl@0: // Smart Pointer methods sl@0: { sl@0: SObj anObj; sl@0: anObj.iInt=10; sl@0: anObj.aBuf=_L("Hello World"); sl@0: RRef aRef1; sl@0: aRef1.Alloc(anObj); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: SObj* pObj=(SObj*)aRef1; sl@0: test(pObj->iInt==10); sl@0: test(pObj->aBuf==_L("Hello World")); sl@0: } sl@0: sl@0: void allocAllL(TInt*& aPtr,RRef& aRef1,SObj& anObj) sl@0: // sl@0: // Alloc all and cause leave. sl@0: // sl@0: { sl@0: sl@0: TInt temp; sl@0: TInt avail=User::Available(temp); sl@0: aPtr=(TInt*)User::AllocL(avail); sl@0: aRef1.AllocL(anObj); sl@0: sl@0: /* sl@0: sl@0: Reinstate if some method is found of forcing alloc fail sl@0: This test no longer works due to heap growing functionality of E32 057 sl@0: sl@0: ... so leave with KErrNoMemory to simulate alloc failure sl@0: */ sl@0: User::Leave(KErrNoMemory); sl@0: } sl@0: sl@0: void Test5() sl@0: // sl@0: // Allocation methods sl@0: // sl@0: { sl@0: sl@0: __UHEAP_MARK; sl@0: SObj anObj; sl@0: anObj.iInt=10; sl@0: anObj.aBuf=_L("Hello World"); sl@0: RRef aRef1; sl@0: aRef1.Alloc(anObj); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: aRef1.Alloc(anObj,sizeof(SObj)-245*sizeof(TText)); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: aRef1.AllocL(anObj); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: aRef1.AllocL(anObj,sizeof(SObj)-245*sizeof(TText)); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: TInt* p=0; sl@0: TRAPD(ret,allocAllL(p,aRef1,anObj)) sl@0: test(ret==KErrNoMemory); sl@0: User::Free(p); sl@0: aRef1.AllocL(anObj); sl@0: test(aRef1->iInt==10); sl@0: test(aRef1->aBuf==_L("Hello World")); sl@0: aRef1.Free(); sl@0: __UHEAP_MARKEND; sl@0: } sl@0: sl@0: TInt E32Main() sl@0: { sl@0: sl@0: test.Title(); sl@0: test.Start(_L("class RRef")); sl@0: test.Next(_L("Test all methods")); sl@0: Test1(); sl@0: test.Next(_L("Constructors")); sl@0: Test2(); sl@0: test.Next(_L("Assignment methods")); sl@0: Test3(); sl@0: test.Next(_L("Smart pointer methods")); sl@0: Test4(); sl@0: test.Next(_L("Allocation methods")); sl@0: Test5(); sl@0: test.End(); sl@0: return(0); sl@0: } sl@0: sl@0: