TianoCore EDK2 master
Loading...
Searching...
No Matches
SampleUnitTestGenerateException.c
Go to the documentation of this file.
1
13#include <PiPei.h>
14#include <Uefi.h>
15#include <Library/UefiLib.h>
16#include <Library/DebugLib.h>
17#include <Library/UnitTestLib.h>
18#include <Library/PrintLib.h>
19
20#define UNIT_TEST_NAME "Sample Unit Test Generate Exception"
21#define UNIT_TEST_VERSION "0.1"
22
26VOID
27EFIAPI
29 VOID
30 )
31{
32 //
33 // Set BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
34 //
35 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) | BIT0);
36}
37
41VOID
42EFIAPI
44 VOID
45 )
46{
47 //
48 // Clear BIT0 (DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED)
49 //
50 PatchPcdSet8 (PcdDebugPropertyMask, PcdGet8 (PcdDebugPropertyMask) & (~BIT0));
51}
52
54DivideWithNoParameterChecking (
55 UINTN Dividend,
56 UINTN Divisor
57 )
58{
59 //
60 // Perform integer division with no check for divide by zero
61 //
62 return (Dividend / Divisor);
63}
64
81EFIAPI
83 IN UNIT_TEST_CONTEXT Context
84 )
85{
86 //
87 // Assertion that passes without generating an exception
88 //
89 UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 1), (UINTN)20);
90 //
91 // Assertion that generates divide by zero exception before result evaluated
92 //
93 UT_ASSERT_EQUAL (DivideWithNoParameterChecking (20, 0), MAX_UINTN);
94
95 return UNIT_TEST_PASSED;
96}
97
107EFIAPI
109 VOID
110 )
111{
112 EFI_STATUS Status;
113 UNIT_TEST_FRAMEWORK_HANDLE Framework;
114 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsEnabled;
115 UNIT_TEST_SUITE_HANDLE MacroTestsAssertsDisabled;
116
117 Framework = NULL;
118
119 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
120
121 //
122 // Start setting up the test framework for running the tests.
123 //
124 Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
125 if (EFI_ERROR (Status)) {
126 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
127 goto EXIT;
128 }
129
130 //
131 // Populate the Macro Tests with ASSERT() enabled
132 //
133 Status = CreateUnitTestSuite (&MacroTestsAssertsEnabled, Framework, "Macro Tests with ASSERT() enabled", "Sample.MacroAssertsEnabled", TestSuiteEnableAsserts, NULL);
134 if (EFI_ERROR (Status)) {
135 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsEnabled\n"));
136 Status = EFI_OUT_OF_RESOURCES;
137 goto EXIT;
138 }
139
140 AddTestCase (MacroTestsAssertsEnabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL);
141
142 //
143 // Populate the Macro Tests with ASSERT() disabled
144 //
145 Status = CreateUnitTestSuite (&MacroTestsAssertsDisabled, Framework, "Macro Tests with ASSERT() disabled", "Sample.MacroAssertsDisables", TestSuiteDisableAsserts, NULL);
146 if (EFI_ERROR (Status)) {
147 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for MacroTestsAssertsDisabled\n"));
148 Status = EFI_OUT_OF_RESOURCES;
149 goto EXIT;
150 }
151
152 AddTestCase (MacroTestsAssertsDisabled, "Test Unexpected Exception", "GenerateUnexpectedException", GenerateUnexpectedException, NULL, NULL, NULL);
153
154 //
155 // Execute the tests.
156 //
157 Status = RunAllTestSuites (Framework);
158
159EXIT:
160 if (Framework) {
161 FreeUnitTestFramework (Framework);
162 }
163
164 return Status;
165}
166
171EFIAPI
173 IN EFI_PEI_FILE_HANDLE FileHandle,
174 IN CONST EFI_PEI_SERVICES **PeiServices
175 )
176{
177 return UefiTestMain ();
178}
179
185EFIAPI
187 IN EFI_HANDLE ImageHandle,
188 IN EFI_SYSTEM_TABLE *SystemTable
189 )
190{
191 return UefiTestMain ();
192}
193
197int
199 int argc,
200 char *argv[]
201 )
202{
203 return UefiTestMain ();
204}
UINT64 UINTN
int main()
=== TEST ENGINE ================================================================================
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#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 GenerateUnexpectedException(IN UNIT_TEST_CONTEXT Context)
EFI_STATUS EFIAPI UefiTestMain(VOID)
VOID EFIAPI TestSuiteDisableAsserts(VOID)
VOID EFIAPI TestSuiteEnableAsserts(VOID)
EFI_STATUS EFIAPI DxeEntryPoint(IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable)
EFI_STATUS EFIAPI PeiEntryPoint(IN EFI_PEI_FILE_HANDLE FileHandle, IN CONST EFI_PEI_SERVICES **PeiServices)
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
VOID * EFI_HANDLE
Definition: UefiBaseType.h:33
EFI_STATUS EFIAPI RunAllTestSuites(IN UNIT_TEST_FRAMEWORK_HANDLE FrameworkHandle)
Definition: RunTests.c:145
VOID * UNIT_TEST_CONTEXT
Definition: UnitTestLib.h:54
#define UT_ASSERT_EQUAL(ValueA, ValueB)
Definition: UnitTestLib.h:375
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
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
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