os/kernelhwsrv/kerneltest/e32test/heap/t_heapdb.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\heap\t_heapdb.cpp
sl@0
    15
// Tests the _DEBUG build dependent aspects of RHeap
sl@0
    16
// Overview:
sl@0
    17
// Tests debug build dependent aspects of RHeap.  
sl@0
    18
// API Information:
sl@0
    19
// RHeap.
sl@0
    20
// Details:
sl@0
    21
// Test1:
sl@0
    22
// - Allocate a variety of user heap objects and verify the nesting level, allocation count, 
sl@0
    23
// allocation level and length are correct. Also check for heap corruption.
sl@0
    24
// Test 2:
sl@0
    25
// - Some assorted indirect calls to alloc, and verify the nesting level, allocation count, 
sl@0
    26
// allocation level and length are correct. Also check for heap corruption.
sl@0
    27
// Test3:
sl@0
    28
// - Allocate a variety of objects and verify that the UHEAP_CHECKALL count is correct.
sl@0
    29
// - Verify the nesting of UHEAP_MARK and UHEAP_MARKEND macros.
sl@0
    30
// - Check the validity of the current thread's default heap.
sl@0
    31
// Test4:
sl@0
    32
// - Allocate memory for different heaps, check the total number of allocated cells
sl@0
    33
// for different heaps and for the current nested level is as expected.
sl@0
    34
// Test5:
sl@0
    35
// - Simulate heap allocation failures, allocate the memory from user and 
sl@0
    36
// kernel heap and check results are as expected.
sl@0
    37
// Platforms/Drives/Compatibility:
sl@0
    38
// All
sl@0
    39
// Assumptions/Requirement/Pre-requisites:
sl@0
    40
// Failures and causes:
sl@0
    41
// Base Port information:
sl@0
    42
// 
sl@0
    43
//
sl@0
    44
sl@0
    45
#include <e32test.h>
sl@0
    46
#include <e32def.h>
sl@0
    47
#include <e32def_private.h>
sl@0
    48
sl@0
    49
LOCAL_D RTest test(_L("T_HEAPDB"));
sl@0
    50
sl@0
    51
#if defined(_DEBUG)
sl@0
    52
sl@0
    53
RHeap::SHeapCellInfo CellInfo[4];
sl@0
    54
sl@0
    55
class RTestHeap : public RHeap
sl@0
    56
	{
sl@0
    57
public:
sl@0
    58
	void AttachInfo(SHeapCellInfo* aInfo)
sl@0
    59
		{iTestData = aInfo;}
sl@0
    60
	};
sl@0
    61
sl@0
    62
void AttachToHeap(RHeap* aHeap, TInt aInfo)
sl@0
    63
	{
sl@0
    64
	if (!aHeap)
sl@0
    65
		aHeap = (RHeap*)&User::Allocator();
sl@0
    66
	((RTestHeap*)aHeap)->AttachInfo(CellInfo + aInfo);
sl@0
    67
	}
sl@0
    68
sl@0
    69
void TestCellInfo(TInt aInfo, TInt aNest, TInt aAllocCount, TInt aLevelAlloc, TInt aSize, TAny* aAddr)
sl@0
    70
	{
sl@0
    71
	RHeap::SHeapCellInfo& ci = CellInfo[aInfo];
sl@0
    72
	RHeap::SDebugCell& cell = *ci.iStranded;
sl@0
    73
	test(cell.nestingLevel == aNest);
sl@0
    74
	test(cell.allocCount == aAllocCount);
sl@0
    75
	test(ci.iLevelAlloc == aLevelAlloc);
sl@0
    76
	test(cell.len == aSize + RHeap::EAllocCellSize);
sl@0
    77
	test((&cell+1) == aAddr);
sl@0
    78
	}
sl@0
    79
sl@0
    80
const TInt KMaxFailureRate=100;
sl@0
    81
const TInt KThreadMemError=-50;
sl@0
    82
const TInt KCellSize=(sizeof(RHeap::SCell)); // Size of free cell header	
sl@0
    83
const TInt KHeadSize=(sizeof(RHeap::SDebugCell)); // Size of allocated cell header with space for heaven info
sl@0
    84
sl@0
    85
LOCAL_D TInt heapCount=1;
sl@0
    86
LOCAL_D RSemaphore threadSemaphore;
sl@0
    87
LOCAL_D TBool array1[KMaxFailureRate+1];
sl@0
    88
LOCAL_D	TBool array2[KMaxFailureRate+1];
sl@0
    89
sl@0
    90
LOCAL_C TInt ThreadEntryPoint(TAny*)
sl@0
    91
	{
sl@0
    92
	threadSemaphore.Wait();
sl@0
    93
	if (User::Alloc(4)==NULL)
sl@0
    94
		return(KThreadMemError);
sl@0
    95
	else
sl@0
    96
		return(KErrNone);
sl@0
    97
	} 
sl@0
    98
sl@0
    99
class TestRHeapDebug
sl@0
   100
	{
sl@0
   101
public:
sl@0
   102
	void Test1(void);
sl@0
   103
	void Test2(void);
sl@0
   104
	void Test3(void);
sl@0
   105
	void Test4(void);
sl@0
   106
	void Test5(void);
sl@0
   107
	};
sl@0
   108
sl@0
   109
LOCAL_C RHeap* allocHeap(TInt aSize)
sl@0
   110
//
sl@0
   111
// Allocate a chunk heap with max size aSize
sl@0
   112
//
sl@0
   113
	{
sl@0
   114
sl@0
   115
	TName n;
sl@0
   116
	n.Format(_L("TESTHEAP%d"),heapCount++);
sl@0
   117
	return(User::ChunkHeap(&n,aSize,aSize));
sl@0
   118
	}
sl@0
   119
sl@0
   120
void TestRHeapDebug::Test1(void)
sl@0
   121
	{
sl@0
   122
sl@0
   123
	TAny* p;
sl@0
   124
sl@0
   125
	///////////////////////
sl@0
   126
	// Test heaven cell is found for each method of allocating memory
sl@0
   127
	////////////////////////
sl@0
   128
sl@0
   129
	// new(TInt aSize)
sl@0
   130
	__UHEAP_MARK;
sl@0
   131
	__UHEAP_CHECKALL(0);
sl@0
   132
	__UHEAP_CHECK(0);
sl@0
   133
	p=new TUint; 
sl@0
   134
	__UHEAP_CHECKALL(1);
sl@0
   135
	__UHEAP_CHECK(1);
sl@0
   136
	__UHEAP_MARKEND;
sl@0
   137
	__UHEAP_CHECK(0);
sl@0
   138
	__UHEAP_CHECKALL(1);
sl@0
   139
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   140
	User::Free(p);
sl@0
   141
sl@0
   142
	// new(TInt aSize,TInt anExtraSize)
sl@0
   143
	__UHEAP_MARK;
sl@0
   144
	p=new(4) TUint; 
sl@0
   145
	__UHEAP_MARKEND;
sl@0
   146
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   147
	User::Free(p);
sl@0
   148
sl@0
   149
	// 	new(TInt aSize,TLeave)
sl@0
   150
	__UHEAP_MARK;
sl@0
   151
	p=new(ELeave) TUint; 
sl@0
   152
	__UHEAP_MARKEND;
sl@0
   153
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   154
	User::Free(p);
sl@0
   155
sl@0
   156
	// Alloc
sl@0
   157
	__UHEAP_MARK;
sl@0
   158
	p=User::Alloc(32); 
sl@0
   159
	__UHEAP_MARKEND;
sl@0
   160
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   161
	User::Free(p);
sl@0
   162
sl@0
   163
	// AllocL
sl@0
   164
	__UHEAP_MARK;
sl@0
   165
	p=User::AllocL(32);
sl@0
   166
	__UHEAP_MARKEND;
sl@0
   167
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   168
	User::Free(p);
sl@0
   169
sl@0
   170
	// ReAlloc with Null parameter
sl@0
   171
	__UHEAP_MARK;
sl@0
   172
	p=User::ReAlloc(NULL, 32); 
sl@0
   173
	__UHEAP_MARKEND;
sl@0
   174
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   175
	User::Free(p);
sl@0
   176
sl@0
   177
	// ReAllocL with Null parameter
sl@0
   178
	__UHEAP_MARK;
sl@0
   179
	p=User::ReAllocL(NULL, 32); 
sl@0
   180
	__UHEAP_MARKEND;
sl@0
   181
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   182
	User::Free(p);
sl@0
   183
sl@0
   184
	// ReAlloc with non Null parameter
sl@0
   185
	__UHEAP_MARK;
sl@0
   186
	p=User::Alloc(128);	   
sl@0
   187
	p=User::ReAlloc(p, 4); 
sl@0
   188
	__UHEAP_MARKEND;
sl@0
   189
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   190
	User::Free(p);
sl@0
   191
					
sl@0
   192
	// ReAlloc with non Null parameter such that cell is moved in memory
sl@0
   193
	__UHEAP_MARK;
sl@0
   194
	p=User::Alloc(128);	   
sl@0
   195
	TAny* temp=User::Alloc(128);
sl@0
   196
	p=User::ReAlloc(p, 526);   
sl@0
   197
	User::Free(temp);
sl@0
   198
	__UHEAP_MARKEND;
sl@0
   199
	TestCellInfo(0, 1, 3, 1, User::AllocLen(p), p);
sl@0
   200
	User::Free(p);
sl@0
   201
sl@0
   202
	// ReAllocL with non Null parameter
sl@0
   203
	__UHEAP_MARK;
sl@0
   204
	p=User::Alloc(32);	   
sl@0
   205
	p=User::ReAllocL(p, 128); 
sl@0
   206
	__UHEAP_MARKEND;
sl@0
   207
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p), p);
sl@0
   208
	User::Free(p);
sl@0
   209
	}
sl@0
   210
sl@0
   211
sl@0
   212
void TestRHeapDebug::Test2(void)
sl@0
   213
	{ 
sl@0
   214
	// Some assorted indirect calls to alloc
sl@0
   215
sl@0
   216
	__UHEAP_MARK;
sl@0
   217
	CBufFlat* pBuf=CBufFlat::NewL(10); 
sl@0
   218
	__UHEAP_MARKEND;
sl@0
   219
	TestCellInfo(0, 1, 1, 1, User::AllocLen(pBuf), pBuf);
sl@0
   220
	delete pBuf;
sl@0
   221
sl@0
   222
	__UHEAP_MARK;
sl@0
   223
	HBufC8* pHBufC=HBufC8::New(10);	
sl@0
   224
	__UHEAP_MARKEND;
sl@0
   225
	TestCellInfo(0, 1, 1, 1, User::AllocLen(pHBufC), pHBufC);
sl@0
   226
	delete pHBufC;
sl@0
   227
sl@0
   228
// can also create a HBufC8 from a descriptor by using TDesC::Alloc
sl@0
   229
	}
sl@0
   230
sl@0
   231
sl@0
   232
void TestRHeapDebug::Test3(void)
sl@0
   233
	{ 
sl@0
   234
sl@0
   235
	//  Check num of cells detected is correct and CHECKTOTALNUM is ok
sl@0
   236
	// NOTE: CHECKTOTALNUM counts the TOTAL number of allocations in the heap regardless of
sl@0
   237
	// any MARKSTARTs
sl@0
   238
	// NOTE: the alloc count commences from the FIRST occurrence of a MARKSTART, so if one is nested
sl@0
   239
	// in another the alloc count will only start from the second MARKSTART if it applies to a 
sl@0
   240
	// different heap.
sl@0
   241
	__UHEAP_MARK;
sl@0
   242
	__UHEAP_CHECKALL(0);
sl@0
   243
	TAny* p1= new TUint; 
sl@0
   244
	__UHEAP_CHECKALL(1);
sl@0
   245
	TAny* p2= new(20) TUint;
sl@0
   246
	__UHEAP_CHECKALL(2);
sl@0
   247
	TAny* p3= User::Alloc(15); 
sl@0
   248
	__UHEAP_CHECKALL(3);
sl@0
   249
	__UHEAP_MARK;
sl@0
   250
	__UHEAP_CHECK(0);
sl@0
   251
	TAny* p4=User::Alloc(1); 
sl@0
   252
	TAny* p5 =new TUint; 
sl@0
   253
	__UHEAP_CHECK(2);
sl@0
   254
	__UHEAP_CHECKALL(5);
sl@0
   255
	__UHEAP_MARKEND;
sl@0
   256
	TestCellInfo(0, 2, 4, 2, User::AllocLen(p4), p4);
sl@0
   257
	__UHEAP_CHECKALL(5);
sl@0
   258
	__UHEAP_CHECK(3);
sl@0
   259
	__UHEAP_MARKENDC(3);
sl@0
   260
	User::Free(p1);
sl@0
   261
	User::Free(p2);
sl@0
   262
	User::Free(p3);
sl@0
   263
	User::Free(p4);
sl@0
   264
	User::Free(p5);
sl@0
   265
sl@0
   266
	// Check some nesting out
sl@0
   267
	p1=new TUint;
sl@0
   268
	__UHEAP_MARK;
sl@0
   269
	p2=new TUint; 
sl@0
   270
	__UHEAP_MARK;
sl@0
   271
	p3=new TUint; 
sl@0
   272
	__UHEAP_MARK;
sl@0
   273
	p4=new TUint; 
sl@0
   274
	__UHEAP_MARKEND;
sl@0
   275
	TestCellInfo(0, 3, 3, 1, User::AllocLen(p4), p4);
sl@0
   276
	__UHEAP_MARKEND;
sl@0
   277
	TestCellInfo(0, 2, 2, 1, User::AllocLen(p3), p3);
sl@0
   278
	__UHEAP_MARKEND;
sl@0
   279
	TestCellInfo(0, 1, 1, 1, User::AllocLen(p2), p2);
sl@0
   280
	User::Free(p1);
sl@0
   281
	User::Free(p2);
sl@0
   282
	User::Free(p3);
sl@0
   283
	User::Free(p4);
sl@0
   284
	User::Check();
sl@0
   285
	}
sl@0
   286
sl@0
   287
void TestRHeapDebug::Test4(void)
sl@0
   288
	{
sl@0
   289
	// Test with different heaps
sl@0
   290
	TAny* p1=new TUint;
sl@0
   291
	__UHEAP_MARK;	// Default start
sl@0
   292
	__UHEAP_CHECKALL(1);
sl@0
   293
	__UHEAP_CHECK(0);
sl@0
   294
	TAny* p2=new TUint;	  
sl@0
   295
	RHeap* pHeap1=allocHeap(1000); 
sl@0
   296
	AttachToHeap(pHeap1,1);
sl@0
   297
	__RHEAP_MARK(pHeap1); // Heap1 start
sl@0
   298
	__RHEAP_CHECKALL(pHeap1,0);
sl@0
   299
	__RHEAP_CHECK(pHeap1,0);
sl@0
   300
	TAny* p3=pHeap1->Alloc(4); 
sl@0
   301
	__RHEAP_CHECKALL(pHeap1,1);
sl@0
   302
	__RHEAP_CHECK(pHeap1,1);
sl@0
   303
	__RHEAP_CHECKALL(pHeap1,1);
sl@0
   304
	__UHEAP_CHECKALL(3);
sl@0
   305
	RHeap* pHeap2=allocHeap(1000);
sl@0
   306
	AttachToHeap(pHeap2,2);
sl@0
   307
	RHeap* pHeap3=allocHeap(1000);
sl@0
   308
	AttachToHeap(pHeap3,3);
sl@0
   309
	__UHEAP_CHECKALL(5);
sl@0
   310
	__RHEAP_MARK(pHeap2); // Heap2 start
sl@0
   311
	__RHEAP_MARK(pHeap3); // Heap3 start
sl@0
   312
	TAny* p4=pHeap2->Alloc(8); 
sl@0
   313
	TAny* p5=pHeap2->Alloc(37);
sl@0
   314
	TAny* p6=pHeap3->Alloc(32);	
sl@0
   315
	TAny* p7=pHeap1->Alloc(43);	
sl@0
   316
	__UHEAP_CHECKALL(5);
sl@0
   317
	__RHEAP_CHECKALL(pHeap1,2);
sl@0
   318
	__RHEAP_CHECKALL(pHeap2,2);
sl@0
   319
	__RHEAP_CHECKALL(pHeap3,1);
sl@0
   320
	__RHEAP_MARKEND(pHeap3); // Heap3 end
sl@0
   321
	TestCellInfo(3, 1, 1, 1, pHeap3->AllocLen(p6), p6);
sl@0
   322
	__RHEAP_MARKEND(pHeap2); // Heap2 end
sl@0
   323
	TestCellInfo(2, 1, 1, 2, pHeap2->AllocLen(p4), p4);
sl@0
   324
	pHeap1->Free(p3);
sl@0
   325
	__RHEAP_MARKEND(pHeap1); // Heap1 end
sl@0
   326
	TestCellInfo(1, 1, 2, 1, pHeap1->AllocLen(p7), p7);
sl@0
   327
	User::Free(p1);
sl@0
   328
	User::Free(p2);
sl@0
   329
	pHeap2->Free(p4);
sl@0
   330
	pHeap2->Free(p5);
sl@0
   331
	pHeap3->Free(p6);
sl@0
   332
	pHeap1->Free(p7);
sl@0
   333
	__UHEAP_CHECKALL(3);   
sl@0
   334
	pHeap2->Close();
sl@0
   335
	pHeap3->Close();
sl@0
   336
	__UHEAP_MARKEND;
sl@0
   337
	pHeap1->Close();
sl@0
   338
	__UHEAP_CHECKALL(0);
sl@0
   339
	}
sl@0
   340
sl@0
   341
void TestRHeapDebug::Test5()
sl@0
   342
// Check the alloc failure macros
sl@0
   343
	{
sl@0
   344
	TAny *p, *p1;
sl@0
   345
	RHeap* pHeap=allocHeap(1000);
sl@0
   346
sl@0
   347
	// DETERMINISTIC FAILURE
sl@0
   348
	__UHEAP_RESET;
sl@0
   349
	__UHEAP_FAILNEXT(1);
sl@0
   350
	test(User::Alloc(1)==NULL);
sl@0
   351
	p=User::Alloc(1);
sl@0
   352
	test(p!=NULL);
sl@0
   353
	User::FreeZ(p);
sl@0
   354
	__UHEAP_RESET;
sl@0
   355
sl@0
   356
	__RHEAP_RESET(pHeap);
sl@0
   357
	__RHEAP_FAILNEXT(pHeap,1);
sl@0
   358
	test(pHeap->Alloc(1)==NULL);
sl@0
   359
	p=pHeap->Alloc(1);
sl@0
   360
	test(p!=NULL);
sl@0
   361
	pHeap->FreeZ(p);
sl@0
   362
   	__RHEAP_RESET(pHeap);
sl@0
   363
sl@0
   364
	__KHEAP_RESET;
sl@0
   365
	__KHEAP_FAILNEXT(1);
sl@0
   366
	RSemaphore semaphore;
sl@0
   367
	test(semaphore.CreateLocal(1)==KErrNoMemory); // allocated from the kernel heap
sl@0
   368
	test(semaphore.CreateLocal(1)==KErrNone);
sl@0
   369
	semaphore.Close();
sl@0
   370
	__KHEAP_RESET;
sl@0
   371
sl@0
   372
	__UHEAP_SETFAIL(RHeap::EDeterministic,0);
sl@0
   373
	test(User::Alloc(1)==NULL);
sl@0
   374
	__UHEAP_RESET;
sl@0
   375
sl@0
   376
	__RHEAP_SETFAIL(pHeap,RHeap::EDeterministic,0);
sl@0
   377
	test(pHeap->Alloc(1)==NULL);
sl@0
   378
	__RHEAP_RESET(pHeap);
sl@0
   379
sl@0
   380
	__KHEAP_SETFAIL(RHeap::EDeterministic,0);
sl@0
   381
	test(semaphore.CreateLocal(1)==KErrNoMemory);
sl@0
   382
	__KHEAP_RESET;
sl@0
   383
sl@0
   384
	TInt determinism;
sl@0
   385
	for(determinism=1; determinism<=KMaxFailureRate; determinism++)
sl@0
   386
		{
sl@0
   387
		__UHEAP_SETFAIL(RHeap::EDeterministic,determinism);
sl@0
   388
		__RHEAP_SETFAIL(pHeap,RHeap::EDeterministic,determinism);
sl@0
   389
		for(TInt ii=1; ii<=determinism; ii++)
sl@0
   390
			{
sl@0
   391
			p=User::Alloc(1);
sl@0
   392
			p1=pHeap->Alloc(1);
sl@0
   393
			if(ii%determinism==0)
sl@0
   394
				{
sl@0
   395
				test(p==NULL);
sl@0
   396
				test(p1==NULL);
sl@0
   397
				}
sl@0
   398
			else
sl@0
   399
				{
sl@0
   400
				test(p!=NULL);
sl@0
   401
				test(p1!=NULL);
sl@0
   402
				pHeap->Free(p1); 
sl@0
   403
				User::Free(p);
sl@0
   404
				}
sl@0
   405
			}
sl@0
   406
		}
sl@0
   407
	__UHEAP_RESET;
sl@0
   408
	__RHEAP_RESET(pHeap);
sl@0
   409
sl@0
   410
	// Test SetKernelAllocFail
sl@0
   411
	// its not possible to test SetKernelAllocFail as above as it is not possible to control the
sl@0
   412
	// number of calls to Alloc for the dernel heap - but the following will definitely fail:
sl@0
   413
	__KHEAP_SETFAIL(RHeap::EDeterministic,1);
sl@0
   414
	RSemaphore r; 
sl@0
   415
	test(r.CreateLocal(1)==KErrNoMemory); // allocated from the kernel heap
sl@0
   416
	__KHEAP_SETFAIL(RHeap::EDeterministic,50);
sl@0
   417
	test(r.CreateLocal(1)==KErrNone);
sl@0
   418
	r.Close();
sl@0
   419
	__KHEAP_RESET;
sl@0
   420
sl@0
   421
	// RANDOM TESTS
sl@0
   422
	TInt numOccurences1, numOccurences2;
sl@0
   423
sl@0
   424
	__UHEAP_SETFAIL(RHeap::ERandom,1);
sl@0
   425
	test(User::Alloc(1)==NULL);
sl@0
   426
	__UHEAP_RESET;
sl@0
   427
sl@0
   428
	__RHEAP_SETFAIL(pHeap,RHeap::ERandom,1);
sl@0
   429
	test(pHeap->Alloc(1)==NULL);
sl@0
   430
	__RHEAP_RESET(pHeap);
sl@0
   431
sl@0
   432
//	__KHEAP_SETFAIL(RHeap::ERandom,1);
sl@0
   433
//	test(semaphore.CreateLocal(1)==KErrNoMemory);
sl@0
   434
//	__KHEAP_RESET;
sl@0
   435
sl@0
   436
	__UHEAP_SETFAIL(RHeap::ETrueRandom,1);
sl@0
   437
	test(User::Alloc(1)==NULL);
sl@0
   438
	__UHEAP_RESET;
sl@0
   439
sl@0
   440
	__RHEAP_SETFAIL(pHeap,RHeap::ETrueRandom,1);
sl@0
   441
	test(pHeap->Alloc(1)==NULL);
sl@0
   442
	__RHEAP_RESET(pHeap);
sl@0
   443
sl@0
   444
//	__KHEAP_SETFAIL(RHeap::ETrueRandom,1);
sl@0
   445
//	test(semaphore.CreateLocal(1)==KErrNoMemory);
sl@0
   446
//	__KHEAP_RESET;
sl@0
   447
sl@0
   448
	for(determinism=1; determinism<=KMaxFailureRate; determinism++)
sl@0
   449
		{
sl@0
   450
		__UHEAP_SETFAIL(RHeap::ERandom,determinism);
sl@0
   451
		__RHEAP_SETFAIL(pHeap,RHeap::ERandom,determinism);
sl@0
   452
        TInt ii;
sl@0
   453
		for(ii=1; ii<=determinism; ii++)
sl@0
   454
			{
sl@0
   455
			p=User::Alloc(1);
sl@0
   456
			p1=pHeap->Alloc(1);
sl@0
   457
			array1[ii]=(p==NULL);
sl@0
   458
			array2[ii]=(p==NULL);
sl@0
   459
			if(p)
sl@0
   460
				User::Free(p);
sl@0
   461
			if(p1)
sl@0
   462
				pHeap->Free(p1);
sl@0
   463
			}
sl@0
   464
		numOccurences1=0;
sl@0
   465
		numOccurences2=0;
sl@0
   466
		for(ii=1; ii<=determinism; ii++)
sl@0
   467
			{
sl@0
   468
			if(array1[ii])
sl@0
   469
				numOccurences1++;					
sl@0
   470
			if(array2[ii])
sl@0
   471
				numOccurences2++;
sl@0
   472
			}
sl@0
   473
		test(numOccurences1==1);
sl@0
   474
		test(numOccurences2==1);
sl@0
   475
		}
sl@0
   476
	__UHEAP_RESET;
sl@0
   477
	__RHEAP_RESET(pHeap);		
sl@0
   478
sl@0
   479
	__UHEAP_SETFAIL(RHeap::ERandom,5);
sl@0
   480
	TInt ii;
sl@0
   481
	for(ii=1; ii<=50; ii++)
sl@0
   482
		{
sl@0
   483
		p=User::Alloc(1);
sl@0
   484
		array1[ii]=(p==NULL);
sl@0
   485
		if(p)	
sl@0
   486
			User::Free(p);
sl@0
   487
		}
sl@0
   488
	numOccurences1=0;
sl@0
   489
	numOccurences2=0;
sl@0
   490
	for(ii=1; ii<=50; ii++)
sl@0
   491
		{
sl@0
   492
		if(array1[ii])
sl@0
   493
			{
sl@0
   494
			numOccurences1++;
sl@0
   495
			numOccurences2++;
sl@0
   496
			}
sl@0
   497
		if(ii%5==0)
sl@0
   498
			{
sl@0
   499
			test(numOccurences1==1);
sl@0
   500
			numOccurences1=0;
sl@0
   501
			}
sl@0
   502
		}
sl@0
   503
	test(numOccurences2==50/5);	
sl@0
   504
	
sl@0
   505
	// Cannot really test random failure of the kernel heap accurately
sl@0
   506
sl@0
   507
	pHeap->Close();
sl@0
   508
	//client.Disconnect();
sl@0
   509
sl@0
   510
	// Test failing the heap of a child thread
sl@0
   511
	// 1st test that it allocates normally
sl@0
   512
	TRequestStatus stat;
sl@0
   513
	RThread thread;
sl@0
   514
	test(threadSemaphore.CreateLocal(0)==KErrNone);
sl@0
   515
	test(thread.Create(_L("Thread"),ThreadEntryPoint,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
sl@0
   516
	thread.Logon(stat);
sl@0
   517
	thread.Resume();
sl@0
   518
	threadSemaphore.Signal();
sl@0
   519
	User::WaitForRequest(stat);
sl@0
   520
	test(thread.ExitReason()==KErrNone);
sl@0
   521
	thread.Close();
sl@0
   522
#if defined(CAN_TEST_THREADS)
sl@0
   523
	// Now make the thread's heap fail
sl@0
   524
	test(thread.Create(_L("Thread"),ThreadEntryPoint,KDefaultStackSize,0x200,0x200,NULL)==KErrNone);
sl@0
   525
	thread.Logon(stat);
sl@0
   526
	thread.Resume();
sl@0
   527
	TH_FAILNEXT(thread.Handle());
sl@0
   528
	threadSemaphore.Signal();
sl@0
   529
	User::WaitForRequest(stat);
sl@0
   530
	test(thread.ExitReason()==KThreadMemError);
sl@0
   531
	thread.Close();
sl@0
   532
	threadSemaphore.Close();
sl@0
   533
#endif
sl@0
   534
	}	
sl@0
   535
sl@0
   536
GLDEF_C TInt E32Main(void)
sl@0
   537
    {
sl@0
   538
sl@0
   539
	test.Title();
sl@0
   540
	AttachToHeap(NULL,0);
sl@0
   541
	test.Start(_L("Test1"));
sl@0
   542
	TestRHeapDebug T;
sl@0
   543
	T.Test1();	 
sl@0
   544
	test.Next(_L("Test2"));
sl@0
   545
	T.Test2();
sl@0
   546
	test.Next(_L("Test3"));
sl@0
   547
	T.Test3();
sl@0
   548
	test.Next(_L("Test4"));
sl@0
   549
	T.Test4();
sl@0
   550
	test.Next(_L("Test5"));
sl@0
   551
	T.Test5();
sl@0
   552
	test.End();
sl@0
   553
	return(0);
sl@0
   554
    }
sl@0
   555
#else
sl@0
   556
GLDEF_C TInt E32Main()
sl@0
   557
//
sl@0
   558
// Test unavailable in release build.
sl@0
   559
//
sl@0
   560
    {
sl@0
   561
sl@0
   562
	test.Title();	
sl@0
   563
	test.Start(_L("No tests for release builds"));
sl@0
   564
	test.End();
sl@0
   565
	return(0);
sl@0
   566
    }
sl@0
   567
#endif
sl@0
   568
sl@0
   569
sl@0
   570