TianoCore EDK2 master
Loading...
Searching...
No Matches
UnitTestLib.h
Go to the documentation of this file.
1
10#ifndef __UNIT_TEST_LIB_H__
11#define __UNIT_TEST_LIB_H__
12
16typedef UINT32 UNIT_TEST_STATUS;
17#define UNIT_TEST_PASSED (0)
18#define UNIT_TEST_ERROR_PREREQUISITE_NOT_MET (1)
19#define UNIT_TEST_ERROR_TEST_FAILED (2)
20#define UNIT_TEST_ERROR_CLEANUP_FAILED (3)
21#define UNIT_TEST_SKIPPED (0xFFFFFFFD)
22#define UNIT_TEST_RUNNING (0xFFFFFFFE)
23#define UNIT_TEST_PENDING (0xFFFFFFFF)
24
28#define UNIT_TEST_LOG_LEVEL_ERROR BIT0
29#define UNIT_TEST_LOG_LEVEL_WARN BIT1
30#define UNIT_TEST_LOG_LEVEL_INFO BIT2
31#define UNIT_TEST_LOG_LEVEL_VERBOSE BIT3
32
36struct UNIT_TEST_FRAMEWORK_OBJECT;
37typedef struct UNIT_TEST_FRAMEWORK_OBJECT *UNIT_TEST_FRAMEWORK_HANDLE;
38
42struct UNIT_TEST_SUITE_OBJECT;
43typedef struct UNIT_TEST_SUITE_OBJECT *UNIT_TEST_SUITE_HANDLE;
44
48struct UNIT_TEST_OBJECT;
49typedef struct UNIT_TEST_OBJECT *UNIT_TEST_HANDLE;
50
54typedef VOID *UNIT_TEST_CONTEXT;
55
75typedef
77(EFIAPI *UNIT_TEST_FUNCTION)(
78 IN UNIT_TEST_CONTEXT Context
79 );
80
101typedef
103(EFIAPI *UNIT_TEST_PREREQUISITE)(
104 IN UNIT_TEST_CONTEXT Context
105 );
106
128typedef
129VOID
130(EFIAPI *UNIT_TEST_CLEANUP)(
131 IN UNIT_TEST_CONTEXT Context
132 );
133
142typedef
143VOID
144(EFIAPI *UNIT_TEST_SUITE_SETUP)(
145 VOID
146 );
147
156typedef
157VOID
159 VOID
160 );
161
187EFIAPI
189 OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle,
190 IN CHAR8 *Title,
191 IN CHAR8 *ShortTitle,
192 IN CHAR8 *VersionString
193 );
194
222EFIAPI
224 OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle,
225 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle,
226 IN CHAR8 *Title,
227 IN CHAR8 *Name,
228 IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL,
229 IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL
230 );
231
257EFIAPI
259 IN UNIT_TEST_SUITE_HANDLE SuiteHandle,
260 IN CHAR8 *Description,
261 IN CHAR8 *Name,
262 IN UNIT_TEST_FUNCTION Function,
263 IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL,
264 IN UNIT_TEST_CLEANUP CleanUp OPTIONAL,
265 IN UNIT_TEST_CONTEXT Context OPTIONAL
266 );
267
283EFIAPI
285 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
286 );
287
303EFIAPI
305 IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle
306 );
307
337EFIAPI
339 IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL,
340 IN UINTN ContextToSaveSize
341 );
342
350#define UT_ASSERT_TRUE(Expression) \
351 if(!UnitTestAssertTrue ((Expression), __func__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \
352 return UNIT_TEST_ERROR_TEST_FAILED; \
353 }
354
362#define UT_ASSERT_FALSE(Expression) \
363 if(!UnitTestAssertFalse ((Expression), __func__, DEBUG_LINE_NUMBER, __FILE__, #Expression)) { \
364 return UNIT_TEST_ERROR_TEST_FAILED; \
365 }
366
375#define UT_ASSERT_EQUAL(ValueA, ValueB) \
376 if(!UnitTestAssertEqual ((UINT64)(ValueA), (UINT64)(ValueB), __func__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \
377 return UNIT_TEST_ERROR_TEST_FAILED; \
378 }
379
389#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length) \
390 if(!UnitTestAssertMemEqual ((VOID *)(UINTN)(BufferA), (VOID *)(UINTN)(BufferB), (UINTN)Length, __func__, DEBUG_LINE_NUMBER, __FILE__, #BufferA, #BufferB)) { \
391 return UNIT_TEST_ERROR_TEST_FAILED; \
392 }
393
402#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB) \
403 if(!UnitTestAssertNotEqual ((UINT64)(ValueA), (UINT64)(ValueB), __func__, DEBUG_LINE_NUMBER, __FILE__, #ValueA, #ValueB)) { \
404 return UNIT_TEST_ERROR_TEST_FAILED; \
405 }
406
414#define UT_ASSERT_NOT_EFI_ERROR(Status) \
415 if(!UnitTestAssertNotEfiError ((Status), __func__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \
416 return UNIT_TEST_ERROR_TEST_FAILED; \
417 }
418
427#define UT_ASSERT_STATUS_EQUAL(Status, Expected) \
428 if(!UnitTestAssertStatusEqual ((Status), (Expected), __func__, DEBUG_LINE_NUMBER, __FILE__, #Status)) { \
429 return UNIT_TEST_ERROR_TEST_FAILED; \
430 }
431
439#define UT_ASSERT_NOT_NULL(Pointer) \
440 if(!UnitTestAssertNotNull ((Pointer), __func__, DEBUG_LINE_NUMBER, __FILE__, #Pointer)) { \
441 return UNIT_TEST_ERROR_TEST_FAILED; \
442 }
443
459#if defined (EDKII_UNIT_TEST_FRAMEWORK_ENABLED)
460 #include <Library/BaseLib.h>
461
466extern BASE_LIBRARY_JUMP_BUFFER *gUnitTestExpectAssertFailureJumpBuffer;
467
468#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) \
469 do { \
470 UNIT_TEST_STATUS UnitTestJumpStatus; \
471 BASE_LIBRARY_JUMP_BUFFER UnitTestJumpBuffer; \
472 UnitTestJumpStatus = UNIT_TEST_SKIPPED; \
473 if (DebugAssertEnabled ()) { \
474 gUnitTestExpectAssertFailureJumpBuffer = &UnitTestJumpBuffer; \
475 if (SetJump (gUnitTestExpectAssertFailureJumpBuffer) == 0) { \
476 FunctionCall; \
477 UnitTestJumpStatus = UNIT_TEST_ERROR_TEST_FAILED; \
478 } else { \
479 UnitTestJumpStatus = UNIT_TEST_PASSED; \
480 } \
481 gUnitTestExpectAssertFailureJumpBuffer = NULL; \
482 } \
483 if (!UnitTestExpectAssertFailure ( \
484 UnitTestJumpStatus, \
485 __func__, DEBUG_LINE_NUMBER, __FILE__, \
486 #FunctionCall, Status)) { \
487 return UNIT_TEST_ERROR_TEST_FAILED; \
488 } \
489 } while (FALSE)
490#else
491#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status) FunctionCall;
492#endif
493
512BOOLEAN
513EFIAPI
515 IN BOOLEAN Expression,
516 IN CONST CHAR8 *FunctionName,
517 IN UINTN LineNumber,
518 IN CONST CHAR8 *FileName,
519 IN CONST CHAR8 *Description
520 );
521
540BOOLEAN
541EFIAPI
543 IN BOOLEAN Expression,
544 IN CONST CHAR8 *FunctionName,
545 IN UINTN LineNumber,
546 IN CONST CHAR8 *FileName,
547 IN CONST CHAR8 *Description
548 );
549
568BOOLEAN
569EFIAPI
571 IN EFI_STATUS Status,
572 IN CONST CHAR8 *FunctionName,
573 IN UINTN LineNumber,
574 IN CONST CHAR8 *FileName,
575 IN CONST CHAR8 *Description
576 );
577
599BOOLEAN
600EFIAPI
602 IN UINT64 ValueA,
603 IN UINT64 ValueB,
604 IN CONST CHAR8 *FunctionName,
605 IN UINTN LineNumber,
606 IN CONST CHAR8 *FileName,
607 IN CONST CHAR8 *DescriptionA,
608 IN CONST CHAR8 *DescriptionB
609 );
610
636BOOLEAN
637EFIAPI
639 IN VOID *BufferA,
640 IN VOID *BufferB,
641 IN UINTN Length,
642 IN CONST CHAR8 *FunctionName,
643 IN UINTN LineNumber,
644 IN CONST CHAR8 *FileName,
645 IN CONST CHAR8 *DescriptionA,
646 IN CONST CHAR8 *DescriptionB
647 );
648
670BOOLEAN
671EFIAPI
673 IN UINT64 ValueA,
674 IN UINT64 ValueB,
675 IN CONST CHAR8 *FunctionName,
676 IN UINTN LineNumber,
677 IN CONST CHAR8 *FileName,
678 IN CONST CHAR8 *DescriptionA,
679 IN CONST CHAR8 *DescriptionB
680 );
681
702BOOLEAN
703EFIAPI
705 IN EFI_STATUS Status,
706 IN EFI_STATUS Expected,
707 IN CONST CHAR8 *FunctionName,
708 IN UINTN LineNumber,
709 IN CONST CHAR8 *FileName,
710 IN CONST CHAR8 *Description
711 );
712
733BOOLEAN
734EFIAPI
736 IN VOID *Pointer,
737 IN CONST CHAR8 *FunctionName,
738 IN UINTN LineNumber,
739 IN CONST CHAR8 *FileName,
740 IN CONST CHAR8 *PointerName
741 );
742
772BOOLEAN
773EFIAPI
775 IN UNIT_TEST_STATUS UnitTestStatus,
776 IN CONST CHAR8 *FunctionName,
777 IN UINTN LineNumber,
778 IN CONST CHAR8 *FileName,
779 IN CONST CHAR8 *FunctionCall,
780 OUT UNIT_TEST_STATUS *ResultStatus OPTIONAL
781 );
782
791#define UT_LOG_ERROR(Format, ...) \
792 UnitTestLog (UNIT_TEST_LOG_LEVEL_ERROR, Format, ##__VA_ARGS__)
793
802#define UT_LOG_WARNING(Format, ...) \
803 UnitTestLog (UNIT_TEST_LOG_LEVEL_WARN, Format, ##__VA_ARGS__)
804
813#define UT_LOG_INFO(Format, ...) \
814 UnitTestLog (UNIT_TEST_LOG_LEVEL_INFO, Format, ##__VA_ARGS__)
815
824#define UT_LOG_VERBOSE(Format, ...) \
825 UnitTestLog (UNIT_TEST_LOG_LEVEL_VERBOSE, Format, ##__VA_ARGS__)
826
836VOID
837EFIAPI
839 IN UINTN ErrorLevel,
840 IN CONST CHAR8 *Format,
841 ...
842 );
843
844#endif
UINT64 UINTN
#define CONST
Definition: Base.h:259
#define IN
Definition: Base.h:279
#define OUT
Definition: Base.h:284
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
BASE_LIBRARY_JUMP_BUFFER * gUnitTestExpectAssertFailureJumpBuffer
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: Assert.c:250
EFI_STATUS EFIAPI SaveFrameworkState(IN UNIT_TEST_CONTEXT ContextToSave OPTIONAL, IN UINTN ContextToSaveSize)
Definition: UnitTestLib.c:818
BOOLEAN EFIAPI UnitTestAssertNotNull(IN VOID *Pointer, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *PointerName)
Definition: Assert.c:481
VOID EFIAPI UnitTestLog(IN UINTN ErrorLevel, IN CONST CHAR8 *Format,...)
Definition: Log.c:155
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
BOOLEAN EFIAPI UnitTestAssertFalse(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:151
UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_PREREQUISITE)(IN UNIT_TEST_CONTEXT Context)
Definition: UnitTestLib.h:103
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
VOID(EFIAPI * UNIT_TEST_SUITE_TEARDOWN)(VOID)
Definition: UnitTestLib.h:158
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: Assert.c:539
BOOLEAN EFIAPI UnitTestAssertTrue(IN BOOLEAN Expression, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:104
EFI_STATUS EFIAPI CreateUnitTestSuite(OUT UNIT_TEST_SUITE_HANDLE *SuiteHandle, IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *Name, IN UNIT_TEST_SUITE_SETUP Setup OPTIONAL, IN UNIT_TEST_SUITE_TEARDOWN Teardown OPTIONAL)
Definition: UnitTestLib.c:326
BOOLEAN EFIAPI UnitTestAssertNotEfiError(IN EFI_STATUS Status, IN CONST CHAR8 *FunctionName, IN UINTN LineNumber, IN CONST CHAR8 *FileName, IN CONST CHAR8 *Description)
Definition: Assert.c:198
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
EFI_STATUS EFIAPI AddTestCase(IN UNIT_TEST_SUITE_HANDLE SuiteHandle, IN CHAR8 *Description, IN CHAR8 *Name, IN UNIT_TEST_FUNCTION Function, IN UNIT_TEST_PREREQUISITE Prerequisite OPTIONAL, IN UNIT_TEST_CLEANUP CleanUp OPTIONAL, IN UNIT_TEST_CONTEXT Context OPTIONAL)
Definition: UnitTestLib.c:426
VOID(EFIAPI * UNIT_TEST_SUITE_SETUP)(VOID)
Definition: UnitTestLib.h:144
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: Assert.c:370
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: Assert.c:427
EFI_STATUS EFIAPI InitUnitTestFramework(OUT UNIT_TEST_FRAMEWORK_HANDLE *FrameworkHandle, IN CHAR8 *Title, IN CHAR8 *ShortTitle, IN CHAR8 *VersionString)
Definition: UnitTestLib.c:204
UINT32 UNIT_TEST_STATUS
Definition: UnitTestLib.h:16
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: Assert.c:312
UNIT_TEST_STATUS(EFIAPI * UNIT_TEST_FUNCTION)(IN UNIT_TEST_CONTEXT Context)
Definition: UnitTestLib.h:77
VOID(EFIAPI * UNIT_TEST_CLEANUP)(IN UNIT_TEST_CONTEXT Context)
Definition: UnitTestLib.h:130