os/kernelhwsrv/kerneltest/e32test/demandpaging/t_chunkheapcreate.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/demandpaging/t_chunkheapcreate.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,379 @@
     1.4 +// Copyright (c) 2008-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\demandpaging\t_chunkheapcreate.cpp
    1.18 +// 
    1.19 +//
    1.20 +
    1.21 +//
    1.22 +#define __E32TEST_EXTENSION__
    1.23 +#include <e32test.h>
    1.24 +#include <dptest.h>
    1.25 +#include <e32hal.h>
    1.26 +#include <u32exec.h>
    1.27 +#include <e32svr.h>
    1.28 +#include <e32panic.h>
    1.29 +#include "u32std.h"
    1.30 +
    1.31 +#include "t_dpcmn.h"
    1.32 +
    1.33 +enum
    1.34 +	{
    1.35 +	ETestLocal, 
    1.36 +	ETestGlobal,
    1.37 +	ETestExisting,
    1.38 +	};
    1.39 +
    1.40 +static TPtrC gGlobalDes = _L("GlobalHeap");
    1.41 +
    1.42 +void UseOrCreateChunk(TChunkHeapCreateInfo aCreateInfo, TInt aTestType)
    1.43 +	{
    1.44 +	switch(aTestType)
    1.45 +		{
    1.46 +		case ETestLocal:
    1.47 +			aCreateInfo.SetCreateChunk(NULL);
    1.48 +			break;
    1.49 +
    1.50 +		case ETestGlobal:
    1.51 +			aCreateInfo.SetCreateChunk(&gGlobalDes);
    1.52 +			break;
    1.53 +			
    1.54 +		case ETestExisting:
    1.55 +			aCreateInfo.SetUseChunk(gChunk);
    1.56 +			break;
    1.57 +		}
    1.58 +	}
    1.59 +
    1.60 +/**
    1.61 +Attempt to create a global or local UserHeap with the given attributes
    1.62 +
    1.63 +@return ETrue The chunk is paged or unpaged as expected.
    1.64 +*/
    1.65 +TInt UserHeapAtt(TBool aPaged, TChunkHeapCreateInfo& aCreateInfo)
    1.66 +	{
    1.67 +	UpdatePaged(aPaged);
    1.68 +	
    1.69 +	RHeap* heap = UserHeap::ChunkHeap(aCreateInfo);
    1.70 +
    1.71 +	test_NotNull(heap);
    1.72 +	RChunk chunk;
    1.73 +	chunk.SetHandle(heap->ChunkHandle());
    1.74 +	TBool paged = chunk.IsPaged();
    1.75 +	chunk.Close();
    1.76 +	return (aPaged == paged);
    1.77 +	}
    1.78 +
    1.79 +TInt PanicUserHeap(TAny*)
    1.80 +	{
    1.81 +	TChunkHeapCreateInfo createInfo(0, gPageSize);
    1.82 +	createInfo.SetCreateChunk(NULL);
    1.83 +	createInfo.SetMode((TUint)~UserHeap::EChunkHeapMask);
    1.84 +	test(UserHeapAtt(gProcessPaged, createInfo));
    1.85 +	return KErrGeneral;	// Shouldn't reach here.
    1.86 +	}
    1.87 +
    1.88 +TInt PanicChunkHeapCreate(TAny* aCreateInfo)
    1.89 +	{
    1.90 +	TChunkHeapCreateInfo createInfo((*(TChunkHeapCreateInfo*) aCreateInfo));
    1.91 +	UserHeap::ChunkHeap(createInfo);
    1.92 +	return KErrGeneral; // Should never reahc here
    1.93 +	}
    1.94 +
    1.95 +//
    1.96 +// TestChunkHeapCreate
    1.97 +//
    1.98 +//----------------------------------------------------------------------------------------------
    1.99 +//! @SYMTestCaseID			KBASE-T_CHUNKHEAPCREATE-xxxx
   1.100 +//! @SYMTestType			UT
   1.101 +//! @SYMPREQ				PREQ1954
   1.102 +//! @SYMTestCaseDesc		Testing TChunkHeapCreateInfo
   1.103 +//!							Verify the chunk heap creation implementation
   1.104 +//! @SYMTestActions		
   1.105 +//! 1.	Create a local chunk by calling SetCreateChunk(NULL). 
   1.106 +//!		Following this check the paging status of the chunk heap. 
   1.107 +//! 2.	Create a local chunk by calling SetCreateChunk(NULL). Set the chunk heap to EPaged. 
   1.108 +//!		Following this check the paging status of the chunk heap. 
   1.109 +//! 3.	Create a local chunk by calling SetCreateChunk(NULL). Set the chunk heap to EUnPaged. 
   1.110 +//!		Following this check the paging status of the chunk heap. 
   1.111 +//! 4.	Create a heap within a local chunk and set the minimum length of the heap to be greater 
   1.112 +//!		than the maximum length of the heap
   1.113 +//! 5.	Create a heap within a local chunk and set the minimum length of the heap to -1
   1.114 +//! 6.	Create a heap within a local chunk and set the maximum length of the heap to 
   1.115 +//!		less than KMinHeapSize
   1.116 +//! 7.	Call TChunkHeapCreateInfo::SetUseChunk()  on a created local chunk calling SetCreateChunk(NULL) 
   1.117 +//!		before hand. Following this call UserHeap::ChunkHeap() and compare the heap and the chunk handles
   1.118 +//! 8.	Call TChunkHeapCreateInfo::SetUseChunk()  on a created local chunk calling SetCreateChunk(NULL) 
   1.119 +//!		after. Following this call UserHeap::ChunkHeap() and compare the heap and the chunk handles
   1.120 +//! 9.	Call TChunkHeapCreateInfo::SetSingleThread() on a created local chunk specifying EFalse. 
   1.121 +//!		Following this call UserHeap::ChunkHeap();
   1.122 +//! 10.	Call TChunkHeapCreateInfo::SetSingleThread() on a created local chunk specifying ETrue. 
   1.123 +//!		Following this call UserHeap::ChunkHeap();
   1.124 +//! 11.	Call TChunkHeapCreateInfo::SetAlignment() on a created local chunk specifying aAlign to 
   1.125 +//!		be a valid value. Following this call UserHeap::ChunkHeap();
   1.126 +//! 12.	Call TChunkHeapCreateInfo::SetAlignment() on a created local chunk specifying aAlign to be 
   1.127 +//!		an invalid value (-1). Following this call UserHeap::ChunkHeap();
   1.128 +//! 13.	Call TChunkHeapCreateInfo::SetGrowBy() on a created local chunk specifying aGrowBy to be 
   1.129 +//!		a valid value. Following this call UserHeap::ChunkHeap(). Following this create and array of 
   1.130 +//!		integers on the heap and check the size of the chunk. 
   1.131 +//!	14.	Call TChunkHeapCreateInfo::SetGrowBy() on a created local chunk specifying aGrowBy to
   1.132 +//!		be greater than the maximum size of the chunk. Following this call UserHeap::ChunkHeap();
   1.133 +//! 15.	Call TChunkHeapCreateInfo::SetOffset() on a created local chunk specifying aOffset to 
   1.134 +//!		be a valid value. Following this call UserHeap::ChunkHeap();
   1.135 +//! 16.	Call TChunkHeapCreateInfo:: SetMode () on a created local chunk specifying aMode to 
   1.136 +//!		be an invalid value. Following this call UserHeap::ChunkHeap();
   1.137 +//!
   1.138 +//! @SYMTestExpectedResults All tests should pass.
   1.139 +//! @SYMTestPriority        High
   1.140 +//! @SYMTestStatus          Implemented
   1.141 +//----------------------------------------------------------------------------------------------
   1.142 +void TestChunkHeapCreate(TInt aTestType)
   1.143 +	{
   1.144 +	
   1.145 +
   1.146 +	test.Start(_L("Test UserHeap::ChunkHeap() data paging attributes"));
   1.147 +		{
   1.148 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.149 +		
   1.150 +		UseOrCreateChunk(createInfo, aTestType);
   1.151 +
   1.152 +		test(UserHeapAtt(gProcessPaged, createInfo));
   1.153 +
   1.154 +		createInfo.SetPaging(TChunkHeapCreateInfo::EPaged);
   1.155 +		test(UserHeapAtt(ETrue, createInfo));
   1.156 +		
   1.157 +		createInfo.SetPaging(TChunkHeapCreateInfo::EUnpaged);
   1.158 +		test(UserHeapAtt(EFalse, createInfo));
   1.159 +		}
   1.160 +
   1.161 +	test.Next(_L("Test maxHeapSize < minHeapSize"));
   1.162 +		{
   1.163 +		TChunkHeapCreateInfo createInfo(KMinHeapSize + gPageSize, KMinHeapSize);
   1.164 +		UseOrCreateChunk(createInfo, aTestType);
   1.165 +		RThread thread;
   1.166 +		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
   1.167 +															KMinHeapSize,  (TAny*) &createInfo));
   1.168 +
   1.169 +		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapCreateMaxLessThanMin));
   1.170 +		}
   1.171 +
   1.172 +	test.Next(_L("Test invalid minHeapSize"));
   1.173 +		{
   1.174 +		TChunkHeapCreateInfo createInfo(-1, KMinHeapSize);
   1.175 +		UseOrCreateChunk(createInfo, aTestType);
   1.176 +		RThread thread;
   1.177 +		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
   1.178 +															KMinHeapSize,  (TAny*) &createInfo));
   1.179 +
   1.180 +		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapMinLengthNegative));
   1.181 +		}
   1.182 +
   1.183 +	test.Next(_L("Test invalid maxHeapSize"));
   1.184 +		{
   1.185 +		RHeap* heap;
   1.186 +		TChunkHeapCreateInfo createInfo(0, 0);
   1.187 +		UseOrCreateChunk(createInfo, aTestType);
   1.188 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.189 +		if (heap == NULL)
   1.190 +			{
   1.191 +			test.Printf(_L("RHeap not created"));
   1.192 +			}
   1.193 +		TUint maxLength = heap->MaxLength();
   1.194 +		test.Printf(_L("heap max length = 0x%x\n"), maxLength);
   1.195 +
   1.196 +		// Need to round up to page size as RHeap maxLength is rounded 
   1.197 +		// up to the nearest page size
   1.198 +		TUint expectedMaxSize = _ALIGN_UP(KMinHeapSize, gPageSize); 
   1.199 +		test_Equal(expectedMaxSize, maxLength);
   1.200 +		heap->Close();
   1.201 +		}
   1.202 +
   1.203 +	test.Next(_L("Test SetUseChunk - calling SetCreate before"));
   1.204 +		{
   1.205 +		RHeap* heap;
   1.206 +		RChunk chunky;
   1.207 +		
   1.208 +		TChunkCreateInfo chunkCreateInfo;
   1.209 +		chunkCreateInfo.SetNormal(gPageSize, gPageSize);
   1.210 +		test_KErrNone(chunky.Create(chunkCreateInfo));
   1.211 +
   1.212 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.213 +
   1.214 +		createInfo.SetCreateChunk(NULL);
   1.215 +		createInfo.SetUseChunk(chunky);
   1.216 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.217 +		if (heap == NULL)
   1.218 +			{
   1.219 +			test.Printf(_L("RHeap not created\n"));
   1.220 +			}
   1.221 +		test.Printf(_L("chunkHandle = %d heapHandle = %d\n"),chunky.Handle(), heap->ChunkHandle());
   1.222 +		test_Equal(chunky.Handle(), heap->ChunkHandle());
   1.223 +		heap->Close();
   1.224 +		}
   1.225 +
   1.226 +	test.Next(_L("Test SetUseChunk - calling SetCreate after"));
   1.227 +		{
   1.228 +		RHeap* heap;
   1.229 +		RChunk chunky;
   1.230 +		
   1.231 +		TChunkCreateInfo chunkCreateInfo;
   1.232 +		chunkCreateInfo.SetNormal(gPageSize, gPageSize);
   1.233 +		test_KErrNone(chunky.Create(chunkCreateInfo));
   1.234 +
   1.235 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.236 +		createInfo.SetUseChunk(chunky);
   1.237 +		createInfo.SetCreateChunk(NULL);
   1.238 +		
   1.239 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.240 +		if (heap == NULL)
   1.241 +			{
   1.242 +			test.Printf(_L("RHeap not created\n"));
   1.243 +			}
   1.244 +		test.Printf(_L("chunkHandle = %d heapHandle = %d\n"),chunky.Handle(), heap->ChunkHandle());
   1.245 +		TBool isSame = EFalse;
   1.246 +		if (chunky.Handle() == heap->ChunkHandle())
   1.247 +			isSame = ETrue;
   1.248 +		test_Equal(EFalse, isSame);
   1.249 +		heap->Close();
   1.250 +		}
   1.251 +
   1.252 +	test.Next(_L("Test SetSingleThread - ETrue"));
   1.253 +		{
   1.254 +		RHeap* heap;
   1.255 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.256 +		UseOrCreateChunk(createInfo, aTestType);
   1.257 +		createInfo.SetSingleThread(ETrue);
   1.258 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.259 +		test_NotNull(heap);	
   1.260 +		heap->Close();
   1.261 +		}
   1.262 +
   1.263 +	test.Next(_L("Test SetSingleThread - EFalse"));
   1.264 +		{
   1.265 +		RHeap* heap;
   1.266 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.267 +		UseOrCreateChunk(createInfo, aTestType);
   1.268 +		createInfo.SetSingleThread(EFalse);
   1.269 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.270 +		test_NotNull(heap);
   1.271 +		heap->Close();
   1.272 +		}
   1.273 +
   1.274 +	test.Next(_L("Test SetAlignment 0"));
   1.275 +		{
   1.276 +		RHeap* heap;
   1.277 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.278 +		UseOrCreateChunk(createInfo, aTestType);
   1.279 +		createInfo.SetAlignment(0);
   1.280 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.281 +		test_NotNull(heap);
   1.282 +		heap->Close();
   1.283 +		}
   1.284 +
   1.285 +	
   1.286 +	test.Next(_L("Test SetAlignment -1"));
   1.287 +		{
   1.288 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.289 +		UseOrCreateChunk(createInfo, aTestType);
   1.290 +		createInfo.SetAlignment(-1);
   1.291 +
   1.292 +		RThread thread;
   1.293 +		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
   1.294 +															KMinHeapSize,  (TAny*) &createInfo));
   1.295 +
   1.296 +		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapNewBadAlignment));
   1.297 +		}
   1.298 +
   1.299 +
   1.300 +	test.Next(_L("Test SetGrowby"));
   1.301 +		{
   1.302 +		RHeap* heap;
   1.303 +		TChunkHeapCreateInfo createInfo(0, gPageSize * 10);
   1.304 +		UseOrCreateChunk(createInfo, aTestType);
   1.305 +		createInfo.SetGrowBy(gPageSize);
   1.306 +		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
   1.307 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.308 +		test_NotNull(heap);
   1.309 +		RChunk chunk;
   1.310 +		chunk.SetHandle(heap->ChunkHandle());
   1.311 +		TInt* numBuf = new TInt[gPageSize];
   1.312 +		test_NotNull(numBuf);
   1.313 +		test.Printf(_L("chunkSize = %d\n"), chunk.Size());
   1.314 +		test(chunk.Size() > KMinHeapGrowBy);
   1.315 +		delete numBuf;
   1.316 +		heap->Close();
   1.317 +		}
   1.318 +
   1.319 +	test.Next(_L("Test SetGrowby growBy > maxSize"));
   1.320 +		{
   1.321 +		RHeap* heap;
   1.322 +		TChunkHeapCreateInfo createInfo(0, gPageSize * 5);
   1.323 +		UseOrCreateChunk(createInfo, aTestType);
   1.324 +		createInfo.SetGrowBy(gPageSize * 6);
   1.325 +		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
   1.326 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.327 +		test_NotNull(heap);
   1.328 +		RChunk chunk;
   1.329 +		chunk.SetHandle(heap->ChunkHandle());
   1.330 +		TInt* numBuf = new TInt[gPageSize];
   1.331 +		test_Equal(NULL, numBuf);
   1.332 +
   1.333 +		delete numBuf;
   1.334 +		heap->Close();
   1.335 +		}
   1.336 +
   1.337 +
   1.338 +	test.Next(_L("Test SetOffset "));
   1.339 +		{
   1.340 +		RHeap* heap;
   1.341 +		TChunkHeapCreateInfo createInfo(0, gPageSize * 10);
   1.342 +		UseOrCreateChunk(createInfo, aTestType);
   1.343 +		createInfo.SetOffset(8);
   1.344 +		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
   1.345 +		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
   1.346 +		TInt heapAddr = (TInt)heap;
   1.347 +		RChunk chunk;
   1.348 +		chunk.SetHandle(heap->ChunkHandle());
   1.349 +		test_Equal((TInt)chunk.Base() + 8, heapAddr);
   1.350 +		test_NotNull(heap);
   1.351 +		heap->Close();
   1.352 +		}
   1.353 +
   1.354 +	test.Next(_L("Test UserHeap::ChunkHeap() SetMode() invalid"));
   1.355 +		{
   1.356 +		TChunkHeapCreateInfo createInfo(0, gPageSize);
   1.357 +		UseOrCreateChunk(createInfo, aTestType);
   1.358 +		
   1.359 +		// Test creating a UserHeap with invalid attributes panics.
   1.360 +		RThread thread;
   1.361 +		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicUserHeap, KDefaultStackSize, 
   1.362 +									KMinHeapSize, KMinHeapSize, NULL));
   1.363 +		test_KErrNone(TestThreadExit(thread, EExitPanic, EHeapCreateInvalidMode));
   1.364 +		}
   1.365 +
   1.366 +	test.End();
   1.367 +	}
   1.368 +
   1.369 +TInt TestingTChunkHeapCreate()
   1.370 +	{
   1.371 +	test.Start(_L("Test TChunkHeapCreateInfo - heap within local chunk"));
   1.372 +	TestChunkHeapCreate(ETestLocal);
   1.373 +
   1.374 +	test.Next(_L("Test TChunkHeapCreateInfo - heap within global chunk"));
   1.375 +	TestChunkHeapCreate(ETestGlobal);
   1.376 +
   1.377 +	test.Next(_L("Test TChunkHeapCreateInfo - heap within existing chunk"));
   1.378 +	TestChunkHeapCreate(ETestExisting);
   1.379 +
   1.380 +	test.End();	
   1.381 +	return 0;
   1.382 +	}