os/kernelhwsrv/kerneltest/e32test/demandpaging/t_chunkheapcreate.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) 2008-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\demandpaging\t_chunkheapcreate.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
//
sl@0
    19
#define __E32TEST_EXTENSION__
sl@0
    20
#include <e32test.h>
sl@0
    21
#include <dptest.h>
sl@0
    22
#include <e32hal.h>
sl@0
    23
#include <u32exec.h>
sl@0
    24
#include <e32svr.h>
sl@0
    25
#include <e32panic.h>
sl@0
    26
#include "u32std.h"
sl@0
    27
sl@0
    28
#include "t_dpcmn.h"
sl@0
    29
sl@0
    30
enum
sl@0
    31
	{
sl@0
    32
	ETestLocal, 
sl@0
    33
	ETestGlobal,
sl@0
    34
	ETestExisting,
sl@0
    35
	};
sl@0
    36
sl@0
    37
static TPtrC gGlobalDes = _L("GlobalHeap");
sl@0
    38
sl@0
    39
void UseOrCreateChunk(TChunkHeapCreateInfo aCreateInfo, TInt aTestType)
sl@0
    40
	{
sl@0
    41
	switch(aTestType)
sl@0
    42
		{
sl@0
    43
		case ETestLocal:
sl@0
    44
			aCreateInfo.SetCreateChunk(NULL);
sl@0
    45
			break;
sl@0
    46
sl@0
    47
		case ETestGlobal:
sl@0
    48
			aCreateInfo.SetCreateChunk(&gGlobalDes);
sl@0
    49
			break;
sl@0
    50
			
sl@0
    51
		case ETestExisting:
sl@0
    52
			aCreateInfo.SetUseChunk(gChunk);
sl@0
    53
			break;
sl@0
    54
		}
sl@0
    55
	}
sl@0
    56
sl@0
    57
/**
sl@0
    58
Attempt to create a global or local UserHeap with the given attributes
sl@0
    59
sl@0
    60
@return ETrue The chunk is paged or unpaged as expected.
sl@0
    61
*/
sl@0
    62
TInt UserHeapAtt(TBool aPaged, TChunkHeapCreateInfo& aCreateInfo)
sl@0
    63
	{
sl@0
    64
	UpdatePaged(aPaged);
sl@0
    65
	
sl@0
    66
	RHeap* heap = UserHeap::ChunkHeap(aCreateInfo);
sl@0
    67
sl@0
    68
	test_NotNull(heap);
sl@0
    69
	RChunk chunk;
sl@0
    70
	chunk.SetHandle(heap->ChunkHandle());
sl@0
    71
	TBool paged = chunk.IsPaged();
sl@0
    72
	chunk.Close();
sl@0
    73
	return (aPaged == paged);
sl@0
    74
	}
sl@0
    75
sl@0
    76
TInt PanicUserHeap(TAny*)
sl@0
    77
	{
sl@0
    78
	TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
    79
	createInfo.SetCreateChunk(NULL);
sl@0
    80
	createInfo.SetMode((TUint)~UserHeap::EChunkHeapMask);
sl@0
    81
	test(UserHeapAtt(gProcessPaged, createInfo));
sl@0
    82
	return KErrGeneral;	// Shouldn't reach here.
sl@0
    83
	}
sl@0
    84
sl@0
    85
TInt PanicChunkHeapCreate(TAny* aCreateInfo)
sl@0
    86
	{
sl@0
    87
	TChunkHeapCreateInfo createInfo((*(TChunkHeapCreateInfo*) aCreateInfo));
sl@0
    88
	UserHeap::ChunkHeap(createInfo);
sl@0
    89
	return KErrGeneral; // Should never reahc here
sl@0
    90
	}
sl@0
    91
sl@0
    92
//
sl@0
    93
// TestChunkHeapCreate
sl@0
    94
//
sl@0
    95
//----------------------------------------------------------------------------------------------
sl@0
    96
//! @SYMTestCaseID			KBASE-T_CHUNKHEAPCREATE-xxxx
sl@0
    97
//! @SYMTestType			UT
sl@0
    98
//! @SYMPREQ				PREQ1954
sl@0
    99
//! @SYMTestCaseDesc		Testing TChunkHeapCreateInfo
sl@0
   100
//!							Verify the chunk heap creation implementation
sl@0
   101
//! @SYMTestActions		
sl@0
   102
//! 1.	Create a local chunk by calling SetCreateChunk(NULL). 
sl@0
   103
//!		Following this check the paging status of the chunk heap. 
sl@0
   104
//! 2.	Create a local chunk by calling SetCreateChunk(NULL). Set the chunk heap to EPaged. 
sl@0
   105
//!		Following this check the paging status of the chunk heap. 
sl@0
   106
//! 3.	Create a local chunk by calling SetCreateChunk(NULL). Set the chunk heap to EUnPaged. 
sl@0
   107
//!		Following this check the paging status of the chunk heap. 
sl@0
   108
//! 4.	Create a heap within a local chunk and set the minimum length of the heap to be greater 
sl@0
   109
//!		than the maximum length of the heap
sl@0
   110
//! 5.	Create a heap within a local chunk and set the minimum length of the heap to -1
sl@0
   111
//! 6.	Create a heap within a local chunk and set the maximum length of the heap to 
sl@0
   112
//!		less than KMinHeapSize
sl@0
   113
//! 7.	Call TChunkHeapCreateInfo::SetUseChunk()  on a created local chunk calling SetCreateChunk(NULL) 
sl@0
   114
//!		before hand. Following this call UserHeap::ChunkHeap() and compare the heap and the chunk handles
sl@0
   115
//! 8.	Call TChunkHeapCreateInfo::SetUseChunk()  on a created local chunk calling SetCreateChunk(NULL) 
sl@0
   116
//!		after. Following this call UserHeap::ChunkHeap() and compare the heap and the chunk handles
sl@0
   117
//! 9.	Call TChunkHeapCreateInfo::SetSingleThread() on a created local chunk specifying EFalse. 
sl@0
   118
//!		Following this call UserHeap::ChunkHeap();
sl@0
   119
//! 10.	Call TChunkHeapCreateInfo::SetSingleThread() on a created local chunk specifying ETrue. 
sl@0
   120
//!		Following this call UserHeap::ChunkHeap();
sl@0
   121
//! 11.	Call TChunkHeapCreateInfo::SetAlignment() on a created local chunk specifying aAlign to 
sl@0
   122
//!		be a valid value. Following this call UserHeap::ChunkHeap();
sl@0
   123
//! 12.	Call TChunkHeapCreateInfo::SetAlignment() on a created local chunk specifying aAlign to be 
sl@0
   124
//!		an invalid value (-1). Following this call UserHeap::ChunkHeap();
sl@0
   125
//! 13.	Call TChunkHeapCreateInfo::SetGrowBy() on a created local chunk specifying aGrowBy to be 
sl@0
   126
//!		a valid value. Following this call UserHeap::ChunkHeap(). Following this create and array of 
sl@0
   127
//!		integers on the heap and check the size of the chunk. 
sl@0
   128
//!	14.	Call TChunkHeapCreateInfo::SetGrowBy() on a created local chunk specifying aGrowBy to
sl@0
   129
//!		be greater than the maximum size of the chunk. Following this call UserHeap::ChunkHeap();
sl@0
   130
//! 15.	Call TChunkHeapCreateInfo::SetOffset() on a created local chunk specifying aOffset to 
sl@0
   131
//!		be a valid value. Following this call UserHeap::ChunkHeap();
sl@0
   132
//! 16.	Call TChunkHeapCreateInfo:: SetMode () on a created local chunk specifying aMode to 
sl@0
   133
//!		be an invalid value. Following this call UserHeap::ChunkHeap();
sl@0
   134
//!
sl@0
   135
//! @SYMTestExpectedResults All tests should pass.
sl@0
   136
//! @SYMTestPriority        High
sl@0
   137
//! @SYMTestStatus          Implemented
sl@0
   138
//----------------------------------------------------------------------------------------------
sl@0
   139
void TestChunkHeapCreate(TInt aTestType)
sl@0
   140
	{
sl@0
   141
	
sl@0
   142
sl@0
   143
	test.Start(_L("Test UserHeap::ChunkHeap() data paging attributes"));
sl@0
   144
		{
sl@0
   145
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   146
		
sl@0
   147
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   148
sl@0
   149
		test(UserHeapAtt(gProcessPaged, createInfo));
sl@0
   150
sl@0
   151
		createInfo.SetPaging(TChunkHeapCreateInfo::EPaged);
sl@0
   152
		test(UserHeapAtt(ETrue, createInfo));
sl@0
   153
		
sl@0
   154
		createInfo.SetPaging(TChunkHeapCreateInfo::EUnpaged);
sl@0
   155
		test(UserHeapAtt(EFalse, createInfo));
sl@0
   156
		}
sl@0
   157
sl@0
   158
	test.Next(_L("Test maxHeapSize < minHeapSize"));
sl@0
   159
		{
sl@0
   160
		TChunkHeapCreateInfo createInfo(KMinHeapSize + gPageSize, KMinHeapSize);
sl@0
   161
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   162
		RThread thread;
sl@0
   163
		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   164
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   165
sl@0
   166
		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapCreateMaxLessThanMin));
sl@0
   167
		}
sl@0
   168
sl@0
   169
	test.Next(_L("Test invalid minHeapSize"));
sl@0
   170
		{
sl@0
   171
		TChunkHeapCreateInfo createInfo(-1, KMinHeapSize);
sl@0
   172
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   173
		RThread thread;
sl@0
   174
		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   175
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   176
sl@0
   177
		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapMinLengthNegative));
sl@0
   178
		}
sl@0
   179
sl@0
   180
	test.Next(_L("Test invalid maxHeapSize"));
sl@0
   181
		{
sl@0
   182
		RHeap* heap;
sl@0
   183
		TChunkHeapCreateInfo createInfo(0, 0);
sl@0
   184
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   185
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   186
		if (heap == NULL)
sl@0
   187
			{
sl@0
   188
			test.Printf(_L("RHeap not created"));
sl@0
   189
			}
sl@0
   190
		TUint maxLength = heap->MaxLength();
sl@0
   191
		test.Printf(_L("heap max length = 0x%x\n"), maxLength);
sl@0
   192
sl@0
   193
		// Need to round up to page size as RHeap maxLength is rounded 
sl@0
   194
		// up to the nearest page size
sl@0
   195
		TUint expectedMaxSize = _ALIGN_UP(KMinHeapSize, gPageSize); 
sl@0
   196
		test_Equal(expectedMaxSize, maxLength);
sl@0
   197
		heap->Close();
sl@0
   198
		}
sl@0
   199
sl@0
   200
	test.Next(_L("Test SetUseChunk - calling SetCreate before"));
sl@0
   201
		{
sl@0
   202
		RHeap* heap;
sl@0
   203
		RChunk chunky;
sl@0
   204
		
sl@0
   205
		TChunkCreateInfo chunkCreateInfo;
sl@0
   206
		chunkCreateInfo.SetNormal(gPageSize, gPageSize);
sl@0
   207
		test_KErrNone(chunky.Create(chunkCreateInfo));
sl@0
   208
sl@0
   209
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   210
sl@0
   211
		createInfo.SetCreateChunk(NULL);
sl@0
   212
		createInfo.SetUseChunk(chunky);
sl@0
   213
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   214
		if (heap == NULL)
sl@0
   215
			{
sl@0
   216
			test.Printf(_L("RHeap not created\n"));
sl@0
   217
			}
sl@0
   218
		test.Printf(_L("chunkHandle = %d heapHandle = %d\n"),chunky.Handle(), heap->ChunkHandle());
sl@0
   219
		test_Equal(chunky.Handle(), heap->ChunkHandle());
sl@0
   220
		heap->Close();
sl@0
   221
		}
sl@0
   222
sl@0
   223
	test.Next(_L("Test SetUseChunk - calling SetCreate after"));
sl@0
   224
		{
sl@0
   225
		RHeap* heap;
sl@0
   226
		RChunk chunky;
sl@0
   227
		
sl@0
   228
		TChunkCreateInfo chunkCreateInfo;
sl@0
   229
		chunkCreateInfo.SetNormal(gPageSize, gPageSize);
sl@0
   230
		test_KErrNone(chunky.Create(chunkCreateInfo));
sl@0
   231
sl@0
   232
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   233
		createInfo.SetUseChunk(chunky);
sl@0
   234
		createInfo.SetCreateChunk(NULL);
sl@0
   235
		
sl@0
   236
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   237
		if (heap == NULL)
sl@0
   238
			{
sl@0
   239
			test.Printf(_L("RHeap not created\n"));
sl@0
   240
			}
sl@0
   241
		test.Printf(_L("chunkHandle = %d heapHandle = %d\n"),chunky.Handle(), heap->ChunkHandle());
sl@0
   242
		TBool isSame = EFalse;
sl@0
   243
		if (chunky.Handle() == heap->ChunkHandle())
sl@0
   244
			isSame = ETrue;
sl@0
   245
		test_Equal(EFalse, isSame);
sl@0
   246
		heap->Close();
sl@0
   247
		}
sl@0
   248
sl@0
   249
	test.Next(_L("Test SetSingleThread - ETrue"));
sl@0
   250
		{
sl@0
   251
		RHeap* heap;
sl@0
   252
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   253
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   254
		createInfo.SetSingleThread(ETrue);
sl@0
   255
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   256
		test_NotNull(heap);	
sl@0
   257
		heap->Close();
sl@0
   258
		}
sl@0
   259
sl@0
   260
	test.Next(_L("Test SetSingleThread - EFalse"));
sl@0
   261
		{
sl@0
   262
		RHeap* heap;
sl@0
   263
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   264
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   265
		createInfo.SetSingleThread(EFalse);
sl@0
   266
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   267
		test_NotNull(heap);
sl@0
   268
		heap->Close();
sl@0
   269
		}
sl@0
   270
sl@0
   271
	test.Next(_L("Test SetAlignment 0"));
sl@0
   272
		{
sl@0
   273
		RHeap* heap;
sl@0
   274
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   275
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   276
		createInfo.SetAlignment(0);
sl@0
   277
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   278
		test_NotNull(heap);
sl@0
   279
		heap->Close();
sl@0
   280
		}
sl@0
   281
sl@0
   282
	
sl@0
   283
	test.Next(_L("Test SetAlignment -1"));
sl@0
   284
		{
sl@0
   285
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   286
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   287
		createInfo.SetAlignment(-1);
sl@0
   288
sl@0
   289
		RThread thread;
sl@0
   290
		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicChunkHeapCreate, KDefaultStackSize, KMinHeapSize, 
sl@0
   291
															KMinHeapSize,  (TAny*) &createInfo));
sl@0
   292
sl@0
   293
		test_KErrNone(TestThreadExit(thread, EExitPanic, ETHeapNewBadAlignment));
sl@0
   294
		}
sl@0
   295
sl@0
   296
sl@0
   297
	test.Next(_L("Test SetGrowby"));
sl@0
   298
		{
sl@0
   299
		RHeap* heap;
sl@0
   300
		TChunkHeapCreateInfo createInfo(0, gPageSize * 10);
sl@0
   301
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   302
		createInfo.SetGrowBy(gPageSize);
sl@0
   303
		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
sl@0
   304
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   305
		test_NotNull(heap);
sl@0
   306
		RChunk chunk;
sl@0
   307
		chunk.SetHandle(heap->ChunkHandle());
sl@0
   308
		TInt* numBuf = new TInt[gPageSize];
sl@0
   309
		test_NotNull(numBuf);
sl@0
   310
		test.Printf(_L("chunkSize = %d\n"), chunk.Size());
sl@0
   311
		test(chunk.Size() > KMinHeapGrowBy);
sl@0
   312
		delete numBuf;
sl@0
   313
		heap->Close();
sl@0
   314
		}
sl@0
   315
sl@0
   316
	test.Next(_L("Test SetGrowby growBy > maxSize"));
sl@0
   317
		{
sl@0
   318
		RHeap* heap;
sl@0
   319
		TChunkHeapCreateInfo createInfo(0, gPageSize * 5);
sl@0
   320
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   321
		createInfo.SetGrowBy(gPageSize * 6);
sl@0
   322
		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
sl@0
   323
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   324
		test_NotNull(heap);
sl@0
   325
		RChunk chunk;
sl@0
   326
		chunk.SetHandle(heap->ChunkHandle());
sl@0
   327
		TInt* numBuf = new TInt[gPageSize];
sl@0
   328
		test_Equal(NULL, numBuf);
sl@0
   329
sl@0
   330
		delete numBuf;
sl@0
   331
		heap->Close();
sl@0
   332
		}
sl@0
   333
sl@0
   334
sl@0
   335
	test.Next(_L("Test SetOffset "));
sl@0
   336
		{
sl@0
   337
		RHeap* heap;
sl@0
   338
		TChunkHeapCreateInfo createInfo(0, gPageSize * 10);
sl@0
   339
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   340
		createInfo.SetOffset(8);
sl@0
   341
		createInfo.SetMode(UserHeap::EChunkHeapSwitchTo);
sl@0
   342
		heap = (RHeap*)UserHeap::ChunkHeap(createInfo);
sl@0
   343
		TInt heapAddr = (TInt)heap;
sl@0
   344
		RChunk chunk;
sl@0
   345
		chunk.SetHandle(heap->ChunkHandle());
sl@0
   346
		test_Equal((TInt)chunk.Base() + 8, heapAddr);
sl@0
   347
		test_NotNull(heap);
sl@0
   348
		heap->Close();
sl@0
   349
		}
sl@0
   350
sl@0
   351
	test.Next(_L("Test UserHeap::ChunkHeap() SetMode() invalid"));
sl@0
   352
		{
sl@0
   353
		TChunkHeapCreateInfo createInfo(0, gPageSize);
sl@0
   354
		UseOrCreateChunk(createInfo, aTestType);
sl@0
   355
		
sl@0
   356
		// Test creating a UserHeap with invalid attributes panics.
sl@0
   357
		RThread thread;
sl@0
   358
		test_KErrNone(thread.Create(_L("Panic UserHeap"), PanicUserHeap, KDefaultStackSize, 
sl@0
   359
									KMinHeapSize, KMinHeapSize, NULL));
sl@0
   360
		test_KErrNone(TestThreadExit(thread, EExitPanic, EHeapCreateInvalidMode));
sl@0
   361
		}
sl@0
   362
sl@0
   363
	test.End();
sl@0
   364
	}
sl@0
   365
sl@0
   366
TInt TestingTChunkHeapCreate()
sl@0
   367
	{
sl@0
   368
	test.Start(_L("Test TChunkHeapCreateInfo - heap within local chunk"));
sl@0
   369
	TestChunkHeapCreate(ETestLocal);
sl@0
   370
sl@0
   371
	test.Next(_L("Test TChunkHeapCreateInfo - heap within global chunk"));
sl@0
   372
	TestChunkHeapCreate(ETestGlobal);
sl@0
   373
sl@0
   374
	test.Next(_L("Test TChunkHeapCreateInfo - heap within existing chunk"));
sl@0
   375
	TestChunkHeapCreate(ETestExisting);
sl@0
   376
sl@0
   377
	test.End();	
sl@0
   378
	return 0;
sl@0
   379
	}