kerneltest/e32test/defrag/t_ramdefrag.cpp
changeset 0 a41df078684a
child 33 0173bcd7697c
equal deleted inserted replaced
-1:000000000000 0:a41df078684a
       
     1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of the License "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // e32test\defrag\t_ramdefrag.cpp
       
    15 // RAM Defragmentation Functional Tests
       
    16 // 
       
    17 //
       
    18 
       
    19 //#define RUN_ALL_TESTS			// Uncomment to ensure that all tests are run regardless of test failures
       
    20 //#define DEBUG_VER				// Uncomment for information output from tests
       
    21 
       
    22 
       
    23 
       
    24 #define __E32TEST_EXTENSION__
       
    25 #include <e32test.h>
       
    26 RTest test(_L("T_RAMDEFRAG"));
       
    27 #include <e32rom.h>
       
    28 #include <u32hal.h>
       
    29 #include <f32file.h>
       
    30 #include <f32dbg.h>
       
    31 #include <e32msgqueue.h>
       
    32 #include <e32math.h>
       
    33 #include <hal.h>
       
    34 #include "testdefs.h"
       
    35 
       
    36 
       
    37 #include <dptest.h>
       
    38 
       
    39 #include "t_ramdefrag.h"
       
    40 
       
    41 #define READ(a) ReadByte((volatile TUint8*)(a))
       
    42 
       
    43 
       
    44 #ifdef RUN_ALL_TESTS
       
    45 #define TEST_FAIL {gTestStepFailed++;}
       
    46 #define CLEANUP(a) {}
       
    47 #else
       
    48 #define TEST_FAIL {TestCleanup(); test(EFalse);}
       
    49 #define CLEANUP(a) {if (!gFailPrintPageInfo) \
       
    50 						{ \
       
    51 						PrintPageInfo(); \
       
    52 						gFailPrintPageInfo = ETrue; \
       
    53 						} \
       
    54 					a;}
       
    55 #endif
       
    56 
       
    57 
       
    58 
       
    59 #define TEST_DRIVER_OPEN		1
       
    60 #define TEST_DRIVER_CLOSE		0
       
    61 
       
    62 #define BEST_MOVABLE			1
       
    63 #define BEST_DISCARDABLE		2
       
    64 #define BEST_FIXED				3
       
    65 
       
    66 #define Z_ALLOC_CONTIG			1
       
    67 #define Z_ALLOC_DISC			2
       
    68 
       
    69 LOCAL_D TUint gTestStarted = EFalse;								// Used to ensure matching TestStart() and TestEnd().
       
    70 LOCAL_D TBool gPagedRom = ETrue;									// Stores whether or not is a paged ROM
       
    71 LOCAL_D TInt gTestStepFailed = 0;									// Stores the number of test steps failed
       
    72 LOCAL_D TBool gFailPrintPageInfo = EFalse;							// Set to ETrue once CLEANUP has been invoked once.
       
    73 LOCAL_D TBool gFileCacheRun = EFalse;								// Set to ETrue whe FSCaching tests have been run
       
    74 
       
    75 LOCAL_D TInt gRamSize;												// The total RAM size in bytes
       
    76 LOCAL_D TInt gFreeRam;												// The amount of free RAM available in bytes
       
    77 LOCAL_D TInt gPageSize;												// The number of bytes per page
       
    78 LOCAL_D TUint gPageShift;
       
    79 #ifdef DEBUG_VER
       
    80 LOCAL_D TInt gRamUsed; 												// The amount of RAM used in bytes
       
    81 #endif
       
    82 LOCAL_D TInt gInitialRam;											// The initial free RAM before a test starts
       
    83 LOCAL_D TInt gEndRam;												// The end free RAM when a test finishes
       
    84 LOCAL_D TUint gOriginalMinCacheSize;									// The original DP minSize
       
    85 LOCAL_D TUint gOriginalMaxCacheSize;									// The original DP maxSize
       
    86 LOCAL_D TInt gTotalRamLost;											// The total amount of RAM lost during the test
       
    87 
       
    88 LOCAL_D TUint gZoneCount = 0;										// Number of zones
       
    89 LOCAL_D const TUint KInvalidZoneID = 0xffffffff;					// Invalid value for a zone ID
       
    90 LOCAL_D STestPageCount gTotalPageCount;
       
    91 
       
    92 
       
    93 LOCAL_D struct SRamZoneConfig*	gZoneConfigArray;					// Contains the configurations of all the zones
       
    94 LOCAL_D struct SRamZoneUtilisation*	gZoneUtilArray;					// Contains the utilisations of all the zones
       
    95 LOCAL_D struct SRamZoneUtilisation* gOriginalPageCountArray;		// Contains the original utilisations of the zones
       
    96 LOCAL_D TInt* gPrefArray;											// Contains the preference order of the zones 
       
    97 LOCAL_D TUint8* gOrigFlagArray;										// Contains the orignal values for the zone flags
       
    98 
       
    99 LOCAL_D TInt gDefragMaxPages = 0;
       
   100 
       
   101 const TInt KFillAllMovable = -1;
       
   102 
       
   103 LOCAL_D RChunk* gChunkArray1 = NULL;								// Stores reference to all the chunks that have been created
       
   104 LOCAL_D RChunk* gChunkArray2 = NULL;								// Stores reference to all the chunks that have been created
       
   105 LOCAL_D TUint gChunkArraySize1 = 0;									// The size of the array gChunkArray
       
   106 LOCAL_D TUint gChunkArraySize2 = 0;									// The size of the array gChunkArray
       
   107 const TUint KChunkDefaultSize = 0x300000;	
       
   108 const TUint KMaxChunks = 14;
       
   109 LOCAL_D const TUint KNumAllocChunks = 10;							// The number of chunks to be allocd for some tests.
       
   110 	
       
   111 LOCAL_D RRamDefragFuncTestLdd Ldd;									// Main Ldd used to call into device driver
       
   112 
       
   113 LOCAL_D TBuf<20> gTestThreadName =_L("TestThread");	
       
   114 LOCAL_D RThread gTestThread;
       
   115 LOCAL_D TRequestStatus status;
       
   116 
       
   117 LOCAL_D TInt gDrive;												// The removable media drive
       
   118 LOCAL_D RFs gTheFs;									
       
   119 LOCAL_D TFileName gSessionPath;
       
   120 
       
   121 const TInt KNoRemovableDrive = -1;									// gDrive is set to this when no suitable drive can be found.
       
   122 const TInt KDefaultCacheSize = (128 + 12) * 1024;					// The default file system cache size 
       
   123 const TUint KNumFilesOrig = (32 * 1024 * 1024) / KDefaultCacheSize;	// The number of files that are needed to fill the file system cache
       
   124 LOCAL_D TInt gFilesNeededToFillCache = KNumFilesOrig ;				// Not constant as can change depending on the size of the disk
       
   125 LOCAL_D RFile gFile[KNumFilesOrig];
       
   126 
       
   127 LOCAL_D TInt* gCandList1;											// Array of zones that have the same preference and the same
       
   128 LOCAL_D TInt* gCandList2;											// amount of free pages
       
   129 const TInt KInvalidCandIndex = -1;
       
   130 
       
   131 //
       
   132 // GetDrive
       
   133 //
       
   134 // Gets the removable drive number
       
   135 //
       
   136 TInt GetDrive()
       
   137 	{
       
   138 	RFs theFs;
       
   139 	TInt r = theFs.Connect();
       
   140 	test_KErrNone(r);
       
   141 
       
   142 	TInt driveLet = KNoRemovableDrive;
       
   143 
       
   144 	TInt i = EDriveA;
       
   145 	for (; i <= EDriveZ; i++)
       
   146 		{
       
   147 		TVolumeInfo volInfo;
       
   148 		r = theFs.Volume(volInfo, i);
       
   149 		if (r == KErrNone)
       
   150 			{// This drive no. exists so determine if it is removable and 
       
   151 			//  formattable media.
       
   152 			if ((volInfo.iDrive.iDriveAtt & KDriveAttRemovable) &&
       
   153 				(volInfo.iDrive.iMediaAtt & KMediaAttFormattable))	
       
   154 				{
       
   155 				driveLet = i;
       
   156 				break;
       
   157 				}
       
   158 			}
       
   159 		}
       
   160 	theFs.Close();
       
   161 	return driveLet;
       
   162 	}
       
   163 
       
   164 
       
   165 //
       
   166 // DeviceDriver
       
   167 //
       
   168 // Opens or closes the device driver used
       
   169 //
       
   170 TInt DeviceDriver(TInt aFunctionNum)
       
   171 	{
       
   172 	TInt r = 0;
       
   173 	switch (aFunctionNum)
       
   174 		{
       
   175 		case TEST_DRIVER_OPEN:
       
   176 			{
       
   177 			r = User::LoadLogicalDevice(KRamDefragFuncTestLddName);
       
   178 			test(r==KErrNone || r==KErrAlreadyExists);
       
   179 			r = Ldd.Open();
       
   180 			test_KErrNone(r);
       
   181 			}
       
   182 		break;
       
   183 		
       
   184 		case TEST_DRIVER_CLOSE:
       
   185 			{
       
   186 			Ldd.Close();
       
   187 			r = User::FreeLogicalDevice(KRamDefragFuncTestLddName);
       
   188 			test_KErrNone(r);
       
   189 			}
       
   190 		break;
       
   191 		
       
   192 		default:
       
   193 		break;
       
   194 		
       
   195 		}
       
   196 	return r;
       
   197 	}
       
   198 
       
   199 
       
   200 //
       
   201 // GetOriginalPageCount
       
   202 //
       
   203 // Obtains the orginal types of pages in each of the zones
       
   204 //
       
   205 void GetOriginalPageCount()
       
   206 	{
       
   207 	TUint index;
       
   208 	TInt ret = 0;
       
   209 	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
       
   210 	for (index = 0; index < gZoneCount; index ++)
       
   211 		{
       
   212 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gOriginalPageCountArray[index]);
       
   213 		test(ret == KErrNone);
       
   214 		}
       
   215 	}
       
   216 
       
   217 
       
   218 //
       
   219 // PrintPageInfo
       
   220 //
       
   221 // Prints various page information to the screen
       
   222 //
       
   223 void PrintPageInfo()
       
   224 	{
       
   225 	test.Printf(_L("\nZONE CONFIGURATIONS:\n"));
       
   226 	for (TUint index = 0; index < gZoneCount; index ++)
       
   227 		{
       
   228 		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
       
   229 		test(ret == KErrNone);
       
   230 		test.Printf(_L("config : id=0x%08x  index=%-2d  base=0x%08x  end=0x%08x  pages=0x%08x  pref=%-2d  flags=0x%x\n"),
       
   231 					gZoneConfigArray[index].iZoneId,gZoneConfigArray[index].iZoneIndex,
       
   232 					gZoneConfigArray[index].iPhysBase,gZoneConfigArray[index].iPhysEnd,
       
   233 					gZoneConfigArray[index].iPhysPages,gZoneConfigArray[index].iPref,gZoneConfigArray[index].iFlags);
       
   234 		}
       
   235 	test.Printf(_L("\nZONE UTILISATIONS:\n"));
       
   236 	for (TUint index = 0; index < gZoneCount; index ++)
       
   237 		{
       
   238 		TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
       
   239 		test(ret == KErrNone);
       
   240 		test.Printf(_L("usage  : id=0x%08x  index=%-2d  pref=%d  pages=0x%08x free=0x%08x  unknown=0x%08x  fixed=0x%08x  move=0x%08x  discard=0x%08x  other=0x%08x\n"),
       
   241 					gZoneUtilArray[index].iZoneId,gZoneUtilArray[index].iZoneIndex,gZoneConfigArray[index].iPref, 
       
   242 					gZoneUtilArray[index].iPhysPages,gZoneUtilArray[index].iFreePages,
       
   243 					gZoneUtilArray[index].iAllocUnknown,gZoneUtilArray[index].iAllocFixed,gZoneUtilArray[index].iAllocMovable,
       
   244 					gZoneUtilArray[index].iAllocDiscardable,gZoneUtilArray[index].iAllocOther);
       
   245 		}
       
   246 	}
       
   247 
       
   248 
       
   249 //
       
   250 // GetAllPageInfo
       
   251 //
       
   252 // Get various different page information for all zones
       
   253 // Also updates the total page count
       
   254 //
       
   255 void GetAllPageInfo()
       
   256 	{
       
   257 	TInt ret = 0;
       
   258 	gTotalPageCount.iFreePages = 0;
       
   259 	gTotalPageCount.iUnknownPages = 0;
       
   260 	gTotalPageCount.iFixedPages = 0;
       
   261 	gTotalPageCount.iMovablePages = 0;
       
   262 	gTotalPageCount.iDiscardablePages = 0;
       
   263 	gTotalPageCount.iOtherPages = 0;
       
   264 
       
   265 	// now get the config of each of the zones.
       
   266 	TUint	index;
       
   267 	TESTDEBUG(test.Printf(_L("ram defrag : Get info about the zones\n")));
       
   268 	for (index = 0; index < gZoneCount; index ++)
       
   269 		{
       
   270 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneConfig,(TAny*)index, (TAny*)&gZoneConfigArray[index]);
       
   271 		test(ret == KErrNone);
       
   272 
       
   273 		ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneUtilisation,(TAny*)index, (TAny*)&gZoneUtilArray[index]);
       
   274 		test(ret == KErrNone);
       
   275 		
       
   276 		gTotalPageCount.iFreePages += gZoneUtilArray[index].iFreePages;
       
   277 		gTotalPageCount.iUnknownPages += gZoneUtilArray[index].iAllocUnknown;
       
   278 		gTotalPageCount.iFixedPages += gZoneUtilArray[index].iAllocFixed;
       
   279 		gTotalPageCount.iMovablePages += gZoneUtilArray[index].iAllocMovable;
       
   280 		gTotalPageCount.iDiscardablePages += gZoneUtilArray[index].iAllocDiscardable;
       
   281 		gTotalPageCount.iOtherPages += gZoneUtilArray[index].iAllocOther;
       
   282 		}
       
   283 	TESTDEBUG(test.Printf(_L("free=0x%x unknown=0x%x fixed=0x%x move=0x%x discard=0x%x other=0x%x\n"),
       
   284 					gTotalPageCount.iFreePages, gTotalPageCount.iUnknownPages, gTotalPageCount.iFixedPages, 
       
   285 					gTotalPageCount.iMovablePages, gTotalPageCount.iDiscardablePages,gTotalPageCount.iOtherPages));
       
   286 
       
   287 	TESTDEBUG(PrintPageInfo());
       
   288 	}
       
   289 
       
   290 void RestoreRamZoneFlags()
       
   291 	{
       
   292 	GetAllPageInfo(); // Update the current set of RAM zone flag data.
       
   293 	for (TUint index=0; index < gZoneCount; index++)
       
   294 		{
       
   295 		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
       
   296 		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ORIG_FLAG, gOrigFlagArray[index]);
       
   297 		}
       
   298 	}
       
   299 
       
   300 void ResetRamZoneFlags()
       
   301 	{
       
   302 	GetAllPageInfo(); // Update the current set of RAM zone flag data.
       
   303 	for (TUint index=0; index < gZoneCount; index++)
       
   304 		{
       
   305 		TUint zoneDefragID = gZoneConfigArray[index].iZoneId;
       
   306 		Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, RESET_FLAG);
       
   307 		}
       
   308 	}
       
   309 
       
   310 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
       
   311 void ResetDPCache();
       
   312 
       
   313 void FSCachCleanUp()
       
   314 	{
       
   315 	// If the File System Caching tests have been run, 
       
   316 	// ensure that they are cleaned up
       
   317 	if (gFileCacheRun)
       
   318 		{
       
   319 		TUint i = 0;
       
   320 		// First close all the open handles to the RFile objects open
       
   321 		for (i = 0; i < KNumFilesOrig; i++) 
       
   322 			{
       
   323 			gFile[i].Close();
       
   324 			}
       
   325 
       
   326 		// Now call EmptyRamZone on every zone to ensure that 
       
   327 		// discardable pages are cleaned up
       
   328 		
       
   329 		GetAllPageInfo();
       
   330 		for (i = 0; i < gZoneCount; i++)
       
   331 			{
       
   332 			TUint zoneID = gZoneConfigArray[i].iZoneId;
       
   333 			Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
       
   334 			}
       
   335 		gFileCacheRun = EFalse;
       
   336 		}
       
   337 	}
       
   338 
       
   339 //
       
   340 // TestCleanup
       
   341 //
       
   342 // Cleans up all the allocations made at the beginning of the test
       
   343 //
       
   344 void TestCleanup()
       
   345 	{
       
   346 
       
   347 	Ldd.ResetDriver();
       
   348 
       
   349 	// Revert the cleared flags to their original values before the tests were carried out
       
   350 	RestoreRamZoneFlags();
       
   351 
       
   352 	// Reset the DP cache and remove any allocated chunks and fixed pages.
       
   353 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
   354 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
   355 	ResetDPCache();
       
   356 	Ldd.FreeAllFixedPages();
       
   357 	Ldd.FreeFromAllZones();
       
   358 	FSCachCleanUp();
       
   359 
       
   360 	User::Free(gPrefArray);
       
   361 	gPrefArray = NULL;
       
   362 
       
   363 	User::Free(gOrigFlagArray);
       
   364 	gOrigFlagArray = NULL;
       
   365 
       
   366 	User::Free(gCandList1);
       
   367 	gCandList1 = NULL;
       
   368 
       
   369 	User::Free(gCandList2);
       
   370 	gCandList2 = NULL;
       
   371 
       
   372 	User::Free(gOriginalPageCountArray);
       
   373 	gOriginalPageCountArray = NULL;
       
   374 
       
   375 	// Output the last possible state of memory
       
   376 	if (!gFailPrintPageInfo)
       
   377 		PrintPageInfo();
       
   378 
       
   379 	User::Free(gZoneConfigArray);
       
   380 	gZoneConfigArray = NULL;
       
   381 
       
   382 	User::Free(gZoneUtilArray);
       
   383 	gZoneUtilArray = NULL;
       
   384 
       
   385 	}
       
   386 
       
   387 
       
   388 // TestSetup
       
   389 //
       
   390 // Get the necessary information needed to carry out the tests
       
   391 //
       
   392 TInt TestSetup()
       
   393 	{
       
   394 	// Get the MMC drive
       
   395 	gDrive = GetDrive();
       
   396 
       
   397 	// first get the number of zones
       
   398 	TInt ret = UserSvr::HalFunction(EHalGroupRam,ERamHalGetZoneCount,&gZoneCount,0);
       
   399 	if (ret != KErrNone)
       
   400 		{
       
   401 		test.Printf(_L("Cannot obtain the number of zones\n"));
       
   402 		return ret;
       
   403 		}
       
   404 	test.Printf(_L("RAM Zones (count=%u)\n"),gZoneCount);
       
   405 
       
   406 	// Obtain the size of the RAM and the size of a page	
       
   407 	ret = HAL::Get(HAL::EMemoryRAM, gRamSize);
       
   408 	if (ret != KErrNone)
       
   409 		{
       
   410 		test.Printf(_L("Cannot obtain the size of RAM\n"));
       
   411 		return ret;
       
   412 		}
       
   413 
       
   414 	// Retrieve the page size and use it to detemine the page shift (assumes 32-bit system).
       
   415 	ret = HAL::Get(HAL::EMemoryPageSize, gPageSize);
       
   416 	if (ret != KErrNone)
       
   417 		{
       
   418 		test.Printf(_L("Cannot obtain the page size\n"));
       
   419 		return ret;
       
   420 		}
       
   421 
       
   422 	TUint32 pageMask = gPageSize;
       
   423 	TUint i = 0;
       
   424 	for (; i < 32; i++)
       
   425 		{
       
   426 		if (pageMask & 1)
       
   427 			{
       
   428 			if (pageMask & ~1u)
       
   429 				{
       
   430 				test.Printf(_L("ERROR - page size not a power of 2"));
       
   431 				return KErrNotSupported;
       
   432 				}
       
   433 			gPageShift = i;
       
   434 			break;
       
   435 			}
       
   436 		pageMask >>= 1;
       
   437 		}
       
   438 
       
   439 	gZoneConfigArray = (SRamZoneConfig *)User::AllocZ(sizeof(SRamZoneConfig) * gZoneCount);
       
   440 	if (gZoneConfigArray == NULL)
       
   441 		return KErrNoMemory;
       
   442 	gZoneUtilArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
       
   443 	if (gZoneUtilArray == NULL)
       
   444 		return KErrNoMemory;
       
   445 	gPrefArray = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
       
   446 	if (gPrefArray == NULL)
       
   447 		return KErrNoMemory;
       
   448 	gOrigFlagArray = (TUint8 *)User::AllocZ(sizeof(TUint8) * gZoneCount);
       
   449 	if (gOrigFlagArray == NULL)
       
   450 		return KErrNoMemory;
       
   451 	gOriginalPageCountArray = (SRamZoneUtilisation *)User::AllocZ(sizeof(SRamZoneUtilisation) * gZoneCount);
       
   452 	if (gOriginalPageCountArray == NULL)
       
   453 		return KErrNoMemory;
       
   454 
       
   455 	gCandList1 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
       
   456 	if (gCandList1 == NULL)
       
   457 		return KErrNoMemory;
       
   458 	gCandList2 = (TInt *)User::AllocZ(sizeof(TInt) * gZoneCount);
       
   459 	if (gCandList2 == NULL)
       
   460 		return KErrNoMemory;
       
   461 
       
   462 	GetAllPageInfo();
       
   463 	PrintPageInfo();
       
   464 	
       
   465 	// Store the original flags
       
   466 	for (i=0; i < gZoneCount; i++)
       
   467 		gOrigFlagArray[i] = gZoneConfigArray[i].iFlags;
       
   468 	
       
   469 	// Now clear the flags for carrying out tests
       
   470 	Ldd.ResetDriver();
       
   471 	ResetRamZoneFlags();
       
   472 	
       
   473 	
       
   474 	// Check whether the ROM is paged or not
       
   475 	TRomHeader* romHeader = (TRomHeader*)UserSvr::RomHeaderAddress();
       
   476 	if(!romHeader->iPageableRomStart)
       
   477 		{
       
   478 		test.Printf(_L("Not a Paged ROM - Skipping all discardable page tests.\n"));
       
   479 		gPagedRom = EFalse;
       
   480 		}
       
   481 	else
       
   482 		{// Save the current state of the DP cache so it can be restored when required and
       
   483 		// after the test has finished.
       
   484 		TUint currentCacheSize;
       
   485 		DPTest::CacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize, currentCacheSize);
       
   486 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
   487 									gOriginalMinCacheSize >> gPageShift, gOriginalMaxCacheSize >> gPageShift, 
       
   488 									currentCacheSize >> gPageShift));
       
   489 		}
       
   490 
       
   491 	return KErrNone;
       
   492 	}
       
   493 
       
   494 
       
   495 // 
       
   496 // UpdateRamInfo
       
   497 //
       
   498 // Updating the various RAM information
       
   499 //
       
   500 void UpdateRamInfo()
       
   501 	{
       
   502 	HAL::Get(HAL::EMemoryRAMFree, gFreeRam);
       
   503 	TESTDEBUG(gRamUsed = gRamSize - gFreeRam);
       
   504 	}
       
   505 	
       
   506 
       
   507 // 
       
   508 // CheckRamDifference
       
   509 //
       
   510 // Checks the difference between the initial free RAM and the end free RAM
       
   511 //
       
   512 void CheckRamDifference()
       
   513 	{
       
   514 	if (gInitialRam == gEndRam)
       
   515 		{
       
   516 		TESTDEBUG(test.Printf(_L("No RAM was lost during this test\n")));
       
   517 		}
       
   518 	else
       
   519 		{
       
   520 		TInt diff = gInitialRam - gEndRam;
       
   521 
       
   522 
       
   523 		gTotalRamLost = gTotalRamLost + diff;
       
   524 		}
       
   525 	}
       
   526 
       
   527 TInt VerifyMovDisAlloc();
       
   528 //
       
   529 // TestStart
       
   530 //
       
   531 // Updates the RAM information at the beginning of a test step	
       
   532 //
       
   533 void TestStart()
       
   534 	{
       
   535 	test(!gTestStarted);
       
   536 	gTestStarted = ETrue;
       
   537 
       
   538 	Ldd.ResetDriver();
       
   539 	
       
   540 	Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
   541 	if (VerifyMovDisAlloc() != KErrNone)
       
   542 		{
       
   543 		CLEANUP(;);
       
   544 		TEST_FAIL;
       
   545 		}
       
   546 	
       
   547 	UpdateRamInfo();
       
   548 	gInitialRam = gFreeRam;
       
   549 	TESTDEBUG(test.Printf(_L("Initial Free RAM = 0x%x, Initial RAM Used = 0x%x\n"), gFreeRam, gRamUsed));
       
   550 	}
       
   551 
       
   552 
       
   553 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize);
       
   554 //
       
   555 // TestEnd
       
   556 //
       
   557 // Updates RAM information at end of test step and checks the RAM delta
       
   558 //
       
   559 void TestEnd()
       
   560 	{
       
   561 	test(gTestStarted);
       
   562 	gTestStarted = EFalse;
       
   563 
       
   564 	gDefragMaxPages = 0;
       
   565 
       
   566 	// Clean up anything that may need to be cleaned.
       
   567 	ResetRamZoneFlags();
       
   568 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
   569 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
   570 	ResetDPCache();
       
   571 	Ldd.FreeAllFixedPages();
       
   572 	FSCachCleanUp();
       
   573 	
       
   574 	UpdateRamInfo();
       
   575 	gEndRam = gFreeRam;
       
   576 	TESTDEBUG(test.Printf(_L("End RAM Free = 0x%x, End RAM Used = 0x%x\n"), gEndRam, gRamUsed));
       
   577 	CheckRamDifference();
       
   578 	test.Printf(_L(" \n"));
       
   579 	}
       
   580 
       
   581 //
       
   582 // CheckZonesSwitchedOff
       
   583 //
       
   584 // Checks that zones have been switched off
       
   585 //
       
   586 TBool CheckZonesSwitchedOff()
       
   587 	{
       
   588 	GetAllPageInfo();
       
   589 	for (TUint i = 0; i < gZoneCount; i++)
       
   590 		{
       
   591 		if (gOriginalPageCountArray[i].iFreePages != gOriginalPageCountArray[i].iPhysPages &&
       
   592 			gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
       
   593 			{
       
   594 			return ETrue;
       
   595 			}
       
   596 		}
       
   597 	return EFalse;
       
   598 	}
       
   599 	
       
   600 
       
   601 //
       
   602 // CheckZoneIsOff
       
   603 //
       
   604 // Checks if a particular zone is empty 
       
   605 //
       
   606 TBool CheckZoneIsOff(TUint aZoneIndex)
       
   607 	{
       
   608 	GetAllPageInfo();
       
   609 	if (gZoneUtilArray[aZoneIndex].iFreePages == gZoneUtilArray[aZoneIndex].iPhysPages)
       
   610 		{
       
   611 		TESTDEBUG(test.Printf(_L("Zone index %d is Empty\n"), aZoneIndex));
       
   612 		return ETrue;
       
   613 		}
       
   614 	else
       
   615 		{
       
   616 		TESTDEBUG(test.Printf(_L("Zone index %d is Not empty\n"), aZoneIndex));
       
   617 		return EFalse;
       
   618 		}
       
   619 	}
       
   620 
       
   621 
       
   622 //
       
   623 // GetPrefOrder
       
   624 //
       
   625 // Go through each zone ordering them in preference order
       
   626 //
       
   627 void GetPrefOrder()
       
   628 	{
       
   629 	GetAllPageInfo();
       
   630 	TESTDEBUG(PrintPageInfo());
       
   631 
       
   632 	for (TUint i=0; i < (TUint)gZoneCount; i++)
       
   633 		gPrefArray[i] = KErrNotFound;
       
   634 
       
   635 	for (TUint curIndex = 0; curIndex < gZoneCount; curIndex++)
       
   636 		{
       
   637 		TBool currentEmpty = gZoneUtilArray[curIndex].iPhysPages == gZoneUtilArray[curIndex].iFreePages;
       
   638 		TUint currentPref = gZoneConfigArray[curIndex].iPref;
       
   639 		TUint currentImmovPages = 	gZoneUtilArray[curIndex].iAllocFixed + 
       
   640 									gZoneUtilArray[curIndex].iAllocUnknown;
       
   641 		TUint morePrefCnt = 0;
       
   642 		for (TUint index = 0; index < gZoneCount; index++)
       
   643 			{// A RAM zone with the same iPref is more preferable if it has 
       
   644 			// more immovable pages.
       
   645 			if (gZoneConfigArray[index].iPref < currentPref || 
       
   646 				(gZoneConfigArray[index].iPref == currentPref && 
       
   647 				(currentImmovPages < gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ||
       
   648 				(currentEmpty &&
       
   649 				gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages))))
       
   650 				{
       
   651 				morePrefCnt++;
       
   652 				}
       
   653 			}
       
   654 
       
   655 		while (gPrefArray[morePrefCnt] != KErrNotFound)
       
   656 			{// Zone(s) of this preference and size already exist so 
       
   657 			 // place this one after it/them
       
   658 			morePrefCnt++;
       
   659 			}
       
   660 		gPrefArray[morePrefCnt] = curIndex;
       
   661 		}
       
   662 	}
       
   663 
       
   664 //
       
   665 // ZonesSamePref
       
   666 //
       
   667 // Return ETrue if the RAM zones are of the same preference.
       
   668 //
       
   669 // NOTE - This requires GetAllPageInfo() to have already been called.
       
   670 //
       
   671 TBool ZonesSamePref(TUint aZoneIndex1, TUint aZoneIndex2)
       
   672 	{
       
   673 	TBool zoneEmpty1 = gZoneUtilArray[aZoneIndex1].iFreePages == gZoneUtilArray[aZoneIndex1].iPhysPages;
       
   674 	TBool zoneEmpty2 = gZoneUtilArray[aZoneIndex2].iFreePages == gZoneUtilArray[aZoneIndex2].iPhysPages;
       
   675 	if (gZoneConfigArray[aZoneIndex1].iPref == gZoneConfigArray[aZoneIndex2].iPref &&
       
   676 		(gZoneUtilArray[aZoneIndex1].iAllocFixed + gZoneUtilArray[aZoneIndex1].iAllocUnknown == 
       
   677 		gZoneUtilArray[aZoneIndex2].iAllocFixed + gZoneUtilArray[aZoneIndex2].iAllocUnknown &&
       
   678 		(zoneEmpty1 == zoneEmpty2)))
       
   679 		{
       
   680 		return ETrue;
       
   681 		}
       
   682 
       
   683 	return EFalse;	
       
   684 	}
       
   685 
       
   686 //
       
   687 // FindMostPrefEmpty
       
   688 //
       
   689 // Checks all zones and returns the most preferable RAM zone which 
       
   690 // is completely emtpy
       
   691 //
       
   692 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable empty RAM zone.
       
   693 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable empty RAM zone.
       
   694 //
       
   695 // @return KErrNotFound if a zone cannot be found, else KErrNone
       
   696 //
       
   697 TInt FindMostPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
       
   698 	{
       
   699 	// Get the most pref zone which is completely free to use as a test zone
       
   700 	GetPrefOrder();
       
   701 	TUint prefIndex = 0;
       
   702 	for (; prefIndex < gZoneCount; prefIndex++)
       
   703 		{
       
   704 		TUint zoneIndex = gPrefArray[prefIndex];
       
   705 		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
       
   706 			{
       
   707 			aZoneIndex = zoneIndex;
       
   708 			if (aPrefIndex)
       
   709 				{
       
   710 				*aPrefIndex = prefIndex;
       
   711 				}
       
   712 			return KErrNone;
       
   713 			}
       
   714 		}
       
   715 	return KErrNotFound;
       
   716 	}
       
   717 
       
   718 //
       
   719 // FindLeastPrefEmpty
       
   720 //
       
   721 // Checks all zones and returns the least preferable RAM zone which 
       
   722 // is completely emtpy
       
   723 //
       
   724 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the least preferable empty RAM zone.
       
   725 // @param aPrefIndex On return this will contain the index into gPrefArray of the least preferable empty RAM zone.
       
   726 //
       
   727 // @return KErrNotFound if a zone cannot be found, else KErrNone
       
   728 //
       
   729 TInt FindLeastPrefEmpty(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
       
   730 	{
       
   731 	// Get the most pref zone which is completely free to use as a test zone
       
   732 	GetPrefOrder();
       
   733 	TInt prefIndex = gZoneCount - 1;
       
   734 	for (; prefIndex >= 0; prefIndex--)
       
   735 		{
       
   736 		TUint zoneIndex = gPrefArray[prefIndex];
       
   737 		if (gZoneUtilArray[zoneIndex].iFreePages == gZoneUtilArray[zoneIndex].iPhysPages)
       
   738 			{
       
   739 			aZoneIndex = zoneIndex;
       
   740 			if (aPrefIndex)
       
   741 				{
       
   742 				*aPrefIndex = (TUint)prefIndex;
       
   743 				}
       
   744 			return KErrNone;
       
   745 			}
       
   746 		}
       
   747 	return KErrNotFound;
       
   748 	}
       
   749 
       
   750 //
       
   751 // FindMostPrefWithFree
       
   752 //
       
   753 // Checks all zones and returns the most preferable RAM zone which 
       
   754 // has at least 1 free page 
       
   755 //
       
   756 // @param aZoneIndex On return this will contain the index into gZoneUtilArray of the most preferable RAM zone with free pages.
       
   757 // @param aPrefIndex On return this will contain the index into gPrefArray of the most preferable RAM zone with free pages.
       
   758 //
       
   759 // @return KErrNotFound if a zone cannot be found, else KErrNone
       
   760 //
       
   761 TInt FindMostPrefWithFree(TUint& aZoneIndex, TUint* aPrefIndex = NULL)
       
   762 	{
       
   763 	// Get the most pref zone which has free pages
       
   764 	GetPrefOrder();
       
   765 	TUint prefIndex = 0;
       
   766 	for (; prefIndex < gZoneCount; prefIndex++)
       
   767 		{
       
   768 		TUint zoneIndex = gPrefArray[prefIndex];
       
   769 		if (gZoneUtilArray[zoneIndex].iFreePages)
       
   770 			{
       
   771 			aZoneIndex = zoneIndex;
       
   772 			if (aPrefIndex)
       
   773 				{
       
   774 				*aPrefIndex = prefIndex;
       
   775 				}
       
   776 			return KErrNone;
       
   777 			}
       
   778 		}
       
   779 	return KErrNotFound;
       
   780 	}
       
   781 //
       
   782 // CanGenSucceed
       
   783 //
       
   784 // Check whether a call to TRamDefragRequest::DefragRam() would be able to 
       
   785 // succeed or not. 
       
   786 //
       
   787 TBool CanGenSucceed()
       
   788 	{
       
   789 	GetPrefOrder();
       
   790 	TBool genSucceed = EFalse;
       
   791 	// Work out if general has anything to do
       
   792 	for(TInt prefIndex = (TInt)gZoneCount-1; prefIndex >= 0; prefIndex--)
       
   793 		{
       
   794 		TUint zoneIndex = gPrefArray[prefIndex];
       
   795 		TESTDEBUG(test.Printf(_L("prefIndex = %d zoneIndex = 0x%x\n"), prefIndex, zoneIndex));
       
   796 
       
   797 		TUint samePrefCount = 1;
       
   798 		TUint samePrefSucceed = 0;
       
   799 		// The number of zones of this preference that will be skipped by the general defrag
       
   800 		TUint samePrefEmptyImmovable = 0;
       
   801 
       
   802 		// Determine how many zones have the same preference as this one
       
   803 		TInt prevPrefIndex = (prefIndex != 0)? (prefIndex - 1) : -1;
       
   804 		for (; prevPrefIndex >= 0; prevPrefIndex--)
       
   805 			{
       
   806 			TUint prevIndex = gPrefArray[prevPrefIndex];
       
   807 			if (ZonesSamePref(zoneIndex, prevIndex))
       
   808 				{
       
   809 				samePrefCount++;
       
   810 				}
       
   811 			else // no more zones with this preference
       
   812 				break;
       
   813 			}
       
   814 		TESTDEBUG(test.Printf(_L("samePrefCount = %d\n"), samePrefCount));
       
   815 
       
   816 		for (TInt l = prefIndex - (samePrefCount-1); l <= prefIndex; l++)
       
   817 			{
       
   818 			TUint curPrefIndex = gPrefArray[l];
       
   819 			TESTDEBUG(test.Printf(_L("curPrefIndex = %d\n"), curPrefIndex));
       
   820 			if (gZoneUtilArray[curPrefIndex].iFreePages != gZoneConfigArray[curPrefIndex].iPhysPages)
       
   821 				{
       
   822 				TBool clearMovable = EFalse;
       
   823 				TBool clearDiscardable = EFalse;
       
   824 
       
   825 				if (gZoneUtilArray[curPrefIndex].iAllocUnknown || gZoneUtilArray[curPrefIndex].iAllocFixed)
       
   826 					{
       
   827 					TESTDEBUG(test.Printf(_L("unknown or fixed\n")));
       
   828 					samePrefEmptyImmovable++;
       
   829 					continue;
       
   830 					}
       
   831 				if (gZoneUtilArray[curPrefIndex].iAllocMovable)
       
   832 					{// determine if movable can potentially be cleared from this zone
       
   833 					TUint freeInLower = 0;
       
   834 					for (TInt j=0; j <= prefIndex; j++)
       
   835 						{
       
   836 						TUint idx = gPrefArray[j];
       
   837 						if (idx == curPrefIndex)
       
   838 							continue;
       
   839 						freeInLower += gZoneUtilArray[idx].iFreePages;
       
   840 						}
       
   841 					if (gZoneUtilArray[curPrefIndex].iAllocMovable <= freeInLower)
       
   842 						{
       
   843 						clearMovable = ETrue;
       
   844 						TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
       
   845 						}
       
   846 					}
       
   847 				else
       
   848 					{
       
   849 					TESTDEBUG(test.Printf(_L("Can clear movable, curPrefIndex = %d\n"), curPrefIndex));
       
   850 					clearMovable = ETrue;
       
   851 					}
       
   852 				if (gZoneUtilArray[curPrefIndex].iAllocDiscardable)
       
   853 					{
       
   854 					if (gPagedRom)
       
   855 						{
       
   856 						TUint minCacheSize = 0;
       
   857 						TUint maxCacheSize = 0;
       
   858 						TUint currentCacheSize = 0;
       
   859 
       
   860 						DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
   861 						TUint spareCache = currentCacheSize - minCacheSize;
       
   862 						if (spareCache >= gZoneUtilArray[curPrefIndex].iAllocDiscardable )
       
   863 							{
       
   864 							clearDiscardable = ETrue;
       
   865 							TESTDEBUG(test.Printf(_L("Paged: Can clear discardable\n")));
       
   866 							}
       
   867 						else
       
   868 							{// determine space for discardable in more preferable zones
       
   869 							TUint freeInLower = 0;
       
   870 							for (TInt j=0; j <= prefIndex; j++)
       
   871 								{
       
   872 								TUint idx = gPrefArray[j];
       
   873 								if (idx == curPrefIndex)
       
   874 									continue;
       
   875 								freeInLower += gZoneUtilArray[idx].iFreePages;
       
   876 								}
       
   877 							if (gZoneUtilArray[curPrefIndex].iAllocDiscardable - spareCache <= freeInLower)
       
   878 								{
       
   879 								clearDiscardable = ETrue;	
       
   880 								TESTDEBUG(test.Printf(_L("Paged: Can clear discardable curPrefIndex = %d\n"), curPrefIndex));
       
   881 								}
       
   882 							}
       
   883 						}
       
   884 					else
       
   885 						{//Should always be OK to discard as no min cache size on non-paged ROMS
       
   886 						clearDiscardable = ETrue;
       
   887 						test.Printf(_L("Can clear discardable curPrefIndex = %d\n"), curPrefIndex);
       
   888 						}
       
   889 					}
       
   890 				else
       
   891 					{
       
   892 					clearDiscardable = ETrue;
       
   893 					}
       
   894 
       
   895 				if (clearDiscardable && clearMovable)
       
   896 					{
       
   897 					samePrefSucceed++;
       
   898 					TESTDEBUG(test.Printf(_L("General should succeed ID=%x\n"), gZoneConfigArray[curPrefIndex].iZoneId));
       
   899 					}
       
   900 				}
       
   901 			else
       
   902 				{//zone already empty
       
   903 				samePrefEmptyImmovable++;
       
   904 				}
       
   905 			}
       
   906 		if (samePrefSucceed == 0 && samePrefEmptyImmovable == 0)
       
   907 			{// no zones can be defragged and none are already empty/have immovable.
       
   908 			break;
       
   909 			}
       
   910 		if (samePrefEmptyImmovable != samePrefCount)
       
   911 			{// Have reached some zones with allocated pages in them.
       
   912 			if (samePrefSucceed + samePrefEmptyImmovable == samePrefCount)
       
   913 				{// general should definitely succeed as each of the zones of this preference 
       
   914 				// can be emptied or are already empty/have immovable pages allocated.
       
   915 				TESTDEBUG(test.Printf(_L("General should succeed \n")));
       
   916 				genSucceed = ETrue;
       
   917 				}
       
   918 			break;
       
   919 			}
       
   920 		prefIndex -= samePrefCount - 1;
       
   921 		}
       
   922 	return genSucceed;
       
   923 	}
       
   924 
       
   925 
       
   926 //
       
   927 // ReadByte
       
   928 //
       
   929 // Read a particular byte 
       
   930 //
       
   931 TUint8 ReadByte(volatile TUint8* aPtr)
       
   932 	{
       
   933 	return *aPtr;
       
   934 	}
       
   935 
       
   936 
       
   937 //
       
   938 // AllocDiscardable
       
   939 //
       
   940 // Allocate Discardable pages in the form of demand paged pages
       
   941 //
       
   942 // @param aNumDiscardableBytes On return this will contain the number of discardable bytes above the min cache size.
       
   943 // @param aMaxBytes The new limit for maximum number of bytes in the DP cache, set to KMaxTUInt64 to fill RAM.
       
   944 // @param aMinOffsetBytes When not set to KMaxTUint64, this sets the min cache size to be the max cache size - aMinOffsetBytes.
       
   945 //
       
   946 TInt AllocDiscardable(TInt& aNumDiscardableBytes, TUint64 aMaxBytes = KMaxTUint64, TUint64 aMinOffsetBytes = KMaxTUint64)
       
   947 	{
       
   948 	TUint minCacheSize = 0;
       
   949 	TUint maxCacheSize = 0;
       
   950 	TUint currentCacheSize = 0;
       
   951 	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
   952 	TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
   953 								minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
   954 
       
   955 	TESTDEBUG(test.Printf(_L("SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), minCacheSize >> gPageShift, aMaxBytes >> gPageShift));
       
   956 
       
   957 	if (aMaxBytes == KMaxTUint64)
       
   958 		{// Need to fill all of free memory with discardable pages
       
   959 		UpdateRamInfo();
       
   960 		maxCacheSize = minCacheSize + gFreeRam;
       
   961 		if (aMinOffsetBytes != KMaxTUint64)
       
   962 			{// Set the min cache size relative to the max cache size.
       
   963 			minCacheSize = maxCacheSize - aMinOffsetBytes;
       
   964 			}
       
   965 		TESTDEBUG(test.Printf(_L("free 0x%x max 0x%x min 0x%x\n"), gFreeRam, maxCacheSize, minCacheSize));
       
   966 		}
       
   967 	else
       
   968 		{
       
   969 		maxCacheSize = aMaxBytes;
       
   970 		}
       
   971 
       
   972 	TInt r = DPTest::SetCacheSize(maxCacheSize, maxCacheSize);
       
   973 	if (r != KErrNone)
       
   974 		return r;
       
   975 	r = DPTest::SetCacheSize(minCacheSize, maxCacheSize);
       
   976 	if (r != KErrNone)
       
   977 		return r;
       
   978 	DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
   979 	TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
   980 					minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
   981 
       
   982 	aNumDiscardableBytes = currentCacheSize - minCacheSize;
       
   983 	TESTDEBUG(test.Printf(_L("Number of discardable bytes 0x%x\n"), aNumDiscardableBytes));
       
   984 
       
   985 	if (aMaxBytes == KMaxTUint64)
       
   986 		{
       
   987 		UpdateRamInfo();
       
   988 		if (gFreeRam != aNumDiscardableBytes)
       
   989 			{// The only free RAM should be that of the DP cache.
       
   990 			test.Printf(_L("gFreeRam 0x%x aNumDiscardableBytes 0x%x\n"), gFreeRam, aNumDiscardableBytes);
       
   991 			return KErrGeneral;
       
   992 			}
       
   993 		}
       
   994 	return KErrNone;
       
   995 	}	
       
   996 	
       
   997 
       
   998 //
       
   999 // ResetDPCache
       
  1000 //
       
  1001 // Flush the cache and set the boundaries back to their original values
       
  1002 //
       
  1003 void ResetDPCache()
       
  1004 	{
       
  1005 	if (gPagedRom)
       
  1006 		{
       
  1007 		TUint minCacheSize = 0;
       
  1008 		TUint maxCacheSize = 0;
       
  1009 		TUint currentCacheSize = 0;
       
  1010 		
       
  1011 		TESTDEBUG(test.Printf(_L("FlushCache\n")));
       
  1012 		TInt r = DPTest::FlushCache();
       
  1013 
       
  1014 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  1015 		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  1016 						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  1017 		
       
  1018 		
       
  1019 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  1020 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMaxCacheSize);
       
  1021 		TESTDEBUG(test.Printf(_L("SetCacheSize returns r = %d\n"), r));
       
  1022 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  1023 		TESTDEBUG(test.Printf(_L("After CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  1024 						minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  1025 		}
       
  1026 	}
       
  1027 	
       
  1028 //
       
  1029 // WriteToChunk
       
  1030 //
       
  1031 // Write to a number of chunks
       
  1032 //
       
  1033 TInt WriteToChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk, TUint8 aStart=0)
       
  1034 	{
       
  1035 	for (TUint i=0; i<10; i++) // Write to all chunks 10 times
       
  1036 		{
       
  1037 		for (TUint j=0; j < aChunkArraySize; j++) //Write to all open chunks except aChunk.
       
  1038 			{
       
  1039 			if (aChunkArray[j].Handle() != NULL)
       
  1040 				{
       
  1041 				if ((TInt)j == aChunk) // Don't write to specified chunk
       
  1042 					{	
       
  1043 					continue;	
       
  1044 					}
       
  1045 				TUint8* base = aChunkArray[j].Base();
       
  1046 				for (TUint8 k = aStart; k < aStart + 10; k++)
       
  1047 					{
       
  1048 					*base++ = k; // write 0 - 9 to the chunk
       
  1049 					}
       
  1050 				}		
       
  1051 			}
       
  1052 		}
       
  1053 	return KErrNone;
       
  1054 	}
       
  1055 
       
  1056 
       
  1057 //
       
  1058 // ReadChunk
       
  1059 //
       
  1060 // Read chunks - If a chunk is specified, that particular chunk is not read
       
  1061 //
       
  1062 TInt ReadChunk(RChunk* aChunkArray, TUint aChunkArraySize, TInt aChunk=-1)
       
  1063 	{
       
  1064 	for (TUint j=0; j < aChunkArraySize; j++) //Read all open chunks
       
  1065 		{
       
  1066 		if (aChunkArray[j].Handle() != NULL)
       
  1067 			{
       
  1068 			if ((TInt)j == aChunk) // Don't read specified chunk
       
  1069 				{	
       
  1070 				continue;	
       
  1071 				}
       
  1072 			TUint8* base = aChunkArray[j].Base();
       
  1073 			while ((aChunkArray[j].Base() + aChunkArray[j].Size()) != base)
       
  1074 				{
       
  1075 				READ(base++);				
       
  1076 				}
       
  1077 			}		
       
  1078 		}
       
  1079 	return KErrNone;
       
  1080 	}
       
  1081 	
       
  1082 
       
  1083 //
       
  1084 // RemoveChunkAlloc
       
  1085 //
       
  1086 // Remove ALL chunks allocated
       
  1087 //
       
  1088 // @param aChunkArray The array that stores a reference to the chunks created.
       
  1089 // @param aChunkArraySize The size of aChunkArray.
       
  1090 //
       
  1091 void RemoveChunkAlloc(RChunk*& aChunkArray, TUint& aChunkArraySize)
       
  1092 	{
       
  1093 	TInt closedChunks = 0;
       
  1094 
       
  1095 	if (aChunkArray == NULL)
       
  1096 		{// The chunk array has already been deleted.
       
  1097 		return;
       
  1098 		}
       
  1099 
       
  1100 	for (TUint i = 0; i < aChunkArraySize; i++)
       
  1101 		{
       
  1102 		if (aChunkArray[i].Handle() != NULL)
       
  1103 			{
       
  1104 			aChunkArray[i].Close();
       
  1105 			closedChunks ++;
       
  1106 			}
       
  1107 		}
       
  1108 	delete[] aChunkArray;
       
  1109 	aChunkArray = NULL;
       
  1110 	aChunkArraySize = 0;
       
  1111 	UpdateRamInfo();
       
  1112 	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
       
  1113 	}	
       
  1114 
       
  1115 
       
  1116 TBool SpaceAvailForPageTables(TUint aZoneIndex, TInt aNumPages)
       
  1117 	{
       
  1118 	// Every 1MB allocated needs a new page table
       
  1119 	const TUint KDataBytesPerPageTable = 1024 * 1024; 
       
  1120 	
       
  1121 	// 1 Page can fit 4 page tables
       
  1122 	const TUint KPageTablesPerPage = 4;
       
  1123 	
       
  1124 	
       
  1125 	GetAllPageInfo();
       
  1126 	if (aNumPages == KFillAllMovable)
       
  1127 		{
       
  1128 		aNumPages = gTotalPageCount.iFreePages;
       
  1129 		}
       
  1130 
       
  1131 	TUint allocBytes = aNumPages << gPageShift;
       
  1132 
       
  1133 	// Add 1 as you always require at least 1 page table
       
  1134 	TUint pageTablesRequired = (allocBytes / KDataBytesPerPageTable) + 1;
       
  1135 	// Add 1 as the first 1-3 page tables may require a new page.
       
  1136 	TUint pageTablePagesRequired = (pageTablesRequired / KPageTablesPerPage) + 1;
       
  1137 	
       
  1138 	// Determine the number of free pages in the other zones
       
  1139 	TUint freeInOther = 0;
       
  1140 	for (TUint index = 0; index < gZoneCount; index++)
       
  1141 		{
       
  1142 		if (index != aZoneIndex)
       
  1143 			{
       
  1144 			freeInOther += gZoneUtilArray[index].iFreePages;
       
  1145 			}
       
  1146 		}
       
  1147 
       
  1148 	// Need an overhead for the heap to grow (5 pages)
       
  1149 	const TUint KOverhead = 5;
       
  1150 	if (freeInOther < pageTablePagesRequired + KOverhead)
       
  1151 		{// Not enough space in other zones to fit all page tables
       
  1152 		test.Printf(_L("No space in other zones for page table pages\n"));
       
  1153 		return EFalse;
       
  1154 		}
       
  1155 	// There is space available in the other zones to fit all the page tables
       
  1156 	return ETrue;
       
  1157 	}
       
  1158 //
       
  1159 // AllocMovable
       
  1160 //
       
  1161 // Allocate movable memory in the form of chunks
       
  1162 //
       
  1163 // @param aChunkArray The array to store a reference to the chunks created.
       
  1164 // @param aChunkArraySize The size of aChunkArray.
       
  1165 // @param aNumChunks The number of chunks to create.
       
  1166 // @param aNumPages The size of each chunk.
       
  1167 //
       
  1168 TInt AllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TInt aNumChunks, TUint aChunkSize=KChunkDefaultSize, TBool aForceFill = ETrue)
       
  1169 	{
       
  1170 	TUint i = 0;
       
  1171 	TInt r = 0;
       
  1172 	TUint chunksAllocd = 0;
       
  1173 	TBool fillAll = EFalse;
       
  1174 
       
  1175 	TInt numChunks = aNumChunks;
       
  1176 	UpdateRamInfo();
       
  1177 	// Allocate chunks to take up all of memory with the maximum number of chunks
       
  1178 	if (aNumChunks == KFillAllMovable)
       
  1179 		{
       
  1180 		fillAll = ETrue;
       
  1181 		if (aChunkSize == KChunkDefaultSize)
       
  1182 			{
       
  1183 			numChunks = KMaxChunks;
       
  1184 			aChunkSize = gFreeRam / numChunks; 
       
  1185 			}
       
  1186 		else
       
  1187 			{
       
  1188 			numChunks = gFreeRam  / aChunkSize;
       
  1189 			}
       
  1190 		}
       
  1191 
       
  1192 	test.Printf(_L("gFreeRam = 0x%x, aChunkSize = 0x%x, numChunks = %d\n"), gFreeRam, aChunkSize, numChunks);
       
  1193 	
       
  1194 	// Allocate as many chunks as is specified, either with the default chunk size or a specified chunk size
       
  1195 	if (aChunkArray == NULL)
       
  1196 		{
       
  1197 		aChunkArraySize = numChunks;
       
  1198 		aChunkArray = new RChunk[aChunkArraySize];
       
  1199 		if (aChunkArray == NULL)
       
  1200 			return KErrNoMemory;
       
  1201 		}
       
  1202 
       
  1203 	// Create chunks for each RChunk with a NULL handle.
       
  1204 	for (i = 0; i < aChunkArraySize; i++)
       
  1205 		{
       
  1206 		if (aChunkArray[i].Handle() == NULL)
       
  1207 			{
       
  1208 			// Keep going even if a chunk creation fails as the flag tests rely 
       
  1209 			// on this.
       
  1210 			r = aChunkArray[i].CreateLocal(aChunkSize, aChunkSize);
       
  1211 			if (r != KErrNone && fillAll && aForceFill)
       
  1212 				{
       
  1213 				while (aChunkArray[i].CreateLocal(aChunkSize, aChunkSize) != KErrNone)
       
  1214 					{
       
  1215 					aChunkSize -= gPageSize;
       
  1216 					}
       
  1217 				}
       
  1218 			if (r == KErrNone)	
       
  1219 				{
       
  1220 				chunksAllocd++;
       
  1221 				}
       
  1222 			}
       
  1223 		User::After(10); // Wait so that the next chunk gets allocated in the next time slice
       
  1224 		}
       
  1225 	test.Printf(_L("Number of chunks allocd = %d\n"),chunksAllocd);
       
  1226 	return r;
       
  1227 	}
       
  1228 
       
  1229 //
       
  1230 // ZoneAllocMovable
       
  1231 //
       
  1232 // Allocate the specified number of movable pages to a specific zone
       
  1233 // If the number of pages is not specified, then fill the specified zone with
       
  1234 // movable pages
       
  1235 //
       
  1236 // @param aChunkArray The array to store a reference to the chunks created.
       
  1237 // @param aChunkArraySize The size of aChunkArray.
       
  1238 // @param aZoneIndex The zone index to allocate movable pages to.
       
  1239 // @param aNumPages The number of movable pages to allocate.
       
  1240 //
       
  1241 TInt ZoneAllocMovable(RChunk*& aChunkArray, TUint& aChunkArraySize, TUint aZoneIndex, TUint aNumPages = KMaxTUint)
       
  1242 	{
       
  1243 	ResetRamZoneFlags();
       
  1244 	TInt r = KErrNone;
       
  1245 	TUint allocBytes = 0;
       
  1246 
       
  1247 	if (aNumPages == KMaxTUint)
       
  1248 		{
       
  1249 		aNumPages = gZoneUtilArray[aZoneIndex].iFreePages;
       
  1250 		}
       
  1251 
       
  1252 	allocBytes = aNumPages << gPageShift;
       
  1253 
       
  1254 	if (!SpaceAvailForPageTables(aZoneIndex, aNumPages))
       
  1255 		{
       
  1256 		return KErrGeneral;
       
  1257 		}
       
  1258 
       
  1259 	// Block all other zones from allocation
       
  1260 	for(TUint index = 0; index < gZoneCount; index++)
       
  1261 		{
       
  1262 		if (index == aZoneIndex)
       
  1263 			{
       
  1264 			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  1265 			}
       
  1266 		else
       
  1267 			{
       
  1268 			r = Ldd.SetZoneFlag(gZoneConfigArray[index].iZoneId, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
       
  1269 			}
       
  1270 		if (r != KErrNone)
       
  1271 			{
       
  1272 			test.Printf(_L("Failed to set RAM zone flag on zone %d r = %d\n"), index, r);
       
  1273 			return r;
       
  1274 			}
       
  1275 		}
       
  1276 
       
  1277 	// Allocate the movable pages
       
  1278 	r = AllocMovable(aChunkArray, aChunkArraySize, 1, allocBytes);
       
  1279 
       
  1280 	ResetRamZoneFlags();
       
  1281 	return r;
       
  1282 	}
       
  1283 
       
  1284 //
       
  1285 // ZoneAllocDiscard
       
  1286 //
       
  1287 // Allocate the specified number of discardable pages to a specific zone
       
  1288 //
       
  1289 // @param aZoneIndex The zone index to allocate discardable pages to.
       
  1290 // @param aNumPages The number of discardable pages to allocate.
       
  1291 // @param aDisPages On return this will contain the number of discardable pages allocated
       
  1292 //
       
  1293 TInt ZoneAllocDiscard(TUint aZoneIndex, TUint aNumPages, TInt& aDisPages)
       
  1294 	{
       
  1295 	TInt r = KErrNone;
       
  1296 	ResetRamZoneFlags();
       
  1297 	
       
  1298 	for (TUint index = 0; index < gZoneCount; index++)
       
  1299 		{
       
  1300 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  1301 		if (index == aZoneIndex)
       
  1302 			{
       
  1303 			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  1304 			}
       
  1305 		else
       
  1306 			{
       
  1307 			r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
       
  1308 			}
       
  1309 
       
  1310 		if (r != KErrNone)
       
  1311 			{			
       
  1312 			test.Printf(_L("Failed to set flag r = %d\n"), r);
       
  1313 			return r;
       
  1314 			}
       
  1315 		}
       
  1316 	
       
  1317 	TUint disBytes = gTotalPageCount.iDiscardablePages + aNumPages << gPageShift;
       
  1318 	r = AllocDiscardable(aDisPages, disBytes);
       
  1319 	aDisPages = aDisPages >> gPageShift;
       
  1320 	if (r != KErrNone)
       
  1321 		{
       
  1322 		test.Printf(_L("Discardable pages not allocated r = %d\n"), r);
       
  1323 		}
       
  1324 	ResetRamZoneFlags();
       
  1325 	return r;
       
  1326 	}
       
  1327 
       
  1328 //
       
  1329 // FreeMovable
       
  1330 //
       
  1331 // Free movable pages by closing chunks. 
       
  1332 // The function will close every other chunk so that the movable pages are scattered in every zone
       
  1333 //
       
  1334 // @param aChunkArray The array that stores reference to the chunks created.
       
  1335 // @param aChunkArraySize The size of aChunkArray.
       
  1336 //
       
  1337 TInt FreeMovable(RChunk* aChunkArray, TUint aChunkArraySize)
       
  1338 	{
       
  1339 	TUint i;
       
  1340 	TInt closedChunks = 0;
       
  1341 		
       
  1342 	for (i=0; i < aChunkArraySize; i+=2) // Close every other chunk
       
  1343 		{
       
  1344 		if (aChunkArray[i].Handle() != NULL)
       
  1345 			{
       
  1346 			aChunkArray[i].Close();
       
  1347 			closedChunks ++;
       
  1348 			}
       
  1349 		}
       
  1350 	UpdateRamInfo();
       
  1351 	test.Printf(_L("Free RAM after closing %d chunks = 0x%x\n"),closedChunks,gFreeRam);
       
  1352 	return KErrNone;
       
  1353 	}
       
  1354 
       
  1355 //
       
  1356 // GetBestZone
       
  1357 //
       
  1358 // Obtains the most preferable zone for allocating a specific type of page
       
  1359 //
       
  1360 // @param aPageType The page type that we are interested in
       
  1361 // @param aBestPrefIndex The index into the preference array for the zone.
       
  1362 //
       
  1363 // @return KErrNotFound if a zone cannot be found, else the zone index into gZoneUtilArray
       
  1364 //
       
  1365 TInt GetBestZone(TInt aPageType, TUint* aBestPrefIndex = NULL)
       
  1366 	{
       
  1367 	GetPrefOrder();
       
  1368 
       
  1369 
       
  1370 	switch (aPageType)
       
  1371 		{
       
  1372 		case BEST_MOVABLE:			
       
  1373 		case BEST_DISCARDABLE:
       
  1374 			{
       
  1375 			TInt bestIndex = KErrNotFound;
       
  1376 			TUint bestPrefIndex = 0;
       
  1377 			TUint curIndex = 0;
       
  1378 			TInt startIndex = KErrNotFound;
       
  1379 			TBool zoneFound = EFalse;
       
  1380 			// Find the least preferable zone that has movable or discardable pages
       
  1381 			for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
       
  1382 				{
       
  1383 				curIndex = gPrefArray[prefIndex];
       
  1384 				if (gZoneUtilArray[curIndex].iAllocMovable || gZoneUtilArray[curIndex].iAllocDiscardable)
       
  1385 					{
       
  1386 					startIndex = prefIndex;
       
  1387 					break;
       
  1388 					}
       
  1389 				}
       
  1390 			if (startIndex == KErrNotFound)
       
  1391 				return startIndex;
       
  1392 				
       
  1393 			// Work up the preference list to look for the best zone
       
  1394 			for (TInt prefIndex = startIndex; prefIndex >= 0; prefIndex--)
       
  1395 				{
       
  1396 				curIndex = gPrefArray[prefIndex];
       
  1397 				if (gZoneUtilArray[curIndex].iFreePages)
       
  1398 					{
       
  1399 					bestIndex = curIndex;
       
  1400 					bestPrefIndex = prefIndex;
       
  1401 					zoneFound = ETrue;
       
  1402 					break;
       
  1403 					}
       
  1404 				}
       
  1405 			
       
  1406 			// If the zone still isn't found, look down the preference list
       
  1407 			if (!zoneFound)
       
  1408 				{
       
  1409 				for (TUint prefIndex = startIndex; prefIndex < gZoneCount; prefIndex++)
       
  1410 					{
       
  1411 					curIndex = gPrefArray[prefIndex];
       
  1412 					if (gZoneUtilArray[curIndex].iFreePages)
       
  1413 						{
       
  1414 						bestIndex = curIndex;
       
  1415 						bestPrefIndex = prefIndex;
       
  1416 						zoneFound = ETrue;
       
  1417 						break;
       
  1418 						}
       
  1419 					}
       
  1420 				}
       
  1421 				
       
  1422 			test.Printf(_L("leastPref = %d\n"), bestIndex);
       
  1423 			if (aBestPrefIndex)
       
  1424 				*aBestPrefIndex = bestPrefIndex;
       
  1425 			return bestIndex;
       
  1426 			}
       
  1427 
       
  1428 		case BEST_FIXED:
       
  1429 			for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
       
  1430 				{
       
  1431 				TUint mostPref = gPrefArray[prefIndex];
       
  1432 				if (gZoneUtilArray[mostPref].iFreePages != 0 || 
       
  1433 					gZoneUtilArray[mostPref].iAllocMovable != 0 ||
       
  1434 					gZoneUtilArray[mostPref].iAllocDiscardable != 0)
       
  1435 					{
       
  1436 					test.Printf(_L("mostPref = %d\n"), mostPref);
       
  1437 					if (aBestPrefIndex)
       
  1438 						*aBestPrefIndex = prefIndex;
       
  1439 					return mostPref;
       
  1440 					}
       
  1441 				}
       
  1442 			break;
       
  1443 		}
       
  1444 	
       
  1445 	test.Printf(_L("Cannot find zone\n"));
       
  1446 	return KErrNotFound;
       
  1447 	}
       
  1448 
       
  1449 void GetCandList1(TInt aIndex);
       
  1450 //
       
  1451 // VerifyMovDisAlloc
       
  1452 //
       
  1453 // Checks that all movable and discardable pages are in the correct RAM zones.
       
  1454 // Should only be invoked after a general defragmentation or a general 
       
  1455 // defragmentation followed by an allocation.
       
  1456 //
       
  1457 //	NOTE - This shouldn't be used to verify RAM if a RAM specific allocation or
       
  1458 //	zone claim operation has been performed.
       
  1459 //
       
  1460 // @return KErrNone if RAM layout is good, KErrGeneral if not, KErrNotFound if no
       
  1461 // movable or discardable pages are allocated.
       
  1462 //
       
  1463 TInt VerifyMovDisAlloc()
       
  1464 	{
       
  1465 	GetPrefOrder();
       
  1466 	TInt leastInUse = KErrNotFound;
       
  1467 	TUint requiredMovDis = 0;
       
  1468 	TUint totalMorePrefInUse = 0;
       
  1469 	TBool verifySpread = ETrue;
       
  1470 	TBool prevZoneNotFull = EFalse;
       
  1471 
       
  1472 	// Determine which is the least prefable RAM zone in use and how many pages
       
  1473 	// are allocated of each type.
       
  1474 	for (TInt prefIndex = gZoneCount - 1; prefIndex >= 0; prefIndex--)
       
  1475 		{
       
  1476 		TUint index = gPrefArray[prefIndex];
       
  1477 		TUint allocMov = gZoneUtilArray[index].iAllocMovable;
       
  1478 		TUint allocDis = gZoneUtilArray[index].iAllocDiscardable;
       
  1479 		TUint allocFixed = gZoneUtilArray[index].iAllocFixed;
       
  1480 		TESTDEBUG(test.Printf(_L("pref ID 0x%x\n"), gZoneConfigArray[index].iZoneId));
       
  1481 		if (allocMov || allocDis || allocFixed)
       
  1482 			{
       
  1483 			TBool zoneNotFull = EFalse;
       
  1484 			GetCandList1(index);
       
  1485 			TUint candIndex = 0;
       
  1486 			for (; candIndex < gZoneCount; candIndex++)
       
  1487 				{
       
  1488 				TInt zoneIndexCand = gCandList1[candIndex];
       
  1489 				if (zoneIndexCand == KInvalidCandIndex)
       
  1490 					{
       
  1491 					break;
       
  1492 					}
       
  1493 				allocMov  += gZoneUtilArray[zoneIndexCand].iAllocMovable;
       
  1494 				allocDis  += gZoneUtilArray[zoneIndexCand].iAllocDiscardable;
       
  1495 				if (gZoneUtilArray[zoneIndexCand].iFreePages)
       
  1496 					{
       
  1497 					zoneNotFull = ETrue;
       
  1498 					}
       
  1499 				}
       
  1500 			prefIndex -= candIndex - 1;
       
  1501 			if (leastInUse == KErrNotFound)
       
  1502 				{// Have found least preferable RAM zone that is in use.
       
  1503 				leastInUse = index;
       
  1504 				if (allocFixed)
       
  1505 					{// The least preferable RAM zone has fixed pages in it so
       
  1506 					// RAM may be more spread out than is necessary.
       
  1507 					verifySpread = EFalse;
       
  1508 					}
       
  1509 				}
       
  1510 			else
       
  1511 				{
       
  1512 				if ((allocMov || allocDis) && prevZoneNotFull)
       
  1513 					{// The previous least preferable RAM zones were not full so shouldn't
       
  1514 					// be any movable or discardable pages in the RAM zones of this preference.
       
  1515 					test.Printf(_L("Movable or discardable pages in more preferable RAM zones unnecessarily\n"));
       
  1516 					return KErrGeneral;
       
  1517 					}
       
  1518 				prevZoneNotFull = zoneNotFull;
       
  1519 
       
  1520 				// Not the least preferable RAM zone so add to total allocatable.
       
  1521 				totalMorePrefInUse += allocMov + allocDis + gZoneUtilArray[index].iFreePages;
       
  1522 				}
       
  1523 			requiredMovDis += allocMov + allocDis;
       
  1524 			}
       
  1525 		}
       
  1526 
       
  1527 	if (leastInUse == KErrNotFound)
       
  1528 		{// No movable or discardable pages are allocated.
       
  1529 		test.Printf(_L("No in use RAM zones found????\n"));
       
  1530 		return KErrNotFound;
       
  1531 		}
       
  1532 
       
  1533 	if (totalMorePrefInUse > requiredMovDis)
       
  1534 		{// There enough allocatable pages in the RAM zones below the currently 
       
  1535 		// least preferable RAM in use.
       
  1536 		test.Printf(_L("Memory is spread out totalMorePref 0x%x required 0x%x\n"), totalMorePrefInUse, requiredMovDis);
       
  1537 		if (verifySpread)
       
  1538 			return KErrGeneral;
       
  1539 		}
       
  1540 	return KErrNone;
       
  1541 	}
       
  1542 
       
  1543 
       
  1544 //
       
  1545 // GetCandList1
       
  1546 //
       
  1547 // Populates a list of all zones that have the same preference and the same amount 
       
  1548 // of immovable pages
       
  1549 //
       
  1550 void GetCandList1(TInt aIndex)
       
  1551 	{
       
  1552 	for (TUint i=0; i<gZoneCount; i++)
       
  1553 		{
       
  1554 		gCandList1[i] = KInvalidCandIndex;
       
  1555 		}
       
  1556 
       
  1557 	TInt candListIndex = 0;
       
  1558 	GetAllPageInfo();
       
  1559 	for (TUint i=0; i<gZoneCount; i++)
       
  1560 		{
       
  1561 		if (ZonesSamePref(aIndex, i))
       
  1562 			{
       
  1563 			gCandList1[candListIndex] = i;
       
  1564 			candListIndex ++;
       
  1565 			}
       
  1566 		}
       
  1567 	}
       
  1568 
       
  1569 //
       
  1570 // GetCandList2
       
  1571 //
       
  1572 // Populates a list of all zones that have the same preference and the same amount 
       
  1573 // of immovable pages
       
  1574 //
       
  1575 void GetCandList2(TInt aIndex)
       
  1576 	{
       
  1577 	for (TUint i=0; i<gZoneCount; i++)
       
  1578 		{
       
  1579 		gCandList2[i] = KInvalidCandIndex;
       
  1580 		}
       
  1581 
       
  1582 	TInt candListIndex = 0;
       
  1583 	GetAllPageInfo();
       
  1584 	for (TUint i=0; i<gZoneCount; i++)
       
  1585 		{
       
  1586 		if (ZonesSamePref(aIndex, i))
       
  1587 			{
       
  1588 			gCandList2[candListIndex] = i;
       
  1589 			candListIndex ++;
       
  1590 			}
       
  1591 		}
       
  1592 	}
       
  1593 
       
  1594 //
       
  1595 // GetCandListFixed1
       
  1596 //
       
  1597 // Populates a list of all zones that have the same preference and the same 
       
  1598 // amount of immovable pages, it will ignore RAM zones that are full of 
       
  1599 // immovable pages as these are the only RAM zones that fixed pages can't be 
       
  1600 // allocated into.
       
  1601 //
       
  1602 void GetCandListFixed1(TInt aIndex)
       
  1603 	{
       
  1604 	for (TUint i=0; i<gZoneCount; i++)
       
  1605 		{
       
  1606 		gCandList1[i] = KInvalidCandIndex;
       
  1607 		}
       
  1608 
       
  1609 	TInt candListIndex = 0;
       
  1610 	GetAllPageInfo();
       
  1611 	for (TUint i=0; i<gZoneCount; i++)
       
  1612 		{
       
  1613 		if (ZonesSamePref(aIndex, i) &&
       
  1614 			gZoneUtilArray[i].iAllocFixed + gZoneUtilArray[i].iAllocUnknown !=
       
  1615 			gZoneConfigArray[i].iPhysPages) 
       
  1616 			{
       
  1617 			gCandList1[candListIndex] = i;
       
  1618 			candListIndex ++;
       
  1619 			}
       
  1620 		}
       
  1621 	}
       
  1622 
       
  1623 //
       
  1624 // MultiGenDefragThreadFunc
       
  1625 //
       
  1626 // Called when a general defrag is called to run at the same time as another operation
       
  1627 //
       
  1628 TInt MultiGenDefragThreadFunc(TAny* /*aPtr*/)
       
  1629 	{
       
  1630 	RRamDefragFuncTestLdd Ldd2;
       
  1631 	TInt r = Ldd2.Open();
       
  1632 	if (r != KErrNone)
       
  1633 		{
       
  1634 		RDebug::Printf("Unable to open Ldd2 in MultiGenDefragThreadFunc, r = %d\n", r);
       
  1635 		return r;
       
  1636 		}
       
  1637 
       
  1638 	RThread thisThread = RThread();
       
  1639 	thisThread.SetPriority(EPriorityLess);
       
  1640 	r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  1641 	Ldd2.Close();
       
  1642 	thisThread.Close();
       
  1643 	return r;
       
  1644 	}
       
  1645 
       
  1646 TBool gTestLoop = EFalse;
       
  1647 //
       
  1648 // MultiLoopGenDefragThreadFunc
       
  1649 //
       
  1650 // Called when a general defrag is called to run at the same time as another operation
       
  1651 //
       
  1652 TInt MultiLoopGenDefragThreadFunc(TAny* /*aPtr*/)
       
  1653 	{
       
  1654 	RRamDefragFuncTestLdd Ldd2;
       
  1655 	TInt r = Ldd2.Open();
       
  1656 	if (r != KErrNone)
       
  1657 		{
       
  1658 		RDebug::Printf("Unable to open Ldd2 in MultiLoopGenDefragThreadFunc, r = %d\n", r);
       
  1659 		return r;
       
  1660 		}
       
  1661 
       
  1662 	RThread thisThread = RThread();
       
  1663 	thisThread.SetPriority(EPriorityLess);
       
  1664 	while (gTestLoop)
       
  1665 		{
       
  1666 		r = Ldd2.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  1667 		if (r != KErrNone)
       
  1668 			goto threadExit;
       
  1669 		}
       
  1670 threadExit:
       
  1671 	Ldd2.Close();
       
  1672 	thisThread.Close();
       
  1673 	return r;
       
  1674 	}
       
  1675 
       
  1676 //
       
  1677 // MultiEmptyZoneThreadFunc
       
  1678 //
       
  1679 // Called when a zone defrag is called to run at the same time as another operation
       
  1680 //
       
  1681 TInt MultiEmptyZoneThreadFunc(TAny* zoneID)
       
  1682 	{
       
  1683 	RRamDefragFuncTestLdd Ldd2;
       
  1684 	TInt r = Ldd2.Open();
       
  1685 	if (r != KErrNone)
       
  1686 		{
       
  1687 		RDebug::Printf("Unable to open Ldd2 in MultiEmptyZoneThreadFunc, r = %d\n", r);
       
  1688 		return r;
       
  1689 		}
       
  1690 
       
  1691 	RThread thisThread = RThread();
       
  1692 	thisThread.SetPriority(EPriorityLess);
       
  1693 	r = Ldd2.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, (TUint)zoneID);
       
  1694 	Ldd2.Close();
       
  1695 	thisThread.Close();
       
  1696 	return r;
       
  1697 	}
       
  1698 
       
  1699 
       
  1700 //
       
  1701 // MultiClaimZoneThreadFunc
       
  1702 //
       
  1703 // Called when a ClaimRamZone is called to run at the same time as another operation
       
  1704 //
       
  1705 TInt MultiClaimZoneThreadFunc(TAny* zoneID)
       
  1706 	{
       
  1707 	RRamDefragFuncTestLdd Ldd2;
       
  1708 	TInt r = Ldd2.Open();
       
  1709 	if (r != KErrNone)
       
  1710 		{
       
  1711 		RDebug::Printf("Unable to open Ldd2 in MultiClaimZoneThreadFunc, r = %d\n", r);
       
  1712 		return r;
       
  1713 		}
       
  1714 
       
  1715 	RThread thisThread = RThread();
       
  1716 	thisThread.SetPriority(EPriorityLess);
       
  1717 	r = Ldd2.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, (TUint)zoneID);
       
  1718 	Ldd2.Close();
       
  1719 	thisThread.Close();
       
  1720 	return r;
       
  1721 	}
       
  1722 
       
  1723 
       
  1724 //
       
  1725 // TestAllocStrategies
       
  1726 //
       
  1727 // Verifying that pages are allocated correctly.  All tests rely on a general defragmentation occuring
       
  1728 // in TestStart() so that memory is in a defragmented state before the allocations.
       
  1729 //
       
  1730 //---------------------------------------------------------------------------------------------------------------------
       
  1731 //! @SYMTestCaseID				KBASE-t_ramdefrag-0525
       
  1732 //! @SYMTestCaseDesc			Verifying that pages are allocated correctly
       
  1733 //! @SYMTestType				CIT
       
  1734 //! @SYMPREQ					PREQ308
       
  1735 //! @SYMTestPriority			High
       
  1736 //! @SYMTestActions				
       
  1737 //! 	1.	Allocate one chunk to memory. 
       
  1738 //! 		Check which zone the chunk has been allocated to by checking the 
       
  1739 //! 		number of movable pages before and after the allocation has taken place. 
       
  1740 //!		2.	Allocate one chunk to memory that is large enough to cause an empty RAM 
       
  1741 //!			zone to be used.
       
  1742 //! 	3.	Allocate a certain number of fixed pages to memory. 
       
  1743 //! 		Check which zone the chunk has been allocated to by checking the 
       
  1744 //! 		number of fixed pages before and after the allocation has taken place. 
       
  1745 //!		4.	Allocate fixed pages when it is known that discardable pages will need 
       
  1746 //!			to be discarded for the allocation to succeed.
       
  1747 //!		5.	Allocate fixed pages when it is known that movable pages will need 
       
  1748 //!			to be moved for the allocation to succeed.
       
  1749 //! 	6.	Allocate fixed pages when it is known that movable pages will need 
       
  1750 //!			to be moved for the allocation to succeed, Determine the "best" zone for fixed pages and allocate 1 more
       
  1751 //!			than that. 
       
  1752 //!		7.	Allocate discardable pages by loading pages that are demand paged. 
       
  1753 //! 		Check which zone the memory has been allocated to by checking the number of 
       
  1754 //! 		discardable pages before and after the allocation has taken place. 
       
  1755 //! 
       
  1756 //! @SYMTestExpectedResults
       
  1757 //! 	1.	The memory has been allocated to the most preferred zone with the least amount of 
       
  1758 //! 		space accounting for the zone threshold for movable pages. 
       
  1759 //!		2.	The new RAM zone is used and the movable page are allocated into it first then the other
       
  1760 //!			more preferable RAM zones.
       
  1761 //! 	3.	The fixed pages are allocated to the most preferred zone
       
  1762 //!		4.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
       
  1763 //!		5.	The fixed pages are allocatted to the most preferred zone with free, movable or discardable pages in it.
       
  1764 //! 	6.	Extra page is placed in the next preferable to the "best"zone 
       
  1765 //!		7.	Memory is allocated to the most preferred zone with the least amount of 
       
  1766 //! 		space accounting for the zone threshold for discardable pages. 
       
  1767 //---------------------------------------------------------------------------------------------------------------------
       
  1768 TInt TestAllocStrategies()
       
  1769 	{
       
  1770 	
       
  1771 	test.Start(_L("Test1: Check which zone a movable page has been allocated to "));	
       
  1772 	TestStart(); //This will perform a general defrag which should tidy up RAM for us
       
  1773 	gChunkArray1 = new RChunk;
       
  1774 	gChunkArraySize1 = 1;
       
  1775 	TBool zoneFound = EFalse;
       
  1776 
       
  1777 	// Determine if suitable RAM zone exists for testing
       
  1778 	TInt best = GetBestZone(BEST_MOVABLE);
       
  1779 	if (best == KErrNotFound)
       
  1780 		{
       
  1781 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  1782 		}
       
  1783 	else
       
  1784 		{
       
  1785 		test.Printf(_L("best = %d\n"), best);
       
  1786 		// Populate the candidate list
       
  1787 		GetCandList2(best);
       
  1788 		// Allocate 1 movable page
       
  1789 		GetOriginalPageCount();
       
  1790 		if (AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize) != KErrNone)
       
  1791 			{
       
  1792 			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
       
  1793 			}
       
  1794 		else
       
  1795 			{
       
  1796 			GetAllPageInfo();
       
  1797 			TInt r = VerifyMovDisAlloc();
       
  1798 			// Need to check all candidates to see if page has gone into any one of them
       
  1799 			for (TUint i=0; i < gZoneCount; i++)
       
  1800 				{
       
  1801 				if (gCandList2[i] == KInvalidCandIndex)
       
  1802 					{
       
  1803 					break;
       
  1804 					}
       
  1805 				TUint zoneIndex = gCandList2[i];
       
  1806 				if (gOriginalPageCountArray[zoneIndex].iAllocMovable < gZoneUtilArray[zoneIndex].iAllocMovable)
       
  1807 					{
       
  1808 					zoneFound = ETrue;
       
  1809 					break;
       
  1810 					}
       
  1811 				}
       
  1812 
       
  1813 			if (r == KErrNone && zoneFound)
       
  1814 				{
       
  1815 				test.Printf(_L("Pass: Movable allocated to the zone expected\n"));
       
  1816 				}
       
  1817 			else
       
  1818 				{
       
  1819 				test.Printf(_L("Fail: Movable allocated to a zone that was not expected, r %d\n"), r);
       
  1820 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  1821 				TEST_FAIL;
       
  1822 				}	
       
  1823 			}
       
  1824 		}
       
  1825 	TestEnd();
       
  1826 
       
  1827 	test.Next(_L("Test2: Check that movable pages allocated from least preferable RAM zone to be used\n"));
       
  1828 	TestStart();
       
  1829 	gChunkArray1 = new RChunk;
       
  1830 	gChunkArraySize1 = 1;
       
  1831 	// Determine if suitable RAM zone exists for testing.
       
  1832 	TUint bestPrefIndex;
       
  1833 	best = GetBestZone(BEST_MOVABLE, &bestPrefIndex); 
       
  1834 	TUint morePrefIndex = bestPrefIndex - 1;
       
  1835 	TUint nextLeastPrefIndex = bestPrefIndex + 1;
       
  1836 	
       
  1837 
       
  1838 	// Check number of free pages in the more preferable zones
       
  1839 	TUint freeInMorePref = 0;
       
  1840 	for (TUint i = 0; i < bestPrefIndex; i++)
       
  1841 		{
       
  1842 		freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
       
  1843 		}
       
  1844 	
       
  1845 	test.Printf(_L("freeInMorePref = 0x%x\n"), freeInMorePref);
       
  1846 	const TUint KHeapOverhead = 5; // Room for kernel heap allocations so they don't affect the page count
       
  1847 	if (best == KErrNotFound || nextLeastPrefIndex >= gZoneCount ||
       
  1848 		(gZoneUtilArray[gPrefArray[nextLeastPrefIndex]].iFreePages < KHeapOverhead && 
       
  1849 		freeInMorePref < KHeapOverhead)||
       
  1850 		gZoneConfigArray[gPrefArray[nextLeastPrefIndex]].iPref == gZoneConfigArray[best].iPref ||
       
  1851 		gZoneConfigArray[gPrefArray[morePrefIndex]].iPref == gZoneConfigArray[best].iPref)
       
  1852 		{// No less preferable RAM zone or there are more or less preferable of
       
  1853 		// same preference so re-ordering potential makes verification too complex.
       
  1854 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  1855 		}
       
  1856 	else
       
  1857 		{
       
  1858 		// Ensure the zone is almost full as chunks will get allocated in blocks
       
  1859 		// by almost filling the best zone with fixed pages, leaving a space (e.g. 20 pages)
       
  1860 		const TUint KSpaceNeeded = 20;
       
  1861 		if (gZoneUtilArray[best].iFreePages > KSpaceNeeded)
       
  1862 			{
       
  1863 			TUint zoneID = gZoneUtilArray[best].iZoneId;
       
  1864 			TUint diffAlloc = gZoneUtilArray[best].iFreePages - KSpaceNeeded;
       
  1865 			TInt r = Ldd.ZoneAllocDiscontiguous(zoneID, diffAlloc);
       
  1866 			if (r != KErrNone)
       
  1867 				{
       
  1868 				CLEANUP(Ldd.FreeAllFixedPages());
       
  1869 				TEST_FAIL;
       
  1870 				}
       
  1871 			}
       
  1872 		
       
  1873 
       
  1874 		
       
  1875 		GetAllPageInfo();
       
  1876 		// Update the number of free pages in the more preferable zones
       
  1877 		freeInMorePref = 0;
       
  1878 		for (TUint i = 0; i < bestPrefIndex; i++)
       
  1879 			{
       
  1880 			freeInMorePref += gZoneUtilArray[gPrefArray[i]].iFreePages;
       
  1881 			}
       
  1882 		TUint origFixed = gTotalPageCount.iFixedPages;
       
  1883 
       
  1884 		GetOriginalPageCount();
       
  1885 
       
  1886 		// Allocate enough movable pages that the next least preferable RAM zone 
       
  1887 		// will need to be used.
       
  1888 		TUint movPages = gZoneUtilArray[best].iFreePages + 1;
       
  1889 		TUint movBytes = movPages << gPageShift;
       
  1890 		TInt r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes);
       
  1891 		if (r != KErrNone)
       
  1892 			{
       
  1893 			CLEANUP(Ldd.FreeAllFixedPages());
       
  1894 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  1895 			TEST_FAIL;
       
  1896 			}
       
  1897 
       
  1898 		GetAllPageInfo();
       
  1899 		TUint curFixed = gTotalPageCount.iFixedPages;
       
  1900 		TInt difFixed = curFixed - origFixed;	
       
  1901 		
       
  1902 		// If there is space in the more preferable zones then they should be allocated starting from
       
  1903 		// the "best" zone to the more preferable zones, else from the next least preferable zone to the more preferable zones
       
  1904 		TUint prefIndex;		
       
  1905 		if ((TInt)freeInMorePref > difFixed)
       
  1906 			{//No new zones should be turned on, allocation should have gone into bestPrefIndex then down towards most preferred zones
       
  1907 			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
       
  1908 			if (gOriginalPageCountArray[best].iAllocMovable >= gZoneUtilArray[best].iAllocMovable ||
       
  1909 				gOriginalPageCountArray[nextIndex].iAllocMovable < gZoneUtilArray[nextIndex].iAllocMovable)
       
  1910 				{
       
  1911 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
       
  1912 								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
       
  1913 								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
       
  1914 				CLEANUP(Ldd.FreeAllFixedPages());
       
  1915 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  1916 				TEST_FAIL;
       
  1917 				}
       
  1918 			prefIndex = morePrefIndex;
       
  1919 			}
       
  1920 		else
       
  1921 			{// If there are enough free pages in "nextIndex" to fit all movable pages allocated then the movable 
       
  1922 			// page count in "best" should stay the same, else they should be allocated between "nextIndex" and "best".
       
  1923 			TUint nextIndex = gPrefArray[nextLeastPrefIndex];
       
  1924 			test.Printf(_L("nextIndex = %d\n"), nextIndex);
       
  1925 			if (gOriginalPageCountArray[nextIndex].iAllocMovable >= gZoneUtilArray[nextIndex].iAllocMovable ||
       
  1926 				(gOriginalPageCountArray[nextIndex].iFreePages >= movPages && 
       
  1927 				gOriginalPageCountArray[best].iAllocMovable != gZoneUtilArray[best].iAllocMovable) ||
       
  1928 				(gOriginalPageCountArray[nextIndex].iFreePages < movPages && 
       
  1929 				gOriginalPageCountArray[best].iAllocMovable == gZoneUtilArray[best].iAllocMovable))
       
  1930 				{
       
  1931 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
       
  1932 								gOriginalPageCountArray[nextIndex].iFreePages, gZoneUtilArray[nextIndex].iFreePages, 
       
  1933 								gZoneUtilArray[nextIndex].iFreePages);
       
  1934 				CLEANUP(Ldd.FreeAllFixedPages());
       
  1935 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  1936 				TEST_FAIL;
       
  1937 				}
       
  1938 			prefIndex = bestPrefIndex;
       
  1939 			}
       
  1940 		
       
  1941 		// Check that movable pages have only been allocated into the more 
       
  1942 		// preferable RAM zones if the less preferable ram zones in use are full.
       
  1943 		prefIndex++;
       
  1944 		do
       
  1945 			{
       
  1946 			prefIndex--;
       
  1947 			TUint indexCurrent = gPrefArray[prefIndex];			
       
  1948 			TUint indexLessPref = gPrefArray[prefIndex+1];
       
  1949 
       
  1950 			if (gOriginalPageCountArray[indexCurrent].iAllocMovable < gZoneUtilArray[indexCurrent].iAllocMovable &&
       
  1951 				gZoneUtilArray[indexLessPref].iFreePages)
       
  1952 				{// Current in use zone or less preferable than current has free pages so fail
       
  1953 				test.Printf(_L("Movable page allocated incorrectly origFree 0x%x curFree 0x%x nxtPrefFree 0x%x\n"), 
       
  1954 								gOriginalPageCountArray[best].iFreePages, gZoneUtilArray[best].iFreePages, 
       
  1955 								gZoneUtilArray[nextLeastPrefIndex].iFreePages);
       
  1956 				CLEANUP(Ldd.FreeAllFixedPages());
       
  1957 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  1958 				TEST_FAIL;
       
  1959 				}
       
  1960 			}
       
  1961 		while(prefIndex);
       
  1962 		test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
       
  1963 		}
       
  1964 	TestEnd();
       
  1965 
       
  1966 	test.Next(_L("Test3: Check which zone a fixed page has been allocated to "));	
       
  1967 	for (TUint testStep = 0; testStep < 2; testStep++)
       
  1968 		{
       
  1969 		switch (testStep)
       
  1970 			{
       
  1971 			case 0:
       
  1972 				test.Printf(_L("Testing discontiguous allocations\n"));
       
  1973 				break;
       
  1974 				
       
  1975 			case 1:
       
  1976 				test.Printf(_L("Testing contiguous allocations\n"));
       
  1977 				break;
       
  1978 			}
       
  1979 		TestStart();
       
  1980 		zoneFound = EFalse;
       
  1981 		GetOriginalPageCount();
       
  1982 		
       
  1983 		best = GetBestZone(BEST_FIXED);
       
  1984 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
       
  1985 		if (best == KErrNotFound)
       
  1986 			{
       
  1987 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  1988 			}
       
  1989 		else
       
  1990 			{
       
  1991 			GetCandList1(best);
       
  1992 			TInt allocFixedPages = 1; // Try and allocate just 1 fixed page
       
  1993 			switch (testStep)
       
  1994 				{
       
  1995 				case 0:
       
  1996 					Ldd.AllocateFixed(allocFixedPages); 
       
  1997 					break;
       
  1998 					
       
  1999 				case 1:
       
  2000 					TUint allocFixedBytes = allocFixedPages << gPageShift;
       
  2001 					Ldd.AllocContiguous(allocFixedBytes); 
       
  2002 					break;
       
  2003 				}
       
  2004 			
       
  2005 			
       
  2006 			GetAllPageInfo();
       
  2007 			for (TUint i=0; i<gZoneCount; i++)
       
  2008 				{
       
  2009 				if (gCandList1[i] == KInvalidCandIndex)
       
  2010 					{
       
  2011 					break;
       
  2012 					}
       
  2013 				TUint zoneIndex = gCandList1[i];
       
  2014 				if (gOriginalPageCountArray[zoneIndex].iAllocFixed + allocFixedPages <= gZoneUtilArray[zoneIndex].iAllocFixed)
       
  2015 					{
       
  2016 					zoneFound = ETrue;
       
  2017 					break;
       
  2018 					}
       
  2019 				}
       
  2020 
       
  2021 			if (zoneFound)
       
  2022 				{
       
  2023 				test.Printf(_L("Pass: Chunk has been allocated to the zone expected\n"));
       
  2024 				}
       
  2025 			else
       
  2026 				{
       
  2027 				test.Printf(_L("Fail: Fixed been allocated to a zone that was not expected\n"));
       
  2028 				CLEANUP(Ldd.FreeAllFixedPages());
       
  2029 				TEST_FAIL;
       
  2030 				}	
       
  2031 			
       
  2032 			Ldd.FreeAllFixedPages();
       
  2033 			}
       
  2034 		TestEnd();
       
  2035 		}
       
  2036 
       
  2037 	test.Next(_L("Test4: Check fixed page allocations will discard pages"));
       
  2038 	for (TUint testStep = 0; testStep < 2; testStep++)
       
  2039 		{
       
  2040 		switch (testStep)
       
  2041 			{
       
  2042 			case 0:
       
  2043 				test.Printf(_L("Testing discontiguous allocations\n"));
       
  2044 				break;
       
  2045 				
       
  2046 			case 1:
       
  2047 				test.Printf(_L("Testing contiguous allocations\n"));
       
  2048 				break;
       
  2049 			}
       
  2050 		TestStart();
       
  2051 		TInt discardBytes;
       
  2052 		TInt r;
       
  2053 
       
  2054 		if (!gPagedRom)
       
  2055 			{
       
  2056 			test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  2057 			goto SkipTest4;
       
  2058 			}
       
  2059 
       
  2060 		best = GetBestZone(BEST_FIXED);
       
  2061 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
       
  2062 		if (best == KErrNotFound)
       
  2063 			{
       
  2064 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  2065 			goto SkipTest4;
       
  2066 			}
       
  2067 		// Ensure discardable pages in the preferred RAM zone.
       
  2068 		r = AllocDiscardable(discardBytes);
       
  2069 		if (r != KErrNone)
       
  2070 			{
       
  2071 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2072 			TEST_FAIL;
       
  2073 			}
       
  2074 		if (discardBytes < gPageSize)
       
  2075 			{// Can't discard any pages so test can't run.
       
  2076 			test.Printf(_L("Memory too full to perform test - Skipping...\n"));
       
  2077 			goto SkipTest4;
       
  2078 			}
       
  2079 
       
  2080 		// Make sure all RAM zones that the fixed page allocation could potentially 
       
  2081 		// go to have discardable pages allocated in it.
       
  2082 		GetOriginalPageCount();
       
  2083 		GetCandListFixed1(best);
       
  2084 		for (TUint i = 0; i < gZoneCount; i++)
       
  2085 			{
       
  2086 			if (gCandList1[i] == KInvalidCandIndex)
       
  2087 				{
       
  2088 				break;
       
  2089 				}
       
  2090 			TUint zoneIndex = gCandList1[i];
       
  2091 			if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable == 0)
       
  2092 				{
       
  2093 				test.Printf(_L("No dicardable pages in one of the candidates RAM zones - Skipping...\n"));
       
  2094 				goto SkipTest4;
       
  2095 				}
       
  2096 			if (gOriginalPageCountArray[zoneIndex].iFreePages != 0)
       
  2097 				{
       
  2098 				test.Printf(_L("Some free pages in candidate RAM zone ID%x\n"), 
       
  2099 							gZoneConfigArray[zoneIndex].iZoneId);
       
  2100 				CLEANUP(ResetDPCache());
       
  2101 				TEST_FAIL;
       
  2102 				}
       
  2103 			}
       
  2104 
       
  2105 		// Allocate 1 fixed page and ensure that it discards a page.
       
  2106 		switch (testStep)
       
  2107 			{
       
  2108 			case 0:
       
  2109 				r = Ldd.AllocateFixed(1); 
       
  2110 				break;
       
  2111 				
       
  2112 			case 1:
       
  2113 				r = Ldd.AllocContiguous(gPageSize); 
       
  2114 				break;
       
  2115 			}
       
  2116 
       
  2117 		// Find RAM zone the fixed page was allocated into.
       
  2118 		GetAllPageInfo();
       
  2119 		zoneFound = EFalse;
       
  2120 		for (TUint i = 0; i < gZoneCount; i++)
       
  2121 			{
       
  2122 			if (gCandList1[i] == KInvalidCandIndex)
       
  2123 				{
       
  2124 				break;
       
  2125 				}
       
  2126 			TUint zoneIndex = gCandList1[i];
       
  2127 			if (gOriginalPageCountArray[zoneIndex].iAllocFixed < gZoneUtilArray[zoneIndex].iAllocFixed)
       
  2128 				{
       
  2129 				zoneFound = ETrue;
       
  2130 				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable <= gZoneUtilArray[zoneIndex].iAllocDiscardable &&
       
  2131 					gOriginalPageCountArray[zoneIndex].iAllocMovable <= gZoneUtilArray[zoneIndex].iAllocMovable)
       
  2132 					{
       
  2133 					test.Printf(_L("Fixed pages allocated but no pages discarded in RAM zone ID 0x%x\n"), 
       
  2134 						gZoneConfigArray[zoneIndex].iZoneId);
       
  2135 					CLEANUP(ResetDPCache());
       
  2136 					TEST_FAIL;
       
  2137 					}
       
  2138 				}
       
  2139 			}
       
  2140 
       
  2141 		if (!zoneFound || r != KErrNone)
       
  2142 			{
       
  2143 			test.Printf(_L("No fixed pages were allocated r = %d\n"), r);
       
  2144 			CLEANUP(ResetDPCache());
       
  2145 			TEST_FAIL;
       
  2146 			}
       
  2147 		else
       
  2148 			{
       
  2149 			test.Printf(_L("Pass: Pages been allocated to the zone expected\n"));
       
  2150 			}
       
  2151 	SkipTest4 :
       
  2152 		// This will free any DP cache pages and fixed pages allocated.
       
  2153 		TestEnd();
       
  2154 		}
       
  2155 
       
  2156 
       
  2157 	for (TUint testIndex = 0; testIndex < 2; testIndex++)
       
  2158 		{
       
  2159 		switch (testIndex)
       
  2160 			{
       
  2161 			case 0:
       
  2162 				test.Next(_L("Test5: Check fixed page allocations (discontiguous) will move pages"));
       
  2163 				break;
       
  2164 
       
  2165 			case 1:
       
  2166 				test.Next(_L("Test6: Check fixed page will only go into a new zone if all other fix page zones are full of fix"));
       
  2167 				break;
       
  2168 			}
       
  2169 
       
  2170 		TestStart();
       
  2171 		gChunkArray1 = new RChunk;
       
  2172 		gChunkArraySize1 = 1;
       
  2173 		TInt r = KErrNone;
       
  2174 		TUint freeInOtherZones = 0;
       
  2175 		TUint allocatablePages;
       
  2176 		TUint allocImmovPages;
       
  2177 		TUint bestPrefIndex;
       
  2178 		TUint nextBestIndex = 0;
       
  2179 		const TUint KMovAllocOverhead = 5;	// need pages for page tables and kernel heap expansion.
       
  2180 		
       
  2181 		best = GetBestZone(BEST_FIXED, &bestPrefIndex);
       
  2182 		TESTDEBUG(test.Printf(_L("best = %d\n"), best));
       
  2183 		GetCandListFixed1(best);
       
  2184 		TUint candidates = 0;
       
  2185 		for (TUint i = 0; i < gZoneCount; i++)
       
  2186 			{
       
  2187 			if (gCandList1[i] == KInvalidCandIndex)
       
  2188 				{
       
  2189 				break;
       
  2190 				}
       
  2191 			candidates++;
       
  2192 			}
       
  2193 		if (best == KErrNotFound || 
       
  2194 			(gZoneUtilArray[best].iAllocMovable == 0 && gZoneUtilArray[best].iFreePages < KMovAllocOverhead) ||
       
  2195 			candidates != 1)
       
  2196 			{
       
  2197 			test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
       
  2198 			goto SkipTest5;
       
  2199 			}
       
  2200 
       
  2201 		if (testIndex == 1)
       
  2202 			{// need to work out what the next best zone would be
       
  2203 			GetPrefOrder();
       
  2204 			if (bestPrefIndex + 1 >= gZoneCount)
       
  2205 				{
       
  2206 				test.Printf(_L("Cannot find next best zone - Skipping...\n"));
       
  2207 				goto SkipTest5;
       
  2208 				}
       
  2209 			nextBestIndex = gPrefArray[bestPrefIndex + 1];
       
  2210 			test.Printf(_L("nextBestIndex= %d\n"), nextBestIndex);
       
  2211 			GetCandListFixed1(nextBestIndex);
       
  2212 			candidates = 0;
       
  2213 			for (TUint i = 0; i < gZoneCount; i++)
       
  2214 				{
       
  2215 				if (gCandList1[i] == KInvalidCandIndex)
       
  2216 					{
       
  2217 					break;
       
  2218 					}
       
  2219 				candidates++;
       
  2220 				}
       
  2221 			if (gZoneUtilArray[nextBestIndex].iPhysPages == gZoneUtilArray[nextBestIndex].iAllocFixed + 
       
  2222 															gZoneUtilArray[nextBestIndex].iAllocUnknown ||
       
  2223 				candidates != 1)
       
  2224 				{
       
  2225 				test.Printf(_L("Cannot find zone or too many equal pref zones to perform test - Skipping...\n"));
       
  2226 				goto SkipTest5;
       
  2227 				}
       
  2228 			}
       
  2229 
       
  2230 
       
  2231 
       
  2232 		for (TUint i = 0; i < gZoneCount; i++)
       
  2233 			{
       
  2234 			if (i != (TUint)best)
       
  2235 				{
       
  2236 				freeInOtherZones += gZoneUtilArray[i].iFreePages;
       
  2237 				}
       
  2238 			}
       
  2239 		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
       
  2240 							gZoneUtilArray[best].iAllocMovable + 
       
  2241 							gZoneUtilArray[best].iAllocDiscardable;
       
  2242 		
       
  2243 		if (allocatablePages > freeInOtherZones)
       
  2244 			{
       
  2245 			test.Printf(_L("Not enough free RAM for test - Skipping...\n"));
       
  2246 			goto SkipTest5;
       
  2247 			}
       
  2248 		
       
  2249 		// Allocate the fixed array before getting any page counts
       
  2250 		r = Ldd.AllocFixedArray(allocatablePages + 1);
       
  2251 		if (r != KErrNone)
       
  2252 			{
       
  2253 			test.Printf(_L("Failed to allocate fixed array r = %d - Skipping...\n"), r);
       
  2254 			goto SkipTest5;
       
  2255 			}
       
  2256 		
       
  2257 		// Fill the RAM zone with movable pages if none already in it.
       
  2258 		GetAllPageInfo();
       
  2259 		if (gZoneUtilArray[best].iAllocMovable == 0)
       
  2260 			{
       
  2261 			if (!gZoneUtilArray[best].iFreePages)
       
  2262 				{
       
  2263 				test.Printf(_L("RAM zone ID %x too full for test - Skipping...\n"), gZoneConfigArray[best].iZoneId);
       
  2264 				goto SkipTest5;
       
  2265 				}
       
  2266 			// Fill the zone with movable pages
       
  2267 			r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, best);
       
  2268 			if (r != KErrNone)
       
  2269 				{
       
  2270 				test.Printf(_L("Failed to fill zone index %d with movable r = %d\n"), best, r);
       
  2271 				CLEANUP(ResetRamZoneFlags());
       
  2272 				TEST_FAIL;
       
  2273 				}
       
  2274 			
       
  2275 			if (GetBestZone(BEST_FIXED) != best)
       
  2276 				{
       
  2277 				test.Printf(_L("Unable to complete test; RAM zone reordering - Skipping...\n"));
       
  2278 				goto SkipTest5;
       
  2279 				}
       
  2280 			}	
       
  2281 		// Allocate fixed pages after reseting the allocation flags.
       
  2282 		GetAllPageInfo();
       
  2283 		ResetRamZoneFlags();
       
  2284 		allocatablePages = 	gZoneUtilArray[best].iFreePages + 
       
  2285 							gZoneUtilArray[best].iAllocMovable + 
       
  2286 							gZoneUtilArray[best].iAllocDiscardable;
       
  2287 		switch (testIndex)
       
  2288 			{
       
  2289 			case 0:
       
  2290 				r = Ldd.AllocateFixed2(allocatablePages);
       
  2291 				GetAllPageInfo();
       
  2292 				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
       
  2293 				if (r != KErrNone || gZoneConfigArray[best].iPhysPages != allocImmovPages)
       
  2294 					{
       
  2295 					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages\n"), gZoneConfigArray[best].iZoneId);
       
  2296 					CLEANUP(ResetRamZoneFlags());
       
  2297 					TEST_FAIL;
       
  2298 					}
       
  2299 				else
       
  2300 					{
       
  2301 					test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
       
  2302 					}
       
  2303 				break;
       
  2304 
       
  2305 			case 1:
       
  2306 				GetOriginalPageCount();
       
  2307 				r = Ldd.AllocateFixed2(allocatablePages + 1);
       
  2308 				GetAllPageInfo();
       
  2309 				allocImmovPages = gZoneUtilArray[best].iAllocFixed + gZoneUtilArray[best].iAllocUnknown;
       
  2310 				if (r != KErrNone || gZoneUtilArray[best].iPhysPages != allocImmovPages ||
       
  2311 					gZoneUtilArray[nextBestIndex].iAllocFixed <= gOriginalPageCountArray[nextBestIndex].iAllocFixed)
       
  2312 					{
       
  2313 					test.Printf(_L("RAM zone ID 0x%x not full of immovable pages or next best ID 0x%x no fix pages\n"), 
       
  2314 									gZoneConfigArray[best].iZoneId, gZoneConfigArray[nextBestIndex].iZoneId);
       
  2315 					test.Printf(_L("nextBest %d origFix 0x%x curFix 0x%x\n"), 
       
  2316 									nextBestIndex, gOriginalPageCountArray[nextBestIndex].iAllocFixed, 
       
  2317 									gZoneUtilArray[nextBestIndex].iAllocFixed);
       
  2318 					CLEANUP(ResetRamZoneFlags());
       
  2319 					TEST_FAIL;
       
  2320 					}
       
  2321 				// Go through every other zone and check that fixed pages haven't increased
       
  2322 				for (TUint i = 0; i < gZoneCount; i++)
       
  2323 					{
       
  2324 					if (i != (TUint)best && i != (TUint)nextBestIndex &&
       
  2325 						gZoneUtilArray[i].iAllocFixed > gOriginalPageCountArray[i].iAllocFixed)
       
  2326 						{
       
  2327 						test.Printf(_L("FAIL: Fix page count increased zoneIndex %d orig 0x%x current 0x%x\n"), 
       
  2328 										i, gOriginalPageCountArray[i].iAllocFixed, gZoneUtilArray[i].iAllocFixed);
       
  2329 						CLEANUP(ResetRamZoneFlags());
       
  2330 						TEST_FAIL;
       
  2331 						}
       
  2332 					}
       
  2333 
       
  2334 				test.Printf(_L("Pass: Pages allocated to the zone expected\n"));
       
  2335 				break;
       
  2336 			}
       
  2337 	SkipTest5 :
       
  2338 		// This will perform any required clean up.
       
  2339 		ResetRamZoneFlags();
       
  2340 		TestEnd();
       
  2341 	}
       
  2342 
       
  2343 	test.Next(_L("Test7: Check which zone a discardable page has been allocated to"));	
       
  2344 	TestStart();
       
  2345 	if (gPagedRom)
       
  2346 		{
       
  2347 		GetAllPageInfo();
       
  2348 
       
  2349 		// Try to allocate just one more discardable page
       
  2350 		TUint allocBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
       
  2351 		TInt discardablePages;
       
  2352 
       
  2353 		best = GetBestZone(BEST_DISCARDABLE);
       
  2354 		if (best == KErrNotFound)
       
  2355 			{
       
  2356 			test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  2357 			}
       
  2358 		else
       
  2359 			{
       
  2360 			// Populate the candidate list
       
  2361 			GetCandList2(best);
       
  2362 			zoneFound = EFalse;
       
  2363 			GetOriginalPageCount();
       
  2364 			TInt r = AllocDiscardable(discardablePages, allocBytes);
       
  2365 			if (r != KErrNone)
       
  2366 				{
       
  2367 				test.Printf(_L("Fail: r %d\n"), r);
       
  2368 				CLEANUP(ResetDPCache());
       
  2369 				TEST_FAIL;
       
  2370 				}
       
  2371 
       
  2372 			r = VerifyMovDisAlloc();
       
  2373 			// Need to check all candidates to see if page has gone into any one of them
       
  2374 			for (TUint i=0; i < gZoneCount; i++)
       
  2375 				{
       
  2376 				if (gCandList2[i] == KInvalidCandIndex)
       
  2377 					{
       
  2378 					break;
       
  2379 					}
       
  2380 				TUint zoneIndex = gCandList2[i];
       
  2381 				if (gOriginalPageCountArray[zoneIndex].iAllocDiscardable < gZoneUtilArray[zoneIndex].iAllocDiscardable)
       
  2382 					{
       
  2383 					zoneFound = ETrue;
       
  2384 					break;
       
  2385 					}
       
  2386 				}
       
  2387 
       
  2388 			if (r == KErrNone && zoneFound)
       
  2389 				{
       
  2390 				test.Printf(_L("Pass: Discardable allocated to the zone expected\n"));
       
  2391 				}
       
  2392 			else
       
  2393 				{
       
  2394 				test.Printf(_L("Fail: Discardable been allocated to a zone that was not expected r %d\n"), r);
       
  2395 				CLEANUP(ResetDPCache());
       
  2396 				TEST_FAIL;
       
  2397 				}	
       
  2398 			}
       
  2399 		}
       
  2400 	else
       
  2401 		{
       
  2402 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  2403 		}
       
  2404 	TestEnd();
       
  2405 
       
  2406 	test.Next(_L("Test8: Check fixed page allocations (contiguous) will move pages"));
       
  2407 	TestStart();
       
  2408 	gChunkArray1 = new RChunk;
       
  2409 	gChunkArraySize1 = 1;
       
  2410 	
       
  2411 	TInt r = KErrNone;
       
  2412 	TUint testZoneIndex = 0;
       
  2413 
       
  2414 	// Get the most pref zone which is completely free to use as a test zone
       
  2415 	r = FindMostPrefEmpty(testZoneIndex);
       
  2416 	if (r != KErrNone)
       
  2417 		{
       
  2418 		test.Printf(_L("Cannot find empty zone - Skipping...\n"));
       
  2419 		goto skipTest8;
       
  2420 		}
       
  2421 
       
  2422 		
       
  2423 	// fill the test zone with movable pages
       
  2424 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex);
       
  2425 	GetAllPageInfo();
       
  2426 	if (r != KErrNone ||
       
  2427 		gZoneUtilArray[testZoneIndex].iAllocMovable != gZoneUtilArray[testZoneIndex].iPhysPages)
       
  2428 		{
       
  2429 		test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
       
  2430 		goto skipTest8;
       
  2431 		}
       
  2432 
       
  2433 	if (gTotalPageCount.iFreePages < 1)
       
  2434 		{
       
  2435 		test.Printf(_L("Insufficient memory - totalFreePages = 0x%x - Skipping...\n"), gTotalPageCount.iFreePages);
       
  2436 		goto skipTest8;
       
  2437 		}
       
  2438 
       
  2439 	for (TUint zoneIndex = 0; zoneIndex < gZoneCount; zoneIndex++)
       
  2440 		{
       
  2441 		TUint zoneId = gZoneConfigArray[zoneIndex].iZoneId;
       
  2442 		if (zoneIndex != testZoneIndex)
       
  2443 			{
       
  2444 			r = Ldd.SetZoneFlag(zoneId, gZoneConfigArray[zoneIndex].iFlags, NO_FIXED_FLAG);
       
  2445 			if (r != KErrNone)
       
  2446 				{
       
  2447 				test.Printf(_L("Failed to set flag on zone index %d r = %d- Skipping...\n"), zoneIndex, r);
       
  2448 				goto skipTest8;
       
  2449 				}
       
  2450 			}
       
  2451 		}
       
  2452 	
       
  2453 	//attempt to alloc 1 contiguous fixed page
       
  2454 	r = Ldd.AllocContiguous(gPageSize);
       
  2455 	GetAllPageInfo();
       
  2456 	if (r != KErrNone ||
       
  2457 		!gZoneUtilArray[testZoneIndex].iAllocFixed)
       
  2458 		{
       
  2459 		test.Printf(_L("FAIL: no fixed pages in testZoneIndex(%d) r = %d\n"), testZoneIndex, r);
       
  2460 		CLEANUP(Ldd.FreeAllFixedPages());
       
  2461 		TEST_FAIL;
       
  2462 		}
       
  2463 	else
       
  2464 		{
       
  2465 		test.Printf(_L("Passed...\n"));
       
  2466 		}
       
  2467 	
       
  2468 skipTest8:
       
  2469 	TestEnd();
       
  2470 
       
  2471 	test.End();
       
  2472 	return KErrNone;
       
  2473 	}
       
  2474 
       
  2475 	
       
  2476 //
       
  2477 // TestMovingPages
       
  2478 //
       
  2479 //---------------------------------------------------------------------------------------------------------------------
       
  2480 //! @SYMTestCaseID				KBASE-t_ramdefrag-0526
       
  2481 //! @SYMTestType				CIT
       
  2482 //! @SYMTestCaseDesc			Verifying that pages are moved correctly
       
  2483 //! @SYMPREQ					PREQ308
       
  2484 //! @SYMTestPriority			High
       
  2485 //! @SYMTestActions				
       
  2486 //! 	1.	Fragment the memory. Write to all chunks apart from chunk 9. 
       
  2487 //! 		Following this start a RAM defrag and whilst defrag is running write to chunk 9. 
       
  2488 //! 	2.	Fragment the memory. Start a RAM defrag and whilst defrag is running 
       
  2489 //! 		continuously write different values to the chunks. 
       
  2490 //! 	3.	Fragment the memory. Following this start a RAM defrag and whilst this 
       
  2491 //! 		is happening, continuously read from all chunks. 
       
  2492 //! 	4.	Allocate some movable pages. Call a device driver that will allocate fixed 
       
  2493 //! 		pages and write values to these fixed pages. 
       
  2494 //! 		Close every other chunk so that only 5 chunks are now still open. 
       
  2495 //! 		Following this perform a RAM defrag. 
       
  2496 //! 		Read from the fixed pages that were originally written to, ensuring 
       
  2497 //! 		that they have not changed.   
       
  2498 //! 	5.	Without starting any processes, allocate discardable pages by loading 
       
  2499 //! 		pages that are demand paged. 
       
  2500 //! 		Read each of the constants from beginning to end. 
       
  2501 //! 		Following this perform a RAM defrag. 
       
  2502 //! 
       
  2503 //! @SYMTestExpectedResults
       
  2504 //! 	1.	Zones are emptied
       
  2505 //! 	2.	Zones are emptied	
       
  2506 //! 	3.	Zones are emptied
       
  2507 //! 	4.	The values written to the fixed pages have not changed. 
       
  2508 //! 	5.	Zones are emptied
       
  2509 //---------------------------------------------------------------------------------------------------------------------
       
  2510 TInt TestMovingPages()
       
  2511 	{
       
  2512 	const TInt KAllChunks = -1;  // Specifies that all chunks should be written to
       
  2513 	
       
  2514 	test.Start(_L("Test1: Whilst moving page, change the usage "));	
       
  2515 	TestStart();	
       
  2516 	
       
  2517 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
       
  2518 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2519 
       
  2520 	// Find a chunk that exists
       
  2521 	TInt chunkIndex = gChunkArraySize1 - 1;
       
  2522 	for (; chunkIndex >= 0 && gChunkArray1[chunkIndex].Handle() == NULL; chunkIndex--);
       
  2523 	if (chunkIndex < 0)
       
  2524 		{
       
  2525 		test.Printf(_L("No chunks were allocated\n"));
       
  2526 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2527 		TEST_FAIL;
       
  2528 		}
       
  2529 
       
  2530 	WriteToChunk(gChunkArray1, gChunkArraySize1, chunkIndex);
       
  2531 
       
  2532 	TInt r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  2533 	if (r != KErrNone)
       
  2534 		{
       
  2535 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2536 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2537 		TEST_FAIL;
       
  2538 		}	
       
  2539 	gTestThread.Logon(status);
       
  2540 	gTestThread.Resume();	
       
  2541 
       
  2542 	TUint8* base = gChunkArray1[chunkIndex].Base();
       
  2543 	while (status.Int() == KRequestPending)
       
  2544 		{
       
  2545 		User::After(10000);
       
  2546 		for (TInt8 k = 0; k < 10; k ++)
       
  2547 			{
       
  2548 			if (base == gChunkArray1[chunkIndex].Base() + gChunkArray1[chunkIndex].Size())
       
  2549 				{
       
  2550 				base = gChunkArray1[chunkIndex].Base();
       
  2551 				}
       
  2552 			*base++ = k; // write 0 - 9 to the chunk
       
  2553 			}
       
  2554 		}
       
  2555 	
       
  2556 	User::WaitForRequest(status);
       
  2557 	r = status.Int();
       
  2558 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
       
  2559 
       
  2560 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2561 
       
  2562 	if (r == KErrNone)
       
  2563 		{
       
  2564 		test.Printf(_L("Correct return value\n"));
       
  2565 		test.Printf(_L("Passed...\n"));
       
  2566 		}
       
  2567 	else
       
  2568 		{
       
  2569 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  2570 		TEST_FAIL;	
       
  2571 		}
       
  2572 		
       
  2573 	gTestThread.Close();
       
  2574 	TestEnd();
       
  2575 	
       
  2576 	
       
  2577 	test.Next(_L("Test2: Whilst moving page, change the contents of the page"));	
       
  2578 	TestStart();	
       
  2579 		
       
  2580 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
       
  2581 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2582 	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
       
  2583 	
       
  2584 	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  2585 	if (r != KErrNone)
       
  2586 		{
       
  2587 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2588 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2589 		TEST_FAIL;
       
  2590 		}	
       
  2591 	gTestThread.Logon(status);
       
  2592 	gTestThread.Resume();
       
  2593 
       
  2594 	TUint8 startValue = 0;
       
  2595 	while (status.Int() == KRequestPending)
       
  2596 		{
       
  2597 		User::After(10000);
       
  2598 		WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks, startValue);
       
  2599 		if (++startValue > 245)
       
  2600 			{
       
  2601 			startValue = 0;
       
  2602 			}
       
  2603 		}
       
  2604 		
       
  2605 	User::WaitForRequest(status);
       
  2606 	r = status.Int();
       
  2607 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
       
  2608 	
       
  2609 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2610 
       
  2611 	if (r == KErrNone)
       
  2612 		{
       
  2613 		test.Printf(_L("Correct return value\n"));
       
  2614 		test.Printf(_L("Passed...\n"));	
       
  2615 		}
       
  2616 	else
       
  2617 		{
       
  2618 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  2619 		TEST_FAIL;	
       
  2620 		}
       
  2621 
       
  2622 	gTestThread.Close();
       
  2623 	TestEnd();
       
  2624 	
       
  2625 
       
  2626 	test.Next(_L("Test3: Whilst moving page, read pages"));	
       
  2627 	TestStart();	
       
  2628 	
       
  2629 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks, KChunkDefaultSize);
       
  2630 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2631 	WriteToChunk(gChunkArray1, gChunkArraySize1, KAllChunks);
       
  2632 	
       
  2633 	r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  2634 	if (r != KErrNone)
       
  2635 		{
       
  2636 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2637 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2638 		TEST_FAIL;
       
  2639 		}	
       
  2640 	gTestThread.Logon(status);
       
  2641 	gTestThread.Resume();
       
  2642 
       
  2643 	while (status.Int() == KRequestPending)
       
  2644 		{
       
  2645 		User::After(100000000);
       
  2646 		ReadChunk(gChunkArray1, gChunkArraySize1);
       
  2647 		}		
       
  2648 	
       
  2649 	User::WaitForRequest(status);
       
  2650 	r = status.Int();
       
  2651 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
       
  2652 
       
  2653 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2654 
       
  2655 	if (r == KErrNone)
       
  2656 		{
       
  2657 		test.Printf(_L("Correct return value\n"));
       
  2658 		test.Printf(_L("Passed...\n"));	
       
  2659 		}
       
  2660 	else
       
  2661 		{
       
  2662 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2663 		TEST_FAIL;	
       
  2664 		}
       
  2665 		
       
  2666 	gTestThread.Close();
       
  2667 	TestEnd();
       
  2668 
       
  2669 	test.Next(_L("Test4: Allocate fixed pages and then perform a defrag"));	
       
  2670 	TestStart();	
       
  2671 	
       
  2672 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  2673 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2674 	
       
  2675 	Ldd.AllocateFixedWrite(FILL_ALL_FIXED);
       
  2676 
       
  2677 	GetOriginalPageCount();
       
  2678 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2679 	test.Printf(_L("defrag returns %d\n"), r);
       
  2680 
       
  2681 	if (CheckZonesSwitchedOff() != EFalse)
       
  2682 		{
       
  2683 		test.Printf(_L("Fail: Zones were switched off when they shouldn't have been\n"));
       
  2684 		CLEANUP(r = Ldd.FreeAllFixedPagesRead());
       
  2685 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2686 		TEST_FAIL;
       
  2687 		}
       
  2688 	else
       
  2689 		{
       
  2690 		test.Printf(_L("Passed...\n"));	
       
  2691 		}
       
  2692 	r = Ldd.FreeAllFixedPagesRead();
       
  2693 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
       
  2694 	TestEnd();
       
  2695 	
       
  2696 	test.Next(_L("Test5: Allocate discardable pages and then perform a defrag"));	
       
  2697 	TestStart();	
       
  2698 	if (gPagedRom)
       
  2699 		{
       
  2700 		TInt discardablePages;
       
  2701 				
       
  2702 		UpdateRamInfo();
       
  2703 		r = AllocDiscardable(discardablePages);
       
  2704 		if (r != KErrNone)
       
  2705 			{
       
  2706 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2707 			TEST_FAIL;
       
  2708 			}
       
  2709 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
       
  2710 
       
  2711 		GetOriginalPageCount();
       
  2712 		TBool genSucceed = CanGenSucceed();
       
  2713 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2714 
       
  2715 		TESTDEBUG(test.Printf(_L("defrag returns %d\n"), r));
       
  2716 		
       
  2717 		if (genSucceed && CheckZonesSwitchedOff() == EFalse)
       
  2718 			{
       
  2719 			test.Printf(_L("Fail: Zones were not switched off when they should have been\n"));
       
  2720 			CLEANUP(ResetDPCache());
       
  2721 			TEST_FAIL;
       
  2722 			}
       
  2723 		else
       
  2724 			{
       
  2725 			test.Printf(_L("Passed...\n"));	
       
  2726 			}
       
  2727 		ResetDPCache();
       
  2728 		
       
  2729 		}
       
  2730 	else
       
  2731 		{
       
  2732 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  2733 		}
       
  2734 	TestEnd();
       
  2735 		
       
  2736 	test.End();
       
  2737 	return 0;
       
  2738 	}
       
  2739 		
       
  2740 
       
  2741 //
       
  2742 // TestMovPgsDefrag
       
  2743 //
       
  2744 //---------------------------------------------------------------------------------------------------------------------
       
  2745 //! @SYMTestCaseID				KBASE-t_ramdefrag-0527
       
  2746 //! @SYMTestType				CIT
       
  2747 //! @SYMTestCaseDesc			Verifying the moving of pages in the defrag implmentation
       
  2748 //! @SYMPREQ					PREQ308
       
  2749 //! @SYMTestPriority			High
       
  2750 //! @SYMTestActions				
       
  2751 //! 	1.	Fragment the memory. Following this perform a RAM defrag. 
       
  2752 //! 		Now that all movable memory has now been moved and the relevant source
       
  2753 //! 		zones have been switched off, again attempt to perform a RAM defrag. 
       
  2754 //! 	2.	Fragment the memory. Following this perform a RAM defrag and whilst 
       
  2755 //! 		this is happening, continuously create more chunks. 
       
  2756 //!		3.	Set up memory so that there are a mix of movable and free pages in a low preference zone, with there
       
  2757 //!			being movable pages in the higher preference zones. Following this call a general defrag
       
  2758 //!		4.	Set up memory so that there are a mix of movable and free pages and a couple of free pages 
       
  2759 //!			in a low preference zone, with there being enough free pages in the higher preference zones for the 
       
  2760 //!			pages to be moved to. Following this call a general defrag
       
  2761 //!		5.	Set up memory so that there is a fixed page and a movable page in an empty zone (testZone). Also ensure that 
       
  2762 //!			there are movable and free pages in more preferable zones than the test zone. Following this call a general
       
  2763 //!			defrag
       
  2764 //!		6.	Set up memory so that the mostPrefZone contains movable and free pages. Allocate 1 movable and 1 fixed
       
  2765 //!			page into an empty zone (testZone). Following this call a general defrag. 
       
  2766 //! 
       
  2767 //! @SYMTestExpectedResults
       
  2768 //! 	1.	Second defrag does not empty any zones
       
  2769 //! 	2.	No zones have been emptied
       
  2770 //!		3.	Zones are emptied if the general defrag can succeed
       
  2771 //! 	4.	Defrag fills up the less preferable zones first
       
  2772 //!		5.	movable pages are moved from the testZone
       
  2773 //!		6.	testZone is not emptied
       
  2774 //---------------------------------------------------------------------------------------------------------------------
       
  2775 TInt TestMovPgsDefrag()
       
  2776 	{
       
  2777 	TInt r = KErrNone;
       
  2778 
       
  2779 	test.Start(_L("Test1: Performing a defrag twice"));
       
  2780 	TestStart();
       
  2781 	
       
  2782 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  2783 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2784 	
       
  2785 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2786 	TESTDEBUG(test.Printf(_L("After calling general defrag r = %d\n"), r));
       
  2787 
       
  2788 	r = VerifyMovDisAlloc();	
       
  2789 	// The first defrag should empty zones if the general can succeed 
       
  2790 	if (r != KErrNone)
       
  2791 		{
       
  2792 		test.Printf(_L("Fail: r = %d, memory is not laid out as expected\n"), r);
       
  2793 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2794 		TEST_FAIL;
       
  2795 		}
       
  2796 
       
  2797 	GetOriginalPageCount();
       
  2798 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2799 	
       
  2800 	TESTDEBUG(test.Printf(_L("After calling general defrag again r = %d\n"), r));
       
  2801 	
       
  2802 	// The second call to general defrag should have nothing further to do
       
  2803 	if (r != KErrNone || CheckZonesSwitchedOff() != EFalse)
       
  2804 		{
       
  2805 		test.Printf(_L("Fail: r = %d, expected = %d, or zones have been switched off \n"), 
       
  2806 						r, KErrNone);
       
  2807 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2808 		TEST_FAIL;
       
  2809 		}
       
  2810 	else
       
  2811 		{
       
  2812 		test.Printf(_L("Passed...\n"));	
       
  2813 		}
       
  2814 	
       
  2815 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);	
       
  2816 	TestEnd();	
       
  2817 	
       
  2818 	
       
  2819 	
       
  2820 	test.Next(_L("Test2: Ensure new memory allocations can occur successfully during a general defrag"));
       
  2821 	TestStart();
       
  2822 	
       
  2823 	// Create a thread to call Defrag continuously and resume it
       
  2824 	r = gTestThread.Create(gTestThreadName,MultiLoopGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  2825 	if (r != KErrNone)
       
  2826 		{
       
  2827 		test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  2828 		TEST_FAIL;
       
  2829 		}		
       
  2830 	gTestThread.Logon(status);
       
  2831 	gTestLoop = ETrue;
       
  2832 	gTestThread.Resume();
       
  2833 	
       
  2834 	// Whilst the defrag loop is taking place, continuously allocate and free memory
       
  2835 	for (TInt i = 0; i < 100; i++)
       
  2836 		{
       
  2837 		r = AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  2838 		if (r != KErrNone)
       
  2839 			break;
       
  2840 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2841 		}
       
  2842 	gTestLoop = EFalse;
       
  2843 	User::WaitForRequest(status);
       
  2844 	TESTDEBUG(test.Printf(_L("defrag running on another thread returns %d\n"), r));
       
  2845 		
       
  2846 	if (r != KErrNone || status.Int() != KErrNone)
       
  2847 		{
       
  2848 		test.Printf(_L("Fail: r = %d, status.Int() = %d\n"), r, status.Int());	
       
  2849 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2850 		TEST_FAIL;
       
  2851 		}
       
  2852 	else
       
  2853 		{
       
  2854 		test.Printf(_L("Passed...\n"));	
       
  2855 		}
       
  2856 
       
  2857 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2858 	RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
  2859 	gTestThread.Close();	
       
  2860 	TestEnd();
       
  2861 	
       
  2862 	
       
  2863 	test.Next(_L("Test3: Check whether RAM defrag switches off any zones"));
       
  2864 	TestStart();
       
  2865 	
       
  2866 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  2867 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  2868 
       
  2869 	GetOriginalPageCount();
       
  2870 	TBool genSucceed = CanGenSucceed();
       
  2871 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2872 	
       
  2873 	TESTDEBUG(test.Printf(_L("After calling zone defrag r = %d\n"), r));
       
  2874 	
       
  2875 	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
       
  2876 		{
       
  2877 		test.Printf(_L("Fail: No zones were switched off or r = %d, expected = %d\n"), r, KErrNone);
       
  2878 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  2879 		TEST_FAIL;
       
  2880 		}
       
  2881 	else
       
  2882 		{
       
  2883 		test.Printf(_L("Passed...\n"));	
       
  2884 		}
       
  2885 
       
  2886 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  2887 	TestEnd();
       
  2888 
       
  2889 
       
  2890 	test.Next(_L("Test4: Verify that a general defrag moves pages to the least preferable RAM zone in use"));
       
  2891 	TestStart();
       
  2892 	gChunkArray1 = new RChunk;
       
  2893 	gChunkArraySize1 = 1;
       
  2894 	// Get the preference order
       
  2895 	GetPrefOrder();
       
  2896 
       
  2897 	TUint mostPrefIndex = 0;
       
  2898 	TUint mostPrefArrayIndex = 0;
       
  2899 	TUint totMov = 0;
       
  2900 	TUint lastPrefIndex = 0;
       
  2901 	TUint lastPrefArrayIndex = 0;
       
  2902 	TUint movPages = 0;
       
  2903 	TUint movBytes = 0;
       
  2904 	TUint totFreeInTestZones = 0;
       
  2905 	TBool zoneNotEmptyOrFull = EFalse;
       
  2906 
       
  2907 
       
  2908 	// Find the first most pref zone that has free pages in it
       
  2909 	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
       
  2910 	if (r != KErrNone)
       
  2911 		{
       
  2912 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  2913 		goto skipTest4;
       
  2914 		}
       
  2915 
       
  2916 	// Find the next most preferable free zone
       
  2917 	lastPrefArrayIndex = mostPrefArrayIndex + 1;
       
  2918 
       
  2919 	while (lastPrefArrayIndex < gZoneCount &&
       
  2920 		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
       
  2921 		{
       
  2922 		lastPrefArrayIndex++;
       
  2923 		}
       
  2924 
       
  2925 	if (lastPrefArrayIndex >= gZoneCount)
       
  2926 		{
       
  2927 		test.Printf(_L("Skipping...\n"));
       
  2928 		goto skipTest4;
       
  2929 		}
       
  2930 
       
  2931 	// Block all other zones
       
  2932 	for (TUint prefIndex = lastPrefArrayIndex + 1; prefIndex < gZoneCount; prefIndex++)
       
  2933 		{
       
  2934 		TUint zoneIndex = gPrefArray[prefIndex];
       
  2935 		Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
       
  2936 		}
       
  2937 
       
  2938 
       
  2939 	// Zone alloc to fill half of the last zone under test
       
  2940 	GetAllPageInfo();
       
  2941 	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
       
  2942 	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, gZoneUtilArray[lastPrefIndex].iFreePages >> 1);
       
  2943 	if (r != KErrNone)
       
  2944 		{
       
  2945 		test.Printf(_L("Failed to allocate fixed pages r = %d - Skipping...\n"), r);
       
  2946 		goto skipTest4;
       
  2947 		}
       
  2948 
       
  2949 
       
  2950 	// Go through the zones under test and determine the number of free pages
       
  2951 	GetAllPageInfo();
       
  2952 	for(TUint prefIndex = mostPrefArrayIndex; prefIndex <= lastPrefArrayIndex; prefIndex++)
       
  2953 		{
       
  2954 		TUint zoneIndex = gPrefArray[prefIndex];
       
  2955 		totFreeInTestZones += gZoneUtilArray[zoneIndex].iFreePages;
       
  2956 		}
       
  2957 
       
  2958 	// Allocate movable pages to almost fill zones under test
       
  2959 	movPages = totFreeInTestZones;
       
  2960 	movBytes = movPages << gPageShift;
       
  2961 	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
       
  2962 		{
       
  2963 		movBytes -= gPageSize;
       
  2964 		movPages--;
       
  2965 		}
       
  2966 	if (!movBytes)
       
  2967 		{
       
  2968 		test.Printf(_L("Failed to allocate 0x%x movable pages - Skipping...\n"), movPages);
       
  2969 		goto skipTest4;
       
  2970 		}
       
  2971 
       
  2972 
       
  2973 	// Free the allocated fixed pages
       
  2974 	Ldd.FreeAllFixedPages();
       
  2975 
       
  2976 	// Reset all the flags
       
  2977 	ResetRamZoneFlags();
       
  2978 	
       
  2979 	// Check that the most preferable zone still has movable pages in it
       
  2980 	// and also check that the last zone under test has free pages in it
       
  2981 	GetAllPageInfo();
       
  2982 	if (gTotalPageCount.iMovablePages < movPages ||
       
  2983 		gZoneUtilArray[mostPrefIndex].iAllocMovable == 0 ||
       
  2984 		gZoneUtilArray[lastPrefIndex].iFreePages == 0)
       
  2985 		{
       
  2986 		test.Printf(_L("Setup failed - Skipping...\n"));
       
  2987 		goto skipTest4;
       
  2988 		}
       
  2989 
       
  2990 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  2991 	// Check that defrag allocated the movable from the less preferable to the more preferable
       
  2992 	GetPrefOrder();
       
  2993 	totMov = gTotalPageCount.iMovablePages;
       
  2994 	for(TInt prefIndex = gZoneCount - 1; prefIndex >= 0 ; prefIndex--)
       
  2995 		{
       
  2996 		TUint zoneIndex = gPrefArray[prefIndex];
       
  2997 		GetCandList1(zoneIndex);
       
  2998 		TUint candIndex = 0;
       
  2999 		for (; candIndex < gZoneCount; candIndex++)
       
  3000 			{// Check all the zones of this preference
       
  3001 			TInt zoneIndexCand = gCandList1[candIndex];
       
  3002 			if (zoneIndexCand == KInvalidCandIndex)
       
  3003 				{
       
  3004 				break;
       
  3005 				}
       
  3006 			totMov -= gZoneUtilArray[zoneIndexCand].iAllocMovable;
       
  3007 			if (gZoneUtilArray[zoneIndexCand].iFreePages != gZoneUtilArray[zoneIndexCand].iPhysPages &&
       
  3008 				gZoneUtilArray[zoneIndexCand].iFreePages != 0)
       
  3009 				{
       
  3010 				zoneNotEmptyOrFull = ETrue;
       
  3011 				}
       
  3012 			}
       
  3013 		prefIndex -= candIndex - 1 ;
       
  3014 
       
  3015 		if (zoneNotEmptyOrFull && totMov != 0)
       
  3016 			{
       
  3017 			test.Printf(_L("FAIL: index = %d free = 0x%x totMov = 0x%x\n"), 
       
  3018 							zoneIndex, gZoneUtilArray[zoneIndex].iFreePages, totMov);
       
  3019 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3020 			CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  3021 			TEST_FAIL;
       
  3022 			}
       
  3023 		}
       
  3024 
       
  3025 skipTest4:
       
  3026 	TestEnd();
       
  3027 
       
  3028 
       
  3029 	test.Next(_L("Test5: Verify that a general defrag tidies up if RAM zone to be emptied contains fixed pages"));
       
  3030 	TestStart();
       
  3031 	gChunkArray1 = new RChunk;
       
  3032 	gChunkArraySize1 = 1;
       
  3033 	gChunkArray2 = new RChunk;
       
  3034 	gChunkArraySize2 = 1;
       
  3035 
       
  3036 	// find 2 empty zones - put 1 movable in 1 of them and 1 movable and 1 fixed in the other
       
  3037 	TUint testZoneIndex1;
       
  3038 	TUint testZoneIndex2;
       
  3039 	TUint testZoneID2;
       
  3040 	TUint numFreeInUse = 0;
       
  3041 
       
  3042 	r = FindMostPrefEmpty(testZoneIndex1);
       
  3043 	if (r != KErrNone)
       
  3044 		{
       
  3045 		test.Printf(_L("Skipping...\n"));
       
  3046 		goto skipTest5;
       
  3047 		}
       
  3048 	// Allocate 1 movable page to the test zone
       
  3049 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, testZoneIndex1, 1);
       
  3050 	if (r != KErrNone)
       
  3051 		{
       
  3052 		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex1, r);
       
  3053 		goto skipTest5;
       
  3054 		}
       
  3055 	
       
  3056 	r = FindMostPrefEmpty(testZoneIndex2);
       
  3057 	if (r != KErrNone)
       
  3058 		{
       
  3059 		test.Printf(_L("Skipping...\n"));
       
  3060 		goto skipTest5;
       
  3061 		}
       
  3062 	// Allocate 1 movable page to the test zone
       
  3063 	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, testZoneIndex2, 1);
       
  3064 	if (r != KErrNone)
       
  3065 		{
       
  3066 		test.Printf(_L("Failed to allocate 1 movable page to zone index %d, r = %d - Skipping...\n"), testZoneIndex2, r);
       
  3067 		goto skipTest5;
       
  3068 		}
       
  3069 
       
  3070 	// Zone alloc to put 1 fixed page last zone under test
       
  3071 	testZoneID2 = gZoneConfigArray[testZoneIndex2].iZoneId;
       
  3072 	r = Ldd.ZoneAllocContiguous(testZoneID2, gPageSize);
       
  3073 	if (r != KErrNone)
       
  3074 		{
       
  3075 		test.Printf(_L("Failed to allocate 1 fixed page to zone %d index, r = %d - Skipping...\n"), testZoneIndex2, r);
       
  3076 		goto skipTest5;
       
  3077 		}
       
  3078 	
       
  3079 	// Allocate 2 fixed (as there is 1 in our test zone) to prevent reordering
       
  3080 	GetAllPageInfo();
       
  3081 	for (TUint index = 0; index < gZoneCount; index++)
       
  3082 		{
       
  3083 		TUint totImmovable = gZoneUtilArray[index].iAllocFixed + gZoneUtilArray[index].iAllocUnknown ;
       
  3084 		if (index != testZoneIndex1 &&
       
  3085 			index != testZoneIndex2 &&
       
  3086 			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
       
  3087 			totImmovable < 2)
       
  3088 			{
       
  3089 			r = Ldd.ZoneAllocToMany(index, 2 - totImmovable);
       
  3090 			if (r != KErrNone)
       
  3091 				{
       
  3092 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Skipping...\n"), 
       
  3093 								2, index, r);
       
  3094 				goto skipTest5; 
       
  3095 				}
       
  3096 			}
       
  3097 		}
       
  3098 
       
  3099 	// Check that the number of free pages in the other in-use zones >= movable pages in test zone
       
  3100 	GetAllPageInfo();
       
  3101 	for (TUint index = 0; index < gZoneCount; index++)
       
  3102 		{
       
  3103 		if (index != testZoneIndex1 &&
       
  3104 			index != testZoneIndex2 &&
       
  3105 			gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
       
  3106 			{
       
  3107 			numFreeInUse += gZoneUtilArray[index].iFreePages;
       
  3108 			}
       
  3109 		}
       
  3110 	if (gZoneUtilArray[testZoneIndex1].iAllocMovable + gZoneUtilArray[testZoneIndex1].iAllocDiscardable + 
       
  3111 		gZoneUtilArray[testZoneIndex2].iAllocMovable + gZoneUtilArray[testZoneIndex2].iAllocDiscardable > numFreeInUse ||
       
  3112 		gZoneUtilArray[testZoneIndex2].iAllocFixed == 0)
       
  3113 		{
       
  3114 		test.Printf(_L("Setup failed - Skipping...\n"));
       
  3115 		goto skipTest5;
       
  3116 		}
       
  3117 
       
  3118 	// Added extra tracing to debug random failures
       
  3119 	PrintPageInfo();
       
  3120 
       
  3121 	// Call a general defrag
       
  3122 	GetOriginalPageCount();
       
  3123 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3124 
       
  3125 	// Check that the defrag tidies up
       
  3126 	GetAllPageInfo();
       
  3127 	if(gZoneUtilArray[testZoneIndex1].iAllocMovable ||
       
  3128 		gZoneUtilArray[testZoneIndex2].iAllocMovable)
       
  3129 		{
       
  3130 		test.Printf(_L("FAIL: testZoneIndex1(%d): mov orig 0x%x cur 0x%x, testZoneIndex2(%d) mov orig 0x%x cur 0x%x \n"), 
       
  3131 						testZoneIndex1, gOriginalPageCountArray[testZoneIndex1].iAllocMovable, gZoneUtilArray[testZoneIndex1].iAllocMovable, 
       
  3132 						testZoneIndex2, gOriginalPageCountArray[testZoneIndex2].iAllocMovable, gZoneUtilArray[testZoneIndex2].iAllocMovable);
       
  3133 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3134 		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  3135 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3136 		TEST_FAIL;
       
  3137 		}
       
  3138 
       
  3139 skipTest5:
       
  3140 	TestEnd();
       
  3141 
       
  3142 
       
  3143 	test.Next(_L("Test6: Verify that a general defrag will not move pages if RAM zone to be emptied contains fixed pages"));
       
  3144 	TestStart();
       
  3145 	gChunkArray1 = new RChunk;
       
  3146 	gChunkArraySize1 = 1;
       
  3147 	// Find the first most pref zone that has free pages in it
       
  3148 	r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
       
  3149 	if (r != KErrNone)
       
  3150 		{
       
  3151 		test.Printf(_L("Cannot find zone to perform test - Skipping...\n"));
       
  3152 		goto skipTest6;
       
  3153 		}
       
  3154 
       
  3155 	// Find the next most preferable free zone
       
  3156 	lastPrefArrayIndex = mostPrefArrayIndex + 1;
       
  3157 	while (lastPrefArrayIndex < gZoneCount &&
       
  3158 		gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iFreePages != gZoneUtilArray[gPrefArray[lastPrefArrayIndex]].iPhysPages)
       
  3159 		{
       
  3160 		lastPrefArrayIndex++;
       
  3161 		}
       
  3162 	if (lastPrefArrayIndex >= gZoneCount)
       
  3163 		{
       
  3164 		test.Printf(_L("Skipping...\n"));
       
  3165 		goto skipTest6;
       
  3166 		}
       
  3167 
       
  3168 
       
  3169 	// Zone alloc to put 1 fixed page last zone under test
       
  3170 	lastPrefIndex = gPrefArray[lastPrefArrayIndex];
       
  3171 	r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[lastPrefIndex].iZoneId, 1);
       
  3172 	if (r != KErrNone)
       
  3173 		{
       
  3174 		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d - Skipping...\n"), lastPrefIndex, r);
       
  3175 		goto skipTest6;
       
  3176 		}
       
  3177 	
       
  3178 	GetAllPageInfo();
       
  3179 	if (!gZoneUtilArray[mostPrefIndex].iFreePages)
       
  3180 		{
       
  3181 		test.Printf(_L("Not enough space in zone under test  mostPrefIndex = %d - Skipping...\n"), mostPrefIndex);
       
  3182 		goto skipTest6;
       
  3183 		}
       
  3184 
       
  3185 	// Block all zones apart from the last zone under test from allocation
       
  3186 	for (TUint prefIndex = 0; prefIndex < gZoneCount; prefIndex++)
       
  3187 		{
       
  3188 		TUint zoneIndex = gPrefArray[prefIndex];
       
  3189 		if (zoneIndex != lastPrefIndex)
       
  3190 			{
       
  3191 			Ldd.SetZoneFlag(gZoneConfigArray[zoneIndex].iZoneId, gZoneConfigArray[zoneIndex].iFlags, NO_ALLOC_FLAG);
       
  3192 			}
       
  3193 		}
       
  3194 	
       
  3195 	TESTDEBUG(test.Printf(_L("mostPrefIndex = %d lastPrefIndex = %d\n"), mostPrefIndex, lastPrefIndex));
       
  3196 	
       
  3197 	// Allocate movable pages to the lastPrefZone that will fit into the most pref zone
       
  3198 	movPages = gZoneUtilArray[mostPrefIndex].iFreePages;
       
  3199 	movBytes = movPages << gPageShift;
       
  3200 	while (movBytes && AllocMovable(gChunkArray1, gChunkArraySize1, 1, movBytes) != KErrNone)
       
  3201 		{
       
  3202 		movBytes -= gPageSize;
       
  3203 		}
       
  3204 	if (!movBytes)
       
  3205 		{
       
  3206 		test.Printf(_L("Failed to allocate 0x%x movable pages r = %d - Skipping...\n"), movPages, r);
       
  3207 		goto skipTest6;
       
  3208 		}
       
  3209 
       
  3210 	// Reset all the flags
       
  3211 	ResetRamZoneFlags();
       
  3212 	
       
  3213 
       
  3214 	// Check that the number of movable pages in the least pref will fit
       
  3215 	// into the most preferable zone
       
  3216 	GetAllPageInfo();
       
  3217 	if (gZoneUtilArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[mostPrefIndex].iFreePages)
       
  3218 		{
       
  3219 		test.Printf(_L("Setup failed - Skipping...\n"));
       
  3220 		PrintPageInfo();
       
  3221 		goto skipTest6;
       
  3222 		}
       
  3223 	
       
  3224 	// Added extra tracing to debug random failures
       
  3225 	PrintPageInfo();	
       
  3226 
       
  3227 	// Call a general defrag
       
  3228 	GetOriginalPageCount();
       
  3229 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3230 
       
  3231 	// Check that the defrag didn't move any pages from the least pref zone
       
  3232 	GetAllPageInfo();
       
  3233 	if(gOriginalPageCountArray[lastPrefIndex].iAllocMovable > gZoneUtilArray[lastPrefIndex].iAllocMovable)
       
  3234 		{
       
  3235 		test.Printf(_L("FAIL: mostPref(index %d): origMov = 0x%x curMov = 0x%x, lastPref(index %d): origMov = 0x%x curMov = 0x%x \n"), 
       
  3236 						mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
       
  3237 						lastPrefIndex, 	gOriginalPageCountArray[lastPrefIndex].iAllocMovable, gZoneUtilArray[lastPrefIndex].iAllocMovable);
       
  3238 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3239 		CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  3240 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3241 		TEST_FAIL;
       
  3242 		}
       
  3243 
       
  3244 skipTest6:
       
  3245 	TestEnd();
       
  3246 
       
  3247 	test.End();
       
  3248 	return KErrNone;
       
  3249 	}
       
  3250 
       
  3251 //
       
  3252 // GenDefragTest123Setup
       
  3253 //
       
  3254 // Used to set up the memory ready for testing in TestGenDefrag, Test Steps 1, 2, 3 and 4
       
  3255 //
       
  3256 // @param aMovAlloc The number of movable pages to allocate.
       
  3257 // @param aFixAlloc The number of fixed pages to allocate.
       
  3258 // @param aDisAlloc The number of discardable pages to allocate.
       
  3259 // @param aMovIndex On return contains the test zone index.
       
  3260 // @param aNumFreeInUse On return contains the number of free pages in the inuse zones.
       
  3261 // @param aTestNo The test number that is using this setup.
       
  3262 //
       
  3263 TInt GenDefragTest123Setup(TUint aMovAlloc, TUint aFixAlloc, TUint aDisAlloc, TUint& aMovIndex, TUint& aNumFreeInUse, TUint aTestNo)
       
  3264 	{
       
  3265 	TInt r = KErrNone;
       
  3266 	aNumFreeInUse = 0;
       
  3267 	const TUint KFillUpTo = 5;	// Rough estimate of how full the in-use zones should be
       
  3268 	const TUint KSpaceNeeded = 2;
       
  3269 	TUint movPrefIndex = 0;
       
  3270 	if (aTestNo == 4)
       
  3271 		{// Test 4 we need to look for the LEAST preferable RAM zone which is empty
       
  3272 		TUint mostPrefIndex;
       
  3273 		r = FindLeastPrefEmpty(aMovIndex, &movPrefIndex);
       
  3274 		TInt r2 = FindMostPrefEmpty(mostPrefIndex);
       
  3275 		if (r != KErrNone || r2 != KErrNone || !gPagedRom ||
       
  3276 			gZoneConfigArray[aMovIndex].iPref == gZoneConfigArray[mostPrefIndex].iPref)
       
  3277 			{
       
  3278 			test.Printf(_L("r %d r2 %d or not a paged ROM or Equal zone preferences- Skipping test step...\n"), 
       
  3279 							r, r2);
       
  3280 			return KErrGeneral;
       
  3281 			}
       
  3282 		}
       
  3283 	else
       
  3284 		{// All other tests look the MOST preferable RAM zone which is empty
       
  3285 		r = FindMostPrefEmpty(aMovIndex, &movPrefIndex);
       
  3286 		if (r != KErrNone || !gPagedRom)
       
  3287 			{
       
  3288 			test.Printf(_L("No suitable RAM zone found or not a paged ROM - Skipping test step...\n"));
       
  3289 			return KErrGeneral;
       
  3290 			}
       
  3291 		}
       
  3292 
       
  3293 	// Go through all the in-use zones and check how many free pages there are
       
  3294 	// Allocate fixed so there is only KFillUpTo pages in each in-use zone to
       
  3295 	// ensure that the total number of free pages in the in-use zones can fit into
       
  3296 	// the zone under test
       
  3297 	GetAllPageInfo();
       
  3298 	for (TUint index = 0; index < gZoneCount; index++)
       
  3299 		{
       
  3300 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
       
  3301 			gZoneUtilArray[index].iFreePages > KFillUpTo)
       
  3302 			{
       
  3303 			r = Ldd.ZoneAllocToMany(index, gZoneUtilArray[index].iFreePages - KFillUpTo);
       
  3304 			if (r != KErrNone)
       
  3305 				{
       
  3306 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
       
  3307 								gZoneUtilArray[index].iFreePages - KFillUpTo, index, r);
       
  3308 				return KErrGeneral; 
       
  3309 				}
       
  3310 			}
       
  3311 		}
       
  3312 
       
  3313 	GetAllPageInfo();
       
  3314 	// If any of the in-use zones doesn't contain any fixed and unknown pages allocate one fixed
       
  3315 	for (TUint index = 0; index < gZoneCount; index++)
       
  3316 		{
       
  3317 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
       
  3318 			!gZoneUtilArray[index].iAllocFixed && !gZoneUtilArray[index].iAllocUnknown)
       
  3319 			{
       
  3320 			r = Ldd.ZoneAllocToMany(index, 1);
       
  3321 			if (r != KErrNone)
       
  3322 				{
       
  3323 				test.Printf(_L("Failed to alloc 0x%x fixed to zone index %d r = %d - Ending setup...\n"), 
       
  3324 								1, index, r);
       
  3325 				return KErrGeneral;
       
  3326 				}
       
  3327 			}
       
  3328 		}
       
  3329 
       
  3330 	GetAllPageInfo();
       
  3331 	// Check the total number of free pages in the in-use zones
       
  3332 	for (TUint index = 0; index < gZoneCount; index++)
       
  3333 		{
       
  3334 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages)
       
  3335 			{
       
  3336 			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
       
  3337 			}
       
  3338 		}
       
  3339 	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
       
  3340 
       
  3341 	test.Printf(_L("aMovIndex = %d\n"), aMovIndex);
       
  3342 	if (aNumFreeInUse < KSpaceNeeded)
       
  3343 		{
       
  3344 		test.Printf(_L("RAM zone to be tested is too full - Skipping test step...\n"));
       
  3345 		return KErrGeneral;
       
  3346 		}
       
  3347 
       
  3348 
       
  3349 	// Ensure that there are movable pages in the zone under test
       
  3350 	// ensuring that there is also room for discardable pages
       
  3351 	GetAllPageInfo();
       
  3352 	if (aMovAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocMovable == 0)
       
  3353 		{
       
  3354 		// Allocate just aMovAlloc movable pages
       
  3355 		TUint movPagesAlloc = aMovAlloc;
       
  3356 		if (aTestNo == 2 || aTestNo == 4)
       
  3357 			{// Tests 2 and 4 require that movable pages can't be moved from zone
       
  3358 			movPagesAlloc = aNumFreeInUse + 1;
       
  3359 			if (movPagesAlloc > gZoneUtilArray[aMovIndex].iFreePages)
       
  3360 				{
       
  3361 				test.Printf(_L("Insufficiant space in zone %d to allocate 0x%x pages - Ending setup...\n"), 
       
  3362 								aMovIndex, movPagesAlloc);
       
  3363 				return KErrGeneral;
       
  3364 				}
       
  3365 			}
       
  3366 
       
  3367 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aMovIndex, movPagesAlloc);
       
  3368 		if (gZoneUtilArray[aMovIndex].iAllocMovable != movPagesAlloc ||
       
  3369 			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
       
  3370 			r != KErrNone)
       
  3371 			{// Movable page didn't go into RAM, or RAM zone is full.
       
  3372 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
       
  3373 			test.Printf(_L("zone mov 0x%x free 0x%x r=%d\n"), 
       
  3374 						gZoneUtilArray[aMovIndex].iAllocMovable,	
       
  3375 						gZoneUtilArray[aMovIndex].iFreePages, r);
       
  3376 			return KErrGeneral;
       
  3377 			}
       
  3378 		}
       
  3379 
       
  3380 	// Block all other zones from allocation
       
  3381 	for(TUint i = 0; i < gZoneCount; i++)
       
  3382 		{
       
  3383 		if (i != aMovIndex)
       
  3384 			{
       
  3385 			r = Ldd.SetZoneFlag(gZoneConfigArray[i].iZoneId, gZoneConfigArray[i].iFlags, NO_ALLOC_FLAG);
       
  3386 			if (r != KErrNone)
       
  3387 				{
       
  3388 				test.Printf(_L("Failed to set flag on zone index %d r = %d- Ending setup...\n"), i, r);
       
  3389 				return KErrGeneral;
       
  3390 				}
       
  3391 			}
       
  3392 		}
       
  3393 
       
  3394 	// Now ensure that there are fixed pages in the zone under test
       
  3395 	// ensuring that there is also room for discardable pages
       
  3396 	GetAllPageInfo();
       
  3397 	if (aFixAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocFixed == 0)
       
  3398 		{//Allocate just aFixAlloc fixed pages 
       
  3399 		r = Ldd.ZoneAllocToMany(aMovIndex, aFixAlloc);
       
  3400 		GetAllPageInfo();
       
  3401 		if (gZoneUtilArray[aMovIndex].iAllocMovable == 0 ||
       
  3402 			gZoneUtilArray[aMovIndex].iFreePages == 0 ||
       
  3403 			gZoneUtilArray[aMovIndex].iAllocFixed == 0 ||
       
  3404 			r != KErrNone)
       
  3405 			{// Fixed page didn't go into RAM or RAM zone is full.
       
  3406 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
       
  3407 			test.Printf(_L("zone mov 0x%x fixed 0x%x free 0x%x r=%d\n"), 
       
  3408 						gZoneUtilArray[aMovIndex].iAllocMovable,	
       
  3409 						gZoneUtilArray[aMovIndex].iAllocFixed,	
       
  3410 						gZoneUtilArray[aMovIndex].iFreePages, r);
       
  3411 			return KErrGeneral;
       
  3412 			}
       
  3413 		}
       
  3414 
       
  3415 
       
  3416 	
       
  3417 	// Allocate aDisAlloc number of discardable pages
       
  3418 	if (aDisAlloc != 0 && gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
       
  3419 		{//Allocate just aDisAlloc discardable pages 
       
  3420 		TInt discard;
       
  3421 		r = ZoneAllocDiscard(aMovIndex, aDisAlloc, discard);
       
  3422 		GetAllPageInfo();
       
  3423 		if (r != KErrNone || gZoneUtilArray[aMovIndex].iAllocDiscardable == 0)
       
  3424 			{// Discardable page didn't go into RAM or RAM zone is full.
       
  3425 			test.Printf(_L("Can't complete test with current RAM layout. - Skipping...\n"));
       
  3426 			test.Printf(_L("zone mov 0x%x discardable 0x%x free 0x%x r=%d\n"), 
       
  3427 						gZoneUtilArray[aMovIndex].iAllocDiscardable,	
       
  3428 						gZoneUtilArray[aMovIndex].iAllocFixed,	
       
  3429 						gZoneUtilArray[aMovIndex].iFreePages, r);
       
  3430 			return KErrGeneral;
       
  3431 			}
       
  3432 		}
       
  3433 	
       
  3434 	// Update the total number of free pages in the in-use zones
       
  3435 	aNumFreeInUse = 0;
       
  3436 	for (TUint index = 0; index < gZoneCount; index++)
       
  3437 		{
       
  3438 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
       
  3439 			index != aMovIndex)
       
  3440 			{
       
  3441 			aNumFreeInUse += gZoneUtilArray[index].iFreePages;
       
  3442 			}
       
  3443 		}
       
  3444 	TESTDEBUG(test.Printf(_L("aNumFreeInUse = 0x%x\n"), aNumFreeInUse));
       
  3445 
       
  3446 	// now reset all the flags 
       
  3447 	ResetRamZoneFlags();
       
  3448 	
       
  3449 	return KErrNone;
       
  3450 	}
       
  3451 //
       
  3452 // TestGenDefrag
       
  3453 //
       
  3454 //---------------------------------------------------------------------------------------------------------------------
       
  3455 //! @SYMTestCaseID				KBASE-t_ramdefrag-0528
       
  3456 //! @SYMTestType				CIT
       
  3457 //! @SYMTestCaseDesc			Verifying general scenarios when RAM defrag would take place
       
  3458 //! @SYMPREQ					PREQ308
       
  3459 //! @SYMTestPriority			High
       
  3460 //! @SYMTestActions				
       
  3461 //! 	1.	Allocate 1 movable, 1 fixed and 1 discardable pages into the most preferable empty RAM zone
       
  3462 //!			ensuring that there is enough free space in the more preferable RAM zones for the
       
  3463 //!			movable pages in the RAM zone under test. Following this call a general defrag.
       
  3464 //!		2.	Allocate 1 movable and 1 discardable page into into the most preferable empty RAM zone
       
  3465 //!			ensuring that there is not enough free space in the more preferable RAM zones for the
       
  3466 //!			movable pages in the RAM zone under test. Following this call a general defrag. 
       
  3467 //!		3.	Allocate 1 movable and 1 discardable and 1 fixed page into the most preferable empty RAM zone(a). 
       
  3468 //!			Following this, place 1 movable page in an empty zone(b) ensuring that there is enough free space in 
       
  3469 //!			the more preferable RAM zones for the movable pages in both zones (a) and (b). 
       
  3470 //!			Following this call a general defrag. 
       
  3471 //!		4.	Allocate 1 movable and 1 discardable page into the least preferable empty RAM zone
       
  3472 //!			ensuring that there is not enough free space in the more preferable RAM zones for the
       
  3473 //!			movable pages in the RAM zone under test. Following this call a general defrag. 
       
  3474 //! 	5.	Fragment the memory. 
       
  3475 //! 		Following this allocate discardable pages by loading pages that are demand paged. 
       
  3476 //! 		Read each of the constants from beginning to end. 
       
  3477 //! 		Following this perform a RAM defrag. 
       
  3478 //! 	6.	Call a device driver that will continuously allocate fixed pages to the
       
  3479 //! 		memory until it reports out of memory. Following this perform a RAM defrag. 
       
  3480 //!		7.	Defrag memory filled with discardable pages when the min cache size is reached
       
  3481 //!		8.	Fragment the memory. Following this, continuously call a general defrag, each time 
       
  3482 //!			reducing the size of the chunks that are allocated  
       
  3483 //! 
       
  3484 //! @SYMTestExpectedResults
       
  3485 //! 	1.	The RAM zone under test is not emptied and all the discardable pages contained in it
       
  3486 //!			have not been discarded.
       
  3487 //! 	2.	The RAM zone under test is not emptied and all the discardable pages contained in it
       
  3488 //!			have not been discarded.
       
  3489 //! 	3.	RAM zones (a) and (b) have been emptied and all the discardable pages contained in them
       
  3490 //!			have not been discarded.
       
  3491 //!		4.	The RAM zone under test is emptied
       
  3492 //! 	5.	Pages are discarded
       
  3493 //! 	6.	No zones are emptied
       
  3494 //!		7.	The least preferable zone is skipped but the other zones are defragmented as required.
       
  3495 //! 	8.	Zones are emptied if the general defrag can succeed
       
  3496 //---------------------------------------------------------------------------------------------------------------------
       
  3497 TInt TestGenDefrag()
       
  3498 	{
       
  3499 	TInt r = KErrNone;
       
  3500 	
       
  3501 	test.Start(_L("Test1: Test General Defrag doesn't discard pages when fixed page in zone to be emptied"));	
       
  3502 	TestStart();
       
  3503 	gChunkArray1 = new RChunk;
       
  3504 	gChunkArraySize1 = 1;
       
  3505 	TUint numFreeInUse = 0;
       
  3506 	TUint testNo = 1;
       
  3507 
       
  3508 	// We just need 1 movable, 1 discardable and 1 fixed in the zone to ensure it cannot be emptied
       
  3509 	// as the fixed should block the defrag
       
  3510 	TInt movAllocPages = 1;
       
  3511 	TInt fixAllocPages = 1;
       
  3512 	TInt disAllocPages = 1;
       
  3513 	TUint zoneIndex;
       
  3514 
       
  3515 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
       
  3516 		{
       
  3517 		test.Printf(_L("Setup failed - Skipping..\n"));
       
  3518 		goto skipTest1;
       
  3519 		}
       
  3520 
       
  3521 	// Call a general defrag and check that no discardable pages were freed.
       
  3522 	GetOriginalPageCount();
       
  3523 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3524 	GetAllPageInfo();
       
  3525 	if (r != KErrNone || 
       
  3526 		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
       
  3527 		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
       
  3528 		{
       
  3529 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
       
  3530 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
       
  3531 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
       
  3532 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3533 		TEST_FAIL;
       
  3534 		}
       
  3535 	else
       
  3536 		{
       
  3537 		test.Printf(_L("Passed...\n"));	
       
  3538 		}
       
  3539 
       
  3540 skipTest1:
       
  3541 	// TestEnd() will perform any required clean up.
       
  3542 	TestEnd();
       
  3543 
       
  3544 	test.Next(_L("Test2: Test General Defrag doesn't discard pages when zone can't be emptied"));	
       
  3545 	TestStart();	
       
  3546 	gChunkArray1 = new RChunk;
       
  3547 	gChunkArraySize1 = 1;
       
  3548 	testNo++;
       
  3549 	// No fixed, but there should be no space for the movable to go to
       
  3550 	// GenDefragTest123Setup(), will ensure that this is the case
       
  3551 	// by allocating 1 more movable page than there is free in the in-use zones
       
  3552 	movAllocPages = 1;
       
  3553 	fixAllocPages = 0;
       
  3554 	disAllocPages = 1;
       
  3555 
       
  3556 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
       
  3557 		{
       
  3558 		test.Printf(_L("Setup failed - Skipping..\n"));
       
  3559 		goto skipTest2;
       
  3560 		}
       
  3561 
       
  3562 	// Call a general defrag and check that no discardable pages were freed.
       
  3563 	GetOriginalPageCount();
       
  3564 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3565 	
       
  3566 	GetAllPageInfo();
       
  3567 	if (r != KErrNone || 
       
  3568 		gZoneUtilArray[zoneIndex].iAllocMovable < gOriginalPageCountArray[zoneIndex].iAllocMovable ||
       
  3569 		gOriginalPageCountArray[zoneIndex].iAllocDiscardable > gZoneUtilArray[zoneIndex].iAllocDiscardable)
       
  3570 		{
       
  3571 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
       
  3572 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
       
  3573 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
       
  3574 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3575 		TEST_FAIL;
       
  3576 		}
       
  3577 	else
       
  3578 		{
       
  3579 		test.Printf(_L("Passed...\n"));	
       
  3580 		}
       
  3581 
       
  3582 skipTest2:
       
  3583 	// TestEnd() will perform any required clean up.
       
  3584 	TestEnd();
       
  3585 
       
  3586 	test.Next(_L("Test3: Test General Defrag tidies when fixed page in zone to be emptied but other can be emptied"));	
       
  3587 	TestStart();
       
  3588 	gChunkArray1 = new RChunk;
       
  3589 	gChunkArraySize1 = 1;
       
  3590 	gChunkArray2 = new RChunk;
       
  3591 	gChunkArraySize2 = 1;
       
  3592 	TUint totalToMove;
       
  3593 	testNo++;
       
  3594 
       
  3595 	// First use GenDefragTest123Setup() to ensure we place 1 fixed, 1 movable and 1 discardable
       
  3596 	// in the least preferable zone
       
  3597 	movAllocPages = 1;
       
  3598 	fixAllocPages = 1;
       
  3599 	disAllocPages = 1;
       
  3600 	TBool zoneFound = EFalse;	
       
  3601 	TUint emptyZoneIndex = 0;
       
  3602 
       
  3603 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
       
  3604 		{
       
  3605 		test.Printf(_L("Setup failed - Skipping..\n"));
       
  3606 		goto skipTest3;
       
  3607 		}
       
  3608 
       
  3609 	// Find an empty zone and place a movable page in it
       
  3610 	GetAllPageInfo();
       
  3611 	for (TUint i = 0; i < gZoneCount; i++)
       
  3612 		{
       
  3613 		if (gZoneUtilArray[i].iFreePages == gZoneUtilArray[i].iPhysPages)
       
  3614 			{
       
  3615 			zoneFound = ETrue;
       
  3616 			emptyZoneIndex = i;
       
  3617 			break;
       
  3618 			}
       
  3619 		}
       
  3620 	if (!zoneFound)
       
  3621 		{
       
  3622 		test.Printf(_L("Can't find empty zone - Skipping..\n"));
       
  3623 		goto skipTest3;
       
  3624 		}
       
  3625 
       
  3626 	// Allocate 1 movable page into the empty zone
       
  3627 	r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, emptyZoneIndex, 1);
       
  3628 	GetAllPageInfo();
       
  3629 	if (r != KErrNone ||
       
  3630 		gZoneUtilArray[emptyZoneIndex].iAllocMovable == 0 ||
       
  3631 		gZoneUtilArray[emptyZoneIndex].iAllocFixed != 0)
       
  3632 		{
       
  3633 		test.Printf(_L("Movable pages not allocated or fixed pages allocated. - Skipping...\n"));
       
  3634 		test.Printf(_L("zone mov 0x%x free 0x%x r =% d\n"), 
       
  3635 					gZoneUtilArray[emptyZoneIndex].iAllocMovable,	
       
  3636 					gZoneUtilArray[emptyZoneIndex].iFreePages, r);
       
  3637 		goto skipTest3;
       
  3638 		}
       
  3639 
       
  3640 	// Check that the amount we are allocating can actually be moved into the in-use zones
       
  3641 	totalToMove = gZoneUtilArray[zoneIndex].iAllocMovable + gZoneUtilArray[zoneIndex].iAllocDiscardable + 
       
  3642 					gZoneUtilArray[emptyZoneIndex].iAllocMovable + gZoneUtilArray[emptyZoneIndex].iAllocDiscardable;
       
  3643 	
       
  3644 	// Check the total number of free pages in the in-use zones
       
  3645 	numFreeInUse = 0;
       
  3646 	for (TUint index = 0; index < gZoneCount; index++)
       
  3647 		{
       
  3648 		if (gZoneUtilArray[index].iFreePages != gZoneUtilArray[index].iPhysPages &&
       
  3649 			index != zoneIndex && index != emptyZoneIndex)
       
  3650 			{
       
  3651 			numFreeInUse += gZoneUtilArray[index].iFreePages;
       
  3652 			}
       
  3653 		}
       
  3654 	if(numFreeInUse < totalToMove)
       
  3655 		{
       
  3656 		test.Printf(_L("No space to move pages numFreeInUse = 0x%x totalToMove = 0x%x - Skipping..\n"), 
       
  3657 						numFreeInUse, totalToMove);
       
  3658 		goto skipTest3;
       
  3659 		}
       
  3660 
       
  3661 	// Call a general defrag and check that zone is emptied.
       
  3662 	GetOriginalPageCount();
       
  3663 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3664 	
       
  3665 	GetAllPageInfo();
       
  3666 	if (r != KErrNone || 
       
  3667 		gZoneUtilArray[zoneIndex].iAllocMovable ||
       
  3668 		gZoneUtilArray[zoneIndex].iAllocDiscardable ||
       
  3669 		gZoneUtilArray[emptyZoneIndex].iAllocMovable)
       
  3670 		{
       
  3671 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
       
  3672 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
       
  3673 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
       
  3674 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3675 		TEST_FAIL;
       
  3676 		}
       
  3677 	else
       
  3678 		{
       
  3679 		test.Printf(_L("Passed...\n"));	
       
  3680 		}
       
  3681 
       
  3682 skipTest3:
       
  3683 	// TestEnd() will perform any required clean up.
       
  3684 	TestEnd();
       
  3685 
       
  3686 	test.Next(_L("Test4: Test General Defrag moves pages into the next most pref if they don't fit into the most pref"));	
       
  3687 	TestStart();
       
  3688 	gChunkArray1 = new RChunk;	
       
  3689 	gChunkArraySize1 = 1;
       
  3690 	testNo++;
       
  3691 	
       
  3692 	// No fixed, but there should be no space for the movable to go to
       
  3693 	// GenDefragTest123Setup(), will ensure that this is the case
       
  3694 	// by allocating 1 more movable page than there is free in the in-use zones
       
  3695 	movAllocPages = 1;
       
  3696 	fixAllocPages = 0;
       
  3697 	disAllocPages = 1;
       
  3698 
       
  3699 	if (GenDefragTest123Setup(movAllocPages, fixAllocPages, disAllocPages, zoneIndex, numFreeInUse, testNo) != KErrNone)
       
  3700 		{
       
  3701 		test.Printf(_L("Setup failed - Skipping..\n"));
       
  3702 		goto skipTest4;
       
  3703 		}
       
  3704 
       
  3705 	// Call a general defrag and check that the test zone is emptied.
       
  3706 	GetOriginalPageCount();
       
  3707 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3708 	
       
  3709 	GetAllPageInfo();
       
  3710 	if (r != KErrNone || 
       
  3711 		gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
       
  3712 		{
       
  3713 		test.Printf(_L("Fail: r = %d zoneIndex %d origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), r, zoneIndex, 
       
  3714 						gOriginalPageCountArray[zoneIndex].iAllocMovable, gZoneUtilArray[zoneIndex].iAllocMovable, 
       
  3715 						gOriginalPageCountArray[zoneIndex].iAllocDiscardable, gZoneUtilArray[zoneIndex].iAllocDiscardable);
       
  3716 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3717 		TEST_FAIL;
       
  3718 		}
       
  3719 	else
       
  3720 		{
       
  3721 		test.Printf(_L("Passed...\n"));	
       
  3722 		}
       
  3723 
       
  3724 skipTest4:
       
  3725 	// TestEnd() will perform any required clean up.
       
  3726 	TestEnd();
       
  3727 
       
  3728 	test.Next(_L("Test5: Defrag memory filled with discardable pages\n"));
       
  3729 	TestStart();
       
  3730 	if (gPagedRom)
       
  3731 		{
       
  3732 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  3733 		FreeMovable(gChunkArray1, gChunkArraySize1);
       
  3734 		
       
  3735 		GetAllPageInfo();	
       
  3736 		TInt discardablePages;
       
  3737 		UpdateRamInfo();
       
  3738 		
       
  3739 		r = AllocDiscardable(discardablePages);
       
  3740 		if (r != KErrNone)
       
  3741 			{
       
  3742 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  3743 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3744 			TEST_FAIL;
       
  3745 			}
       
  3746 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
       
  3747 		
       
  3748 		GetOriginalPageCount();
       
  3749 		TBool genSucceed = CanGenSucceed();
       
  3750 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3751 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  3752 		
       
  3753 		if (r != KErrNone)
       
  3754 			{
       
  3755 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
       
  3756 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3757 			TEST_FAIL;
       
  3758 			}
       
  3759 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
       
  3760 			{
       
  3761 			test.Printf(_L("No Zones Switched off and should have been\n"));
       
  3762 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3763 			TEST_FAIL;
       
  3764 			}
       
  3765 		else
       
  3766 			{
       
  3767 			test.Printf(_L("Passed...\n"));	
       
  3768 			}
       
  3769 
       
  3770 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  3771 		ResetDPCache();	
       
  3772 		}
       
  3773 	else
       
  3774 		{
       
  3775 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  3776 		}
       
  3777 	TestEnd();
       
  3778 		
       
  3779 	
       
  3780 	test.Next(_L("Test6: Defrag memory filled with fixed pages"));	
       
  3781 	TestStart();
       
  3782 	
       
  3783 	TESTDEBUG(test.Printf(_L("Filling memory with fixed pages, r = %d\n")));
       
  3784 	r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  3785 
       
  3786 	GetOriginalPageCount();
       
  3787 	TBool genSucceed = CanGenSucceed();
       
  3788 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3789 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  3790 
       
  3791 	// The zones should be full of fixed pages so the general should do nothing.
       
  3792 	if (r != KErrNone || 
       
  3793 		genSucceed || 
       
  3794 		CheckZonesSwitchedOff())
       
  3795 		{
       
  3796 		test.Printf(_L("Fail: r = %d, expected = %d, or zone have been emptied\n"), r, KErrNone);
       
  3797 		CLEANUP(Ldd.FreeAllFixedPages());
       
  3798 		TEST_FAIL;
       
  3799 		}
       
  3800 	else
       
  3801 		{
       
  3802 		test.Printf(_L("Passed...\n"));	
       
  3803 		}
       
  3804 	Ldd.FreeAllFixedPages();
       
  3805 	TestEnd();
       
  3806 
       
  3807 	test.Next(_L("Test7: Defrag memory filled with discardable pages when the min cache size is reached\n"));
       
  3808 	TestStart();
       
  3809 	if (gPagedRom)
       
  3810 		{
       
  3811 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  3812 		FreeMovable(gChunkArray1, gChunkArraySize1);
       
  3813 		
       
  3814 		GetAllPageInfo();	
       
  3815 		TInt discardablePages;
       
  3816 		UpdateRamInfo();
       
  3817 		
       
  3818 		r = AllocDiscardable(discardablePages);
       
  3819 		if (r != KErrNone)
       
  3820 			{
       
  3821 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  3822 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3823 			TEST_FAIL;
       
  3824 			}
       
  3825 		TESTDEBUG(test.Printf(_L("Number of discardable pages = 0x%x\n"), discardablePages >> gPageShift));
       
  3826 
       
  3827 		TUint minCacheSize = 0;
       
  3828 		TUint maxCacheSize = 0;
       
  3829 		TUint currentCacheSize = 0;
       
  3830 
       
  3831 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  3832 
       
  3833 		TUint setMin = maxCacheSize;
       
  3834 		TUint64 setMax = maxCacheSize;
       
  3835 		TInt r = DPTest::SetCacheSize(setMin, setMax);
       
  3836 		if (r != KErrNone)
       
  3837 			{
       
  3838 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  3839 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3840 			CLEANUP(ResetDPCache());
       
  3841 			TEST_FAIL;
       
  3842 			}			
       
  3843 		TESTDEBUG(test.Printf(_L("After SetCacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x\n"), 
       
  3844 															setMin >> gPageShift, setMax >> gPageShift));
       
  3845 		
       
  3846 		GetOriginalPageCount();
       
  3847 		genSucceed = CanGenSucceed();
       
  3848 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3849 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  3850 
       
  3851 		if (r != KErrNone)
       
  3852 			{
       
  3853 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
       
  3854 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3855 			CLEANUP(ResetDPCache());
       
  3856 			TEST_FAIL;
       
  3857 			}
       
  3858 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
       
  3859 			{
       
  3860 			test.Printf(_L("No Zones Switched off and should have been\n"));
       
  3861 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3862 			CLEANUP(ResetDPCache());
       
  3863 			TEST_FAIL;
       
  3864 			}
       
  3865 		else
       
  3866 			{
       
  3867 			test.Printf(_L("Passed...\n"));	
       
  3868 			}
       
  3869 
       
  3870 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  3871 		ResetDPCache();
       
  3872 		
       
  3873 		
       
  3874 		}
       
  3875 	else
       
  3876 		{
       
  3877 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  3878 		}
       
  3879 	TestEnd();
       
  3880 
       
  3881 	test.Next(_L("Test8: Defrag fragmented memory for various smaller chunk sizes"));	
       
  3882 	TInt chunkSize = 0x80000;
       
  3883 	while (chunkSize >= 0x4000)
       
  3884 		{
       
  3885 		test.Printf(_L("chunkSize = %dKB\n"), chunkSize/1024);
       
  3886 		TestStart();
       
  3887 		r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, chunkSize, EFalse);
       
  3888 		r = FreeMovable(gChunkArray1, gChunkArraySize1);
       
  3889 		
       
  3890 		GetOriginalPageCount();
       
  3891 		TBool genSucceed = CanGenSucceed();
       
  3892 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3893 		TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  3894 		
       
  3895 		if (r != KErrNone)
       
  3896 			{
       
  3897 			test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
       
  3898 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3899 			TEST_FAIL;
       
  3900 			}
       
  3901 		else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
       
  3902 			{
       
  3903 			test.Printf(_L("No Zones Switched off and should have been\n"));
       
  3904 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3905 			TEST_FAIL;
       
  3906 			}
       
  3907 		else
       
  3908 			{
       
  3909 			test.Printf(_L("Passed...\n"));	
       
  3910 			}
       
  3911 
       
  3912 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  3913 		TestEnd();
       
  3914 		chunkSize = chunkSize>>1; 
       
  3915 		}
       
  3916 
       
  3917 	test.Next(_L("Test9: Defrag fragmented memory "));	
       
  3918 	TestStart();
       
  3919 	
       
  3920 	r = AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
       
  3921 	r = FreeMovable(gChunkArray1, gChunkArraySize1);
       
  3922 	
       
  3923 	GetOriginalPageCount();
       
  3924 	genSucceed = CanGenSucceed();
       
  3925 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  3926 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  3927 
       
  3928 	if (r != KErrNone)
       
  3929 		{
       
  3930 		test.Printf(_L("Fail: r = %d, expected = %d\n"),r,KErrNone);
       
  3931 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3932 		TEST_FAIL;
       
  3933 		}
       
  3934 	else if (genSucceed && CheckZonesSwitchedOff() == EFalse)
       
  3935 		{
       
  3936 		test.Printf(_L("No Zones Switched off and should have been\n"));
       
  3937 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3938 		TEST_FAIL;
       
  3939 		}
       
  3940 	else
       
  3941 		{
       
  3942 		test.Printf(_L("Passed...\n"));	
       
  3943 		}
       
  3944 
       
  3945 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  3946 	TestEnd();
       
  3947 
       
  3948 	test.End();
       
  3949 	return KErrNone;
       
  3950 	}
       
  3951 
       
  3952 
       
  3953 //
       
  3954 // TestGetRamZonePageCount
       
  3955 //
       
  3956 //---------------------------------------------------------------------------------------------------------------------
       
  3957 //! @SYMTestCaseID				KBASE-t_ramdefrag-0529
       
  3958 //! @SYMTestType				CIT
       
  3959 //! @SYMTestCaseDesc			Verifying the implementation of the function GetRamZonePageCount()
       
  3960 //! @SYMPREQ					PREQ308
       
  3961 //! @SYMTestPriority			High
       
  3962 //! @SYMTestActions				
       
  3963 //! 	1.	Fragment the memory. Following this, call GetRamZonePageCount() on every zone and verify
       
  3964 //!			the values with the test HAL functions. 
       
  3965 //! 	2.	Fragment the memory. Following this, call function with an valid aID
       
  3966 //! 
       
  3967 //! @SYMTestExpectedResults
       
  3968 //! 	1.	KErrNone
       
  3969 //! 	2.	KErrArgument
       
  3970 //---------------------------------------------------------------------------------------------------------------------
       
  3971 TInt TestGetRamZonePageCount()
       
  3972 	{
       
  3973 	test.Start(_L("Test1: Call GetRamZonePageCount() on every zone one after the other"));	
       
  3974 	TestStart();	
       
  3975 		
       
  3976 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  3977 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  3978 	
       
  3979 	for (TUint index = 0; index < gZoneCount; index++)
       
  3980 		{
       
  3981 		GetAllPageInfo();
       
  3982 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  3983 
       
  3984 		STestUserSidePageCount pageData;
       
  3985 		TInt r = Ldd.PageCount(zoneID, &pageData);
       
  3986 		TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
       
  3987 		if (r != KErrNone)
       
  3988 			{
       
  3989 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  3990 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  3991 			TEST_FAIL;
       
  3992 			}
       
  3993 		
       
  3994 		if (pageData.iFreePages != gZoneUtilArray[index].iFreePages ||
       
  3995 			pageData.iFixedPages != gZoneUtilArray[index].iAllocFixed ||
       
  3996 			pageData.iMovablePages != gZoneUtilArray[index].iAllocMovable ||
       
  3997 			pageData.iDiscardablePages != gZoneUtilArray[index].iAllocDiscardable)
       
  3998 			{
       
  3999 			test.Printf(_L("RAM zone page count does not match test HAL page count, Zone %d\n"), zoneID);
       
  4000 			test.Printf(_L("PgCnt: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
       
  4001 									pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages);
       
  4002 			test.Printf(_L("HalFunc: free = 0x%x, fixed = 0x%x, movable= 0x%x, discard = 0x%x\n"),
       
  4003 									gZoneUtilArray[index].iFreePages, gZoneUtilArray[index].iAllocFixed, 
       
  4004 									gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);
       
  4005 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4006 			TEST_FAIL;
       
  4007 			}
       
  4008 		else
       
  4009 			{
       
  4010 			test.Printf(_L("Zone %d Passed...\n"), zoneID);	
       
  4011 			}
       
  4012 		TESTDEBUG(test.Printf(_L("iFreePages = 0x%x, iFixedPages = 0x%x, iMovablePages = 0x%x, iDiscardablePages = 0x%x\n"),
       
  4013 						pageData.iFreePages, pageData.iFixedPages, pageData.iMovablePages, pageData.iDiscardablePages));
       
  4014 		}	
       
  4015 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4016 	TestEnd();
       
  4017 	
       
  4018 	test.Next(_L("Test2: Call GetRamZonePageCount() with an invalid aID "));	
       
  4019 	TestStart();	
       
  4020 		
       
  4021 	TUint zoneID = KInvalidZoneID;
       
  4022 	STestUserSidePageCount pageData;
       
  4023 	TInt r = Ldd.PageCount(zoneID, &pageData);
       
  4024 	TESTDEBUG(test.Printf(_L("Page count function r = %d\n"), r));
       
  4025 	if (r != KErrArgument)
       
  4026 		{
       
  4027 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  4028 		TEST_FAIL;
       
  4029 		}
       
  4030 	else
       
  4031 		{
       
  4032 		test.Printf(_L("Passed...\n"));	
       
  4033 		}
       
  4034 	TestEnd();
       
  4035 	
       
  4036 	test.End();
       
  4037 	return 0;
       
  4038 	}
       
  4039 
       
  4040 const TUint KIndex2Alloc = 10;
       
  4041 const TUint KTestMaxPages = 6;
       
  4042 
       
  4043 //
       
  4044 // DefragMaxPagesSetup
       
  4045 //
       
  4046 // Arranges the memory layout in preparation for TestDefragRamMaxPages()
       
  4047 //
       
  4048 // WARNING THIS WILL BE UNRELIABLE IF aMaxPages > KIndex2Alloc
       
  4049 // 
       
  4050 TInt DefragMaxPagesSetup(TInt aPageType, TUint aMaxPages, TUint& aIndex, TUint& aIndex2)
       
  4051 	{
       
  4052 	TInt r = KErrNoMemory;
       
  4053 	
       
  4054 	// Get the preference order and determine the 2 most preferable zones
       
  4055 	// that are currently not in use.
       
  4056 	GetPrefOrder();
       
  4057 	TUint freeInUsePages = 0;
       
  4058 	TBool zonesFound = EFalse;
       
  4059 	for (TUint i = 1; i < gZoneCount; i++)
       
  4060 		{
       
  4061 		aIndex = gPrefArray[i];
       
  4062 		aIndex2 = gPrefArray[i-1];
       
  4063 		TUint indexFree = gZoneUtilArray[aIndex].iFreePages;
       
  4064 		TUint index2Free = gZoneUtilArray[aIndex2].iFreePages;
       
  4065 		if (indexFree == gZoneUtilArray[aIndex].iPhysPages &&
       
  4066 			index2Free == gZoneUtilArray[aIndex2].iPhysPages &&
       
  4067 			indexFree >= aMaxPages && index2Free >= KIndex2Alloc &&
       
  4068 			freeInUsePages >= KIndex2Alloc + aMaxPages)
       
  4069 			{
       
  4070 			zonesFound = ETrue;
       
  4071 			break;
       
  4072 			}
       
  4073 		freeInUsePages += index2Free;
       
  4074 		}	
       
  4075 	
       
  4076 	// Could suitable RAM zones be found.
       
  4077 	if (!zonesFound)
       
  4078 		{
       
  4079 		test.Printf(_L("Insufficient memory - Skipping test...\n"));
       
  4080 		return KErrNoMemory;
       
  4081 		}
       
  4082 
       
  4083 	if (aPageType == BEST_MOVABLE)
       
  4084 		{		
       
  4085 		// Allocate KIndex2Alloc movable pages to aIndex2
       
  4086 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, aIndex2, KIndex2Alloc);
       
  4087 		if (r != KErrNone)
       
  4088 			{
       
  4089 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
       
  4090 			r = KErrNoMemory;
       
  4091 			goto error;
       
  4092 			}
       
  4093 
       
  4094 			
       
  4095 
       
  4096 		// Now allow movable pages to be allocated into the least preferable RAM zone under test only.
       
  4097 		GetAllPageInfo();
       
  4098 		if (aMaxPages >= gZoneUtilArray[aIndex].iFreePages)
       
  4099 			{
       
  4100 			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
       
  4101 			r = KErrNoMemory;
       
  4102 			goto error;
       
  4103 			}
       
  4104 		// Allocate aMaxPages movable pages to aIndex
       
  4105 		r = ZoneAllocMovable(gChunkArray2, gChunkArraySize2, aIndex, aMaxPages);
       
  4106 		if (r != KErrNone)
       
  4107 			{
       
  4108 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
       
  4109 			r = KErrNoMemory;
       
  4110 			goto error;
       
  4111 			}
       
  4112 		
       
  4113 		// Determine how many free pages there are in the RAM zones more preferable
       
  4114 		// than the RAM zones under test.
       
  4115 		GetPrefOrder();
       
  4116 		freeInUsePages = 0;
       
  4117 		for (TUint i = 0; i < gZoneCount; i++)
       
  4118 			{
       
  4119 			TUint tmpIndex = gPrefArray[i];
       
  4120 			if (tmpIndex == aIndex2)
       
  4121 				break;
       
  4122 			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
       
  4123 			}
       
  4124 		// Verify that the RAM layout is still suitable for the test.
       
  4125 		if (gZoneUtilArray[aIndex].iAllocMovable != aMaxPages ||
       
  4126 			gZoneUtilArray[aIndex2].iAllocMovable != KIndex2Alloc ||
       
  4127 			gZoneUtilArray[aIndex].iAllocDiscardable || gZoneUtilArray[aIndex].iAllocFixed || 
       
  4128 			gZoneUtilArray[aIndex2].iAllocDiscardable || gZoneUtilArray[aIndex2].iAllocFixed ||
       
  4129 			freeInUsePages < KIndex2Alloc + aMaxPages)
       
  4130 			{
       
  4131 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
       
  4132 			r = KErrNoMemory;
       
  4133 			goto error;
       
  4134 			}
       
  4135 
       
  4136 		// now reset all the flags
       
  4137 		ResetRamZoneFlags();
       
  4138 
       
  4139 		// Perform a general defrag
       
  4140 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
       
  4141 		goto exit;
       
  4142 		}
       
  4143 		
       
  4144 	if (aPageType == BEST_DISCARDABLE)
       
  4145 		{
       
  4146 
       
  4147 		TUint minCacheSize = 0;
       
  4148 		TUint maxCacheSize = 0;
       
  4149 		TUint currentCacheSize = 0;
       
  4150 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  4151 		test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  4152 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift);
       
  4153 					
       
  4154 
       
  4155 	
       
  4156 		// Allocate a number of discardable pages to the 2nd least preferable zone under test
       
  4157 		TInt disPages;
       
  4158 		r = ZoneAllocDiscard(aIndex2, KIndex2Alloc, disPages);
       
  4159 		if (r != KErrNone)
       
  4160 			{
       
  4161 			test.Printf(_L("ZoneAllocDiscard() r = %d KIndex2Alloc = 0x%x disPages = 0x%x aIndex2 = %d - Skipping test...\n"),
       
  4162 							r, KIndex2Alloc, disPages, aIndex2);
       
  4163 			GetAllPageInfo();
       
  4164 			PrintPageInfo();
       
  4165 			r = KErrNoMemory;
       
  4166 			goto error;
       
  4167 			}
       
  4168 		TUint discFillBytes = KIndex2Alloc << gPageShift;
       
  4169 		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
       
  4170 		if (r != KErrNone)
       
  4171 			{
       
  4172 			test.Printf(_L("SetCacheSize r = 0x%x currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
       
  4173 							r, currentCacheSize + discFillBytes);
       
  4174 			GetAllPageInfo();
       
  4175 			PrintPageInfo();
       
  4176 			r = KErrNoMemory;
       
  4177 			goto error;
       
  4178 			}
       
  4179 
       
  4180 	
       
  4181 		// Allocate a discardable pages equal to aMaxPages to the least preferable zone under test
       
  4182 		GetAllPageInfo();
       
  4183 		if(aMaxPages >= gTotalPageCount.iFreePages)
       
  4184 			{
       
  4185 			test.Printf(_L("Insufficient memory available - Skipping test...\n"));
       
  4186 			r = KErrNoMemory;
       
  4187 			goto error;
       
  4188 			}
       
  4189 		TUint allocPages = aMaxPages + KIndex2Alloc;
       
  4190 		r = ZoneAllocDiscard(aIndex, aMaxPages, disPages);
       
  4191 		if (r != KErrNone)
       
  4192 			{
       
  4193 			test.Printf(_L("ZoneAllocDiscard() r = %d aMaxPages = 0x%x disPages = 0x%x aIndex = %d - Skipping test...\n"),
       
  4194 							r, aMaxPages, disPages, aIndex);
       
  4195 			GetAllPageInfo();
       
  4196 			PrintPageInfo();
       
  4197 			r = KErrNoMemory;
       
  4198 			goto error;
       
  4199 			}
       
  4200 		discFillBytes = allocPages << gPageShift;
       
  4201 		r = DPTest::SetCacheSize(currentCacheSize + discFillBytes, currentCacheSize + discFillBytes);
       
  4202 		if (r != KErrNone)
       
  4203 			{
       
  4204 			test.Printf(_L("SetCacheSize r = %d currentCacheSize + discFillBytes = 0x%x - Skipping test...\n"),
       
  4205 							r, currentCacheSize + discFillBytes);
       
  4206 			GetAllPageInfo();
       
  4207 			PrintPageInfo();
       
  4208 			r = KErrNoMemory;
       
  4209 			goto error;
       
  4210 			}
       
  4211 
       
  4212 
       
  4213 		// Determine how many free pages there are in the RAM zones more preferable
       
  4214 		// than the RAM zones under test.
       
  4215 		GetPrefOrder();
       
  4216 		freeInUsePages = 0;
       
  4217 		for (TUint i = 0; i < gZoneCount; i++)
       
  4218 			{
       
  4219 			TUint tmpIndex = gPrefArray[i];
       
  4220 			if (tmpIndex == aIndex2)
       
  4221 				break;
       
  4222 			freeInUsePages += gZoneUtilArray[tmpIndex].iFreePages;
       
  4223 			}
       
  4224 		// Verify that the RAM layout is still suitable for the test.
       
  4225 		if (r != KErrNone || gZoneUtilArray[aIndex].iAllocDiscardable != aMaxPages ||
       
  4226 			gZoneUtilArray[aIndex2].iAllocDiscardable != KIndex2Alloc ||
       
  4227 			gZoneUtilArray[aIndex].iAllocMovable || gZoneUtilArray[aIndex].iAllocFixed || 
       
  4228 			gZoneUtilArray[aIndex2].iAllocMovable || gZoneUtilArray[aIndex2].iAllocFixed ||
       
  4229 			freeInUsePages < KIndex2Alloc + aMaxPages)
       
  4230 			{
       
  4231 			test.Printf(_L("Insufficient memory - Skipping test...\n"));
       
  4232 			r = KErrNoMemory;
       
  4233 			goto error;
       
  4234 			}
       
  4235 
       
  4236 		// now reset all the flags
       
  4237 		ResetRamZoneFlags();
       
  4238 
       
  4239 		// Perform a general defrag with maxPages = KTestMaxPages
       
  4240 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, KTestMaxPages);
       
  4241 		goto exit;
       
  4242 
       
  4243 		}
       
  4244 error:
       
  4245 	// Reset all the flags
       
  4246 	ResetRamZoneFlags();
       
  4247 exit:
       
  4248 	return r;
       
  4249 	}
       
  4250 
       
  4251 //
       
  4252 // TestDefragRamMaxPages
       
  4253 //
       
  4254 //---------------------------------------------------------------------------------------------------------------------
       
  4255 //! @SYMTestCaseID				KBASE-t_ramdefrag-0530
       
  4256 //! @SYMTestType				CIT
       
  4257 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::DefragRam()
       
  4258 //! @SYMPREQ					PREQ308
       
  4259 //! @SYMTestPriority			High
       
  4260 //! @SYMTestActions				
       
  4261 //! 	1.	Call function with invalid aMaxPages 
       
  4262 //! 	2.	Call DefragRam when aMaxPages < number of movable pages in least preferable zone 
       
  4263 //! 	3.	Call DefragRam when aMaxPages > number of movable pages in least preferable zone
       
  4264 //! 	4.	Call DefragRam when aMaxPages = number of movable pages in least preferable zone
       
  4265 //! 	5.	Call DefragRam when aMaxPages < number of discardable pages in least preferable zone 
       
  4266 //! 	6.	Call DefragRam when aMaxPages > number of discardable pages in least preferable zone
       
  4267 //! 	7.	Call DefragRam when aMaxPages = number of discardable pages in least preferable zone
       
  4268 //! 
       
  4269 //! @SYMTestExpectedResults
       
  4270 //! 	1.	KErrArgument
       
  4271 //! 	2.	Least preferable zone and 2nd least preferable zone have not been emptied
       
  4272 //! 	3.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
       
  4273 //! 	4.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
       
  4274 //! 	5.	Least preferable zone and 2nd least preferable zone have not been emptied
       
  4275 //! 	6.	Least preferable zone has been emptied and 2nd least preferable zone has not been emptied
       
  4276 //! 	7.	Least preferable zone has been emptied and 2nd least preferable zone not not been emptied
       
  4277 //---------------------------------------------------------------------------------------------------------------------
       
  4278 TInt TestDefragRamMaxPages()
       
  4279 	{
       
  4280 	test.Start(_L("Test1: Call DefragRam with invalid aMaxPages "));	
       
  4281 	TestStart();	
       
  4282 	
       
  4283 	gDefragMaxPages = -1;
       
  4284 	
       
  4285 	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4286 	TESTDEBUG(test.Printf(_L("After calling defrag r = %d\n"), r));
       
  4287 	if (r != KErrArgument)
       
  4288 		{
       
  4289 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  4290 		TEST_FAIL;
       
  4291 		}
       
  4292 	else
       
  4293 		{
       
  4294 		test.Printf(_L("Passed...\n"));	
       
  4295 		}	
       
  4296 	TestEnd();
       
  4297 	
       
  4298 	test.Next(_L("Test2: Call DefragRam aMaxPages < number of movable pages in least preferable zone "));	
       
  4299 	TestStart();	
       
  4300 	gChunkArray1 = new RChunk;
       
  4301 	gChunkArraySize1 = 1;
       
  4302 	gChunkArray2 = new RChunk;
       
  4303 	gChunkArraySize2 = 1;
       
  4304 	TUint index = 0;
       
  4305 	TUint index2 = 0;
       
  4306 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages + 1, index, index2);	
       
  4307 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4308 		{// Cannot perform test with fixed pages in least preferable zones
       
  4309 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4310 		}
       
  4311 	else
       
  4312 		{
       
  4313 		if (r == KErrNone)
       
  4314 			{
       
  4315 			GetAllPageInfo();
       
  4316 			if (gZoneUtilArray[index].iAllocMovable < KTestMaxPages + 1 ||
       
  4317 				gZoneUtilArray[index2].iAllocMovable < KIndex2Alloc)
       
  4318 				{
       
  4319 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4320 										index, KTestMaxPages + 1, index2, KIndex2Alloc);
       
  4321 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4322 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4323 				CLEANUP(Ldd.FreeFromAllZones());
       
  4324 				TEST_FAIL;
       
  4325 				}
       
  4326 
       
  4327 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4328 			// if there is free space elsewhere
       
  4329 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4330 			GetAllPageInfo();
       
  4331 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages + 1 &&
       
  4332 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
       
  4333 				{
       
  4334 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4335 										index, 0, index2, 0);
       
  4336 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4337 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4338 				CLEANUP(Ldd.FreeFromAllZones());
       
  4339 				TEST_FAIL;
       
  4340 				}
       
  4341 			else
       
  4342 				{
       
  4343 				test.Printf(_L("Passed...\n"));
       
  4344 				}
       
  4345 			}
       
  4346 		else 
       
  4347 			{
       
  4348 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4349 			}
       
  4350 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4351 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
  4352 		}
       
  4353 	Ldd.FreeFromAllZones();
       
  4354 	TestEnd();
       
  4355 
       
  4356 
       
  4357 	test.Next(_L("Test3: Call DefragRam aMaxPages > number of movable pages in least preferable zone "));	
       
  4358 	TestStart();	
       
  4359 	gChunkArray1 = new RChunk;
       
  4360 	gChunkArraySize1 = 1;
       
  4361 	gChunkArray2 = new RChunk;
       
  4362 	gChunkArraySize2 = 1;
       
  4363 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages - 1, index, index2);
       
  4364 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4365 		{// Cannot perform test with fixed pages in least preferable zones
       
  4366 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4367 		}
       
  4368 	else
       
  4369 		{		
       
  4370 		if (r == KErrNone)
       
  4371 			{
       
  4372 			GetAllPageInfo();
       
  4373 			if (gZoneUtilArray[index].iAllocMovable != 0)
       
  4374 				{
       
  4375 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4376 										index, 0, index2, KIndex2Alloc);
       
  4377 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4378 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4379 				CLEANUP(Ldd.FreeFromAllZones());
       
  4380 				TEST_FAIL;		
       
  4381 				}
       
  4382 			
       
  4383 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4384 			// if there is free space elsewhere
       
  4385 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4386 			
       
  4387 			GetAllPageInfo();
       
  4388 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages - 1 &&
       
  4389 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
       
  4390 				{
       
  4391 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4392 										index, 0, index2, 0);
       
  4393 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4394 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4395 				CLEANUP(Ldd.FreeFromAllZones());
       
  4396 				TEST_FAIL;		
       
  4397 				}
       
  4398 			else
       
  4399 				{
       
  4400 				test.Printf(_L("Passed...\n"));
       
  4401 				}
       
  4402 			}
       
  4403 		else 
       
  4404 			{
       
  4405 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4406 			}	
       
  4407 		
       
  4408 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4409 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
  4410 		}
       
  4411 	Ldd.FreeFromAllZones();
       
  4412 	TestEnd();
       
  4413 
       
  4414 
       
  4415 	test.Next(_L("Test4: Call DefragRam aMaxPages = number of movable pages in least preferable zone "));	
       
  4416 	TestStart();	
       
  4417 	gChunkArray1 = new RChunk;
       
  4418 	gChunkArraySize1 = 1;
       
  4419 	gChunkArray2 = new RChunk;
       
  4420 	gChunkArraySize2 = 1;
       
  4421 	r = DefragMaxPagesSetup(BEST_MOVABLE, KTestMaxPages, index, index2);
       
  4422 	if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4423 		{// Cannot perform test with fixed pages in least preferable zones
       
  4424 		test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4425 		}
       
  4426 	else
       
  4427 		{		
       
  4428 		if (r == KErrNone)
       
  4429 			{
       
  4430 			GetAllPageInfo();
       
  4431 			if (gZoneUtilArray[index].iAllocMovable != 0)
       
  4432 				{
       
  4433 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4434 										index, 0, index2, KIndex2Alloc);
       
  4435 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4436 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4437 				CLEANUP(Ldd.FreeFromAllZones());
       
  4438 				TEST_FAIL;		
       
  4439 				}
       
  4440 
       
  4441 			// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4442 			// if there is free space elsewhere
       
  4443 			r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4444 			
       
  4445 			GetAllPageInfo();
       
  4446 			if (gTotalPageCount.iFreePages > KIndex2Alloc + KTestMaxPages &&
       
  4447 				(gZoneUtilArray[index].iAllocMovable != 0 || gZoneUtilArray[index2].iAllocMovable != 0))
       
  4448 				{
       
  4449 				test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4450 										index, 0, index2, 0);
       
  4451 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4452 				CLEANUP(RemoveChunkAlloc(gChunkArray2, gChunkArraySize2));
       
  4453 				CLEANUP(Ldd.FreeFromAllZones());
       
  4454 				TEST_FAIL;		
       
  4455 				}
       
  4456 			else 
       
  4457 				{
       
  4458 				test.Printf(_L("Passed...\n"));
       
  4459 				}
       
  4460 			}
       
  4461 		else 
       
  4462 			{
       
  4463 			test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4464 			}
       
  4465 		RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4466 		RemoveChunkAlloc(gChunkArray2, gChunkArraySize2);
       
  4467 		}
       
  4468 	Ldd.FreeFromAllZones();
       
  4469 	TestEnd();
       
  4470 	
       
  4471 	test.Next(_L("Test5: Call DefragRam aMaxPages < number of discardable pages in least preferable zone "));	
       
  4472 	TestStart();	
       
  4473 	if (gPagedRom)
       
  4474 		{
       
  4475 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages + 1, index, index2);
       
  4476 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4477 			{// Cannot perform test with fixed pages in least preferable zones
       
  4478 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4479 			}
       
  4480 		else
       
  4481 			{
       
  4482 			if (r == KErrNone)
       
  4483 				{
       
  4484 				GetAllPageInfo();
       
  4485 				if (gZoneUtilArray[index].iAllocDiscardable < KTestMaxPages + 1 ||
       
  4486 					gZoneUtilArray[index2].iAllocDiscardable < KIndex2Alloc)
       
  4487 					{
       
  4488 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4489 											index, KTestMaxPages + 1, index2, KIndex2Alloc);
       
  4490 					CLEANUP(ResetDPCache());
       
  4491 					CLEANUP(Ldd.FreeFromAllZones());
       
  4492 					TEST_FAIL;		
       
  4493 					}
       
  4494 				
       
  4495 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4496 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4497 				
       
  4498 				GetAllPageInfo();
       
  4499 				if (gZoneUtilArray[index].iAllocDiscardable != 0 ||
       
  4500 					gZoneUtilArray[index2].iAllocDiscardable != 0)
       
  4501 					{
       
  4502 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4503 											index, 0, index2, 0);
       
  4504 					CLEANUP(ResetDPCache());
       
  4505 					CLEANUP(Ldd.FreeFromAllZones());
       
  4506 					TEST_FAIL;		
       
  4507 					}
       
  4508 				else
       
  4509 					{
       
  4510 					test.Printf(_L("Passed...\n"));
       
  4511 					}
       
  4512 				}
       
  4513 			else 
       
  4514 				{
       
  4515 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4516 				}
       
  4517 			ResetDPCache();
       
  4518 			}
       
  4519 		Ldd.FreeFromAllZones();
       
  4520 		}
       
  4521 	else
       
  4522 		{
       
  4523 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
       
  4524 		}
       
  4525 	TestEnd();
       
  4526 
       
  4527 	test.Next(_L("Test6: Call DefragRam aMaxPages > number of discardable pages in least preferable zone "));	
       
  4528 	TestStart();	
       
  4529 	
       
  4530 	if (gPagedRom)
       
  4531 		{
       
  4532 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages - 1, index, index2);
       
  4533 		
       
  4534 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4535 			{// Cannot perform test with fixed pages in least preferable zones
       
  4536 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4537 			}
       
  4538 		else
       
  4539 			{	
       
  4540 			if (r == KErrNone)
       
  4541 				{
       
  4542 				GetAllPageInfo();
       
  4543 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
       
  4544 					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
       
  4545 					{
       
  4546 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4547 											index, 0, index2, KIndex2Alloc);
       
  4548 					CLEANUP(ResetDPCache());
       
  4549 					CLEANUP(Ldd.FreeFromAllZones());
       
  4550 					TEST_FAIL;		
       
  4551 					}
       
  4552 				
       
  4553 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4554 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4555 				GetAllPageInfo();
       
  4556 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
       
  4557 					gZoneUtilArray[index2].iAllocDiscardable != 0)
       
  4558 					{
       
  4559 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4560 											index, 0, index2, 0);
       
  4561 					CLEANUP(ResetDPCache());
       
  4562 					CLEANUP(Ldd.FreeFromAllZones());
       
  4563 					TEST_FAIL;		
       
  4564 					}
       
  4565 				else
       
  4566 					{
       
  4567 					test.Printf(_L("Passed...\n"));
       
  4568 					}
       
  4569 				}
       
  4570 			else 
       
  4571 				{
       
  4572 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4573 				}	
       
  4574 			ResetDPCache();
       
  4575 			}
       
  4576 		Ldd.FreeFromAllZones();
       
  4577 		}
       
  4578 	else
       
  4579 		{
       
  4580 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
       
  4581 		}
       
  4582 	TestEnd();
       
  4583 
       
  4584 
       
  4585 	test.Next(_L("Test4: Call DefragRam aMaxPages = number of discardable pages in least preferable zone "));	
       
  4586 	TestStart();	
       
  4587 	
       
  4588 	if(gPagedRom)
       
  4589 		{
       
  4590 		r = DefragMaxPagesSetup(BEST_DISCARDABLE, KTestMaxPages, index, index2);
       
  4591 		
       
  4592 		if (gZoneUtilArray[index].iAllocFixed > 0 || gZoneUtilArray[index2].iAllocFixed > 0)
       
  4593 			{// Cannot perform test with fixed pages in least preferable zones
       
  4594 			test.Printf(_L("Fixed pages in least preferable zone - Skipping...\n"));
       
  4595 			}
       
  4596 		else
       
  4597 			{	
       
  4598 			if (r == KErrNone)
       
  4599 				{
       
  4600 				GetAllPageInfo();
       
  4601 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
       
  4602 					gZoneUtilArray[index2].iAllocDiscardable != KIndex2Alloc)
       
  4603 					{
       
  4604 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4605 											index, 0, index2, KIndex2Alloc);
       
  4606 					CLEANUP(ResetDPCache());
       
  4607 					CLEANUP(Ldd.FreeFromAllZones());
       
  4608 					TEST_FAIL;		
       
  4609 					}
       
  4610 
       
  4611 				// Double check that if DefragRam is called with maxPages = 0, these zones are emptied
       
  4612 				r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4613 				GetAllPageInfo();
       
  4614 				if (gZoneUtilArray[index].iAllocDiscardable != 0||
       
  4615 					gZoneUtilArray[index2].iAllocDiscardable != 0)
       
  4616 					{
       
  4617 					test.Printf(_L("Fail: index = %d, expected = 0x%x, index2 = %d, expected = 0x%x\n"), 
       
  4618 											index, 0, index2, 0);
       
  4619 					CLEANUP(ResetDPCache());
       
  4620 					CLEANUP(Ldd.FreeFromAllZones());
       
  4621 					TEST_FAIL;		
       
  4622 					}
       
  4623 				else
       
  4624 					{
       
  4625 					test.Printf(_L("Passed...\n"));
       
  4626 					}
       
  4627 				}
       
  4628 			else 
       
  4629 				{
       
  4630 				test.Printf(_L("DefragMaxPagesSetup failed r = %d\n"), r);
       
  4631 				}
       
  4632 			ResetDPCache();
       
  4633 			}
       
  4634 		Ldd.FreeFromAllZones();
       
  4635 		}
       
  4636 	else
       
  4637 		{
       
  4638 		test.Printf(_L("Not a Paged Rom - Skipping...\n"));
       
  4639 		}
       
  4640 	TestEnd();
       
  4641 
       
  4642 	test.End();
       
  4643 	return KErrNone;
       
  4644 	}
       
  4645 
       
  4646 
       
  4647 //
       
  4648 // TestEmptyRamZone
       
  4649 //
       
  4650 //---------------------------------------------------------------------------------------------------------------------
       
  4651 //! @SYMTestCaseID				KBASE-t_ramdefrag-0531
       
  4652 //! @SYMTestType				CIT
       
  4653 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()
       
  4654 //! @SYMPREQ					PREQ308
       
  4655 //! @SYMTestPriority			High
       
  4656 //! @SYMTestActions				
       
  4657 //! 	1.	Fragment the memory. Call function with a valid aID, 
       
  4658 //! 		i.e. a zone which lies within the range of zones. 
       
  4659 //! 	2.	Fragment the memory. Call function with an invalid aID
       
  4660 //! 		i.e. a zone which does not exist within the range of zones. 
       
  4661 //! 	3.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
       
  4662 //! 		Call the zone specific defrag again on the same zone whilst the other defrag is still 
       
  4663 //!			running on that zone. 
       
  4664 //! 	4.	Fragment the memory. Following this, call the TRamDefragRequest::DefragRam() to perform general defrag. 
       
  4665 //! 		Following this call zone specific defrag whilst the general defrag is still running. 
       
  4666 //! 	5.	Fragment the memory. Following this, call a zone specific defrag on a particular zone. 
       
  4667 //! 		Whilst the zone defrag is running, call a general defrag
       
  4668 //! 	6:  Fragment the memory. Following this, call the function on specifc zone and 
       
  4669 //! 		at the same time allocate pages to the zone
       
  4670 //! 	7.	Fragment the memory. Call EmptyRamZone() on every zone one after the other. 
       
  4671 //! 	8.	Fragment the memory. Allocate a couple of fixed pages to a zone that contains movable pages. 
       
  4672 //! 		Following this, Call EmptyRamZone() on that zone. 
       
  4673 //! 
       
  4674 //! @SYMTestExpectedResults
       
  4675 //! 	1.	KErrNone
       
  4676 //! 	2.	KErrArgument
       
  4677 //! 	3.	KErrNone and the zone has been emptied
       
  4678 //! 	4.	Zone has not been emptied, as the general defrag has already placed pages in the zone
       
  4679 //! 	5.	Zone has been emptied
       
  4680 //! 	6.	KErrNone and pages have been allocated in the zone
       
  4681 //! 	7.	One of the following scenarios should occur:
       
  4682 //!			a.	If the cache has not reached its minimum size, then all the movable pages should 
       
  4683 //!				be moved from the zone if there are enough free pages in the other zones. 
       
  4684 //!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
       
  4685 //!				are enough free pages in the other zones. 
       
  4686 //!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is 
       
  4687 //!				not less than the minimum cache size, then all the discardable pages should be discarded of. 
       
  4688 //!				However, when demand paging is off, all discardable pages should be removed from 
       
  4689 //!				the zone. 
       
  4690 //! 	8.	KErrNoMemory, however all movable and discardable pages have been moved from the zone 
       
  4691 //---------------------------------------------------------------------------------------------------------------------
       
  4692 TInt TestEmptyRamZone()
       
  4693 	{
       
  4694 	TInt r = KErrNone;
       
  4695 	TInt r2 = KErrNone;
       
  4696 
       
  4697 	test.Start(_L("Test1: Call EmptyRamZone with valid aID "));	
       
  4698 	TestStart();	
       
  4699 	
       
  4700 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  4701 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  4702 	
       
  4703 	GetAllPageInfo();
       
  4704 	TUint index = gZoneCount - 1;
       
  4705 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  4706 		{
       
  4707 		-- index;
       
  4708 		}
       
  4709 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
       
  4710 	test.Printf(_L("Zone ID = %d\n"), defragZoneID);
       
  4711 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  4712 		{
       
  4713 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  4714 		}
       
  4715 	else
       
  4716 		{
       
  4717 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
       
  4718 		GetAllPageInfo();
       
  4719 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  4720 		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
       
  4721 			{
       
  4722 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  4723 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4724 			TEST_FAIL;
       
  4725 			}
       
  4726 		else
       
  4727 			{
       
  4728 			test.Printf(_L("Passed...\n"));	
       
  4729 			}
       
  4730 		}
       
  4731 
       
  4732 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  4733 	TestEnd();
       
  4734 	
       
  4735 	
       
  4736 	test.Next(_L("Test2: Call EmptyRamZone with invalid aID "));	
       
  4737 	TestStart();	
       
  4738 	gChunkArray1 = new RChunk;
       
  4739 	gChunkArraySize1 = 1;
       
  4740 	defragZoneID = KInvalidZoneID; 
       
  4741 
       
  4742 	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
       
  4743 	
       
  4744 	if (r != KErrArgument)
       
  4745 		{
       
  4746 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  4747 		TEST_FAIL;
       
  4748 		}
       
  4749 	else
       
  4750 		{
       
  4751 		test.Printf(_L("Passed...\n"));	
       
  4752 		}
       
  4753 	
       
  4754 	TestEnd();
       
  4755 	
       
  4756 	
       
  4757 	test.Next(_L("Test3: Call EmptyRamZone twice at the same time "));	
       
  4758 	TestStart();	
       
  4759 	
       
  4760 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  4761 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  4762 
       
  4763 	GetAllPageInfo();
       
  4764 	index = gZoneCount - 1;
       
  4765 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  4766 		{
       
  4767 		-- index;
       
  4768 		}
       
  4769 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  4770 	
       
  4771 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  4772 		{
       
  4773 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  4774 		}
       
  4775 	else
       
  4776 		{
       
  4777 		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny *)defragZoneID);
       
  4778 		if (r != KErrNone)
       
  4779 			{
       
  4780 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  4781 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4782 			TEST_FAIL;
       
  4783 			}		
       
  4784 				
       
  4785 		gTestThread.Logon(status);
       
  4786 		gTestThread.Resume();
       
  4787 		
       
  4788 		User::After(10);
       
  4789 
       
  4790 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
       
  4791 		
       
  4792 		User::WaitForRequest(status);
       
  4793 		r2 = status.Int();
       
  4794 
       
  4795 		GetAllPageInfo();
       
  4796 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  4797 		if (r != r2 || 
       
  4798 			(freeInOthers && (r != KErrNone || r2 != KErrNone || !CheckZoneIsOff(index))))
       
  4799 			{
       
  4800 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d has not been emptied\n"), 
       
  4801 									r, r2, KErrNone, defragZoneID);
       
  4802 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
       
  4803 			TEST_FAIL;
       
  4804 			}	
       
  4805 		else
       
  4806 			{
       
  4807 			test.Printf(_L("Passed...\n"));	
       
  4808 			}
       
  4809 		
       
  4810 		gTestThread.Close();	
       
  4811 		}
       
  4812 	
       
  4813 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  4814 	TestEnd();
       
  4815 
       
  4816 		
       
  4817 	test.Next(_L("Test4: Call general defrag and zone defrag at the same time "));	
       
  4818 	TestStart();	
       
  4819 		
       
  4820 	TInt waitTime = 10000;
       
  4821 	
       
  4822 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  4823 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  4824 
       
  4825 	GetAllPageInfo();
       
  4826 	index = gZoneCount - 1;
       
  4827 	while (index > 0 && gZoneUtilArray[index].iAllocFixed > 0)
       
  4828 		{
       
  4829 		index--;
       
  4830 		}
       
  4831 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  4832 	
       
  4833 	if (index == 0 && gZoneUtilArray[index].iAllocFixed > 0)
       
  4834 		{
       
  4835 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  4836 		}
       
  4837 	else
       
  4838 		{
       
  4839 		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  4840 		if (r != KErrNone)
       
  4841 			{
       
  4842 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  4843 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4844 			TEST_FAIL;
       
  4845 			}		
       
  4846 		gTestThread.Logon(status);
       
  4847 				
       
  4848 		gTestThread.Resume();
       
  4849 		User::After(waitTime);
       
  4850 		
       
  4851 		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
       
  4852 
       
  4853 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
       
  4854 
       
  4855 		User::WaitForRequest(status);
       
  4856 		r2 = status.Int();
       
  4857 
       
  4858 		GetAllPageInfo();
       
  4859 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  4860 		if (r2 != KErrNone || 
       
  4861 			(freeInOthers && (r != KErrNone || !CheckZoneIsOff(index))))
       
  4862 			{
       
  4863 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone %d is on unexpectedly\n"),
       
  4864 							r, r2, KErrNone, defragZoneID);
       
  4865 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
       
  4866 			TEST_FAIL;
       
  4867 			}	
       
  4868 		else
       
  4869 			{
       
  4870 			test.Printf(_L("Passed...\n"));	
       
  4871 			}	
       
  4872 
       
  4873 		gTestThread.Close();	
       
  4874 		}
       
  4875 
       
  4876 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4877 	TestEnd();
       
  4878 	
       
  4879 	
       
  4880 	test.Next(_L("Test5: Call zone defrag and general defrag at the same time "));	
       
  4881 	TestStart();	
       
  4882 	
       
  4883 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  4884 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  4885 
       
  4886 	GetAllPageInfo();
       
  4887 	index = gZoneCount - 1;
       
  4888 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  4889 		{
       
  4890 		-- index;
       
  4891 		}
       
  4892 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  4893 
       
  4894 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  4895 		{
       
  4896 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  4897 		}
       
  4898 	else
       
  4899 		{
       
  4900 		r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
       
  4901 		if (r != KErrNone)
       
  4902 			{
       
  4903 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  4904 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4905 			TEST_FAIL;
       
  4906 			}		
       
  4907 		gTestThread.Logon(status);
       
  4908 				
       
  4909 		gTestThread.Resume();
       
  4910 		User::After(0);
       
  4911 		
       
  4912 		TESTDEBUG(test.Printf(_L("Zone defrag running on main thread\n")));
       
  4913 		
       
  4914 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, gDefragMaxPages);
       
  4915 
       
  4916 		User::WaitForRequest(status);
       
  4917 		r2 = status.Int();
       
  4918 
       
  4919 		GetAllPageInfo();
       
  4920 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  4921 		if (r != KErrNone ||
       
  4922 			(freeInOthers && (r2 != KErrNone || !CheckZoneIsOff(index))))
       
  4923 			{
       
  4924 			test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone 0x%x is on unexpectedly\n"),
       
  4925 							r, r2, KErrNone, defragZoneID);
       
  4926 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4927 			TEST_FAIL;
       
  4928 			}
       
  4929 		else
       
  4930 			{
       
  4931 			test.Printf(_L("Passed...\n"));	
       
  4932 			}
       
  4933 
       
  4934 		gTestThread.Close();	
       
  4935 		}
       
  4936 			
       
  4937 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  4938 	TestEnd();
       
  4939 
       
  4940 
       
  4941 	test.Next(_L("Test6: Call function TRamDefragRequest::EmptyRamZone on specifc zone and at the same time allocate pages to the zone"));	
       
  4942 	TestStart();	
       
  4943 	
       
  4944 	TInt pagesAlloc = 1; // Try and allocate just one page whilst trying to empty the zone. 
       
  4945 
       
  4946 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  4947 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  4948 
       
  4949 	GetAllPageInfo();
       
  4950 	index = gZoneCount - 1;
       
  4951 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  4952 		{
       
  4953 		-- index;
       
  4954 		}
       
  4955 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  4956 	
       
  4957 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  4958 		{
       
  4959 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  4960 		}
       
  4961 	else
       
  4962 		{
       
  4963 		TInt numPassed = 0;
       
  4964 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
       
  4965 			{
       
  4966 			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
       
  4967 			if (r != KErrNone)
       
  4968 				{
       
  4969 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  4970 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4971 				TEST_FAIL;
       
  4972 				}
       
  4973 			gTestThread.Logon(status);
       
  4974 			
       
  4975 			gTestThread.Resume();
       
  4976 			User::After(waitTime);
       
  4977 			
       
  4978 			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
       
  4979 
       
  4980 			User::WaitForRequest(status);
       
  4981 			r2 = status.Int();
       
  4982 			
       
  4983 			if (r2 != KErrNone)
       
  4984 				{
       
  4985 				test.Printf(_L("Empty was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
       
  4986 				}
       
  4987 			else if (r != KErrNone)
       
  4988 				{
       
  4989 				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone = 0x%x\n"), 
       
  4990 										r, KErrNone, r2, KErrNone, defragZoneID);
       
  4991 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  4992 				CLEANUP(Ldd.FreeAllFixedPages());
       
  4993 				TEST_FAIL;
       
  4994 				}
       
  4995 			else
       
  4996 				{
       
  4997 				numPassed ++;
       
  4998 				}
       
  4999 							
       
  5000 			Ldd.FreeAllFixedPages();
       
  5001 
       
  5002 			gTestThread.Close();	
       
  5003 			}
       
  5004 		if (numPassed > 0)
       
  5005 			{
       
  5006 			test.Printf(_L("Passed...\n"));	
       
  5007 			}
       
  5008 		}
       
  5009 			
       
  5010 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5011 	TestEnd();
       
  5012 
       
  5013 
       
  5014 	test.Next(_L("Test7: Call function TRamDefragRequest::EmptyRamZone on every zone one after the other"));	
       
  5015 	TestStart();
       
  5016 		
       
  5017 	Ldd.ResetDriver();
       
  5018 			
       
  5019 	for (index = 0; index < gZoneCount; index ++)
       
  5020 		{
       
  5021 		// Variables for DP ROM cache sizes
       
  5022 		TUint minCacheSize = 0;
       
  5023 		TUint maxCacheSize = 0;
       
  5024 		TUint currentCacheSize = 0;
       
  5025 
       
  5026 		if (gPagedRom)
       
  5027 			{
       
  5028 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  5029 			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5030 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5031 			// Calculate the maximum size available for the DP cache.
       
  5032 			TUint minZoneFree = KMaxTUint;
       
  5033 			TUint maxDisFillBytes = 0;
       
  5034 			GetAllPageInfo();
       
  5035 			for (TUint i = 0; i < gZoneCount; i++)
       
  5036 				{
       
  5037 				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
       
  5038 				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
       
  5039 				if (minZoneFree > gZoneUtilArray[i].iFreePages)
       
  5040 					minZoneFree = gZoneUtilArray[i].iFreePages;
       
  5041 				}
       
  5042 			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
       
  5043 			maxDisFillBytes <<= gPageShift;
       
  5044 
       
  5045 			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
       
  5046 			if (r != KErrNone)
       
  5047 				{
       
  5048 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5049 				CLEANUP(ResetDPCache());
       
  5050 				TEST_FAIL;
       
  5051 				}
       
  5052 			
       
  5053 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  5054 			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5055 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5056 			if (currentCacheSize != maxDisFillBytes)
       
  5057 				{
       
  5058 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5059 				CLEANUP(ResetDPCache());
       
  5060 				TEST_FAIL;
       
  5061 				}
       
  5062 			
       
  5063 			GetAllPageInfo();
       
  5064 			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
       
  5065 
       
  5066 			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
       
  5067 			if (r != KErrNone)
       
  5068 				{
       
  5069 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5070 				CLEANUP(ResetDPCache());
       
  5071 				TEST_FAIL;
       
  5072 				}
       
  5073 			
       
  5074 			// Check all zones have no free pages.
       
  5075 			GetAllPageInfo();
       
  5076 			for (TUint i = 0; i < gZoneCount; i++)
       
  5077 				{
       
  5078 				if (gZoneUtilArray[i].iFreePages != 0)
       
  5079 					{
       
  5080 					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5081 					CLEANUP(ResetDPCache());
       
  5082 					TEST_FAIL;
       
  5083 					}
       
  5084 				}
       
  5085 			}
       
  5086 		else
       
  5087 			{
       
  5088 			// Fragment the RAM with some movable pages
       
  5089 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
       
  5090 			FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5091 			}
       
  5092 		
       
  5093 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  5094 		
       
  5095 		GetAllPageInfo();
       
  5096 		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
       
  5097 		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
       
  5098 		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
       
  5099 
       
  5100 		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
       
  5101 						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
       
  5102 						origMovInZone, origFreeInZone);
       
  5103 
       
  5104 
       
  5105 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
       
  5106 
       
  5107 		GetAllPageInfo();		
       
  5108 		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
       
  5109 		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
       
  5110 		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
       
  5111 	
       
  5112 		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5113 		if(movablePages && freeInOtherZones)
       
  5114 			{
       
  5115 			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
       
  5116 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5117 			CLEANUP(ResetDPCache());
       
  5118 			TEST_FAIL;
       
  5119 			}
       
  5120 
       
  5121 		if (origUnknownInZone != unknownPages)
       
  5122 			{
       
  5123 			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
       
  5124 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5125 			CLEANUP(ResetDPCache());
       
  5126 			TEST_FAIL;
       
  5127 			}
       
  5128 
       
  5129 		if (!gPagedRom)
       
  5130 			{
       
  5131 			if (discPages && freeInOtherZones)
       
  5132 				{
       
  5133 				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
       
  5134 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5135 				CLEANUP(ResetDPCache());
       
  5136 				TEST_FAIL;
       
  5137 				}
       
  5138 			}
       
  5139 		else
       
  5140 			{
       
  5141 			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
       
  5142 			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5143 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5144 			// The the discardable pages should have been discarded or moved unless 
       
  5145 			// there is no room in the other zones and the cache size is already at its minimum
       
  5146 			if (discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
       
  5147 				{
       
  5148 				test.Printf(_L("Fail: Zone 0x%x all the discardable pages haven't been moved\n"), zoneID);
       
  5149 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5150 				CLEANUP(ResetDPCache());
       
  5151 				TEST_FAIL;
       
  5152 				}
       
  5153 			}
       
  5154 		test.Printf(_L("Passed...\n"));
       
  5155 		}
       
  5156 	// TestEnd() willl cleanup what's required
       
  5157 	TestEnd();
       
  5158 
       
  5159 	test.Next(_L("Test8: Call function TRamDefragRequest::EmptyRamZone on a zone that contains fixed and movable pages"));
       
  5160 	TestStart();
       
  5161 	
       
  5162 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5163 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5164 
       
  5165 	GetAllPageInfo();	
       
  5166 	index = gZoneCount - 1;
       
  5167 	while (index > 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
       
  5168 		{	
       
  5169 		-- index;
       
  5170 		}
       
  5171 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5172 	
       
  5173 	if (index == 0 && (gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
       
  5174 		{
       
  5175 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5176 		}
       
  5177 	else
       
  5178 		{
       
  5179 		test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID);		
       
  5180 		test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
       
  5181 						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable);	
       
  5182 
       
  5183 		
       
  5184 		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
       
  5185 		
       
  5186 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, defragZoneID);
       
  5187 		
       
  5188 		GetAllPageInfo();
       
  5189 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5190 		if (r != KErrNoMemory || 
       
  5191 			(freeInOthers && 
       
  5192 			(gZoneUtilArray[index].iAllocMovable ||	
       
  5193 			gZoneUtilArray[index].iAllocDiscardable)))
       
  5194 			{
       
  5195 			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
       
  5196 							r, KErrNoMemory);
       
  5197 			CLEANUP(Ldd.FreeAllFixedPages());
       
  5198 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5199 			TEST_FAIL;
       
  5200 			}
       
  5201 		else
       
  5202 			{
       
  5203 			test.Printf(_L("Passed..."));
       
  5204 			}
       
  5205 		
       
  5206 		Ldd.FreeAllFixedPages();
       
  5207 		}
       
  5208 	
       
  5209 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  5210 	TestEnd();
       
  5211 
       
  5212 	test.End();
       
  5213 	return r;
       
  5214 	}
       
  5215 
       
  5216 
       
  5217 //
       
  5218 // TestClaimRamZone
       
  5219 //
       
  5220 //---------------------------------------------------------------------------------------------------------------------
       
  5221 //! @SYMTestCaseID				KBASE-t_ramdefrag-0532
       
  5222 //! @SYMTestType				CIT
       
  5223 //! @SYMTestCaseDesc			Verifying the implemntation of the function TRamDefragRequest::ClaimRamZone()
       
  5224 //! @SYMPREQ					PREQ308
       
  5225 //! @SYMTestPriority			High
       
  5226 //! @SYMTestActions				
       
  5227 //! 	1.	Fragment the memory. Following this, call function with a valid aID
       
  5228 //! 	2.	Fragment the memory. Following this, call function with an invalid aID
       
  5229 //! 	3.	Fragment the memory. Following this, start a zone defrag and whilst this 
       
  5230 //! 		is running, call function on the same zone. 
       
  5231 //! 	4.	Fragment the memory. Following this, start a general defrag and whilst this 
       
  5232 //! 		is running, call function on the same zone.
       
  5233 //! 	5.	Fragment the memory. Following this, call ClaimRamZone() on a specific zone, whilst at the
       
  5234 //! 		same time calling ClaimRamZone() on another zone. 
       
  5235 //! 	6:	Fragment the memory. Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the 
       
  5236 //! 		same time allocate pages to the zone
       
  5237 //!		7.	Fragment the memory. Following this allocate fixed pages to a zone and attempt to claim
       
  5238 //!			the zone. 
       
  5239 //!		8.	Fragment the memory. Following this call ClaimRamZone() on every zone, one after the other
       
  5240 //! 
       
  5241 //! @SYMTestExpectedResults
       
  5242 //! 	1.	KErrNone
       
  5243 //! 	2.	KErrArgument
       
  5244 //! 	3.	KErrNone and the zone has been claimed
       
  5245 //! 	4.	KErrNone and the zone has been claimed
       
  5246 //! 	5.	KErrNone and both zones have been claimed
       
  5247 //! 	6.	KErrNoMemory
       
  5248 //!		7.	KErrNoMemory, all movable and discardable pages have been moved
       
  5249 //!		8.	One of the following scenarios should occur:
       
  5250 //!			a.	If the cache has not reached its minimum size and there are no unknown pages in 
       
  5251 //!				the zone, then all the movable pages should be moved from the zone if there are enough free pages
       
  5252 //!				in the other zones. 
       
  5253 //!				However, when demand paging is off, all movable pages should be moved from the zone, again if there 
       
  5254 //!				are enough free pages in the other zones and if there are no unknown pages in the zone. 
       
  5255 //!			b.	If the current size of the cache minus the number of discardable pages in the zone being emptied is not 
       
  5256 //!				less than the minimum cache size and there are no unknown pages in the zone, 
       
  5257 //!				then all the discardable pages should be discarded of. 
       
  5258 //!				However, when demand paging is off, all discardable pages should be removed from 
       
  5259 //!				the zone if there are no unknown pages in the zone. 
       
  5260 //---------------------------------------------------------------------------------------------------------------------
       
  5261 TInt TestClaimRamZone()
       
  5262 	{
       
  5263 	TInt r = 0;
       
  5264 	TInt r2 = 0;
       
  5265 	TUint32 addr = 0;
       
  5266 	TInt waitTime = 1000;
       
  5267 	
       
  5268 	test.Start(_L("Test1: Call function TRamDefragRequest::ClaimRamZone with a valid aID"));	
       
  5269 	TestStart();	
       
  5270 	
       
  5271 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5272 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5273 
       
  5274 	GetAllPageInfo();
       
  5275 	TUint index = gZoneCount - 1;
       
  5276 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  5277 		{
       
  5278 		-- index;
       
  5279 		}
       
  5280 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5281 	
       
  5282 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  5283 		{
       
  5284 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5285 		}
       
  5286 	else
       
  5287 		{
       
  5288 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
       
  5289 		GetAllPageInfo();
       
  5290 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5291 		if (freeInOthers && r != KErrNone && 
       
  5292 			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
       
  5293 			{
       
  5294 			test.Printf(_L("Fail: Zone 0x%x has not been claimed r %d expected %d\n"), defragZoneID, r, KErrNone);
       
  5295 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5296 			TEST_FAIL;
       
  5297 			}
       
  5298 		else
       
  5299 			{
       
  5300 			test.Printf(_L("Passed...\n"));	
       
  5301 			}
       
  5302 
       
  5303 		addr = gZoneConfigArray[index].iPhysBase;
       
  5304 		if (r == KErrNone)
       
  5305 			{
       
  5306 			r = Ldd.FreeAllFixedPages();
       
  5307 			}
       
  5308 		}
       
  5309 	
       
  5310 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  5311 	TestEnd();
       
  5312 
       
  5313 
       
  5314 	test.Next(_L("Test2: Call function TRamDefragRequest::ClaimRamZone with an invalid aID"));	
       
  5315 	TestStart();	
       
  5316 	gChunkArray1 = new RChunk;
       
  5317 	gChunkArraySize1 = 1;
       
  5318 	defragZoneID = KInvalidZoneID;
       
  5319 	
       
  5320 	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);	
       
  5321 	if (r != KErrArgument)
       
  5322 		{
       
  5323 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  5324 		TEST_FAIL;
       
  5325 		}
       
  5326 	else
       
  5327 		{
       
  5328 		test.Printf(_L("Passed...\n"));	
       
  5329 		}	
       
  5330 	
       
  5331 	TestEnd();
       
  5332 
       
  5333 
       
  5334 	test.Next(_L("Test3: Call function TRamDefragRequest::ClaimRamZone when a EmptyRamZone is already running"));	
       
  5335 	TestStart();	
       
  5336 	
       
  5337 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5338 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5339 
       
  5340 	GetAllPageInfo();
       
  5341 	index = gZoneCount - 1;
       
  5342 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  5343 		{
       
  5344 		-- index;
       
  5345 		}
       
  5346 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5347 
       
  5348 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  5349 		{
       
  5350 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5351 		}
       
  5352 	else
       
  5353 		{
       
  5354 		TInt numPassed = 0;
       
  5355 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
       
  5356 			{
       
  5357 			r = gTestThread.Create(gTestThreadName,MultiEmptyZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
       
  5358 			if (r != KErrNone)
       
  5359 				{
       
  5360 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5361 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5362 				TEST_FAIL;
       
  5363 				}
       
  5364 
       
  5365 			gTestThread.Logon(status);
       
  5366 			gTestThread.Resume();
       
  5367 			User::After(waitTime);
       
  5368 			
       
  5369 			r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
       
  5370 
       
  5371 			User::WaitForRequest(status);
       
  5372 			r2 = status.Int();
       
  5373 			
       
  5374 			GetAllPageInfo();
       
  5375 			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5376 			if (freeInOthers && r != KErrNone && r2 != KErrNone)
       
  5377 				{
       
  5378 				test.Printf(_L("Fail: r = %d, r2 = %d, expected = %d, or zone ID 0x%x has not been claimed\n"), 
       
  5379 										r, r2, KErrNone, defragZoneID);
       
  5380 				if (r == KErrNone)
       
  5381 					{
       
  5382 					CLEANUP(r = Ldd.FreeAllFixedPages());
       
  5383 					}	
       
  5384 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5385 				TEST_FAIL;
       
  5386 				}
       
  5387 			else
       
  5388 				{
       
  5389 				numPassed ++;
       
  5390 				}
       
  5391 			
       
  5392 			if (r == KErrNone)
       
  5393 				{
       
  5394 				r = Ldd.FreeAllFixedPages();
       
  5395 				}		
       
  5396 			
       
  5397 			gTestThread.Close();	
       
  5398 			}
       
  5399 		if (numPassed > 0)
       
  5400 			{
       
  5401 			test.Printf(_L("Passed...\n"));	
       
  5402 			}
       
  5403 		}
       
  5404 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5405 	TestEnd();
       
  5406 
       
  5407 
       
  5408 	test.Next(_L("Test4: Call function TRamDefragRequest::ClaimRamZone when a general defrag is already running"));	
       
  5409 	TestStart();	
       
  5410 	
       
  5411 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5412 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5413 
       
  5414 	GetAllPageInfo();
       
  5415 	index = gZoneCount - 1;
       
  5416 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  5417 		{
       
  5418 		-- index;
       
  5419 		}
       
  5420 
       
  5421 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  5422 		{
       
  5423 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5424 		}
       
  5425 	else
       
  5426 		{
       
  5427 		defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5428 
       
  5429 		r = gTestThread.Create(gTestThreadName,MultiGenDefragThreadFunc,KDefaultStackSize,0x1000,0x1000,NULL);
       
  5430 		if (r != KErrNone)
       
  5431 			{
       
  5432 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5433 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5434 			TEST_FAIL;
       
  5435 			}
       
  5436 		
       
  5437 		gTestThread.Logon(status);
       
  5438 		gTestThread.Resume();
       
  5439 		User::After(waitTime);
       
  5440 
       
  5441 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
       
  5442 		
       
  5443 		User::WaitForRequest(status);
       
  5444 		r2 = status.Int();
       
  5445 		
       
  5446 		if (r != KErrNone)
       
  5447 			{
       
  5448 			test.Printf(_L("ClaimZone: r = %d, expected = %d\n"), r, KErrNone);
       
  5449 			}	
       
  5450 		
       
  5451 		if (r2 != KErrNone)
       
  5452 			{
       
  5453 			test.Printf(_L("General: r2 = %d, expected = %d\n"), r, KErrNone);
       
  5454 			}
       
  5455 
       
  5456 		GetAllPageInfo();
       
  5457 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5458 		if (freeInOthers && gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages)
       
  5459 			{
       
  5460 			test.Printf(_L("Fail: Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
       
  5461 										defragZoneID, r, r2, KErrNone);
       
  5462 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));	
       
  5463 			TEST_FAIL;
       
  5464 			}
       
  5465 		else
       
  5466 			{
       
  5467 			test.Printf(_L("Passed...\n"));	
       
  5468 			}
       
  5469 
       
  5470 		if (r == KErrNone)
       
  5471 			{
       
  5472 			r = Ldd.FreeAllFixedPages();
       
  5473 			}
       
  5474 					
       
  5475 		gTestThread.Close();	
       
  5476 		}
       
  5477 	
       
  5478 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  5479 	TestEnd();
       
  5480 
       
  5481 
       
  5482 
       
  5483 	test.Next(_L("Test5: Call function TRamDefragRequest::ClaimRamZone on specifc zone at the same time as calling on another zone"));	
       
  5484 	TestStart();	
       
  5485 	
       
  5486 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5487 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5488 	
       
  5489 	GetAllPageInfo();
       
  5490 	index = gZoneCount - 1;
       
  5491 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  5492 		{
       
  5493 		-- index;
       
  5494 		}
       
  5495 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5496 	test.Printf(_L("index = 0x%x "), index);
       
  5497 
       
  5498 	TUint index2 = gZoneCount - 1;
       
  5499 	while (	index2 > 0 &&  
       
  5500 			(index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0))
       
  5501 		{
       
  5502 		-- index2;
       
  5503 		}
       
  5504 	TUint defragZoneID2 = gZoneConfigArray[index2].iZoneId;
       
  5505 	test.Printf(_L("index2 = %d\n"), index2);
       
  5506 
       
  5507 	if ((index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) || 
       
  5508 		(index2 == 0 && (index == index2 || gZoneUtilArray[index2].iAllocFixed != 0 || gZoneUtilArray[index2].iAllocUnknown != 0)))
       
  5509 		{
       
  5510 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5511 		}
       
  5512 	else
       
  5513 		{
       
  5514 		r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)(defragZoneID2));
       
  5515 		if (r != KErrNone)
       
  5516 			{
       
  5517 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5518 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5519 			TEST_FAIL;
       
  5520 			}
       
  5521 
       
  5522 		gTestThread.Logon(status);
       
  5523 		gTestThread.Resume();
       
  5524 		User::After(waitTime);
       
  5525 		
       
  5526 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
       
  5527 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  5528 
       
  5529 		User::WaitForRequest(status);
       
  5530 		r2 = status.Int();
       
  5531 		
       
  5532 		GetAllPageInfo();
       
  5533 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5534 		TUint freeInOthers2 = gTotalPageCount.iFreePages - gZoneUtilArray[index2].iFreePages;
       
  5535 		if ((freeInOthers && r != KErrNone && 
       
  5536 			gZoneUtilArray[index].iAllocFixed != gZoneUtilArray[index].iPhysPages) ||
       
  5537 			(freeInOthers2 && r != KErrNone && 
       
  5538 			gZoneUtilArray[index2].iAllocFixed != gZoneUtilArray[index2].iPhysPages))
       
  5539 			{
       
  5540 			test.Printf(_L("Fail: Zone ID 0x%x or Zone ID 0x%x has not been claimed, r = %d, r2 = %d, expected = %d\n"), 
       
  5541 									defragZoneID, defragZoneID2, r, r2, KErrNone);
       
  5542 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5543 			TEST_FAIL;
       
  5544 			}
       
  5545 		else
       
  5546 			{
       
  5547 			test.Printf(_L("Passed...\n"));	
       
  5548 			}
       
  5549 		
       
  5550 		if (r == KErrNone)
       
  5551 			{
       
  5552 			r = Ldd.FreeAllFixedPages();
       
  5553 			}
       
  5554 
       
  5555 		if (r2 == KErrNone)
       
  5556 			{// Have to free from specific address as RAM zone claimed by other channel.
       
  5557 			addr = gZoneConfigArray[index2].iPhysBase;
       
  5558 			r = Ldd.FreeFromAddr(gZoneUtilArray[index2].iAllocFixed, addr);
       
  5559 			}		
       
  5560 
       
  5561 		gTestThread.Close();	
       
  5562 		}
       
  5563 	
       
  5564 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  5565 	TestEnd();
       
  5566 
       
  5567 
       
  5568 	test.Next(_L("Test6: Call function TRamDefragRequest::ClaimRamZone on specifc zone and at the same time allocate pages to the zone"));	
       
  5569 	TestStart();		
       
  5570 	
       
  5571 	TInt pagesAlloc = 1; // Try and allocate just one page whilst attempting to claim the zone
       
  5572 
       
  5573 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5574 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5575 
       
  5576 	GetAllPageInfo();
       
  5577 	index = gZoneCount - 1;
       
  5578 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  5579 		{
       
  5580 		-- index;
       
  5581 		}
       
  5582 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  5583 		{
       
  5584 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5585 		}
       
  5586 	else
       
  5587 		{
       
  5588 		GetAllPageInfo();
       
  5589 		defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5590 		TInt numPassed = 0;
       
  5591 		for (waitTime = 1000; waitTime > 0; waitTime-=10)
       
  5592 			{
       
  5593 			r = gTestThread.Create(gTestThreadName,MultiClaimZoneThreadFunc,KDefaultStackSize,0x1000,0x1000,(TAny*)defragZoneID);
       
  5594 			if (r != KErrNone)
       
  5595 			{
       
  5596 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5597 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5598 			TEST_FAIL;
       
  5599 			}
       
  5600 
       
  5601 			gTestThread.Logon(status);
       
  5602 			gTestThread.Resume();
       
  5603 			test(status.Int() == KRequestPending);
       
  5604 			User::After(waitTime);
       
  5605 			
       
  5606 			r = Ldd.ZoneAllocDiscontiguous(defragZoneID, pagesAlloc);
       
  5607 
       
  5608 			TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  5609 			
       
  5610 			User::WaitForRequest(status);
       
  5611 			r2 = status.Int();
       
  5612 				
       
  5613 			GetAllPageInfo();
       
  5614 			TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5615 			if (freeInOthers && r2 != KErrNone)
       
  5616 				{
       
  5617 				test.Printf(_L("Claim was unsuccsessful: r2 = %d, expected = %d\n"), r2, KErrNone);
       
  5618 				}
       
  5619 			else if (r2 == KErrNone && r != KErrNoMemory)
       
  5620 				{
       
  5621 				test.Printf(_L("Fail: r = %d, expected = %d, r2 = %d, expected = %d, zone ID = 0x%x\n"), 
       
  5622 										r, KErrNoMemory, r2, KErrNone, defragZoneID);
       
  5623 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5624 				CLEANUP(Ldd.FreeAllFixedPages());
       
  5625 				// Free from address as a different channel claimed the RAM zone.
       
  5626 				addr = gZoneConfigArray[index].iPhysBase;
       
  5627 				CLEANUP(Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr));
       
  5628 				TEST_FAIL;
       
  5629 				}
       
  5630 			else
       
  5631 				{
       
  5632 				numPassed ++;
       
  5633 				}
       
  5634 			
       
  5635 			Ldd.FreeAllFixedPages();
       
  5636 		
       
  5637 			GetAllPageInfo();
       
  5638 			if (r2 == KErrNone)
       
  5639 				{// Free from address as a different channel claimed the RAM zone.
       
  5640 				addr = gZoneConfigArray[index].iPhysBase;
       
  5641 				r = Ldd.FreeFromAddr(gZoneUtilArray[index].iAllocFixed, addr);
       
  5642 				}
       
  5643 			gTestThread.Close();
       
  5644 			}
       
  5645 		if (numPassed > 0)
       
  5646 			{
       
  5647 			test.Printf(_L("Passed...\n"));	
       
  5648 			}	
       
  5649 		}	
       
  5650 	
       
  5651 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5652 	TestEnd();
       
  5653 
       
  5654 
       
  5655 
       
  5656 	test.Next(_L("Test7: Call function TRamDefragRequest::ClaimRamZone on a zone that contains fixed and movable pages"));
       
  5657 	TestStart();
       
  5658 	
       
  5659 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5660 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5661 
       
  5662 	GetAllPageInfo();
       
  5663 	index = gZoneCount - 1;
       
  5664 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
       
  5665 			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3))
       
  5666 			{
       
  5667 			-- index;
       
  5668 			}
       
  5669 
       
  5670 	if (index == 0 && ((gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0) &&
       
  5671 			(gZoneUtilArray[index].iAllocMovable < 10 || gZoneUtilArray[index].iFreePages < 3)))
       
  5672 		{
       
  5673 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5674 		}
       
  5675 	else
       
  5676 		{
       
  5677 		defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5678 		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"), defragZoneID));		
       
  5679 		TESTDEBUG(test.Printf(_L("movable = 0x%x discardable = 0x%x\n"), 
       
  5680 						gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocDiscardable));	
       
  5681 
       
  5682 		
       
  5683 		Ldd.ZoneAllocDiscontiguous(defragZoneID, 2); // Allocated 2 fixed pages to ensure that zone cannot be emptied
       
  5684 		
       
  5685 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, defragZoneID);
       
  5686 		GetAllPageInfo();
       
  5687 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5688 		if (r != KErrNoMemory || 
       
  5689 			(freeInOthers && 
       
  5690 			(gZoneUtilArray[index].iAllocMovable || 
       
  5691 			gZoneUtilArray[index].iAllocDiscardable)))
       
  5692 			{
       
  5693 			test.Printf(_L("r = %d, expected = %d, or all movable/discardable pages have not been moved"), 
       
  5694 							r, KErrNoMemory);
       
  5695 			CLEANUP(Ldd.FreeAllFixedPages());
       
  5696 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5697 			TEST_FAIL;
       
  5698 			}
       
  5699 		else
       
  5700 			{
       
  5701 			test.Printf(_L("Passed...\n"));
       
  5702 			}
       
  5703 		
       
  5704 		Ldd.FreeAllFixedPages();
       
  5705 		}
       
  5706 
       
  5707 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5708 	TestEnd();
       
  5709 
       
  5710 	test.Next(_L("Test8: Call function TRamDefragRequest::ClaimRamZone on every zone one after the other"));
       
  5711 	TestStart();
       
  5712 		
       
  5713 	Ldd.ResetDriver();
       
  5714 			
       
  5715 	for (index = 0; index < gZoneCount; index ++)
       
  5716 		{
       
  5717 		// Variables for DP ROM cache sizes
       
  5718 		TUint minCacheSize = 0;
       
  5719 		TUint maxCacheSize = 0;
       
  5720 		TUint currentCacheSize = 0;
       
  5721 
       
  5722 		if (gPagedRom)
       
  5723 			{
       
  5724 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  5725 			TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5726 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5727 			// Calculate the maximum size of the DP cache 
       
  5728 			TUint minZoneFree = KMaxTUint;
       
  5729 			TUint maxDisFillBytes = 0;
       
  5730 			GetAllPageInfo();
       
  5731 			for (TUint i = 0; i < gZoneCount; i++)
       
  5732 				{
       
  5733 				maxDisFillBytes += gZoneUtilArray[i].iFreePages;
       
  5734 				maxDisFillBytes += gZoneUtilArray[i].iAllocDiscardable;
       
  5735 				if (minZoneFree > gZoneUtilArray[i].iFreePages)
       
  5736 					minZoneFree = gZoneUtilArray[i].iFreePages;
       
  5737 				}
       
  5738 			test.Printf(_L("Free pages 0x%x maxDisFillBytes 0x%x\n"), gTotalPageCount.iFreePages, maxDisFillBytes);
       
  5739 			maxDisFillBytes <<= gPageShift;
       
  5740 
       
  5741 			r = DPTest::SetCacheSize(maxDisFillBytes, maxDisFillBytes);
       
  5742 			if (r != KErrNone)
       
  5743 				{
       
  5744 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5745 				CLEANUP(ResetDPCache());
       
  5746 				TEST_FAIL;
       
  5747 				}
       
  5748 			
       
  5749 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  5750 			TESTDEBUG(test.Printf(_L("CacheSize2: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5751 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5752 			if (currentCacheSize != maxDisFillBytes)
       
  5753 				{
       
  5754 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5755 				CLEANUP(ResetDPCache());
       
  5756 				TEST_FAIL;
       
  5757 				}
       
  5758 			
       
  5759 			GetAllPageInfo();
       
  5760 			test.Printf(_L("Free pages after alloc discardable1 = 0x%x\n"), gTotalPageCount.iFreePages);
       
  5761 
       
  5762 			r = DPTest::SetCacheSize(minZoneFree << gPageShift, maxDisFillBytes);
       
  5763 			if (r != KErrNone)
       
  5764 				{
       
  5765 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5766 				CLEANUP(ResetDPCache());
       
  5767 				TEST_FAIL;
       
  5768 				}
       
  5769 			
       
  5770 			// Check all zones have no free pages.
       
  5771 			GetAllPageInfo();
       
  5772 			for (TUint i = 0; i < gZoneCount; i++)
       
  5773 				{
       
  5774 				if (gZoneUtilArray[i].iFreePages != 0)
       
  5775 					{
       
  5776 					test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  5777 					CLEANUP(ResetDPCache());
       
  5778 					TEST_FAIL;
       
  5779 					}
       
  5780 				}
       
  5781 			}
       
  5782 		else
       
  5783 			{
       
  5784 			// Fragment the RAM with some movable pages
       
  5785 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
       
  5786 			FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5787 			}
       
  5788 		
       
  5789 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  5790 		
       
  5791 		GetAllPageInfo();
       
  5792 		TUint origUnknownInZone = gZoneUtilArray[index].iAllocUnknown;
       
  5793 		TUint origMovInZone = gZoneUtilArray[index].iAllocMovable;
       
  5794 		TUint origFreeInZone = gZoneUtilArray[index].iFreePages;
       
  5795 
       
  5796 		test.Printf(_L("Zone ID 0x%x - fixedPages = 0x%x, unknownPages = 0x%x, discardablePages = 0x%x movable = 0x%x free = 0x%x\n"), 
       
  5797 						zoneID, gZoneUtilArray[index].iAllocFixed, origUnknownInZone, gZoneUtilArray[index].iAllocDiscardable, 
       
  5798 						origMovInZone, origFreeInZone);
       
  5799 
       
  5800 
       
  5801 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
       
  5802 
       
  5803 		GetAllPageInfo();		
       
  5804 		TUint unknownPages = gZoneUtilArray[index].iAllocUnknown;
       
  5805 		TUint discPages = gZoneUtilArray[index].iAllocDiscardable;
       
  5806 		TUint movablePages = gZoneUtilArray[index].iAllocMovable;
       
  5807 	
       
  5808 		if (r == KErrNone)
       
  5809 			{
       
  5810 			r = Ldd.FreeAllFixedPages();
       
  5811 			}
       
  5812 		TUint freeInOtherZones = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  5813 		if (!unknownPages && movablePages && freeInOtherZones)
       
  5814 			{
       
  5815 			test.Printf(_L("Fail: Zone ID %x all the movable pages haven't been moved\n"), zoneID);
       
  5816 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5817 			CLEANUP(ResetDPCache());
       
  5818 			TEST_FAIL;
       
  5819 			}
       
  5820 
       
  5821 		if (origUnknownInZone != unknownPages)
       
  5822 			{
       
  5823 			test.Printf(_L("Fail: Zone ID %x unknown pages before and after are not equal\n"), zoneID);
       
  5824 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5825 			CLEANUP(ResetDPCache());
       
  5826 			TEST_FAIL;
       
  5827 			}
       
  5828 
       
  5829 		if (!gPagedRom)
       
  5830 			{
       
  5831 			if (!unknownPages && freeInOtherZones && discPages)
       
  5832 				{
       
  5833 				test.Printf(_L("Fail: Zone ID %x all the discardable pages haven't been moved\n"), zoneID);
       
  5834 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5835 				CLEANUP(ResetDPCache());
       
  5836 				TEST_FAIL;
       
  5837 				}
       
  5838 			}
       
  5839 		else
       
  5840 			{
       
  5841 			DPTest::CacheSize(minCacheSize, maxCacheSize, currentCacheSize);
       
  5842 			TESTDEBUG(test.Printf(_L("CacheSize3: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  5843 										minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  5844 			// The the discardable pages should have been discarded or moved unless 
       
  5845 			// there is no room in the other zones and the cache size is already at its minimum
       
  5846 			if (!unknownPages && discPages && (freeInOtherZones || currentCacheSize != minCacheSize))
       
  5847 				{
       
  5848 				test.Printf(_L("Fail: Zone ID 0x%x all the discardable pages haven't been moved\n"), zoneID);
       
  5849 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5850 				CLEANUP(ResetDPCache());
       
  5851 				TEST_FAIL;
       
  5852 				}
       
  5853 			}
       
  5854 		test.Printf(_L("Passed...\n"));
       
  5855 		}
       
  5856 	// TestEnd() will cleanup what's required.
       
  5857 	TestEnd();
       
  5858 	test.End();
       
  5859 	return 0;
       
  5860 	}
       
  5861 
       
  5862 
       
  5863 //
       
  5864 // TestCancelDefrag
       
  5865 //
       
  5866 //-----------------------------------------------------------------------------------------------------------------------------------------
       
  5867 //! @SYMTestCaseID				KBASE-t_ramdefrag-0533
       
  5868 //! @SYMTestType				CIT
       
  5869 //! @SYMTestCaseDesc			Verifying the implementation of the function TRamDefragRequest::CancelDefrag()
       
  5870 //! @SYMPREQ					PREQ308
       
  5871 //! @SYMTestPriority			High
       
  5872 //! @SYMTestActions				
       
  5873 //! 	1.	Fragment the memory. Following this, start a general defrag and cancel it
       
  5874 //! 	2.	Fragment the memory. Following this, start a zone defrag and cancel it 
       
  5875 //! 	3.	Fragment the memory. Following this, start a claim zone and cancel it
       
  5876 //! 
       
  5877 //! @SYMTestExpectedResults
       
  5878 //! 	1.	KErrCancel
       
  5879 //! 	2.	KErrCancel
       
  5880 //! 	3.	KErrCancel
       
  5881 //-----------------------------------------------------------------------------------------------------------------------------------------
       
  5882 TInt TestCancelDefrag()
       
  5883 	{
       
  5884 	TInt r = 0;
       
  5885 	TUint defragZoneID = 0;
       
  5886 
       
  5887 	test.Start(_L("Test1: Call general defrag and cancel it"));	
       
  5888 	TestStart();	
       
  5889 	
       
  5890 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5891 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5892 	
       
  5893 	r = Ldd.CheckCancel(DEFRAG_TYPE_GEN);
       
  5894 
       
  5895 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  5896 	if (r != KErrCancel)
       
  5897 		{
       
  5898 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
       
  5899 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5900 		TEST_FAIL;
       
  5901 		}
       
  5902 	else
       
  5903 		{
       
  5904 		test.Printf(_L("Passed...\n"));	
       
  5905 		}
       
  5906 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5907 	TestEnd();
       
  5908 
       
  5909 
       
  5910 	test.Next(_L("Test2: Call zone defrag and cancel it"));	
       
  5911 	TestStart();		
       
  5912 	
       
  5913 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5914 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5915 
       
  5916 	GetAllPageInfo();
       
  5917 	TUint index = gZoneCount - 1;
       
  5918 	while (index > 0 && 
       
  5919 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
       
  5920 			gZoneUtilArray[index].iAllocFixed !=0 ||
       
  5921 			gZoneUtilArray[index].iAllocUnknown !=0))
       
  5922 		{	
       
  5923 		-- index; 
       
  5924 		}
       
  5925 	if (index == 0 && 
       
  5926 		(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
       
  5927 		gZoneUtilArray[index].iAllocFixed !=0 ||
       
  5928 		gZoneUtilArray[index].iAllocUnknown !=0))
       
  5929 		{
       
  5930 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5931 		}
       
  5932 	else
       
  5933 		{		
       
  5934 		defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5935 		TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
       
  5936 
       
  5937 		r = Ldd.CheckCancel(DEFRAG_TYPE_EMPTY, defragZoneID);
       
  5938 		
       
  5939 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  5940 		if (r != KErrCancel)
       
  5941 			{
       
  5942 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
       
  5943 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5944 			TEST_FAIL;
       
  5945 			}	
       
  5946 		else
       
  5947 			{
       
  5948 			test.Printf(_L("Passed...\n"));	
       
  5949 			}
       
  5950 		}	
       
  5951 
       
  5952 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5953 	TestEnd();
       
  5954 
       
  5955 
       
  5956 	test.Next(_L("Test3: Call Claim RAM Zone and cancel it"));	
       
  5957 	TestStart();
       
  5958 	
       
  5959 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  5960 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  5961 
       
  5962 	GetAllPageInfo();
       
  5963 	index = gZoneCount - 1;	
       
  5964 	while (index > 0 && 
       
  5965 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
       
  5966 			gZoneUtilArray[index].iAllocFixed !=0 ||
       
  5967 			gZoneUtilArray[index].iAllocUnknown !=0))
       
  5968 		{
       
  5969 		-- index;
       
  5970 		}
       
  5971 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  5972 	TESTDEBUG(test.Printf(_L("defragZoneID = 0x%x\n"),defragZoneID));
       
  5973 
       
  5974 	if (index == 0 && 
       
  5975 			(gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
       
  5976 			gZoneUtilArray[index].iAllocFixed !=0 ||
       
  5977 			gZoneUtilArray[index].iAllocUnknown !=0))
       
  5978 		{
       
  5979 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  5980 		}
       
  5981 	else
       
  5982 		{
       
  5983 		r = Ldd.CheckCancel(DEFRAG_TYPE_CLAIM, defragZoneID);
       
  5984 
       
  5985 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  5986 		if (r != KErrCancel)
       
  5987 			{
       
  5988 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrCancel);
       
  5989 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  5990 			TEST_FAIL;
       
  5991 			}	
       
  5992 		else
       
  5993 			{
       
  5994 			test.Printf(_L("Passed...\n"));	
       
  5995 			}
       
  5996 		}
       
  5997 
       
  5998 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  5999 	TestEnd(); 
       
  6000 
       
  6001 	test.End();
       
  6002 	return 0;
       
  6003 	}
       
  6004 
       
  6005 //
       
  6006 // TestZoneAllocNoAffect
       
  6007 //
       
  6008 // Ensures that fixed page allocations do not affect the movable
       
  6009 // or discardable page allocations
       
  6010 //
       
  6011 TInt TestZoneAllocNoAffect(TInt aZoneAllocType, TInt aPageType)
       
  6012 	{
       
  6013 	TInt retVal = KErrNone;
       
  6014 	TInt r = KErrNone;
       
  6015 	TInt mostPrefIndex = 0;
       
  6016 	TUint leastPrefIndex = 0;
       
  6017 	TUint leastPrefZoneID = 0;
       
  6018 
       
  6019 	TestStart();
       
  6020 	gChunkArray1 = new RChunk;
       
  6021 	gChunkArraySize1 = 1;
       
  6022 	switch(aPageType)
       
  6023 		{
       
  6024 		case BEST_MOVABLE:
       
  6025 			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of movable pages\n"));
       
  6026 			break;
       
  6027 
       
  6028 		case BEST_DISCARDABLE:
       
  6029 			test.Printf(_L("Test Epoc::ZoneAllocPhysicalRam() doesn't affect the allocation of discardable pages\n"));
       
  6030 			if (!gPagedRom)
       
  6031 				{
       
  6032 				test.Printf(_L("Not a paged ROM - Skipping...\n"));
       
  6033 				goto skipSetup;
       
  6034 				}
       
  6035 			break;
       
  6036 		}
       
  6037 	// Fist find the zone that movable page allocations should go into
       
  6038 	// Getting the best movable will be the same for discardable as well
       
  6039 	mostPrefIndex = GetBestZone(aPageType);
       
  6040 	if (mostPrefIndex == KErrNotFound)
       
  6041 		{
       
  6042 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  6043 		goto skipSetup;
       
  6044 		}
       
  6045 
       
  6046 	// Get the least pref zone to zone alloc into and ensure it has free pages
       
  6047 	GetPrefOrder();
       
  6048 	leastPrefIndex = gPrefArray[gZoneCount - 1];
       
  6049 	if (gZoneConfigArray[mostPrefIndex].iPref == gZoneConfigArray[leastPrefIndex].iPref)
       
  6050 		{
       
  6051 		test.Printf(_L("Zones with same preference - Skipping...\n"));
       
  6052 		goto skipSetup;
       
  6053 		}
       
  6054 	if (leastPrefIndex == (TUint)mostPrefIndex ||
       
  6055 		gZoneUtilArray[leastPrefIndex].iFreePages == 0)
       
  6056 		{
       
  6057 		test.Printf(_L("leastPrefIndex = mostPrefIndex or leastPrefIndex(%d) / mostPrefIndex(%d) has 0 free - Skipping test step...\n"), 
       
  6058 						leastPrefIndex, mostPrefIndex);
       
  6059 		goto skipSetup;
       
  6060 		}
       
  6061 
       
  6062 
       
  6063 	// Zone alloc 1 fixed page into the least preferable zone
       
  6064 	leastPrefZoneID = gZoneConfigArray[leastPrefIndex].iZoneId;	
       
  6065 	GetOriginalPageCount();
       
  6066 	switch(aZoneAllocType)
       
  6067 		{
       
  6068 		case Z_ALLOC_DISC:
       
  6069 			{
       
  6070 			r = Ldd.ZoneAllocDiscontiguous(leastPrefZoneID, 1);
       
  6071 			break;
       
  6072 			}
       
  6073 
       
  6074 		case Z_ALLOC_CONTIG:
       
  6075 			{			
       
  6076 			r = Ldd.ZoneAllocContiguous(leastPrefZoneID, gPageSize);
       
  6077 			break;
       
  6078 			}
       
  6079 		}
       
  6080 	GetAllPageInfo();
       
  6081 	if (r != KErrNone ||
       
  6082 		gZoneUtilArray[leastPrefIndex].iAllocFixed <= gOriginalPageCountArray[leastPrefIndex].iAllocFixed)
       
  6083 		{
       
  6084 		test.Printf(_L("Failed to allocate 1 fixed page to zone index %d r = %d\n"), leastPrefIndex, r);
       
  6085 		goto skipSetup;
       
  6086 		}
       
  6087 	test.Printf(_L("leastPrefIndex = %d mostPrefIndex = %d\n"), leastPrefIndex, mostPrefIndex);
       
  6088 	switch(aPageType)
       
  6089 		{
       
  6090 		case BEST_MOVABLE:
       
  6091 			// now allocate 1 movable page
       
  6092 			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
       
  6093 			if (r != KErrNone)
       
  6094 				{
       
  6095 				test.Printf(_L("Failed to allocate 1 movable page r = %d\n"), r);
       
  6096 				goto skipSetup;
       
  6097 				}
       
  6098 			break;
       
  6099 			
       
  6100 		case BEST_DISCARDABLE:
       
  6101 			// now allocate 1 discardable page
       
  6102 			TInt disPages;					
       
  6103 			TUint disBytes = (gTotalPageCount.iDiscardablePages + 1) << gPageShift;
       
  6104 			r = AllocDiscardable(disPages, disBytes);
       
  6105 			if (r != KErrNone)
       
  6106 				{
       
  6107 				test.Printf(_L("Failed to allocate 1 discardable page r = %d\n"), r);
       
  6108 				goto skipSetup;
       
  6109 				}
       
  6110 			break;
       
  6111 		}
       
  6112 	
       
  6113 	GetAllPageInfo();
       
  6114 	switch(aPageType)
       
  6115 		{
       
  6116 		case BEST_MOVABLE:
       
  6117 			if (gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
       
  6118 				gOriginalPageCountArray[leastPrefIndex].iAllocMovable < gZoneUtilArray[leastPrefIndex].iAllocMovable)
       
  6119 				{
       
  6120 				test.Printf(_L("FAIL: mostPrefIndex(%d) origMov 0x%x curMov 0x%x leastPrefIndex(%d) origMov 0x%x curMov 0x%x\n"), 
       
  6121 								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
       
  6122 								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable);
       
  6123 				CLEANUP(Ldd.FreeAllFixedPages());
       
  6124 				retVal = KErrGeneral;
       
  6125 				}
       
  6126 			else
       
  6127 				{
       
  6128 				test.Printf(_L("Passed...\n"));
       
  6129 				}
       
  6130 			break;
       
  6131 
       
  6132 			
       
  6133 		case BEST_DISCARDABLE:
       
  6134 			if (gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
       
  6135 				gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[leastPrefIndex].iAllocDiscardable)
       
  6136 				{
       
  6137 				test.Printf(_L("FAIL: mostPrefIndex(%d) origDis 0x%x curDis 0x%x leastPrefIndex(%d) origDis 0x%x curDis 0x%x\n"), 
       
  6138 								mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable, gZoneUtilArray[mostPrefIndex].iAllocDiscardable, 
       
  6139 								leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
       
  6140 				CLEANUP(Ldd.FreeAllFixedPages());
       
  6141 				retVal = KErrGeneral;
       
  6142 				}
       
  6143 			else
       
  6144 				{
       
  6145 				test.Printf(_L("Passed...\n"));
       
  6146 				}
       
  6147 			break;
       
  6148 		}
       
  6149 	
       
  6150 	// This will clean up any fixed pages allocated.
       
  6151 skipSetup:
       
  6152 	TestEnd();
       
  6153 	return retVal;
       
  6154 	}
       
  6155 		
       
  6156 //
       
  6157 // TestZoneAllocContiguous
       
  6158 //
       
  6159 //---------------------------------------------------------------------------------------------------------------------
       
  6160 //! @SYMTestCaseID				KBASE-t_ramdefrag-0535
       
  6161 //! @SYMTestType				CIT
       
  6162 //! @SYMTestCaseDesc			Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam().
       
  6163 //! @SYMPREQ					PREQ308
       
  6164 //! @SYMTestPriority			High
       
  6165 //! @SYMTestActions				
       
  6166 //! 	1.	Call function with a valid aZoneID.  
       
  6167 //! 	2.	Call function with an invalid aZoneID
       
  6168 //! 	3.	Call function with aSize > zone size 
       
  6169 //!		4.	Call multiple RAM zone overload of the function with a mix of valid and invalid IDs.
       
  6170 //!		5.	Call multiple RAM zone overload of the function with contiguous RAM zones and attempt
       
  6171 //!			to allocate over both RAM zones.
       
  6172 //!		6.	Call function on a RAM zone that has some non-DP pages allocated into it and ask
       
  6173 //!			for the whole RAM zone to be filled with fixed pages.
       
  6174 //!		7.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 contiguous fixed page 
       
  6175 //!			in the least preferable zone. Following this allocate 1 movable page
       
  6176 //!		8.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 contiguous fixed 
       
  6177 //!			page in the least preferable zone. Following this allocate 1 discardable page
       
  6178 //! 
       
  6179 //! @SYMTestExpectedResults
       
  6180 //! 	1.	KErrNone
       
  6181 //! 	2.	KErrArgument 
       
  6182 //! 	3.	KErrArgument
       
  6183 //!		4.	KErrArgument
       
  6184 //!		5.	KErrNone
       
  6185 //!		6.	KErrNoMemory
       
  6186 //!		7.	Movable pages are allocated into zone mostPref.
       
  6187 //!		8.	Discardable pages are allocated into zone mostPref
       
  6188 //---------------------------------------------------------------------------------------------------------------------
       
  6189 TInt TestZoneAllocContiguous()
       
  6190 	{
       
  6191 	TInt r = 0;
       
  6192 	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
       
  6193 	TestStart();	
       
  6194 		
       
  6195 	const TUint KAllocPages = 2;
       
  6196 	
       
  6197 	GetOriginalPageCount();
       
  6198 	GetAllPageInfo();
       
  6199 	TUint index = gZoneCount - 1;
       
  6200 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
       
  6201 		{
       
  6202 		-- index;
       
  6203 		}
       
  6204 	TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  6205 	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
       
  6206 		{
       
  6207 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  6208 		}
       
  6209 	else
       
  6210 		{
       
  6211 		TUint allocBytes = KAllocPages << gPageShift;
       
  6212 		r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
       
  6213 		GetAllPageInfo();
       
  6214 		if (r == KErrNone && 
       
  6215 			gZoneUtilArray[index].iAllocFixed - gOriginalPageCountArray[index].iAllocFixed == KAllocPages)
       
  6216 			{
       
  6217 			test.Printf(_L("Pass: Correct number of fixed pages allocated to zone ID 0x%x\n"),zoneID);
       
  6218 			}
       
  6219 		else
       
  6220 			{
       
  6221 			test.Printf(_L("Fail: r = %d, expected = %d, or number of pages allocated is not expected\n"), 
       
  6222 							r, KErrNone);
       
  6223 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6224 			TEST_FAIL;
       
  6225 			}
       
  6226 
       
  6227 		Ldd.FreeAllFixedPages();
       
  6228 		}
       
  6229 	TestEnd();
       
  6230 
       
  6231 
       
  6232 	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
       
  6233 	TestStart();	
       
  6234 	
       
  6235 	zoneID = KInvalidZoneID; 
       
  6236  
       
  6237 	TESTDEBUG(test.Printf(_L("zoneID = 0x%x\n"), zoneID));
       
  6238 	
       
  6239 	r = Ldd.ZoneAllocContiguous(zoneID, gPageSize);
       
  6240 	GetAllPageInfo();
       
  6241 	if (r == KErrArgument)
       
  6242 		{
       
  6243 		test.Printf(_L("Pass: Correct return value\n"));
       
  6244 		}
       
  6245 	else
       
  6246 		{
       
  6247 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  6248 		CLEANUP(Ldd.FreeAllFixedPages());
       
  6249 		TEST_FAIL;
       
  6250 		}
       
  6251 	Ldd.FreeAllFixedPages();
       
  6252 	TestEnd();
       
  6253 
       
  6254 
       
  6255 	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() with aSize > zone size"));	
       
  6256 	TestStart();	
       
  6257 	
       
  6258 	index = gZoneCount - 1;
       
  6259 	zoneID = gZoneConfigArray[index].iZoneId;
       
  6260 
       
  6261 	TUint allocBytes = (gZoneUtilArray[index].iPhysPages + 5)  << gPageShift;
       
  6262 	
       
  6263 	r = Ldd.ZoneAllocContiguous(zoneID, allocBytes);
       
  6264 	GetAllPageInfo();
       
  6265 	if (r == KErrArgument)
       
  6266 		{
       
  6267 		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
       
  6268 		}
       
  6269 	else
       
  6270 		{
       
  6271 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  6272 		CLEANUP(Ldd.FreeAllFixedPages());
       
  6273 		TEST_FAIL;
       
  6274 		}
       
  6275 	Ldd.FreeAllFixedPages();
       
  6276 	TestEnd();
       
  6277 
       
  6278 	const TUint KMultiZoneIds = 10;
       
  6279 	TUint* zoneIdArray = new TUint[KMultiZoneIds];
       
  6280 	test_NotNull(zoneIdArray);
       
  6281 
       
  6282 	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
       
  6283 	TestStart();
       
  6284 
       
  6285 	index = gZoneCount - 1;
       
  6286 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
       
  6287 		{
       
  6288 		-- index;
       
  6289 		}
       
  6290 	if (index == 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
       
  6291 		{
       
  6292 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  6293 		}
       
  6294 	else
       
  6295 		{
       
  6296 		TUint zoneIdSize = 2;
       
  6297 		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
       
  6298 		zoneIdArray[1] = KInvalidZoneID;
       
  6299 		TUint allocBytes = KAllocPages << gPageShift;
       
  6300 		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIdSize, allocBytes);
       
  6301 		if (r != KErrArgument)
       
  6302 			{
       
  6303 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6304 			TEST_FAIL;
       
  6305 			}
       
  6306 		else
       
  6307 			{
       
  6308 			test.Printf(_L("Pass: Correct return value\n"));
       
  6309 			}
       
  6310 		}
       
  6311 
       
  6312 	TestEnd();
       
  6313 
       
  6314 	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
       
  6315 	TestStart();
       
  6316 	// Attempt to find to physically contiguous RAM zones where higher addressed
       
  6317 	// one is empty, relies on RAM zones are returned by HAL functions in 
       
  6318 	// ascending physical address order.
       
  6319 	GetAllPageInfo();
       
  6320 	TBool zonesFound = EFalse;
       
  6321 	index = gZoneCount - 1;
       
  6322 	for (; index > 1; index--)
       
  6323 		{
       
  6324 		if (gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages &&
       
  6325 			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages &&
       
  6326 			gZoneConfigArray[index].iPhysBase - 1 == gZoneConfigArray[index-1].iPhysEnd)
       
  6327 			{
       
  6328 			zonesFound = ETrue;
       
  6329 			break;
       
  6330 			}
       
  6331 		}
       
  6332 
       
  6333 	if (!zonesFound)
       
  6334 		{
       
  6335 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6336 		}
       
  6337 	else
       
  6338 		{
       
  6339 		// Allocate one page more than the first RAM zone to force the allocation
       
  6340 		// to spread over both RAM zones.
       
  6341 		TUint allocPages = gZoneUtilArray[index-1].iPhysPages + 1;
       
  6342 		TUint allocBytes = allocPages << gPageShift;
       
  6343 
       
  6344 		// Attempt to find a zone that has less free pages than the allocation
       
  6345 		// size so we can test that the method continues past too full RAM zones.
       
  6346 		zonesFound = EFalse;
       
  6347 		TUint noAllocZone = 0;
       
  6348 		for (; noAllocZone < gZoneCount; noAllocZone++)
       
  6349 			{
       
  6350 			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
       
  6351 				noAllocZone != index && noAllocZone != index-1)
       
  6352 				{
       
  6353 				zonesFound = ETrue;
       
  6354 				break;
       
  6355 				} 
       
  6356 			}
       
  6357 		TUint zoneIds = 2;
       
  6358 		if (!zonesFound)
       
  6359 			{
       
  6360 			zoneIdArray[0] = gZoneConfigArray[index-1].iZoneId;
       
  6361 			zoneIdArray[1] = gZoneConfigArray[index].iZoneId;
       
  6362 			}
       
  6363 		else
       
  6364 			{// Have a zone that won't meet the allocation so use it
       
  6365 			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
       
  6366 			zoneIds++;
       
  6367 			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
       
  6368 			zoneIdArray[1] = gZoneConfigArray[index-1].iZoneId;
       
  6369 			zoneIdArray[2] = gZoneConfigArray[index].iZoneId;
       
  6370 			}
       
  6371 
       
  6372 		r = Ldd.MultiZoneAllocContiguous(zoneIdArray, zoneIds, allocBytes);
       
  6373 
       
  6374 		GetAllPageInfo();
       
  6375 		if (r != KErrNone || 
       
  6376 			gZoneUtilArray[index].iFreePages == gZoneUtilArray[index].iPhysPages ||
       
  6377 			gZoneUtilArray[index-1].iFreePages == gZoneUtilArray[index-1].iPhysPages)
       
  6378 			{// The allocation failed.
       
  6379 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6380 			TEST_FAIL;
       
  6381 			}
       
  6382 		else
       
  6383 			{
       
  6384 			test.Printf(_L("Passed...\n"));
       
  6385 			}
       
  6386 		}
       
  6387 	// TestEnd() will free the allocation of fixed pages.
       
  6388 	TestEnd();
       
  6389 
       
  6390 	test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when appropriate"));
       
  6391 	TestStart();
       
  6392 	// Attempt to find a RAM zone with some non-discarable pages allocated into it.
       
  6393 	// (At time of writing discardable pages wouldn't be discarded on demand by 
       
  6394 	// this function but this may be changed in the future as discontiguous case does that).
       
  6395 	GetAllPageInfo();
       
  6396 	zonesFound = EFalse;
       
  6397 	index = gZoneCount - 1;
       
  6398 	for (; index > 0; index--)
       
  6399 		{
       
  6400 		if (gZoneUtilArray[index].iFreePages && 
       
  6401 			(gZoneUtilArray[index].iAllocMovable || gZoneUtilArray[index].iAllocFixed))
       
  6402 			{
       
  6403 			zonesFound = ETrue;
       
  6404 			break;
       
  6405 			}
       
  6406 		}
       
  6407 	
       
  6408 	if (!zonesFound)
       
  6409 		{
       
  6410 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));		
       
  6411 		}
       
  6412 	else
       
  6413 		{
       
  6414 		// Attempt to allocate the whole of the RAM zone.
       
  6415 		GetOriginalPageCount();
       
  6416 		TUint allocBytes = gZoneConfigArray[index].iPhysPages << gPageShift;
       
  6417 		r = Ldd.ZoneAllocContiguous(gZoneConfigArray[index].iZoneId, allocBytes);
       
  6418 
       
  6419 		// The allocation should have failed and no pages should have
       
  6420 		// been allocated.
       
  6421 		GetAllPageInfo();
       
  6422 		if (r != KErrNoMemory ||
       
  6423 			gOriginalPageCountArray[index].iFreePages > gZoneUtilArray[index].iFreePages)
       
  6424 			{
       
  6425 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6426 			TEST_FAIL;
       
  6427 			}
       
  6428 		else
       
  6429 			{
       
  6430 			test.Printf(_L("Pass: Correct return value\n"));
       
  6431 			}
       
  6432 		}
       
  6433 	// TestEnd() will free the allocation of fixed pages.
       
  6434 	TestEnd();
       
  6435 	delete[] zoneIdArray;
       
  6436 
       
  6437 	
       
  6438 	test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of movable pages"));
       
  6439 	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_MOVABLE);
       
  6440 	if (r != KErrNone)
       
  6441 		{
       
  6442 		TEST_FAIL;
       
  6443 		}
       
  6444 
       
  6445 	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() (Contiguous) doesn't affect the allocation of discardable pages"));
       
  6446 	r = TestZoneAllocNoAffect(Z_ALLOC_CONTIG, BEST_DISCARDABLE);
       
  6447 	if (r != KErrNone)
       
  6448 		{
       
  6449 		TEST_FAIL;
       
  6450 		}
       
  6451 
       
  6452 	test.End();
       
  6453 	return KErrNone;
       
  6454 	}
       
  6455 
       
  6456 
       
  6457 //
       
  6458 // TestZoneAllocDiscontiguous
       
  6459 //
       
  6460 //---------------------------------------------------------------------------------------------------------------------
       
  6461 //! @SYMTestCaseID				KBASE-t_ramdefrag-0536
       
  6462 //! @SYMTestType				CIT
       
  6463 //! @SYMTestCaseDesc			Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam().
       
  6464 //! @SYMPREQ					PREQ308
       
  6465 //! @SYMTestPriority			High
       
  6466 //! @SYMTestActions				
       
  6467 //! 	1.	Call function with a valid aZoneID.  
       
  6468 //! 	2.	Call function with an invalid aZoneID
       
  6469 //! 	3.	Call function with aNumPages > zone size 
       
  6470 //!		4.	Call multiple RAM zone overload of function with a mix of valid and invalid IDs
       
  6471 //!		5.	Call multiple RAM zone overload of function so that the allocation will have to
       
  6472 //!			span multiple RAM zones.
       
  6473 //!		6.	Call function with memory full with DP cache that has reached it's 
       
  6474 //!			minimum cache size.
       
  6475 //!		7.	Call function with memory not quite full with DP cache that has reached it's 
       
  6476 //!			minimum cache size and with the specified RAM zone full.
       
  6477 //!		8.	Call function to allocate a whole RAM zone on a RAM zone that has non-discardable
       
  6478 //!			pages already allocated into it.
       
  6479 //!		9.	Call function to allocate one less than the whole RAM zone on a RAM zone that has movable 
       
  6480 //!			pages allocated.
       
  6481 //!		10.	Get the most preferable zone for movable page allocations (mostPref). Allocate 1 discontiguous fixed page 
       
  6482 //!			in the least preferable zone. Following this allocate 1 movable page
       
  6483 //!		11.	Get the most preferable zone for discardable page allocations (mostPref). Allocate 1 discontiguous fixed 
       
  6484 //!			page in the least preferable zone. Following this allocate 1 discardable page
       
  6485 //! 
       
  6486 //! @SYMTestExpectedResults
       
  6487 //! 	1.	KErrNone
       
  6488 //! 	2.	KErrArgument
       
  6489 //! 	3.	KErrArgument
       
  6490 //!		4.	KErrArgument
       
  6491 //!		5.	KErrNone
       
  6492 //!		6.	KErrNoMemory
       
  6493 //!		7.	KErrNone
       
  6494 //!		8. 	KErrNoMemory
       
  6495 //!		9.	KErrNone (i.e. the movable pages are shifted out of the way but only those that need to be moved).
       
  6496 //!		10.	Movable pages are allocated into zone mostPref.
       
  6497 //!		11.	Discardable pages are allocated into zone mostPref
       
  6498 //---------------------------------------------------------------------------------------------------------------------
       
  6499 TInt TestZoneAllocDiscontiguous()
       
  6500 	{
       
  6501 	TInt r = KErrNone;
       
  6502 	test.Start(_L("Test1: Call function Epoc::ZoneAllocPhysicalRam() with a valid aZoneID"));	
       
  6503 	TestStart();	
       
  6504 	
       
  6505 	const TUint KAllocPages = 5;
       
  6506 	// Detemine how many extra pages the kernel heap may grow by
       
  6507 	// as these may need to be accounted for.
       
  6508 	TUint fixedOverhead = Ldd.GetAllocDiff(KAllocPages);
       
  6509 
       
  6510 	GetOriginalPageCount();
       
  6511 	GetAllPageInfo();
       
  6512 	TUint index = gZoneCount - 1;
       
  6513 	while (index > 0 && gZoneUtilArray[index].iFreePages < KAllocPages)
       
  6514 		{
       
  6515 		-- index;
       
  6516 		}
       
  6517 
       
  6518 	if (gZoneUtilArray[index].iFreePages < KAllocPages || gTotalPageCount.iFreePages < KAllocPages + fixedOverhead)
       
  6519 		{
       
  6520 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  6521 		}
       
  6522 	else
       
  6523 		{
       
  6524 		// Allocate KAllocPages discontiguous fixed pages into RAM zone zoneID 
       
  6525 		// and verfiy that the pages were allocated to the correct zone, allow for
       
  6526 		// extra fixed pages to be allocated as the kernel heap may grow.
       
  6527 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  6528 		r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
       
  6529 
       
  6530 		GetAllPageInfo();
       
  6531 		if ((r == KErrNone) && 
       
  6532 			(gZoneUtilArray[index].iAllocFixed >= gOriginalPageCountArray[index].iAllocFixed + KAllocPages))
       
  6533 			{
       
  6534 			test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
       
  6535 			}
       
  6536 		else
       
  6537 			{
       
  6538 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  6539 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6540 			TEST_FAIL;
       
  6541 			}
       
  6542 		Ldd.FreeAllFixedPages();
       
  6543 		}	
       
  6544 	TestEnd();
       
  6545 
       
  6546 
       
  6547 	test.Next(_L("Test2: Call function Epoc::ZoneAllocPhysicalRam() with an invalid aZoneID"));	
       
  6548 	TestStart();	
       
  6549 	
       
  6550 	TUint zoneID = KInvalidZoneID;
       
  6551 	
       
  6552 	r = Ldd.ZoneAllocDiscontiguous(zoneID, KAllocPages);
       
  6553 	GetAllPageInfo();
       
  6554 	if (r == KErrArgument)
       
  6555 		{
       
  6556 		test.Printf(_L("Pass: Correct return value\n"));
       
  6557 		}
       
  6558 	else
       
  6559 		{
       
  6560 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  6561 		CLEANUP(Ldd.FreeAllFixedPages());
       
  6562 		TEST_FAIL;
       
  6563 		}
       
  6564 	Ldd.FreeAllFixedPages();
       
  6565 	TestEnd();
       
  6566 
       
  6567 	test.Next(_L("Test3: Call function Epoc::ZoneAllocPhysicalRam() when aNumPages > zone size"));	
       
  6568 	TestStart();	
       
  6569 	
       
  6570 	GetAllPageInfo();
       
  6571 	index = gZoneCount - 1;
       
  6572 	zoneID = gZoneConfigArray[index].iZoneId;
       
  6573 	
       
  6574 	TUint allocPages = gZoneUtilArray[index].iPhysPages + 1;
       
  6575 	r = Ldd.ZoneAllocDiscontiguous(zoneID, allocPages);
       
  6576 	
       
  6577 	GetAllPageInfo();
       
  6578 	if (r == KErrArgument)
       
  6579 		{
       
  6580 		test.Printf(_L("Pass: Correct return value and number of pages allocated is correct\n"));
       
  6581 		}
       
  6582 	else
       
  6583 		{
       
  6584 		test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrArgument);
       
  6585 		CLEANUP(Ldd.FreeAllFixedPages());
       
  6586 		TEST_FAIL;
       
  6587 		}
       
  6588 	Ldd.FreeAllFixedPages();
       
  6589 	TestEnd();
       
  6590 
       
  6591 	const TUint KMultiZoneIds = 10;
       
  6592 	TUint* zoneIdArray = new TUint[KMultiZoneIds];
       
  6593 	test_NotNull(zoneIdArray);
       
  6594 
       
  6595 	test.Next(_L("Test4: Test Epoc::ZoneAllocPhysicaRam() always fails when at least one ID is invalid"));
       
  6596 	TestStart();
       
  6597 
       
  6598 	TBool zonesFound = EFalse;
       
  6599 	index = gZoneCount - 1;
       
  6600 	for (; index > 0; index--)
       
  6601 		{
       
  6602 		if (gZoneUtilArray[index].iFreePages >= KAllocPages)
       
  6603 			{
       
  6604 			zonesFound = ETrue;
       
  6605 			break;
       
  6606 			}
       
  6607 		}
       
  6608 	if (!zonesFound)
       
  6609 		{
       
  6610 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  6611 		}
       
  6612 	else
       
  6613 		{
       
  6614 		TUint zoneIdSize = 2;
       
  6615 		zoneIdArray[0] = gZoneConfigArray[index].iZoneId;
       
  6616 		zoneIdArray[1] = KInvalidZoneID;
       
  6617 		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIdSize, KAllocPages);
       
  6618 		if (r != KErrArgument)
       
  6619 			{// Make sure we cleanup.
       
  6620 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6621 			TEST_FAIL;
       
  6622 			}
       
  6623 		else
       
  6624 			{
       
  6625 			test.Printf(_L("Passed...\n"));	
       
  6626 			}
       
  6627 		}
       
  6628 
       
  6629 	TestEnd();
       
  6630 
       
  6631 	test.Next(_L("Test5: Test Epoc::ZoneAllocPhysicalRam() can span multiple RAM zones"));
       
  6632 	TestStart();
       
  6633 	zonesFound = EFalse;
       
  6634 	TUint zonesCount = 0;
       
  6635 	const TUint KTest5Zones = 2;
       
  6636 	TUint zoneIndices[KTest5Zones];
       
  6637 	allocPages = 0;
       
  6638 
       
  6639 	// Attempt to find KTest5Zones RAM zones with some free pages,
       
  6640 	// search in reverse preference order to reduce chances of kernel heap pages
       
  6641 	// being allocated into the RAM zones under test.
       
  6642 	GetPrefOrder();
       
  6643 	index = gZoneCount - 1;
       
  6644 	for (; index > 0; index--)
       
  6645 		{
       
  6646 		TUint prefIndex = gPrefArray[index];
       
  6647 		if (gZoneUtilArray[prefIndex].iFreePages != 0)
       
  6648 			{
       
  6649 			allocPages += gZoneUtilArray[prefIndex].iFreePages;
       
  6650 			zoneIndices[zonesCount++] = prefIndex;
       
  6651 			if (zonesCount == KTest5Zones)
       
  6652 				{
       
  6653 				zonesFound = ETrue;
       
  6654 				break;
       
  6655 				}
       
  6656 			}
       
  6657 		}
       
  6658 
       
  6659 	if (!zonesFound)
       
  6660 		{
       
  6661 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6662 		}
       
  6663 	else
       
  6664 		{
       
  6665 		// Attempt to find a zone that has less free pages than the allocation
       
  6666 		// size so we can test that the method continues past too full RAM zones.
       
  6667 		zonesFound = EFalse;
       
  6668 		TUint noAllocZone = 0;
       
  6669 		for (; noAllocZone < gZoneCount; noAllocZone++)
       
  6670 			{
       
  6671 			if (allocPages > gZoneUtilArray[noAllocZone].iFreePages &&
       
  6672 				noAllocZone != zoneIndices[0] && noAllocZone != zoneIndices[1])
       
  6673 				{
       
  6674 				zonesFound = ETrue;
       
  6675 				break;
       
  6676 				}
       
  6677 			}
       
  6678 		
       
  6679 		if (gPagedRom)
       
  6680 			{// Fill memory with DP pages to test the allocation will discard
       
  6681 			// pages when necessary.
       
  6682 			TInt discard;
       
  6683 			r = AllocDiscardable(discard);
       
  6684 			GetAllPageInfo();
       
  6685 			if (r != KErrNone ||
       
  6686 				gZoneUtilArray[zoneIndices[0]].iFreePages != 0 || 
       
  6687 				gZoneUtilArray[zoneIndices[1]].iFreePages != 0)
       
  6688 				{
       
  6689 				test.Printf(_L("r %d\n"), r);
       
  6690 				CLEANUP(ResetDPCache());
       
  6691 				TEST_FAIL;
       
  6692 				}			
       
  6693 			}
       
  6694 
       
  6695 		TUint zoneIds = KTest5Zones;
       
  6696 		if (!zonesFound)
       
  6697 			{
       
  6698 			zoneIdArray[0] = gZoneConfigArray[zoneIndices[0]].iZoneId;
       
  6699 			zoneIdArray[1] = gZoneConfigArray[zoneIndices[1]].iZoneId;
       
  6700 			}
       
  6701 		else
       
  6702 			{// Have a zone that won't meet the allocation so use it
       
  6703 			TESTDEBUG(test.Printf(_L("noAllocZone ID %x\n"), gZoneConfigArray[noAllocZone].iZoneId));
       
  6704 			zoneIds++;
       
  6705 			zoneIdArray[0] = gZoneConfigArray[noAllocZone].iZoneId;
       
  6706 			zoneIdArray[1] = gZoneConfigArray[zoneIndices[0]].iZoneId;
       
  6707 			zoneIdArray[2] = gZoneConfigArray[zoneIndices[1]].iZoneId;
       
  6708 			}
       
  6709 
       
  6710 		// Adjust the allocation size for any kernel heap pages that may be
       
  6711 		// required as they may get allocated into the RAM zones under test.
       
  6712 		allocPages -= Ldd.GetAllocDiff(allocPages);
       
  6713 
       
  6714 		GetOriginalPageCount();
       
  6715 		r = Ldd.MultiZoneAllocDiscontiguous(zoneIdArray, zoneIds, allocPages);
       
  6716 
       
  6717 		GetAllPageInfo();
       
  6718 		if (r != KErrNone ||
       
  6719 			gZoneUtilArray[zoneIndices[0]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[0]].iAllocFixed ||
       
  6720 			gZoneUtilArray[zoneIndices[1]].iAllocFixed <= gOriginalPageCountArray[zoneIndices[1]].iAllocFixed)
       
  6721 			{// The allocation failed.
       
  6722 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6723 			TEST_FAIL;
       
  6724 			}
       
  6725 		else
       
  6726 			{
       
  6727 			test.Printf(_L("Passed...\n"));	
       
  6728 			}
       
  6729 		}
       
  6730 	// TestEnd() will free the allocation of fixed pages.
       
  6731 	TestEnd();
       
  6732 
       
  6733 
       
  6734 	if (gPagedRom)
       
  6735 		{
       
  6736 		test.Next(_L("Test6: Test Epoc::ZoneAllocPhysicalRam() returns KErrNoMemory when DP minimum cache size is hit"));
       
  6737 		TestStart();
       
  6738 		GetPrefOrder();
       
  6739 		TUint zoneFreePages = 0;
       
  6740 		index = gZoneCount - 1;
       
  6741 		for (; index > 0; index--)
       
  6742 			{
       
  6743 			TUint prefIndex = gPrefArray[index];
       
  6744 			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
       
  6745 			if (zoneFreePages > 2)
       
  6746 				{
       
  6747 				index = prefIndex;
       
  6748 				zonesFound = ETrue;
       
  6749 				break;
       
  6750 				}
       
  6751 			}
       
  6752 
       
  6753 		if (!zonesFound)
       
  6754 			{
       
  6755 			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6756 			}
       
  6757 		else
       
  6758 			{
       
  6759 			// Fill the RAM with DP cache pages and up the minimum cache size so
       
  6760 			// that the allocation will fail.
       
  6761 			TInt discardBytes;
       
  6762 			TInt r = AllocDiscardable(discardBytes, KMaxTUint64, (zoneFreePages - 1) << gPageShift);
       
  6763 			test_KErrNone(r);
       
  6764 
       
  6765 			// Ensure that the RAM zone under test is full.
       
  6766 			GetAllPageInfo();
       
  6767 			if (gZoneUtilArray[index].iFreePages != 0)
       
  6768 				{
       
  6769 				CLEANUP(ResetDPCache());
       
  6770 				TEST_FAIL;
       
  6771 				}
       
  6772 
       
  6773 			r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, zoneFreePages);
       
  6774 
       
  6775 			if (r != KErrNoMemory)
       
  6776 				{
       
  6777 				CLEANUP(Ldd.FreeAllFixedPages());
       
  6778 				TEST_FAIL;
       
  6779 				}
       
  6780 			else
       
  6781 				{
       
  6782 				test.Printf(_L("Passed...\n"));	
       
  6783 				}
       
  6784 			}
       
  6785 		TestEnd();
       
  6786 
       
  6787 		test.Next(_L("Test7: Test Epoc::ZoneAllocPhysicalRam() replaces DP cache when DP minimum cache size is hit"));
       
  6788 		TestStart();
       
  6789 		gChunkArray1 = new RChunk;
       
  6790 		gChunkArraySize1 = 1;
       
  6791 		GetPrefOrder();
       
  6792 		zonesFound = EFalse;
       
  6793 		for (index = gZoneCount - 1; index > 0 && !zonesFound; index--)
       
  6794 			{
       
  6795 			TUint prefIndex = gPrefArray[index];
       
  6796 			zoneFreePages = gZoneUtilArray[prefIndex].iFreePages;
       
  6797 			if (zoneFreePages > 1)
       
  6798 				{
       
  6799 				// Check there is at least one free page in the other RAM zones.
       
  6800 				TUint i = 0;
       
  6801 				for (; i < gZoneCount; i++)
       
  6802 					{
       
  6803 					if (i != prefIndex && gZoneUtilArray[i].iFreePages != 0)
       
  6804 						{
       
  6805 						index = prefIndex;
       
  6806 						zonesFound = ETrue;
       
  6807 						break;
       
  6808 						}
       
  6809 					}
       
  6810 				}
       
  6811 			}
       
  6812 		if (!zonesFound)
       
  6813 			{
       
  6814 			test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6815 			}
       
  6816 		else
       
  6817 			{
       
  6818 			index++;
       
  6819 			// Attempt to allocate a movable page to create a gap for the DP cache 
       
  6820 			// page to be replaced with.
       
  6821 			GetOriginalPageCount();
       
  6822 			r = AllocMovable(gChunkArray1, gChunkArraySize1, 1, gPageSize);
       
  6823 
       
  6824 			GetAllPageInfo();
       
  6825 			TESTDEBUG(test.Printf(_L("index %d prev free 0x%x cur free 0x%x\n"), 
       
  6826 										index, gOriginalPageCountArray[index].iFreePages, gZoneUtilArray[index].iFreePages));
       
  6827 
       
  6828 			if (r != KErrNone || 
       
  6829 				gOriginalPageCountArray[index].iFreePages != gZoneUtilArray[index].iFreePages)
       
  6830 				{// The gap was allocated into the RAM zone under test so can't continue as
       
  6831 				// the DP cache will attempt to be reallocated into the same RAM zone.
       
  6832 				test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6833 				}
       
  6834 			else
       
  6835 				{
       
  6836 				// Get the ldd to create the array for the fixed page addresses here
       
  6837 				// so that any kernel heap allocations have already occurred before 
       
  6838 				// memory is filled etc.  Make allocation really large so it will always be enough.
       
  6839 				r = Ldd.AllocFixedArray(50);
       
  6840 
       
  6841 				// Fill RAM with DP cache pages and free the gap.
       
  6842 				TInt discardBytes;
       
  6843 				r = AllocDiscardable(discardBytes, KMaxTUint64, 0);
       
  6844 				if (r != KErrNone)	
       
  6845 					{
       
  6846 					CLEANUP(ResetDPCache());
       
  6847 					TEST_FAIL;
       
  6848 					}
       
  6849 				UpdateRamInfo();
       
  6850 				TInt prevFreeBytes = gFreeRam;
       
  6851 				RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  6852 				UpdateRamInfo();
       
  6853 				TInt freedPages = (gFreeRam - prevFreeBytes) >> gPageShift;
       
  6854 				if (freedPages < 1)
       
  6855 					{// Something went wrong as should have freed at least one page
       
  6856 					CLEANUP(ResetDPCache());
       
  6857 					TEST_FAIL;
       
  6858 					}
       
  6859 				TUint extraFreePages = freedPages - 1;
       
  6860 
       
  6861 				// Ensure that the RAM zone under test is full.
       
  6862 				GetAllPageInfo();
       
  6863 				if (gZoneUtilArray[index].iFreePages != 0)
       
  6864 					{
       
  6865 					CLEANUP(ResetDPCache());
       
  6866 					TEST_FAIL;
       
  6867 					}
       
  6868 
       
  6869 				// Allocate from the RAM zone which should force a DP cache 
       
  6870 				// page to be allocated.
       
  6871 				GetOriginalPageCount();
       
  6872 				TUint fixedAllocPages = 1 + extraFreePages;
       
  6873 				r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, fixedAllocPages);
       
  6874 			
       
  6875 				GetAllPageInfo();
       
  6876 				if (r != KErrNone ||
       
  6877 					gOriginalPageCountArray[index].iAllocFixed + fixedAllocPages != gZoneUtilArray[index].iAllocFixed)
       
  6878 					{
       
  6879 					test.Printf(_L("r %d index %d alloc 0x%x prevFixed 0x%x curFixed 0x%x\n"), r, index, fixedAllocPages, 
       
  6880 								gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
       
  6881 					CLEANUP(Ldd.FreeAllFixedPages());
       
  6882 					TEST_FAIL;
       
  6883 					}
       
  6884 				else
       
  6885 					{
       
  6886 					test.Printf(_L("Passed...\n"));						
       
  6887 					}
       
  6888 				}
       
  6889 			}
       
  6890 		// This should cleanup any fixed pages allocated.
       
  6891 		TestEnd();
       
  6892 		}
       
  6893 
       
  6894 	test.Next(_L("Test8: Test Epoc::ZoneAllocPhysicalRam() return KErrNoMemory when appropriate"));
       
  6895 	TestStart();
       
  6896 	// Search for a RAM zone that has some immovable pages allocated into 
       
  6897 	// it but isn't totally full.
       
  6898 	GetAllPageInfo();
       
  6899 	zonesFound = EFalse;
       
  6900 	for (index = 0; index < gZoneCount; index++)
       
  6901 		{
       
  6902 		if (gZoneUtilArray[index].iFreePages && 
       
  6903 			(gZoneUtilArray[index].iAllocUnknown || gZoneUtilArray[index].iAllocFixed))
       
  6904 			{
       
  6905 			zonesFound = ETrue;
       
  6906 			break;
       
  6907 			}
       
  6908 		}
       
  6909 	if (!zonesFound)
       
  6910 		{
       
  6911 		test.Printf(_L("Cannot find RAM zones to perform test, Skipping test step...\n"));
       
  6912 		}
       
  6913 	else
       
  6914 		{
       
  6915 		// Attempt to allocate the whole RAM zone.
       
  6916 		r = Ldd.ZoneAllocDiscontiguous(gZoneConfigArray[index].iZoneId, gZoneUtilArray[index].iPhysPages);
       
  6917 		
       
  6918 		if (r != KErrNoMemory)
       
  6919 			{
       
  6920 			test.Printf(_L("FAIL: r %d index %d\n"), r, index);
       
  6921 			CLEANUP(Ldd.FreeAllFixedPages());
       
  6922 			TEST_FAIL;
       
  6923 			}
       
  6924 		else
       
  6925 			{
       
  6926 			test.Printf(_L("Passed...\n"));
       
  6927 			}
       
  6928 		}
       
  6929 	TestEnd();
       
  6930 
       
  6931 	test.Next(_L("Test9: Test Epoc::ZoneAllocPhysicalRam() moves the required number of movable pages"));
       
  6932 	TestStart();
       
  6933 	TUint allocFixedPages;
       
  6934 	// Search for a RAM zone that has at least 2 movable pages allocated into it.
       
  6935 	// Need 2 so that we can move one and leave one.
       
  6936 	GetAllPageInfo();
       
  6937 	zonesFound = EFalse;
       
  6938 	for (index = 0; index < gZoneCount; index++)
       
  6939 		{
       
  6940 		if (gZoneUtilArray[index].iAllocMovable > 1)
       
  6941 			{
       
  6942 			// Only use this zone if the other RAM zones have enough free space for 
       
  6943 			// the movable page in this zone to be moved to.
       
  6944 			TUint freeInOther = 0;
       
  6945 			for (TUint i = 0; i < gZoneCount && !zonesFound; i++)
       
  6946 				{
       
  6947 				if (i != index)
       
  6948 					{
       
  6949 					freeInOther += gZoneUtilArray[i].iFreePages;
       
  6950 					}
       
  6951 				}
       
  6952 			if (freeInOther >= gZoneUtilArray[index].iAllocMovable)
       
  6953 				{
       
  6954 				zonesFound = ETrue;
       
  6955 				break;
       
  6956 				}
       
  6957 			}
       
  6958 		}
       
  6959 	
       
  6960 	if (!zonesFound)
       
  6961 		{
       
  6962 		test.Printf(_L("No suitable RAM zone could be found - Skipping...\n"));
       
  6963 		goto skipTest9;
       
  6964 		}
       
  6965 
       
  6966 	// Allocate up to one less than the RAM zone size. Do 2 stage fixed allocation
       
  6967 	// to avoid kernel heap allocations spoiling test setup.
       
  6968 	r = Ldd.AllocFixedArray(gZoneConfigArray[index].iPhysPages);
       
  6969 	if (r != KErrNone)
       
  6970 		{
       
  6971 		test.Printf(_L("Not enough free RAM to perform test - Skipping...\n"));
       
  6972 		goto skipTest9;
       
  6973 		}
       
  6974 
       
  6975 	GetAllPageInfo();
       
  6976 	if (gZoneUtilArray[index].iAllocMovable < 2)
       
  6977 		{
       
  6978 		test.Printf(_L("Expanding kernel heap for phys address array spoiled RAM zone - Skipping...\n"));
       
  6979 		goto skipTest9;
       
  6980 		}
       
  6981 	allocFixedPages =	gZoneUtilArray[index].iAllocMovable + 
       
  6982 						gZoneUtilArray[index].iAllocDiscardable +
       
  6983 						gZoneUtilArray[index].iFreePages - 1;
       
  6984 	r = Ldd.ZoneAllocDiscontiguous2(gZoneConfigArray[index].iZoneId, allocFixedPages);
       
  6985 
       
  6986 	if (r != KErrNone || !gZoneUtilArray[index].iAllocMovable)
       
  6987 		{
       
  6988 		test.Printf(_L("Fixed not allocated or too many movable moved RAM zone ID%x\n"), 
       
  6989 					gZoneConfigArray[index].iZoneId);
       
  6990 		CLEANUP(Ldd.FreeAllFixedPages());
       
  6991 		TEST_FAIL;
       
  6992 		}
       
  6993 
       
  6994 skipTest9 :
       
  6995 	// This will clean up any fixed pages allocated.
       
  6996 	TestEnd();
       
  6997 
       
  6998 	delete[] zoneIdArray;
       
  6999 	
       
  7000 	test.Next(_L("Test10: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of movable pages"));
       
  7001 	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_MOVABLE);
       
  7002 	if (r != KErrNone)
       
  7003 		{
       
  7004 		TEST_FAIL;
       
  7005 		}
       
  7006 
       
  7007 	test.Next(_L("Test11: Test Epoc::ZoneAllocPhysicalRam() (Discontiguous) doesn't affect the allocation of discardable pages"));
       
  7008 	r = TestZoneAllocNoAffect(Z_ALLOC_DISC, BEST_DISCARDABLE);
       
  7009 	if (r != KErrNone)
       
  7010 		{
       
  7011 		TEST_FAIL;
       
  7012 		}
       
  7013 
       
  7014 	test.End();
       
  7015 	return KErrNone;
       
  7016 	}
       
  7017 
       
  7018 
       
  7019 //
       
  7020 // TestFreeZone
       
  7021 //
       
  7022 //---------------------------------------------------------------------------------------------------------------------
       
  7023 //! @SYMTestCaseID				KBASE-t_ramdefrag-0537
       
  7024 //! @SYMTestType				CIT
       
  7025 //! @SYMTestCaseDesc			Verifying the function Epoc::FreePhysicalRam()
       
  7026 //! @SYMPREQ					PREQ308
       
  7027 //! @SYMTestPriority			High
       
  7028 //! @SYMTestActions				
       
  7029 //! 	1.	Allocate fixed pages and call function to free all fixed pages allocated.  
       
  7030 //! 
       
  7031 //! @SYMTestExpectedResults
       
  7032 //! 	1.	KErrNone
       
  7033 //---------------------------------------------------------------------------------------------------------------------
       
  7034 TInt TestFreeZone()
       
  7035 	{
       
  7036 	TInt r = 0;
       
  7037 	TUint zoneID = 0;
       
  7038 	test.Start(_L("Test1: Free allocated pages"));	
       
  7039 	TestStart();	
       
  7040 	
       
  7041 	TInt pages = 50;
       
  7042 
       
  7043 	GetAllPageInfo();
       
  7044 	TUint index = gZoneCount - 1;
       
  7045 	while (index > 0 && 
       
  7046 			(gZoneUtilArray[index].iAllocFixed != 0 || 
       
  7047 			gZoneUtilArray[index].iAllocUnknown != 0 || 
       
  7048 			(TInt)gZoneUtilArray[index].iFreePages < pages)) 
       
  7049 		{
       
  7050 		-- index;
       
  7051 		}
       
  7052 	zoneID = gZoneConfigArray[index].iZoneId;
       
  7053 	
       
  7054 	if (index == 0 && 
       
  7055 		(gZoneUtilArray[index].iAllocFixed != 0 || 
       
  7056 		gZoneUtilArray[index].iAllocUnknown != 0 || 
       
  7057 		(TInt)gZoneUtilArray[index].iFreePages < pages))
       
  7058 		{
       
  7059 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  7060 		}
       
  7061 	else
       
  7062 		{
       
  7063 		TESTDEBUG(test.Printf(_L("Allocating 0x%x pages to zone ID 0x%x\n"), pages, zoneID));
       
  7064 		r = Ldd.ZoneAllocDiscontiguous(zoneID, pages);
       
  7065 		GetAllPageInfo();
       
  7066 
       
  7067 		TESTDEBUG(test.Printf(_L("Freeing 0x%x fixed pages\n"), pages));
       
  7068 		if (r == KErrNone)
       
  7069 			{
       
  7070 			r = Ldd.FreeZone(pages);
       
  7071 			}
       
  7072 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  7073 		if (r == KErrNone)
       
  7074 			{
       
  7075 			test.Printf(_L("Pass: Correct return value\n"));
       
  7076 			}
       
  7077 		else
       
  7078 			{
       
  7079 			test.Printf(_L("Fail: r = %d, expected = %d\n"), r, KErrNone);
       
  7080 			TEST_FAIL;
       
  7081 			}
       
  7082 		}
       
  7083 	TestEnd();
       
  7084 
       
  7085 	test.End();
       
  7086 	return KErrNone;
       
  7087 	}
       
  7088 
       
  7089 
       
  7090 //
       
  7091 // TestDefragSemMethod
       
  7092 //
       
  7093 //---------------------------------------------------------------------------------------------------------------------
       
  7094 //! @SYMTestCaseID				KBASE-t_ramdefrag-0538
       
  7095 //! @SYMTestType				CIT
       
  7096 //! @SYMTestCaseDesc			Verifying the semaphore versions of the various defrag methods
       
  7097 //! @SYMPREQ					PREQ308
       
  7098 //! @SYMTestPriority			High
       
  7099 //! @SYMTestActions				
       
  7100 //! 	1.	Fragment the memory. Following this, call the semaphore variation of DefragRam.  
       
  7101 //! 	2.	Fragment the memory. Following this, call the semaphore variation of EmptyRamZone 
       
  7102 //! 	3.	Fragment the memory. Following this, call the semaphore variation of ClaimRamZone 
       
  7103 //! 
       
  7104 //! @SYMTestExpectedResults
       
  7105 //! 	1.	1 or more zones have been emptied
       
  7106 //! 	2.	Zone specified has been emptied
       
  7107 //! 	3.	Zone has been claimed
       
  7108 //---------------------------------------------------------------------------------------------------------------------
       
  7109 TInt TestDefragSemMethod()
       
  7110 	{
       
  7111 	TInt r = 0;
       
  7112 	
       
  7113 	test.Start(_L("Test1: Call semaphore method of DefragRam"));	
       
  7114 	TestStart();	
       
  7115 	
       
  7116 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7117 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7118 
       
  7119 	GetOriginalPageCount();
       
  7120 	TBool genSucceed = CanGenSucceed();
       
  7121 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SEM);
       
  7122 	
       
  7123 	
       
  7124 	if (r != KErrNone ||(genSucceed && CheckZonesSwitchedOff() == EFalse))
       
  7125 		{
       
  7126 		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
       
  7127 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7128 		TEST_FAIL;
       
  7129 		}
       
  7130 	else
       
  7131 		{
       
  7132 		test.Printf(_L("Passed...\n"));	
       
  7133 		}
       
  7134 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  7135 	TestEnd();
       
  7136 
       
  7137 
       
  7138 	test.Next(_L("Test2: Call semaphore method of EmptyRamZone"));	
       
  7139 	TestStart();	
       
  7140 	
       
  7141 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7142 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7143 
       
  7144 	GetAllPageInfo();
       
  7145 	TUint index = gZoneCount - 1;
       
  7146 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  7147 		{
       
  7148 		-- index;
       
  7149 		}
       
  7150 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
       
  7151 	
       
  7152 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  7153 		{
       
  7154 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  7155 		}
       
  7156 	else
       
  7157 		{
       
  7158 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SEM, defragZoneID);
       
  7159 		
       
  7160 		GetAllPageInfo();
       
  7161 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  7162 		if (freeInOthers && (r != KErrNone || !CheckZoneIsOff(index)))
       
  7163 			{
       
  7164 			test.Printf(_L("Fail: r = %d, or zones has not been swtiched off\n"), r);
       
  7165 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7166 			TEST_FAIL;
       
  7167 			}
       
  7168 		else
       
  7169 			{
       
  7170 			test.Printf(_L("Passed...\n"));	
       
  7171 			}
       
  7172 		}
       
  7173 	// This will free any allocated memory.			
       
  7174 	TestEnd();
       
  7175 
       
  7176 	test.Next(_L("Test3: Call semaphore method of ClaimRamZone"));	
       
  7177 	TestStart();	
       
  7178 	
       
  7179 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7180 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7181 
       
  7182 	GetAllPageInfo();
       
  7183 	index = gZoneCount - 2;
       
  7184 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  7185 		{
       
  7186 		-- index;
       
  7187 		}
       
  7188 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  7189 	
       
  7190 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  7191 		{
       
  7192 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  7193 		}
       
  7194 	else
       
  7195 		{
       
  7196 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SEM, defragZoneID);
       
  7197 
       
  7198 		GetAllPageInfo();
       
  7199 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  7200 		if (CheckZoneIsOff(index) ||
       
  7201 			(freeInOthers && ( r != KErrNone ||
       
  7202 			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
       
  7203 			{
       
  7204 			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
       
  7205 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7206 			TEST_FAIL;
       
  7207 			}
       
  7208 		else
       
  7209 			{
       
  7210 			test.Printf(_L("Passed...\n"));	
       
  7211 			}
       
  7212 
       
  7213 		r = Ldd.FreeAllFixedPages();
       
  7214 		}
       
  7215 
       
  7216 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  7217 	TestEnd();
       
  7218 
       
  7219 	test.End();
       
  7220 	return KErrNone;
       
  7221 
       
  7222 	}
       
  7223 
       
  7224 
       
  7225 //
       
  7226 // TestDefragDfcMethod
       
  7227 //
       
  7228 //---------------------------------------------------------------------------------------------------------------------
       
  7229 //! @SYMTestCaseID				KBASE-t_ramdefrag-0539
       
  7230 //! @SYMTestType				CIT
       
  7231 //! @SYMTestCaseDesc			Verifying the Dfc versions of the various defrag methods
       
  7232 //! @SYMPREQ					PREQ308
       
  7233 //! @SYMTestPriority			High
       
  7234 //! @SYMTestActions				
       
  7235 //! 	1.	Fragment the memory. Following this, call the Dfc variation of DefragRam.  
       
  7236 //! 	2.	Fragment the memory. Following this, call the Dfc variation of EmptyRamZone 
       
  7237 //! 	3.	Fragment the memory. Following this, call the Dfc variation of ClaimRamZone 
       
  7238 //! 
       
  7239 //! @SYMTestExpectedResults
       
  7240 //! 	1.	1 or more zones have been emptied
       
  7241 //! 	2.	Zone specified has been emptied
       
  7242 //! 	3.	Zone has been claimed
       
  7243 //---------------------------------------------------------------------------------------------------------------------
       
  7244 TInt TestDefragDfcMethod()
       
  7245 	{
       
  7246 	TInt r = 0;
       
  7247 	TRequestStatus req;
       
  7248 	test.Start(_L("Test1: Call Dfc method of DefragRam"));	
       
  7249 	TestStart();	
       
  7250 	
       
  7251 	GetAllPageInfo();
       
  7252 	
       
  7253 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7254 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7255 
       
  7256 	GetOriginalPageCount();
       
  7257 	TBool genSucceed = CanGenSucceed();
       
  7258 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_DFC, 0, 0, -1, &req);
       
  7259 	TESTDEBUG(test.Printf(_L("After queueing defrag r = %d\n"), r));
       
  7260 	User::WaitForRequest(req);
       
  7261 	r = req.Int();
       
  7262 	
       
  7263 	if (r != KErrNone || (genSucceed && CheckZonesSwitchedOff() == EFalse))
       
  7264 		{
       
  7265 		test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
       
  7266 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7267 		TEST_FAIL;
       
  7268 		}
       
  7269 	else
       
  7270 		{
       
  7271 		test.Printf(_L("Passed...\n"));	
       
  7272 		}
       
  7273 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  7274 	TestEnd();
       
  7275 
       
  7276 
       
  7277 	test.Next(_L("Test2: Call Dfc method of EmptyRamZone"));	
       
  7278 	TestStart();	
       
  7279 	
       
  7280 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7281 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7282 
       
  7283 	GetAllPageInfo();
       
  7284 	TUint index = gZoneCount - 1;
       
  7285 	while (index > 0 && 
       
  7286 			(gZoneUtilArray[index].iAllocMovable < 10 ||
       
  7287 			gZoneUtilArray[index].iAllocFixed != 0 || 
       
  7288 			gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  7289 		{
       
  7290 		-- index;
       
  7291 		}
       
  7292 
       
  7293 	TUint defragZoneID = gZoneConfigArray[index].iZoneId;
       
  7294 	test.Printf(_L("zone ID = 0x%x\n"), defragZoneID);
       
  7295 	if (index == 0 && 
       
  7296 		(gZoneUtilArray[index].iAllocMovable < 10 ||
       
  7297 		gZoneUtilArray[index].iAllocFixed != 0 || 
       
  7298 		gZoneUtilArray[index].iAllocUnknown != 0))
       
  7299 		{
       
  7300 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  7301 		}
       
  7302 	else
       
  7303 		{
       
  7304 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
       
  7305 		
       
  7306 		User::WaitForRequest(req);
       
  7307 		r = req.Int();
       
  7308 
       
  7309 		GetAllPageInfo();
       
  7310 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  7311 		if (freeInOthers && (r != KErrNone || CheckZoneIsOff(index) == EFalse))
       
  7312 			{
       
  7313 			test.Printf(_L("Fail: r = %d, or zones have not been swtiched off\n"), r);
       
  7314 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7315 			TEST_FAIL;
       
  7316 			}
       
  7317 		else
       
  7318 			{
       
  7319 			test.Printf(_L("Passed...\n"));	
       
  7320 			}
       
  7321 		}
       
  7322 	// This will free any allocated memory
       
  7323 	TestEnd();
       
  7324 	
       
  7325 
       
  7326 	test.Next(_L("Test3: Call Dfc method of ClaimRamZone"));	
       
  7327 	TestStart();	
       
  7328 	
       
  7329 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7330 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7331 
       
  7332 	GetAllPageInfo();
       
  7333 	index = gZoneCount - 1;
       
  7334 	while (index > 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0)) 
       
  7335 		{
       
  7336 		-- index;
       
  7337 		}
       
  7338 	defragZoneID = gZoneConfigArray[index].iZoneId;
       
  7339 
       
  7340 	if (index == 0 && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  7341 		{
       
  7342 		test.Printf(_L("Cannot find zone to perform test, Skipping test step...\n"));
       
  7343 		}
       
  7344 	else
       
  7345 		{
       
  7346 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_DFC, defragZoneID, 0, -1, &req);
       
  7347 		User::WaitForRequest(req);
       
  7348 		r = req.Int();
       
  7349 		
       
  7350 		GetAllPageInfo();
       
  7351 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  7352 		if (CheckZoneIsOff(index) || 
       
  7353 			(freeInOthers && (r != KErrNone || 
       
  7354 			gZoneUtilArray[index].iAllocFixed != gZoneConfigArray[index].iPhysPages)))
       
  7355 			{
       
  7356 			test.Printf(_L("Fail: r = %d, or zone ID 0x%x has not been claimed\n"), r, defragZoneID);
       
  7357 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7358 			if (r == KErrNone)
       
  7359 				{
       
  7360 				CLEANUP(Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
       
  7361 											gZoneConfigArray[index].iPhysBase));
       
  7362 				}
       
  7363 			TEST_FAIL;
       
  7364 			}
       
  7365 		else
       
  7366 			{
       
  7367 			test.Printf(_L("Passed...\n"));	
       
  7368 			}
       
  7369 		
       
  7370 		if (r == KErrNone)
       
  7371 			{
       
  7372 			Ldd.FreeFromAddr(	gZoneUtilArray[index].iAllocFixed, 
       
  7373 								gZoneConfigArray[index].iPhysBase);
       
  7374 			}
       
  7375 		}
       
  7376 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);		
       
  7377 	TestEnd();
       
  7378 
       
  7379 	test.End();
       
  7380 	return KErrNone;
       
  7381 	}
       
  7382 
       
  7383 
       
  7384 //
       
  7385 // TestPriorities
       
  7386 //
       
  7387 //---------------------------------------------------------------------------------------------------------------------
       
  7388 //! @SYMTestCaseID				KBASE-t_ramdefrag-0540
       
  7389 //! @SYMTestType				CIT
       
  7390 //! @SYMTestCaseDesc			Verifying the priorities of the defrag methods
       
  7391 //! @SYMPREQ					PREQ308
       
  7392 //! @SYMTestPriority			High
       
  7393 //! @SYMTestActions				
       
  7394 //! 	1.	Call DefragRam with a lower invalid priority value, e.g. -2  
       
  7395 //! 	2.	Call DefragRam with a lower invalid priority value, e.g. 100
       
  7396 //! 	3.	Queue three asynchronous defrags using the Dfc method:
       
  7397 //! 			a.	First one with the lowest priority, this will start the straight away 
       
  7398 //! 				and will busy the defrag method, causing any other defrag requests to 
       
  7399 //! 				be queued. 
       
  7400 //! 			b.	Queue a defrag with a relatively low priority
       
  7401 //! 			c.	Queue a defrag with a higher priority than the one queued in (b)
       
  7402 //! 		Record the order in which the defrags are completed
       
  7403 //! 
       
  7404 //! @SYMTestExpectedResults
       
  7405 //! 	1.	KErrArgument
       
  7406 //! 	2.	KErrArgument
       
  7407 //! 	3.	(a) will complete first as it started straight away. 
       
  7408 //! 		(b) and (c) were both queued whilst (a) was running, 
       
  7409 //! 		however as (c) has a higher priority, it will complete first - 
       
  7410 //! 		therefore the order returned would be "a,c,b"
       
  7411 //---------------------------------------------------------------------------------------------------------------------
       
  7412 TInt TestPriorities()
       
  7413 	{
       
  7414 	test.Start(_L("Test1: Call defrag with an invalid lower priority"));	
       
  7415 	TestStart();	
       
  7416 	
       
  7417 	TInt r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, -2);
       
  7418 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  7419 	if (r != KErrArgument)
       
  7420 		{
       
  7421 		test.Printf(_L("Fail: r = %d, not expected\n"), r);
       
  7422 		TEST_FAIL;
       
  7423 		}
       
  7424 	else
       
  7425 		{
       
  7426 		test.Printf(_L("Passed...\n"));	
       
  7427 		}
       
  7428 	
       
  7429 	TestEnd();
       
  7430 
       
  7431 	test.Next(_L("Test2: Call defrag with an invalid higher priority"));	
       
  7432 	TestStart();	
       
  7433 	
       
  7434 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC, 0, 0, 100);
       
  7435 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  7436 	if (r != KErrArgument)
       
  7437 		{
       
  7438 		test.Printf(_L("Fail: r = %d, not expected\n"), r);
       
  7439 		TEST_FAIL;
       
  7440 		}
       
  7441 	else
       
  7442 		{
       
  7443 		test.Printf(_L("Passed...\n"));	
       
  7444 		}
       
  7445 	TestEnd();
       
  7446 
       
  7447 	if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) == 1)
       
  7448 		{// Only test priorities on single core system as this test is not smp safe.
       
  7449 		test.Next(_L("Test3: Call Dfc method of EmptyRamZone to test priorities"));	
       
  7450 		TestStart();
       
  7451 		
       
  7452 		
       
  7453 		TRequestStatus req;
       
  7454 		TRequestStatus req2;
       
  7455 		TRequestStatus req3;	
       
  7456 		TInt expectedOrder = 132; // Priorities set in Device driver 
       
  7457 		
       
  7458 		AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  7459 		FreeMovable(gChunkArray1, gChunkArraySize1);
       
  7460 
       
  7461 		TUint index = (gZoneCount + 1) / 2;
       
  7462 		TUint defragZoneID = gZoneConfigArray[index].iZoneId;
       
  7463 
       
  7464 		r = Ldd.CheckPriorities(DEFRAG_TYPE_EMPTY, defragZoneID, &req, &req2, &req3);
       
  7465 
       
  7466 		User::WaitForRequest(req);
       
  7467 		User::WaitForRequest(req2);
       
  7468 		User::WaitForRequest(req3);
       
  7469 
       
  7470 		TInt order = Ldd.GetDefragOrder();
       
  7471 		if (order != expectedOrder)
       
  7472 			{
       
  7473 			test.Printf(_L("Fail: order = %d. expected = %d\n"), order, expectedOrder);
       
  7474 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7475 			TEST_FAIL;
       
  7476 			}
       
  7477 		else
       
  7478 			{
       
  7479 			test.Printf(_L("Passed...\n"));	
       
  7480 			}
       
  7481 		// This will free any allocated memory
       
  7482 		TestEnd();
       
  7483 		}
       
  7484 
       
  7485 	test.End();
       
  7486 	return KErrNone;
       
  7487 	}
       
  7488 
       
  7489 
       
  7490 //
       
  7491 // TestFlags
       
  7492 //
       
  7493 //---------------------------------------------------------------------------------------------------------------------
       
  7494 //! @SYMTestCaseID				KBASE-t_ramdefrag-0541
       
  7495 //! @SYMTestType				CIT
       
  7496 //! @SYMTestCaseDesc			Verifying that when certain flags are set, 
       
  7497 //! 							only certain types of pages can be allocated to the zone. 
       
  7498 //! @SYMPREQ					PREQ308
       
  7499 //! @SYMTestPriority			High
       
  7500 //! @SYMTestActions				
       
  7501 //! 	1.	Set the NoFixed flag in a zone and allocate movable pages
       
  7502 //! 	2.	Set the NoFixed flag in a zone and allocate fixed pages
       
  7503 //! 	3.	Set the NoFixed flag in a zone and allocate discardable pages
       
  7504 //! 	4.	Set the NoFixed flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
       
  7505 //! 	5.	Set the NoFixed flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
       
  7506 //! 	6.	Set the NoMovable flag in a zone and allocate movable pages
       
  7507 //! 	7.	Set the NoMovable flag in a zone and allocate fixed pages
       
  7508 //! 	8.	Set the NoMovable flag in a zone and allocate discardable pages
       
  7509 //! 	9.	Set the NoDiscardable flag in a zone and allocate movable pages
       
  7510 //! 	10.	Set the NoDiscardable flag in a zone and allocate fixed pages
       
  7511 //! 	11.	Set the NoDiscardable flag in a zone and allocate discardable pages
       
  7512 //! 	12.	Set the OnlyDiscardable flag in a zone and allocate movable pages
       
  7513 //! 	13.	Set the OnlyDiscardable flag in a zone and allocate fixed pages
       
  7514 //! 	14.	Set the OnlyDiscardable flag in a zone and allocate discardable pages
       
  7515 //! 	15.	Set the OnlyDiscardable flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
       
  7516 //! 	16.	Set the OnlyDiscardable flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
       
  7517 //! 	17.	Set the NoFurtherAlloc flag in a zone and allocate movable pages
       
  7518 //! 	18.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages
       
  7519 //! 	19.	Set the NoFurtherAlloc flag in a zone and allocate discardable pages
       
  7520 //! 	20.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages to that zone using Epoc::ZoneAllocPhysicalRam()
       
  7521 //! 	21.	Set the NoFurtherAlloc flag in a zone and allocate fixed pages by calling TRamDefragRequest::ClaimRamZone()
       
  7522 //!		22.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most  
       
  7523 //!			preferable zone. Ensure that the discardable pages cannot be discarded and so must be moved. Now set the flag on  
       
  7524 //!			all zones barring the most preferable zone to KRamZoneFlagNoMovable, ensuring that most pref has no flags set. 
       
  7525 //!			Following this call a general defrag. 
       
  7526 //!		23.	Set up memory so that the least preferable RAM zone has movable pages + discardable pages > free pages in the most 
       
  7527 //!			preferable RAM zone. Ensure that the discardable pages cannot be discarded and so must be moved. 
       
  7528 //!			Now set the flag on all zones barring the most preferable zone to KRamZoneFlagNoDiscard, ensuring that 
       
  7529 //!			most preferable RAM zone has no flags set. Following this call a general defrag. 
       
  7530 //!		24. Set up memory so that the least preferable RAM zone has movable pages and discardable pages. Set all the zone 
       
  7531 //!			flags to KRamZoneFlagNoMovable. Following this call a general defrag. 
       
  7532 //!
       
  7533 //! @SYMTestExpectedResults
       
  7534 //! 	1.	Movable pages are allocated and no fixed pages allocated
       
  7535 //! 	2.	No fixed pages have been allocated
       
  7536 //! 	3.	Discardable pages are allocated and no fixed pages allocated
       
  7537 //!		4.	KErrNone, flag is ignored with zone specific allocation
       
  7538 //!		5.	KErrNone, flag is ignored when claiming a zone
       
  7539 //! 	6.	No movable pages have been allocated
       
  7540 //! 	7.	Fixed pages allocated, no movable allocated
       
  7541 //! 	8.	Discardable pages allocated, no movable pages allocated
       
  7542 //! 	9.	Movable pages allocated, no discardable pages allocated
       
  7543 //! 	10.	Fixed pages allocated, no discardable allocated
       
  7544 //! 	11.	No discardable pages allocated
       
  7545 //! 	12.	No movable pages allocated
       
  7546 //! 	13.	No fixed pages allocated
       
  7547 //! 	14.	Discardable pages allocated, no movable or fixed allocated
       
  7548 //!		15.	KErrNone, flag is ignored with zone specific allocation
       
  7549 //!		16.	KErrNone, flag is ignored when claiming a zone
       
  7550 //! 	17.	No moving, fixed or discardable pages allocated
       
  7551 //! 	18.	No moving, fixed or discardable pages allocated
       
  7552 //! 	19.	No moving, fixed or discardable pages allocated
       
  7553 //!		20.	KErrNoMemory, flag is obeyed with zone specific allocation
       
  7554 //!		21.	KErrNone, flag is ignored when claiming a zone
       
  7555 //!		22.	Movable pages moved to the most preferable zone, discardable pages moved to next most preferable zone
       
  7556 //!		23.	Discardable pages moved to most preferable zone, movable pages moved to next next most preferable zone
       
  7557 //!		24.	No pages are moved from the least preferable zone zone
       
  7558 //---------------------------------------------------------------------------------------------------------------------
       
  7559 TInt TestFlags()
       
  7560 	{
       
  7561 	TInt r = 0;
       
  7562 	TUint zoneDefragID = 0;
       
  7563 
       
  7564 	test.Start(_L("Test1: No Fixed Flag, Alloc Movable"));	
       
  7565 	TestStart();	
       
  7566 	
       
  7567 	TInt index = GetBestZone(BEST_MOVABLE);
       
  7568 
       
  7569 	if (index == KErrNotFound)
       
  7570 		{
       
  7571 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7572 		}
       
  7573 	else
       
  7574 		{
       
  7575 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7576 		if (SpaceAvailForPageTables(index, KFillAllMovable))
       
  7577 			{
       
  7578 			GetOriginalPageCount();
       
  7579 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  7580 			test_KErrNone(r);		
       
  7581 
       
  7582 			AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
       
  7583 			GetAllPageInfo();
       
  7584 			if (gZoneUtilArray[index].iAllocMovable == gOriginalPageCountArray[index].iAllocMovable || 
       
  7585 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  7586 				{
       
  7587 				test.Printf(_L("Fail: Either fixed pages have been allocated or movable pages have not been allocated to zone ID %x\n"), gZoneConfigArray[index].iZoneId);
       
  7588 				CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7589 				TEST_FAIL;
       
  7590 				}
       
  7591 			else
       
  7592 				{
       
  7593 				test.Printf(_L("Passed...\n"));	
       
  7594 				}
       
  7595 			}
       
  7596 		else
       
  7597 			{
       
  7598 			test.Printf(_L("Skipping...\n"));
       
  7599 			}
       
  7600 		}
       
  7601 	TestEnd();
       
  7602 
       
  7603 
       
  7604 	test.Next(_L("Test2: No Fixed Flag, Alloc Fixed"));	
       
  7605 	TestStart();
       
  7606 
       
  7607 	if(gPagedRom)
       
  7608 		{
       
  7609 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  7610 		test_KErrNone(r);
       
  7611 		}
       
  7612 
       
  7613 	// find a zone that has free pages in it to block it
       
  7614 	GetAllPageInfo();
       
  7615 	TUint i = 0;
       
  7616 	for (; i < gZoneCount; i++)
       
  7617 		{
       
  7618 		if (gZoneUtilArray[i].iFreePages > 0)
       
  7619 			{
       
  7620 			index = i;
       
  7621 			break;
       
  7622 			}
       
  7623 		}
       
  7624 	
       
  7625 	if (i == gZoneCount)
       
  7626 		{
       
  7627 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7628 		}
       
  7629 	else
       
  7630 		{
       
  7631 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7632 
       
  7633 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  7634 		test_KErrNone(r);
       
  7635 
       
  7636 		GetOriginalPageCount();
       
  7637 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  7638 
       
  7639 		GetAllPageInfo();
       
  7640 		// Ensure that either zone does not contain extra fixed pages 
       
  7641 		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  7642 			{
       
  7643 			test.Printf(_L("Fail: Fixed pages have been allocated into the zone ID 0x%x r = %d\n"), zoneDefragID, r);
       
  7644 			CLEANUP(Ldd.FreeAllFixedPages());
       
  7645 			TEST_FAIL;
       
  7646 			}
       
  7647 		else
       
  7648 			{
       
  7649 			test.Printf(_L("Passed...\n"));	
       
  7650 			}
       
  7651 		}	
       
  7652 	TestEnd();
       
  7653 
       
  7654 
       
  7655 	test.Next(_L("Test3: No Fixed Flag, Alloc Discardable"));	
       
  7656 	TestStart();
       
  7657 	if (gPagedRom)
       
  7658 		{
       
  7659 		index = GetBestZone(BEST_DISCARDABLE);
       
  7660 			
       
  7661 		if (index == KErrNotFound)
       
  7662 			{
       
  7663 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7664 			}
       
  7665 		else
       
  7666 			{
       
  7667 			zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7668 			GetOriginalPageCount();
       
  7669 			
       
  7670 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  7671 			test_KErrNone(r);
       
  7672 
       
  7673 			UpdateRamInfo();
       
  7674 			TInt discardablePages;
       
  7675 			r = AllocDiscardable(discardablePages);
       
  7676 			if (r != KErrNone)
       
  7677 				{
       
  7678 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  7679 				TEST_FAIL;
       
  7680 				}
       
  7681 			
       
  7682 			GetAllPageInfo();
       
  7683 			if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  7684 				{
       
  7685 				test.Printf(_L("Fail: Fixed pages have been allocated\n"));
       
  7686 				CLEANUP(ResetDPCache());
       
  7687 				TEST_FAIL;
       
  7688 				}
       
  7689 			else
       
  7690 				{
       
  7691 				test.Printf(_L("Passed...\n"));	
       
  7692 				}
       
  7693 			}		
       
  7694 		}
       
  7695 	else
       
  7696 		{
       
  7697 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  7698 		}
       
  7699 	TestEnd();
       
  7700 	
       
  7701 	test.Next(_L("Test4: No Fixed Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
       
  7702 	TestStart();
       
  7703 	
       
  7704 	index = GetBestZone(BEST_FIXED);
       
  7705 	
       
  7706 	if (index == KErrNotFound)
       
  7707 		{
       
  7708 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7709 		}
       
  7710 	else
       
  7711 		{
       
  7712 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7713 
       
  7714 		GetOriginalPageCount();
       
  7715 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  7716 		test_KErrNone(r);		
       
  7717 
       
  7718 		// Just need to try and allocate one page
       
  7719 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
       
  7720 
       
  7721 		GetAllPageInfo();
       
  7722 		if (r != KErrNone)
       
  7723 			{
       
  7724 			test.Printf(_L("Fail: Fixed pages have not been allocated into the zone\n"));
       
  7725 			CLEANUP(Ldd.FreeAllFixedPages());
       
  7726 			TEST_FAIL;
       
  7727 			}
       
  7728 		else
       
  7729 			{
       
  7730 			test.Printf(_L("Passed...\n"));	
       
  7731 			}
       
  7732 		}	
       
  7733 	TestEnd();
       
  7734 
       
  7735 	test.Next(_L("Test5: No Fixed Flag, Alloc Fixed by attempting to claim zone"));	
       
  7736 	TestStart();
       
  7737 	
       
  7738 	GetAllPageInfo();
       
  7739 	index = 0;	
       
  7740 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  7741 		{
       
  7742 		index++;
       
  7743 		}
       
  7744 	
       
  7745 	if ((TUint)index == gZoneCount)
       
  7746 		{
       
  7747 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7748 		}
       
  7749 	else
       
  7750 		{
       
  7751 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7752 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_FIXED_FLAG);
       
  7753 		test_KErrNone(r);		
       
  7754 
       
  7755 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
       
  7756 		GetAllPageInfo();
       
  7757 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  7758 		if (freeInOthers && r != KErrNone)
       
  7759 			{
       
  7760 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
       
  7761 			TEST_FAIL;
       
  7762 			}
       
  7763 		else
       
  7764 			{
       
  7765 			test.Printf(_L("Passed...\n"));	
       
  7766 			}			
       
  7767 		}	
       
  7768 	TestEnd();
       
  7769 	
       
  7770 
       
  7771 
       
  7772 //------------------------------------------------------------
       
  7773 	test.Next(_L("Test6: No Movable Flag, Alloc Movable"));	
       
  7774 	TestStart();
       
  7775 	
       
  7776 	index = GetBestZone(BEST_MOVABLE);
       
  7777 	
       
  7778 	if (index == KErrNotFound)
       
  7779 		{
       
  7780 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7781 		}
       
  7782 	else
       
  7783 		{
       
  7784 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7785 
       
  7786 		GetOriginalPageCount();
       
  7787 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
       
  7788 		test_KErrNone(r);
       
  7789 		
       
  7790 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
       
  7791 
       
  7792 		GetAllPageInfo();
       
  7793 		if (gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable)
       
  7794 			{
       
  7795 			test.Printf(_L("Fail: Movable pages have been allocated in the zone\n"));
       
  7796 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7797 			TEST_FAIL;
       
  7798 			}
       
  7799 		else
       
  7800 			{
       
  7801 			test.Printf(_L("Passed...\n"));	
       
  7802 			}		
       
  7803 		}
       
  7804 	TestEnd();
       
  7805 
       
  7806 
       
  7807 	test.Next(_L("Test7: No Movable Flag, Alloc Fixed"));	
       
  7808 	TestStart();
       
  7809 	
       
  7810 	if(gPagedRom)
       
  7811 		{
       
  7812 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  7813 		test_KErrNone(r);
       
  7814 		}
       
  7815 
       
  7816 	index = GetBestZone(BEST_FIXED);
       
  7817 		
       
  7818 	if (index == KErrNotFound)
       
  7819 		{
       
  7820 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7821 		}
       
  7822 	else
       
  7823 		{ 
       
  7824 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7825 
       
  7826 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
       
  7827 		test_KErrNone(r);
       
  7828 
       
  7829 		GetOriginalPageCount();
       
  7830 		// Fill up all of RAM with fixed pages.
       
  7831 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  7832 		test.Printf(_L("r = %d\n"), r);
       
  7833 
       
  7834 		GetAllPageInfo();
       
  7835 		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable || 
       
  7836 			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
       
  7837 			{
       
  7838 			test.Printf(_L("Fail: orig mov 0x%x fix 0x%x current mov 0x%x fix 0x%x\n"), gOriginalPageCountArray[index].iAllocMovable,
       
  7839 						gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocMovable, gZoneUtilArray[index].iAllocFixed);
       
  7840 			CLEANUP(Ldd.FreeAllFixedPages());
       
  7841 			TEST_FAIL;
       
  7842 			}
       
  7843 		else
       
  7844 			{
       
  7845 			test.Printf(_L("Passed...\n"));	
       
  7846 			}
       
  7847 		}
       
  7848 	TestEnd();
       
  7849 
       
  7850 
       
  7851 	test.Next(_L("Test8: No Movable Flag, Alloc Discardable"));	
       
  7852 	TestStart();
       
  7853 	if (gPagedRom)
       
  7854 		{	
       
  7855 		index = GetBestZone(BEST_DISCARDABLE);
       
  7856 		
       
  7857 		if (index == KErrNotFound)
       
  7858 			{
       
  7859 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7860 			}
       
  7861 		else
       
  7862 			{
       
  7863 			zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7864 
       
  7865 			GetOriginalPageCount();
       
  7866 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
       
  7867 			test_KErrNone(r)
       
  7868 			UpdateRamInfo();
       
  7869 			TInt discardablePages;
       
  7870 			r = AllocDiscardable(discardablePages);
       
  7871 			if (r != KErrNone)
       
  7872 				{
       
  7873 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  7874 				TEST_FAIL;
       
  7875 				}
       
  7876 
       
  7877 			GetAllPageInfo();
       
  7878 			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
       
  7879 				{
       
  7880 				test.Printf(_L("Fail: Movable pages have been allocated into the zone \n"));
       
  7881 				CLEANUP(ResetDPCache());
       
  7882 				TEST_FAIL;
       
  7883 				}
       
  7884 			else
       
  7885 				{
       
  7886 				test.Printf(_L("Passed...\n"));	
       
  7887 				}
       
  7888 			}
       
  7889 		}
       
  7890 	else
       
  7891 		{
       
  7892 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  7893 		}
       
  7894 	TestEnd();
       
  7895 
       
  7896 //-----------------------------------------------------------------------------------------------
       
  7897 
       
  7898 	test.Next(_L("Test9: No Discardable Flag, Alloc Movable"));	
       
  7899 	TestStart();
       
  7900 	
       
  7901 	index = GetBestZone(BEST_MOVABLE);
       
  7902 
       
  7903 	if (index == KErrNotFound)
       
  7904 		{
       
  7905 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7906 		}
       
  7907 	else
       
  7908 		{
       
  7909 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7910 
       
  7911 		GetOriginalPageCount();
       
  7912 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
       
  7913 		test_KErrNone(r);
       
  7914 
       
  7915 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable);
       
  7916 		GetAllPageInfo();
       
  7917 
       
  7918 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
       
  7919 			(gZoneUtilArray[index].iAllocMovable <=  gOriginalPageCountArray[index].iAllocMovable &&
       
  7920 			gZoneUtilArray[index].iAllocFixed ==  gOriginalPageCountArray[index].iAllocFixed))
       
  7921 			{
       
  7922 			test.Printf(_L("Fail: Either discardable pages have been allocated or movable pages have not been allocated\n"));
       
  7923 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  7924 			TEST_FAIL;
       
  7925 			}
       
  7926 		else
       
  7927 			{
       
  7928 			test.Printf(_L("Passed...\n"));	
       
  7929 			}
       
  7930 		}
       
  7931 	TestEnd();
       
  7932 
       
  7933 
       
  7934 	test.Next(_L("Test10: No Discardable Flag, Alloc Fixed"));	
       
  7935 	TestStart();
       
  7936 	
       
  7937 	if(gPagedRom)
       
  7938 		{
       
  7939 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  7940 		test_KErrNone(r);
       
  7941 		}
       
  7942 
       
  7943 	index = GetBestZone(BEST_FIXED);
       
  7944 			
       
  7945 	if (index == KErrNotFound)
       
  7946 		{
       
  7947 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7948 		}
       
  7949 	else
       
  7950 		{ 
       
  7951 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7952 
       
  7953 		GetOriginalPageCount();
       
  7954 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
       
  7955 		test_KErrNone(r);
       
  7956 		// Fill up all of RAM with fixed pages.
       
  7957 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  7958 
       
  7959 		GetAllPageInfo();
       
  7960 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
       
  7961 			gZoneUtilArray[index].iAllocFixed <= gOriginalPageCountArray[index].iAllocFixed)
       
  7962 			{
       
  7963 			test.Printf(_L("Fail: Discardable pages have been allocated or fixed pages have not been allocated\n"));
       
  7964 			CLEANUP(Ldd.FreeAllFixedPages());
       
  7965 			TEST_FAIL;
       
  7966 			}
       
  7967 		else
       
  7968 			{
       
  7969 			test.Printf(_L("Passed...\n"));	
       
  7970 			}
       
  7971 		}
       
  7972 	TestEnd();
       
  7973 
       
  7974 
       
  7975 	test.Next(_L("Test11: No Discardable Flag, Alloc Discardable"));	
       
  7976 	TestStart();
       
  7977 	if (gPagedRom)
       
  7978 		{
       
  7979 		index = GetBestZone(BEST_DISCARDABLE);
       
  7980 			
       
  7981 		if (index == KErrNotFound)
       
  7982 			{
       
  7983 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  7984 			}
       
  7985 		else
       
  7986 			{
       
  7987 			zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  7988 			GetOriginalPageCount();
       
  7989 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_DISCARD_FLAG);
       
  7990 			test_KErrNone(r);
       
  7991 			UpdateRamInfo();
       
  7992 			
       
  7993 			TInt discardablePages;
       
  7994 			r = AllocDiscardable(discardablePages);
       
  7995 			if (r != KErrNoMemory)
       
  7996 				{// Allocation should fail as no dis flag is set
       
  7997 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  7998 				TEST_FAIL;
       
  7999 				}
       
  8000 			GetAllPageInfo();
       
  8001 
       
  8002 			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable)
       
  8003 				{
       
  8004 				test.Printf(_L("Fail: Discardable pages have been allocated into the zone\n"));
       
  8005 				CLEANUP(ResetDPCache());
       
  8006 				TEST_FAIL;
       
  8007 				}
       
  8008 			else
       
  8009 				{
       
  8010 				test.Printf(_L("Passed...\n"));	
       
  8011 				}
       
  8012 			}
       
  8013 		}
       
  8014 	else
       
  8015 		{
       
  8016 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  8017 		}
       
  8018 	TestEnd();
       
  8019 
       
  8020 //-----------------------------------------------------------------------------------------------
       
  8021 
       
  8022 	test.Next(_L("Test12: Only Discardable Flag, Alloc Movable"));	
       
  8023 	TestStart();
       
  8024 	
       
  8025 	index = GetBestZone(BEST_MOVABLE);
       
  8026 
       
  8027 	if (index == KErrNotFound)
       
  8028 		{
       
  8029 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8030 		}
       
  8031 	else
       
  8032 		{
       
  8033 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8034 
       
  8035 		GetOriginalPageCount();
       
  8036 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  8037 		test_KErrNone(r);
       
  8038 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
       
  8039 		GetAllPageInfo();
       
  8040 
       
  8041 		if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable)
       
  8042 			{
       
  8043 			test.Printf(_L("Fail: Movable pages have been allocated\n"));
       
  8044 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  8045 			TEST_FAIL;
       
  8046 			}
       
  8047 		else
       
  8048 			{
       
  8049 			test.Printf(_L("Passed...\n"));	
       
  8050 			}
       
  8051 		}
       
  8052 	TestEnd();
       
  8053 
       
  8054 
       
  8055 	test.Next(_L("Test13: Only Discardable Flag, Alloc Fixed"));	
       
  8056 	TestStart();
       
  8057 	
       
  8058 	if(gPagedRom)
       
  8059 		{
       
  8060 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  8061 		test_KErrNone(r);
       
  8062 		}
       
  8063 
       
  8064 	index = GetBestZone(BEST_FIXED);
       
  8065 
       
  8066 	if (index == KErrNotFound)
       
  8067 		{
       
  8068 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8069 		}
       
  8070 	else
       
  8071 		{
       
  8072 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8073 
       
  8074 		GetOriginalPageCount();
       
  8075 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  8076 		test_KErrNone(r);
       
  8077 
       
  8078 		// Fill up all of RAM with fixed pages.
       
  8079 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  8080 
       
  8081 		GetAllPageInfo();
       
  8082 		if (gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  8083 			{
       
  8084 			test.Printf(_L("Fail: Fixed pages have been allocated\n"));
       
  8085 			CLEANUP(Ldd.FreeAllFixedPages());
       
  8086 			TEST_FAIL;
       
  8087 			}
       
  8088 		else
       
  8089 			{
       
  8090 			test.Printf(_L("Passed...\n"));	
       
  8091 			}	
       
  8092 		}
       
  8093 	TestEnd();
       
  8094 
       
  8095 
       
  8096 	test.Next(_L("Test14: Only Discardable Flag, Alloc Discardable"));	
       
  8097 	TestStart();
       
  8098 	if (gPagedRom)
       
  8099 		{
       
  8100 		index = GetBestZone(BEST_DISCARDABLE);
       
  8101 
       
  8102 		if (index == KErrNotFound)
       
  8103 			{
       
  8104 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8105 			}
       
  8106 		else
       
  8107 			{
       
  8108 			zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8109 
       
  8110 			GetOriginalPageCount();
       
  8111 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  8112 			test_KErrNone(r);
       
  8113 			UpdateRamInfo();
       
  8114 			
       
  8115 			TInt discardablePages;
       
  8116 			r = AllocDiscardable(discardablePages);
       
  8117 			if (r != KErrNone)
       
  8118 				{
       
  8119 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  8120 				TEST_FAIL;
       
  8121 				}
       
  8122 			GetAllPageInfo();
       
  8123 
       
  8124 			if (gZoneUtilArray[index].iAllocMovable > gOriginalPageCountArray[index].iAllocMovable ||
       
  8125 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  8126 				{
       
  8127 				test.Printf(_L("Fail: Pages other than discardable have been allocated\n"));
       
  8128 				CLEANUP(ResetDPCache());
       
  8129 				TEST_FAIL;
       
  8130 				}
       
  8131 			else
       
  8132 				{
       
  8133 				test.Printf(_L("Passed...\n"));	
       
  8134 				}
       
  8135 			}
       
  8136 		}
       
  8137 	else
       
  8138 		{
       
  8139 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  8140 		}
       
  8141 	TestEnd();
       
  8142 
       
  8143 	test.Next(_L("Test15: Only Discardable Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
       
  8144 	TestStart();
       
  8145 	
       
  8146 	index = GetBestZone(BEST_FIXED);
       
  8147 	
       
  8148 	if (index == KErrNotFound)
       
  8149 		{
       
  8150 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8151 		}
       
  8152 	else
       
  8153 		{
       
  8154 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8155 
       
  8156 		GetOriginalPageCount();
       
  8157 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  8158 		test_KErrNone(r);		
       
  8159 
       
  8160 		// Just need to try and allocate one page
       
  8161 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
       
  8162 
       
  8163 		GetAllPageInfo();
       
  8164 		if (r != KErrNone)
       
  8165 			{
       
  8166 			test.Printf(_L("Fail: Fixed pages have not been allocated into zone ID 0x%x\n"), zoneDefragID);
       
  8167 			CLEANUP(Ldd.FreeAllFixedPages());
       
  8168 			TEST_FAIL;
       
  8169 			}
       
  8170 		else
       
  8171 			{
       
  8172 			test.Printf(_L("Passed...\n"));	
       
  8173 			}	
       
  8174 		}	
       
  8175 	TestEnd();
       
  8176 
       
  8177 	test.Next(_L("Test16: Only Discardable Flag, Alloc Fixed by attempting to claim zone"));	
       
  8178 	TestStart();
       
  8179 	
       
  8180 	GetAllPageInfo();
       
  8181 	index = 0;	
       
  8182 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  8183 		{
       
  8184 		index++;
       
  8185 		}
       
  8186 	
       
  8187 	if ((TUint)index == gZoneCount)
       
  8188 		{
       
  8189 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8190 		}
       
  8191 	else
       
  8192 		{
       
  8193 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8194 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, ONLY_DISCARD_FLAG);
       
  8195 		test_KErrNone(r);		
       
  8196 
       
  8197 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
       
  8198 		GetAllPageInfo();
       
  8199 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  8200 		if (freeInOthers && r != KErrNone)
       
  8201 			{
       
  8202 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
       
  8203 			TEST_FAIL;
       
  8204 			}
       
  8205 		else
       
  8206 			{
       
  8207 			test.Printf(_L("Passed...\n"));	
       
  8208 			}		
       
  8209 		}	
       
  8210 	TestEnd();
       
  8211 //-----------------------------------------------------------------------------------------------
       
  8212 
       
  8213 	test.Next(_L("Test17: No further alloc Flag, Alloc Movable"));	
       
  8214 	TestStart();
       
  8215 	
       
  8216 	index = GetBestZone(BEST_MOVABLE);
       
  8217 
       
  8218 	
       
  8219 	if (index == KErrNotFound)
       
  8220 		{
       
  8221 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8222 		}
       
  8223 	else
       
  8224 		{
       
  8225 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8226 
       
  8227 		GetOriginalPageCount();
       
  8228 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
       
  8229 		test_KErrNone(r);
       
  8230 		AllocMovable(gChunkArray1, gChunkArraySize1, KFillAllMovable, KChunkDefaultSize, EFalse);
       
  8231 		GetAllPageInfo();
       
  8232 
       
  8233 		if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
       
  8234 			gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
       
  8235 			gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  8236 			{
       
  8237 			test.Printf(_L("Fail: Pages been allocated\n"));
       
  8238 			CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  8239 			TEST_FAIL;
       
  8240 			}
       
  8241 		else
       
  8242 			{
       
  8243 			test.Printf(_L("Passed...\n"));	
       
  8244 			}
       
  8245 		}
       
  8246 	TestEnd();
       
  8247 
       
  8248 
       
  8249 	test.Next(_L("Test18: No further alloc Flag, Alloc Fixed"));	
       
  8250 	TestStart();
       
  8251 	
       
  8252 	if(gPagedRom)
       
  8253 		{
       
  8254 		r = DPTest::SetCacheSize(gOriginalMinCacheSize, gOriginalMinCacheSize);
       
  8255 		test_KErrNone(r);
       
  8256 		}
       
  8257 
       
  8258 	// Find a zone with free pages and set the flag on this zone 
       
  8259 	// as it will ensure that you cannot fill all of free RAM with fixed pages
       
  8260 	index = KErrNotFound;
       
  8261 	GetAllPageInfo();
       
  8262 	for (TUint i = 0; i < gZoneCount; i++)
       
  8263 		{
       
  8264 		if (gZoneUtilArray[i].iFreePages != 0)
       
  8265 			{
       
  8266 			index = i;
       
  8267 			break;
       
  8268 			}
       
  8269 		}
       
  8270 	if (index == KErrNotFound)
       
  8271 		{
       
  8272 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8273 		}
       
  8274 	else
       
  8275 		{ 
       
  8276 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8277 
       
  8278 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
       
  8279 		test_KErrNone(r);
       
  8280 
       
  8281 		GetOriginalPageCount();
       
  8282 		r = Ldd.AllocateFixed(FILL_ALL_FIXED);
       
  8283 		
       
  8284 		// Ensure memory wasn't filled as it should have hit the blocked zone.
       
  8285 		GetAllPageInfo();
       
  8286 		if (r != KErrNoMemory || gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  8287 			{
       
  8288 			test.Printf(_L("Fail: orig fix 0x%x current fix 0x%x\n"), gOriginalPageCountArray[index].iAllocFixed, gZoneUtilArray[index].iAllocFixed);
       
  8289 			CLEANUP(Ldd.FreeAllFixedPages());
       
  8290 			TEST_FAIL;
       
  8291 			}
       
  8292 		else
       
  8293 			{
       
  8294 			test.Printf(_L("Passed...\n"));	
       
  8295 			}
       
  8296 		}
       
  8297 	TestEnd();
       
  8298 
       
  8299 
       
  8300 	test.Next(_L("Test19: No further alloc Flag, Alloc Discardable"));	
       
  8301 	TestStart();
       
  8302 	if (gPagedRom)
       
  8303 		{
       
  8304 		index = GetBestZone(BEST_DISCARDABLE);
       
  8305 		
       
  8306 		if (index == KErrNotFound)
       
  8307 			{
       
  8308 			test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8309 			}
       
  8310 		else
       
  8311 			{
       
  8312 			zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8313 
       
  8314 			GetOriginalPageCount();
       
  8315 			r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
       
  8316 			test_KErrNone(r);
       
  8317 			UpdateRamInfo();
       
  8318 			TInt discardablePages;
       
  8319 			r = AllocDiscardable(discardablePages);
       
  8320 			if (r != KErrNoMemory)
       
  8321 				{// Allocation should fail as no alloc flag is set
       
  8322 				test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  8323 				TEST_FAIL;
       
  8324 				}
       
  8325 			GetAllPageInfo();
       
  8326 
       
  8327 			if (gZoneUtilArray[index].iAllocDiscardable > gOriginalPageCountArray[index].iAllocDiscardable || 
       
  8328 				gZoneUtilArray[index].iAllocMovable >  gOriginalPageCountArray[index].iAllocMovable || 
       
  8329 				gZoneUtilArray[index].iAllocFixed > gOriginalPageCountArray[index].iAllocFixed)
       
  8330 				{
       
  8331 				test.Printf(_L("Fail: Pages have been allocated into the zone\n"));
       
  8332 				CLEANUP(ResetDPCache());
       
  8333 				TEST_FAIL;
       
  8334 				}
       
  8335 			else
       
  8336 				{
       
  8337 				test.Printf(_L("Passed...\n"));	
       
  8338 				}
       
  8339 			}
       
  8340 		}
       
  8341 	else
       
  8342 		{
       
  8343 		test.Printf(_L("Not a paged ROM - Skipping test step\n"));
       
  8344 		}
       
  8345 	TestEnd();
       
  8346 
       
  8347 	
       
  8348 	test.Next(_L("Test20: No Further Alloc Flag, Alloc Fixed using ZoneAllocPhyicalRam"));	
       
  8349 	TestStart();
       
  8350 	
       
  8351 	GetAllPageInfo();
       
  8352 	index = 0;
       
  8353 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iFreePages == 0 || 
       
  8354 			gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  8355 		{
       
  8356 		index++;
       
  8357 		}
       
  8358 	
       
  8359 	if ((TUint)index == gZoneCount)
       
  8360 		{
       
  8361 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8362 		}
       
  8363 	else
       
  8364 		{
       
  8365 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8366 
       
  8367 		GetOriginalPageCount();
       
  8368 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
       
  8369 		test_KErrNone(r);		
       
  8370 
       
  8371 		// Just need to try and allocate one page
       
  8372 		r = Ldd.ZoneAllocDiscontiguous(zoneDefragID, 1);
       
  8373 
       
  8374 		GetAllPageInfo();
       
  8375 		if (r != KErrNoMemory)
       
  8376 			{
       
  8377 			test.Printf(_L("Fail: Fixed pages have been allocated into zone ID 0x%x\n"), zoneDefragID);
       
  8378 			CLEANUP(Ldd.FreeAllFixedPages());
       
  8379 			TEST_FAIL;
       
  8380 			}
       
  8381 		else
       
  8382 			{
       
  8383 			test.Printf(_L("Passed...\n"));	
       
  8384 			}
       
  8385 		}	
       
  8386 	TestEnd();
       
  8387 
       
  8388 	test.Next(_L("Test21: No Further Alloc Flag, Alloc Fixed by attempting to claim zone"));	
       
  8389 	TestStart();
       
  8390 	
       
  8391 	GetAllPageInfo();
       
  8392 	index = 0;	
       
  8393 	while ((TUint)index < gZoneCount && (gZoneUtilArray[index].iAllocFixed != 0 || gZoneUtilArray[index].iAllocUnknown != 0))
       
  8394 		{
       
  8395 		index++;
       
  8396 		}
       
  8397 	
       
  8398 	if ((TUint)index == gZoneCount)
       
  8399 		{
       
  8400 		test.Printf(_L("Cannot find zone to perform test - Skipping test step...\n"));
       
  8401 		}
       
  8402 	else
       
  8403 		{
       
  8404 		zoneDefragID = gZoneConfigArray[index].iZoneId;
       
  8405 		r = Ldd.SetZoneFlag(zoneDefragID, gZoneConfigArray[index].iFlags, NO_ALLOC_FLAG);
       
  8406 		test_KErrNone(r);		
       
  8407 
       
  8408 		r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneDefragID);
       
  8409 		GetAllPageInfo();
       
  8410 		TUint freeInOthers = gTotalPageCount.iFreePages - gZoneUtilArray[index].iFreePages;
       
  8411 		if (freeInOthers && r != KErrNone)
       
  8412 			{
       
  8413 			test.Printf(_L("Fail: Claim zone ID 0x%x was unsuccessful, r = %d\n"), zoneDefragID, r);
       
  8414 			CLEANUP(ResetRamZoneFlags());
       
  8415 			TEST_FAIL;
       
  8416 			}
       
  8417 		else
       
  8418 			{
       
  8419 			test.Printf(_L("Passed...\n"));	
       
  8420 			}
       
  8421 		}	
       
  8422 	TestEnd();
       
  8423 
       
  8424 	TUint mostPrefArrayIndex = 0;
       
  8425 	TUint mostPrefIndex = 0;
       
  8426 	TUint leastPrefIndex = 0;
       
  8427 	const TUint KFreeMostPref = 10;
       
  8428 	TUint flag = 0;
       
  8429 	TUint prefIndex;
       
  8430 	TUint totalFree;
       
  8431 
       
  8432 
       
  8433 	for (TUint testStep = 0; testStep < 2; testStep++)
       
  8434 		{
       
  8435 		switch(testStep)
       
  8436 			{
       
  8437 			case 0:
       
  8438 				test.Next(_L("Test22: Ensure that the General Defrag looks at the flags 1"));
       
  8439 				break;
       
  8440 
       
  8441 			case 1:
       
  8442 				test.Next(_L("Test23: Ensure that the General Defrag looks at the flags 2"));
       
  8443 				break;
       
  8444 			}
       
  8445 			
       
  8446 		TestStart();
       
  8447 		gChunkArray1 = new RChunk;	
       
  8448 		gChunkArraySize1 = 1;
       
  8449 		TUint minCacheSize = 0;
       
  8450 		TUint maxCacheSize = 0;
       
  8451 		TUint currentCacheSize = 0;
       
  8452 		TUint freeNeededInMostPref = 0;
       
  8453 		if (!gPagedRom)
       
  8454 			{
       
  8455 			test.Printf(_L("Not a paged ROM - Skipping...\n"));
       
  8456 			goto skipTest22;
       
  8457 			}
       
  8458 
       
  8459 		// Find the most pref zone with free pages
       
  8460 		r = FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex);
       
  8461 		if (r != KErrNone)
       
  8462 			{
       
  8463 			test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
       
  8464 			goto skipTest22;
       
  8465 			}
       
  8466 
       
  8467 		// Ensure that the least pref zone is empty
       
  8468 		leastPrefIndex = gPrefArray[gZoneCount - 1];
       
  8469 		if (gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
       
  8470 			{
       
  8471 			test.Printf(_L("Least pref zone is not empty - Skipping...\n"));
       
  8472 			goto skipTest22;
       
  8473 			}
       
  8474 
       
  8475 		// Allocate 1 movable page to the least preferable zone
       
  8476 		r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
       
  8477 		if (r != KErrNone)
       
  8478 			{			
       
  8479 			test.Printf(_L("Failed to allocate movable r = %d - Skipping...\n"), r);
       
  8480 			goto skipTest22;
       
  8481 			}
       
  8482 		
       
  8483 		// Allocate 1 discardable page to the least preferable zone
       
  8484 		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
       
  8485 			{
       
  8486 			TInt disPages;
       
  8487 			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
       
  8488 			if (r != KErrNone)
       
  8489 				{			
       
  8490 				test.Printf(_L("Failed to allocate discardable pages r = %d - Skipping...\n"), r);
       
  8491 				goto skipTest22;
       
  8492 				}
       
  8493 
       
  8494 			// up the minimum cache size so that the pages have to be moved - not discarded
       
  8495 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  8496 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
       
  8497 			if (r != KErrNone)
       
  8498 				{			
       
  8499 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
       
  8500 				goto skipTest22;
       
  8501 				}
       
  8502 			}
       
  8503 		else
       
  8504 			{
       
  8505 			test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
       
  8506 			goto skipTest22;
       
  8507 			}
       
  8508 
       
  8509 		// Check that the least pref zone has movable and discardable pages in it
       
  8510 		GetAllPageInfo();
       
  8511 		if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
       
  8512 			gZoneUtilArray[leastPrefIndex].iAllocDiscardable == 0)
       
  8513 			{
       
  8514 			test.Printf(_L("No movable or discardable in least pref zone\n"));
       
  8515 			PrintPageInfo();
       
  8516 			goto skipTest22;
       
  8517 			}
       
  8518 
       
  8519 		ResetRamZoneFlags();
       
  8520 		// if most pref zone has too many free pages fill up with fixed
       
  8521 		if (gZoneUtilArray[mostPrefIndex].iFreePages > KFreeMostPref)
       
  8522 			{
       
  8523 			TUint allocPages = gZoneUtilArray[mostPrefIndex].iFreePages - KFreeMostPref;
       
  8524 			r = Ldd.ZoneAllocToMany(mostPrefIndex, allocPages);
       
  8525 			if (r != KErrNone)
       
  8526 				{	
       
  8527 				test.Printf(_L("Failed allocate 0x%x fixed to index %d r = %d - Skipping...\n"), 
       
  8528 								allocPages, mostPrefIndex,r);
       
  8529 				PrintPageInfo();
       
  8530 				goto skipTest22;
       
  8531 				}
       
  8532 			}
       
  8533 		
       
  8534 		
       
  8535 		// if the no. of discardable pages is less than free in most pref increase the min cache size
       
  8536 		GetAllPageInfo();
       
  8537 		if (gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gZoneUtilArray[mostPrefIndex].iFreePages)
       
  8538 			{
       
  8539 			TUint discDiff = gZoneUtilArray[mostPrefIndex].iFreePages - gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
       
  8540 			test.Printf(_L("discDiff = 0x%x\n"), discDiff);
       
  8541 			TInt disPages;
       
  8542 			if (ZoneAllocDiscard(leastPrefIndex, discDiff, disPages) != KErrNone)
       
  8543 				{			
       
  8544 				test.Printf(_L("Failed allocate discardable to zone index %d- Skipping...\n"), leastPrefIndex);
       
  8545 				goto skipTest22;
       
  8546 				}
       
  8547 			// up the minimum cache size by the difference as we don't want these pages to be discarded
       
  8548 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  8549 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
       
  8550 			test.Printf(_L("r = %d\n"), r);
       
  8551 			if (r != KErrNone)
       
  8552 				{
       
  8553 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
       
  8554 				goto skipTest22;
       
  8555 				}
       
  8556 			}
       
  8557 		ResetRamZoneFlags();
       
  8558 
       
  8559 		// if the rest of the zones are either full or empty put a fixed page into the next most pref empty zone
       
  8560 		GetPrefOrder();
       
  8561 		prefIndex = 0;
       
  8562 		totalFree = 0;
       
  8563 		for (; prefIndex < gZoneCount && totalFree < gZoneUtilArray[leastPrefIndex].iAllocDiscardable; prefIndex++)
       
  8564 			{// Look for zone that has enough free pages to fit all the discardable
       
  8565 			TUint zoneIndex = gPrefArray[prefIndex];
       
  8566 			if (zoneIndex != mostPrefIndex && zoneIndex != leastPrefIndex)
       
  8567 				{
       
  8568 				if (gZoneUtilArray[zoneIndex].iFreePages != gZoneUtilArray[zoneIndex].iPhysPages)
       
  8569 					{
       
  8570 					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
       
  8571 					}
       
  8572 				else
       
  8573 					{
       
  8574 					r = Ldd.ZoneAllocToMany(zoneIndex, 1);
       
  8575 					if (r != KErrNone)
       
  8576 						{
       
  8577 						test.Printf(_L("Couldn't alloc fixed to zone index %d - r = %d\n"), zoneIndex, r);
       
  8578 						goto skipTest22;
       
  8579 						}
       
  8580 					GetAllPageInfo();
       
  8581 					totalFree += gZoneUtilArray[zoneIndex].iFreePages;
       
  8582 					}
       
  8583 				}
       
  8584 			}
       
  8585 
       
  8586 		if (prefIndex >= gZoneCount)
       
  8587 			{
       
  8588 			test.Printf(_L("Couldn't find zone\n"));
       
  8589 			goto skipTest22;
       
  8590 			}
       
  8591 
       
  8592 
       
  8593 		
       
  8594 		// If the most preferable zone does not have enough free pages, skip
       
  8595 		freeNeededInMostPref = (testStep == 0) ? gZoneUtilArray[leastPrefIndex].iAllocMovable : gZoneUtilArray[leastPrefIndex].iAllocDiscardable;
       
  8596 		if (gZoneUtilArray[mostPrefIndex].iFreePages < freeNeededInMostPref)
       
  8597 			{
       
  8598 			test.Printf(_L("Free needed in mostPref(%d) = %d, Free available in mostPref = %d - skipping...\n"), 
       
  8599 							mostPrefIndex,freeNeededInMostPref,gZoneUtilArray[mostPrefIndex].iFreePages);
       
  8600 			goto skipTest22;
       
  8601 			}
       
  8602 
       
  8603 		GetAllPageInfo();
       
  8604 		// Set up the RAM zone flags for the test
       
  8605 		flag = (testStep == 0)? NO_MOVE_FLAG: NO_DISCARD_FLAG;
       
  8606 		// Set all zones except most pref to KRamZoneFlagNoMovable	or KRamZoneFlagNoDiscard
       
  8607 		for (TUint index = 0; index < gZoneCount; index++)
       
  8608 			{
       
  8609 			TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  8610 			if (index != mostPrefIndex)
       
  8611 				{
       
  8612 				r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, flag);
       
  8613 				if (r != KErrNone)
       
  8614 					{			
       
  8615 					test.Printf(_L("Failed to set flag r = %d - Skipping...\n"), r);
       
  8616 					goto skipTest22;
       
  8617 					}
       
  8618 				}
       
  8619 			}
       
  8620 		
       
  8621 		GetOriginalPageCount();
       
  8622 		r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  8623 		
       
  8624 		GetAllPageInfo();
       
  8625 		switch(testStep)
       
  8626 			{
       
  8627 			case 0:
       
  8628 				if (r != KErrNone ||
       
  8629 					gOriginalPageCountArray[mostPrefIndex].iAllocMovable >= gZoneUtilArray[mostPrefIndex].iAllocMovable ||
       
  8630 					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
       
  8631 					{
       
  8632 					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
       
  8633 									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
       
  8634 									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
       
  8635 									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
       
  8636 					CLEANUP(Ldd.FreeAllFixedPages());
       
  8637 					TEST_FAIL;
       
  8638 					}
       
  8639 				else
       
  8640 					{
       
  8641 					test.Printf(_L("Passed...\n"));
       
  8642 					}
       
  8643 				break;
       
  8644 
       
  8645 			case 1:
       
  8646 				if (r != KErrNone ||
       
  8647 					gOriginalPageCountArray[mostPrefIndex].iAllocDiscardable >= gZoneUtilArray[mostPrefIndex].iAllocDiscardable ||
       
  8648 					gZoneUtilArray[leastPrefIndex].iFreePages != gZoneUtilArray[leastPrefIndex].iPhysPages)
       
  8649 					{
       
  8650 					test.Printf(_L("FAIL:r=%d MostPref(%d): origMov 0x%x curMov 0x%x LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
       
  8651 									r, mostPrefIndex, gOriginalPageCountArray[mostPrefIndex].iAllocMovable, gZoneUtilArray[mostPrefIndex].iAllocMovable, 
       
  8652 									leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
       
  8653 									gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
       
  8654 					CLEANUP(Ldd.FreeAllFixedPages());
       
  8655 					TEST_FAIL;
       
  8656 					}
       
  8657 				else
       
  8658 					{
       
  8659 					test.Printf(_L("Passed...\n"));
       
  8660 					}
       
  8661 				break;
       
  8662 			}
       
  8663 
       
  8664 	skipTest22:
       
  8665 		TestEnd();
       
  8666 		}
       
  8667 
       
  8668 	test.Next(_L("Test24: Ensure that the General Defrag doesnt move or discard pages if KRamZoneFlagNoMovable set on all zones "));	
       
  8669 	TestStart();
       
  8670 	gChunkArray1 = new RChunk;
       
  8671 	gChunkArraySize1 = 1;
       
  8672 	// Find the most pref zone with free pages
       
  8673 	GetPrefOrder();
       
  8674 	TInt disPages = 0;	
       
  8675 	if (FindMostPrefWithFree(mostPrefIndex, &mostPrefArrayIndex) != KErrNone)
       
  8676 		{
       
  8677 		test.Printf(_L("Cannot find zone with free pages - Skipping...\n"));
       
  8678 		goto skipTest24;
       
  8679 		}
       
  8680 
       
  8681 	// Ensure that the least pref zone has free pages in it
       
  8682 	leastPrefIndex = gPrefArray[gZoneCount-1];
       
  8683 	if (gZoneUtilArray[leastPrefIndex].iFreePages == 0)
       
  8684 		{
       
  8685 		test.Printf(_L("Least pref zone has no free pages - Skipping...\n"));
       
  8686 		goto skipTest24;
       
  8687 		}
       
  8688 
       
  8689 	// Allocate 1 movable page to the least preferable zone
       
  8690 	r = ZoneAllocMovable(gChunkArray1, gChunkArraySize1, leastPrefIndex, 1);
       
  8691 	if (r != KErrNone)
       
  8692 		{			
       
  8693 		test.Printf(_L("Failed to allocate movable page r = %d - Skipping...\n"), r);
       
  8694 		goto skipTest24;
       
  8695 		}
       
  8696 	
       
  8697 	if (gPagedRom)
       
  8698 		{
       
  8699 		
       
  8700 		TUint minCacheSize = 0;
       
  8701 		TUint maxCacheSize = 0;
       
  8702 		TUint currentCacheSize = 0;
       
  8703 		
       
  8704 		// Allocate 1 discardable page to the least preferable zone
       
  8705 		if (gZoneUtilArray[leastPrefIndex].iFreePages != 0)
       
  8706 			{
       
  8707 			r = ZoneAllocDiscard(leastPrefIndex, 1, disPages);
       
  8708 			if (r != KErrNone)
       
  8709 				{
       
  8710 				test.Printf(_L("Discardable pages not allocated r= %d\n"), r);
       
  8711 				}
       
  8712 			
       
  8713 			DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  8714 			r = DPTest::SetCacheSize(currentCacheSize, currentCacheSize);
       
  8715 			if (r != KErrNone)
       
  8716 				{			
       
  8717 				test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
       
  8718 				goto skipTest24;
       
  8719 				}
       
  8720 			}	
       
  8721 		}
       
  8722 
       
  8723 	
       
  8724 	// Check that the least pref zone has movable and discardable (if allocated) pages in it
       
  8725 	GetAllPageInfo();
       
  8726 	if (gZoneUtilArray[leastPrefIndex].iAllocMovable == 0 ||
       
  8727 		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < (TUint)disPages ||
       
  8728 		gZoneUtilArray[leastPrefIndex].iAllocMovable > gTotalPageCount.iFreePages - gZoneUtilArray[leastPrefIndex].iFreePages)
       
  8729 		{
       
  8730 		test.Printf(_L("No movable in least pref zone or no space for moveable pages to be moved to\n"));
       
  8731 		PrintPageInfo();
       
  8732 		goto skipTest24;
       
  8733 		}
       
  8734 
       
  8735 	ResetRamZoneFlags();
       
  8736 	GetAllPageInfo();
       
  8737 	// Now set all zones to KRamZoneFlagNoMovable
       
  8738 	for (TUint index = 0; index < gZoneCount; index++)
       
  8739 		{
       
  8740 		TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  8741 		r = Ldd.SetZoneFlag(zoneID, gZoneConfigArray[index].iFlags, NO_MOVE_FLAG);
       
  8742 		if (r != KErrNone)
       
  8743 			{			
       
  8744 			test.Printf(_L("Failed to set cache size r = %d - Skipping...\n"), r);
       
  8745 			goto skipTest24;
       
  8746 			}
       
  8747 		}
       
  8748 
       
  8749 	GetOriginalPageCount();
       
  8750 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  8751 	GetAllPageInfo();
       
  8752 	if (r != KErrNone ||
       
  8753 		gOriginalPageCountArray[leastPrefIndex].iAllocMovable != gZoneUtilArray[leastPrefIndex].iAllocMovable ||
       
  8754 		gZoneUtilArray[leastPrefIndex].iAllocDiscardable < gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable)
       
  8755 		{
       
  8756 		test.Printf(_L("FAIL:r=%d LeastPref(%d): origMov 0x%x curMov 0x%x origDis 0x%x curDis 0x%x\n"), 
       
  8757 						r,leastPrefIndex, gOriginalPageCountArray[leastPrefIndex].iAllocMovable, gZoneUtilArray[leastPrefIndex].iAllocMovable,
       
  8758 						gOriginalPageCountArray[leastPrefIndex].iAllocDiscardable, gZoneUtilArray[leastPrefIndex].iAllocDiscardable);
       
  8759 		CLEANUP(ResetRamZoneFlags());
       
  8760 		TEST_FAIL;
       
  8761 		}
       
  8762 	else
       
  8763 		{
       
  8764 		test.Printf(_L("Passed...\n"));
       
  8765 		}
       
  8766 skipTest24:
       
  8767 	TestEnd();
       
  8768 
       
  8769 	test.End();
       
  8770 	return KErrNone;
       
  8771 	}
       
  8772 
       
  8773 
       
  8774 //
       
  8775 // Template functions encapsulating ControlIo magic
       
  8776 //
       
  8777 template <class C>
       
  8778 TInt controlIo(RFs &fs, TInt drv, TInt fkn, C &c)
       
  8779 	{
       
  8780     TPtr8 ptrC((TUint8 *)&c, sizeof(C), sizeof(C));
       
  8781 
       
  8782     TInt r = fs.ControlIo(drv, fkn, ptrC);
       
  8783 
       
  8784     return r;
       
  8785 	}
       
  8786 
       
  8787 //
       
  8788 // FormatMMC
       
  8789 //
       
  8790 // Formats the MMC card
       
  8791 //
       
  8792 void FormatMMC()
       
  8793 	{	
       
  8794 	test.Printf(_L("Formatting MMC...\n"));
       
  8795 
       
  8796 	RFs theFs;
       
  8797 	TBuf<4> driveBuf = _L("D:\\");
       
  8798 	RFormat format;
       
  8799 	TInt count;
       
  8800 	TChar driveLet;
       
  8801 
       
  8802 	TInt r = theFs.Connect();
       
  8803 	test_KErrNone(r);
       
  8804 	
       
  8805 	r = theFs.DriveToChar(gDrive, driveLet);
       
  8806 	test_KErrNone(r);
       
  8807 
       
  8808 	driveBuf[0] = driveLet;
       
  8809 	test.Printf(_L("Formatting Drive: %C\n"),(TInt)driveLet);
       
  8810 	
       
  8811 	r = format.Open(theFs,driveBuf,EFullFormat,count);
       
  8812 	test_KErrNone(r);
       
  8813 	
       
  8814 	while(count)
       
  8815 		{
       
  8816 		TInt r = format.Next(count);
       
  8817 		test_KErrNone(r);
       
  8818 		}
       
  8819 	format.Close();
       
  8820 	theFs.Close();
       
  8821 	}
       
  8822 
       
  8823 
       
  8824 //
       
  8825 // FileNameGen
       
  8826 //
       
  8827 // Generates file names to create the files
       
  8828 //
       
  8829 void FileNameGen(TDes16& aBuffer, TInt aLong, TInt aPos) 
       
  8830 	{
       
  8831 	TInt padding;
       
  8832 	TInt i = 0;
       
  8833 	TBuf16<10> tempbuf;
       
  8834 
       
  8835 	_LIT(KNumber,"%d");
       
  8836 	tempbuf.Format(KNumber,aPos);
       
  8837 	
       
  8838 	padding = aLong-tempbuf.Size()/2;
       
  8839 	aBuffer = _L("");
       
  8840 	
       
  8841 	while(i < padding)
       
  8842 		{
       
  8843 		aBuffer.Append('F');
       
  8844 		i++;
       
  8845 		}
       
  8846 	aBuffer.Append(tempbuf);
       
  8847 
       
  8848 	_LIT(KExtension1, ".TXT");
       
  8849 	aBuffer.Append(KExtension1);
       
  8850 	}
       
  8851 
       
  8852 
       
  8853 //
       
  8854 // CreateFiles
       
  8855 //
       
  8856 // Creates the files to fill part of the read cache
       
  8857 //
       
  8858 void CreateFiles(TInt aFiles, TInt aFileSize)
       
  8859 	{
       
  8860 	TInt i = 0, r = 0;
       
  8861 	RFile file;
       
  8862 	TBuf16<50> directory;
       
  8863 	
       
  8864 	TBuf16<50> path;
       
  8865 	TBuf16<50> buffer(50); 	
       
  8866 	
       
  8867 	directory = gSessionPath;
       
  8868 	
       
  8869 	test.Printf(_L("Creating %d files for filling the cache (size %d)\n"), aFiles, aFileSize);
       
  8870 
       
  8871 	// create a big buffer to speed things up
       
  8872 	HBufC8* bigBuf = NULL;
       
  8873 	const TInt KBigBufferSize = 32 * 1024;
       
  8874 	TRAPD(res,bigBuf = HBufC8::NewL(KBigBufferSize));
       
  8875 	test(res == KErrNone && bigBuf != NULL);
       
  8876 		
       
  8877 	TPtr8 bigBufWritePtr(NULL, 0);	
       
  8878 	bigBufWritePtr.Set(bigBuf->Des());
       
  8879 
       
  8880 	// Fill the buffer
       
  8881 	TChar aC = 'A';
       
  8882 	for(i = 0; i < KBigBufferSize; i++)
       
  8883 		{
       
  8884 		bigBufWritePtr.Append((i%32) + aC);
       
  8885 		}
       
  8886 	
       
  8887 
       
  8888 	i = 0;		
       
  8889 	while(i < aFiles) 
       
  8890 		{
       
  8891 		if (i % 10 == 0)
       
  8892 			test.Printf(_L("Creating file %d of %d...\r"), i, aFiles);
       
  8893 		FileNameGen(buffer, 8, i+3) ;
       
  8894 		path = directory;
       
  8895 		path.Append(buffer);
       
  8896 
       
  8897 		// delete file first to ensure it's contents are not in the cache (file may be on the closed file queue)
       
  8898 		r = gTheFs.Delete(path);
       
  8899 		test(r == KErrNone || r == KErrNotFound);
       
  8900 
       
  8901 		r = file.Create(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
       
  8902 		if(r == KErrAlreadyExists) 
       
  8903 			r = file.Open(gTheFs,path,EFileShareAny|EFileWrite|EFileReadDirectIO|EFileWriteDirectIO);
       
  8904 		TInt j = 0;
       
  8905 		i++;
       
  8906 		while(j < aFileSize)
       
  8907 			{
       
  8908 			bigBufWritePtr.SetLength(Min(KBigBufferSize, aFileSize - j));
       
  8909 			r = file.Write(bigBufWritePtr);
       
  8910 			// Running out of disk space is expected for the last file.
       
  8911 			// Premature "disk full" conditions need to abort.
       
  8912 			if (r == KErrDiskFull)
       
  8913 				{
       
  8914 				test(i == aFiles);
       
  8915 				break;
       
  8916 				}
       
  8917 			test_KErrNone(r);
       
  8918 			j += bigBufWritePtr.Length();
       
  8919 			}					
       
  8920 
       
  8921 		file.Close();
       
  8922 		}
       
  8923 	test.Printf(_L("\nFiles created\n"));
       
  8924 	delete bigBuf;
       
  8925 	}
       
  8926 
       
  8927 
       
  8928 //
       
  8929 // FillCache
       
  8930 //
       
  8931 // Allocate discardable pages using file system caching
       
  8932 //
       
  8933 TInt FillCache(TInt aFiles, TInt aFileSize)
       
  8934 	{
       
  8935 	// Fail if files already open
       
  8936 	test(!gFileCacheRun);
       
  8937 
       
  8938 
       
  8939 	TInt i = 0, r = 0;
       
  8940 	TBuf16<50> directory;
       
  8941 	
       
  8942 	TBuf16<50> path;
       
  8943 	TBuf16<50> buffer(50); 	
       
  8944 	HBufC8* buf = NULL;
       
  8945 	TPtr8 bufPtr(NULL, 0);	
       
  8946 	
       
  8947 	TRAPD(res,buf = HBufC8::NewL(2));
       
  8948 	test(res == KErrNone && buf != NULL);
       
  8949 	bufPtr.Set(buf->Des());
       
  8950 	
       
  8951 	TESTDEBUG(test.Printf(_L("Filling the cache\n")));
       
  8952 
       
  8953 	directory = gSessionPath;
       
  8954 	
       
  8955 	i = 0;		
       
  8956 
       
  8957 	GetAllPageInfo();
       
  8958 	TESTDEBUG(test.Printf(_L("total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
       
  8959 	
       
  8960 
       
  8961 	while(i < aFiles) 
       
  8962 		{
       
  8963 		FileNameGen(buffer, 8, i+3) ;
       
  8964 		path = directory;
       
  8965 		path.Append(buffer);
       
  8966 		r = gFile[i].Open(gTheFs,path,EFileShareAny|EFileRead|EFileReadBuffered|EFileReadAheadOff);
       
  8967 		test_KErrNone(r);
       
  8968 		
       
  8969 		TInt j = 0;
       
  8970 		while(j < aFileSize)
       
  8971 			{
       
  8972 			r = gFile[i].Read(j,bufPtr);
       
  8973 			test_KErrNone(r);
       
  8974 			j += 4 * 1024;
       
  8975 			}					
       
  8976 		i++;
       
  8977 		}
       
  8978 	gFileCacheRun = ETrue;
       
  8979 	GetAllPageInfo();
       
  8980 	TESTDEBUG(test.Printf(_L("after - total disc pages = %d\n"), gTotalPageCount.iDiscardablePages)); 
       
  8981 	delete buf;
       
  8982 	TESTDEBUG(test.Printf(_L("Cache filled\n")));
       
  8983 	
       
  8984 #if defined(_DEBUG) || defined(_DEBUG_RELEASE)
       
  8985 	// get number of items on Page Cache
       
  8986 	TFileCacheStats startPageCacheStats;
       
  8987 
       
  8988 	r = controlIo(gTheFs,gDrive, KControlIoFileCacheStats, startPageCacheStats);
       
  8989 	test.Printf(_L("control stats r= %d\n"), r);
       
  8990 	test(r==KErrNone || r == KErrNotSupported);
       
  8991 	
       
  8992 	TESTDEBUG(test.Printf(_L("Allocated segment count=%d\n"),startPageCacheStats.iAllocatedSegmentCount));
       
  8993 #endif
       
  8994 	// if we do not have any discardable pages then something went
       
  8995 	// wrong with file caching
       
  8996 	if (gTotalPageCount.iDiscardablePages == 0)
       
  8997 		return KErrNotSupported;
       
  8998 		
       
  8999 	return KErrNone;
       
  9000 	}
       
  9001 	
       
  9002 
       
  9003 
       
  9004 //
       
  9005 // SetUpMMC
       
  9006 //
       
  9007 // Sets up the MMC to be used by the test by creating files on the MMC
       
  9008 //
       
  9009 void SetUpMMC()
       
  9010 	{
       
  9011 	FormatMMC();
       
  9012 
       
  9013 	TInt r = 0;
       
  9014 
       
  9015 	TChar driveToTest;	
       
  9016 	
       
  9017 	TVolumeInfo volInfo;
       
  9018 	
       
  9019 	r = gTheFs.DriveToChar(gDrive, driveToTest);
       
  9020 	test_KErrNone(r);
       
  9021 					
       
  9022 	r = gTheFs.CharToDrive(driveToTest,gDrive);
       
  9023 	test_KErrNone(r);
       
  9024 
       
  9025 	gSessionPath = _L("?:\\F32-TST\\");
       
  9026 	gSessionPath[0] = (TUint16) driveToTest;
       
  9027 	test.Printf(_L("Drive Letter=%C\n"),(TInt)driveToTest);
       
  9028 
       
  9029 	TDriveInfo info;
       
  9030 	r = gTheFs.Drive(info,gDrive);
       
  9031 	test_KErrNone(r);
       
  9032 	r = gTheFs.SetSessionPath(gSessionPath);
       
  9033 	test_KErrNone(r);
       
  9034 	r = gTheFs.MkDirAll(gSessionPath);
       
  9035 	if (r != KErrNone && r != KErrAlreadyExists)
       
  9036 		{
       
  9037 		test_KErrNone(r);
       
  9038 		}
       
  9039 	
       
  9040 	r = gTheFs.Volume(volInfo, gDrive);
       
  9041 	test_KErrNone(r);
       
  9042 	TInt64 gMediaSize = volInfo.iSize;
       
  9043 
       
  9044 	// This calculation is approximate because the client cannot know
       
  9045 	// internal allocation mechanisms of the filesystem, i.e. how much
       
  9046 	// metadata is associated with a file of name X / size Y, whether
       
  9047 	// space used by such metadata is reflected in TDriveInfo::iSize and
       
  9048 	// what block/clustersize the filesystem will round filesizes to.
       
  9049 	// The last file that fills up the drive may therefore be partial
       
  9050 	// (smaller than this calculation predicts).
       
  9051 	TInt maxPossibleFiles = gFilesNeededToFillCache;
       
  9052 	test.Printf(_L("Original files needed = %d\n"), maxPossibleFiles);
       
  9053 	if(gMediaSize < (KDefaultCacheSize * maxPossibleFiles))
       
  9054 		{
       
  9055 		maxPossibleFiles = (gMediaSize - 10) / KDefaultCacheSize;
       
  9056 		test.Printf(_L("Disk size is smaller - files needed = %d\n"), maxPossibleFiles);
       
  9057 		}
       
  9058 	gFilesNeededToFillCache = maxPossibleFiles;
       
  9059 	CreateFiles(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9060 	}
       
  9061 
       
  9062 
       
  9063 //
       
  9064 // TestFileCaching
       
  9065 //
       
  9066 //---------------------------------------------------------------------------------------------------------------------
       
  9067 //! @SYMTestCaseID				KBASE-t_ramdefrag-0599
       
  9068 //! @SYMTestType				CIT
       
  9069 //! @SYMTestCaseDesc			Verifying that when File System Caching allocates dicardable pages, 
       
  9070 //! 							Defrag and allocation of fixed pages happens correctly. 
       
  9071 //! @SYMPREQ					PREQ308
       
  9072 //! @SYMTestPriority			High
       
  9073 //! @SYMTestActions				
       
  9074 //! 	1.	Fill the file system cache to allocate discardable pages
       
  9075 //! 		following this call EmptyRamZone() in a zone with discardable pages. 
       
  9076 //! 	2.	Fill the file system cache to allocate discardable pages
       
  9077 //! 		folling this allocate discontiguous fixed pages to a zone
       
  9078 //! 	3.	Fill the file system cache to allocate discardable pages
       
  9079 //! 	.	following this allocate discontuguous fixed pages
       
  9080 //! 	4.	Fill the file system cache to allocate discardable pages
       
  9081 //! 		following this allocate less than 16 contiguous fixed pages
       
  9082 //! 	5.	Fill the file system cache to allocate discardable pages
       
  9083 //! 		following this allocate more than 16 contiguous fixed pages
       
  9084 //!
       
  9085 //! @SYMTestExpectedResults
       
  9086 //! 	1.	Discardable pages are removed
       
  9087 //! 	2.	KErrNone
       
  9088 //! 	3.	KErrNone
       
  9089 //! 	4.	KErrNone and numDiscardablePages != 0
       
  9090 //! 	5.	KErrNone and numDiscardablePages = 0
       
  9091 //---------------------------------------------------------------------------------------------------------------------
       
  9092 TInt TestFileCaching()
       
  9093 	{
       
  9094 	const TUint KDisPagesReq = 1;
       
  9095 	TInt r = KErrNone;
       
  9096 	TInt allocSize = 0;
       
  9097 	TUint zoneID = 0;
       
  9098 	TUint index = 0;
       
  9099 	TUint minDiscardPages = 0;
       
  9100 	if (gDrive == KNoRemovableDrive)
       
  9101 		{
       
  9102 		test.Start(_L("Cannot find drive to write files to - Skipping FS Caching Tests\n"));
       
  9103 		test.End();
       
  9104 		return 0;
       
  9105 		}
       
  9106 	
       
  9107 	r = gTheFs.Connect();
       
  9108 	test_KErrNone(r);
       
  9109 
       
  9110 	SetUpMMC();
       
  9111 	
       
  9112 	RRamDefragFuncTestLdd Ldd2;
       
  9113 	r = Ldd2.Open();
       
  9114 	test_KErrNone(r);
       
  9115 	
       
  9116 	test.Start(_L("Test1: Test EmptyRamZone() clears file server cache pages"));	
       
  9117 	TestStart();	
       
  9118 	
       
  9119 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9120 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9121 
       
  9122 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9123 	if (r != KErrNone)
       
  9124 		{
       
  9125 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
       
  9126 		goto skipFileCacheTests;
       
  9127 		}
       
  9128 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  9129 	
       
  9130 	GetAllPageInfo();
       
  9131 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
       
  9132 		{
       
  9133 		++ index;
       
  9134 		}
       
  9135 	
       
  9136 	if (index == gZoneCount)  
       
  9137 		{
       
  9138 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
       
  9139 		}
       
  9140 	else
       
  9141 		{
       
  9142 		zoneID = gZoneConfigArray[index].iZoneId;
       
  9143 
       
  9144 		r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
       
  9145 		
       
  9146 		GetAllPageInfo();
       
  9147 		if (gZoneUtilArray[index].iAllocDiscardable != 0)
       
  9148 			{
       
  9149 			test.Printf(_L("Fail: Zone ID 0x%x has 0x%x discardable pages\n"), zoneID, gZoneUtilArray[index].iAllocDiscardable);
       
  9150 			TEST_FAIL;
       
  9151 			}
       
  9152 		else
       
  9153 			{
       
  9154 			test.Printf(_L("Passed...\n"));
       
  9155 			}
       
  9156 		}
       
  9157 	TestEnd();
       
  9158 
       
  9159 	test.Next(_L("Test2: Filling the FS Cache and allocating fixed pages to a zone"));	
       
  9160 	TestStart();
       
  9161 	
       
  9162 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9163 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9164 
       
  9165 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9166 	if (r != KErrNone)
       
  9167 		{
       
  9168 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
       
  9169 		goto skipFileCacheTests;
       
  9170 		}
       
  9171 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  9172 
       
  9173 	GetAllPageInfo();	
       
  9174 	index = 0;
       
  9175 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
       
  9176 		{
       
  9177 		++ index;
       
  9178 		} 
       
  9179 
       
  9180 	if (index == gZoneCount)  
       
  9181 		{
       
  9182 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
       
  9183 		}
       
  9184 	else
       
  9185 		{
       
  9186 		zoneID = gZoneConfigArray[index].iZoneId;
       
  9187 
       
  9188 		// Just need to attempt to allocate one more page than there is free in the zone
       
  9189 		allocSize = gZoneUtilArray[index].iFreePages + 1;
       
  9190 		
       
  9191 		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
       
  9192 		r = Ldd.ZoneAllocDiscontiguous(zoneID, allocSize);
       
  9193 		test.Printf(_L("r = %d\n"), r);
       
  9194 
       
  9195 		GetAllPageInfo();
       
  9196 		if (r != KErrNone)
       
  9197 			{
       
  9198 			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
       
  9199 			TEST_FAIL;
       
  9200 			}
       
  9201 		else
       
  9202 			{
       
  9203 			test.Printf(_L("Passed...\n"));
       
  9204 			}
       
  9205 		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
       
  9206 		Ldd.FreeAllFixedPages();
       
  9207 		}	
       
  9208 	TestEnd();
       
  9209 
       
  9210 
       
  9211 	test.Next(_L("Test3: Filling the FS Cache and allocating fixed pages"));	
       
  9212 	TestStart();
       
  9213 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9214 	if (r != KErrNone)
       
  9215 		{
       
  9216 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
       
  9217 		goto skipFileCacheTests;
       
  9218 		}
       
  9219 
       
  9220 	GetAllPageInfo();	
       
  9221 	index = 0;
       
  9222 	while (index < gZoneCount && gZoneUtilArray[index].iAllocDiscardable < KDisPagesReq)
       
  9223 		{
       
  9224 		++ index;
       
  9225 		}
       
  9226 		
       
  9227 	if (index == gZoneCount)  
       
  9228 		{
       
  9229 		test.Printf(_L("Cannot find zone to perform test on - Skipping test step...\n"));
       
  9230 		}
       
  9231 	else
       
  9232 		{ 
       
  9233 		zoneID = gZoneConfigArray[index].iZoneId;
       
  9234 
       
  9235 		allocSize = 14; 
       
  9236 
       
  9237 		TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
       
  9238 		Ldd.AllocateFixed(gTotalPageCount.iFreePages);
       
  9239 		
       
  9240 		test.Printf(_L("Allocating 0x%x fixed pages to zone ID 0x%x.....\n"), allocSize, zoneID);
       
  9241 		r = Ldd2.AllocateFixed(allocSize);
       
  9242 		
       
  9243 		TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  9244 		
       
  9245 		GetAllPageInfo();
       
  9246 		if (r != KErrNone)
       
  9247 			{
       
  9248 			test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
       
  9249 			CLEANUP(Ldd.FreeAllFixedPages());
       
  9250 			CLEANUP(Ldd2.FreeAllFixedPages());
       
  9251 			TEST_FAIL;
       
  9252 			}
       
  9253 		else
       
  9254 			{
       
  9255 			test.Printf(_L("Passed...\n"));
       
  9256 			}
       
  9257 		TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
       
  9258 		Ldd2.FreeAllFixedPages();
       
  9259 		}
       
  9260 	TestEnd();
       
  9261 
       
  9262 	test.Next(_L("Test4: Filling the FS Cache and allocating less than 16 contiguous fixed pages"));	
       
  9263 	TestStart();
       
  9264 	
       
  9265 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);	
       
  9266 	if (r != KErrNone)
       
  9267 		{
       
  9268 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
       
  9269 		goto skipFileCacheTests;
       
  9270 		}
       
  9271 	allocSize = 14 << gPageShift; 
       
  9272 	
       
  9273 	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
       
  9274 	GetAllPageInfo();
       
  9275 	// Allocate the fixed array before getting any page counts
       
  9276 	for (TUint index = 0; index < gZoneCount; index++)
       
  9277 		{
       
  9278 		GetAllPageInfo();
       
  9279 		if (gZoneUtilArray[index].iFreePages)
       
  9280 			{
       
  9281 			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
       
  9282 			if (r != KErrNone)
       
  9283 				{
       
  9284 				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
       
  9285 				goto SkipTest4;
       
  9286 				}
       
  9287 			}
       
  9288 		}
       
  9289 
       
  9290 	// Now fill all zones with fixed pages, 1 zone at a time
       
  9291 	// to avoid the discardable pages being disturbed
       
  9292 	for (TUint index = 0; index < gZoneCount; index++)
       
  9293 		{
       
  9294 		GetAllPageInfo();
       
  9295 		if (gZoneUtilArray[index].iFreePages)
       
  9296 			{
       
  9297 			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
       
  9298 			if (r != KErrNone)
       
  9299 				{
       
  9300 				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
       
  9301 													gZoneUtilArray[index].iFreePages, index,  r);
       
  9302 				goto SkipTest4;
       
  9303 				}
       
  9304 			}
       
  9305 		}
       
  9306 	
       
  9307 	GetAllPageInfo();
       
  9308 	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
       
  9309 	if (gTotalPageCount.iFreePages || 
       
  9310 		gTotalPageCount.iDiscardablePages <= (TUint)(allocSize >> gPageShift))
       
  9311 		{
       
  9312 		test.Printf(_L("Setup failed - Skipping...\n"));
       
  9313 		goto SkipTest4;
       
  9314 		}
       
  9315 
       
  9316 	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
       
  9317 	r = Ldd2.AllocContiguous(allocSize);
       
  9318 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  9319 	
       
  9320 	GetAllPageInfo();
       
  9321 	if (r != KErrNone || !gTotalPageCount.iDiscardablePages)
       
  9322 		{
       
  9323 		test.Printf(_L("Fail: Fixed pages have not been allocated, r = %d, expected = %d\n"), r, KErrNone);
       
  9324 		CLEANUP(Ldd.FreeAllFixedPages());
       
  9325 		CLEANUP(Ldd2.FreeAllFixedPages());
       
  9326 		TEST_FAIL;
       
  9327 		}
       
  9328 	else
       
  9329 		{
       
  9330 		test.Printf(_L("Passed...\n"));
       
  9331 		}
       
  9332 
       
  9333 SkipTest4:
       
  9334 	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
       
  9335 	Ldd2.FreeAllFixedPages();
       
  9336 	TestEnd();
       
  9337 
       
  9338 	test.Next(_L("Test5: Filling the FS Cache and allocating more than 16 contiguous fixed pages"));	
       
  9339 	TestStart();
       
  9340 	// TestEnd() will have reduced any cache pages to minimum so just get current 
       
  9341 	// count of discardable pages.
       
  9342 	GetAllPageInfo();
       
  9343 	minDiscardPages = gTotalPageCount.iDiscardablePages;
       
  9344 	
       
  9345 	r = FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9346 	if (r != KErrNone)
       
  9347 		{
       
  9348 		test.Printf(_L("File system caching failed - Skipping all file caching tests...\n"));
       
  9349 		goto skipFileCacheTests;
       
  9350 		}
       
  9351 	
       
  9352 	allocSize = 18 << gPageShift; 
       
  9353 
       
  9354 	TESTDEBUG(test.Printf(_L("Filling the remaining free pages with fixed pages.....\n")));
       
  9355 	GetAllPageInfo();
       
  9356 	
       
  9357 	// Allocate the fixed array before getting any page counts
       
  9358 	for (TUint index = 0; index < gZoneCount; index++)
       
  9359 		{
       
  9360 		GetAllPageInfo();
       
  9361 		if (gZoneUtilArray[index].iFreePages)
       
  9362 			{
       
  9363 			r = Ldd.ZoneAllocToManyArray(index, gZoneUtilArray[index].iFreePages);
       
  9364 			if (r != KErrNone)
       
  9365 				{
       
  9366 				test.Printf(_L("Failed to allocate fixed array Zone %d r = %d - Skipping...\n"), index,  r);
       
  9367 				goto SkipTest5;
       
  9368 				}
       
  9369 			}
       
  9370 		}
       
  9371 
       
  9372 	// Now fill all zones with fixed pages, 1 zone at a time
       
  9373 	// to avoid the discardable pages being disturbed
       
  9374 	for (TUint index = 0; index < gZoneCount; index++)
       
  9375 		{
       
  9376 		GetAllPageInfo();
       
  9377 		if (gZoneUtilArray[index].iFreePages)
       
  9378 			{
       
  9379 			r = Ldd.ZoneAllocToMany2(index, gZoneUtilArray[index].iFreePages);
       
  9380 			if (r != KErrNone)
       
  9381 				{
       
  9382 				test.Printf(_L("Failed to allocate %d fixed to Zone %d r = %d - Skipping...\n"), 
       
  9383 													gZoneUtilArray[index].iFreePages, index,  r);
       
  9384 				goto SkipTest5;
       
  9385 				}
       
  9386 			}
       
  9387 		}
       
  9388 	
       
  9389 	GetAllPageInfo();
       
  9390 	test.Printf(_L("number of free pages = 0x%x\n"), gTotalPageCount.iFreePages);
       
  9391 	if (gTotalPageCount.iFreePages)
       
  9392 		{
       
  9393 		test.Printf(_L("Setup failed - Skipping...\n"));
       
  9394 		goto SkipTest5;
       
  9395 		}
       
  9396 	
       
  9397 	test.Printf(_L("Allocating 0x%x fixed pages.....\n"), allocSize >> gPageShift);
       
  9398 	r = Ldd2.AllocContiguous(allocSize);
       
  9399 	TESTDEBUG(test.Printf(_L("r = %d\n"), r));
       
  9400 
       
  9401 	GetAllPageInfo();
       
  9402 	if (r != KErrNone || gTotalPageCount.iDiscardablePages != minDiscardPages)
       
  9403 		{
       
  9404 		test.Printf(_L("Fail: r = %d, expected = %d - Discardable Pages = 0x%x, expected = %d\n"), 
       
  9405 								r, KErrNone, gTotalPageCount.iDiscardablePages, minDiscardPages);
       
  9406 		CLEANUP(Ldd.FreeAllFixedPages());
       
  9407 		CLEANUP(Ldd2.FreeAllFixedPages());
       
  9408 		TEST_FAIL;
       
  9409 		}
       
  9410 	else
       
  9411 		{
       
  9412 		test.Printf(_L("Passed...\n"));
       
  9413 		}
       
  9414 SkipTest5:
       
  9415 
       
  9416 skipFileCacheTests:
       
  9417 	TestEnd();
       
  9418 	TESTDEBUG(test.Printf(_L("Freeing all Fixed Pages.....\n")));
       
  9419 	Ldd2.FreeAllFixedPages();
       
  9420 	Ldd2.Close();
       
  9421 	gTheFs.Close();
       
  9422 	FormatMMC();
       
  9423 	test.End();
       
  9424 	return KErrNone;
       
  9425 
       
  9426 	}
       
  9427 
       
  9428 
       
  9429 //
       
  9430 // TestOneZoneConfig
       
  9431 //
       
  9432 //---------------------------------------------------------------------------------------------------------------------
       
  9433 //! @SYMTestCaseID				KBASE-t_ramdefrag-0600
       
  9434 //! @SYMTestType				CIT
       
  9435 //! @SYMTestCaseDesc			Verifying that when only 1 zone is cofigured in the variant, that
       
  9436 //!								the defrag and allocation of fixed pages happend correctly
       
  9437 //! @SYMPREQ					PREQ308
       
  9438 //! @SYMTestPriority			High
       
  9439 //! @SYMTestActions				
       
  9440 //! 	1.	Fragment the memory and perform a DefragRam
       
  9441 //! 	2.	Fragment the memory and perform an EmptyZone
       
  9442 //! 	3.	Fragment the memory and perform a ClaimZone
       
  9443 //! 	4.	Call ZoneAllocPhysicalRam to allocate discontiguous fixed pages
       
  9444 //! 	5.	Call ZoneAllocPhysicalRam to allocate contiguous fixed pages
       
  9445 //!
       
  9446 //! @SYMTestExpectedResults
       
  9447 //! 	1.	Number of pages does not differ from the original
       
  9448 //! 	2.	KErrNoMemroy and discardable pages are discarded of
       
  9449 //! 	3.	KErrNoMemory
       
  9450 //! 	4.	KErrNone
       
  9451 //! 	5.	KErrNone
       
  9452 //---------------------------------------------------------------------------------------------------------------------
       
  9453 TInt TestOneZoneConfig()
       
  9454 	{
       
  9455 	TInt r = gTheFs.Connect();
       
  9456 	test_KErrNone(r);
       
  9457 	
       
  9458 	if (gDrive != KNoRemovableDrive)
       
  9459 		{	
       
  9460 		SetUpMMC();
       
  9461 		}
       
  9462 
       
  9463 	TUint index = 0;
       
  9464 	GetAllPageInfo();
       
  9465 	TUint zoneID = gZoneConfigArray[index].iZoneId;
       
  9466 	test.Printf(_L("Zone ID = 0x%x\n"), zoneID);
       
  9467 	TUint minCacheSize = 0;
       
  9468 	TUint maxCacheSize = 0;
       
  9469 	TUint currentCacheSize = 0;
       
  9470 	
       
  9471 
       
  9472 	TUint origFree = 0;
       
  9473 	TUint origUnknown = 0;
       
  9474 	TUint origFixed = 0;
       
  9475 	TUint origMovable = 0;
       
  9476 	TUint origDiscard = 0;
       
  9477 	TUint origOther = 0;
       
  9478 
       
  9479 	if (gPagedRom)
       
  9480 		{
       
  9481 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  9482 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  9483 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  9484 
       
  9485 		TUint setMin = 60 << gPageShift;
       
  9486 		TInt setMax = -1;
       
  9487 		TInt r = DPTest::SetCacheSize(setMin, setMax);
       
  9488 		if (r != KErrNone)
       
  9489 			{
       
  9490 			test.Printf(_L("r = %d, expected = %d\n"), r, KErrNone);
       
  9491 			CLEANUP(ResetDPCache());
       
  9492 			TEST_FAIL;
       
  9493 			}
       
  9494 		DPTest::CacheSize(minCacheSize,maxCacheSize,currentCacheSize);
       
  9495 		TESTDEBUG(test.Printf(_L("Original CacheSize: minCacheSize = 0x%x, maxCacheSize = 0x%x, currentCacheSize = 0x%x\n"), 
       
  9496 									minCacheSize >> gPageShift, maxCacheSize >> gPageShift, currentCacheSize >> gPageShift));
       
  9497 		}
       
  9498 
       
  9499 	test.Start(_L("Test1: Fragmenting the memory and performing a general defrag"));	
       
  9500 	TestStart();	
       
  9501 	
       
  9502 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9503 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9504 	
       
  9505 	if (gDrive != KNoRemovableDrive)
       
  9506 		{
       
  9507 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9508 		}
       
  9509 	else
       
  9510 		{
       
  9511 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
       
  9512 		}
       
  9513 
       
  9514 	GetAllPageInfo();
       
  9515 	origFree = gTotalPageCount.iFreePages;
       
  9516 	origUnknown = gTotalPageCount.iUnknownPages;
       
  9517 	origFixed = gTotalPageCount.iFixedPages;
       
  9518 	origMovable = gTotalPageCount.iMovablePages;
       
  9519 	origDiscard = gTotalPageCount.iDiscardablePages;
       
  9520 	origOther = gTotalPageCount.iOtherPages;
       
  9521 	
       
  9522 	r = Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  9523 	
       
  9524 	GetAllPageInfo();
       
  9525 
       
  9526 	if ((origUnknown != gTotalPageCount.iUnknownPages) ||
       
  9527 		(origFixed != gTotalPageCount.iFixedPages) ||
       
  9528 		(origMovable != gTotalPageCount.iMovablePages) ||
       
  9529 		(origDiscard != gTotalPageCount.iDiscardablePages) ||
       
  9530 		(origOther != gTotalPageCount.iOtherPages))
       
  9531 		{
       
  9532 		test.Printf(_L("Fail: Pages after defrag are not equal to those before"));
       
  9533 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  9534 		TEST_FAIL;
       
  9535 		}
       
  9536 	else
       
  9537 		{
       
  9538 		test.Printf(_L("Passed..."));
       
  9539 		}
       
  9540 	// This will free any allocated memory
       
  9541 	TestEnd();
       
  9542 
       
  9543 
       
  9544 	test.Next(_L("Test2: Fragmenting the memory and performing an EmptyZone"));	
       
  9545 	TestStart();	
       
  9546 	
       
  9547 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9548 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9549 	
       
  9550 	if (gDrive != KNoRemovableDrive)
       
  9551 		{
       
  9552 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9553 		}
       
  9554 	else
       
  9555 		{
       
  9556 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
       
  9557 		}	
       
  9558 	r = Ldd.CallDefrag(DEFRAG_TYPE_EMPTY, DEFRAG_VER_SYNC, zoneID);
       
  9559 
       
  9560 	if (r != KErrNoMemory || CheckZoneIsOff(index))
       
  9561 		{
       
  9562 		test.Printf(_L("Fail: r = %d, expected = -4"), r);
       
  9563 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  9564 		TEST_FAIL;
       
  9565 		}
       
  9566 	else
       
  9567 		{
       
  9568 		test.Printf(_L("Passed..."));
       
  9569 		}
       
  9570 	// This will free any allocated memory
       
  9571 	TestEnd();
       
  9572 
       
  9573 	test.Next(_L("Test3: Fragmenting the memory and performing a ClaimZone"));	
       
  9574 	TestStart();	
       
  9575 	
       
  9576 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9577 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9578 
       
  9579 	if (gDrive != KNoRemovableDrive)
       
  9580 		{
       
  9581 		FillCache(gFilesNeededToFillCache, KDefaultCacheSize);
       
  9582 		}
       
  9583 	else
       
  9584 		{
       
  9585 		test.Printf(_L("Cannot find drive to write files to - Not allocating discardable pages through FS Caching\n"));
       
  9586 		}
       
  9587 	
       
  9588 	GetAllPageInfo();
       
  9589 	origMovable = gTotalPageCount.iMovablePages;
       
  9590 		
       
  9591 	r = Ldd.CallDefrag(DEFRAG_TYPE_CLAIM, DEFRAG_VER_SYNC, zoneID);
       
  9592 
       
  9593 	GetAllPageInfo();
       
  9594 	if (r != KErrNoMemory || origMovable != gTotalPageCount.iMovablePages)
       
  9595 		{
       
  9596 		test.Printf(_L("Fail: r = %d, expected = -4"), r);
       
  9597 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  9598 		TEST_FAIL;
       
  9599 		}
       
  9600 	else
       
  9601 		{
       
  9602 		test.Printf(_L("Passed..."));
       
  9603 		}
       
  9604 	// This will free any allocated memory.
       
  9605 	TestEnd();
       
  9606 
       
  9607 	test.Next(_L("Test4: Calling ZoneAllocPhysicalRam to allocate discontiguous fixed pages"));	
       
  9608 	TestStart();	
       
  9609 	
       
  9610 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9611 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9612 	
       
  9613 	GetAllPageInfo();	
       
  9614 	origFree = gTotalPageCount.iFreePages;
       
  9615 	origUnknown = gTotalPageCount.iUnknownPages;
       
  9616 	origFixed = gTotalPageCount.iFixedPages;
       
  9617 	origMovable = gTotalPageCount.iMovablePages;
       
  9618 	origDiscard = gTotalPageCount.iDiscardablePages;
       
  9619 	origOther = gTotalPageCount.iOtherPages;
       
  9620 	
       
  9621 	r = Ldd.ZoneAllocDiscontiguous(zoneID, (TInt)(origFree / 2));
       
  9622 
       
  9623 	GetAllPageInfo();
       
  9624 	if (gTotalPageCount.iFixedPages < (origFixed + (origFree / 2)))
       
  9625 		{
       
  9626 		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
       
  9627 						gTotalPageCount.iFixedPages, (origFixed + (origFree / 2)));
       
  9628 		CLEANUP(Ldd.FreeAllFixedPages());
       
  9629 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  9630 		TEST_FAIL;
       
  9631 		}
       
  9632 	else
       
  9633 		{
       
  9634 		test.Printf(_L("Passed..."));
       
  9635 		}
       
  9636 	Ldd.FreeAllFixedPages();
       
  9637 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  9638 	TestEnd();
       
  9639 
       
  9640 
       
  9641 	test.Next(_L("Test5: Calling ZoneAllocPhysicalRam to allocate contiguous fixed pages"));	
       
  9642 	TestStart();	
       
  9643 	
       
  9644 	AllocMovable(gChunkArray1, gChunkArraySize1, KNumAllocChunks);
       
  9645 	FreeMovable(gChunkArray1, gChunkArraySize1);
       
  9646 
       
  9647 	GetAllPageInfo();
       
  9648 	origFree = gTotalPageCount.iFreePages;
       
  9649 	origUnknown = gTotalPageCount.iUnknownPages;
       
  9650 	origFixed = gTotalPageCount.iFixedPages;
       
  9651 	origMovable = gTotalPageCount.iMovablePages;
       
  9652 	origDiscard = gTotalPageCount.iDiscardablePages;
       
  9653 	origOther = gTotalPageCount.iOtherPages;
       
  9654 	
       
  9655 	TInt allocSize = 50 << gPageShift; 
       
  9656 	
       
  9657 	r = Ldd.ZoneAllocContiguous(zoneID, allocSize);
       
  9658 
       
  9659 	GetAllPageInfo();
       
  9660 	if (gTotalPageCount.iFixedPages < (origFixed + (allocSize >> gPageShift)))
       
  9661 		{
       
  9662 		test.Printf(_L("Fail: fixed pages = 0x%x, expected >= 0x%x\n"), 
       
  9663 							gTotalPageCount.iFixedPages, (origFixed + (allocSize >> gPageShift)));
       
  9664 		CLEANUP(Ldd.FreeAllFixedPages());
       
  9665 		CLEANUP(RemoveChunkAlloc(gChunkArray1, gChunkArraySize1));
       
  9666 		TEST_FAIL;
       
  9667 		}
       
  9668 	else
       
  9669 		{
       
  9670 		test.Printf(_L("Passed..."));
       
  9671 		}
       
  9672 	Ldd.FreeAllFixedPages();
       
  9673 	RemoveChunkAlloc(gChunkArray1, gChunkArraySize1);
       
  9674 	TestEnd();
       
  9675 
       
  9676 	if (gPagedRom)
       
  9677 		{
       
  9678 		test_KErrNone(DPTest::FlushCache());
       
  9679 		ResetDPCache();
       
  9680 		}
       
  9681 
       
  9682 	gTheFs.Close();
       
  9683 	if (gDrive != KNoRemovableDrive)
       
  9684 		{
       
  9685 		FormatMMC();
       
  9686 		}
       
  9687 	
       
  9688 	test.End();
       
  9689 	return KErrNone;
       
  9690 	}
       
  9691 
       
  9692 
       
  9693 //
       
  9694 // RunDefragTests
       
  9695 //
       
  9696 // List of defrag tests to be run 
       
  9697 //
       
  9698 void RunDefragTests()
       
  9699 	{
       
  9700 	test.Start(_L("Testing the moving of pages in a defrag"));
       
  9701 	TestMovPgsDefrag();
       
  9702 
       
  9703 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::DefragRam() arg aMaxPages"));
       
  9704 	TestDefragRamMaxPages();
       
  9705 
       
  9706 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::EmptyRamZone()"));
       
  9707 	TestEmptyRamZone();
       
  9708 
       
  9709 	test.Next(_L("Verifying the implementation of the function Epoc::GetRamZonePageCount()"));
       
  9710 	TestGetRamZonePageCount();
       
  9711 
       
  9712 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::ClaimRamZone()"));
       
  9713 	TestClaimRamZone();
       
  9714 
       
  9715 	test.Next(_L("Verifying the implementation of the function TRamDefragRequest::Cancel()"));
       
  9716 	TestCancelDefrag();
       
  9717 
       
  9718 	test.Next(_L("Verifying that pages are moved correctly"));
       
  9719 	TestMovingPages();
       
  9720 
       
  9721 	test.Next(_L("Verifying Semaphore Methods of the Defrag"));
       
  9722 	TestDefragSemMethod();
       
  9723 
       
  9724 	test.Next(_L("Verifying Dfc Methods of the Defrag"));
       
  9725 	TestDefragDfcMethod();
       
  9726 
       
  9727 	test.Next(_L("Testing priorities"));
       
  9728 	TestPriorities();
       
  9729 
       
  9730 	test.Next(_L("Testing File System Caching"));
       
  9731 	if (!gPagedRom)
       
  9732 		{
       
  9733 		TestFileCaching();
       
  9734 		}
       
  9735 	else
       
  9736 		{
       
  9737 		test.Printf(_L("Skipping... \n"));
       
  9738 		}
       
  9739 	
       
  9740 	test.Next(_L("Testing general RAM defrag implementation"));
       
  9741 	TestGenDefrag();
       
  9742 
       
  9743 	test.End();
       
  9744 	}
       
  9745 
       
  9746 
       
  9747 //
       
  9748 // RunAllocTests
       
  9749 //
       
  9750 // List of allocating tests to be run 
       
  9751 // These tests only need to be executed once
       
  9752 //
       
  9753 void RunAllocTests()
       
  9754 	{
       
  9755 	test.Start(_L("Verifying the allocating strategies"));
       
  9756 	TestAllocStrategies();
       
  9757 
       
  9758 	test.Next(_L("Verifying the contiguous overload of Epoc::ZoneAllocPhysicalRam()"));
       
  9759 	TestZoneAllocContiguous();
       
  9760 
       
  9761 	test.Next(_L("Verifying the discontiguous overload of Epoc::ZoneAllocPhysicalRam()"));
       
  9762 	TestZoneAllocDiscontiguous();
       
  9763 	
       
  9764 	test.Next(_L("Test Free Zone"));
       
  9765 	TestFreeZone();
       
  9766 
       
  9767 	test.Next(_L("Testing zone flags"));
       
  9768 	TestFlags();
       
  9769 
       
  9770 	test.End();
       
  9771 	}
       
  9772 
       
  9773 
       
  9774 //
       
  9775 // E32Main
       
  9776 //
       
  9777 // Main entry point.
       
  9778 //
       
  9779 TInt E32Main()
       
  9780 	{
       
  9781 	test.Title();
       
  9782 	DeviceDriver(TEST_DRIVER_OPEN);
       
  9783 	gTotalRamLost = 0;
       
  9784 	
       
  9785 	TInt r = TestSetup();
       
  9786 	if (r != KErrNone)
       
  9787 		{
       
  9788 		test.Printf(_L("Test Setup failed, r = %d\n"), r);
       
  9789 		TestCleanup();
       
  9790 		return r;
       
  9791 		}
       
  9792 
       
  9793 	if (gZoneCount == 1)
       
  9794 		{
       
  9795 		GetAllPageInfo();
       
  9796 
       
  9797 		test.Start(_L("Zone Count 1..."));
       
  9798 		TestOneZoneConfig();
       
  9799 		}
       
  9800 	else
       
  9801 		{
       
  9802 		test.Start(_L("Running Alloc tests"));
       
  9803 		RunAllocTests();
       
  9804 
       
  9805 		Ldd.ResetDriver();
       
  9806 		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  9807 		
       
  9808 
       
  9809 		test.Next(_L("Running Defrag tests"));
       
  9810 		RunDefragTests();
       
  9811 
       
  9812 		Ldd.ResetDriver();
       
  9813 		Ldd.CallDefrag(DEFRAG_TYPE_GEN, DEFRAG_VER_SYNC);
       
  9814 		}
       
  9815 	
       
  9816 
       
  9817 	test.Printf(_L("The total number of test steps failed = %d\n"), gTestStepFailed);
       
  9818 	test_Equal(KErrNone, gTestStepFailed);
       
  9819 	
       
  9820 	TestCleanup();
       
  9821 	
       
  9822 	DeviceDriver(TEST_DRIVER_CLOSE);
       
  9823 	test.End();
       
  9824 	test.Close();
       
  9825 	
       
  9826 	return 0;
       
  9827 	}