--- /dev/null
+/** @file\r
+ Unit tests of the UefiSortLib\r
+\r
+ Copyright (C) Huawei Technologies Co., Ltd. All rights reserved\r
+ SPDX-License-Identifier: BSD-2-Clause-Patent\r
+\r
+**/\r
+\r
+#include <stdio.h>\r
+#include <string.h>\r
+#include <stdarg.h>\r
+#include <stddef.h>\r
+#include <setjmp.h>\r
+#include <cmocka.h>\r
+\r
+#include <Uefi.h>\r
+#include <Library/BaseLib.h>\r
+#include <Library/DebugLib.h>\r
+#include <Library/MemoryAllocationLib.h>\r
+\r
+#include <Library/UnitTestLib.h>\r
+#include <Library/SortLib.h>\r
+\r
+#define UNIT_TEST_APP_NAME "UefiSortLib Unit Tests"\r
+#define UNIT_TEST_APP_VERSION "1.0"\r
+\r
+#define TEST_ARRAY_SIZE_9 9\r
+\r
+/**\r
+ The function is called by PerformQuickSort to compare int values.\r
+\r
+ @param[in] Left The pointer to first buffer.\r
+ @param[in] Right The pointer to second buffer.\r
+\r
+ @retval 0 Buffer1 equal to Buffer2.\r
+ @return <0 Buffer1 is less than Buffer2.\r
+ @return >0 Buffer1 is greater than Buffer2.\r
+\r
+**/\r
+INTN\r
+EFIAPI\r
+TestCompareFunction (\r
+ IN CONST VOID *Left,\r
+ IN CONST VOID *Right\r
+ )\r
+{\r
+ if (*(UINT32*)Right > *(UINT32*)Left) {\r
+ return 1;\r
+ } else if (*(UINT32*)Right < *(UINT32*)Left) {\r
+ return -1;\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+/**\r
+ Unit test for PerformQuickSort () API of the UefiSortLib.\r
+\r
+ @param[in] Context [Optional] An optional parameter that enables:\r
+ 1) test-case reuse with varied parameters and\r
+ 2) test-case re-entry for Target tests that need a\r
+ reboot. This parameter is a VOID* and it is the\r
+ responsibility of the test author to ensure that the\r
+ contents are well understood by all test cases that may\r
+ consume it.\r
+\r
+ @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
+ case was successful.\r
+ @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+SortUINT32ArrayShouldSucceed (\r
+ IN UNIT_TEST_CONTEXT Context\r
+ )\r
+{\r
+ UINTN TestCount;\r
+ UINT32 Index;\r
+ UINT32 TestBuffer[TEST_ARRAY_SIZE_9];\r
+ UINT32 TestResult[TEST_ARRAY_SIZE_9];\r
+\r
+ TestCount = TEST_ARRAY_SIZE_9;\r
+ for (Index = 0; Index < TEST_ARRAY_SIZE_9; Index++) {\r
+ TestBuffer[Index] = Index + 1;\r
+ TestResult[Index] = TEST_ARRAY_SIZE_9 - Index;\r
+ }\r
+\r
+ PerformQuickSort (TestBuffer, TestCount, sizeof (UINT32), (SORT_COMPARE)TestCompareFunction);\r
+ UT_ASSERT_MEM_EQUAL (TestBuffer, TestResult, sizeof (UINT32) * TEST_ARRAY_SIZE_9);\r
+\r
+ return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+ Unit test for StringCompare () API of the UefiSortLib.\r
+\r
+ @param[in] Context [Optional] An optional parameter that enables:\r
+ 1) test-case reuse with varied parameters and\r
+ 2) test-case re-entry for Target tests that need a\r
+ reboot. This parameter is a VOID* and it is the\r
+ responsibility of the test author to ensure that the\r
+ contents are well understood by all test cases that may\r
+ consume it.\r
+\r
+ @retval UNIT_TEST_PASSED The Unit test has completed and the test\r
+ case was successful.\r
+ @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r
+**/\r
+UNIT_TEST_STATUS\r
+EFIAPI\r
+CompareSameBufferShouldSucceed (\r
+ IN UNIT_TEST_CONTEXT Context\r
+ )\r
+{\r
+ INTN retval;\r
+ CONST CHAR16* TestBuffer[] = { L"abcdefg" };\r
+\r
+ retval = StringCompare (TestBuffer, TestBuffer);\r
+ UT_ASSERT_TRUE (retval == 0);\r
+\r
+ return UNIT_TEST_PASSED;\r
+}\r
+\r
+/**\r
+ Initialze the unit test framework, suite, and unit tests for the\r
+ UefiSortLib and run the UefiSortLib unit test.\r
+\r
+ @retval EFI_SUCCESS All test cases were dispatched.\r
+ @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r
+ initialize the unit tests.\r
+**/\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+UnitTestingEntry (\r
+ VOID\r
+ )\r
+{\r
+ EFI_STATUS Status;\r
+ UNIT_TEST_FRAMEWORK_HANDLE Framework;\r
+ UNIT_TEST_SUITE_HANDLE SortTests;\r
+\r
+ Framework = NULL;\r
+\r
+ DEBUG(( DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION ));\r
+\r
+ //\r
+ // Start setting up the test framework for running the tests.\r
+ //\r
+ Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r
+ goto EXIT;\r
+ }\r
+\r
+ //\r
+ // Populate the UefiSortLib Unit Test Suite.\r
+ //\r
+ Status = CreateUnitTestSuite (&SortTests, Framework, "UefiSortLib Sort Tests", "UefiSortLib.SortLib", NULL, NULL);\r
+ if (EFI_ERROR (Status)) {\r
+ DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for UefiSortLib API Tests\n"));\r
+ Status = EFI_OUT_OF_RESOURCES;\r
+ goto EXIT;\r
+ }\r
+\r
+ //\r
+ // --------------Suite--------Description------------Name--------------Function----------------Pre---Post---Context-----------\r
+ //\r
+ AddTestCase (SortTests, "Sort the Array", "Sort", SortUINT32ArrayShouldSucceed, NULL, NULL, NULL);\r
+ AddTestCase (SortTests, "Compare the Buffer", "Compare", CompareSameBufferShouldSucceed, NULL, NULL, NULL);\r
+\r
+ //\r
+ // Execute the tests.\r
+ //\r
+ Status = RunAllTestSuites (Framework);\r
+\r
+EXIT:\r
+ if (Framework) {\r
+ FreeUnitTestFramework (Framework);\r
+ }\r
+\r
+ return Status;\r
+}\r
+\r
+///\r
+/// Avoid ECC error for function name that starts with lower case letter\r
+///\r
+#define UefiSortLibUnitTestMain main\r
+\r
+/**\r
+ Standard POSIX C entry point for host based unit test execution.\r
+\r
+ @param[in] Argc Number of arguments\r
+ @param[in] Argv Array of pointers to arguments\r
+\r
+ @retval 0 Success\r
+ @retval other Error\r
+**/\r
+INT32\r
+UefiSortLibUnitTestMain (\r
+ IN INT32 Argc,\r
+ IN CHAR8 *Argv[]\r
+ )\r
+{\r
+ UnitTestingEntry ();\r
+ return 0;\r
+}\r