kerneltest/e32test/system/t_ref.cpp
changeset 0 a41df078684a
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_ref.cpp
       
    15 // Overview:
       
    16 // Test the methods of the RRef class.
       
    17 // API Information:
       
    18 // RRef
       
    19 // Details:
       
    20 // - Check for the existence of all RRef methods.
       
    21 // - Test and verify the results of the RRef constructors.
       
    22 // - Test and verify the results of the RRef assignment methods.
       
    23 // - Test and verify the results of the RRef smart pointer methods.
       
    24 // - Attempt to allocate all available heap space, verify that it 
       
    25 // cause a leave.
       
    26 // - Test and verify the results of the RRef allocation methods.
       
    27 // Platforms/Drives/Compatibility:
       
    28 // All.
       
    29 // Assumptions/Requirement/Pre-requisites:
       
    30 // Failures and causes:
       
    31 // Base Port information:
       
    32 // 
       
    33 //
       
    34 
       
    35 #include <e32test.h>
       
    36 
       
    37 LOCAL_D RTest test(_L("T_REF"));
       
    38 
       
    39 struct SObj
       
    40 	{
       
    41 	TInt iInt;
       
    42 	TBuf<0x100> aBuf;
       
    43 	};
       
    44 
       
    45 void Test1()
       
    46 // All methods
       
    47 	{
       
    48 
       
    49 	__UHEAP_MARK;
       
    50 	SObj anObj;
       
    51 	anObj.iInt=10;
       
    52 	anObj.aBuf=_L("Hello World");
       
    53 	RRef<SObj> aRef1;
       
    54 	aRef1.Alloc(anObj);
       
    55 	RRef<SObj> aRef2(aRef1);
       
    56 	aRef2=aRef1;
       
    57 	aRef2.AllocL(anObj);
       
    58 	test(aRef1->iInt==10);
       
    59 	test(((SObj*)aRef1)->aBuf==_L("Hello World"));
       
    60 	aRef1.Free();
       
    61 	aRef2.Free();
       
    62 	__UHEAP_MARKEND;
       
    63 	}	
       
    64 
       
    65 void Test2()
       
    66 // Constructors
       
    67 	{
       
    68 	__UHEAP_MARK;
       
    69 	__UHEAP_MARK;
       
    70 	SObj anObj;
       
    71 	anObj.iInt=10;
       
    72 	anObj.aBuf=_L("Hello World");
       
    73 	RRef<SObj> aRef1;
       
    74 	aRef1.Alloc(anObj);
       
    75 	RRef<SObj> aRef2(aRef1);
       
    76 	test(aRef1->iInt==10);
       
    77 	test(aRef1->aBuf==_L("Hello World"));
       
    78 	test(aRef2->iInt==10);
       
    79 	test(aRef2->aBuf==_L("Hello World"));
       
    80 	aRef1.Free();
       
    81 	__UHEAP_MARKENDC(1);
       
    82 	aRef2.Free();
       
    83 	__UHEAP_MARKEND;
       
    84 	}
       
    85 
       
    86 void Test3()
       
    87 // Assignment methods
       
    88 	{
       
    89 	__UHEAP_MARK;
       
    90 	__UHEAP_MARK;
       
    91 	SObj anObj;
       
    92 	anObj.iInt=10;
       
    93 	anObj.aBuf=_L("Hello World");
       
    94 	RRef<SObj> aRef1;
       
    95 	RRef<SObj> aRef2;
       
    96 	aRef1.Alloc(anObj);
       
    97 	aRef2=aRef1;
       
    98 	test(aRef1->iInt==10);
       
    99 	test(aRef1->aBuf==_L("Hello World"));
       
   100 	test(aRef2->iInt==10);
       
   101 	test(aRef2->aBuf==_L("Hello World"));
       
   102 	aRef1.Free();
       
   103 	__UHEAP_MARKENDC(1);
       
   104 	aRef2.Free();
       
   105 	__UHEAP_MARKEND;
       
   106 	}
       
   107 
       
   108 void Test4()
       
   109 // Smart Pointer methods
       
   110 	{
       
   111 	SObj anObj;
       
   112 	anObj.iInt=10;
       
   113 	anObj.aBuf=_L("Hello World");
       
   114 	RRef<SObj> aRef1;
       
   115 	aRef1.Alloc(anObj);
       
   116 	test(aRef1->iInt==10);
       
   117 	test(aRef1->aBuf==_L("Hello World"));
       
   118 	SObj* pObj=(SObj*)aRef1;
       
   119 	test(pObj->iInt==10);
       
   120 	test(pObj->aBuf==_L("Hello World"));
       
   121 	}
       
   122 
       
   123 void allocAllL(TInt*& aPtr,RRef<SObj>& aRef1,SObj& anObj)
       
   124 //
       
   125 // Alloc all and cause leave.
       
   126 //
       
   127 	{
       
   128 
       
   129 	TInt temp;
       
   130 	TInt avail=User::Available(temp);
       
   131 	aPtr=(TInt*)User::AllocL(avail);	
       
   132 	aRef1.AllocL(anObj);
       
   133 
       
   134 /*
       
   135 
       
   136     Reinstate if some method is found of forcing alloc fail
       
   137     This test no longer works due to heap growing functionality of E32 057
       
   138 
       
   139     ... so leave with KErrNoMemory to simulate alloc failure
       
   140 */
       
   141     User::Leave(KErrNoMemory);
       
   142 	}
       
   143 
       
   144 void Test5()
       
   145 //
       
   146 // Allocation methods
       
   147 //
       
   148 	{
       
   149 
       
   150 	__UHEAP_MARK;
       
   151 	SObj anObj;
       
   152 	anObj.iInt=10;
       
   153 	anObj.aBuf=_L("Hello World");
       
   154 	RRef<SObj> aRef1;
       
   155 	aRef1.Alloc(anObj);
       
   156 	test(aRef1->iInt==10);
       
   157 	test(aRef1->aBuf==_L("Hello World"));
       
   158 	aRef1.Alloc(anObj,sizeof(SObj)-245*sizeof(TText));
       
   159 	test(aRef1->iInt==10);
       
   160 	test(aRef1->aBuf==_L("Hello World"));
       
   161 	aRef1.AllocL(anObj);
       
   162 	test(aRef1->iInt==10);
       
   163 	test(aRef1->aBuf==_L("Hello World"));
       
   164 	aRef1.AllocL(anObj,sizeof(SObj)-245*sizeof(TText));
       
   165 	test(aRef1->iInt==10);
       
   166 	test(aRef1->aBuf==_L("Hello World"));
       
   167 	TInt* p=0;
       
   168 	TRAPD(ret,allocAllL(p,aRef1,anObj))
       
   169 	test(ret==KErrNoMemory);
       
   170 	User::Free(p);
       
   171 	aRef1.AllocL(anObj);
       
   172 	test(aRef1->iInt==10);
       
   173 	test(aRef1->aBuf==_L("Hello World"));
       
   174 	aRef1.Free();
       
   175 	__UHEAP_MARKEND;
       
   176 	}
       
   177 
       
   178 TInt E32Main()
       
   179 	{
       
   180 
       
   181 	test.Title();
       
   182 	test.Start(_L("class RRef"));
       
   183 	test.Next(_L("Test all methods"));
       
   184 	Test1();
       
   185 	test.Next(_L("Constructors"));
       
   186 	Test2();
       
   187 	test.Next(_L("Assignment methods"));
       
   188 	Test3();
       
   189 	test.Next(_L("Smart pointer methods"));
       
   190 	Test4();
       
   191 	test.Next(_L("Allocation methods"));
       
   192 	Test5();
       
   193 	test.End();
       
   194 	return(0);
       
   195 	}
       
   196 
       
   197