|
1 // Copyright (c) 2008-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 "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 // |
|
15 |
|
16 /** |
|
17 @file |
|
18 @test |
|
19 @internalComponent |
|
20 */ |
|
21 |
|
22 |
|
23 #include "utils.h" |
|
24 #include "panic.h" |
|
25 |
|
26 /************************************************************************************* |
|
27 class TRnd |
|
28 */ |
|
29 |
|
30 /** |
|
31 Math::Rand() degenerates to approx seed*3.3+38 for the first numbers generated |
|
32 with a low seed, which isn't very random. To illustrate: The random numbers generated |
|
33 by the seeds {0..9} are {38, 41, 45, 48, 51, 54, 58, 61, 64, 68}. |
|
34 */ |
|
35 const TInt64 KMinSeed = 1000000000; |
|
36 |
|
37 TInt64 TRnd::iSeed = KMinSeed; // Need to change CComparison::StartL() to use this value |
|
38 |
|
39 CExecutionContext::CLogMediator* TRnd::iMediator = NULL; |
|
40 CExecutionContext* TRnd::iExecutionContext = NULL; |
|
41 |
|
42 TInt64 TRnd::Seed() |
|
43 { |
|
44 return iSeed; |
|
45 } |
|
46 |
|
47 void TRnd::SetSeed(TInt64 aSeed) |
|
48 { |
|
49 iSeed = aSeed; |
|
50 } |
|
51 |
|
52 void TRnd::SetLogMediator(CExecutionContext::CLogMediator* aMediator) |
|
53 { |
|
54 iMediator = aMediator; |
|
55 } |
|
56 |
|
57 void TRnd::SetExecutionContext(CExecutionContext* aExecutionContext) |
|
58 { |
|
59 iExecutionContext = aExecutionContext; |
|
60 } |
|
61 |
|
62 |
|
63 TInt TRnd::GenRand(TInt x) |
|
64 { |
|
65 TInt res = Math::Rand( iSeed ); |
|
66 if (x) |
|
67 { |
|
68 res = res % x; |
|
69 } |
|
70 return res; |
|
71 } |
|
72 |
|
73 TInt TRnd::rnd(TInt x) |
|
74 { |
|
75 TInt res; |
|
76 |
|
77 switch ( iExecutionContext->ContextMode() ) |
|
78 { |
|
79 case CExecutionContext::ECtxPlayback: |
|
80 res = iMediator->PlaybackInt(); |
|
81 break; |
|
82 |
|
83 case CExecutionContext::ECtxRandomAndRecord: |
|
84 res = GenRand(x); |
|
85 iMediator->RecordInt( res ); |
|
86 break; |
|
87 |
|
88 case CExecutionContext::ECtxRandom: // fallthrough |
|
89 default: |
|
90 res = GenRand(x); |
|
91 break; |
|
92 } |
|
93 |
|
94 return res; |
|
95 } |
|
96 |
|
97 /************************************************************************************* |
|
98 class CExecutionContext |
|
99 */ |
|
100 TInt CExecutionContext::CLogMediator::PlaybackInt() |
|
101 { |
|
102 |
|
103 TInt res = 0; |
|
104 |
|
105 if ( !iStalled ) |
|
106 { |
|
107 |
|
108 TBool failed = ETrue; |
|
109 |
|
110 TBuf8<1024> logRec; |
|
111 |
|
112 TInt readRes; |
|
113 |
|
114 readRes = iLog.Read( logRec, 3 ); // 'Num' |
|
115 |
|
116 |
|
117 |
|
118 if ( readRes == KErrNone && logRec == _L8("Num") ) |
|
119 { |
|
120 iLog.Read( logRec, 16 ); // EntryNo, 16 characters |
|
121 TLex8 strLex; |
|
122 strLex.Assign( logRec ); |
|
123 TInt64 readVal64 = 0; |
|
124 readRes = strLex.Val( readVal64, EDecimal ); |
|
125 if ( KErrNone == readRes && readVal64 == iEntryNo ) |
|
126 { |
|
127 |
|
128 readRes = iLog.Read( logRec, 3 ); // ' = ' |
|
129 |
|
130 if ( readRes == KErrNone && logRec == _L8(" = ") ) |
|
131 { |
|
132 |
|
133 readRes = iLog.Read( logRec, 13 ); // value 12 + eol |
|
134 strLex.Assign( logRec ); |
|
135 |
|
136 TUint32 readVal32; |
|
137 |
|
138 readRes = strLex.Val( readVal32, EDecimal ); |
|
139 |
|
140 if ( KErrNone == readRes ) |
|
141 { // finally we got a number |
|
142 |
|
143 res = (TInt) readVal32; |
|
144 |
|
145 failed = EFalse; |
|
146 } |
|
147 |
|
148 } |
|
149 } |
|
150 |
|
151 } |
|
152 |
|
153 if ( failed ) |
|
154 { |
|
155 iExecutionContext.MediatorEmptied( this ); |
|
156 res = 0; |
|
157 iStalled = ETrue; |
|
158 } |
|
159 |
|
160 } |
|
161 |
|
162 iEntryNo++; |
|
163 |
|
164 return res; |
|
165 } |
|
166 |
|
167 void CExecutionContext::CLogMediator::RecordInt(TInt aIntToBeRecorded) |
|
168 { |
|
169 if ( !iStalled ) |
|
170 { |
|
171 TBuf8<1024> logRec; |
|
172 logRec.Format( _L8("Num%016Ld = %012d\n"), iEntryNo, aIntToBeRecorded ); |
|
173 iLog.Write( logRec ); |
|
174 } |
|
175 |
|
176 iEntryNo++; |
|
177 } |
|
178 |
|
179 CExecutionContext::CLogMediator::CLogMediator(CExecutionContext& aExecutionContext): |
|
180 iExecutionContext( aExecutionContext ) |
|
181 { } |
|
182 |
|
183 CExecutionContext::CLogMediator::~CLogMediator() |
|
184 { |
|
185 iLog.Close(); |
|
186 iExecutionContext.MediatorDestroyed ( this ); |
|
187 } |
|
188 |
|
189 void CExecutionContext::CLogMediator::ConstructL( RFs& aFs, const TDesC& aFileName ) |
|
190 { |
|
191 TInt fileErr; |
|
192 |
|
193 switch ( iExecutionContext.ContextMode() ) |
|
194 { |
|
195 case CExecutionContext::ECtxPlayback: |
|
196 fileErr = iLog.Open( aFs, aFileName, EFileRead ); |
|
197 break; |
|
198 |
|
199 case CExecutionContext::ECtxRandomAndRecord: |
|
200 fileErr = iLog.Create( aFs, aFileName, EFileWrite ); |
|
201 if(fileErr == KErrAlreadyExists) |
|
202 { |
|
203 fileErr = iLog.Replace( aFs, aFileName, EFileWrite ); |
|
204 } |
|
205 else if(fileErr == KErrPathNotFound) |
|
206 { |
|
207 User::LeaveIfError(aFs.CreatePrivatePath(EDriveC)); |
|
208 fileErr = iLog.Create( aFs, aFileName, EFileWrite ); |
|
209 } |
|
210 __ASSERT_ALWAYS(KErrNone == fileErr, User::Panic(_L("t_stress utils.cpp"), EPanic2)); |
|
211 break; |
|
212 |
|
213 default: |
|
214 fileErr = KErrGeneral; |
|
215 break; |
|
216 } |
|
217 |
|
218 User::LeaveIfError( fileErr ); |
|
219 |
|
220 } |
|
221 |
|
222 CExecutionContext::CLogMediator* CExecutionContext::CLogMediator::NewLC(CExecutionContext& aExecutionContext, RFs& aFs, const TDesC& aFileName ) |
|
223 { |
|
224 CExecutionContext::CLogMediator* self = new (ELeave) CExecutionContext::CLogMediator( aExecutionContext ); |
|
225 CleanupStack::PushL ( self ); |
|
226 self->ConstructL( aFs, aFileName ); |
|
227 return self; |
|
228 } |
|
229 |
|
230 CExecutionContext* CExecutionContext::NewL(CExecutionContext::TExecutionMode aExecutionMode, CTestExecWatchCat& aWatchCat) |
|
231 { |
|
232 CExecutionContext* self = new (ELeave) CExecutionContext( aExecutionMode, aWatchCat ); |
|
233 CleanupStack::PushL ( self ); |
|
234 self->ConstructL( ); |
|
235 CleanupStack::Pop( self ); |
|
236 return self; |
|
237 } |
|
238 |
|
239 CExecutionContext::TExecutionMode CExecutionContext::ContextMode() |
|
240 { |
|
241 return iContextMode; |
|
242 } |
|
243 |
|
244 CExecutionContext::CLogMediator& CExecutionContext::CreateLogMediatorL(const TDesC& aLogName) |
|
245 { |
|
246 CExecutionContext::CLogMediator* mediator = CExecutionContext::CLogMediator::NewLC( *this, iFs, aLogName ); |
|
247 iMediators.AppendL( mediator ); |
|
248 CleanupStack::Pop(mediator); |
|
249 return *mediator; |
|
250 } |
|
251 |
|
252 void CExecutionContext::MediatorDestroyed(CLogMediator* aMediator) |
|
253 { |
|
254 TInt pos = iMediators.Find( aMediator ); |
|
255 iMediators.Remove ( pos ); |
|
256 } |
|
257 |
|
258 void CExecutionContext::MediatorEmptied(CLogMediator* aMediator) |
|
259 { |
|
260 (void)aMediator; |
|
261 iWatchCat.ExecutionContextRunOut( this ); |
|
262 } |
|
263 |
|
264 |
|
265 void CExecutionContext::ConstructL() |
|
266 { |
|
267 TInt conResult = iFs.Connect(); |
|
268 User::LeaveIfError( conResult ); |
|
269 } |
|
270 |
|
271 |
|
272 CExecutionContext::CExecutionContext(CExecutionContext::TExecutionMode aExecutionMode, CTestExecWatchCat& aWatchCat): |
|
273 iWatchCat(aWatchCat), |
|
274 iContextMode(aExecutionMode) |
|
275 { } // empty |
|
276 |
|
277 CExecutionContext::~CExecutionContext() |
|
278 { |
|
279 while ( iMediators.Count() > 0 ) |
|
280 { |
|
281 delete iMediators[0]; // this will remove the mediator from the list |
|
282 } |
|
283 |
|
284 iMediators.Close(); |
|
285 iFs.Close(); |
|
286 } |
|
287 |
|
288 /************************************************************************************* |
|
289 class CTestExecWatchCat |
|
290 */ |
|
291 CTestExecWatchCat* CTestExecWatchCat::NewL(CExecutionContext::TExecutionMode aExecutionMode) |
|
292 { |
|
293 CTestExecWatchCat* self = new (ELeave) CTestExecWatchCat(); |
|
294 CleanupStack::PushL( self ); |
|
295 self->ConstructL( aExecutionMode ); |
|
296 |
|
297 // finally hook into TRnd |
|
298 TRnd::SetExecutionContext(self->iExecutionContext); |
|
299 |
|
300 CleanupStack::Pop(self); |
|
301 return self; |
|
302 } |
|
303 |
|
304 void CTestExecWatchCat::ConstructL( CExecutionContext::TExecutionMode aExecutionMode ) |
|
305 { |
|
306 iExecutionContext = CExecutionContext::NewL( aExecutionMode, *this ); |
|
307 } |
|
308 |
|
309 |
|
310 CTestExecWatchCat::CTestExecWatchCat() |
|
311 { } // empty |
|
312 |
|
313 CTestExecWatchCat::~CTestExecWatchCat() |
|
314 { |
|
315 delete iExecutionContext; |
|
316 } |
|
317 |
|
318 void CTestExecWatchCat::ExecutionContextRunOut(CExecutionContext* /*aContext*/) |
|
319 { } |
|
320 |
|
321 /** |
|
322 Sets the path where the logging file will be created, then constructs the mediator |
|
323 */ |
|
324 void CTestExecWatchCat::SetLoggingPathL(const TDesC& aPath) |
|
325 { |
|
326 RBuf path; |
|
327 path.CleanupClosePushL(); |
|
328 path.CreateL(aPath.Length() + 16); |
|
329 if (aPath.Length()) |
|
330 { |
|
331 path.Copy(aPath); |
|
332 if (aPath[aPath.Length()-1] != '\\') |
|
333 { |
|
334 path.Append('\\'); |
|
335 } |
|
336 } |
|
337 path.Append(KLogFileName); |
|
338 // now construct log mediator |
|
339 CExecutionContext::CLogMediator& mediator = iExecutionContext->CreateLogMediatorL( path ); |
|
340 TRnd::SetLogMediator(&mediator); |
|
341 CleanupStack::PopAndDestroy(&path); |
|
342 } |
|
343 |
|
344 /************************************************************************************* |
|
345 class TTickUtils |
|
346 */ |
|
347 TUint32 TTickUtils::CalcTickDelta(TUint32 tick1, TUint32 tick2) |
|
348 { |
|
349 TInt32 res; |
|
350 if ( tick1 > tick2 ) |
|
351 { |
|
352 res = tick1 - tick2; |
|
353 } |
|
354 else |
|
355 { |
|
356 res = tick2 - tick1; |
|
357 } |
|
358 return res; |
|
359 } |