TianoCore EDK2 master
Loading...
Searching...
No Matches
AssertCmocka.c
Go to the documentation of this file.
1
8#include <stdio.h>
9#include <string.h>
10#include <stdarg.h>
11#include <stddef.h>
12#include <setjmp.h>
13#include <cmocka.h>
14
15#include <Uefi.h>
17#include <Library/UnitTestLib.h>
18
19#define MAX_STRING_SIZE 1025
20
39BOOLEAN
40EFIAPI
42 IN BOOLEAN Expression,
43 IN CONST CHAR8 *FunctionName,
44 IN UINTN LineNumber,
45 IN CONST CHAR8 *FileName,
46 IN CONST CHAR8 *Description
47 )
48{
49 CHAR8 TempStr[MAX_STRING_SIZE];
50
51 snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_TRUE(%s:%x)", Description, Expression);
52 _assert_true (Expression, TempStr, FileName, (INT32)LineNumber);
53
54 return Expression;
55}
56
75BOOLEAN
76EFIAPI
78 IN BOOLEAN Expression,
79 IN CONST CHAR8 *FunctionName,
80 IN UINTN LineNumber,
81 IN CONST CHAR8 *FileName,
82 IN CONST CHAR8 *Description
83 )
84{
85 CHAR8 TempStr[MAX_STRING_SIZE];
86
87 snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_FALSE(%s:%x)", Description, Expression);
88 _assert_true (!Expression, TempStr, FileName, (INT32)LineNumber);
89
90 return !Expression;
91}
92
111BOOLEAN
112EFIAPI
114 IN EFI_STATUS Status,
115 IN CONST CHAR8 *FunctionName,
116 IN UINTN LineNumber,
117 IN CONST CHAR8 *FileName,
118 IN CONST CHAR8 *Description
119 )
120{
121 CHAR8 TempStr[MAX_STRING_SIZE];
122
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);
125
126 return !EFI_ERROR (Status);
127}
128
150BOOLEAN
151EFIAPI
153 IN UINT64 ValueA,
154 IN UINT64 ValueB,
155 IN CONST CHAR8 *FunctionName,
156 IN UINTN LineNumber,
157 IN CONST CHAR8 *FileName,
158 IN CONST CHAR8 *DescriptionA,
159 IN CONST CHAR8 *DescriptionB
160 )
161{
162 CHAR8 TempStr[MAX_STRING_SIZE];
163
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);
166
167 return (ValueA == ValueB);
168}
169
195BOOLEAN
196EFIAPI
198 IN VOID *BufferA,
199 IN VOID *BufferB,
200 IN UINTN Length,
201 IN CONST CHAR8 *FunctionName,
202 IN UINTN LineNumber,
203 IN CONST CHAR8 *FileName,
204 IN CONST CHAR8 *DescriptionA,
205 IN CONST CHAR8 *DescriptionB
206 )
207{
208 CHAR8 TempStr[MAX_STRING_SIZE];
209 BOOLEAN Result;
210
211 Result = (CompareMem (BufferA, BufferB, Length) == 0);
212
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);
215
216 return Result;
217}
218
240BOOLEAN
241EFIAPI
243 IN UINT64 ValueA,
244 IN UINT64 ValueB,
245 IN CONST CHAR8 *FunctionName,
246 IN UINTN LineNumber,
247 IN CONST CHAR8 *FileName,
248 IN CONST CHAR8 *DescriptionA,
249 IN CONST CHAR8 *DescriptionB
250 )
251{
252 CHAR8 TempStr[MAX_STRING_SIZE];
253
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);
256
257 return (ValueA != ValueB);
258}
259
280BOOLEAN
281EFIAPI
283 IN EFI_STATUS Status,
284 IN EFI_STATUS Expected,
285 IN CONST CHAR8 *FunctionName,
286 IN UINTN LineNumber,
287 IN CONST CHAR8 *FileName,
288 IN CONST CHAR8 *Description
289 )
290{
291 CHAR8 TempStr[MAX_STRING_SIZE];
292
293 snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_STATUS_EQUAL(%s:%p expected:%p)", Description, (VOID *)Status, (VOID *)Expected);
294 _assert_true ((Status == Expected), TempStr, FileName, (INT32)LineNumber);
295
296 return (Status == Expected);
297}
298
319BOOLEAN
320EFIAPI
322 IN VOID *Pointer,
323 IN CONST CHAR8 *FunctionName,
324 IN UINTN LineNumber,
325 IN CONST CHAR8 *FileName,
326 IN CONST CHAR8 *PointerName
327 )
328{
329 CHAR8 TempStr[MAX_STRING_SIZE];
330
331 snprintf (TempStr, sizeof (TempStr), "UT_ASSERT_NOT_NULL(%s:%p)", PointerName, Pointer);
332 _assert_true ((Pointer != NULL), TempStr, FileName, (INT32)LineNumber);
333
334 return (Pointer != NULL);
335}
336
366BOOLEAN
367EFIAPI
369 IN UNIT_TEST_STATUS UnitTestStatus,
370 IN CONST CHAR8 *FunctionName,
371 IN UINTN LineNumber,
372 IN CONST CHAR8 *FileName,
373 IN CONST CHAR8 *FunctionCall,
374 OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL
375 )
376{
377 CHAR8 TempStr[MAX_STRING_SIZE];
378
379 if (ResultStatus != NULL) {
380 *ResultStatus = UnitTestStatus;
381 }
382
383 if (UnitTestStatus == UNIT_TEST_PASSED) {
385 "[ASSERT PASS] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) detected expected assert\n",
386 FileName,
387 LineNumber,
388 FunctionCall
389 );
390 }
391
392 if (UnitTestStatus == UNIT_TEST_SKIPPED) {
394 "[ASSERT WARN] %a:%d: UT_EXPECT_ASSERT_FAILURE(%a) disabled\n",
395 FileName,
396 LineNumber,
397 FunctionCall
398 );
399 }
400
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);
404 }
405
406 return (UnitTestStatus != UNIT_TEST_ERROR_TEST_FAILED);
407}
UINT64 UINTN
BOOLEAN EFIAPI UnitTestAssertEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: AssertCmocka.c:152
BOOLEAN EFIAPI UnitTestAssertNotNull(IN VOID *Pointer, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *PointerName)
Definition: AssertCmocka.c:321
BOOLEAN EFIAPI UnitTestAssertFalse(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: AssertCmocka.c:77
BOOLEAN EFIAPI UnitTestExpectAssertFailure(IN UNIT_TEST_STATUS UnitTestStatus, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *FunctionCall, OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL)
Definition: AssertCmocka.c:368
BOOLEAN EFIAPI UnitTestAssertTrue(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: AssertCmocka.c:41
BOOLEAN EFIAPI UnitTestAssertNotEfiError(IN EFI_STATUS Status, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: AssertCmocka.c:113
BOOLEAN EFIAPI UnitTestAssertNotEqual(IN UINT64 ValueA, IN UINT64 ValueB, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: AssertCmocka.c:242
BOOLEAN EFIAPI UnitTestAssertStatusEqual(IN EFI_STATUS Status, IN EFI_STATUS Expected, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: AssertCmocka.c:282
BOOLEAN EFIAPI UnitTestAssertMemEqual(IN VOID *BufferA, IN VOID *BufferB, IN UINTN Length, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *DescriptionA, IN CONST CHAR8 *DescriptionB)
Definition: AssertCmocka.c:197
INTN EFIAPI CompareMem(IN CONST VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define UT_LOG_INFO(Format,...)
Definition: UnitTestLib.h:813
#define UT_LOG_WARNING(Format,...)
Definition: UnitTestLib.h:802
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16