os/kernelhwsrv/kerneltest/e32test/system/t_tick.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) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\system\t_tick.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test tick-based timers
sl@0
    17
// API Information:
sl@0
    18
// RTimer
sl@0
    19
// Details:
sl@0
    20
// - Create a number of periodic timers, start each and wait for them to 
sl@0
    21
// complete. Print results.
sl@0
    22
// - Create a number of random relative timers, start each and wait for 
sl@0
    23
// them to complete. Print results.
sl@0
    24
// - Create a number of separate random relative timers, start each and 
sl@0
    25
// wait for them to complete. Print results.
sl@0
    26
// - Create a number of random absolute timers, start each and wait for 
sl@0
    27
// them to complete. Print results.
sl@0
    28
// Platforms/Drives/Compatibility:
sl@0
    29
// All.
sl@0
    30
// Assumptions/Requirement/Pre-requisites:
sl@0
    31
// Failures and causes:
sl@0
    32
// Base Port information:
sl@0
    33
// 
sl@0
    34
//
sl@0
    35
sl@0
    36
#include <e32test.h>
sl@0
    37
#include <e32uid.h>
sl@0
    38
#include "d_tick.h"
sl@0
    39
sl@0
    40
RTest test(_L("T_TICK"));
sl@0
    41
RTickTest ticktest;
sl@0
    42
TInt ActiveCount;
sl@0
    43
TInt ErrorCount;
sl@0
    44
sl@0
    45
TBool PauseOnError = 0;
sl@0
    46
#define GETCH()		(PauseOnError&&test.Getch())
sl@0
    47
sl@0
    48
#define TEST(c)		((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),GETCH(),test(0),0)))
sl@0
    49
#define CHECK(c)	((void)(((c)==0)||(test.Printf(_L("Error %d at line %d\n"),(c),__LINE__),GETCH(),test(0),0)))
sl@0
    50
sl@0
    51
const TPtrC KLddFileName=_L("D_TICK.LDD");
sl@0
    52
sl@0
    53
class CTickTest : public CActive
sl@0
    54
	{
sl@0
    55
public:
sl@0
    56
	CTickTest(TInt aPriority, TInt aId, RTickTest aLdd);
sl@0
    57
	~CTickTest();
sl@0
    58
	virtual void RunL();
sl@0
    59
	virtual void DoCancel();
sl@0
    60
public:
sl@0
    61
	void StartPeriodic(TInt aInterval, TInt aCount);
sl@0
    62
	void StartNShotRel(TInt aMin, TInt aRange, TInt aCount);
sl@0
    63
	void StartNShotAbs(TInt aMin, TInt aRange, TInt aCount);
sl@0
    64
	void GetInfo(STickTestInfo& aInfo);
sl@0
    65
public:
sl@0
    66
	TInt iId;
sl@0
    67
	TInt iCount;
sl@0
    68
	TInt iMin;
sl@0
    69
	TInt iRange;
sl@0
    70
	TInt64 iErrorAcc;
sl@0
    71
	RTickTest iLdd;
sl@0
    72
	STickTestInfo iInfo;
sl@0
    73
	};
sl@0
    74
sl@0
    75
class CBackgroundTimer : public CActive
sl@0
    76
	{
sl@0
    77
public:
sl@0
    78
	static CBackgroundTimer* NewL(TInt aPriority);
sl@0
    79
	CBackgroundTimer(TInt aPriority);
sl@0
    80
	~CBackgroundTimer();
sl@0
    81
	virtual void RunL();
sl@0
    82
	virtual void DoCancel();
sl@0
    83
	void Start();
sl@0
    84
public:
sl@0
    85
	RTimer iShort;
sl@0
    86
	RTimer iLong;
sl@0
    87
	TRequestStatus iLongStatus;
sl@0
    88
	};
sl@0
    89
sl@0
    90
CTickTest* TickTest[KMaxTimers];
sl@0
    91
CIdle* Idler;
sl@0
    92
CBackgroundTimer* BackgroundTimer;
sl@0
    93
sl@0
    94
CTickTest::CTickTest(TInt aPriority, TInt aId, RTickTest aLdd)
sl@0
    95
	:	CActive(aPriority),
sl@0
    96
		iId(aId),
sl@0
    97
		iLdd(aLdd)
sl@0
    98
	{
sl@0
    99
	}
sl@0
   100
sl@0
   101
CTickTest::~CTickTest()
sl@0
   102
	{
sl@0
   103
	Cancel();
sl@0
   104
	iLdd.SetHandle(0);
sl@0
   105
	}
sl@0
   106
sl@0
   107
void CTickTest::StartPeriodic(TInt aInterval, TInt aCount)
sl@0
   108
	{
sl@0
   109
	TInt r=iLdd.StartPeriodic(iStatus,iId,aInterval,aCount);
sl@0
   110
	if (r!=KErrNone)
sl@0
   111
		{
sl@0
   112
		TRequestStatus* pS=&iStatus;
sl@0
   113
		User::RequestComplete(pS,r);
sl@0
   114
		}
sl@0
   115
	SetActive();
sl@0
   116
	}
sl@0
   117
sl@0
   118
void CTickTest::StartNShotRel(TInt aMin, TInt aRange, TInt aCount)
sl@0
   119
	{
sl@0
   120
	TInt c=aCount;
sl@0
   121
	if (aCount<0)
sl@0
   122
		{
sl@0
   123
		iCount=-aCount;
sl@0
   124
		c=1;
sl@0
   125
		}
sl@0
   126
	iMin=aMin;
sl@0
   127
	iRange=aRange;
sl@0
   128
	iErrorAcc=0;
sl@0
   129
	iInfo.iMinErr=KMaxTInt;
sl@0
   130
	iInfo.iMaxErr=KMinTInt;
sl@0
   131
	iInfo.iCount=0;
sl@0
   132
	iInfo.iRequestedCount=iCount;
sl@0
   133
	iInfo.iTotalTime=0;
sl@0
   134
	TInt r=iLdd.StartNShotRel(iStatus,iId,aMin,aRange,c);
sl@0
   135
	if (r!=KErrNone)
sl@0
   136
		{
sl@0
   137
		TRequestStatus* pS=&iStatus;
sl@0
   138
		User::RequestComplete(pS,r);
sl@0
   139
		}
sl@0
   140
	SetActive();
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CTickTest::StartNShotAbs(TInt aMin, TInt aRange, TInt aCount)
sl@0
   144
	{
sl@0
   145
	TInt r=iLdd.StartNShotAbs(iStatus,iId,aMin,aRange,aCount);
sl@0
   146
	if (r!=KErrNone)
sl@0
   147
		{
sl@0
   148
		TRequestStatus* pS=&iStatus;
sl@0
   149
		User::RequestComplete(pS,r);
sl@0
   150
		}
sl@0
   151
	SetActive();
sl@0
   152
	}
sl@0
   153
sl@0
   154
void CTickTest::GetInfo(STickTestInfo& aInfo)
sl@0
   155
	{
sl@0
   156
	iLdd.GetInfo(iId,aInfo);
sl@0
   157
	}
sl@0
   158
sl@0
   159
void CTickTest::RunL()
sl@0
   160
	{
sl@0
   161
	if (iStatus!=KErrNone)
sl@0
   162
		{
sl@0
   163
		test.Printf(_L("Timer %d error %d\n"),iId,iStatus.Int());
sl@0
   164
		++ErrorCount;
sl@0
   165
		CActiveScheduler::Stop();
sl@0
   166
		return;
sl@0
   167
		}
sl@0
   168
	if (iCount==0)
sl@0
   169
		GetInfo(iInfo);
sl@0
   170
	else
sl@0
   171
		{
sl@0
   172
		STickTestInfo info;
sl@0
   173
		iLdd.GetInfo(iId,info);
sl@0
   174
		TInt err=info.iMinErr;
sl@0
   175
		if (err<iInfo.iMinErr)
sl@0
   176
			iInfo.iMinErr=err;
sl@0
   177
		if (err>iInfo.iMaxErr)
sl@0
   178
			iInfo.iMaxErr=err;
sl@0
   179
		++iInfo.iCount;
sl@0
   180
		iErrorAcc+=err;
sl@0
   181
		if (--iCount)
sl@0
   182
			{
sl@0
   183
			TInt r=iLdd.StartNShotRel(iStatus,iId,iMin,iRange,1);
sl@0
   184
			if (r!=KErrNone)
sl@0
   185
				{
sl@0
   186
				TRequestStatus* pS=&iStatus;
sl@0
   187
				User::RequestComplete(pS,r);
sl@0
   188
				}
sl@0
   189
			SetActive();
sl@0
   190
			return;
sl@0
   191
			}
sl@0
   192
		iInfo.iAvgErr=I64INT(iErrorAcc/TInt64(iInfo.iCount));
sl@0
   193
		}
sl@0
   194
	test.Printf(_L("Timer %d\n"),iId);
sl@0
   195
	if (!--ActiveCount)
sl@0
   196
		CActiveScheduler::Stop();
sl@0
   197
	}
sl@0
   198
sl@0
   199
void CTickTest::DoCancel()
sl@0
   200
	{
sl@0
   201
	iLdd.Stop(iId);
sl@0
   202
	}
sl@0
   203
sl@0
   204
CBackgroundTimer::CBackgroundTimer(TInt aPriority)
sl@0
   205
	:	CActive(aPriority)
sl@0
   206
	{
sl@0
   207
	}
sl@0
   208
sl@0
   209
CBackgroundTimer::~CBackgroundTimer()
sl@0
   210
	{
sl@0
   211
	iShort.Close();
sl@0
   212
	iLong.Close();
sl@0
   213
	}
sl@0
   214
sl@0
   215
void CBackgroundTimer::Start()
sl@0
   216
	{
sl@0
   217
	iLong.After(iLongStatus, 100000);
sl@0
   218
	iShort.After(iStatus, 20000);
sl@0
   219
	SetActive();
sl@0
   220
	}
sl@0
   221
sl@0
   222
void CBackgroundTimer::RunL()
sl@0
   223
	{
sl@0
   224
	iLong.Cancel();
sl@0
   225
	Start();
sl@0
   226
	}
sl@0
   227
sl@0
   228
void CBackgroundTimer::DoCancel()
sl@0
   229
	{
sl@0
   230
	iShort.Cancel();
sl@0
   231
	iLong.Cancel();
sl@0
   232
	}
sl@0
   233
sl@0
   234
CBackgroundTimer* CBackgroundTimer::NewL(TInt aPriority)
sl@0
   235
	{
sl@0
   236
	CBackgroundTimer* pB=new (ELeave) CBackgroundTimer(aPriority);
sl@0
   237
	TInt r=pB->iShort.CreateLocal();
sl@0
   238
	if (r==KErrNone)
sl@0
   239
		r=pB->iLong.CreateLocal();
sl@0
   240
	if (r!=KErrNone)
sl@0
   241
		{
sl@0
   242
		delete pB;
sl@0
   243
		pB=NULL;
sl@0
   244
		}
sl@0
   245
	return pB;
sl@0
   246
	}
sl@0
   247
sl@0
   248
void InitialiseL()
sl@0
   249
	{
sl@0
   250
	CActiveScheduler* pS=new (ELeave) CActiveScheduler;
sl@0
   251
	CActiveScheduler::Install(pS);
sl@0
   252
	TInt i;
sl@0
   253
	for (i=0; i<KMaxTimers; ++i)
sl@0
   254
		{
sl@0
   255
		TickTest[i]=new (ELeave) CTickTest(0,i,ticktest);
sl@0
   256
		CActiveScheduler::Add(TickTest[i]);
sl@0
   257
		}
sl@0
   258
	Idler=CIdle::NewL(-100);
sl@0
   259
	BackgroundTimer=CBackgroundTimer::NewL(-10);
sl@0
   260
	CActiveScheduler::Add(BackgroundTimer);
sl@0
   261
	}
sl@0
   262
sl@0
   263
void PrintInfo(TInt aId, STickTestInfo& a)
sl@0
   264
	{
sl@0
   265
	test.Printf(_L("%1d: min=%-6d max=%-6d avg=%-6d tot=%-10u count=%d rcount=%d\n"),aId,a.iMinErr,a.iMaxErr,a.iAvgErr,a.iTotalTime,a.iCount,a.iRequestedCount);
sl@0
   266
	}
sl@0
   267
sl@0
   268
void PrintInfo(TInt aId)
sl@0
   269
	{
sl@0
   270
	PrintInfo(aId, TickTest[aId]->iInfo);
sl@0
   271
	}
sl@0
   272
sl@0
   273
void PrintInfo()
sl@0
   274
	{
sl@0
   275
	TInt i;
sl@0
   276
	for (i=0; i<KMaxTimers; ++i)
sl@0
   277
		PrintInfo(i);
sl@0
   278
	}
sl@0
   279
sl@0
   280
TInt ChangeTime(TAny*)
sl@0
   281
	{
sl@0
   282
	TTime now;
sl@0
   283
	now.UniversalTime();
sl@0
   284
	User::SetUTCTime(now+TTimeIntervalSeconds(1000));
sl@0
   285
	User::SetUTCTime(now);
sl@0
   286
	return 1;	// so we run again
sl@0
   287
	}
sl@0
   288
sl@0
   289
GLDEF_C TInt E32Main()
sl@0
   290
//
sl@0
   291
// Test tick-based timers
sl@0
   292
//
sl@0
   293
    {
sl@0
   294
sl@0
   295
	test.Title();
sl@0
   296
sl@0
   297
	test.Start(_L("Load test LDD"));
sl@0
   298
	TInt r=User::LoadLogicalDevice(KLddFileName);
sl@0
   299
	TEST(r==KErrNone || r==KErrAlreadyExists);
sl@0
   300
	
sl@0
   301
	r=ticktest.Open();
sl@0
   302
	CHECK(r);
sl@0
   303
sl@0
   304
	test.Next(_L("Create test objects"));
sl@0
   305
	TRAP(r, InitialiseL());
sl@0
   306
	CHECK(r);
sl@0
   307
sl@0
   308
	test.Next(_L("Start periodics"));
sl@0
   309
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   310
	TickTest[1]->StartPeriodic(5,600);
sl@0
   311
	TickTest[2]->StartPeriodic(7,400);
sl@0
   312
	TickTest[3]->StartPeriodic(11,300);
sl@0
   313
	TickTest[4]->StartPeriodic(13,30);
sl@0
   314
	TickTest[5]->StartPeriodic(19,30);
sl@0
   315
	TickTest[6]->StartPeriodic(23,30);
sl@0
   316
	TickTest[7]->StartPeriodic(37,30);
sl@0
   317
	ActiveCount=8;
sl@0
   318
	ErrorCount=0;
sl@0
   319
	CActiveScheduler::Start();
sl@0
   320
	PrintInfo();
sl@0
   321
sl@0
   322
	test.Next(_L("Start random relative"));
sl@0
   323
	TickTest[0]->StartNShotRel(1,10,1000);
sl@0
   324
	TickTest[1]->StartNShotRel(5,25,300);
sl@0
   325
	TickTest[2]->StartNShotRel(7,93,100);
sl@0
   326
	TickTest[3]->StartNShotRel(2,2,1000);
sl@0
   327
	TickTest[4]->StartPeriodic(13,30);
sl@0
   328
	TickTest[5]->StartPeriodic(19,30);
sl@0
   329
	TickTest[6]->StartPeriodic(23,30);
sl@0
   330
	TickTest[7]->StartPeriodic(37,30);
sl@0
   331
	ActiveCount=8;
sl@0
   332
	ErrorCount=0;
sl@0
   333
	CActiveScheduler::Start();
sl@0
   334
	PrintInfo();
sl@0
   335
sl@0
   336
	test.Next(_L("Start separate random relative"));
sl@0
   337
	TickTest[0]->StartNShotRel(1,10,1000);
sl@0
   338
	TickTest[1]->StartNShotRel(5,25,300);
sl@0
   339
	TickTest[2]->StartNShotRel(7,93,100);
sl@0
   340
	TickTest[3]->StartNShotRel(2,2,1000);
sl@0
   341
	TickTest[4]->StartNShotRel(1,10,-1000);
sl@0
   342
	TickTest[5]->StartNShotRel(5,25,-300);
sl@0
   343
	TickTest[6]->StartNShotRel(7,93,-100);
sl@0
   344
	TickTest[7]->StartNShotRel(2,2,-1000);
sl@0
   345
	ActiveCount=8;
sl@0
   346
	ErrorCount=0;
sl@0
   347
	CActiveScheduler::Start();
sl@0
   348
	PrintInfo();
sl@0
   349
sl@0
   350
	test.Next(_L("Start random absolute"));
sl@0
   351
	TickTest[0]->StartNShotAbs(1,10,10);
sl@0
   352
	TickTest[1]->StartNShotAbs(5,13,3);
sl@0
   353
	TickTest[2]->StartNShotAbs(1,3,20);
sl@0
   354
	TickTest[3]->StartNShotAbs(10,1,4);
sl@0
   355
	TickTest[4]->StartPeriodic(13,30);
sl@0
   356
	TickTest[5]->StartPeriodic(19,30);
sl@0
   357
	TickTest[6]->StartPeriodic(23,30);
sl@0
   358
	TickTest[7]->StartPeriodic(37,30);
sl@0
   359
	ActiveCount=8;
sl@0
   360
	ErrorCount=0;
sl@0
   361
	CActiveScheduler::Start();
sl@0
   362
	PrintInfo();
sl@0
   363
sl@0
   364
	ticktest.Close();
sl@0
   365
	test.End();
sl@0
   366
	return(KErrNone);
sl@0
   367
    }
sl@0
   368