os/kernelhwsrv/kerneltest/e32test/heap/t_fail.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/heap/t_fail.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,652 @@
     1.4 +// Copyright (c) 1996-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\heap\t_fail.cpp
    1.18 +// Overview:
    1.19 +// Test deterministic, random and fail-next heap failure modes. 
    1.20 +// API Information:
    1.21 +// RHeap.
    1.22 +// Details:
    1.23 +// - Simulate the EFailNext, EDeterministic, ERandom, ETrueRandom, 
    1.24 +// ENone modes of heap allocation failures and the burst variants.
    1.25 +// - Reallocate the size of an existing cell without moving it 
    1.26 +// and check the result is as expected.
    1.27 +// - Check whether heap has been corrupted by all the tests.
    1.28 +// Platforms/Drives/Compatibility:
    1.29 +// All
    1.30 +// Assumptions/Requirement/Pre-requisites:
    1.31 +// Failures and causes:
    1.32 +// Base Port information:
    1.33 +// 
    1.34 +//
    1.35 +
    1.36 +#define __E32TEST_EXTENSION__
    1.37 +#include <e32test.h>
    1.38 +#include <hal.h>
    1.39 +#include <f32file.h>
    1.40 +#include <e32panic.h>
    1.41 +#include <e32def.h>
    1.42 +#include <e32def_private.h>
    1.43 +#include <e32ldr.h>
    1.44 +#include <e32ldr_private.h>
    1.45 +#include "d_kheap.h"
    1.46 +
    1.47 +LOCAL_D RTest test(_L("T_FAIL"));
    1.48 +RKHeapDevice KHeapDevice;
    1.49 +
    1.50 +
    1.51 +#if defined _DEBUG
    1.52 +
    1.53 +/**
    1.54 +	Test we fail burst times for EBurstFailNext
    1.55 +	Defined as a macro so that it is easier to determine which test is failing.
    1.56 +
    1.57 +@param aCount The number of allocations before it should fail.
    1.58 +@param aBurst The number of allocations that should fail.
    1.59 +*/
    1.60 +
    1.61 +#define __UHEAP_TEST_BURST_FAILNEXT(aCount, aBurst) \
    1.62 +	__UHEAP_BURSTFAILNEXT(aCount, aBurst); \
    1.63 +	TEST_BURST_FAILNEXT(__UHEAP_CHECKFAILURE, aCount, aBurst)
    1.64 +
    1.65 +
    1.66 +#define __RHEAP_TEST_BURST_FAILNEXT(aHeap, aCount, aBurst) \
    1.67 +	__RHEAP_BURSTFAILNEXT(aHeap, aCount, aBurst); \
    1.68 +	TEST_BURST_FAILNEXT(__RHEAP_CHECKFAILURE(aHeap), aCount, aBurst)
    1.69 +
    1.70 +
    1.71 +#define __KHEAP_TEST_BURST_FAILNEXT(aCount, aBurst) \
    1.72 +	__KHEAP_BURSTFAILNEXT(aCount, aBurst); \
    1.73 +	test_Equal(0, __KHEAP_CHECKFAILURE); \
    1.74 +	test_KErrNone(KHeapDevice.TestBurstFailNext(aCount, aBurst)); \
    1.75 +	test_Equal(aBurst, __KHEAP_CHECKFAILURE)
    1.76 +
    1.77 +
    1.78 +#define TEST_BURST_FAILNEXT(aCheckFailure, aCount, aBurst) \
    1.79 +	{ \
    1.80 +	test_Equal(0, aCheckFailure); \
    1.81 +	for (i = 0; i < aCount; i++) \
    1.82 +		{ \
    1.83 +		if (i < aCount - 1) \
    1.84 +			{ \
    1.85 +			p = new TInt; \
    1.86 +			test_NotNull(p); \
    1.87 +			delete p; \
    1.88 +			} \
    1.89 +		else \
    1.90 +			{ \
    1.91 +			for (TUint j = 0; j < aBurst; j++) \
    1.92 +				{ \
    1.93 +				p = new TInt; \
    1.94 +				test_Equal(NULL, p); \
    1.95 +				test_Equal(j + 1, aCheckFailure); \
    1.96 +				} \
    1.97 +			} \
    1.98 +		} \
    1.99 +	p = new TInt; \
   1.100 +	test_NotNull(p); \
   1.101 +	delete p; \
   1.102 +	}
   1.103 +
   1.104 +
   1.105 +/**
   1.106 +	Test we fail burst times for EBurstDeterministic
   1.107 +	Defined as a macro so that it is easier to determine which test is failing.
   1.108 +
   1.109 +@param aRate The rate of each set of failures.
   1.110 +@param aBurst The number of allocations that should fail.
   1.111 +*/
   1.112 +#define __UHEAP_TEST_BURST_DETERMINISTIC(aRate, aBurst) \
   1.113 +	__UHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, aRate, aBurst); \
   1.114 +	TEST_BURST_DETERMINISTIC(__UHEAP_CHECKFAILURE, aRate, aBurst)
   1.115 +
   1.116 +#define __RHEAP_TEST_BURST_DETERMINISTIC(aHeap, aRate, aBurst) \
   1.117 +	__RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstDeterministic, aRate, aBurst); \
   1.118 +	TEST_BURST_DETERMINISTIC(__RHEAP_CHEAKFAILURE(aHeap), aRate, aBurst)
   1.119 +
   1.120 +#define __KHEAP_TEST_BURST_DETERMINISTIC(aRate, aBurst) \
   1.121 +	__KHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, aRate, aBurst); \
   1.122 +	test_Equal(0, __KHEAP_CHECKFAILURE); \
   1.123 +	test_KErrNone(KHeapDevice.TestBurstDeterministic(aRate, aBurst)); \
   1.124 +	test_Equal(aBurst * KHeapFailCycles, __KHEAP_CHECKFAILURE)
   1.125 +
   1.126 +#define TEST_BURST_DETERMINISTIC(aCheckFailure, aRate, aBurst) \
   1.127 +	{ \
   1.128 +	test_Equal(0, aCheckFailure); \
   1.129 +	TUint failures = 0; \
   1.130 +	for (i = 1; i <= aRate * KHeapFailCycles; i++) \
   1.131 +		{ \
   1.132 +		if (i % aRate == 0) \
   1.133 +			{ \
   1.134 +			for (TInt j = 0; j < aBurst; j++) \
   1.135 +				{ \
   1.136 +				p = new TInt; \
   1.137 +				test_Equal(NULL, p); \
   1.138 +				test_Equal(++failures, aCheckFailure); \
   1.139 +				} \
   1.140 +			} \
   1.141 +		else \
   1.142 +			{ \
   1.143 +			p = new TInt; \
   1.144 +			test(p!=NULL); \
   1.145 +			delete p; \
   1.146 +			} \
   1.147 +		} \
   1.148 +	}
   1.149 +
   1.150 +/**
   1.151 +	Test we fail burst times for EBurstRandom and EBurstTrueRandom.
   1.152 +	Even though it is random it should always fail within aRate allocations.
   1.153 +	Defined as a macro so that it is easier to determine which test is failing.
   1.154 +
   1.155 +@param aRate The limiting rate of each set of failures.
   1.156 +@param aBurst The number of allocations that should fail.
   1.157 +	
   1.158 +*/
   1.159 +#define __UHEAP_TEST_BURST_RANDOM(aRate, aBurst) \
   1.160 +	__UHEAP_SETBURSTFAIL(RHeap::EBurstRandom, aRate, aBurst); \
   1.161 +	TEST_BURST_RANDOM(aRate, aBurst)
   1.162 +
   1.163 +#define __RHEAP_TEST_BURST_RANDOM(aHeap, aRate, aBurst) \
   1.164 +	__RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstRandom, aRate, aBurst); \
   1.165 +	TEST_BURST_RANDOM(aRate, aBurst)
   1.166 +
   1.167 +#define __UHEAP_TEST_BURST_TRUERANDOM(aRate, aBurst) \
   1.168 +	__UHEAP_SETBURSTFAIL(RHeap::EBurstTrueRandom, aRate, aBurst); \
   1.169 +	TEST_BURST_RANDOM(aRate, aBurst)
   1.170 +
   1.171 +#define __RHEAP_TEST_BURST_TRUERANDOM(aHeap, aRate, aBurst) \
   1.172 +	__RHEAP_SETBURSTFAIL(aHeap, RHeap::EBurstTrueRandom, aRate, aBurst); \
   1.173 +	TEST_BURST_RANDOM(aRate, aBurst)
   1.174 +
   1.175 +
   1.176 +#define TEST_BURST_RANDOM(aRate, aBurst) \
   1.177 +	failed = 0; \
   1.178 +	for (i = 0; i < aRate * KHeapFailCycles; i++) \
   1.179 +		{ \
   1.180 +		p = new TInt; \
   1.181 +		if (p == NULL) \
   1.182 +			{/* we've started failing so check that we fail burst times*/ \
   1.183 +			failed++; \
   1.184 +			for (TInt j = 1; j < aBurst; j++) \
   1.185 +				{ \
   1.186 +				p = new TInt; \
   1.187 +				test_Equal(NULL, p); \
   1.188 +				} \
   1.189 +			} \
   1.190 +		delete p; \
   1.191 +		} \
   1.192 +	test_NotNull(failed);
   1.193 +
   1.194 +struct SBurstPanicParams
   1.195 +	{
   1.196 +	TInt iRate;
   1.197 +	TUint iBurst;
   1.198 +	};
   1.199 +
   1.200 +TInt TestBurstPanicThread(TAny* aParams)
   1.201 +	{
   1.202 +	SBurstPanicParams* burstParams = (SBurstPanicParams*) aParams;
   1.203 +	__UHEAP_SETBURSTFAIL(RHeap::EBurstDeterministic, burstParams->iRate, burstParams->iBurst); \
   1.204 +	return KErrNone;
   1.205 +	}
   1.206 +
   1.207 +#define __UHEAP_TEST_BURST_PANIC(aRate, aBurst) \
   1.208 +	{ \
   1.209 +	RThread thread; \
   1.210 +	TRequestStatus status; \
   1.211 +	SBurstPanicParams threadParams; \
   1.212 +	threadParams.iRate = aRate; \
   1.213 +	threadParams.iBurst = aBurst; \
   1.214 +	test_KErrNone(thread.Create(_L("TestBurstPanicThread"), TestBurstPanicThread, 0x1000, NULL, (TAny*)&threadParams)); \
   1.215 +	thread.Logon(status); \
   1.216 +	thread.Resume(); \
   1.217 +	User::WaitForRequest(status); \
   1.218 +	test_Equal(EExitPanic, thread.ExitType()); \
   1.219 +	test_Equal(ETHeapBadDebugFailParameter, status.Int()); \
   1.220 +	CLOSE_AND_WAIT(thread); \
   1.221 +	}
   1.222 +
   1.223 +GLDEF_C TInt E32Main(void)
   1.224 +    {
   1.225 +
   1.226 +	test.Title();
   1.227 +	test.Start(_L("Test the heap debug failure mechanisms"));
   1.228 +
   1.229 +	// Prepare for __UHEAP tests
   1.230 +	__UHEAP_RESET;
   1.231 +	__UHEAP_MARK;
   1.232 +
   1.233 +	// Prepare for __RHEAP tests
   1.234 +	TInt pageSize;
   1.235 +	test_KErrNone(HAL::Get(HAL::EMemoryPageSize, pageSize));
   1.236 +	
   1.237 +	RChunk heapChunk;
   1.238 +	test_KErrNone(heapChunk.CreateLocal(pageSize<<1, pageSize<<1));
   1.239 +	RHeap* rHeap = UserHeap::ChunkHeap(NULL, 0, pageSize);
   1.240 +	test_NotNull(rHeap);
   1.241 +	__RHEAP_RESET(rHeap);
   1.242 +	__RHEAP_MARK(rHeap);
   1.243 +
   1.244 +
   1.245 +	// Prepare for __KHEAP tests by:
   1.246 +	// Turning off lazy dll unloading
   1.247 +	RLoader l;
   1.248 +	test(l.Connect()==KErrNone);
   1.249 +	test(l.CancelLazyDllUnload()==KErrNone);
   1.250 +	l.Close();
   1.251 +
   1.252 +	// Loading the kernel heap test driver
   1.253 +	test.Next(_L("Load/open d_kheap test driver"));
   1.254 +	TInt r = User::LoadLogicalDevice(KHeapTestDriverName);
   1.255 +	test( r==KErrNone || r==KErrAlreadyExists);
   1.256 +	if( KErrNone != (r=KHeapDevice.Open()) )	
   1.257 +		{
   1.258 +		User::FreeLogicalDevice(KHeapTestDriverName);
   1.259 +		test.Printf(_L("Could not open LDD"));
   1.260 +		test(0);
   1.261 +		}
   1.262 +	__KHEAP_RESET;
   1.263 +	__KHEAP_MARK;
   1.264 +
   1.265 +//=============================================================================
   1.266 +	test.Next(_L("Test __UHEAP EFailNext"));
   1.267 +	TInt *p;
   1.268 +	TInt *q;
   1.269 +	p=new int;
   1.270 +	test(p!=NULL);
   1.271 +	delete p;
   1.272 +	__UHEAP_FAILNEXT(1);
   1.273 +	p=new int;
   1.274 +	test(p==NULL);
   1.275 +	p=new int;
   1.276 +	test(p!=NULL);
   1.277 +	delete p;
   1.278 +	__UHEAP_FAILNEXT(2);
   1.279 +	p=new int;
   1.280 +	q=new int;
   1.281 +	test(p!=NULL);
   1.282 +	test(q==NULL);
   1.283 +	delete p;
   1.284 +	__UHEAP_FAILNEXT(10);
   1.285 +	TUint i;
   1.286 +	for (i=0; i<9; i++)
   1.287 +		{
   1.288 +		p=new int;
   1.289 +		test(p!=NULL);
   1.290 +		delete p;
   1.291 +		}
   1.292 +	p=new int;
   1.293 +	test(p==NULL);
   1.294 +	for (i=0; i<30; i++)
   1.295 +		{
   1.296 +		p=new int;
   1.297 +		test(p!=NULL);
   1.298 +		delete p;
   1.299 +		}
   1.300 +
   1.301 +	// Test EFailNext with burst macro should default to burst of 1
   1.302 +	 __UHEAP_SETBURSTFAIL(RAllocator::EFailNext, 5, 5);
   1.303 +	for (i = 0; i < 4; i++)
   1.304 +		{
   1.305 +		p = new TInt;
   1.306 +		test_NotNull(p);
   1.307 +		delete p;
   1.308 +		}
   1.309 +	p = new TInt;
   1.310 +	test_Equal(NULL, p);
   1.311 +	p = new TInt;
   1.312 +	test_NotNull(p);
   1.313 +	delete p;
   1.314 +	
   1.315 +
   1.316 +//=============================================================================
   1.317 +	test.Next(_L("Test __UHEAP BurstFailNext"));
   1.318 +	__UHEAP_TEST_BURST_FAILNEXT(2, 1);
   1.319 +	__UHEAP_TEST_BURST_FAILNEXT(10, 12);
   1.320 +	__UHEAP_TEST_BURST_FAILNEXT(5, 50);
   1.321 +	__UHEAP_TEST_BURST_FAILNEXT(50, 5);
   1.322 +	
   1.323 +	// test using burst with non-burst macro should default to burst=1
   1.324 +	__UHEAP_SETFAIL(RHeap::EBurstFailNext, 5);
   1.325 +	for (i = 0; i < 4; i++)
   1.326 +		{
   1.327 +		p = new TInt;
   1.328 +		test_NotNull(p);
   1.329 +		delete p;
   1.330 +		}
   1.331 +	q = new TInt;
   1.332 +	test_Equal(NULL, q);
   1.333 +	q = new TInt;
   1.334 +	test_NotNull(q);
   1.335 +	delete q;
   1.336 +
   1.337 +
   1.338 +	test.Next(_L("Test __RHEAP BurstFailNext"));
   1.339 +	RHeap* origHeap = User::SwitchHeap(rHeap);
   1.340 +
   1.341 +	__RHEAP_TEST_BURST_FAILNEXT(rHeap, 2, 1);
   1.342 +	__RHEAP_TEST_BURST_FAILNEXT(rHeap, 10, 12);
   1.343 +	__RHEAP_TEST_BURST_FAILNEXT(rHeap, 5, 50);
   1.344 +	__RHEAP_TEST_BURST_FAILNEXT(rHeap, 50, 5);
   1.345 +
   1.346 +	User::SwitchHeap(origHeap);
   1.347 +		
   1.348 +	test.Next(_L("Test __KHEAP BurstFailNext"));
   1.349 +	__KHEAP_TEST_BURST_FAILNEXT(1, 1);
   1.350 +	__KHEAP_TEST_BURST_FAILNEXT(10, 12);
   1.351 +	__KHEAP_TEST_BURST_FAILNEXT(5, 50);
   1.352 +	__KHEAP_TEST_BURST_FAILNEXT(50, 5);
   1.353 +	__KHEAP_RESET;
   1.354 +	
   1.355 +		
   1.356 +//=============================================================================
   1.357 +	test.Next(_L("Test __UHEAP EDeterministic"));
   1.358 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 1);
   1.359 +	for (i=0; i<20; i++)
   1.360 +		{
   1.361 +		p=new int;
   1.362 +		test(p==NULL);
   1.363 +		}
   1.364 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 2);
   1.365 +	for (i=0; i<20; i++)
   1.366 +		{
   1.367 +		p=new int;
   1.368 +		q=new int;
   1.369 +		test(p!=NULL);
   1.370 +		test(q==NULL);
   1.371 +		delete p;
   1.372 +		}
   1.373 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 11);
   1.374 +	for (i=1; i<=100; i++)
   1.375 +		{
   1.376 +		p=new int;
   1.377 +		if (i%11==0)
   1.378 +			test(p==NULL);
   1.379 +		else
   1.380 +			test(p!=NULL);
   1.381 +		delete p;
   1.382 +		}
   1.383 +	// Test using burst macro for non-burst fail type
   1.384 +	// The burst value will be ignored.
   1.385 +	__UHEAP_SETBURSTFAIL(RHeap::EDeterministic, 2, 3);
   1.386 +	for (i=0; i<20; i++)
   1.387 +		{
   1.388 +		p=new int;
   1.389 +		q=new int;
   1.390 +		test(p!=NULL);
   1.391 +		test(q==NULL);
   1.392 +		delete p;
   1.393 +		}
   1.394 +
   1.395 +//=============================================================================
   1.396 +	test.Next(_L("Test __UHEAP EBurstDeterministic"));
   1.397 +	__UHEAP_TEST_BURST_DETERMINISTIC(1, 1);
   1.398 +	__UHEAP_TEST_BURST_DETERMINISTIC(2, 1);
   1.399 +	__UHEAP_TEST_BURST_DETERMINISTIC(11, 2);
   1.400 +	__UHEAP_TEST_BURST_DETERMINISTIC(2, 3);	// Test with burst > rate.
   1.401 +
   1.402 +	// Test setting EBurstDeterministic with non-burst MACRO
   1.403 +	// it should still work but default to a burst rate of 1
   1.404 +	__UHEAP_SETFAIL(RHeap::EBurstDeterministic, 2);
   1.405 +	for (i=0; i<20; i++)
   1.406 +		{
   1.407 +		p = new int;
   1.408 +		q = new int;
   1.409 +		test_NotNull(p);
   1.410 +		test_Equal(NULL, q);
   1.411 +		delete p;
   1.412 +		}
   1.413 +
   1.414 +	test.Next(_L("Test __RHEAP EBurstDeterministic"));
   1.415 +	origHeap = User::SwitchHeap(rHeap);
   1.416 +
   1.417 +	__UHEAP_TEST_BURST_DETERMINISTIC(1, 1);
   1.418 +	__UHEAP_TEST_BURST_DETERMINISTIC(2, 1);
   1.419 +	__UHEAP_TEST_BURST_DETERMINISTIC(11, 2);
   1.420 +	__UHEAP_TEST_BURST_DETERMINISTIC(2, 3);	// Test with burst > rate.
   1.421 +
   1.422 +	User::SwitchHeap(origHeap);
   1.423 +
   1.424 +
   1.425 +	test.Next(_L("Test __KHEAP EBurstDeterministic"));
   1.426 +	__KHEAP_TEST_BURST_DETERMINISTIC(1, 1);
   1.427 +	__KHEAP_TEST_BURST_DETERMINISTIC(2, 1);
   1.428 +	__KHEAP_TEST_BURST_DETERMINISTIC(11, 2);
   1.429 +	__KHEAP_TEST_BURST_DETERMINISTIC(2, 3);	// Test with burst > rate.
   1.430 +	__KHEAP_RESET;
   1.431 +
   1.432 +//=============================================================================
   1.433 +	test.Next(_L("Test __UHEAP ERandom"));
   1.434 +	__UHEAP_SETFAIL(RHeap::ERandom, 1);
   1.435 +	for (i=1; i<=100; i++)
   1.436 +		{
   1.437 +		p=new int;
   1.438 +		test(p==NULL);
   1.439 +		}
   1.440 +	__UHEAP_SETFAIL(RHeap::ERandom, 2);
   1.441 +	for (i=1; i<=100; i++)
   1.442 +		{
   1.443 +		p=new int;
   1.444 +		q=new int;
   1.445 +		test(p==NULL || q==NULL);
   1.446 +		delete p;
   1.447 +		delete q;
   1.448 +		}
   1.449 +	__UHEAP_SETFAIL(RHeap::ERandom, 10);
   1.450 +	TInt failed=0;
   1.451 +	for (i=0; i<10; i++)
   1.452 +		{
   1.453 +		p=new int;
   1.454 +		if (p==NULL) failed++;
   1.455 +		delete p;
   1.456 +		}
   1.457 +	test(failed);
   1.458 +	for (i=0; i<10; i++)
   1.459 +		{
   1.460 +		p=new int;
   1.461 +		if (p==NULL) failed++;
   1.462 +		delete p;
   1.463 +		}
   1.464 +	test(failed>=2);
   1.465 +
   1.466 +	// Test using the burst macro for ERandom.
   1.467 +	// Can't really check that it only fails once as being random
   1.468 +	// it may fail again immediately after a previous failure.
   1.469 +	__UHEAP_SETBURSTFAIL(RHeap::ERandom, 10, 5);
   1.470 +	TEST_BURST_RANDOM(10, 1);
   1.471 +
   1.472 +//=============================================================================
   1.473 +	test.Next(_L("Test __UHEAP EBurstRandom"));
   1.474 +	__UHEAP_TEST_BURST_RANDOM(10, 2);
   1.475 +	__UHEAP_TEST_BURST_RANDOM(15, 5);
   1.476 +	__UHEAP_TEST_BURST_RANDOM(10, 20);
   1.477 +
   1.478 +	// Test using EBurstRandom with non-burst macro.
   1.479 +	// Can't really check that it only fails once as being random
   1.480 +	// it may fail again immediately after a previous failure.
   1.481 +	__UHEAP_SETFAIL(RHeap::EBurstRandom, 10);
   1.482 +	__UHEAP_TEST_BURST_RANDOM(10, 1); 
   1.483 +
   1.484 +
   1.485 +	test.Next(_L("Test __RHEAP EBurstRandom"));
   1.486 +	origHeap = User::SwitchHeap(rHeap);
   1.487 +
   1.488 +	__RHEAP_TEST_BURST_RANDOM(rHeap, 10, 2);
   1.489 +	__RHEAP_TEST_BURST_RANDOM(rHeap, 15, 5);
   1.490 +	__RHEAP_TEST_BURST_RANDOM(rHeap, 10, 20);
   1.491 +
   1.492 +	User::SwitchHeap(origHeap);
   1.493 +	
   1.494 +	// No random modes for kernel heap
   1.495 +
   1.496 +//=============================================================================
   1.497 +	test.Next(_L("Test __UHEAP ETrueRandom"));
   1.498 +	__UHEAP_SETFAIL(RHeap::ETrueRandom, 10);
   1.499 +	failed=0;
   1.500 +	for (i=0; i<10; i++)
   1.501 +		{
   1.502 +		p=new int;
   1.503 +		if (p==NULL) failed++;
   1.504 +		delete p;
   1.505 +		}
   1.506 +	test(failed);
   1.507 +	for (i=0; i<10; i++)
   1.508 +		{
   1.509 +		p=new int;
   1.510 +		if (p==NULL) failed++;
   1.511 +		delete p;
   1.512 +		}
   1.513 +	test(failed>=2);
   1.514 +
   1.515 +	// Test using ETrueRandom with burst macro.
   1.516 +	// Can't really check that it only fails once as being random
   1.517 +	// it may fail again immediately after a previous failure.
   1.518 +	__UHEAP_SETBURSTFAIL(RHeap::ETrueRandom, 10, 2);
   1.519 +	TEST_BURST_RANDOM(10, 1);
   1.520 +
   1.521 +//=============================================================================
   1.522 +	test.Next(_L("Test __UHEAP EBurstTrueRandom"));
   1.523 +	__UHEAP_TEST_BURST_TRUERANDOM(10, 2);
   1.524 +	__UHEAP_TEST_BURST_TRUERANDOM(15, 5);
   1.525 +	__UHEAP_TEST_BURST_TRUERANDOM(10, 20);
   1.526 +
   1.527 +	// Test using EBurstRandom with non-burst macro.
   1.528 +	// Can't really check that it only fails once as being random
   1.529 +	// it may fail again immediately after a previous failure.
   1.530 +	__UHEAP_SETFAIL(RHeap::EBurstTrueRandom, 10);
   1.531 +	TEST_BURST_RANDOM(10, 1);
   1.532 +
   1.533 +	test.Next(_L("Test __RHEAP EBurstTrueRandom"));
   1.534 +	origHeap = User::SwitchHeap(rHeap);
   1.535 +
   1.536 +	__RHEAP_TEST_BURST_TRUERANDOM(rHeap, 10, 2);
   1.537 +	__RHEAP_TEST_BURST_TRUERANDOM(rHeap, 15, 5);
   1.538 +	__RHEAP_TEST_BURST_TRUERANDOM(rHeap, 10, 20);
   1.539 +
   1.540 +	User::SwitchHeap(origHeap);
   1.541 +
   1.542 +	// No random modes for kernel heap
   1.543 +
   1.544 +//=============================================================================
   1.545 +	test.Next(_L("Test __UHEAP ENone"));
   1.546 +	__UHEAP_SETFAIL(RHeap::ENone, 0);
   1.547 +	for (i=0; i<100; i++)
   1.548 +		{
   1.549 +		p=new int;
   1.550 +		test(p!=NULL);
   1.551 +		delete p;
   1.552 +		}
   1.553 +
   1.554 +	// Test using ENone with burst macro.
   1.555 +	__UHEAP_SETBURSTFAIL(RHeap::ENone, 0, 0);
   1.556 +	for (i=0; i<100; i++)
   1.557 +		{
   1.558 +		p = new TInt;
   1.559 +		test_NotNull(p);
   1.560 +		delete p;
   1.561 +		}
   1.562 +
   1.563 +//=============================================================================
   1.564 +	test.Next(_L("Test __UHEAP Reset"));
   1.565 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 1);
   1.566 +	for (i=0; i<10; i++)
   1.567 +		{
   1.568 +		p=new int;
   1.569 +		test(p==NULL);
   1.570 +		}
   1.571 +	__UHEAP_RESET;
   1.572 +	p=new int;
   1.573 +	test(p!=NULL);
   1.574 +	delete p;
   1.575 +
   1.576 +
   1.577 +	// Test using EReset with non-burst macro.
   1.578 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 1);
   1.579 +	for (i=0; i<10; i++)
   1.580 +		{
   1.581 +		p=new int;
   1.582 +		test(p==NULL);
   1.583 +		}
   1.584 +	__UHEAP_SETFAIL(RHeap::EReset, 1);
   1.585 +	p=new int;
   1.586 +	test(p!=NULL);
   1.587 +	delete p;
   1.588 +
   1.589 +	// Test using EReset with burst macro.
   1.590 +	__UHEAP_SETFAIL(RHeap::EDeterministic, 1);
   1.591 +	for (i=0; i<10; i++)
   1.592 +		{
   1.593 +		p=new int;
   1.594 +		test(p==NULL);
   1.595 +		}
   1.596 +	__UHEAP_SETBURSTFAIL(RHeap::EReset, 1, 1);
   1.597 +	p=new int;
   1.598 +	test(p!=NULL);
   1.599 +	delete p;
   1.600 +
   1.601 +//=============================================================================
   1.602 +	test.Next(_L("Test ETHeapBadDebugFailParameter panics"));
   1.603 +	__UHEAP_TEST_BURST_PANIC(50, KMaxTUint16 + 1);
   1.604 +	__UHEAP_TEST_BURST_PANIC(KMaxTUint16 + 1, 2);
   1.605 +	__UHEAP_TEST_BURST_PANIC(-50, 3);
   1.606 +
   1.607 +	// Test maximum aRate and aBurst values don't panic.
   1.608 +	__UHEAP_TEST_BURST_FAILNEXT(2, KMaxTUint16);	// Use failnext as quicker
   1.609 +	__UHEAP_TEST_BURST_FAILNEXT(KMaxTUint16, 2);
   1.610 +
   1.611 +//=============================================================================
   1.612 +	test.Next(_L("Test __UHEAP User::ReAlloc without cell moving"));
   1.613 +	TAny* a = User::Alloc(256);
   1.614 +	test(a!=NULL);
   1.615 +	__UHEAP_FAILNEXT(1);
   1.616 +	TAny* a2 = User::ReAlloc(a,192);
   1.617 +	test(a2==a);
   1.618 +	a2 = User::ReAlloc(a,128);
   1.619 +	test(a2==a);
   1.620 +	a2 = User::ReAlloc(a,256);
   1.621 +	test(a2==NULL);
   1.622 +	a2 = User::ReAlloc(a,256);
   1.623 +	test(a2==a);
   1.624 +	User::Free(a);
   1.625 +
   1.626 +//=============================================================================
   1.627 +	// Clean up
   1.628 +	__RHEAP_MARKEND(rHeap);
   1.629 +	rHeap->Close();
   1.630 +
   1.631 +	__KHEAP_MARKEND;
   1.632 +	// Ensure all kernel heap debug failures are not active for future tests etc.
   1.633 +	__KHEAP_RESET;	
   1.634 +	KHeapDevice.Close();
   1.635 +	User::FreeLogicalDevice(KHeapTestDriverName);	
   1.636 +
   1.637 +	__UHEAP_MARKEND;
   1.638 +	test.End();
   1.639 +	return(KErrNone);
   1.640 +    }
   1.641 +
   1.642 +#else
   1.643 +GLDEF_C TInt E32Main()
   1.644 +//
   1.645 +// __KHEAP_SETFAIL etc. not available in release mode, so don't test
   1.646 +//
   1.647 +	{	 
   1.648 +
   1.649 +	test.Title();
   1.650 +	test.Start(_L("No tests in release mode"));
   1.651 +	test.End();
   1.652 +	return(KErrNone);
   1.653 +	}
   1.654 +#endif
   1.655 +