First public contribution.
1 // Copyright (c) 1995-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\active\t_cper.cpp
16 // Test periodic timers.
18 // CPeriodic, CHeartbeat
20 // - Create some CPeriodic timer active objects with different priorities.
21 // - Start the periodic timers with varying delay time to start generation
22 // of first event and different intervals between events
23 // - Verify the callback functions associated with each periodic are called
24 // in order of the time when the event occurred and considering the priority
26 // - Create heartbeat timer with different priorities
27 // - Start one heartbeat synchronized at ETwelveOClock
28 // - Start two heartbeats synchronized at ETwelveOClock, ESixOClock
29 // - Start three heartbeats synchronized at ETwelveOClock, ESixOClock, ETwelveOClock
30 // - Display start time and beat time for each heartbeat timer
31 // - Check if the heap has been corrupted by all the tests.
32 // Platforms/Drives/Compatibility:
34 // Assumptions/Requirement/Pre-requisites:
35 // Failures and causes:
36 // - The first part of the test (for CPeriodic) will fail if the timers are not completed in order.
37 // The test on emulator is very sensitive on the background activities on PC.
38 // Base Port information:
43 #include <e32base_private.h>
50 LOCAL_D RTest test(_L("T_CPER"));
52 class myScheduler: public CActiveScheduler
55 virtual void Error(TInt anError) const;
58 void myScheduler::Error(TInt anError) const
60 // virtual error handler
63 test.Panic(anError,_L("myScheduler::Error"));
78 TInt CallBackFn(TAny* Ptr)
80 // Callback function used for all periodics
81 // On calling Ptr is actually a TInt - the periodic Id
86 Array[counter] = (TInt)Ptr;
87 Times[counter].HomeTime();
93 TInt CallBackPanic(TAny* Ptr)
95 // Periodic should never get called
98 test.Printf(_L(" PERIODIC %d HAS GONE OFF!\n"),(TInt)Ptr);
103 class myTimer: public CTimer
106 myTimer(TInt aPriority);
110 myTimer::myTimer(TInt aPriority) : CTimer(aPriority)
112 // Constructor - Creates AND ADDS TO MYSCHEDULER
116 myScheduler::Add(this);
121 // The timer stops the scheduler
125 test.Printf(_L(" Timer has stopped ActiveScheduler\n"));
130 // CHeartbeat test code
132 class CTick : public CBase, public MBeating
136 virtual void Synchronize();
145 test.Printf(_L("Tick\n"));
146 iTimes[iTicks].HomeTime();
148 CActiveScheduler::Stop();
150 void CTick::Synchronize()
153 test.Printf(_L("Sync tick to system clock\n"));
154 iStartTime.HomeTime();
158 void PrintTime(const TDesC& aName, const TTime& aTime)
160 TDateTime dt(aTime.DateTime());
161 test.Printf(_L("%S = %02d:%02d:%02d:%06d\n"),&aName,dt.Hour(),dt.Minute(),dt.Second(),dt.MicroSecond());
164 void CTick::Display()
166 PrintTime(_L("Start time"),iStartTime);
171 name.Format(_L("Beat %d"),i);
172 PrintTime(name,iTimes[i]);
176 class CTock : public CTick
180 virtual void Synchronize();
186 iTimes[iTicks++].HomeTime();
187 test.Printf(_L("Tock\n"));
190 void CTock::Synchronize()
193 test.Printf(_L("Sync tock to system clock\n"));
194 iStartTime.HomeTime();
198 class CBigTock : public CTick
202 virtual void Synchronize();
205 void CBigTock::Beat()
208 iTimes[iTicks++].HomeTime();
209 test.Printf(_L("TOCK!\n"));
212 void CBigTock::Synchronize()
215 test.Printf(_L("Sync TOCK to system clock\n"));
216 iStartTime.HomeTime();
226 test.Start(_L("Test CHeartbeat timer"));
227 CActiveScheduler *scheduler = new CActiveScheduler;
228 CActiveScheduler::Install(scheduler);
230 test.Next(_L("Create a beating object synchronised at ETwelveOClock"));
231 CTick *tick=new CTick;
233 TRAPD(r, pH=CHeartbeat::NewL(EPriorityNormal));
235 test.Next(_L("Run for 4 beats on the second"));
236 pH->Start(ETwelveOClock, tick);
237 CActiveScheduler::Start();
241 User::After(1000000);
242 test.Next(_L("Create another heartbeat synchronised at ESixOClock"));
243 CHeartbeat *pH6=CHeartbeat::New(EPriorityNormal);
244 CTock *tock=new CTock;
245 test.Next(_L("Start both"));
246 pH->Start(ETwelveOClock, tick);
247 pH6->Start(ESixOClock, tock);
248 CActiveScheduler::Start();
254 User::After(1000000);
255 test.Next(_L("Create another beating object synchronised at ESixOClock with a higher priority"));
256 CHeartbeat *pH2=CHeartbeat::New(EPriorityHigh);
257 CBigTock *bigtock=new CBigTock;
258 test.Next(_L("Start all"));
259 pH->Start(ETwelveOClock, tick);
260 pH6->Start(ESixOClock, tock);
261 pH2->Start(ESixOClock, bigtock);
262 CActiveScheduler::Start();
282 // test the operators defined for TTimerLockSpec
286 test.Start(_L("Test pre fix operator ++"));
287 TTimerLockSpec i=ETwelveOClock,k=EOneOClock,l;
302 test(i==EThreeOClock);
303 test(l==EThreeOClock);
306 test(i==EFourOClock);
307 test(l==EFourOClock);
310 test(i==EFiveOClock);
311 test(l==EFiveOClock);
318 test(i==ESevenOClock);
319 test(l==ESevenOClock);
322 test(i==EEightOClock);
323 test(l==EEightOClock);
326 test(i==ENineOClock);
327 test(l==ENineOClock);
334 test(i==EElevenOClock);
335 test(l==EElevenOClock);
338 test(i==ETwelveOClock);
339 test(l==ETwelveOClock);
348 test.Next(_L("Test post fix operator ++"));
362 test(i==EThreeOClock);
366 test(i==EFourOClock);
370 test(i==EFiveOClock);
378 test(i==ESevenOClock);
382 test(i==EEightOClock);
386 test(i==ENineOClock);
394 test(i==EElevenOClock);
398 test(i==ETwelveOClock);
412 GLDEF_C TInt E32Main()
417 test.Start(_L("Create some CPeriodics"));
419 myScheduler* pScheduler = new myScheduler;
420 myScheduler::Install(pScheduler);
422 pPer1 = CPeriodic::New(0);
423 pPer2 = CPeriodic::NewL(0);
424 pPer3 = CPeriodic::NewL(10);
425 pPer4 = CPeriodic::NewL(100);
426 pPer5 = CPeriodic::NewL(100);
427 pPer6 = CPeriodic::NewL(100);
428 pPer7 = CPeriodic::NewL(100);
429 myTimer* pTimer = new myTimer(50);
431 test.Next(_L("Start them"));
433 TCallBack callBack1(CallBackFn,(TAny*)1);
434 TCallBack callBack2(CallBackFn,(TAny*)2);
435 TCallBack callBack3(CallBackFn,(TAny*)3);
436 TCallBack callBack4(CallBackPanic,(TAny*)4);
437 TCallBack callBack5(CallBackPanic,(TAny*)5);
438 TCallBack callBack6(CallBackPanic,(TAny*)6);
439 TCallBack callBack7(CallBackPanic,(TAny*)7);
442 HAL::Get(HAL::ESystemTickPeriod, p);
444 User::After(p); // ensure tick does not occur while starting all these timers
446 pPer1->Start(2*p+1,7*p+1,callBack1); //After 3 ticks, complete every 8th tick
447 pPer2->Start(1, 2*p+1,callBack2); //After 1 tick , complete every 3rd tick
448 pPer3->Start(7*p+1, p+1,callBack3); //After 8 ticks, complete every 2nd tick
450 pPer4->Start(KMaxTInt,KMaxTInt,callBack4);
451 pPer5->Start(60000000,60000000,callBack5);
452 pPer6->Start(KMaxTInt/91,KMaxTInt/91,callBack6);
453 pPer7->Start(KMaxTInt/91+1,KMaxTInt/91+1,callBack7);
454 pTimer->After(20*p-1); // ensure there's enough time for them to fill up the array.
473 myScheduler::Start();
476 for (i=0; i<counter; ++i)
478 test.Printf(_L(" Time: %7d Periodic: %d\n"),static_cast<TUint32>(Times[i].Int64()-Times[0].Int64()),Array[i]);
486 TBool normal56 = (Array[5]==3 && Array[6]==2);
487 TBool reverse56 = (Array[5]==2 && Array[6]==3);
488 if (UserSvr::HalFunction(EHalGroupKernel, EKernelHalNumLogicalCpus, 0, 0) > 1)
490 // If there are multiple processors the order of 'simultaneous' timers is undefined since
491 // the test may get to run as soon as the first timer is completed, instead of only after
492 // the timer thread blocks, which would be after both timers completed.
493 test(normal56 || reverse56);
502 test.Next(_L("Destroy them"));
514 test.Next(_L("Test CHeartbeat"));
516 test.Next(_L("Test TTimerLockSpec"));