os/kernelhwsrv/kerneltest/e32test/system/t_tock.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_tock.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 change the time
sl@0
    21
// offset via TLocale::SetUniversalTimeOffset(). Print the results.
sl@0
    22
// - Create a number of periodic timers, start each and change the system
sl@0
    23
// time. Print the results.
sl@0
    24
// - Create a number of periodic timers, start each and change the system
sl@0
    25
// time in a second thread. Print the results.
sl@0
    26
// - Create a number of periodic timers, start each along with a background
sl@0
    27
// timer. Print the results.
sl@0
    28
// - Create a number of periodic timers, start each and change the system
sl@0
    29
// time and tick delay. Print the results.
sl@0
    30
// Platforms/Drives/Compatibility:
sl@0
    31
// All.
sl@0
    32
// Assumptions/Requirement/Pre-requisites:
sl@0
    33
// Failures and causes:
sl@0
    34
// Base Port information:
sl@0
    35
// 
sl@0
    36
//
sl@0
    37
sl@0
    38
#include <e32test.h>
sl@0
    39
#include <e32uid.h>
sl@0
    40
#include "d_tick.h"
sl@0
    41
sl@0
    42
RTest test(_L("T_TOCK"));
sl@0
    43
RTickTest ticktest;
sl@0
    44
TInt ActiveCount;
sl@0
    45
TInt ErrorCount;
sl@0
    46
sl@0
    47
TBool PauseOnError = 0;
sl@0
    48
#define GETCH()		(PauseOnError&&test.Getch())
sl@0
    49
sl@0
    50
#define TEST(c)		((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),GETCH(),test(0),0)))
sl@0
    51
#define CHECK(c)	((void)(((c)==0)||(test.Printf(_L("Error %d at line %d\n"),(c),__LINE__),GETCH(),test(0),0)))
sl@0
    52
sl@0
    53
const TPtrC KLddFileName=_L("D_TICK.LDD");
sl@0
    54
sl@0
    55
class CTickTest : public CActive
sl@0
    56
	{
sl@0
    57
public:
sl@0
    58
	CTickTest(TInt aPriority, TInt aId, RTickTest aLdd);
sl@0
    59
	~CTickTest();
sl@0
    60
	virtual void RunL();
sl@0
    61
	virtual void DoCancel();
sl@0
    62
public:
sl@0
    63
	void StartPeriodic(TInt aInterval, TInt aCount);
sl@0
    64
	void StartNShotRel(TInt aMin, TInt aRange, TInt aCount);
sl@0
    65
	void StartNShotAbs(TInt aMin, TInt aRange, TInt aCount);
sl@0
    66
	void StartNShotDelay(TInt aPeriod, TInt aDelay, TInt aCount);
sl@0
    67
	void GetInfo(STickTestInfo& aInfo);
sl@0
    68
public:
sl@0
    69
	TInt iId;
sl@0
    70
	TInt iCount;
sl@0
    71
	TInt iMin;
sl@0
    72
	TInt iRange;
sl@0
    73
	TInt64 iErrorAcc;
sl@0
    74
	RTickTest iLdd;
sl@0
    75
	STickTestInfo iInfo;
sl@0
    76
	};
sl@0
    77
sl@0
    78
class CBackgroundTimer : public CActive
sl@0
    79
	{
sl@0
    80
public:
sl@0
    81
	static CBackgroundTimer* NewL(TInt aPriority);
sl@0
    82
	CBackgroundTimer(TInt aPriority);
sl@0
    83
	~CBackgroundTimer();
sl@0
    84
	virtual void RunL();
sl@0
    85
	virtual void DoCancel();
sl@0
    86
	void Start();
sl@0
    87
public:
sl@0
    88
	RTimer iShort;
sl@0
    89
	RTimer iLong;
sl@0
    90
	TRequestStatus iLongStatus;
sl@0
    91
	};
sl@0
    92
sl@0
    93
CTickTest* TickTest[KMaxTimers];
sl@0
    94
CIdle* Idler;
sl@0
    95
CBackgroundTimer* BackgroundTimer;
sl@0
    96
sl@0
    97
CTickTest::CTickTest(TInt aPriority, TInt aId, RTickTest aLdd)
sl@0
    98
	:	CActive(aPriority),
sl@0
    99
		iId(aId),
sl@0
   100
		iLdd(aLdd)
sl@0
   101
	{
sl@0
   102
	}
sl@0
   103
sl@0
   104
CTickTest::~CTickTest()
sl@0
   105
	{
sl@0
   106
	Cancel();
sl@0
   107
	iLdd.SetHandle(0);
sl@0
   108
	}
sl@0
   109
sl@0
   110
void CTickTest::StartPeriodic(TInt aInterval, TInt aCount)
sl@0
   111
	{
sl@0
   112
	TInt r=iLdd.StartPeriodic(iStatus,iId,aInterval,aCount);
sl@0
   113
	if (r!=KErrNone)
sl@0
   114
		{
sl@0
   115
		TRequestStatus* pS=&iStatus;
sl@0
   116
		User::RequestComplete(pS,r);
sl@0
   117
		}
sl@0
   118
	SetActive();
sl@0
   119
	}
sl@0
   120
sl@0
   121
void CTickTest::StartNShotRel(TInt aMin, TInt aRange, TInt aCount)
sl@0
   122
	{
sl@0
   123
	TInt c=aCount;
sl@0
   124
	if (aCount<0)
sl@0
   125
		{
sl@0
   126
		iCount=-aCount;
sl@0
   127
		c=1;
sl@0
   128
		}
sl@0
   129
	iMin=aMin;
sl@0
   130
	iRange=aRange;
sl@0
   131
	iErrorAcc=0;
sl@0
   132
	iInfo.iMinErr=KMaxTInt;
sl@0
   133
	iInfo.iMaxErr=KMinTInt;
sl@0
   134
	iInfo.iCount=0;
sl@0
   135
	iInfo.iRequestedCount=iCount;
sl@0
   136
	iInfo.iTotalTime=0;
sl@0
   137
	TInt r=iLdd.StartNShotRel(iStatus,iId,aMin,aRange,c);
sl@0
   138
	if (r!=KErrNone)
sl@0
   139
		{
sl@0
   140
		TRequestStatus* pS=&iStatus;
sl@0
   141
		User::RequestComplete(pS,r);
sl@0
   142
		}
sl@0
   143
	SetActive();
sl@0
   144
	}
sl@0
   145
sl@0
   146
void CTickTest::StartNShotAbs(TInt aMin, TInt aRange, TInt aCount)
sl@0
   147
	{
sl@0
   148
	TInt r=iLdd.StartNShotAbs(iStatus,iId,aMin,aRange,aCount);
sl@0
   149
	if (r!=KErrNone)
sl@0
   150
		{
sl@0
   151
		TRequestStatus* pS=&iStatus;
sl@0
   152
		User::RequestComplete(pS,r);
sl@0
   153
		}
sl@0
   154
	SetActive();
sl@0
   155
	}
sl@0
   156
sl@0
   157
void CTickTest::StartNShotDelay(TInt aPeriod, TInt aDelay, TInt aCount)
sl@0
   158
	{
sl@0
   159
	iCount=0;
sl@0
   160
	TInt r=iLdd.StartNShotDelay(iStatus,iId,aPeriod,aDelay,aCount);
sl@0
   161
	if (r!=KErrNone)
sl@0
   162
		{
sl@0
   163
		TRequestStatus* pS=&iStatus;
sl@0
   164
		User::RequestComplete(pS,r);
sl@0
   165
		}
sl@0
   166
	SetActive();
sl@0
   167
	}
sl@0
   168
sl@0
   169
void CTickTest::GetInfo(STickTestInfo& aInfo)
sl@0
   170
	{
sl@0
   171
	iLdd.GetInfo(iId,aInfo);
sl@0
   172
	}
sl@0
   173
sl@0
   174
void CTickTest::RunL()
sl@0
   175
	{
sl@0
   176
	if (iStatus!=KErrNone)
sl@0
   177
		{
sl@0
   178
		test.Printf(_L("Timer %d error %d\n"),iId,iStatus.Int());
sl@0
   179
		++ErrorCount;
sl@0
   180
		CActiveScheduler::Stop();
sl@0
   181
		return;
sl@0
   182
		}
sl@0
   183
	if (iCount==0)
sl@0
   184
		GetInfo(iInfo);
sl@0
   185
	else
sl@0
   186
		{
sl@0
   187
		STickTestInfo info;
sl@0
   188
		iLdd.GetInfo(iId,info);
sl@0
   189
		TInt err=info.iMinErr;
sl@0
   190
		if (err<iInfo.iMinErr)
sl@0
   191
			iInfo.iMinErr=err;
sl@0
   192
		if (err>iInfo.iMaxErr)
sl@0
   193
			iInfo.iMaxErr=err;
sl@0
   194
		++iInfo.iCount;
sl@0
   195
		iErrorAcc+=err;
sl@0
   196
		if (--iCount)
sl@0
   197
			{
sl@0
   198
			TInt r=iLdd.StartNShotRel(iStatus,iId,iMin,iRange,1);
sl@0
   199
			if (r!=KErrNone)
sl@0
   200
				{
sl@0
   201
				TRequestStatus* pS=&iStatus;
sl@0
   202
				User::RequestComplete(pS,r);
sl@0
   203
				}
sl@0
   204
			SetActive();
sl@0
   205
			return;
sl@0
   206
			}
sl@0
   207
		iInfo.iAvgErr=I64INT(iErrorAcc/TInt64(iInfo.iCount));
sl@0
   208
		}
sl@0
   209
	test.Printf(_L("Timer %d\n"),iId);
sl@0
   210
	if (!--ActiveCount)
sl@0
   211
		CActiveScheduler::Stop();
sl@0
   212
	}
sl@0
   213
sl@0
   214
void CTickTest::DoCancel()
sl@0
   215
	{
sl@0
   216
	iLdd.Stop(iId);
sl@0
   217
	}
sl@0
   218
sl@0
   219
CBackgroundTimer::CBackgroundTimer(TInt aPriority)
sl@0
   220
	:	CActive(aPriority)
sl@0
   221
	{
sl@0
   222
	}
sl@0
   223
sl@0
   224
CBackgroundTimer::~CBackgroundTimer()
sl@0
   225
	{
sl@0
   226
	iShort.Close();
sl@0
   227
	iLong.Close();
sl@0
   228
	}
sl@0
   229
sl@0
   230
void CBackgroundTimer::Start()
sl@0
   231
	{
sl@0
   232
	iLong.After(iLongStatus, 100000);
sl@0
   233
	iShort.After(iStatus, 20000);
sl@0
   234
	SetActive();
sl@0
   235
	}
sl@0
   236
sl@0
   237
void CBackgroundTimer::RunL()
sl@0
   238
	{
sl@0
   239
	iLong.Cancel();
sl@0
   240
	User::WaitForRequest(iLongStatus);
sl@0
   241
	Start();
sl@0
   242
	}
sl@0
   243
sl@0
   244
void CBackgroundTimer::DoCancel()
sl@0
   245
	{
sl@0
   246
	iShort.Cancel();
sl@0
   247
	iLong.Cancel();
sl@0
   248
	User::WaitForRequest(iLongStatus);
sl@0
   249
	}
sl@0
   250
sl@0
   251
CBackgroundTimer* CBackgroundTimer::NewL(TInt aPriority)
sl@0
   252
	{
sl@0
   253
	CBackgroundTimer* pB=new (ELeave) CBackgroundTimer(aPriority);
sl@0
   254
	TInt r=pB->iShort.CreateLocal();
sl@0
   255
	if (r==KErrNone)
sl@0
   256
		r=pB->iLong.CreateLocal();
sl@0
   257
	if (r!=KErrNone)
sl@0
   258
		{
sl@0
   259
		delete pB;
sl@0
   260
		pB=NULL;
sl@0
   261
		}
sl@0
   262
	return pB;
sl@0
   263
	}
sl@0
   264
sl@0
   265
void InitialiseL()
sl@0
   266
	{
sl@0
   267
	CActiveScheduler* pS=new (ELeave) CActiveScheduler;
sl@0
   268
	CActiveScheduler::Install(pS);
sl@0
   269
	TInt i;
sl@0
   270
	for (i=0; i<KMaxTimers; ++i)
sl@0
   271
		{
sl@0
   272
		TickTest[i]=new (ELeave) CTickTest(0,i,ticktest);
sl@0
   273
		CActiveScheduler::Add(TickTest[i]);
sl@0
   274
		}
sl@0
   275
	Idler=CIdle::NewL(-100);
sl@0
   276
	BackgroundTimer=CBackgroundTimer::NewL(-10);
sl@0
   277
	CActiveScheduler::Add(BackgroundTimer);
sl@0
   278
	}
sl@0
   279
sl@0
   280
void PrintInfo(TInt aId, STickTestInfo& a)
sl@0
   281
	{
sl@0
   282
	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
   283
	}
sl@0
   284
sl@0
   285
void PrintInfo(TInt aId)
sl@0
   286
	{
sl@0
   287
	PrintInfo(aId, TickTest[aId]->iInfo);
sl@0
   288
	}
sl@0
   289
sl@0
   290
void PrintInfo()
sl@0
   291
	{
sl@0
   292
	TInt i;
sl@0
   293
	for (i=0; i<KMaxTimers; ++i)
sl@0
   294
		PrintInfo(i);
sl@0
   295
	}
sl@0
   296
sl@0
   297
TInt ChangeOffset(TAny*)
sl@0
   298
	{
sl@0
   299
	User::SetUTCOffset(3600);
sl@0
   300
	User::SetUTCOffset(0);
sl@0
   301
	return 1;	// so we run again
sl@0
   302
	}
sl@0
   303
sl@0
   304
TInt ChangeTime(TAny*)
sl@0
   305
	{
sl@0
   306
	TTime now;
sl@0
   307
	now.UniversalTime();
sl@0
   308
	User::SetUTCTime(now+TTimeIntervalSeconds(1000));
sl@0
   309
	User::SetUTCTime(now);
sl@0
   310
	return 1;	// so we run again
sl@0
   311
	}
sl@0
   312
sl@0
   313
TInt ChangeTimeThread(TAny*)
sl@0
   314
	{
sl@0
   315
	FOREVER
sl@0
   316
		{
sl@0
   317
		User::AfterHighRes(3000);
sl@0
   318
		TTime now;
sl@0
   319
		now.UniversalTime();
sl@0
   320
		User::SetUTCTime(now+TTimeIntervalSeconds(1000));
sl@0
   321
		User::AfterHighRes(1000);
sl@0
   322
		User::SetUTCTime(now);
sl@0
   323
		}
sl@0
   324
	}
sl@0
   325
sl@0
   326
GLDEF_C TInt E32Main()
sl@0
   327
//
sl@0
   328
// Test tick-based timers
sl@0
   329
//
sl@0
   330
    {
sl@0
   331
sl@0
   332
//	test.SetLogged(EFalse);
sl@0
   333
	test.Title();
sl@0
   334
sl@0
   335
	test.Start(_L("Load test LDD"));
sl@0
   336
	TInt r=User::LoadLogicalDevice(KLddFileName);
sl@0
   337
	TEST(r==KErrNone || r==KErrAlreadyExists);
sl@0
   338
	
sl@0
   339
	r=ticktest.Open();
sl@0
   340
	CHECK(r);
sl@0
   341
sl@0
   342
	test.Next(_L("Create test objects"));
sl@0
   343
	TRAP(r, InitialiseL());
sl@0
   344
	CHECK(r);
sl@0
   345
sl@0
   346
	test.Next(_L("Periodics with changing home time offset"));
sl@0
   347
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   348
	TickTest[1]->StartPeriodic(5,600);
sl@0
   349
	TickTest[2]->StartPeriodic(7,400);
sl@0
   350
	TickTest[3]->StartPeriodic(11,300);
sl@0
   351
	TickTest[4]->StartPeriodic(13,30);
sl@0
   352
	TickTest[5]->StartPeriodic(19,30);
sl@0
   353
	TickTest[6]->StartPeriodic(23,30);
sl@0
   354
	TickTest[7]->StartPeriodic(37,30);
sl@0
   355
	Idler->Start(TCallBack(ChangeOffset,NULL));
sl@0
   356
	ActiveCount=8;
sl@0
   357
	ErrorCount=0;
sl@0
   358
	CActiveScheduler::Start();
sl@0
   359
	Idler->Cancel();
sl@0
   360
	PrintInfo();
sl@0
   361
sl@0
   362
	test.Next(_L("Periodics with changing system time"));
sl@0
   363
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   364
	TickTest[1]->StartPeriodic(5,600);
sl@0
   365
	TickTest[2]->StartPeriodic(7,400);
sl@0
   366
	TickTest[3]->StartPeriodic(11,300);
sl@0
   367
	TickTest[4]->StartPeriodic(13,30);
sl@0
   368
	TickTest[5]->StartPeriodic(19,30);
sl@0
   369
	TickTest[6]->StartPeriodic(23,30);
sl@0
   370
	TickTest[7]->StartPeriodic(37,30);
sl@0
   371
	Idler->Start(TCallBack(ChangeTime,NULL));
sl@0
   372
	ActiveCount=8;
sl@0
   373
	ErrorCount=0;
sl@0
   374
	CActiveScheduler::Start();
sl@0
   375
	Idler->Cancel();
sl@0
   376
	PrintInfo();
sl@0
   377
sl@0
   378
	test.Next(_L("Periodics with changing system time in second thread"));
sl@0
   379
	RThread t;
sl@0
   380
	r=t.Create(KNullDesC(),ChangeTimeThread,0x1000,NULL,NULL);
sl@0
   381
	CHECK(r);
sl@0
   382
	t.SetPriority(EPriorityMore);
sl@0
   383
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   384
	TickTest[1]->StartPeriodic(5,600);
sl@0
   385
	TickTest[2]->StartPeriodic(7,400);
sl@0
   386
	TickTest[3]->StartPeriodic(11,300);
sl@0
   387
	TickTest[4]->StartPeriodic(13,30);
sl@0
   388
	TickTest[5]->StartPeriodic(19,30);
sl@0
   389
	TickTest[6]->StartPeriodic(23,30);
sl@0
   390
	TickTest[7]->StartPeriodic(37,30);
sl@0
   391
	Idler->Start(TCallBack(ChangeTime,NULL));
sl@0
   392
	ActiveCount=8;
sl@0
   393
	ErrorCount=0;
sl@0
   394
	t.Resume();
sl@0
   395
	CActiveScheduler::Start();
sl@0
   396
	Idler->Cancel();
sl@0
   397
	TRequestStatus s;
sl@0
   398
	t.Logon(s);
sl@0
   399
	t.Kill(0);
sl@0
   400
	User::WaitForRequest(s);
sl@0
   401
	test(t.ExitType()==EExitKill);
sl@0
   402
	test(t.ExitReason()==0);
sl@0
   403
	test(s==0);
sl@0
   404
	CLOSE_AND_WAIT(t);
sl@0
   405
	PrintInfo();
sl@0
   406
sl@0
   407
	test.Next(_L("Periodics with background timer"));
sl@0
   408
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   409
	TickTest[1]->StartPeriodic(5,600);
sl@0
   410
	TickTest[2]->StartPeriodic(7,400);
sl@0
   411
	TickTest[3]->StartPeriodic(11,300);
sl@0
   412
	TickTest[4]->StartPeriodic(13,30);
sl@0
   413
	TickTest[5]->StartPeriodic(19,30);
sl@0
   414
	TickTest[6]->StartPeriodic(23,30);
sl@0
   415
	TickTest[7]->StartPeriodic(37,30);
sl@0
   416
	BackgroundTimer->Start();
sl@0
   417
	ActiveCount=8;
sl@0
   418
	ErrorCount=0;
sl@0
   419
	CActiveScheduler::Start();
sl@0
   420
	BackgroundTimer->Cancel();
sl@0
   421
	PrintInfo();
sl@0
   422
sl@0
   423
	test.Next(_L("Periodics with changing system time and tick delay"));
sl@0
   424
	TickTest[0]->StartPeriodic(3,1000);
sl@0
   425
	TickTest[1]->StartPeriodic(5,600);
sl@0
   426
	TickTest[2]->StartPeriodic(7,400);
sl@0
   427
	TickTest[3]->StartPeriodic(11,300);
sl@0
   428
	TickTest[4]->StartPeriodic(13,30);
sl@0
   429
	TickTest[5]->StartPeriodic(19,30);
sl@0
   430
	TickTest[6]->StartPeriodic(23,30);
sl@0
   431
	TickTest[7]->StartNShotDelay(17,30,200);
sl@0
   432
	Idler->Start(TCallBack(ChangeTime,NULL));
sl@0
   433
	ActiveCount=8;
sl@0
   434
	ErrorCount=0;
sl@0
   435
	CActiveScheduler::Start();
sl@0
   436
	Idler->Cancel();
sl@0
   437
	PrintInfo();
sl@0
   438
sl@0
   439
	ticktest.Close();
sl@0
   440
	test.End();
sl@0
   441
	return(KErrNone);
sl@0
   442
    }
sl@0
   443