2 Implement UnitTestLib assert services using cmocka services
4 Copyright (c) 2019 - 2020, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/UnitTestLib.h>
19 #define MAX_STRING_SIZE 1025
22 If Expression is TRUE, then TRUE is returned.
23 If Expression is FALSE, then an assert is triggered and the location of the
24 assert provided by FunctionName, LineNumber, FileName, and Description are
25 recorded and FALSE is returned.
27 @param[in] Expression The BOOLEAN result of the expression evaluation.
28 @param[in] FunctionName Null-terminated ASCII string of the function
29 executing the assert macro.
30 @param[in] LineNumber The source file line number of the assert macro.
31 @param[in] FileName Null-terminated ASCII string of the filename
32 executing the assert macro.
33 @param[in] Description Null-terminated ASCII string of the expression being
36 @retval TRUE Expression is TRUE.
37 @retval FALSE Expression is FALSE.
42 IN BOOLEAN Expression
,
43 IN CONST CHAR8
*FunctionName
,
45 IN CONST CHAR8
*FileName
,
46 IN CONST CHAR8
*Description
49 CHAR8 TempStr
[MAX_STRING_SIZE
];
51 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_TRUE(%s:%x)", Description
, Expression
);
52 _assert_true (Expression
, TempStr
, FileName
, (INT32
)LineNumber
);
58 If Expression is FALSE, then TRUE is returned.
59 If Expression is TRUE, then an assert is triggered and the location of the
60 assert provided by FunctionName, LineNumber, FileName, and Description are
61 recorded and FALSE is returned.
63 @param[in] Expression The BOOLEAN result of the expression evaluation.
64 @param[in] FunctionName Null-terminated ASCII string of the function
65 executing the assert macro.
66 @param[in] LineNumber The source file line number of the assert macro.
67 @param[in] FileName Null-terminated ASCII string of the filename
68 executing the assert macro.
69 @param[in] Description Null-terminated ASCII string of the expression being
72 @retval TRUE Expression is FALSE.
73 @retval FALSE Expression is TRUE.
78 IN BOOLEAN Expression
,
79 IN CONST CHAR8
*FunctionName
,
81 IN CONST CHAR8
*FileName
,
82 IN CONST CHAR8
*Description
85 CHAR8 TempStr
[MAX_STRING_SIZE
];
87 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_FALSE(%s:%x)", Description
, Expression
);
88 _assert_true (!Expression
, TempStr
, FileName
, (INT32
)LineNumber
);
94 If Status is not an EFI_ERROR(), then TRUE is returned.
95 If Status is an EFI_ERROR(), then an assert is triggered and the location of
96 the assert provided by FunctionName, LineNumber, FileName, and Description are
97 recorded and FALSE is returned.
99 @param[in] Status The EFI_STATUS value to evaluate.
100 @param[in] FunctionName Null-terminated ASCII string of the function
101 executing the assert macro.
102 @param[in] LineNumber The source file line number of the assert macro.
103 @param[in] FileName Null-terminated ASCII string of the filename
104 executing the assert macro.
105 @param[in] Description Null-terminated ASCII string of the status
106 expression being evaluated.
108 @retval TRUE Status is not an EFI_ERROR().
109 @retval FALSE Status is an EFI_ERROR().
113 UnitTestAssertNotEfiError (
114 IN EFI_STATUS Status
,
115 IN CONST CHAR8
*FunctionName
,
117 IN CONST CHAR8
*FileName
,
118 IN CONST CHAR8
*Description
121 CHAR8 TempStr
[MAX_STRING_SIZE
];
123 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_NOT_EFI_ERROR(%s:%p)", Description
, (void *)Status
);
124 _assert_true (!EFI_ERROR (Status
), TempStr
, FileName
, (INT32
)LineNumber
);
126 return !EFI_ERROR (Status
);
130 If ValueA is equal ValueB, then TRUE is returned.
131 If ValueA is not equal to ValueB, then an assert is triggered and the location
132 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA,
133 and DescriptionB are recorded and FALSE is returned.
135 @param[in] ValueA 64-bit value.
136 @param[in] ValueB 64-bit value.
137 @param[in] FunctionName Null-terminated ASCII string of the function
138 executing the assert macro.
139 @param[in] LineNumber The source file line number of the assert macro.
140 @param[in] FileName Null-terminated ASCII string of the filename
141 executing the assert macro.
142 @param[in] DescriptionA Null-terminated ASCII string that is a description
144 @param[in] DescriptionB Null-terminated ASCII string that is a description
147 @retval TRUE ValueA is equal to ValueB.
148 @retval FALSE ValueA is not equal to ValueB.
152 UnitTestAssertEqual (
155 IN CONST CHAR8
*FunctionName
,
157 IN CONST CHAR8
*FileName
,
158 IN CONST CHAR8
*DescriptionA
,
159 IN CONST CHAR8
*DescriptionB
162 CHAR8 TempStr
[MAX_STRING_SIZE
];
164 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_EQUAL(%s:%llx, %s:%llx)", DescriptionA
, ValueA
, DescriptionB
, ValueB
);
165 _assert_true ((ValueA
== ValueB
), TempStr
, FileName
, (INT32
)LineNumber
);
167 return (ValueA
== ValueB
);
171 If the contents of BufferA are identical to the contents of BufferB, then TRUE
172 is returned. If the contents of BufferA are not identical to the contents of
173 BufferB, then an assert is triggered and the location of the assert provided
174 by FunctionName, LineNumber, FileName, DescriptionA, and DescriptionB are
175 recorded and FALSE is returned.
177 @param[in] BufferA Pointer to a buffer for comparison.
178 @param[in] BufferB Pointer to a buffer for comparison.
179 @param[in] Length Number of bytes to compare in BufferA and BufferB.
180 @param[in] FunctionName Null-terminated ASCII string of the function
181 executing the assert macro.
182 @param[in] LineNumber The source file line number of the assert macro.
183 @param[in] FileName Null-terminated ASCII string of the filename
184 executing the assert macro.
185 @param[in] DescriptionA Null-terminated ASCII string that is a description
187 @param[in] DescriptionB Null-terminated ASCII string that is a description
190 @retval TRUE The contents of BufferA are identical to the contents of
192 @retval FALSE The contents of BufferA are not identical to the contents of
197 UnitTestAssertMemEqual (
201 IN CONST CHAR8
*FunctionName
,
203 IN CONST CHAR8
*FileName
,
204 IN CONST CHAR8
*DescriptionA
,
205 IN CONST CHAR8
*DescriptionB
208 CHAR8 TempStr
[MAX_STRING_SIZE
];
211 Result
= (CompareMem (BufferA
, BufferB
, Length
) == 0);
213 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_MEM_EQUAL(%s:%p, %s:%p)", DescriptionA
, BufferA
, DescriptionB
, BufferB
);
214 _assert_true (Result
, TempStr
, FileName
, (INT32
)LineNumber
);
220 If ValueA is not equal ValueB, then TRUE is returned.
221 If ValueA is equal to ValueB, then an assert is triggered and the location
222 of the assert provided by FunctionName, LineNumber, FileName, DescriptionA
223 and DescriptionB are recorded and FALSE is returned.
225 @param[in] ValueA 64-bit value.
226 @param[in] ValueB 64-bit value.
227 @param[in] FunctionName Null-terminated ASCII string of the function
228 executing the assert macro.
229 @param[in] LineNumber The source file line number of the assert macro.
230 @param[in] FileName Null-terminated ASCII string of the filename
231 executing the assert macro.
232 @param[in] DescriptionA Null-terminated ASCII string that is a description
234 @param[in] DescriptionB Null-terminated ASCII string that is a description
237 @retval TRUE ValueA is not equal to ValueB.
238 @retval FALSE ValueA is equal to ValueB.
242 UnitTestAssertNotEqual (
245 IN CONST CHAR8
*FunctionName
,
247 IN CONST CHAR8
*FileName
,
248 IN CONST CHAR8
*DescriptionA
,
249 IN CONST CHAR8
*DescriptionB
252 CHAR8 TempStr
[MAX_STRING_SIZE
];
254 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_NOT_EQUAL(%s:%llx, %s:%llx)", DescriptionA
, ValueA
, DescriptionB
, ValueB
);
255 _assert_true ((ValueA
!= ValueB
), TempStr
, FileName
, (INT32
)LineNumber
);
257 return (ValueA
!= ValueB
);
261 If Status is equal to Expected, then TRUE is returned.
262 If Status is not equal to Expected, then an assert is triggered and the
263 location of the assert provided by FunctionName, LineNumber, FileName, and
264 Description are recorded and FALSE is returned.
266 @param[in] Status EFI_STATUS value returned from an API under test.
267 @param[in] Expected The expected EFI_STATUS return value from an API
269 @param[in] FunctionName Null-terminated ASCII string of the function
270 executing the assert macro.
271 @param[in] LineNumber The source file line number of the assert macro.
272 @param[in] FileName Null-terminated ASCII string of the filename
273 executing the assert macro.
274 @param[in] Description Null-terminated ASCII string that is a description
277 @retval TRUE Status is equal to Expected.
278 @retval FALSE Status is not equal to Expected.
282 UnitTestAssertStatusEqual (
283 IN EFI_STATUS Status
,
284 IN EFI_STATUS Expected
,
285 IN CONST CHAR8
*FunctionName
,
287 IN CONST CHAR8
*FileName
,
288 IN CONST CHAR8
*Description
291 CHAR8 TempStr
[MAX_STRING_SIZE
];
293 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_STATUS_EQUAL(%s:%p)", Description
, (VOID
*)Status
);
294 _assert_true ((Status
== Expected
), TempStr
, FileName
, (INT32
)LineNumber
);
296 return (Status
== Expected
);
300 If Pointer is not equal to NULL, then TRUE is returned.
301 If Pointer is equal to NULL, then an assert is triggered and the location of
302 the assert provided by FunctionName, LineNumber, FileName, and PointerName
303 are recorded and FALSE is returned.
305 @param[in] Pointer Pointer value to be checked against NULL.
306 @param[in] Expected The expected EFI_STATUS return value from a function
308 @param[in] FunctionName Null-terminated ASCII string of the function
309 executing the assert macro.
310 @param[in] LineNumber The source file line number of the assert macro.
311 @param[in] FileName Null-terminated ASCII string of the filename
312 executing the assert macro.
313 @param[in] PointerName Null-terminated ASCII string that is a description
316 @retval TRUE Pointer is not equal to NULL.
317 @retval FALSE Pointer is equal to NULL.
321 UnitTestAssertNotNull (
323 IN CONST CHAR8
*FunctionName
,
325 IN CONST CHAR8
*FileName
,
326 IN CONST CHAR8
*PointerName
329 CHAR8 TempStr
[MAX_STRING_SIZE
];
331 snprintf (TempStr
, sizeof (TempStr
), "UT_ASSERT_NOT_NULL(%s:%p)", PointerName
, Pointer
);
332 _assert_true ((Pointer
!= NULL
), TempStr
, FileName
, (INT32
)LineNumber
);
334 return (Pointer
!= NULL
);
338 If UnitTestStatus is UNIT_TEST_PASSED, then log an info message and return
339 TRUE because an ASSERT() was expected when FunctionCall was executed and an
340 ASSERT() was triggered. If UnitTestStatus is UNIT_TEST_SKIPPED, then log a
341 warning message and return TRUE because ASSERT() macros are disabled. If
342 UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED, then log an error message and
343 return FALSE because an ASSERT() was expected when FunctionCall was executed,
344 but no ASSERT() conditions were triggered. The log messages contain
345 FunctionName, LineNumber, and FileName strings to provide the location of the
346 UT_EXPECT_ASSERT_FAILURE() macro.
348 @param[in] UnitTestStatus The status from UT_EXPECT_ASSERT_FAILURE() that
349 is either pass, skipped, or failed.
350 @param[in] FunctionName Null-terminated ASCII string of the function
351 executing the UT_EXPECT_ASSERT_FAILURE() macro.
352 @param[in] LineNumber The source file line number of the the function
353 executing the UT_EXPECT_ASSERT_FAILURE() macro.
354 @param[in] FileName Null-terminated ASCII string of the filename
355 executing the UT_EXPECT_ASSERT_FAILURE() macro.
356 @param[in] FunctionCall Null-terminated ASCII string of the function call
357 executed by the UT_EXPECT_ASSERT_FAILURE() macro.
358 @param[out] ResultStatus Used to return the UnitTestStatus value to the
359 caller of UT_EXPECT_ASSERT_FAILURE(). This is
360 optional parameter that may be NULL.
362 @retval TRUE UnitTestStatus is UNIT_TEST_PASSED.
363 @retval TRUE UnitTestStatus is UNIT_TEST_SKIPPED.
364 @retval FALSE UnitTestStatus is UNIT_TEST_ERROR_TEST_FAILED.
368 UnitTestExpectAssertFailure (
369 IN UNIT_TEST_STATUS UnitTestStatus
,
370 IN CONST CHAR8
*FunctionName
,
372 IN CONST CHAR8
*FileName
,
373 IN CONST CHAR8
*FunctionCall
,
374 OUT UNIT_TEST_STATUS
*ResultStatus OPTIONAL
377 CHAR8 TempStr
[MAX_STRING_SIZE
];
379 if (ResultStatus
!= NULL
) {
380 *ResultStatus
= UnitTestStatus
;
383 if (UnitTestStatus
== UNIT_TEST_PASSED
) {
385 "[ASSERT PASS] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) detected expected assert\n",
392 if (UnitTestStatus
== UNIT_TEST_SKIPPED
) {
394 "[ASSERT WARN] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) disabled\n",
401 if (UnitTestStatus
== UNIT_TEST_ERROR_TEST_FAILED
) {
402 snprintf (TempStr
, sizeof (TempStr
), "UT_EXPECT_ASSERT_FAILURE(%s) did not trigger ASSERT()", FunctionCall
);
403 _assert_true (FALSE
, TempStr
, FileName
, (INT32
)LineNumber
);
406 return (UnitTestStatus
!= UNIT_TEST_ERROR_TEST_FAILED
);