TianoCore EDK2 master
Loading...
Searching...
No Matches
UefiSortLibUnitTest.c
Go to the documentation of this file.
1
9#include <stdio.h>
10#include <string.h>
11#include <stdarg.h>
12#include <stddef.h>
13#include <setjmp.h>
14#include <cmocka.h>
15
16#include <Uefi.h>
17#include <Library/BaseLib.h>
18#include <Library/DebugLib.h>
20
21#include <Library/UnitTestLib.h>
22#include <Library/SortLib.h>
23
24#define UNIT_TEST_APP_NAME "UefiSortLib Unit Tests"
25#define UNIT_TEST_APP_VERSION "1.0"
26
27#define TEST_ARRAY_SIZE_9 9
28
40INTN
41EFIAPI
43 IN CONST VOID *Left,
44 IN CONST VOID *Right
45 )
46{
47 if (*(UINT32 *)Right > *(UINT32 *)Left) {
48 return 1;
49 } else if (*(UINT32 *)Right < *(UINT32 *)Left) {
50 return -1;
51 }
52
53 return 0;
54}
55
72EFIAPI
74 IN UNIT_TEST_CONTEXT Context
75 )
76{
77 UINTN TestCount;
78 UINT32 Index;
79 UINT32 TestBuffer[TEST_ARRAY_SIZE_9];
80 UINT32 TestResult[TEST_ARRAY_SIZE_9];
81
82 TestCount = TEST_ARRAY_SIZE_9;
83 for (Index = 0; Index < TEST_ARRAY_SIZE_9; Index++) {
84 TestBuffer[Index] = Index + 1;
85 TestResult[Index] = TEST_ARRAY_SIZE_9 - Index;
86 }
87
88 PerformQuickSort (TestBuffer, TestCount, sizeof (UINT32), (SORT_COMPARE)TestCompareFunction);
89 UT_ASSERT_MEM_EQUAL (TestBuffer, TestResult, sizeof (UINT32) * TEST_ARRAY_SIZE_9);
90
91 return UNIT_TEST_PASSED;
92}
93
110EFIAPI
112 IN UNIT_TEST_CONTEXT Context
113 )
114{
115 INTN retval;
116 CONST CHAR16 *TestBuffer[] = { L"abcdefg" };
117
118 retval = StringCompare (TestBuffer, TestBuffer);
119 UT_ASSERT_TRUE (retval == 0);
120
121 return UNIT_TEST_PASSED;
122}
123
132STATIC
134EFIAPI
136 VOID
137 )
138{
139 EFI_STATUS Status;
140 UNIT_TEST_FRAMEWORK_HANDLE Framework;
141 UNIT_TEST_SUITE_HANDLE SortTests;
142
143 Framework = NULL;
144
145 DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
146
147 //
148 // Start setting up the test framework for running the tests.
149 //
150 Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
151 if (EFI_ERROR (Status)) {
152 DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
153 goto EXIT;
154 }
155
156 //
157 // Populate the UefiSortLib Unit Test Suite.
158 //
159 Status = CreateUnitTestSuite (&SortTests, Framework, "UefiSortLib Sort Tests", "UefiSortLib.SortLib", NULL, NULL);
160 if (EFI_ERROR (Status)) {
161 DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for UefiSortLib API Tests\n"));
162 Status = EFI_OUT_OF_RESOURCES;
163 goto EXIT;
164 }
165
166 //
167 // --------------Suite--------Description------------Name--------------Function----------------Pre---Post---Context-----------
168 //
169 AddTestCase (SortTests, "Sort the Array", "Sort", SortUINT32ArrayShouldSucceed, NULL, NULL, NULL);
170 AddTestCase (SortTests, "Compare the Buffer", "Compare", CompareSameBufferShouldSucceed, NULL, NULL, NULL);
171
172 //
173 // Execute the tests.
174 //
175 Status = RunAllTestSuites (Framework);
176
177EXIT:
178 if (Framework) {
179 FreeUnitTestFramework (Framework);
180 }
181
182 return Status;
183}
184
188#define UefiSortLibUnitTestMain main
189
199INT32
201 IN INT32 Argc,
202 IN CHAR8 *Argv[]
203 )
204{
206 return 0;
207}
UINT64 UINTN
INT64 INTN
#define NULL
Definition: Base.h:319
#define CONST
Definition: Base.h:259
#define STATIC
Definition: Base.h:264
#define IN
Definition: Base.h:279
#define DEBUG(Expression)
Definition: DebugLib.h:434
VOID EFIAPI PerformQuickSort(IN OUT VOID *BufferToSort, IN CONST UINTN Count, IN CONST UINTN ElementSize, IN SORT_COMPARE CompareFunction)
Definition: BaseSortLib.c:36
INTN EFIAPI StringCompare(IN CONST VOID *Buffer1, IN CONST VOID *Buffer2)
Definition: BaseSortLib.c:111
RETURN_STATUS EFI_STATUS
Definition: UefiBaseType.h:29
#define UefiSortLibUnitTestMain
INTN EFIAPI TestCompareFunction(IN CONST VOID *Left, IN CONST VOID *Right)
UNIT_TEST_STATUS EFIAPI CompareSameBufferShouldSucceed(IN UNIT_TEST_CONTEXT Context)
UNIT_TEST_STATUS EFIAPI SortUINT32ArrayShouldSucceed(IN UNIT_TEST_CONTEXT Context)
STATIC EFI_STATUS EFIAPI UnitTestingEntry(VOID)
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_MEM_EQUAL(BufferA, BufferB, Length)
Definition: UnitTestLib.h:389
#define UT_ASSERT_TRUE(Expression)
Definition: UnitTestLib.h:350
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