os/graphics/windowing/windowserver/test/t_stress/src/utils.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
//
sl@0
    15
sl@0
    16
/**
sl@0
    17
 @file
sl@0
    18
 @test
sl@0
    19
 @internalComponent
sl@0
    20
*/
sl@0
    21
  
sl@0
    22
sl@0
    23
#include "utils.h"
sl@0
    24
#include "panic.h"
sl@0
    25
sl@0
    26
/*************************************************************************************
sl@0
    27
 class TRnd
sl@0
    28
 */
sl@0
    29
sl@0
    30
/**
sl@0
    31
Math::Rand() degenerates to approx seed*3.3+38 for the first numbers generated 
sl@0
    32
with a low seed, which isn't very random. To illustrate: The random numbers generated 
sl@0
    33
by the seeds {0..9} are {38, 41, 45, 48, 51, 54, 58, 61, 64, 68}.
sl@0
    34
*/
sl@0
    35
const TInt64 KMinSeed = 1000000000;
sl@0
    36
sl@0
    37
TInt64 TRnd::iSeed = KMinSeed; // Need to change CComparison::StartL() to use this value
sl@0
    38
sl@0
    39
CExecutionContext::CLogMediator* TRnd::iMediator 			= NULL;
sl@0
    40
CExecutionContext* 				 TRnd::iExecutionContext	= NULL;
sl@0
    41
sl@0
    42
TInt64 TRnd::Seed()
sl@0
    43
	{
sl@0
    44
	return iSeed;
sl@0
    45
	}
sl@0
    46
sl@0
    47
void TRnd::SetSeed(TInt64 aSeed)
sl@0
    48
	{
sl@0
    49
	iSeed = aSeed;
sl@0
    50
	}
sl@0
    51
sl@0
    52
void TRnd::SetLogMediator(CExecutionContext::CLogMediator* aMediator)
sl@0
    53
	{
sl@0
    54
	iMediator = aMediator;
sl@0
    55
	}
sl@0
    56
sl@0
    57
void TRnd::SetExecutionContext(CExecutionContext* aExecutionContext)
sl@0
    58
	{
sl@0
    59
	iExecutionContext = aExecutionContext;
sl@0
    60
	}
sl@0
    61
sl@0
    62
sl@0
    63
TInt TRnd::GenRand(TInt x)
sl@0
    64
	{
sl@0
    65
	TInt res = Math::Rand( iSeed );
sl@0
    66
	if (x)
sl@0
    67
		{
sl@0
    68
		res = res % x;
sl@0
    69
		}
sl@0
    70
	return res;
sl@0
    71
	}
sl@0
    72
sl@0
    73
TInt TRnd::rnd(TInt x)
sl@0
    74
	{
sl@0
    75
	TInt res;
sl@0
    76
	
sl@0
    77
	switch ( iExecutionContext->ContextMode() )
sl@0
    78
	{
sl@0
    79
	case CExecutionContext::ECtxPlayback:	
sl@0
    80
		res = iMediator->PlaybackInt();
sl@0
    81
		break;
sl@0
    82
		
sl@0
    83
	case CExecutionContext::ECtxRandomAndRecord:
sl@0
    84
		res = GenRand(x);
sl@0
    85
		iMediator->RecordInt( res );
sl@0
    86
		break;
sl@0
    87
		
sl@0
    88
	case CExecutionContext::ECtxRandom:		// fallthrough
sl@0
    89
	default:
sl@0
    90
		res = GenRand(x);
sl@0
    91
		break;
sl@0
    92
	}
sl@0
    93
	
sl@0
    94
	return res; 
sl@0
    95
	}
sl@0
    96
sl@0
    97
/*************************************************************************************
sl@0
    98
 class CExecutionContext
sl@0
    99
 */
sl@0
   100
TInt CExecutionContext::CLogMediator::PlaybackInt()
sl@0
   101
	{
sl@0
   102
	
sl@0
   103
	TInt res = 0;
sl@0
   104
	
sl@0
   105
	if ( !iStalled )
sl@0
   106
		{
sl@0
   107
	
sl@0
   108
		TBool failed = ETrue;
sl@0
   109
		
sl@0
   110
		TBuf8<1024> logRec;
sl@0
   111
	
sl@0
   112
		TInt readRes;
sl@0
   113
		
sl@0
   114
		readRes = iLog.Read( logRec, 3 ); // 'Num'
sl@0
   115
	
sl@0
   116
		
sl@0
   117
		
sl@0
   118
		if ( readRes == KErrNone && logRec == _L8("Num") )
sl@0
   119
			{
sl@0
   120
			iLog.Read( logRec, 16 ); // EntryNo, 16 characters	
sl@0
   121
			TLex8 strLex;		
sl@0
   122
			strLex.Assign( logRec );		
sl@0
   123
			TInt64 readVal64 = 0;		
sl@0
   124
			readRes = strLex.Val( readVal64, EDecimal );		
sl@0
   125
			if ( KErrNone == readRes && readVal64 == iEntryNo )
sl@0
   126
				{
sl@0
   127
							
sl@0
   128
				readRes = iLog.Read( logRec, 3 ); // ' = '
sl@0
   129
				
sl@0
   130
				if ( readRes == KErrNone && logRec == _L8(" = ")  )
sl@0
   131
					{
sl@0
   132
	
sl@0
   133
					readRes = iLog.Read( logRec, 13 ); // value 12 + eol
sl@0
   134
					strLex.Assign( logRec );		
sl@0
   135
					
sl@0
   136
					TUint32 readVal32;
sl@0
   137
	
sl@0
   138
					readRes = strLex.Val( readVal32, EDecimal );		
sl@0
   139
					
sl@0
   140
					if ( KErrNone == readRes )
sl@0
   141
						{  // finally we got a number
sl@0
   142
						
sl@0
   143
						res = (TInt) readVal32;
sl@0
   144
						
sl@0
   145
						failed = EFalse;
sl@0
   146
						}
sl@0
   147
					
sl@0
   148
					}							
sl@0
   149
				}
sl@0
   150
			
sl@0
   151
			}
sl@0
   152
		
sl@0
   153
		if ( failed )
sl@0
   154
			{
sl@0
   155
			iExecutionContext.MediatorEmptied( this );
sl@0
   156
			res = 0;
sl@0
   157
			iStalled = ETrue;
sl@0
   158
			}
sl@0
   159
		
sl@0
   160
		}
sl@0
   161
	
sl@0
   162
	iEntryNo++;
sl@0
   163
	
sl@0
   164
	return res;
sl@0
   165
	}
sl@0
   166
sl@0
   167
void CExecutionContext::CLogMediator::RecordInt(TInt aIntToBeRecorded)
sl@0
   168
	{
sl@0
   169
	if ( !iStalled )
sl@0
   170
		{
sl@0
   171
		TBuf8<1024> logRec;
sl@0
   172
		logRec.Format( _L8("Num%016Ld = %012d\n"), iEntryNo, aIntToBeRecorded );
sl@0
   173
		iLog.Write( logRec );	
sl@0
   174
		}
sl@0
   175
	
sl@0
   176
	iEntryNo++;
sl@0
   177
	}
sl@0
   178
sl@0
   179
CExecutionContext::CLogMediator::CLogMediator(CExecutionContext&	aExecutionContext):
sl@0
   180
	iExecutionContext( aExecutionContext )
sl@0
   181
	{ }
sl@0
   182
sl@0
   183
CExecutionContext::CLogMediator::~CLogMediator()
sl@0
   184
	{
sl@0
   185
	iLog.Close();
sl@0
   186
	iExecutionContext.MediatorDestroyed ( this );
sl@0
   187
	}
sl@0
   188
sl@0
   189
void CExecutionContext::CLogMediator::ConstructL( RFs& aFs, const TDesC& aFileName )
sl@0
   190
	{
sl@0
   191
	TInt fileErr;
sl@0
   192
	
sl@0
   193
	switch ( iExecutionContext.ContextMode() )
sl@0
   194
	{
sl@0
   195
	case CExecutionContext::ECtxPlayback:
sl@0
   196
		fileErr = iLog.Open( aFs, aFileName, EFileRead );
sl@0
   197
		break;
sl@0
   198
	
sl@0
   199
	case CExecutionContext::ECtxRandomAndRecord:
sl@0
   200
		fileErr = iLog.Create( aFs, aFileName, EFileWrite );
sl@0
   201
		if(fileErr == KErrAlreadyExists)
sl@0
   202
			{
sl@0
   203
			fileErr = iLog.Replace( aFs, aFileName, EFileWrite );		
sl@0
   204
			}
sl@0
   205
		else if(fileErr == KErrPathNotFound)
sl@0
   206
			{
sl@0
   207
			User::LeaveIfError(aFs.CreatePrivatePath(EDriveC));
sl@0
   208
			fileErr = iLog.Create( aFs, aFileName, EFileWrite );
sl@0
   209
			}
sl@0
   210
		__ASSERT_ALWAYS(KErrNone == fileErr, User::Panic(_L("t_stress utils.cpp"), EPanic2));
sl@0
   211
		break;
sl@0
   212
	
sl@0
   213
	default:										
sl@0
   214
		fileErr = KErrGeneral;
sl@0
   215
		break;
sl@0
   216
	}
sl@0
   217
	
sl@0
   218
	User::LeaveIfError( fileErr );
sl@0
   219
	
sl@0
   220
	}
sl@0
   221
		
sl@0
   222
CExecutionContext::CLogMediator* CExecutionContext::CLogMediator::NewLC(CExecutionContext& aExecutionContext, RFs& aFs, const TDesC& aFileName )
sl@0
   223
	{
sl@0
   224
	CExecutionContext::CLogMediator* self = new (ELeave) CExecutionContext::CLogMediator( aExecutionContext );
sl@0
   225
	CleanupStack::PushL ( self );
sl@0
   226
	self->ConstructL( aFs, aFileName );
sl@0
   227
	return self; 
sl@0
   228
	}
sl@0
   229
		
sl@0
   230
CExecutionContext* CExecutionContext::NewL(CExecutionContext::TExecutionMode aExecutionMode, CTestExecWatchCat&	aWatchCat)
sl@0
   231
	{
sl@0
   232
	CExecutionContext* self = new (ELeave) CExecutionContext( aExecutionMode, aWatchCat );
sl@0
   233
	CleanupStack::PushL ( self );
sl@0
   234
	self->ConstructL( );
sl@0
   235
	CleanupStack::Pop( self );
sl@0
   236
	return self;
sl@0
   237
	}
sl@0
   238
	
sl@0
   239
CExecutionContext::TExecutionMode CExecutionContext::ContextMode()
sl@0
   240
	{
sl@0
   241
	return iContextMode;
sl@0
   242
	}
sl@0
   243
sl@0
   244
CExecutionContext::CLogMediator&	CExecutionContext::CreateLogMediatorL(const TDesC& aLogName)
sl@0
   245
	{
sl@0
   246
	CExecutionContext::CLogMediator* mediator = CExecutionContext::CLogMediator::NewLC( *this, iFs, aLogName );
sl@0
   247
	iMediators.AppendL( mediator );
sl@0
   248
	CleanupStack::Pop(mediator);
sl@0
   249
	return *mediator;
sl@0
   250
	}
sl@0
   251
sl@0
   252
void CExecutionContext::MediatorDestroyed(CLogMediator* aMediator)
sl@0
   253
	{
sl@0
   254
	TInt pos = iMediators.Find( aMediator );
sl@0
   255
	iMediators.Remove ( pos );
sl@0
   256
	}
sl@0
   257
sl@0
   258
void CExecutionContext::MediatorEmptied(CLogMediator* aMediator)
sl@0
   259
	{
sl@0
   260
	(void)aMediator;
sl@0
   261
	iWatchCat.ExecutionContextRunOut( this );
sl@0
   262
	}
sl@0
   263
sl@0
   264
sl@0
   265
void CExecutionContext::ConstructL()
sl@0
   266
	{
sl@0
   267
	TInt conResult = iFs.Connect();
sl@0
   268
	User::LeaveIfError( conResult );	
sl@0
   269
	}
sl@0
   270
	
sl@0
   271
sl@0
   272
CExecutionContext::CExecutionContext(CExecutionContext::TExecutionMode aExecutionMode, CTestExecWatchCat&	aWatchCat):
sl@0
   273
	iWatchCat(aWatchCat),
sl@0
   274
	iContextMode(aExecutionMode)
sl@0
   275
	{ } // empty
sl@0
   276
sl@0
   277
CExecutionContext::~CExecutionContext()
sl@0
   278
	{
sl@0
   279
	while ( iMediators.Count() > 0 )
sl@0
   280
		{
sl@0
   281
			delete iMediators[0]; // this will remove the mediator from the list
sl@0
   282
		}
sl@0
   283
	
sl@0
   284
	iMediators.Close();
sl@0
   285
	iFs.Close();
sl@0
   286
	}
sl@0
   287
sl@0
   288
/*************************************************************************************
sl@0
   289
 class CTestExecWatchCat
sl@0
   290
 */
sl@0
   291
CTestExecWatchCat* CTestExecWatchCat::NewL(CExecutionContext::TExecutionMode aExecutionMode)
sl@0
   292
	{
sl@0
   293
	CTestExecWatchCat* self = new (ELeave) CTestExecWatchCat();
sl@0
   294
	CleanupStack::PushL( self );	
sl@0
   295
	self->ConstructL( aExecutionMode );
sl@0
   296
	
sl@0
   297
	// finally hook into TRnd
sl@0
   298
	TRnd::SetExecutionContext(self->iExecutionContext);
sl@0
   299
	
sl@0
   300
	CleanupStack::Pop(self); 
sl@0
   301
	return self;
sl@0
   302
	}
sl@0
   303
sl@0
   304
void CTestExecWatchCat::ConstructL( CExecutionContext::TExecutionMode aExecutionMode )
sl@0
   305
	{
sl@0
   306
	iExecutionContext = CExecutionContext::NewL( aExecutionMode, *this );
sl@0
   307
	}
sl@0
   308
sl@0
   309
sl@0
   310
CTestExecWatchCat::CTestExecWatchCat()
sl@0
   311
	{ } // empty
sl@0
   312
sl@0
   313
CTestExecWatchCat::~CTestExecWatchCat()
sl@0
   314
	{
sl@0
   315
	delete iExecutionContext;
sl@0
   316
	}
sl@0
   317
	
sl@0
   318
void CTestExecWatchCat::ExecutionContextRunOut(CExecutionContext* /*aContext*/)
sl@0
   319
	{ }
sl@0
   320
sl@0
   321
/**
sl@0
   322
 Sets the path where the logging file will be created, then constructs the mediator
sl@0
   323
 */
sl@0
   324
void CTestExecWatchCat::SetLoggingPathL(const TDesC& aPath)
sl@0
   325
	{
sl@0
   326
	RBuf path;
sl@0
   327
	path.CleanupClosePushL();
sl@0
   328
	path.CreateL(aPath.Length() + 16);
sl@0
   329
	if (aPath.Length())
sl@0
   330
		{
sl@0
   331
		path.Copy(aPath);
sl@0
   332
		if (aPath[aPath.Length()-1] != '\\')
sl@0
   333
			{
sl@0
   334
			path.Append('\\');
sl@0
   335
			}
sl@0
   336
		}
sl@0
   337
	path.Append(KLogFileName);
sl@0
   338
	// now construct log mediator
sl@0
   339
	CExecutionContext::CLogMediator& mediator = iExecutionContext->CreateLogMediatorL( path );
sl@0
   340
	TRnd::SetLogMediator(&mediator);
sl@0
   341
	CleanupStack::PopAndDestroy(&path);
sl@0
   342
	}
sl@0
   343
sl@0
   344
/*************************************************************************************
sl@0
   345
 class TTickUtils
sl@0
   346
 */
sl@0
   347
TUint32 TTickUtils::CalcTickDelta(TUint32 tick1, TUint32 tick2)
sl@0
   348
	{
sl@0
   349
	TInt32 res;
sl@0
   350
	if ( tick1 > tick2 )
sl@0
   351
		{
sl@0
   352
		res = tick1 - tick2;
sl@0
   353
		}
sl@0
   354
	else
sl@0
   355
		{
sl@0
   356
		res = tick2 - tick1;		
sl@0
   357
		}
sl@0
   358
	return res;
sl@0
   359
	}