]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/UefiSortLib:Add UefiSortLib unit test
authorWenyi Xie <xiewenyi2@huawei.com>
Wed, 11 Aug 2021 06:35:39 +0000 (14:35 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Thu, 12 Aug 2021 08:34:04 +0000 (08:34 +0000)
Adding two unit test case for UefiSortLib. One is a test on
sorting an array of UINT32 by using PerformQuickSort, another
is a test on comparing the same buffer by using StringCompare.
Add 'main' function name to ECC exception list to avoid ECC
error.

Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Hao A Wu <hao.a.wu@intel.com>
Signed-off-by: Wenyi Xie <xiewenyi2@huawei.com>
Reviewed-by: Hao A Wu <hao.a.wu@intel.com>
MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c [new file with mode: 0644]
MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf [new file with mode: 0644]
MdeModulePkg/MdeModulePkg.ci.yaml
MdeModulePkg/Test/MdeModulePkgHostTest.dsc

diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c
new file mode 100644 (file)
index 0000000..4f44a02
--- /dev/null
@@ -0,0 +1,207 @@
+/** @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
diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf
new file mode 100644 (file)
index 0000000..85d8dcd
--- /dev/null
@@ -0,0 +1,32 @@
+## @file\r
+# This is a unit test for the UefiSortLib.\r
+#\r
+# Copyright (C) Huawei Technologies Co., Ltd. All rights reserved\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+##\r
+\r
+[Defines]\r
+  INF_VERSION         = 0x00010017\r
+  BASE_NAME           = UefiSortLibUnitTest\r
+  FILE_GUID           = 271337A3-0D79-BA3E-BC03-714E518E3B1B\r
+  VERSION_STRING      = 1.0\r
+  MODULE_TYPE         = HOST_APPLICATION\r
+\r
+#\r
+# The following information is for reference only and not required by the build tools.\r
+#\r
+#  VALID_ARCHITECTURES           = IA32 X64\r
+#\r
+\r
+[Sources]\r
+  UefiSortLibUnitTest.c\r
+\r
+[Packages]\r
+  MdePkg/MdePkg.dec\r
+  MdeModulePkg/MdeModulePkg.dec\r
+  UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec\r
+\r
+[LibraryClasses]\r
+  UnitTestLib\r
+  DebugLib\r
+  UefiSortLib\r
index 8e01caf94cbf3c4fcfe21b1c8ee0e69671832e39..aa304f2ccd5c040995eb7b5b9c4a0deb106a344d 100644 (file)
@@ -19,6 +19,7 @@
         "ExceptionList": [\r
             "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.UID",\r
             "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.HID",\r
+            "8001", "UefiSortLibUnitTestMain",\r
         ],\r
         ## Both file path and directory path are accepted.\r
         "IgnoreFiles": [\r
index 4da4692c84510ccfb5ff5109833cdebc3cef5331..c9ec835df65d27900d92fc3d3454977c7201a0a1 100644 (file)
@@ -41,3 +41,9 @@
     <PcdsFixedAtBuild>\r
       gEfiMdeModulePkgTokenSpaceGuid.PcdAllowVariablePolicyEnforcementDisable|TRUE\r
   }\r
+\r
+  MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf {\r
+    <LibraryClasses>\r
+      UefiSortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf\r
+      DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf\r
+  }\r