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