os/kernelhwsrv/kerneltest/e32test/active/t_cper.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 1995-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\active\t_cper.cpp
sl@0
    15
// Overview:
sl@0
    16
// Test periodic timers. 
sl@0
    17
// API Information:
sl@0
    18
// CPeriodic, CHeartbeat
sl@0
    19
// Details:	
sl@0
    20
// - Create some CPeriodic timer active objects with different priorities.
sl@0
    21
// - Start the periodic timers with varying delay time to start generation 
sl@0
    22
// of first event and different intervals between events
sl@0
    23
// - Verify the callback functions associated with each periodic are called 
sl@0
    24
// in order of the time when the event occurred and considering the priority 
sl@0
    25
// of the periodics.
sl@0
    26
// - Create heartbeat timer with different priorities
sl@0
    27
// - Start one heartbeat synchronized at ETwelveOClock 
sl@0
    28
// - Start two heartbeats synchronized at ETwelveOClock, ESixOClock
sl@0
    29
// - Start three heartbeats synchronized at ETwelveOClock, ESixOClock, ETwelveOClock
sl@0
    30
// - Display start time and beat time for each heartbeat timer
sl@0
    31
// - Check if the heap has been corrupted by all the tests.
sl@0
    32
// Platforms/Drives/Compatibility:
sl@0
    33
// All.
sl@0
    34
// Assumptions/Requirement/Pre-requisites:
sl@0
    35
// Failures and causes:
sl@0
    36
// -	The first part of the test (for CPeriodic) will fail if the timers are not completed in order. 
sl@0
    37
// The test on emulator is very sensitive on the background activities on PC.
sl@0
    38
// Base Port information:
sl@0
    39
// 
sl@0
    40
//
sl@0
    41
sl@0
    42
#include <e32base.h>
sl@0
    43
#include <e32base_private.h>
sl@0
    44
#include <e32hal.h>
sl@0
    45
#include <e32test.h>
sl@0
    46
#include <hal.h>
sl@0
    47
#include <u32hal.h>
sl@0
    48
#include <e32svr.h>
sl@0
    49
sl@0
    50
LOCAL_D RTest test(_L("T_CPER"));
sl@0
    51
sl@0
    52
class myScheduler: public CActiveScheduler
sl@0
    53
	{
sl@0
    54
public:	
sl@0
    55
	virtual void Error(TInt anError) const;
sl@0
    56
	};
sl@0
    57
sl@0
    58
void myScheduler::Error(TInt anError) const
sl@0
    59
//
sl@0
    60
// virtual error handler 
sl@0
    61
//
sl@0
    62
	{
sl@0
    63
	test.Panic(anError,_L("myScheduler::Error"));
sl@0
    64
	}
sl@0
    65
sl@0
    66
TInt Array[11];
sl@0
    67
TTime Times[11];
sl@0
    68
TInt counter = 0;
sl@0
    69
sl@0
    70
CPeriodic* pPer1;
sl@0
    71
CPeriodic* pPer2;
sl@0
    72
CPeriodic* pPer3;
sl@0
    73
CPeriodic* pPer4;
sl@0
    74
CPeriodic* pPer5;
sl@0
    75
CPeriodic* pPer6;
sl@0
    76
CPeriodic* pPer7;
sl@0
    77
sl@0
    78
TInt CallBackFn(TAny* Ptr)
sl@0
    79
//
sl@0
    80
// Callback function used for all periodics
sl@0
    81
// On calling Ptr is actually a TInt - the periodic Id
sl@0
    82
//
sl@0
    83
	{
sl@0
    84
	if (counter < 11)
sl@0
    85
		{
sl@0
    86
		Array[counter] = (TInt)Ptr;
sl@0
    87
		Times[counter].HomeTime();
sl@0
    88
		counter++;
sl@0
    89
		}
sl@0
    90
	return(0);
sl@0
    91
	}
sl@0
    92
sl@0
    93
TInt CallBackPanic(TAny* Ptr)
sl@0
    94
//
sl@0
    95
// Periodic should never get called
sl@0
    96
//
sl@0
    97
	{
sl@0
    98
	test.Printf(_L("  PERIODIC %d HAS GONE OFF!\n"),(TInt)Ptr);
sl@0
    99
	test(EFalse);
sl@0
   100
	return(KErrGeneral);
sl@0
   101
	}
sl@0
   102
sl@0
   103
class myTimer: public CTimer
sl@0
   104
	{
sl@0
   105
public:
sl@0
   106
	myTimer(TInt aPriority);
sl@0
   107
	virtual void RunL(); 
sl@0
   108
	};
sl@0
   109
sl@0
   110
myTimer::myTimer(TInt aPriority) : CTimer(aPriority)
sl@0
   111
//
sl@0
   112
// Constructor - Creates AND ADDS TO MYSCHEDULER
sl@0
   113
//	
sl@0
   114
	{
sl@0
   115
	ConstructL();
sl@0
   116
	myScheduler::Add(this);
sl@0
   117
	}
sl@0
   118
sl@0
   119
void myTimer::RunL()
sl@0
   120
//
sl@0
   121
// The timer stops the scheduler
sl@0
   122
//
sl@0
   123
	{
sl@0
   124
	myScheduler::Stop();
sl@0
   125
	test.Printf(_L("   Timer has stopped ActiveScheduler\n"));
sl@0
   126
	}
sl@0
   127
sl@0
   128
sl@0
   129
//
sl@0
   130
// CHeartbeat test code
sl@0
   131
//
sl@0
   132
class CTick : public CBase, public MBeating
sl@0
   133
	{
sl@0
   134
public:
sl@0
   135
	virtual void Beat();
sl@0
   136
	virtual void Synchronize();
sl@0
   137
	void Display();
sl@0
   138
	TInt iTicks;
sl@0
   139
	TTime iStartTime;
sl@0
   140
	TTime iTimes[4];
sl@0
   141
	};
sl@0
   142
void CTick::Beat()
sl@0
   143
	{
sl@0
   144
sl@0
   145
	test.Printf(_L("Tick\n"));
sl@0
   146
	iTimes[iTicks].HomeTime();
sl@0
   147
	if (++iTicks>=4)
sl@0
   148
		CActiveScheduler::Stop();
sl@0
   149
	}
sl@0
   150
void CTick::Synchronize()
sl@0
   151
	{
sl@0
   152
sl@0
   153
	test.Printf(_L("Sync tick to system clock\n"));
sl@0
   154
	iStartTime.HomeTime();
sl@0
   155
	iTicks=0;
sl@0
   156
	}
sl@0
   157
sl@0
   158
void PrintTime(const TDesC& aName, const TTime& aTime)
sl@0
   159
	{
sl@0
   160
	TDateTime dt(aTime.DateTime());
sl@0
   161
	test.Printf(_L("%S = %02d:%02d:%02d:%06d\n"),&aName,dt.Hour(),dt.Minute(),dt.Second(),dt.MicroSecond());
sl@0
   162
	}
sl@0
   163
sl@0
   164
void CTick::Display()
sl@0
   165
	{
sl@0
   166
	PrintTime(_L("Start time"),iStartTime);
sl@0
   167
	TInt i;
sl@0
   168
	for (i=0; i<4; i++)
sl@0
   169
		{
sl@0
   170
		TBuf<16> name;
sl@0
   171
		name.Format(_L("Beat %d"),i);
sl@0
   172
		PrintTime(name,iTimes[i]);
sl@0
   173
		}
sl@0
   174
	}
sl@0
   175
sl@0
   176
class CTock : public CTick
sl@0
   177
	{
sl@0
   178
public:
sl@0
   179
	virtual void Beat();
sl@0
   180
	virtual void Synchronize();
sl@0
   181
	};
sl@0
   182
sl@0
   183
void CTock::Beat()
sl@0
   184
	{
sl@0
   185
sl@0
   186
	iTimes[iTicks++].HomeTime();
sl@0
   187
	test.Printf(_L("Tock\n"));
sl@0
   188
	}
sl@0
   189
sl@0
   190
void CTock::Synchronize()
sl@0
   191
	{
sl@0
   192
sl@0
   193
	test.Printf(_L("Sync tock to system clock\n"));
sl@0
   194
	iStartTime.HomeTime();
sl@0
   195
	iTicks=0;
sl@0
   196
	}
sl@0
   197
sl@0
   198
class CBigTock : public CTick
sl@0
   199
	{
sl@0
   200
public:
sl@0
   201
	virtual void Beat();
sl@0
   202
	virtual void Synchronize();
sl@0
   203
	};
sl@0
   204
sl@0
   205
void CBigTock::Beat()
sl@0
   206
	{
sl@0
   207
sl@0
   208
	iTimes[iTicks++].HomeTime();
sl@0
   209
	test.Printf(_L("TOCK!\n"));
sl@0
   210
	}
sl@0
   211
sl@0
   212
void CBigTock::Synchronize()
sl@0
   213
	{
sl@0
   214
sl@0
   215
	test.Printf(_L("Sync TOCK to system clock\n"));
sl@0
   216
	iStartTime.HomeTime();
sl@0
   217
	iTicks=0;
sl@0
   218
	}
sl@0
   219
sl@0
   220
void testHeartbeat()
sl@0
   221
//
sl@0
   222
// Test CHeartBeat
sl@0
   223
//
sl@0
   224
	{
sl@0
   225
sl@0
   226
	test.Start(_L("Test CHeartbeat timer"));
sl@0
   227
	CActiveScheduler *scheduler = new CActiveScheduler;
sl@0
   228
	CActiveScheduler::Install(scheduler);
sl@0
   229
sl@0
   230
	test.Next(_L("Create a beating object synchronised at ETwelveOClock"));
sl@0
   231
	CTick *tick=new CTick;
sl@0
   232
	CHeartbeat *pH=NULL;
sl@0
   233
	TRAPD(r, pH=CHeartbeat::NewL(EPriorityNormal));
sl@0
   234
	test(r==KErrNone);
sl@0
   235
	test.Next(_L("Run for 4 beats on the second"));
sl@0
   236
	pH->Start(ETwelveOClock, tick);
sl@0
   237
	CActiveScheduler::Start();
sl@0
   238
	pH->Cancel();
sl@0
   239
	tick->Display();
sl@0
   240
sl@0
   241
	User::After(1000000);
sl@0
   242
	test.Next(_L("Create another heartbeat synchronised at ESixOClock"));
sl@0
   243
	CHeartbeat *pH6=CHeartbeat::New(EPriorityNormal);
sl@0
   244
	CTock *tock=new CTock;
sl@0
   245
	test.Next(_L("Start both"));
sl@0
   246
	pH->Start(ETwelveOClock, tick);
sl@0
   247
	pH6->Start(ESixOClock, tock);
sl@0
   248
	CActiveScheduler::Start();
sl@0
   249
	tick->Display();
sl@0
   250
	tock->Display();
sl@0
   251
sl@0
   252
	pH->Cancel();
sl@0
   253
	pH6->Cancel();
sl@0
   254
	User::After(1000000);
sl@0
   255
	test.Next(_L("Create another beating object synchronised at ESixOClock with a higher priority"));
sl@0
   256
	CHeartbeat *pH2=CHeartbeat::New(EPriorityHigh);
sl@0
   257
	CBigTock *bigtock=new CBigTock;
sl@0
   258
	test.Next(_L("Start all"));
sl@0
   259
	pH->Start(ETwelveOClock, tick);
sl@0
   260
	pH6->Start(ESixOClock, tock);
sl@0
   261
	pH2->Start(ESixOClock, bigtock);
sl@0
   262
	CActiveScheduler::Start();
sl@0
   263
	pH->Cancel();
sl@0
   264
	pH2->Cancel();
sl@0
   265
	pH6->Cancel();
sl@0
   266
	tick->Display();
sl@0
   267
	tock->Display();
sl@0
   268
	bigtock->Display();
sl@0
   269
sl@0
   270
	delete pH;
sl@0
   271
	delete pH2;
sl@0
   272
	delete pH6;
sl@0
   273
	delete tock;
sl@0
   274
	delete tick;
sl@0
   275
	delete bigtock;
sl@0
   276
	delete scheduler;
sl@0
   277
	test.End();
sl@0
   278
	}
sl@0
   279
sl@0
   280
void testLockSpec()
sl@0
   281
//
sl@0
   282
// test the operators defined for TTimerLockSpec
sl@0
   283
//
sl@0
   284
	{
sl@0
   285
/*
sl@0
   286
	test.Start(_L("Test pre fix operator ++"));
sl@0
   287
	TTimerLockSpec i=ETwelveOClock,k=EOneOClock,l;
sl@0
   288
	TInt j;
sl@0
   289
	for (j=0; j<30; j++)
sl@0
   290
		{
sl@0
   291
		++k=EOneOClock;
sl@0
   292
		test(k==EOneOClock);
sl@0
   293
		k=i;
sl@0
   294
		l=++i;
sl@0
   295
		switch (k)
sl@0
   296
			{
sl@0
   297
		case EOneOClock:
sl@0
   298
			test(i==ETwoOClock);
sl@0
   299
			test(l==ETwoOClock);
sl@0
   300
			break;
sl@0
   301
		case ETwoOClock:
sl@0
   302
			test(i==EThreeOClock);
sl@0
   303
			test(l==EThreeOClock);
sl@0
   304
			break;
sl@0
   305
		case EThreeOClock:
sl@0
   306
			test(i==EFourOClock);
sl@0
   307
			test(l==EFourOClock);
sl@0
   308
			break;
sl@0
   309
		case EFourOClock:
sl@0
   310
			test(i==EFiveOClock);
sl@0
   311
			test(l==EFiveOClock);
sl@0
   312
			break;
sl@0
   313
		case EFiveOClock:
sl@0
   314
			test(i==ESixOClock);
sl@0
   315
			test(l==ESixOClock);
sl@0
   316
			break;
sl@0
   317
		case ESixOClock:
sl@0
   318
			test(i==ESevenOClock);
sl@0
   319
			test(l==ESevenOClock);
sl@0
   320
			break;
sl@0
   321
		case ESevenOClock:
sl@0
   322
			test(i==EEightOClock);
sl@0
   323
			test(l==EEightOClock);
sl@0
   324
			break;
sl@0
   325
		case EEightOClock:
sl@0
   326
			test(i==ENineOClock);
sl@0
   327
			test(l==ENineOClock);
sl@0
   328
			break;
sl@0
   329
		case ENineOClock:
sl@0
   330
			test(i==ETenOClock);
sl@0
   331
			test(l==ETenOClock);
sl@0
   332
			break;
sl@0
   333
		case ETenOClock:
sl@0
   334
			test(i==EElevenOClock);
sl@0
   335
			test(l==EElevenOClock);
sl@0
   336
			break;
sl@0
   337
		case EElevenOClock:
sl@0
   338
			test(i==ETwelveOClock);
sl@0
   339
			test(l==ETwelveOClock);
sl@0
   340
			break;
sl@0
   341
		case ETwelveOClock:
sl@0
   342
			test(i==EOneOClock);
sl@0
   343
			test(l==EOneOClock);
sl@0
   344
			break;
sl@0
   345
			}
sl@0
   346
		}
sl@0
   347
sl@0
   348
	test.Next(_L("Test post fix operator ++"));
sl@0
   349
	for (j=0; j<30; j++)
sl@0
   350
		{
sl@0
   351
		++k=EOneOClock;
sl@0
   352
		test(k==EOneOClock);
sl@0
   353
		k=i;
sl@0
   354
		l=i++;
sl@0
   355
		switch (k)
sl@0
   356
			{
sl@0
   357
		case EOneOClock:
sl@0
   358
			test(i==ETwoOClock);
sl@0
   359
			test(l==k);
sl@0
   360
			break;
sl@0
   361
		case ETwoOClock:
sl@0
   362
			test(i==EThreeOClock);
sl@0
   363
			test(l==k);
sl@0
   364
			break;
sl@0
   365
		case EThreeOClock:
sl@0
   366
			test(i==EFourOClock);
sl@0
   367
			test(l==k);
sl@0
   368
			break;
sl@0
   369
		case EFourOClock:
sl@0
   370
			test(i==EFiveOClock);
sl@0
   371
			test(l==k);
sl@0
   372
			break;
sl@0
   373
		case EFiveOClock:
sl@0
   374
			test(i==ESixOClock);
sl@0
   375
			test(l==k);
sl@0
   376
			break;
sl@0
   377
		case ESixOClock:
sl@0
   378
			test(i==ESevenOClock);
sl@0
   379
			test(l==k);
sl@0
   380
			break;
sl@0
   381
		case ESevenOClock:
sl@0
   382
			test(i==EEightOClock);
sl@0
   383
			test(l==k);
sl@0
   384
			break;
sl@0
   385
		case EEightOClock:
sl@0
   386
			test(i==ENineOClock);
sl@0
   387
			test(l==k);
sl@0
   388
			break;
sl@0
   389
		case ENineOClock:
sl@0
   390
			test(i==ETenOClock);
sl@0
   391
			test(l==k);
sl@0
   392
			break;
sl@0
   393
		case ETenOClock:
sl@0
   394
			test(i==EElevenOClock);
sl@0
   395
			test(l==k);
sl@0
   396
			break;
sl@0
   397
		case EElevenOClock:
sl@0
   398
			test(i==ETwelveOClock);
sl@0
   399
			test(l==k);
sl@0
   400
			break;
sl@0
   401
		case ETwelveOClock:
sl@0
   402
			test(i==EOneOClock);
sl@0
   403
			test(l==k);
sl@0
   404
			break;
sl@0
   405
			}
sl@0
   406
		}
sl@0
   407
	test.End();
sl@0
   408
*/
sl@0
   409
	}
sl@0
   410
sl@0
   411
sl@0
   412
GLDEF_C TInt E32Main()
sl@0
   413
	{
sl@0
   414
	
sl@0
   415
	test.Title();
sl@0
   416
	__UHEAP_MARK;
sl@0
   417
	test.Start(_L("Create some CPeriodics"));
sl@0
   418
sl@0
   419
	myScheduler* pScheduler = new myScheduler;
sl@0
   420
	myScheduler::Install(pScheduler);
sl@0
   421
sl@0
   422
	pPer1 = CPeriodic::New(0);
sl@0
   423
	pPer2 = CPeriodic::NewL(0);
sl@0
   424
	pPer3 = CPeriodic::NewL(10);
sl@0
   425
	pPer4 = CPeriodic::NewL(100);
sl@0
   426
	pPer5 = CPeriodic::NewL(100);
sl@0
   427
	pPer6 = CPeriodic::NewL(100);
sl@0
   428
	pPer7 = CPeriodic::NewL(100);
sl@0
   429
	myTimer* pTimer = new myTimer(50);
sl@0
   430
sl@0
   431
	test.Next(_L("Start them"));
sl@0
   432
sl@0
   433
	TCallBack callBack1(CallBackFn,(TAny*)1);
sl@0
   434
	TCallBack callBack2(CallBackFn,(TAny*)2);
sl@0
   435
	TCallBack callBack3(CallBackFn,(TAny*)3);
sl@0
   436
	TCallBack callBack4(CallBackPanic,(TAny*)4);
sl@0
   437
	TCallBack callBack5(CallBackPanic,(TAny*)5);
sl@0
   438
	TCallBack callBack6(CallBackPanic,(TAny*)6);
sl@0
   439
	TCallBack callBack7(CallBackPanic,(TAny*)7);
sl@0
   440
sl@0
   441
	TInt p=0;
sl@0
   442
	HAL::Get(HAL::ESystemTickPeriod, p);
sl@0
   443
sl@0
   444
	User::After(p); // ensure tick does not occur while starting all these timers
sl@0
   445
sl@0
   446
	pPer1->Start(2*p+1,7*p+1,callBack1);	//After 3 ticks, complete every 8th tick
sl@0
   447
	pPer2->Start(1,    2*p+1,callBack2);	//After 1 tick , complete every 3rd tick
sl@0
   448
	pPer3->Start(7*p+1,  p+1,callBack3);	//After 8 ticks, complete every 2nd tick
sl@0
   449
sl@0
   450
	pPer4->Start(KMaxTInt,KMaxTInt,callBack4);
sl@0
   451
	pPer5->Start(60000000,60000000,callBack5);
sl@0
   452
	pPer6->Start(KMaxTInt/91,KMaxTInt/91,callBack6);
sl@0
   453
	pPer7->Start(KMaxTInt/91+1,KMaxTInt/91+1,callBack7);
sl@0
   454
	pTimer->After(20*p-1); // ensure there's enough time for them to fill up the array.
sl@0
   455
	/*
sl@0
   456
		Time	per1   per2	  per3
sl@0
   457
		  1				-
sl@0
   458
		  2
sl@0
   459
		  3		 -
sl@0
   460
		  4				-
sl@0
   461
		  5
sl@0
   462
		  6
sl@0
   463
		  7				-
sl@0
   464
		  8					   -
sl@0
   465
		  9
sl@0
   466
		 10				-	   -
sl@0
   467
		 11		 -			   
sl@0
   468
		 12					   -
sl@0
   469
		 13				-	   
sl@0
   470
		 14					   -
sl@0
   471
	*/
sl@0
   472
sl@0
   473
	myScheduler::Start();
sl@0
   474
sl@0
   475
	TInt i;
sl@0
   476
	for (i=0; i<counter; ++i)
sl@0
   477
		{
sl@0
   478
		test.Printf(_L("   Time: %7d Periodic: %d\n"),static_cast<TUint32>(Times[i].Int64()-Times[0].Int64()),Array[i]);
sl@0
   479
		}
sl@0
   480
sl@0
   481
	test(Array[0]==2);
sl@0
   482
	test(Array[1]==1);
sl@0
   483
	test(Array[2]==2);
sl@0
   484
	test(Array[3]==2);
sl@0
   485
	test(Array[4]==3);
sl@0
   486
	TBool normal56 = (Array[5]==3 && Array[6]==2);
sl@0
   487
	TBool reverse56 = (Array[5]==2 && Array[6]==3);
sl@0
   488
	if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) > 1)
sl@0
   489
		{
sl@0
   490
		// If there are multiple processors the order of 'simultaneous' timers is undefined since
sl@0
   491
		// the test may get to run as soon as the first timer is completed, instead of only after
sl@0
   492
		// the timer thread blocks, which would be after both timers completed.
sl@0
   493
		test(normal56 || reverse56);
sl@0
   494
		}
sl@0
   495
	else
sl@0
   496
		test(normal56);
sl@0
   497
	test(Array[7]==1);
sl@0
   498
	test(Array[8]==3);
sl@0
   499
	test(Array[9]==2);
sl@0
   500
	test(Array[10]==3);
sl@0
   501
sl@0
   502
	test.Next(_L("Destroy them"));
sl@0
   503
sl@0
   504
	delete pPer1;
sl@0
   505
	delete pPer2;
sl@0
   506
	delete pPer3;
sl@0
   507
	delete pPer4;
sl@0
   508
	delete pPer5;
sl@0
   509
	delete pPer6;
sl@0
   510
	delete pPer7;
sl@0
   511
	delete pTimer;
sl@0
   512
	delete pScheduler;
sl@0
   513
sl@0
   514
	test.Next(_L("Test CHeartbeat"));
sl@0
   515
	testHeartbeat();
sl@0
   516
	test.Next(_L("Test TTimerLockSpec"));
sl@0
   517
	testLockSpec();
sl@0
   518
	__UHEAP_MARKEND;
sl@0
   519
	test.End();
sl@0
   520
	return(KErrNone);
sl@0
   521
	}