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