1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/system/t_ref.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,197 @@
1.4 +// Copyright (c) 1995-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\system\t_ref.cpp
1.18 +// Overview:
1.19 +// Test the methods of the RRef class.
1.20 +// API Information:
1.21 +// RRef
1.22 +// Details:
1.23 +// - Check for the existence of all RRef methods.
1.24 +// - Test and verify the results of the RRef constructors.
1.25 +// - Test and verify the results of the RRef assignment methods.
1.26 +// - Test and verify the results of the RRef smart pointer methods.
1.27 +// - Attempt to allocate all available heap space, verify that it
1.28 +// cause a leave.
1.29 +// - Test and verify the results of the RRef allocation methods.
1.30 +// Platforms/Drives/Compatibility:
1.31 +// All.
1.32 +// Assumptions/Requirement/Pre-requisites:
1.33 +// Failures and causes:
1.34 +// Base Port information:
1.35 +//
1.36 +//
1.37 +
1.38 +#include <e32test.h>
1.39 +
1.40 +LOCAL_D RTest test(_L("T_REF"));
1.41 +
1.42 +struct SObj
1.43 + {
1.44 + TInt iInt;
1.45 + TBuf<0x100> aBuf;
1.46 + };
1.47 +
1.48 +void Test1()
1.49 +// All methods
1.50 + {
1.51 +
1.52 + __UHEAP_MARK;
1.53 + SObj anObj;
1.54 + anObj.iInt=10;
1.55 + anObj.aBuf=_L("Hello World");
1.56 + RRef<SObj> aRef1;
1.57 + aRef1.Alloc(anObj);
1.58 + RRef<SObj> aRef2(aRef1);
1.59 + aRef2=aRef1;
1.60 + aRef2.AllocL(anObj);
1.61 + test(aRef1->iInt==10);
1.62 + test(((SObj*)aRef1)->aBuf==_L("Hello World"));
1.63 + aRef1.Free();
1.64 + aRef2.Free();
1.65 + __UHEAP_MARKEND;
1.66 + }
1.67 +
1.68 +void Test2()
1.69 +// Constructors
1.70 + {
1.71 + __UHEAP_MARK;
1.72 + __UHEAP_MARK;
1.73 + SObj anObj;
1.74 + anObj.iInt=10;
1.75 + anObj.aBuf=_L("Hello World");
1.76 + RRef<SObj> aRef1;
1.77 + aRef1.Alloc(anObj);
1.78 + RRef<SObj> aRef2(aRef1);
1.79 + test(aRef1->iInt==10);
1.80 + test(aRef1->aBuf==_L("Hello World"));
1.81 + test(aRef2->iInt==10);
1.82 + test(aRef2->aBuf==_L("Hello World"));
1.83 + aRef1.Free();
1.84 + __UHEAP_MARKENDC(1);
1.85 + aRef2.Free();
1.86 + __UHEAP_MARKEND;
1.87 + }
1.88 +
1.89 +void Test3()
1.90 +// Assignment methods
1.91 + {
1.92 + __UHEAP_MARK;
1.93 + __UHEAP_MARK;
1.94 + SObj anObj;
1.95 + anObj.iInt=10;
1.96 + anObj.aBuf=_L("Hello World");
1.97 + RRef<SObj> aRef1;
1.98 + RRef<SObj> aRef2;
1.99 + aRef1.Alloc(anObj);
1.100 + aRef2=aRef1;
1.101 + test(aRef1->iInt==10);
1.102 + test(aRef1->aBuf==_L("Hello World"));
1.103 + test(aRef2->iInt==10);
1.104 + test(aRef2->aBuf==_L("Hello World"));
1.105 + aRef1.Free();
1.106 + __UHEAP_MARKENDC(1);
1.107 + aRef2.Free();
1.108 + __UHEAP_MARKEND;
1.109 + }
1.110 +
1.111 +void Test4()
1.112 +// Smart Pointer methods
1.113 + {
1.114 + SObj anObj;
1.115 + anObj.iInt=10;
1.116 + anObj.aBuf=_L("Hello World");
1.117 + RRef<SObj> aRef1;
1.118 + aRef1.Alloc(anObj);
1.119 + test(aRef1->iInt==10);
1.120 + test(aRef1->aBuf==_L("Hello World"));
1.121 + SObj* pObj=(SObj*)aRef1;
1.122 + test(pObj->iInt==10);
1.123 + test(pObj->aBuf==_L("Hello World"));
1.124 + }
1.125 +
1.126 +void allocAllL(TInt*& aPtr,RRef<SObj>& aRef1,SObj& anObj)
1.127 +//
1.128 +// Alloc all and cause leave.
1.129 +//
1.130 + {
1.131 +
1.132 + TInt temp;
1.133 + TInt avail=User::Available(temp);
1.134 + aPtr=(TInt*)User::AllocL(avail);
1.135 + aRef1.AllocL(anObj);
1.136 +
1.137 +/*
1.138 +
1.139 + Reinstate if some method is found of forcing alloc fail
1.140 + This test no longer works due to heap growing functionality of E32 057
1.141 +
1.142 + ... so leave with KErrNoMemory to simulate alloc failure
1.143 +*/
1.144 + User::Leave(KErrNoMemory);
1.145 + }
1.146 +
1.147 +void Test5()
1.148 +//
1.149 +// Allocation methods
1.150 +//
1.151 + {
1.152 +
1.153 + __UHEAP_MARK;
1.154 + SObj anObj;
1.155 + anObj.iInt=10;
1.156 + anObj.aBuf=_L("Hello World");
1.157 + RRef<SObj> aRef1;
1.158 + aRef1.Alloc(anObj);
1.159 + test(aRef1->iInt==10);
1.160 + test(aRef1->aBuf==_L("Hello World"));
1.161 + aRef1.Alloc(anObj,sizeof(SObj)-245*sizeof(TText));
1.162 + test(aRef1->iInt==10);
1.163 + test(aRef1->aBuf==_L("Hello World"));
1.164 + aRef1.AllocL(anObj);
1.165 + test(aRef1->iInt==10);
1.166 + test(aRef1->aBuf==_L("Hello World"));
1.167 + aRef1.AllocL(anObj,sizeof(SObj)-245*sizeof(TText));
1.168 + test(aRef1->iInt==10);
1.169 + test(aRef1->aBuf==_L("Hello World"));
1.170 + TInt* p=0;
1.171 + TRAPD(ret,allocAllL(p,aRef1,anObj))
1.172 + test(ret==KErrNoMemory);
1.173 + User::Free(p);
1.174 + aRef1.AllocL(anObj);
1.175 + test(aRef1->iInt==10);
1.176 + test(aRef1->aBuf==_L("Hello World"));
1.177 + aRef1.Free();
1.178 + __UHEAP_MARKEND;
1.179 + }
1.180 +
1.181 +TInt E32Main()
1.182 + {
1.183 +
1.184 + test.Title();
1.185 + test.Start(_L("class RRef"));
1.186 + test.Next(_L("Test all methods"));
1.187 + Test1();
1.188 + test.Next(_L("Constructors"));
1.189 + Test2();
1.190 + test.Next(_L("Assignment methods"));
1.191 + Test3();
1.192 + test.Next(_L("Smart pointer methods"));
1.193 + Test4();
1.194 + test.Next(_L("Allocation methods"));
1.195 + Test5();
1.196 + test.End();
1.197 + return(0);
1.198 + }
1.199 +
1.200 +