diff options
| author | kx <kx@radix.pro> | 2024-06-04 06:43:08 +0300 |
|---|---|---|
| committer | kx <kx@radix.pro> | 2024-06-04 06:43:08 +0300 |
| commit | 7e2ccccace24636f29ddc210b94606abd4c7e42b (patch) | |
| tree | 545d43ea12671048956cafeb12303e84d601e571 /MdeModulePkg/Library/UefiSortLib | |
| parent | 27b044605cd5f6b33a3d231576003850b3fe305b (diff) | |
| download | edk2-trunk.tar.xz | |
Renormalized end-of-lines from master@27b044605cd5f6b33a3d231576003850b3fe305btrunk
Diffstat (limited to 'MdeModulePkg/Library/UefiSortLib')
7 files changed, 613 insertions, 613 deletions
diff --git a/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.cpp b/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.cpp index 16986a512f..43d324a4ec 100644 --- a/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.cpp +++ b/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.cpp @@ -1,66 +1,66 @@ -/** @file
- Unit tests for the implementation of UefiSortLib.
-
- Copyright (c) 2022, Intel Corporation. All rights reserved.
- SPDX-License-Identifier: BSD-2-Clause-Patent
-**/
-#include <Library/GoogleTestLib.h>
-
-extern "C" {
- #include <Uefi.h>
- #include <Library/SortLib.h>
-}
-
-using namespace testing;
-
-INTN
-EFIAPI
-CompareUint32 (
- IN CONST VOID *Left,
- IN CONST VOID *Right
- )
-{
- if (*(UINT32 *)Right > *(UINT32 *)Left) {
- return 1;
- } else if (*(UINT32 *)Right < *(UINT32 *)Left) {
- return -1;
- }
-
- return 0;
-}
-
-// Test PerformQuickSort() API from UefiSortLib to verify a UINT32 array
-// with 9 elements in ascending order is sorted into descending order.
-TEST (PerformQuickSortTest, SortUint32AscendingArray_Size9) {
- CONST UINT32 ArraySize = 9;
- UINT32 BuffActual[ArraySize];
- UINT32 BuffExpected[ArraySize];
-
- for (UINT32 Index = 0; Index < ArraySize; Index++) {
- BuffActual[Index] = Index + 1;
- BuffExpected[Index] = ArraySize - Index;
- }
-
- PerformQuickSort (BuffActual, (UINTN)ArraySize, sizeof (UINT32), (SORT_COMPARE)CompareUint32);
- EXPECT_THAT (BuffActual, ElementsAreArray (BuffExpected, ArraySize));
-}
-
-// Test StringCompare() API from UefiSortLib to verify the comparison
-// succeeds when the same buffer is compared with itself.
-TEST (StringCompareTest, CompareSameBuffer) {
- INTN RetVal;
- CONST CHAR16 *Buffer = (CHAR16 *)L"abcdefg";
-
- RetVal = StringCompare (&Buffer, &Buffer);
- EXPECT_EQ (RetVal, 0);
-}
-
-int
-main (
- int argc,
- char *argv[]
- )
-{
- testing::InitGoogleTest (&argc, argv);
- return RUN_ALL_TESTS ();
-}
+/** @file + Unit tests for the implementation of UefiSortLib. + + Copyright (c) 2022, Intel Corporation. All rights reserved. + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ +#include <Library/GoogleTestLib.h> + +extern "C" { + #include <Uefi.h> + #include <Library/SortLib.h> +} + +using namespace testing; + +INTN +EFIAPI +CompareUint32 ( + IN CONST VOID *Left, + IN CONST VOID *Right + ) +{ + if (*(UINT32 *)Right > *(UINT32 *)Left) { + return 1; + } else if (*(UINT32 *)Right < *(UINT32 *)Left) { + return -1; + } + + return 0; +} + +// Test PerformQuickSort() API from UefiSortLib to verify a UINT32 array +// with 9 elements in ascending order is sorted into descending order. +TEST (PerformQuickSortTest, SortUint32AscendingArray_Size9) { + CONST UINT32 ArraySize = 9; + UINT32 BuffActual[ArraySize]; + UINT32 BuffExpected[ArraySize]; + + for (UINT32 Index = 0; Index < ArraySize; Index++) { + BuffActual[Index] = Index + 1; + BuffExpected[Index] = ArraySize - Index; + } + + PerformQuickSort (BuffActual, (UINTN)ArraySize, sizeof (UINT32), (SORT_COMPARE)CompareUint32); + EXPECT_THAT (BuffActual, ElementsAreArray (BuffExpected, ArraySize)); +} + +// Test StringCompare() API from UefiSortLib to verify the comparison +// succeeds when the same buffer is compared with itself. +TEST (StringCompareTest, CompareSameBuffer) { + INTN RetVal; + CONST CHAR16 *Buffer = (CHAR16 *)L"abcdefg"; + + RetVal = StringCompare (&Buffer, &Buffer); + EXPECT_EQ (RetVal, 0); +} + +int +main ( + int argc, + char *argv[] + ) +{ + testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.inf b/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.inf index ac5ffb3bc2..8852adfa54 100644 --- a/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.inf +++ b/MdeModulePkg/Library/UefiSortLib/GoogleTest/UefiSortLibGoogleTest.inf @@ -1,31 +1,31 @@ -## @file
-# Unit test suite for the UefiSortLib using Google Test
-#
-# Copyright (c) 2022, Intel Corporation. All rights reserved.
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010017
- BASE_NAME = UefiSortLibGoogleTest
- FILE_GUID = 78FB0BEE-D0EA-4E1A-BD38-67458C8ECDEF
- VERSION_STRING = 1.0
- MODULE_TYPE = HOST_APPLICATION
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- UefiSortLibGoogleTest.cpp
-
-[Packages]
- MdePkg/MdePkg.dec
- MdeModulePkg/MdeModulePkg.dec
- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
-
-[LibraryClasses]
- GoogleTestLib
- UefiSortLib
+## @file +# Unit test suite for the UefiSortLib using Google Test +# +# Copyright (c) 2022, Intel Corporation. All rights reserved. +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010017 + BASE_NAME = UefiSortLibGoogleTest + FILE_GUID = 78FB0BEE-D0EA-4E1A-BD38-67458C8ECDEF + VERSION_STRING = 1.0 + MODULE_TYPE = HOST_APPLICATION + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + UefiSortLibGoogleTest.cpp + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + GoogleTestLib + UefiSortLib diff --git a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.c b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.c index 0ba1244930..677744a6f1 100644 --- a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.c +++ b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.c @@ -1,215 +1,215 @@ -/** @file
- Library used for sorting routines.
-
- Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved. <BR>
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#include <Uefi.h>
-
-#include <Protocol/UnicodeCollation.h>
-#include <Protocol/DevicePath.h>
-
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/BaseLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/DebugLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/SortLib.h>
-#include <Library/DevicePathLib.h>
-
-STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL;
-
-#define USL_FREE_NON_NULL(Pointer) \
-{ \
- if ((Pointer) != NULL) { \
- FreePool((Pointer)); \
- (Pointer) = NULL; \
- } \
-}
-
-/**
- Function to perform a Quick Sort alogrithm on a buffer of comparable elements.
-
- Each element must be equal sized.
-
- if BufferToSort is NULL, then ASSERT.
- if CompareFunction is NULL, then ASSERT.
-
- if Count is < 2 then perform no action.
- if Size is < 1 then perform no action.
-
- @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements
- on return a buffer of sorted elements
- @param[in] Count the number of elements in the buffer to sort
- @param[in] ElementSize Size of an element in bytes
- @param[in] CompareFunction The function to call to perform the comparison
- of any 2 elements
-**/
-VOID
-EFIAPI
-PerformQuickSort (
- IN OUT VOID *BufferToSort,
- IN CONST UINTN Count,
- IN CONST UINTN ElementSize,
- IN SORT_COMPARE CompareFunction
- )
-{
- VOID *Buffer;
-
- ASSERT (BufferToSort != NULL);
- ASSERT (CompareFunction != NULL);
-
- Buffer = AllocateZeroPool (ElementSize);
- ASSERT (Buffer != NULL);
-
- QuickSort (
- BufferToSort,
- Count,
- ElementSize,
- CompareFunction,
- Buffer
- );
-
- FreePool (Buffer);
- return;
-}
-
-/**
- Function to compare 2 device paths for use in QuickSort.
-
- @param[in] Buffer1 pointer to Device Path poiner to compare
- @param[in] Buffer2 pointer to second DevicePath pointer to compare
-
- @retval 0 Buffer1 equal to Buffer2
- @retval <0 Buffer1 is less than Buffer2
- @retval >0 Buffer1 is greater than Buffer2
-**/
-INTN
-EFIAPI
-DevicePathCompare (
- IN CONST VOID *Buffer1,
- IN CONST VOID *Buffer2
- )
-{
- EFI_DEVICE_PATH_PROTOCOL *DevicePath1;
- EFI_DEVICE_PATH_PROTOCOL *DevicePath2;
- CHAR16 *TextPath1;
- CHAR16 *TextPath2;
- EFI_STATUS Status;
- INTN RetVal;
-
- DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer1;
- DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer2;
-
- if (DevicePath1 == NULL) {
- if (DevicePath2 == NULL) {
- return 0;
- }
-
- return -1;
- }
-
- if (DevicePath2 == NULL) {
- return 1;
- }
-
- if (mUnicodeCollation == NULL) {
- Status = gBS->LocateProtocol (
- &gEfiUnicodeCollation2ProtocolGuid,
- NULL,
- (VOID **)&mUnicodeCollation
- );
-
- ASSERT_EFI_ERROR (Status);
- }
-
- TextPath1 = ConvertDevicePathToText (
- DevicePath1,
- FALSE,
- FALSE
- );
-
- TextPath2 = ConvertDevicePathToText (
- DevicePath2,
- FALSE,
- FALSE
- );
-
- if (TextPath1 == NULL) {
- RetVal = -1;
- } else if (TextPath2 == NULL) {
- RetVal = 1;
- } else {
- RetVal = mUnicodeCollation->StriColl (
- mUnicodeCollation,
- TextPath1,
- TextPath2
- );
- }
-
- USL_FREE_NON_NULL (TextPath1);
- USL_FREE_NON_NULL (TextPath2);
-
- return (RetVal);
-}
-
-/**
- Function to compare 2 strings without regard to case of the characters.
-
- @param[in] Buffer1 Pointer to String to compare.
- @param[in] Buffer2 Pointer to second String to compare.
-
- @retval 0 Buffer1 equal to Buffer2.
- @retval <0 Buffer1 is less than Buffer2.
- @retval >0 Buffer1 is greater than Buffer2.
-**/
-INTN
-EFIAPI
-StringNoCaseCompare (
- IN CONST VOID *Buffer1,
- IN CONST VOID *Buffer2
- )
-{
- EFI_STATUS Status;
-
- if (mUnicodeCollation == NULL) {
- Status = gBS->LocateProtocol (
- &gEfiUnicodeCollation2ProtocolGuid,
- NULL,
- (VOID **)&mUnicodeCollation
- );
-
- ASSERT_EFI_ERROR (Status);
- }
-
- return (mUnicodeCollation->StriColl (
- mUnicodeCollation,
- *(CHAR16 **)Buffer1,
- *(CHAR16 **)Buffer2
- ));
-}
-
-/**
- Function to compare 2 strings.
-
- @param[in] Buffer1 Pointer to String to compare (CHAR16**).
- @param[in] Buffer2 Pointer to second String to compare (CHAR16**).
-
- @retval 0 Buffer1 equal to Buffer2.
- @retval <0 Buffer1 is less than Buffer2.
- @retval >0 Buffer1 is greater than Buffer2.
-**/
-INTN
-EFIAPI
-StringCompare (
- IN CONST VOID *Buffer1,
- IN CONST VOID *Buffer2
- )
-{
- return (StrCmp (
- *(CHAR16 **)Buffer1,
- *(CHAR16 **)Buffer2
- ));
-}
+/** @file + Library used for sorting routines. + + Copyright (c) 2009 - 2021, Intel Corporation. All rights reserved. <BR> + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <Uefi.h> + +#include <Protocol/UnicodeCollation.h> +#include <Protocol/DevicePath.h> + +#include <Library/UefiBootServicesTableLib.h> +#include <Library/BaseLib.h> +#include <Library/BaseMemoryLib.h> +#include <Library/DebugLib.h> +#include <Library/MemoryAllocationLib.h> +#include <Library/SortLib.h> +#include <Library/DevicePathLib.h> + +STATIC EFI_UNICODE_COLLATION_PROTOCOL *mUnicodeCollation = NULL; + +#define USL_FREE_NON_NULL(Pointer) \ +{ \ + if ((Pointer) != NULL) { \ + FreePool((Pointer)); \ + (Pointer) = NULL; \ + } \ +} + +/** + Function to perform a Quick Sort alogrithm on a buffer of comparable elements. + + Each element must be equal sized. + + if BufferToSort is NULL, then ASSERT. + if CompareFunction is NULL, then ASSERT. + + if Count is < 2 then perform no action. + if Size is < 1 then perform no action. + + @param[in, out] BufferToSort on call a Buffer of (possibly sorted) elements + on return a buffer of sorted elements + @param[in] Count the number of elements in the buffer to sort + @param[in] ElementSize Size of an element in bytes + @param[in] CompareFunction The function to call to perform the comparison + of any 2 elements +**/ +VOID +EFIAPI +PerformQuickSort ( + IN OUT VOID *BufferToSort, + IN CONST UINTN Count, + IN CONST UINTN ElementSize, + IN SORT_COMPARE CompareFunction + ) +{ + VOID *Buffer; + + ASSERT (BufferToSort != NULL); + ASSERT (CompareFunction != NULL); + + Buffer = AllocateZeroPool (ElementSize); + ASSERT (Buffer != NULL); + + QuickSort ( + BufferToSort, + Count, + ElementSize, + CompareFunction, + Buffer + ); + + FreePool (Buffer); + return; +} + +/** + Function to compare 2 device paths for use in QuickSort. + + @param[in] Buffer1 pointer to Device Path poiner to compare + @param[in] Buffer2 pointer to second DevicePath pointer to compare + + @retval 0 Buffer1 equal to Buffer2 + @retval <0 Buffer1 is less than Buffer2 + @retval >0 Buffer1 is greater than Buffer2 +**/ +INTN +EFIAPI +DevicePathCompare ( + IN CONST VOID *Buffer1, + IN CONST VOID *Buffer2 + ) +{ + EFI_DEVICE_PATH_PROTOCOL *DevicePath1; + EFI_DEVICE_PATH_PROTOCOL *DevicePath2; + CHAR16 *TextPath1; + CHAR16 *TextPath2; + EFI_STATUS Status; + INTN RetVal; + + DevicePath1 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer1; + DevicePath2 = *(EFI_DEVICE_PATH_PROTOCOL **)Buffer2; + + if (DevicePath1 == NULL) { + if (DevicePath2 == NULL) { + return 0; + } + + return -1; + } + + if (DevicePath2 == NULL) { + return 1; + } + + if (mUnicodeCollation == NULL) { + Status = gBS->LocateProtocol ( + &gEfiUnicodeCollation2ProtocolGuid, + NULL, + (VOID **)&mUnicodeCollation + ); + + ASSERT_EFI_ERROR (Status); + } + + TextPath1 = ConvertDevicePathToText ( + DevicePath1, + FALSE, + FALSE + ); + + TextPath2 = ConvertDevicePathToText ( + DevicePath2, + FALSE, + FALSE + ); + + if (TextPath1 == NULL) { + RetVal = -1; + } else if (TextPath2 == NULL) { + RetVal = 1; + } else { + RetVal = mUnicodeCollation->StriColl ( + mUnicodeCollation, + TextPath1, + TextPath2 + ); + } + + USL_FREE_NON_NULL (TextPath1); + USL_FREE_NON_NULL (TextPath2); + + return (RetVal); +} + +/** + Function to compare 2 strings without regard to case of the characters. + + @param[in] Buffer1 Pointer to String to compare. + @param[in] Buffer2 Pointer to second String to compare. + + @retval 0 Buffer1 equal to Buffer2. + @retval <0 Buffer1 is less than Buffer2. + @retval >0 Buffer1 is greater than Buffer2. +**/ +INTN +EFIAPI +StringNoCaseCompare ( + IN CONST VOID *Buffer1, + IN CONST VOID *Buffer2 + ) +{ + EFI_STATUS Status; + + if (mUnicodeCollation == NULL) { + Status = gBS->LocateProtocol ( + &gEfiUnicodeCollation2ProtocolGuid, + NULL, + (VOID **)&mUnicodeCollation + ); + + ASSERT_EFI_ERROR (Status); + } + + return (mUnicodeCollation->StriColl ( + mUnicodeCollation, + *(CHAR16 **)Buffer1, + *(CHAR16 **)Buffer2 + )); +} + +/** + Function to compare 2 strings. + + @param[in] Buffer1 Pointer to String to compare (CHAR16**). + @param[in] Buffer2 Pointer to second String to compare (CHAR16**). + + @retval 0 Buffer1 equal to Buffer2. + @retval <0 Buffer1 is less than Buffer2. + @retval >0 Buffer1 is greater than Buffer2. +**/ +INTN +EFIAPI +StringCompare ( + IN CONST VOID *Buffer1, + IN CONST VOID *Buffer2 + ) +{ + return (StrCmp ( + *(CHAR16 **)Buffer1, + *(CHAR16 **)Buffer2 + )); +} diff --git a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf index 2ac6bfacfa..d8318a1a22 100644 --- a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf +++ b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf @@ -1,42 +1,42 @@ -## @file
-# Library used for sorting routines.
-#
-# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR>
-#
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-#
-#
-##
-
-[Defines]
- INF_VERSION = 0x00010006
- BASE_NAME = UefiSortLib
- MODULE_UNI_FILE = UefiSortLib.uni
- FILE_GUID = 4264A823-45A3-42db-B92C-AA078555CBD3
- MODULE_TYPE = UEFI_DRIVER
- VERSION_STRING = 1.0
- LIBRARY_CLASS = SortLib|UEFI_APPLICATION UEFI_DRIVER UEFI_DRIVER DXE_RUNTIME_DRIVER DXE_DRIVER
-
-#
-# VALID_ARCHITECTURES = IA32 X64 EBC
-#
-
-[Sources.common]
- UefiSortLib.c
-
-[Packages]
- MdePkg/MdePkg.dec
- MdeModulePkg/MdeModulePkg.dec
-
-[LibraryClasses]
- MemoryAllocationLib
- BaseLib
- BaseMemoryLib
- DebugLib
- UefiBootServicesTableLib
- DevicePathLib
-
-[Protocols]
- gEfiUnicodeCollation2ProtocolGuid ## SOMETIMES_CONSUMES
- gEfiDevicePathProtocolGuid ## CONSUMES
-
+## @file +# Library used for sorting routines. +# +# Copyright (c) 2009 - 2018, Intel Corporation. All rights reserved.<BR> +# +# SPDX-License-Identifier: BSD-2-Clause-Patent +# +# +## + +[Defines] + INF_VERSION = 0x00010006 + BASE_NAME = UefiSortLib + MODULE_UNI_FILE = UefiSortLib.uni + FILE_GUID = 4264A823-45A3-42db-B92C-AA078555CBD3 + MODULE_TYPE = UEFI_DRIVER + VERSION_STRING = 1.0 + LIBRARY_CLASS = SortLib|UEFI_APPLICATION UEFI_DRIVER UEFI_DRIVER DXE_RUNTIME_DRIVER DXE_DRIVER + +# +# VALID_ARCHITECTURES = IA32 X64 EBC +# + +[Sources.common] + UefiSortLib.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + +[LibraryClasses] + MemoryAllocationLib + BaseLib + BaseMemoryLib + DebugLib + UefiBootServicesTableLib + DevicePathLib + +[Protocols] + gEfiUnicodeCollation2ProtocolGuid ## SOMETIMES_CONSUMES + gEfiDevicePathProtocolGuid ## CONSUMES + diff --git a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.uni b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.uni index 42bc43a1ce..14f80b5960 100644 --- a/MdeModulePkg/Library/UefiSortLib/UefiSortLib.uni +++ b/MdeModulePkg/Library/UefiSortLib/UefiSortLib.uni @@ -1,20 +1,20 @@ -// /** @file
-// Library used for sorting routines.
-//
-// Library used for sorting routines.
-//
-// Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
-//
-// SPDX-License-Identifier: BSD-2-Clause-Patent
-//
-// **/
-
-#string STR_MODULE_ABSTRACT
-#language en-US
-"Library used for sorting routines."
-
-#string STR_MODULE_DESCRIPTION
-#language en-US
-"Library used for sorting routines."
-
-
+// /** @file +// Library used for sorting routines. +// +// Library used for sorting routines. +// +// Copyright (c) 2015, Intel Corporation. All rights reserved.<BR> +// +// SPDX-License-Identifier: BSD-2-Clause-Patent +// +// **/ + +#string STR_MODULE_ABSTRACT +#language en-US +"Library used for sorting routines." + +#string STR_MODULE_DESCRIPTION +#language en-US +"Library used for sorting routines." + + diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c index 3d15167f9a..58404f4eb2 100644 --- a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c +++ b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c @@ -1,207 +1,207 @@ -/** @file
- Unit tests of the UefiSortLib
-
- Copyright (C) Huawei Technologies Co., Ltd. All rights reserved
- SPDX-License-Identifier: BSD-2-Clause-Patent
-
-**/
-
-#include <stdio.h>
-#include <string.h>
-#include <stdarg.h>
-#include <stddef.h>
-#include <setjmp.h>
-#include <cmocka.h>
-
-#include <Uefi.h>
-#include <Library/BaseLib.h>
-#include <Library/DebugLib.h>
-#include <Library/MemoryAllocationLib.h>
-
-#include <Library/UnitTestLib.h>
-#include <Library/SortLib.h>
-
-#define UNIT_TEST_APP_NAME "UefiSortLib Unit Tests"
-#define UNIT_TEST_APP_VERSION "1.0"
-
-#define TEST_ARRAY_SIZE_9 9
-
-/**
- The function is called by PerformQuickSort to compare int values.
-
- @param[in] Left The pointer to first buffer.
- @param[in] Right The pointer to second buffer.
-
- @retval 0 Buffer1 equal to Buffer2.
- @return <0 Buffer1 is less than Buffer2.
- @return >0 Buffer1 is greater than Buffer2.
-
-**/
-INTN
-EFIAPI
-TestCompareFunction (
- IN CONST VOID *Left,
- IN CONST VOID *Right
- )
-{
- if (*(UINT32 *)Right > *(UINT32 *)Left) {
- return 1;
- } else if (*(UINT32 *)Right < *(UINT32 *)Left) {
- return -1;
- }
-
- return 0;
-}
-
-/**
- Unit test for PerformQuickSort () API of the UefiSortLib.
-
- @param[in] Context [Optional] An optional parameter that enables:
- 1) test-case reuse with varied parameters and
- 2) test-case re-entry for Target tests that need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that the
- contents are well understood by all test cases that may
- consume it.
-
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
-**/
-UNIT_TEST_STATUS
-EFIAPI
-SortUINT32ArrayShouldSucceed (
- IN UNIT_TEST_CONTEXT Context
- )
-{
- UINTN TestCount;
- UINT32 Index;
- UINT32 TestBuffer[TEST_ARRAY_SIZE_9];
- UINT32 TestResult[TEST_ARRAY_SIZE_9];
-
- TestCount = TEST_ARRAY_SIZE_9;
- for (Index = 0; Index < TEST_ARRAY_SIZE_9; Index++) {
- TestBuffer[Index] = Index + 1;
- TestResult[Index] = TEST_ARRAY_SIZE_9 - Index;
- }
-
- PerformQuickSort (TestBuffer, TestCount, sizeof (UINT32), (SORT_COMPARE)TestCompareFunction);
- UT_ASSERT_MEM_EQUAL (TestBuffer, TestResult, sizeof (UINT32) * TEST_ARRAY_SIZE_9);
-
- return UNIT_TEST_PASSED;
-}
-
-/**
- Unit test for StringCompare () API of the UefiSortLib.
-
- @param[in] Context [Optional] An optional parameter that enables:
- 1) test-case reuse with varied parameters and
- 2) test-case re-entry for Target tests that need a
- reboot. This parameter is a VOID* and it is the
- responsibility of the test author to ensure that the
- contents are well understood by all test cases that may
- consume it.
-
- @retval UNIT_TEST_PASSED The Unit test has completed and the test
- case was successful.
- @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.
-**/
-UNIT_TEST_STATUS
-EFIAPI
-CompareSameBufferShouldSucceed (
- IN UNIT_TEST_CONTEXT Context
- )
-{
- INTN retval;
- CONST CHAR16 *TestBuffer[] = { L"abcdefg" };
-
- retval = StringCompare (TestBuffer, TestBuffer);
- UT_ASSERT_TRUE (retval == 0);
-
- return UNIT_TEST_PASSED;
-}
-
-/**
- Initialze the unit test framework, suite, and unit tests for the
- UefiSortLib and run the UefiSortLib unit test.
-
- @retval EFI_SUCCESS All test cases were dispatched.
- @retval EFI_OUT_OF_RESOURCES There are not enough resources available to
- initialize the unit tests.
-**/
-STATIC
-EFI_STATUS
-EFIAPI
-UnitTestingEntry (
- VOID
- )
-{
- EFI_STATUS Status;
- UNIT_TEST_FRAMEWORK_HANDLE Framework;
- UNIT_TEST_SUITE_HANDLE SortTests;
-
- Framework = NULL;
-
- DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));
-
- //
- // Start setting up the test framework for running the tests.
- //
- Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
- goto EXIT;
- }
-
- //
- // Populate the UefiSortLib Unit Test Suite.
- //
- Status = CreateUnitTestSuite (&SortTests, Framework, "UefiSortLib Sort Tests", "UefiSortLib.SortLib", NULL, NULL);
- if (EFI_ERROR (Status)) {
- DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for UefiSortLib API Tests\n"));
- Status = EFI_OUT_OF_RESOURCES;
- goto EXIT;
- }
-
- //
- // --------------Suite--------Description------------Name--------------Function----------------Pre---Post---Context-----------
- //
- AddTestCase (SortTests, "Sort the Array", "Sort", SortUINT32ArrayShouldSucceed, NULL, NULL, NULL);
- AddTestCase (SortTests, "Compare the Buffer", "Compare", CompareSameBufferShouldSucceed, NULL, NULL, NULL);
-
- //
- // Execute the tests.
- //
- Status = RunAllTestSuites (Framework);
-
-EXIT:
- if (Framework) {
- FreeUnitTestFramework (Framework);
- }
-
- return Status;
-}
-
-///
-/// Avoid ECC error for function name that starts with lower case letter
-///
-#define UefiSortLibUnitTestMain main
-
-/**
- Standard POSIX C entry point for host based unit test execution.
-
- @param[in] Argc Number of arguments
- @param[in] Argv Array of pointers to arguments
-
- @retval 0 Success
- @retval other Error
-**/
-INT32
-UefiSortLibUnitTestMain (
- IN INT32 Argc,
- IN CHAR8 *Argv[]
- )
-{
- UnitTestingEntry ();
- return 0;
-}
+/** @file + Unit tests of the UefiSortLib + + Copyright (C) Huawei Technologies Co., Ltd. All rights reserved + SPDX-License-Identifier: BSD-2-Clause-Patent + +**/ + +#include <stdio.h> +#include <string.h> +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#include <Uefi.h> +#include <Library/BaseLib.h> +#include <Library/DebugLib.h> +#include <Library/MemoryAllocationLib.h> + +#include <Library/UnitTestLib.h> +#include <Library/SortLib.h> + +#define UNIT_TEST_APP_NAME "UefiSortLib Unit Tests" +#define UNIT_TEST_APP_VERSION "1.0" + +#define TEST_ARRAY_SIZE_9 9 + +/** + The function is called by PerformQuickSort to compare int values. + + @param[in] Left The pointer to first buffer. + @param[in] Right The pointer to second buffer. + + @retval 0 Buffer1 equal to Buffer2. + @return <0 Buffer1 is less than Buffer2. + @return >0 Buffer1 is greater than Buffer2. + +**/ +INTN +EFIAPI +TestCompareFunction ( + IN CONST VOID *Left, + IN CONST VOID *Right + ) +{ + if (*(UINT32 *)Right > *(UINT32 *)Left) { + return 1; + } else if (*(UINT32 *)Right < *(UINT32 *)Left) { + return -1; + } + + return 0; +} + +/** + Unit test for PerformQuickSort () API of the UefiSortLib. + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +SortUINT32ArrayShouldSucceed ( + IN UNIT_TEST_CONTEXT Context + ) +{ + UINTN TestCount; + UINT32 Index; + UINT32 TestBuffer[TEST_ARRAY_SIZE_9]; + UINT32 TestResult[TEST_ARRAY_SIZE_9]; + + TestCount = TEST_ARRAY_SIZE_9; + for (Index = 0; Index < TEST_ARRAY_SIZE_9; Index++) { + TestBuffer[Index] = Index + 1; + TestResult[Index] = TEST_ARRAY_SIZE_9 - Index; + } + + PerformQuickSort (TestBuffer, TestCount, sizeof (UINT32), (SORT_COMPARE)TestCompareFunction); + UT_ASSERT_MEM_EQUAL (TestBuffer, TestResult, sizeof (UINT32) * TEST_ARRAY_SIZE_9); + + return UNIT_TEST_PASSED; +} + +/** + Unit test for StringCompare () API of the UefiSortLib. + + @param[in] Context [Optional] An optional parameter that enables: + 1) test-case reuse with varied parameters and + 2) test-case re-entry for Target tests that need a + reboot. This parameter is a VOID* and it is the + responsibility of the test author to ensure that the + contents are well understood by all test cases that may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and the test + case was successful. + @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed. +**/ +UNIT_TEST_STATUS +EFIAPI +CompareSameBufferShouldSucceed ( + IN UNIT_TEST_CONTEXT Context + ) +{ + INTN retval; + CONST CHAR16 *TestBuffer[] = { L"abcdefg" }; + + retval = StringCompare (TestBuffer, TestBuffer); + UT_ASSERT_TRUE (retval == 0); + + return UNIT_TEST_PASSED; +} + +/** + Initialze the unit test framework, suite, and unit tests for the + UefiSortLib and run the UefiSortLib unit test. + + @retval EFI_SUCCESS All test cases were dispatched. + @retval EFI_OUT_OF_RESOURCES There are not enough resources available to + initialize the unit tests. +**/ +STATIC +EFI_STATUS +EFIAPI +UnitTestingEntry ( + VOID + ) +{ + EFI_STATUS Status; + UNIT_TEST_FRAMEWORK_HANDLE Framework; + UNIT_TEST_SUITE_HANDLE SortTests; + + Framework = NULL; + + DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION)); + + // + // Start setting up the test framework for running the tests. + // + Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status)); + goto EXIT; + } + + // + // Populate the UefiSortLib Unit Test Suite. + // + Status = CreateUnitTestSuite (&SortTests, Framework, "UefiSortLib Sort Tests", "UefiSortLib.SortLib", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for UefiSortLib API Tests\n")); + Status = EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // + // --------------Suite--------Description------------Name--------------Function----------------Pre---Post---Context----------- + // + AddTestCase (SortTests, "Sort the Array", "Sort", SortUINT32ArrayShouldSucceed, NULL, NULL, NULL); + AddTestCase (SortTests, "Compare the Buffer", "Compare", CompareSameBufferShouldSucceed, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status = RunAllTestSuites (Framework); + +EXIT: + if (Framework) { + FreeUnitTestFramework (Framework); + } + + return Status; +} + +/// +/// Avoid ECC error for function name that starts with lower case letter +/// +#define UefiSortLibUnitTestMain main + +/** + Standard POSIX C entry point for host based unit test execution. + + @param[in] Argc Number of arguments + @param[in] Argv Array of pointers to arguments + + @retval 0 Success + @retval other Error +**/ +INT32 +UefiSortLibUnitTestMain ( + IN INT32 Argc, + IN CHAR8 *Argv[] + ) +{ + UnitTestingEntry (); + return 0; +} diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf index 85d8dcd696..22f4afb776 100644 --- a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf +++ b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf @@ -1,32 +1,32 @@ -## @file
-# This is a unit test for the UefiSortLib.
-#
-# Copyright (C) Huawei Technologies Co., Ltd. All rights reserved
-# SPDX-License-Identifier: BSD-2-Clause-Patent
-##
-
-[Defines]
- INF_VERSION = 0x00010017
- BASE_NAME = UefiSortLibUnitTest
- FILE_GUID = 271337A3-0D79-BA3E-BC03-714E518E3B1B
- VERSION_STRING = 1.0
- MODULE_TYPE = HOST_APPLICATION
-
-#
-# The following information is for reference only and not required by the build tools.
-#
-# VALID_ARCHITECTURES = IA32 X64
-#
-
-[Sources]
- UefiSortLibUnitTest.c
-
-[Packages]
- MdePkg/MdePkg.dec
- MdeModulePkg/MdeModulePkg.dec
- UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec
-
-[LibraryClasses]
- UnitTestLib
- DebugLib
- UefiSortLib
+## @file +# This is a unit test for the UefiSortLib. +# +# Copyright (C) Huawei Technologies Co., Ltd. All rights reserved +# SPDX-License-Identifier: BSD-2-Clause-Patent +## + +[Defines] + INF_VERSION = 0x00010017 + BASE_NAME = UefiSortLibUnitTest + FILE_GUID = 271337A3-0D79-BA3E-BC03-714E518E3B1B + VERSION_STRING = 1.0 + MODULE_TYPE = HOST_APPLICATION + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + UefiSortLibUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + UnitTestLib + DebugLib + UefiSortLib |
