|
1 /* |
|
2 * Copyright (c) 2002-2007 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: FSQ Test DLL |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <utf.h> |
|
20 |
|
21 #include "../inc/TestFramework/testCase.h" |
|
22 #include "../inc/TestFramework/AssertFailure.h" |
|
23 #include <StifTestModule.h> |
|
24 |
|
25 #if 0 |
|
26 #include "../inc/TestFramework/TestCase.h" |
|
27 #include "../inc/TestFramework/TestResult.h" |
|
28 #include "../inc/TestFramework/AssertFailure.h" |
|
29 #include "../inc/TestFramework/CppUnitLog.h" |
|
30 #endif |
|
31 |
|
32 // Assertion failure message formats: |
|
33 _LIT8(KIntsNotEqualFormat,"expected: %d but was: %d"); |
|
34 _LIT8(KRealsNotEqualFormat,"expected: %g but was: %g"); |
|
35 _LIT8(KDesCsNotEqualFormat,"expected: '%S' but was: '%S'"); |
|
36 |
|
37 // A unique error code: |
|
38 const TInt KErrCppUnitAssertionFailed=(-99999999); |
|
39 |
|
40 void CTestCase::ConstructL (const TDesC8& aName) |
|
41 { |
|
42 iName = aName.AllocL(); |
|
43 User::LeaveIfError(Dll::SetTls(0)); |
|
44 } |
|
45 |
|
46 CTestCase::CTestCase () |
|
47 : iAllocFailureType(RHeap::ENone), |
|
48 iAllocFailureRate(0) |
|
49 { |
|
50 } |
|
51 |
|
52 CTestCase::~CTestCase () |
|
53 { |
|
54 delete iName; |
|
55 } |
|
56 |
|
57 // From MTest: |
|
58 void CTestCase::ExecuteL (TTestResult& aResult) |
|
59 { |
|
60 TInt error = ExecuteImplL(); |
|
61 aResult.iResult = error; |
|
62 // add the possible failure or error to the result |
|
63 |
|
64 if (error == KErrCppUnitAssertionFailed) |
|
65 { |
|
66 CAssertFailure* assertFailure = AssertFailureFromTlsL (); |
|
67 CleanupStack::PushL(assertFailure); |
|
68 TBuf16 <0x80> convertBuf; |
|
69 TBuf16 <256> temporaryBuf; |
|
70 convertBuf.Copy(assertFailure->What()); |
|
71 temporaryBuf.Append(convertBuf); |
|
72 temporaryBuf.AppendFormat(_L(" at Line %i of "), assertFailure->LineNumber()); |
|
73 convertBuf.Copy(assertFailure->FileName()); |
|
74 if (convertBuf.Length() + temporaryBuf.Length() >= 0x80) |
|
75 { |
|
76 TBuf <0x80> printBuf; |
|
77 printBuf = convertBuf.Right(0x80 - temporaryBuf.Length() - 1 -3 ); |
|
78 convertBuf = _L("..."); |
|
79 convertBuf.Append(printBuf); |
|
80 } |
|
81 temporaryBuf.Append(convertBuf); |
|
82 aResult.iResultDes = temporaryBuf; |
|
83 CleanupStack::PopAndDestroy(assertFailure); |
|
84 } |
|
85 } |
|
86 |
|
87 // From MTest: |
|
88 TInt CTestCase::CountTestCases () |
|
89 { |
|
90 return 1; |
|
91 } |
|
92 |
|
93 // From MTest: |
|
94 const TDesC8& CTestCase::Name () |
|
95 { |
|
96 return *iName; |
|
97 } |
|
98 |
|
99 #if 1 |
|
100 // From MTest: |
|
101 void CTestCase::ExecuteTestL(TTestResult& aResult, |
|
102 TInt /*aIndex */) |
|
103 { |
|
104 ExecuteL(aResult); |
|
105 } |
|
106 |
|
107 const TDesC8& CTestCase::TestCaseName (TInt /*aIndex*/) |
|
108 { |
|
109 return Name(); |
|
110 } |
|
111 #endif |
|
112 |
|
113 // Check for a failed general assertion |
|
114 void CTestCase::AssertL (TBool aCondition, |
|
115 const TDesC8& aConditionExpression, |
|
116 TInt aLineNumber, |
|
117 const TDesC8& aFileName) |
|
118 { |
|
119 if (!aCondition) |
|
120 { |
|
121 AllocFailureSimulation(EFalse); |
|
122 AssertFailureToTlsL (aConditionExpression,aLineNumber,aFileName); |
|
123 User::Leave (KErrCppUnitAssertionFailed); |
|
124 } |
|
125 } |
|
126 |
|
127 // Check for a failed equality assertion |
|
128 void CTestCase::AssertEqualsL (TInt anExpected, |
|
129 TInt anActual, |
|
130 TInt aLineNumber, |
|
131 const TDesC8& aFileName) |
|
132 { |
|
133 if (anExpected != anActual) |
|
134 { |
|
135 AllocFailureSimulation(EFalse); |
|
136 HBufC8* msg = HBufC8::NewLC (KIntsNotEqualFormat().Size()+100); |
|
137 msg->Des().Format(KIntsNotEqualFormat, anExpected, anActual); |
|
138 AssertFailureToTlsL(*msg,aLineNumber,aFileName); |
|
139 CleanupStack::PopAndDestroy(); // msg |
|
140 User::Leave (KErrCppUnitAssertionFailed); |
|
141 } |
|
142 } |
|
143 |
|
144 // Check for a failed equality assertion |
|
145 void CTestCase::AssertEqualsL (TReal anExpected, |
|
146 TReal anActual, |
|
147 TReal aDelta, |
|
148 TInt aLineNumber, |
|
149 const TDesC8& aFileName) |
|
150 { |
|
151 if (Abs(anExpected-anActual) > aDelta) |
|
152 { |
|
153 AllocFailureSimulation(EFalse); |
|
154 HBufC8* msg = HBufC8::NewLC (KRealsNotEqualFormat().Size()+100); |
|
155 msg->Des().Format(KRealsNotEqualFormat, anExpected, anActual); |
|
156 AssertFailureToTlsL(*msg,aLineNumber,aFileName); |
|
157 CleanupStack::PopAndDestroy(); // msg |
|
158 User::Leave (KErrCppUnitAssertionFailed); |
|
159 } |
|
160 } |
|
161 |
|
162 // Check equality of the contents of two 8-bit descriptors |
|
163 void CTestCase::AssertEqualsL (const TDesC8& anExpected, |
|
164 const TDesC8& anActual, |
|
165 TInt aLineNumber, |
|
166 const TDesC8& aFileName) |
|
167 { |
|
168 if (anExpected.Compare(anActual) != 0) |
|
169 { |
|
170 AllocFailureSimulation(EFalse); |
|
171 HBufC8* msg = NotEqualsMessageLC (anExpected, anActual); |
|
172 AssertFailureToTlsL(*msg,aLineNumber,aFileName); |
|
173 CleanupStack::PopAndDestroy(); // msg |
|
174 User::Leave (KErrCppUnitAssertionFailed); |
|
175 } |
|
176 } |
|
177 |
|
178 // Check equality of the contents of two 16-bit descriptors |
|
179 void CTestCase::AssertEqualsL (const TDesC16& aExpected, |
|
180 const TDesC16& aActual, |
|
181 TInt aLineNumber, |
|
182 const TDesC8& aFileName) |
|
183 { |
|
184 if (aExpected.Compare(aActual) != 0) |
|
185 { |
|
186 AllocFailureSimulation(EFalse); |
|
187 HBufC8* msg = NotEqualsMessageLC (aExpected, aActual); |
|
188 AssertFailureToTlsL(*msg,aLineNumber,aFileName); |
|
189 CleanupStack::PopAndDestroy(); // msg |
|
190 User::Leave (KErrCppUnitAssertionFailed); |
|
191 } |
|
192 } |
|
193 |
|
194 void CTestCase::AllocFailureSimulation (TBool aSwitchedOn) |
|
195 { |
|
196 if (aSwitchedOn) |
|
197 { |
|
198 __UHEAP_SETFAIL (iAllocFailureType, iAllocFailureRate); |
|
199 } |
|
200 else |
|
201 { |
|
202 __UHEAP_RESET; |
|
203 } |
|
204 } |
|
205 |
|
206 TInt CTestCase::ExecuteImplL () |
|
207 { |
|
208 |
|
209 __UHEAP_MARK; |
|
210 TRAPD (setupError, setUpL()); |
|
211 if (setupError != KErrNone) |
|
212 { |
|
213 tearDown(); |
|
214 __UHEAP_MARKEND; |
|
215 User::Leave(setupError); |
|
216 } |
|
217 |
|
218 TRAPD (executionError, executeTestL()); |
|
219 |
|
220 tearDown(); |
|
221 __UHEAP_MARKENDC(HeapCellsReservedByAssertFailure()); |
|
222 |
|
223 return executionError; |
|
224 } |
|
225 |
|
226 // Construct the message and put it in the cleanup stack |
|
227 HBufC8* CTestCase::NotEqualsMessageLC (const TDesC8& aExpected, |
|
228 const TDesC8& aActual) |
|
229 { |
|
230 TInt size = KDesCsNotEqualFormat().Size()+aExpected.Size()+aActual.Size(); |
|
231 HBufC8 *msg = HBufC8::NewLC(size); |
|
232 msg->Des().Format(KDesCsNotEqualFormat, &aExpected, &aActual); |
|
233 return msg; |
|
234 } |
|
235 |
|
236 // Construct the message and put it in the cleanup stack |
|
237 HBufC8* CTestCase::NotEqualsMessageLC (const TDesC16& aExpected, |
|
238 const TDesC16& aActual) |
|
239 { |
|
240 TInt length = KDesCsNotEqualFormat().Length() + |
|
241 aExpected.Length() + aActual.Length(); |
|
242 |
|
243 HBufC8* msg = HBufC8::NewLC(length); |
|
244 |
|
245 // Convert 16-bit to 8-bit to ensure readability |
|
246 // of the output possibly directed to a file. |
|
247 HBufC8* expected = HBufC8::NewLC(aExpected.Length()); |
|
248 expected->Des().Copy(aExpected); |
|
249 |
|
250 HBufC8* actual = HBufC8::NewLC(aActual.Length()); |
|
251 actual->Des().Copy(aActual); |
|
252 |
|
253 msg->Des().Format(KDesCsNotEqualFormat,expected,actual); |
|
254 |
|
255 // pop and destroy actual and expected |
|
256 CleanupStack::PopAndDestroy(2); |
|
257 return msg; |
|
258 } |
|
259 |
|
260 void CTestCase::AssertFailureToTlsL (const TDesC8& aMessage, |
|
261 TInt aLineNumber, |
|
262 const TDesC8& aFileName) |
|
263 { |
|
264 TInt cellsBefore = User::CountAllocCells(); |
|
265 CAssertFailure* assertFailure = |
|
266 CAssertFailure::NewLC(aMessage,aLineNumber,aFileName); |
|
267 User::LeaveIfError(Dll::SetTls(assertFailure)); |
|
268 CleanupStack::Pop(); // assertFailure |
|
269 TInt cellsAfter = User::CountAllocCells(); |
|
270 assertFailure->SetMyHeapCellCount(cellsAfter-cellsBefore); |
|
271 } |
|
272 |
|
273 CAssertFailure* CTestCase::AssertFailureFromTlsL () |
|
274 { |
|
275 CAssertFailure* assertFailure = static_cast<CAssertFailure*>(Dll::Tls()); |
|
276 CAssertFailure* copy = CAssertFailure::NewL(*assertFailure); |
|
277 delete assertFailure; |
|
278 Dll::SetTls(0); |
|
279 return copy; |
|
280 } |
|
281 |
|
282 TInt CTestCase::HeapCellsReservedByAssertFailure () |
|
283 { |
|
284 if (Dll::Tls() == 0) return 0; |
|
285 CAssertFailure* assertFailure = static_cast<CAssertFailure*>(Dll::Tls()); |
|
286 return assertFailure->MyHeapCellCount(); |
|
287 } |
|
288 |
|
289 |