1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This module contains the implementation of |
|
15 * CTestModuleDemo class member functions that does the actual |
|
16 * tests. |
|
17 * |
|
18 */ |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <e32math.h> |
|
22 #include "DemoModule.h" |
|
23 |
|
24 // EXTERNAL DATA STRUCTURES |
|
25 // None |
|
26 |
|
27 // EXTERNAL FUNCTION PROTOTYPES |
|
28 // None |
|
29 |
|
30 // CONSTANTS |
|
31 // None |
|
32 |
|
33 // MACROS |
|
34 // None |
|
35 |
|
36 // LOCAL CONSTANTS AND MACROS |
|
37 // None |
|
38 |
|
39 // MODULE DATA STRUCTURES |
|
40 // None |
|
41 |
|
42 // LOCAL FUNCTION PROTOTYPES |
|
43 // None |
|
44 |
|
45 // FORWARD DECLARATIONS |
|
46 // None |
|
47 |
|
48 // ==================== LOCAL FUNCTIONS ======================================= |
|
49 |
|
50 // ============================ MEMBER FUNCTIONS =============================== |
|
51 |
|
52 /* |
|
53 ------------------------------------------------------------------------------- |
|
54 |
|
55 Class: CDemoModule |
|
56 |
|
57 Method: Case |
|
58 |
|
59 Description: Returns a test case by number. |
|
60 |
|
61 This function contains an array of all available test cases |
|
62 i.e pair of case name and test function. If case specified by parameter |
|
63 aCaseNumber is found from array, then that item is returned. |
|
64 |
|
65 The reason for this rather complicated function is to specify all the |
|
66 test cases only in one place. It is not necessary to understand how |
|
67 function pointers to class member functions works when adding new test |
|
68 cases. See function body for instructions how to add new test case. |
|
69 |
|
70 Parameters: const TInt aCaseNumber :in: Test case number |
|
71 |
|
72 Return Values: const TCaseInfo Struct containing case name & function |
|
73 |
|
74 Errors/Exceptions: None |
|
75 |
|
76 Status: Proposal |
|
77 |
|
78 ------------------------------------------------------------------------------- |
|
79 */ |
|
80 const TCaseInfo CDemoModule::Case ( |
|
81 const TInt aCaseNumber ) const |
|
82 { |
|
83 |
|
84 /* |
|
85 * To add new test cases, implement new test case function and add new |
|
86 * line to KCases array specify the name of the case and the function |
|
87 * doing the test case |
|
88 * In practice, do following |
|
89 * |
|
90 * 1) Make copy of existing test case function and change its name |
|
91 * and functionality. Note that the function must be added to |
|
92 * OOMHard.cpp file and to OOMHard.h |
|
93 * header file. |
|
94 * |
|
95 * 2) Add entry to following KCases array either by using: |
|
96 * |
|
97 * 2.1: FUNCENTRY or ENTRY macro |
|
98 * ENTRY macro takes two parameters: test case name and test case |
|
99 * function name. |
|
100 * |
|
101 * FUNCENTRY macro takes only test case function name as a parameter and |
|
102 * uses that as a test case name and test case function name. |
|
103 * |
|
104 * Or |
|
105 * |
|
106 * 2.2: OOM_FUNCENTRY or OOM_ENTRY macro. Note that these macros are used |
|
107 * only with OOM (Out-Of-Memory) testing! |
|
108 * |
|
109 * OOM_ENTRY macro takes five parameters: test case name, test case |
|
110 * function name, TBool which specifies is method supposed to be run using |
|
111 * OOM conditions, TInt value for first heap memory allocation failure and |
|
112 * TInt value for last heap memory allocation failure. |
|
113 * |
|
114 * OOM_FUNCENTRY macro takes test case function name as a parameter and uses |
|
115 * that as a test case name, TBool which specifies is method supposed to be |
|
116 * run using OOM conditions, TInt value for first heap memory allocation |
|
117 * failure and TInt value for last heap memory allocation failure. |
|
118 */ |
|
119 |
|
120 static TCaseInfoInternal const KCases[] = |
|
121 { |
|
122 // To add new test cases, add new items to this array |
|
123 |
|
124 // NOTE: When compiled to GCCE, there must be Classname:: |
|
125 // declaration in front of the method name, e.g. |
|
126 // CDemoModule::PrintTest. Otherwise the compiler |
|
127 // gives errors. |
|
128 |
|
129 ENTRY( "Loop test", CDemoModule::LoopTest ), |
|
130 ENTRY( "Simple math test", CDemoModule::SimpleMathTest ), |
|
131 ENTRY( "Math test", CDemoModule::MathTest ), |
|
132 ENTRY( "Print test", CDemoModule::PrintTest ), |
|
133 // Example how to use OOM functionality |
|
134 OOM_ENTRY( "Heap memory allocation with OOM (aborts)", CDemoModule::HeapMemoryAllocation, ETrue, 1, 2 ), |
|
135 ENTRY( "Heap memory allocation (passes)", CDemoModule::HeapMemoryAllocation ) |
|
136 }; |
|
137 |
|
138 // Verify that case number is valid |
|
139 if( (TUint) aCaseNumber >= sizeof( KCases ) / |
|
140 sizeof( TCaseInfoInternal ) ) |
|
141 { |
|
142 |
|
143 // Invalid case, construct empty object |
|
144 TCaseInfo null( (const TText*) L"" ); |
|
145 null.iMethod = NULL; |
|
146 null.iIsOOMTest = EFalse; |
|
147 null.iFirstMemoryAllocation = 0; |
|
148 null.iLastMemoryAllocation = 0; |
|
149 return null; |
|
150 |
|
151 } |
|
152 |
|
153 // Construct TCaseInfo object and return it |
|
154 TCaseInfo tmp ( KCases[ aCaseNumber ].iCaseName ); |
|
155 tmp.iMethod = KCases[ aCaseNumber ].iMethod; |
|
156 tmp.iIsOOMTest = KCases[ aCaseNumber ].iIsOOMTest; |
|
157 tmp.iFirstMemoryAllocation = KCases[ aCaseNumber ].iFirstMemoryAllocation; |
|
158 tmp.iLastMemoryAllocation = KCases[ aCaseNumber ].iLastMemoryAllocation; |
|
159 return tmp; |
|
160 |
|
161 } |
|
162 |
|
163 /* |
|
164 ------------------------------------------------------------------------------- |
|
165 |
|
166 Class: CDemoModule |
|
167 |
|
168 Method: SimpleMathTest |
|
169 |
|
170 Description: Simple Math testing. |
|
171 |
|
172 Parameters: TTestResult& aErrorDescription: out: |
|
173 Test result and on error case a short description of error |
|
174 |
|
175 Return Values: TInt: Always KErrNone to indicate that test was valid |
|
176 |
|
177 Errors/Exceptions: None |
|
178 |
|
179 Status: Approved |
|
180 |
|
181 ------------------------------------------------------------------------------- |
|
182 */ |
|
183 TInt CDemoModule::SimpleMathTest( TTestResult& aResult ) |
|
184 { |
|
185 /* Test with mathematic calculations and printing */ |
|
186 _LIT( KDefinion , "RunTestCase"); |
|
187 _LIT( KData , "Simple Math calculations"); |
|
188 TestModuleIf().Printf( 0, KDefinion, KData ); |
|
189 |
|
190 // Random number. |
|
191 TReal num = Math::Random(); |
|
192 // Intermediate result |
|
193 TReal sqrt = 0; |
|
194 // Result |
|
195 TReal result = 0; |
|
196 |
|
197 // Take square root of the number |
|
198 TInt ret = Math::Sqrt( sqrt, num ); |
|
199 if( ret != KErrNone ) |
|
200 { |
|
201 // Return error if Sqrt failed |
|
202 _LIT( KResult ,"Math::Sqrt failed"); |
|
203 aResult.iResultDes.Copy( KResult ); |
|
204 aResult.iResult = KErrGeneral; |
|
205 |
|
206 // Case was executed but failed |
|
207 return KErrNone; |
|
208 } |
|
209 |
|
210 // Take power of two from the previous result |
|
211 ret = Math::Pow( result, sqrt, 2 ); |
|
212 if( ret != KErrNone ) |
|
213 { |
|
214 // Return error if Pow failed |
|
215 _LIT( KResult ,"Math::Pow failed"); |
|
216 aResult.iResultDes.Copy( KResult ); |
|
217 aResult.iResult = KErrGeneral; |
|
218 |
|
219 // Case was executed but failed |
|
220 return KErrNone; |
|
221 } |
|
222 |
|
223 // Compare final result to the original value, |
|
224 // rounded values should usually be same. |
|
225 TInt32 a1; |
|
226 TInt32 a2; |
|
227 TInt r = Math::Int(a1, result); |
|
228 |
|
229 if (r != KErrNone ) |
|
230 { |
|
231 _LIT( KResult ,"1st conversion failed"); |
|
232 aResult.iResultDes.Copy( KResult ); |
|
233 aResult.iResult = r; |
|
234 return KErrNone; |
|
235 } |
|
236 |
|
237 r = Math::Int(a2, num); |
|
238 if (r != KErrNone ) |
|
239 { |
|
240 _LIT( KResult ,"2nd conversion failed"); |
|
241 aResult.iResultDes.Copy( KResult ); |
|
242 aResult.iResult = r; |
|
243 return KErrNone; |
|
244 } |
|
245 |
|
246 if( a1 != a2 ) |
|
247 { |
|
248 _LIT( KResult ,"Calculation doesn't match"); |
|
249 // Return error if comparison failed |
|
250 aResult.iResultDes.Copy( KResult ); |
|
251 aResult.iResult = KErrGeneral; |
|
252 // Case was executed but failed |
|
253 return KErrNone; |
|
254 } |
|
255 |
|
256 |
|
257 // Test case passed |
|
258 |
|
259 // Sets test case result and description(Maximum size is KStifMaxResultDes) |
|
260 _LIT( KDescription , "Test case passed"); |
|
261 aResult.SetResult( KErrNone, KDescription ); |
|
262 //aResult.iResult = KErrNone; |
|
263 //aResult.iResultDes = KDescription; |
|
264 |
|
265 // Case was executed |
|
266 return KErrNone; |
|
267 |
|
268 } |
|
269 |
|
270 /* |
|
271 ------------------------------------------------------------------------------- |
|
272 |
|
273 Class: CDemoModule |
|
274 |
|
275 Method: MathTest |
|
276 |
|
277 Description: Print loop test. Actually does not test anything, just |
|
278 demonstrate how to print. Test is always pased. |
|
279 |
|
280 Parameters: TTestResult& aErrorDescription: out: |
|
281 Test result and on error case a short description of error |
|
282 |
|
283 Return Values: TInt: Always KErrNone to indicate that test was valid |
|
284 |
|
285 Errors/Exceptions: None |
|
286 |
|
287 Status: Approved |
|
288 |
|
289 ------------------------------------------------------------------------------- |
|
290 */ |
|
291 TInt CDemoModule::LoopTest( TTestResult& aResult ) |
|
292 { |
|
293 |
|
294 /* Simple print and wait loop */ |
|
295 _LIT( KDefinion ,"State"); |
|
296 _LIT( KData ,"Looping"); |
|
297 TestModuleIf().Printf( 0, KDefinion, KData ); |
|
298 for( TInt i=0; i<10; i++) |
|
299 { |
|
300 _LIT( KRunning ,"Running"); |
|
301 _LIT( KLoop ,"%d"); |
|
302 TestModuleIf().Printf( 0, KRunning, KLoop, i); |
|
303 User::After( 1000000 ); |
|
304 } |
|
305 _LIT( KData2 ,"Finished"); |
|
306 TestModuleIf().Printf( 0, KDefinion, KData2 ); |
|
307 |
|
308 // Test case passed |
|
309 |
|
310 // Sets test case result and description(Maximum size is KStifMaxResultDes) |
|
311 _LIT( KDescription , "Test case passed"); |
|
312 aResult.SetResult( KErrNone, KDescription ); |
|
313 //aResult.iResult = KErrNone; |
|
314 //aResult.iResultDes = KDescription; |
|
315 |
|
316 // Case was executed |
|
317 return KErrNone; |
|
318 } |
|
319 |
|
320 /* |
|
321 ------------------------------------------------------------------------------- |
|
322 |
|
323 Class: CDemoModule |
|
324 |
|
325 Method: MathTest |
|
326 |
|
327 Description: Math testing. |
|
328 |
|
329 Parameters: TTestResult& aErrorDescription: out: |
|
330 Test result and on error case a short description of error |
|
331 |
|
332 Return Values: TInt: Always KErrNone to indicate that test was valid |
|
333 |
|
334 Errors/Exceptions: None |
|
335 |
|
336 Status: Approved |
|
337 |
|
338 ------------------------------------------------------------------------------- |
|
339 */ |
|
340 TInt CDemoModule::MathTest( TTestResult& aResult ) |
|
341 { |
|
342 /* Test with mathematic calculations and printing */ |
|
343 _LIT( KDefinion ,"RunTestCase"); |
|
344 _LIT( KData ,"Math calculations"); |
|
345 TestModuleIf().Printf( 0, KDefinion, KData ); |
|
346 // Result |
|
347 TRealX res; |
|
348 // Random numbers |
|
349 TInt rand[6]; |
|
350 TRealX coef[5]; |
|
351 TInt i; |
|
352 TInt j; |
|
353 for( i=0; i<10; i++ ) |
|
354 { |
|
355 for(j=0; j<6; j++) |
|
356 rand[j] = Math::Random(); |
|
357 for(j=0; j<5; j++) |
|
358 coef[j] = rand[j]; |
|
359 Math::PolyX(res, rand[5], 5, coef ); |
|
360 |
|
361 _LIT( KResult ,"Result %d"); |
|
362 TestModuleIf().Printf( 0, KDefinion, KResult, ( TInt )res); |
|
363 } |
|
364 |
|
365 // Test case passed |
|
366 |
|
367 // Sets test case result and description(Maximum size is KStifMaxResultDes) |
|
368 _LIT( KDescription , "Test case passed"); |
|
369 aResult.SetResult( KErrNone, KDescription ); |
|
370 //aResult.iResult = KErrNone; |
|
371 //aResult.iResultDes = KDescription; |
|
372 |
|
373 // Case was executed |
|
374 return KErrNone; |
|
375 |
|
376 } |
|
377 |
|
378 /* |
|
379 ------------------------------------------------------------------------------- |
|
380 |
|
381 Class: CDemoModule |
|
382 |
|
383 Method: PrintTest |
|
384 |
|
385 Description: Printing testing. |
|
386 |
|
387 Parameters: TTestResult& aErrorDescription: out: |
|
388 Test result and on error case a short description of error |
|
389 |
|
390 Return Values: TInt: Always KErrNone to indicate that test was valid |
|
391 |
|
392 Errors/Exceptions: None |
|
393 |
|
394 Status: Approved |
|
395 |
|
396 ------------------------------------------------------------------------------- |
|
397 */ |
|
398 TInt CDemoModule::PrintTest( TTestResult& aResult ) |
|
399 { |
|
400 /* Tests printing with high intencity and different priority */ |
|
401 _LIT( KDefinion ,"CTestModuleDemo::RunTestCase"); |
|
402 _LIT( KData ,"Heavy print looping"); |
|
403 TestModuleIf().Printf( 0, KDefinion, KData ); |
|
404 TInt j; |
|
405 for( TInt i=0; i<10; i++) |
|
406 { |
|
407 |
|
408 for(j=0; j<2; j++) |
|
409 { |
|
410 _LIT( KTmp ,"RunTestCase"); |
|
411 TBuf<15> tmp; |
|
412 tmp.Copy( KTmp ); |
|
413 tmp.AppendNum( j ); |
|
414 _LIT( KNewData ,"p"); |
|
415 TestModuleIf().Printf(3, tmp, KNewData ); |
|
416 } |
|
417 |
|
418 for(j=0; j<500; j++) |
|
419 { |
|
420 _LIT( KNewDefinion ,"RunTestCase"); |
|
421 _LIT( KNewData ,"%c"); |
|
422 TestModuleIf().Printf(1+(Math::Random() % 12), |
|
423 KNewDefinion, |
|
424 KNewData, 'a'+(j%('z'-'a')) ); |
|
425 } |
|
426 |
|
427 _LIT( KNewDefinion ,"RunTestCase"); |
|
428 _LIT( KNewData ,"Running %d"); |
|
429 TestModuleIf().Printf( 2, KNewDefinion,KNewData, i); |
|
430 User::After( 1000000 ); |
|
431 } |
|
432 |
|
433 // Test case passed |
|
434 |
|
435 // Sets test case result and description(Maximum size is KStifMaxResultDes) |
|
436 _LIT( KDescription , "Test case passed"); |
|
437 aResult.SetResult( KErrNone, KDescription ); |
|
438 //aResult.iResult = KErrNone; |
|
439 //aResult.iResultDes = KDescription; |
|
440 |
|
441 // Case was executed |
|
442 return KErrNone; |
|
443 |
|
444 } |
|
445 |
|
446 /* |
|
447 ------------------------------------------------------------------------------- |
|
448 |
|
449 Class: CDemoModule |
|
450 |
|
451 Method: HeapMemoryAllocation |
|
452 |
|
453 Description: Allocates heap descriptor. |
|
454 |
|
455 Parameters: TTestResult& aError |
|
456 |
|
457 Return Values: TInt |
|
458 |
|
459 Errors/Exceptions: Leaves with KErrNoMemory if heap allocation fails |
|
460 |
|
461 Status: Proposal |
|
462 |
|
463 ------------------------------------------------------------------------------- |
|
464 */ |
|
465 TInt CDemoModule::HeapMemoryAllocation( TTestResult& aResult ) |
|
466 { |
|
467 _LIT( KLogInfo , "CDemoModule::HeapMemoryAllocation" ); |
|
468 iLog->Log( KLogInfo ); |
|
469 |
|
470 // Allocate heap descriptor |
|
471 HBufC * buffer = HBufC::New( 10 ); |
|
472 if( buffer == NULL ) |
|
473 { |
|
474 // Allocation failed |
|
475 _LIT( KNewLogInfo , "buffer was NULL"); |
|
476 iLog->Log( KNewLogInfo ); |
|
477 User::Leave( KErrNoMemory ); |
|
478 } |
|
479 else |
|
480 { |
|
481 // Descriptor creation was ok |
|
482 _LIT( KNewLogInfo ,"buffer was allocated properly" ); |
|
483 iLog->Log( KNewLogInfo ); |
|
484 } |
|
485 |
|
486 delete buffer; |
|
487 |
|
488 _LIT( KDescription , "Test case passed"); |
|
489 aResult.SetResult( KErrNone, KDescription ); |
|
490 return KErrNone; |
|
491 } |
|
492 |
|
493 // ================= OTHER EXPORTED FUNCTIONS ================================= |
|
494 |
|
495 // End of File |
|