TianoCore EDK2 master
Loading...
Searching...
No Matches
SampleUnitTest.c
Go to the documentation of this file.
1
9#include <PiPei.h>
10#include <Uefi.h>
11#include <Library/UefiLib.h>
12#include <Library/DebugLib.h>
13#include <Library/UnitTestLib.h>
14#include <Library/PrintLib.h>
15
16#define UNIT_TEST_NAME "Sample Unit Test"
17#define UNIT_TEST_VERSION "0.1"
18
23VOID *mSampleGlobalTestPointer = NULL;
24
47EFIAPI
49 IN UNIT_TEST_CONTEXT Context
50 )
51{
52 UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)NULL);
53 return UNIT_TEST_PASSED;
54}
55
78VOID
79EFIAPI
81 IN UNIT_TEST_CONTEXT Context
82 )
83{
84 mSampleGlobalTestPointer = NULL;
85}
86
104EFIAPI
106 IN UNIT_TEST_CONTEXT Context
107 )
108{
109 UINTN A;
110 UINTN B;
111 UINTN C;
112
113 A = 1;
114 B = 1;
115 C = A + B;
116
117 UT_ASSERT_EQUAL (C, 2);
118
119 return UNIT_TEST_PASSED;
120}
121
138EFIAPI
140 IN UNIT_TEST_CONTEXT Context
141 )
142{
145
148
149 return UNIT_TEST_PASSED;
150}
151
169EFIAPI
171 IN UNIT_TEST_CONTEXT Context
172 )
173{
174 //
175 // Example of logging.
176 //
177 UT_LOG_WARNING ("About to change a global pointer! Current value is 0x%X\n", mSampleGlobalTestPointer);
178
179 mSampleGlobalTestPointer = (VOID *)-1;
180 UT_ASSERT_EQUAL ((UINTN)mSampleGlobalTestPointer, (UINTN)((VOID *)-1));
181 return UNIT_TEST_PASSED;
182}
183
187VOID
188EFIAPI
190 VOID
191 )
192{
193 //
194 // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
195 //
196 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);
197}
198
202VOID
203EFIAPI
205 VOID
206 )
207{
208 //
209 // Clear BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
210 //
211 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (~BIT0));
212}
213
230EFIAPI
232 IN UNIT_TEST_CONTEXT Context
233 )
234{
235 UINT64 Result;
236
237 //
238 // This test passes because expression always evaluated to TRUE.
239 //
241
242 //
243 // This test passes because expression always evaluates to TRUE.
244 //
245 Result = LShiftU64 (BIT0, 1);
246 UT_ASSERT_TRUE (Result == BIT1);
247
248 return UNIT_TEST_PASSED;
249}
250
267EFIAPI
269 IN UNIT_TEST_CONTEXT Context
270 )
271{
272 UINT64 Result;
273
274 //
275 // This test passes because expression always evaluated to FALSE.
276 //
278
279 //
280 // This test passes because expression always evaluates to FALSE.
281 //
282 Result = LShiftU64 (BIT0, 1);
283 UT_ASSERT_FALSE (Result == BIT0);
284
285 return UNIT_TEST_PASSED;
286}
287
304EFIAPI
306 IN UNIT_TEST_CONTEXT Context
307 )
308{
309 UINT64 Result;
310
311 //
312 // This test passes because both values are always equal.
313 //
314 UT_ASSERT_EQUAL (1, 1);
315
316 //
317 // This test passes because both values are always equal.
318 //
319 Result = LShiftU64 (BIT0, 1);
320 UT_ASSERT_EQUAL (Result, BIT1);
321
322 return UNIT_TEST_PASSED;
323}
324
341EFIAPI
343 IN UNIT_TEST_CONTEXT Context
344 )
345{
346 CHAR8 *String1;
347 CHAR8 *String2;
348 UINTN Length;
349
350 //
351 // This test passes because String1 and String2 are the same.
352 //
353 String1 = "Hello";
354 String2 = "Hello";
355 Length = sizeof ("Hello");
356 UT_ASSERT_MEM_EQUAL (String1, String2, Length);
357
358 return UNIT_TEST_PASSED;
359}
360
377EFIAPI
379 IN UNIT_TEST_CONTEXT Context
380 )
381{
382 UINT64 Result;
383
384 //
385 // This test passes because both values are never equal.
386 //
387 UT_ASSERT_NOT_EQUAL (0, 1);
388
389 //
390 // This test passes because both values are never equal.
391 //
392 Result = LShiftU64 (BIT0, 1);
393 UT_ASSERT_NOT_EQUAL (Result, BIT0);
394
395 return UNIT_TEST_PASSED;
396}
397
414EFIAPI
416 IN UNIT_TEST_CONTEXT Context
417 )
418{
419 //
420 // This test passes because the status is not an EFI error.
421 //
423
424 //
425 // This test passes because the status is not an EFI error.
426 //
427 UT_ASSERT_NOT_EFI_ERROR (EFI_WARN_BUFFER_TOO_SMALL);
428
429 return UNIT_TEST_PASSED;
430}
431
448EFIAPI
450 IN UNIT_TEST_CONTEXT Context
451 )
452{
453 //
454 // This test passes because the status value are always equal.
455 //
457
458 return UNIT_TEST_PASSED;
459}
460
477EFIAPI
479 IN UNIT_TEST_CONTEXT Context
480 )
481{
482 UINT64 Result;
483
484 //
485 // This test passes because the pointer is never NULL.
486 //
487 UT_ASSERT_NOT_NULL (&Result);
488
489 return UNIT_TEST_PASSED;
490}
491
508EFIAPI
510 IN UNIT_TEST_CONTEXT Context
511 )
512{
513 //
514 // This test passes because it directly triggers an ASSERT().
515 //
517
518 //
519 // This test passes because DecimalToBcd() generates an ASSERT() if the
520 // value passed in is >= 100. The expected ASSERT() is caught by the unit
521 // test framework and UT_EXPECT_ASSERT_FAILURE() returns without an error.
522 //
524
525 return UNIT_TEST_PASSED;
526}
527
544EFIAPI
546 IN UNIT_TEST_CONTEXT Context
547 )
548{
549 //
550 // Example of logging.
551 //
552 UT_LOG_ERROR ("UT_LOG_ERROR() message\n");
553
554 return UNIT_TEST_PASSED;
555}
556
573EFIAPI
575 IN UNIT_TEST_CONTEXT Context
576 )
577{
578 //
579 // Example of logging.
580 //
581 UT_LOG_WARNING ("UT_LOG_WARNING() message\n");
582
583 return UNIT_TEST_PASSED;
584}
585
602EFIAPI
604 IN UNIT_TEST_CONTEXT Context
605 )
606{
607 //
608 // Example of logging.
609 //
610 UT_LOG_INFO ("UT_LOG_INFO() message\n");
611
612 return UNIT_TEST_PASSED;
613}
614
631EFIAPI
633 IN UNIT_TEST_CONTEXT Context
634 )
635{
636 //
637 // Example of logging.
638 //
639 UT_LOG_VERBOSE ("UT_LOG_VERBOSE() message\n");
640
641 return UNIT_TEST_PASSED;
642}
643
653EFIAPI
655 VOID
656 )
657{
658 EFI_STATUS Status;
659 UNIT_TEST_FRAMEWORK_HANDLE Framework;
660 UNIT_TEST_SUITE_HANDLE SimpleMathTests;
661 UNIT_TEST_SUITE_HANDLE GlobalVarTests;
662 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsEnabled;
663 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsDisabled;
664
665 Framework = NULL;
666
667 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
668
669 //
670 // Start setting up the test framework for running the tests.
671 //
672 Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
673 if (EFI_ERROR (Status)) {
674 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
675 goto EXIT;
676 }
677
678 //
679 // Populate the SimpleMathTests Unit Test Suite.
680 //
681 Status = CreateUnitTestSuite (&SimpleMathTests, Framework, "Simple Math Tests", "Sample.Math", NULL, NULL);
682 if (EFI_ERROR (Status)) {
683 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for SimpleMathTests\n"));
684 Status = EFI_OUT_OF_RESOURCES;
685 goto EXIT;
686 }
687
688 AddTestCase (SimpleMathTests, "Adding 1 to 1 should produce 2", "Addition", OnePlusOneShouldEqualTwo, NULL, NULL, NULL);
689
690 //
691 // Populate the GlobalVarTests Unit Test Suite.
692 //
693 Status = CreateUnitTestSuite (&GlobalVarTests, Framework, "Global Variable Tests", "Sample.Globals", NULL, NULL);
694 if (EFI_ERROR (Status)) {
695 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for GlobalVarTests\n"));
696 Status = EFI_OUT_OF_RESOURCES;
697 goto EXIT;
698 }
699
700 AddTestCase (GlobalVarTests, "You should be able to change a global BOOLEAN", "Boolean", GlobalBooleanShouldBeChangeable, NULL, NULL, NULL);
701 AddTestCase (GlobalVarTests, "You should be able to change a global pointer", "Pointer", GlobalPointerShouldBeChangeable, MakeSureThatPointerIsNull, ClearThePointer, NULL);
702
703 //
704 // Populate the Macro Tests with ASSERT() enabled
705 //
706 Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);
707 if (EFI_ERROR (Status)) {
708 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));
709 Status = EFI_OUT_OF_RESOURCES;
710 goto EXIT;
711 }
712
713 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_TRUE() macro", "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL);
714 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_FALSE() macro", "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL);
715 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_EQUAL() macro", "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL);
716 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_MEM_EQUAL() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
717 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EQUAL() macro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL);
718 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_EFI_ERROR() macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL, NULL);
719 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_STATUS_EQUAL() macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, NULL);
720 AddTestCase (MacroTestsAssertsEnabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
721 AddTestCase (MacroTestsAssertsEnabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);
722
723 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);
724 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);
725 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
726 AddTestCase (MacroTestsAssertsEnabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
727
728 //
729 // Populate the Macro Tests with ASSERT() disabled
730 //
731 Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);
732 if (EFI_ERROR (Status)) {
733 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));
734 Status = EFI_OUT_OF_RESOURCES;
735 goto EXIT;
736 }
737
738 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_TRUE() macro", "MacroUtAssertTrue", MacroUtAssertTrue, NULL, NULL, NULL);
739 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_FALSE() macro", "MacroUtAssertFalse", MacroUtAssertFalse, NULL, NULL, NULL);
740 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_EQUAL() macro", "MacroUtAssertEqual", MacroUtAssertEqual, NULL, NULL, NULL);
741 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_MEM_EQUAL() macro", "MacroUtAssertMemEqual", MacroUtAssertMemEqual, NULL, NULL, NULL);
742 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EQUAL() macro", "MacroUtAssertNotEqual", MacroUtAssertNotEqual, NULL, NULL, NULL);
743 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_EFI_ERROR() macro", "MacroUtAssertNotEfiError", MacroUtAssertNotEfiError, NULL, NULL, NULL);
744 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_STATUS_EQUAL() macro", "MacroUtAssertStatusEqual", MacroUtAssertStatusEqual, NULL, NULL, NULL);
745 AddTestCase (MacroTestsAssertsDisabled, "Test UT_ASSERT_NOT_NULL() macro", "MacroUtAssertNotNull", MacroUtAssertNotNull, NULL, NULL, NULL);
746 AddTestCase (MacroTestsAssertsDisabled, "Test UT_EXPECT_ASSERT_FAILURE() macro", "MacroUtExpectAssertFailure", MacroUtExpectAssertFailure, NULL, NULL, NULL);
747
748 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_ERROR() macro", "MacroUtLogError", MacroUtLogError, NULL, NULL, NULL);
749 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_WARNING() macro", "MacroUtLogWarning", MacroUtLogWarning, NULL, NULL, NULL);
750 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_INFO() macro", "MacroUtLogInfo", MacroUtLogInfo, NULL, NULL, NULL);
751 AddTestCase (MacroTestsAssertsDisabled, "Test UT_LOG_VERBOSE() macro", "MacroUtLogVerbose", MacroUtLogVerbose, NULL, NULL, NULL);
752
753 //
754 // Execute the tests.
755 //
756 Status = RunAllTestSuites (Framework);
757
758EXIT:
759 if (Framework) {
760 FreeUnitTestFramework (Framework);
761 }
762
763 return Status;
764}
765
770EFIAPI
772 IN EFI_PEI_FILE_HANDLE FileHandle,
773 IN CONST EFI_PEI_SERVICES **PeiServices
774 )
775{
776 return UefiTestMain ();
777}
778
784EFIAPI
786 IN EFI_HANDLE ImageHandle,
787 IN EFI_SYSTEM_TABLE *SystemTable
788 )
789{
790 return UefiTestMain ();
791}
792
796int
798 int argc,
799 char *argv[]
800 )
801{
802 return UefiTestMain ();
803}
UINT64 UINTN
UINT8 EFIAPI DecimalToBcd8(IN UINT8 Value)
Definition: String.c:1653
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
int main()
=== TEST ENGINE ================================================================================
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define TRUE
Definition: Base.h:301
#define FALSE
Definition: Base.h:307
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
#define PcdGet8(TokenName)
Definition: PcdLib.h:336
#define PatchPcdSet8(TokenName, Value)
Definition: PcdLib.h:233
VOID * EFI_PEI_FILE_HANDLE
Definition: PiPeiCis.h:26
UNIT_TEST_STATUS EFIAPI OnePlusOneShouldEqualTwo(IN UNIT_TEST_CONTEXT Context)
VOID EFIAPI ClearThePointer(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtLogInfo(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI UefiTestMain(VOID)
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEfiError(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI GlobalPointerShouldBeChangeable(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertTrue(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertEqual(IN UNIT_TEST_CONTEXT Context)
VOID EFIAPI TestSuiteDisableAsserts(VOID)
UNIT_TEST_STATUS EFIAPI GlobalBooleanShouldBeChangeable(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtLogError(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotEqual(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertNotNull(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertStatusEqual(IN UNIT_TEST_CONTEXT Context)
VOID EFIAPI TestSuiteEnableAsserts(VOID)
EFI_STATUS EFIAPI DxeEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
UNIT_TEST_STATUS EFIAPI MacroUtAssertMemEqual(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtExpectAssertFailure(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtLogVerbose(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtLogWarning(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI PeiEntryPoint(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
UNIT_TEST_STATUS EFIAPI MakeSureThatPointerIsNull(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI MacroUtAssertFalse(IN UNIT_TEST_CONTEXT Context)
BOOLEAN mSampleGlobalTestBoolean
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
#define EFI_SUCCESS
Definition: UefiBaseType.h:112
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
#define UT_ASSERT_NOT_NULL(Pointer)
Definition: UnitTestLib.h:439
#define UT_LOG_ERROR(Format,...)
Definition: UnitTestLib.h:791
#define UT_ASSERT_NOT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:402
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_EXPECT_ASSERT_FAILURE(FunctionCall, Status)
Definition: UnitTestLib.h:491
#define UT_ASSERT_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
#define UT_ASSERT_STATUS_EQUAL(Status, Expected)
Definition: UnitTestLib.h:427
#define UT_LOG_INFO(Format,...)
Definition: UnitTestLib.h:813
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
EFI_STATUS EFIAPI FreeUnitTestFramework(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: UnitTestLib.c:150
#define UT_LOG_VERBOSE(Format,...)
Definition: UnitTestLib.h:824
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
#define UT_LOG_WARNING(Format,...)
Definition: UnitTestLib.h:802
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
#define UT_ASSERT_FALSE(Expression)
Definition: UnitTestLib.h:362
#define UT_ASSERT_NOT_EFI_ERROR(Status)
Definition: UnitTestLib.h:414