2 Implement UnitTestLib assert services
4 Copyright (c) Microsoft Corporation.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
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>
19 IN OUT UNIT_TEST
*UnitTest
,
20 IN CONST CHAR8
*FailureMessage
,
21 IN FAILURE_TYPE FailureType
25 // Make sure that you're cooking with gas.
27 if (UnitTest
== NULL
|| FailureMessage
== NULL
) {
28 return EFI_INVALID_PARAMETER
;
31 UnitTest
->FailureType
= FailureType
;
33 &UnitTest
->FailureMessage
[0],
34 UNIT_TEST_TESTFAILUREMSG_LENGTH
,
44 IN FAILURE_TYPE FailureType
,
45 IN CONST CHAR8
*Format
,
49 UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
;
50 CHAR8 LogString
[UNIT_TEST_TESTFAILUREMSG_LENGTH
];
54 // Get active Framework handle
56 FrameworkHandle
= GetActiveFrameworkHandle ();
59 // Convert the message to an ASCII String
61 VA_START (Marker
, Format
);
62 AsciiVSPrint (LogString
, sizeof (LogString
), Format
, Marker
);
66 // Finally, add the string to the log.
69 ((UNIT_TEST_FRAMEWORK
*)FrameworkHandle
)->CurrentTest
,
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.
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
92 @retval TRUE Expression is TRUE.
93 @retval FALSE Expression is FALSE.
98 IN BOOLEAN Expression
,
99 IN CONST CHAR8
*FunctionName
,
101 IN CONST CHAR8
*FileName
,
102 IN CONST CHAR8
*Description
107 FAILURETYPE_ASSERTTRUE
,
108 "%a:%d: Expression (%a) is not TRUE!\n",
114 "[ASSERT FAIL] %a:%d: Expression (%a) is not TRUE!\n",
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.
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
138 @retval TRUE Expression is FALSE.
139 @retval FALSE Expression is TRUE.
143 UnitTestAssertFalse (
144 IN BOOLEAN Expression
,
145 IN CONST CHAR8
*FunctionName
,
147 IN CONST CHAR8
*FileName
,
148 IN CONST CHAR8
*Description
153 FAILURETYPE_ASSERTFALSE
,
154 "%a:%d: Expression(%a) is not FALSE!\n",
160 "[ASSERT FAIL] %a:%d: Expression (%a) is not FALSE!\n",
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.
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.
184 @retval TRUE Status is not an EFI_ERROR().
185 @retval FALSE Status is an EFI_ERROR().
189 UnitTestAssertNotEfiError (
190 IN EFI_STATUS Status
,
191 IN CONST CHAR8
*FunctionName
,
193 IN CONST CHAR8
*FileName
,
194 IN CONST CHAR8
*Description
197 if (EFI_ERROR (Status
)) {
199 FAILURETYPE_ASSERTNOTEFIERROR
,
200 "%a:%d: Status '%a' is EFI_ERROR (%r)!\n",
207 "[ASSERT FAIL] %a:%d: Status '%a' is EFI_ERROR (%r)!\n",
214 return !EFI_ERROR( Status
);
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.
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
232 @param[in] DescriptionB Null-terminated ASCII string that is a description
235 @retval TRUE ValueA is equal to ValueB.
236 @retval FALSE ValueA is not equal to ValueB.
240 UnitTestAssertEqual (
243 IN CONST CHAR8
*FunctionName
,
245 IN CONST CHAR8
*FileName
,
246 IN CONST CHAR8
*DescriptionA
,
247 IN CONST CHAR8
*DescriptionB
250 if (ValueA
!= ValueB
) {
252 FAILURETYPE_ASSERTEQUAL
,
253 "%a:%d: Value %a != %a (%d != %d)!\n",
262 "[ASSERT FAIL] %a:%d: Value %a != %a (%d != %d)!\n",
271 return (ValueA
== ValueB
);
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.
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
291 @param[in] DescriptionB Null-terminated ASCII string that is a description
294 @retval TRUE The contents of BufferA are identical to the contents of
296 @retval FALSE The contents of BufferA are not identical to the contents of
301 UnitTestAssertMemEqual (
305 IN CONST CHAR8
*FunctionName
,
307 IN CONST CHAR8
*FileName
,
308 IN CONST CHAR8
*DescriptionA
,
309 IN CONST CHAR8
*DescriptionB
312 if (CompareMem(BufferA
, BufferB
, Length
) != 0) {
314 FAILURETYPE_ASSERTEQUAL
,
315 "%a:%d: Memory at %a != %a for length %d bytes!\n",
323 "[ASSERT FAIL] %a:%d: Value %a != %a for length %d bytes!\n",
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.
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
350 @param[in] DescriptionB Null-terminated ASCII string that is a description
353 @retval TRUE ValueA is not equal to ValueB.
354 @retval FALSE ValueA is equal to ValueB.
358 UnitTestAssertNotEqual (
361 IN CONST CHAR8
*FunctionName
,
363 IN CONST CHAR8
*FileName
,
364 IN CONST CHAR8
*DescriptionA
,
365 IN CONST CHAR8
*DescriptionB
368 if (ValueA
== ValueB
) {
370 FAILURETYPE_ASSERTNOTEQUAL
,
371 "%a:%d: Value %a == %a (%d == %d)!\n",
380 "[ASSERT FAIL] %a:%d: Value %a == %a (%d == %d)!\n",
389 return (ValueA
!= ValueB
);
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.
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
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
409 @retval TRUE Status is equal to Expected.
410 @retval FALSE Status is not equal to Expected.
414 UnitTestAssertStatusEqual (
415 IN EFI_STATUS Status
,
416 IN EFI_STATUS Expected
,
417 IN CONST CHAR8
*FunctionName
,
419 IN CONST CHAR8
*FileName
,
420 IN CONST CHAR8
*Description
423 if (Status
!= Expected
) {
425 FAILURETYPE_ASSERTSTATUSEQUAL
,
426 "%a:%d: Status '%a' is %r, should be %r!\n",
434 "[ASSERT FAIL] %a:%d: Status '%a' is %r, should be %r!\n",
442 return (Status
== Expected
);
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.
451 @param[in] Pointer Pointer value to be checked against NULL.
452 @param[in] Expected The expected EFI_STATUS return value from a function
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
462 @retval TRUE Pointer is not equal to NULL.
463 @retval FALSE Pointer is equal to NULL.
467 UnitTestAssertNotNull (
469 IN CONST CHAR8
*FunctionName
,
471 IN CONST CHAR8
*FileName
,
472 IN CONST CHAR8
*PointerName
475 if (Pointer
== NULL
) {
477 FAILURETYPE_ASSERTNOTNULL
,
478 "%a:%d: Pointer (%a) is NULL!\n",
484 "[ASSERT FAIL] %a:%d: Pointer (%a) is NULL!\n",
490 return (Pointer
!= NULL
);