Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\system\t_tock.cpp
16 // Test tick-based timers
20 // - Create a number of periodic timers, start each and change the time
21 // offset via TLocale::SetUniversalTimeOffset(). Print the results.
22 // - Create a number of periodic timers, start each and change the system
23 // time. Print the results.
24 // - Create a number of periodic timers, start each and change the system
25 // time in a second thread. Print the results.
26 // - Create a number of periodic timers, start each along with a background
27 // timer. Print the results.
28 // - Create a number of periodic timers, start each and change the system
29 // time and tick delay. Print the results.
30 // Platforms/Drives/Compatibility:
32 // Assumptions/Requirement/Pre-requisites:
33 // Failures and causes:
34 // Base Port information:
42 RTest test(_L("T_TOCK"));
47 TBool PauseOnError = 0;
48 #define GETCH() (PauseOnError&&test.Getch())
50 #define TEST(c) ((void)((c)||(test.Printf(_L("Failed at line %d\n"),__LINE__),GETCH(),test(0),0)))
51 #define CHECK(c) ((void)(((c)==0)||(test.Printf(_L("Error %d at line %d\n"),(c),__LINE__),GETCH(),test(0),0)))
53 const TPtrC KLddFileName=_L("D_TICK.LDD");
55 class CTickTest : public CActive
58 CTickTest(TInt aPriority, TInt aId, RTickTest aLdd);
61 virtual void DoCancel();
63 void StartPeriodic(TInt aInterval, TInt aCount);
64 void StartNShotRel(TInt aMin, TInt aRange, TInt aCount);
65 void StartNShotAbs(TInt aMin, TInt aRange, TInt aCount);
66 void StartNShotDelay(TInt aPeriod, TInt aDelay, TInt aCount);
67 void GetInfo(STickTestInfo& aInfo);
78 class CBackgroundTimer : public CActive
81 static CBackgroundTimer* NewL(TInt aPriority);
82 CBackgroundTimer(TInt aPriority);
85 virtual void DoCancel();
90 TRequestStatus iLongStatus;
93 CTickTest* TickTest[KMaxTimers];
95 CBackgroundTimer* BackgroundTimer;
97 CTickTest::CTickTest(TInt aPriority, TInt aId, RTickTest aLdd)
104 CTickTest::~CTickTest()
110 void CTickTest::StartPeriodic(TInt aInterval, TInt aCount)
112 TInt r=iLdd.StartPeriodic(iStatus,iId,aInterval,aCount);
115 TRequestStatus* pS=&iStatus;
116 User::RequestComplete(pS,r);
121 void CTickTest::StartNShotRel(TInt aMin, TInt aRange, TInt aCount)
132 iInfo.iMinErr=KMaxTInt;
133 iInfo.iMaxErr=KMinTInt;
135 iInfo.iRequestedCount=iCount;
137 TInt r=iLdd.StartNShotRel(iStatus,iId,aMin,aRange,c);
140 TRequestStatus* pS=&iStatus;
141 User::RequestComplete(pS,r);
146 void CTickTest::StartNShotAbs(TInt aMin, TInt aRange, TInt aCount)
148 TInt r=iLdd.StartNShotAbs(iStatus,iId,aMin,aRange,aCount);
151 TRequestStatus* pS=&iStatus;
152 User::RequestComplete(pS,r);
157 void CTickTest::StartNShotDelay(TInt aPeriod, TInt aDelay, TInt aCount)
160 TInt r=iLdd.StartNShotDelay(iStatus,iId,aPeriod,aDelay,aCount);
163 TRequestStatus* pS=&iStatus;
164 User::RequestComplete(pS,r);
169 void CTickTest::GetInfo(STickTestInfo& aInfo)
171 iLdd.GetInfo(iId,aInfo);
174 void CTickTest::RunL()
176 if (iStatus!=KErrNone)
178 test.Printf(_L("Timer %d error %d\n"),iId,iStatus.Int());
180 CActiveScheduler::Stop();
188 iLdd.GetInfo(iId,info);
189 TInt err=info.iMinErr;
190 if (err<iInfo.iMinErr)
192 if (err>iInfo.iMaxErr)
198 TInt r=iLdd.StartNShotRel(iStatus,iId,iMin,iRange,1);
201 TRequestStatus* pS=&iStatus;
202 User::RequestComplete(pS,r);
207 iInfo.iAvgErr=I64INT(iErrorAcc/TInt64(iInfo.iCount));
209 test.Printf(_L("Timer %d\n"),iId);
211 CActiveScheduler::Stop();
214 void CTickTest::DoCancel()
219 CBackgroundTimer::CBackgroundTimer(TInt aPriority)
224 CBackgroundTimer::~CBackgroundTimer()
230 void CBackgroundTimer::Start()
232 iLong.After(iLongStatus, 100000);
233 iShort.After(iStatus, 20000);
237 void CBackgroundTimer::RunL()
240 User::WaitForRequest(iLongStatus);
244 void CBackgroundTimer::DoCancel()
248 User::WaitForRequest(iLongStatus);
251 CBackgroundTimer* CBackgroundTimer::NewL(TInt aPriority)
253 CBackgroundTimer* pB=new (ELeave) CBackgroundTimer(aPriority);
254 TInt r=pB->iShort.CreateLocal();
256 r=pB->iLong.CreateLocal();
267 CActiveScheduler* pS=new (ELeave) CActiveScheduler;
268 CActiveScheduler::Install(pS);
270 for (i=0; i<KMaxTimers; ++i)
272 TickTest[i]=new (ELeave) CTickTest(0,i,ticktest);
273 CActiveScheduler::Add(TickTest[i]);
275 Idler=CIdle::NewL(-100);
276 BackgroundTimer=CBackgroundTimer::NewL(-10);
277 CActiveScheduler::Add(BackgroundTimer);
280 void PrintInfo(TInt aId, STickTestInfo& a)
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);
285 void PrintInfo(TInt aId)
287 PrintInfo(aId, TickTest[aId]->iInfo);
293 for (i=0; i<KMaxTimers; ++i)
297 TInt ChangeOffset(TAny*)
299 User::SetUTCOffset(3600);
300 User::SetUTCOffset(0);
301 return 1; // so we run again
304 TInt ChangeTime(TAny*)
308 User::SetUTCTime(now+TTimeIntervalSeconds(1000));
309 User::SetUTCTime(now);
310 return 1; // so we run again
313 TInt ChangeTimeThread(TAny*)
317 User::AfterHighRes(3000);
320 User::SetUTCTime(now+TTimeIntervalSeconds(1000));
321 User::AfterHighRes(1000);
322 User::SetUTCTime(now);
326 GLDEF_C TInt E32Main()
328 // Test tick-based timers
332 // test.SetLogged(EFalse);
335 test.Start(_L("Load test LDD"));
336 TInt r=User::LoadLogicalDevice(KLddFileName);
337 TEST(r==KErrNone || r==KErrAlreadyExists);
342 test.Next(_L("Create test objects"));
343 TRAP(r, InitialiseL());
346 test.Next(_L("Periodics with changing home time offset"));
347 TickTest[0]->StartPeriodic(3,1000);
348 TickTest[1]->StartPeriodic(5,600);
349 TickTest[2]->StartPeriodic(7,400);
350 TickTest[3]->StartPeriodic(11,300);
351 TickTest[4]->StartPeriodic(13,30);
352 TickTest[5]->StartPeriodic(19,30);
353 TickTest[6]->StartPeriodic(23,30);
354 TickTest[7]->StartPeriodic(37,30);
355 Idler->Start(TCallBack(ChangeOffset,NULL));
358 CActiveScheduler::Start();
362 test.Next(_L("Periodics with changing system time"));
363 TickTest[0]->StartPeriodic(3,1000);
364 TickTest[1]->StartPeriodic(5,600);
365 TickTest[2]->StartPeriodic(7,400);
366 TickTest[3]->StartPeriodic(11,300);
367 TickTest[4]->StartPeriodic(13,30);
368 TickTest[5]->StartPeriodic(19,30);
369 TickTest[6]->StartPeriodic(23,30);
370 TickTest[7]->StartPeriodic(37,30);
371 Idler->Start(TCallBack(ChangeTime,NULL));
374 CActiveScheduler::Start();
378 test.Next(_L("Periodics with changing system time in second thread"));
380 r=t.Create(KNullDesC(),ChangeTimeThread,0x1000,NULL,NULL);
382 t.SetPriority(EPriorityMore);
383 TickTest[0]->StartPeriodic(3,1000);
384 TickTest[1]->StartPeriodic(5,600);
385 TickTest[2]->StartPeriodic(7,400);
386 TickTest[3]->StartPeriodic(11,300);
387 TickTest[4]->StartPeriodic(13,30);
388 TickTest[5]->StartPeriodic(19,30);
389 TickTest[6]->StartPeriodic(23,30);
390 TickTest[7]->StartPeriodic(37,30);
391 Idler->Start(TCallBack(ChangeTime,NULL));
395 CActiveScheduler::Start();
400 User::WaitForRequest(s);
401 test(t.ExitType()==EExitKill);
402 test(t.ExitReason()==0);
407 test.Next(_L("Periodics with background timer"));
408 TickTest[0]->StartPeriodic(3,1000);
409 TickTest[1]->StartPeriodic(5,600);
410 TickTest[2]->StartPeriodic(7,400);
411 TickTest[3]->StartPeriodic(11,300);
412 TickTest[4]->StartPeriodic(13,30);
413 TickTest[5]->StartPeriodic(19,30);
414 TickTest[6]->StartPeriodic(23,30);
415 TickTest[7]->StartPeriodic(37,30);
416 BackgroundTimer->Start();
419 CActiveScheduler::Start();
420 BackgroundTimer->Cancel();
423 test.Next(_L("Periodics with changing system time and tick delay"));
424 TickTest[0]->StartPeriodic(3,1000);
425 TickTest[1]->StartPeriodic(5,600);
426 TickTest[2]->StartPeriodic(7,400);
427 TickTest[3]->StartPeriodic(11,300);
428 TickTest[4]->StartPeriodic(13,30);
429 TickTest[5]->StartPeriodic(19,30);
430 TickTest[6]->StartPeriodic(23,30);
431 TickTest[7]->StartNShotDelay(17,30,200);
432 Idler->Start(TCallBack(ChangeTime,NULL));
435 CActiveScheduler::Start();