]> git.proxmox.com Git - mirror_edk2.git/blob - UnitTestFrameworkPkg/Library/UnitTestLib/Assert.c
UnitTestFrameworkPkg/UnitTestLib: Fix XCODE parenthesis issues
[mirror_edk2.git] / UnitTestFrameworkPkg / Library / UnitTestLib / Assert.c
1 /**
2 Implement UnitTestLib assert services
3
4 Copyright (c) Microsoft Corporation.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 **/
7
8 #include <Uefi.h>
9 #include <UnitTestFrameworkTypes.h>
10 #include <Library/UnitTestLib.h>
11 #include <Library/BaseLib.h>
12 #include <Library/BaseMemoryLib.h>
13 #include <Library/DebugLib.h>
14 #include <Library/PrintLib.h>
15
16 STATIC
17 EFI_STATUS
18 AddUnitTestFailure (
19 IN OUT UNIT_TEST *UnitTest,
20 IN CONST CHAR8 *FailureMessage,
21 IN FAILURE_TYPE FailureType
22 )
23 {
24 //
25 // Make sure that you're cooking with gas.
26 //
27 if (UnitTest == NULL || FailureMessage == NULL) {
28 return EFI_INVALID_PARAMETER;
29 }
30
31 UnitTest->FailureType = FailureType;
32 AsciiStrCpyS (
33 &UnitTest->FailureMessage[0],
34 UNIT_TEST_TESTFAILUREMSG_LENGTH,
35 FailureMessage
36 );
37
38 return EFI_SUCCESS;
39 }
40
41 STATIC
42 VOID
43 UnitTestLogFailure (
44 IN FAILURE_TYPE FailureType,
45 IN CONST CHAR8 *Format,
46 ...
47 )
48 {
49 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle;
50 CHAR8 LogString[UNIT_TEST_TESTFAILUREMSG_LENGTH];
51 VA_LIST Marker;
52
53 //
54 // Get active Framework handle
55 //
56 FrameworkHandle = GetActiveFrameworkHandle ();
57
58 //
59 // Convert the message to an ASCII String
60 //
61 VA_START (Marker, Format);
62 AsciiVSPrint (LogString, sizeof (LogString), Format, Marker);
63 VA_END (Marker);
64
65 //
66 // Finally, add the string to the log.
67 //
68 AddUnitTestFailure (
69 ((UNIT_TEST_FRAMEWORK *)FrameworkHandle)->CurrentTest,
70 LogString,
71 FailureType
72 );
73
74 return;
75 }
76
77 /**
78 If Expression is TRUE, then TRUE is returned.
79 If Expression is FALSE, then an assert is triggered and the location of the
80 assert provided by FunctionName, LineNumber, FileName, and Description are
81 recorded and FALSE is returned.
82
83 @param[in] Expression The BOOLEAN result of the expression evaluation.
84 @param[in] FunctionName Null-terminated ASCII string of the function
85 executing the assert macro.
86 @param[in] LineNumber The source file line number of the assert macro.
87 @param[in] FileName Null-terminated ASCII string of the filename
88 executing the assert macro.
89 @param[in] Description Null-terminated ASCII string of the expression being
90 evaluated.
91
92 @retval TRUE Expression is TRUE.
93 @retval FALSE Expression is FALSE.
94 **/
95 BOOLEAN
96 EFIAPI
97 UnitTestAssertTrue (
98 IN BOOLEAN Expression,
99 IN CONST CHAR8 *FunctionName,
100 IN UINTN LineNumber,
101 IN CONST CHAR8 *FileName,
102 IN CONST CHAR8 *Description
103 )
104 {
105 if (!Expression) {
106 UnitTestLogFailure (
107 FAILURETYPE_ASSERTTRUE,
108 "%a::%d Expression (%a) is not TRUE!\n",
109 FunctionName,
110 LineNumber,
111 Description
112 );
113 UT_LOG_ERROR (
114 "[ASSERT FAIL] %a::%d Expression (%a) is not TRUE!\n",
115 FunctionName,
116 LineNumber,
117 Description
118 );
119 }
120 return Expression;
121 }
122
123 /**
124 If Expression is FALSE, then TRUE is returned.
125 If Expression is TRUE, then an assert is triggered and the location of the
126 assert provided by FunctionName, LineNumber, FileName, and Description are
127 recorded and FALSE is returned.
128
129 @param[in] Expression The BOOLEAN result of the expression evaluation.
130 @param[in] FunctionName Null-terminated ASCII string of the function
131 executing the assert macro.
132 @param[in] LineNumber The source file line number of the assert macro.
133 @param[in] FileName Null-terminated ASCII string of the filename
134 executing the assert macro.
135 @param[in] Description Null-terminated ASCII string of the expression being
136 evaluated.
137
138 @retval TRUE Expression is FALSE.
139 @retval FALSE Expression is TRUE.
140 **/
141 BOOLEAN
142 EFIAPI
143 UnitTestAssertFalse (
144 IN BOOLEAN Expression,
145 IN CONST CHAR8 *FunctionName,
146 IN UINTN LineNumber,
147 IN CONST CHAR8 *FileName,
148 IN CONST CHAR8 *Description
149 )
150 {
151 if (Expression) {
152 UnitTestLogFailure (
153 FAILURETYPE_ASSERTFALSE,
154 "%a::%d Expression(%a) is not FALSE!\n",
155 FunctionName,
156 LineNumber,
157 Description
158 );
159 UT_LOG_ERROR (
160 "[ASSERT FAIL] %a::%d Expression (%a) is not FALSE!\n",
161 FunctionName,
162 LineNumber,
163 Description
164 );
165 }
166 return !Expression;
167 }
168
169 /**
170 If Status is not an EFI_ERROR(), then TRUE is returned.
171 If Status is an EFI_ERROR(), then an assert is triggered and the location of
172 the assert provided by FunctionName, LineNumber, FileName, and Description are
173 recorded and FALSE is returned.
174
175 @param[in] Status The EFI_STATUS value to evaluate.
176 @param[in] FunctionName Null-terminated ASCII string of the function
177 executing the assert macro.
178 @param[in] LineNumber The source file line number of the assert macro.
179 @param[in] FileName Null-terminated ASCII string of the filename
180 executing the assert macro.
181 @param[in] Description Null-terminated ASCII string of the status
182 expression being evaluated.
183
184 @retval TRUE Status is not an EFI_ERROR().
185 @retval FALSE Status is an EFI_ERROR().
186 **/
187 BOOLEAN
188 EFIAPI
189 UnitTestAssertNotEfiError (
190 IN EFI_STATUS Status,
191 IN CONST CHAR8 *FunctionName,
192 IN UINTN LineNumber,
193 IN CONST CHAR8 *FileName,
194 IN CONST CHAR8 *Description
195 )
196 {
197 if (EFI_ERROR (Status)) {
198 UnitTestLogFailure (
199 FAILURETYPE_ASSERTNOTEFIERROR,
200 "%a::%d Status '%a' is EFI_ERROR (%r)!\n",
201 FunctionName,
202 LineNumber,
203 Description,
204 Status
205 );
206 UT_LOG_ERROR (
207 "[ASSERT FAIL] %a::%d Status '%a' is EFI_ERROR (%r)!\n",
208 FunctionName,
209 LineNumber,
210 Description,
211 Status
212 );
213 }
214 return !EFI_ERROR( Status );
215 }
216
217 /**
218 If ValueA is equal ValueB, then TRUE is returned.
219 If ValueA is not equal to ValueB, then an assert is triggered and the location
220 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,
221 and DescriptionB are recorded and FALSE is returned.
222
223 @param[in] ValueA 64-bit value.
224 @param[in] ValueB 64-bit value.
225 @param[in] FunctionName Null-terminated ASCII string of the function
226 executing the assert macro.
227 @param[in] LineNumber The source file line number of the assert macro.
228 @param[in] FileName Null-terminated ASCII string of the filename
229 executing the assert macro.
230 @param[in] DescriptionA Null-terminated ASCII string that is a description
231 of ValueA.
232 @param[in] DescriptionB Null-terminated ASCII string that is a description
233 of ValueB.
234
235 @retval TRUE ValueA is equal to ValueB.
236 @retval FALSE ValueA is not equal to ValueB.
237 **/
238 BOOLEAN
239 EFIAPI
240 UnitTestAssertEqual (
241 IN UINT64 ValueA,
242 IN UINT64 ValueB,
243 IN CONST CHAR8 *FunctionName,
244 IN UINTN LineNumber,
245 IN CONST CHAR8 *FileName,
246 IN CONST CHAR8 *DescriptionA,
247 IN CONST CHAR8 *DescriptionB
248 )
249 {
250 if (ValueA != ValueB) {
251 UnitTestLogFailure (
252 FAILURETYPE_ASSERTEQUAL,
253 "%a::%d Value %a != %a (%d != %d)!\n",
254 FunctionName,
255 LineNumber,
256 DescriptionA,
257 DescriptionB,
258 ValueA,
259 ValueB
260 );
261 UT_LOG_ERROR (
262 "[ASSERT FAIL] %a::%d Value %a != %a (%d != %d)!\n",
263 FunctionName,
264 LineNumber,
265 DescriptionA,
266 DescriptionB,
267 ValueA,
268 ValueB
269 );
270 }
271 return (ValueA == ValueB);
272 }
273
274 /**
275 If the contents of BufferA are identical to the contents of BufferB, then TRUE
276 is returned. If the contents of BufferA are not identical to the contents of
277 BufferB, then an assert is triggered and the location of the assert provided
278 by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are
279 recorded and FALSE is returned.
280
281 @param[in] BufferA Pointer to a buffer for comparison.
282 @param[in] BufferB Pointer to a buffer for comparison.
283 @param[in] Length Number of bytes to compare in BufferA and BufferB.
284 @param[in] FunctionName Null-terminated ASCII string of the function
285 executing the assert macro.
286 @param[in] LineNumber The source file line number of the assert macro.
287 @param[in] FileName Null-terminated ASCII string of the filename
288 executing the assert macro.
289 @param[in] DescriptionA Null-terminated ASCII string that is a description
290 of BufferA.
291 @param[in] DescriptionB Null-terminated ASCII string that is a description
292 of BufferB.
293
294 @retval TRUE The contents of BufferA are identical to the contents of
295 BufferB.
296 @retval FALSE The contents of BufferA are not identical to the contents of
297 BufferB.
298 **/
299 BOOLEAN
300 EFIAPI
301 UnitTestAssertMemEqual (
302 IN VOID *BufferA,
303 IN VOID *BufferB,
304 IN UINTN Length,
305 IN CONST CHAR8 *FunctionName,
306 IN UINTN LineNumber,
307 IN CONST CHAR8 *FileName,
308 IN CONST CHAR8 *DescriptionA,
309 IN CONST CHAR8 *DescriptionB
310 )
311 {
312 if (CompareMem(BufferA, BufferB, Length) != 0) {
313 UnitTestLogFailure (
314 FAILURETYPE_ASSERTEQUAL,
315 "%a::%d Memory at %a != %a for length %d bytes!\n",
316 FunctionName,
317 LineNumber,
318 DescriptionA,
319 DescriptionB,
320 Length
321 );
322 UT_LOG_ERROR (
323 "[ASSERT FAIL] %a::%d Value %a != %a for length %d bytes!\n",
324 FunctionName,
325 LineNumber,
326 DescriptionA,
327 DescriptionB,
328 Length
329 );
330 return FALSE;
331 }
332 return TRUE;
333 }
334
335 /**
336 If ValueA is not equal ValueB, then TRUE is returned.
337 If ValueA is equal to ValueB, then an assert is triggered and the location
338 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA
339 and DescriptionB are recorded and FALSE is returned.
340
341 @param[in] ValueA 64-bit value.
342 @param[in] ValueB 64-bit value.
343 @param[in] FunctionName Null-terminated ASCII string of the function
344 executing the assert macro.
345 @param[in] LineNumber The source file line number of the assert macro.
346 @param[in] FileName Null-terminated ASCII string of the filename
347 executing the assert macro.
348 @param[in] DescriptionA Null-terminated ASCII string that is a description
349 of ValueA.
350 @param[in] DescriptionB Null-terminated ASCII string that is a description
351 of ValueB.
352
353 @retval TRUE ValueA is not equal to ValueB.
354 @retval FALSE ValueA is equal to ValueB.
355 **/
356 BOOLEAN
357 EFIAPI
358 UnitTestAssertNotEqual (
359 IN UINT64 ValueA,
360 IN UINT64 ValueB,
361 IN CONST CHAR8 *FunctionName,
362 IN UINTN LineNumber,
363 IN CONST CHAR8 *FileName,
364 IN CONST CHAR8 *DescriptionA,
365 IN CONST CHAR8 *DescriptionB
366 )
367 {
368 if (ValueA == ValueB) {
369 UnitTestLogFailure (
370 FAILURETYPE_ASSERTNOTEQUAL,
371 "%a::%d Value %a == %a (%d == %d)!\n",
372 FunctionName,
373 LineNumber,
374 DescriptionA,
375 DescriptionB,
376 ValueA,
377 ValueB
378 );
379 UT_LOG_ERROR (
380 "[ASSERT FAIL] %a::%d Value %a == %a (%d == %d)!\n",
381 FunctionName,
382 LineNumber,
383 DescriptionA,
384 DescriptionB,
385 ValueA,
386 ValueB
387 );
388 }
389 return (ValueA != ValueB);
390 }
391
392 /**
393 If Status is equal to Expected, then TRUE is returned.
394 If Status is not equal to Expected, then an assert is triggered and the
395 location of the assert provided by FunctionName, LineNumber, FileName, and
396 Description are recorded and FALSE is returned.
397
398 @param[in] Status EFI_STATUS value returned from an API under test.
399 @param[in] Expected The expected EFI_STATUS return value from an API
400 under test.
401 @param[in] FunctionName Null-terminated ASCII string of the function
402 executing the assert macro.
403 @param[in] LineNumber The source file line number of the assert macro.
404 @param[in] FileName Null-terminated ASCII string of the filename
405 executing the assert macro.
406 @param[in] Description Null-terminated ASCII string that is a description
407 of Status.
408
409 @retval TRUE Status is equal to Expected.
410 @retval FALSE Status is not equal to Expected.
411 **/
412 BOOLEAN
413 EFIAPI
414 UnitTestAssertStatusEqual (
415 IN EFI_STATUS Status,
416 IN EFI_STATUS Expected,
417 IN CONST CHAR8 *FunctionName,
418 IN UINTN LineNumber,
419 IN CONST CHAR8 *FileName,
420 IN CONST CHAR8 *Description
421 )
422 {
423 if (Status != Expected) {
424 UnitTestLogFailure (
425 FAILURETYPE_ASSERTSTATUSEQUAL,
426 "%a::%d Status '%a' is %r, should be %r!\n",
427 FunctionName,
428 LineNumber,
429 Description,
430 Status,
431 Expected
432 );
433 UT_LOG_ERROR (
434 "[ASSERT FAIL] %a::%d Status '%a' is %r, should be %r!\n",
435 FunctionName,
436 LineNumber,
437 Description,
438 Status,
439 Expected
440 );
441 }
442 return (Status == Expected);
443 }
444
445 /**
446 If Pointer is not equal to NULL, then TRUE is returned.
447 If Pointer is equal to NULL, then an assert is triggered and the location of
448 the assert provided by FunctionName, LineNumber, FileName, and PointerName
449 are recorded and FALSE is returned.
450
451 @param[in] Pointer Pointer value to be checked against NULL.
452 @param[in] Expected The expected EFI_STATUS return value from a function
453 under test.
454 @param[in] FunctionName Null-terminated ASCII string of the function
455 executing the assert macro.
456 @param[in] LineNumber The source file line number of the assert macro.
457 @param[in] FileName Null-terminated ASCII string of the filename
458 executing the assert macro.
459 @param[in] PointerName Null-terminated ASCII string that is a description
460 of Pointer.
461
462 @retval TRUE Pointer is not equal to NULL.
463 @retval FALSE Pointer is equal to NULL.
464 **/
465 BOOLEAN
466 EFIAPI
467 UnitTestAssertNotNull (
468 IN VOID *Pointer,
469 IN CONST CHAR8 *FunctionName,
470 IN UINTN LineNumber,
471 IN CONST CHAR8 *FileName,
472 IN CONST CHAR8 *PointerName
473 )
474 {
475 if (Pointer == NULL) {
476 UnitTestLogFailure (
477 FAILURETYPE_ASSERTNOTNULL,
478 "%a::%d Pointer (%a) is NULL!\n",
479 FunctionName,
480 LineNumber,
481 PointerName
482 );
483 UT_LOG_ERROR (
484 "[ASSERT FAIL] %a::%d Pointer (%a) is NULL!\n",
485 FunctionName,
486 LineNumber,
487 PointerName
488 );
489 }
490 return (Pointer != NULL);
491 }