From nobody Thu May 2 13:04:17 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+79089+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+79089+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1628664039; cv=none; d=zohomail.com; s=zohoarc; b=JN8ufQmdU4l4L+uCV5VnsyWsbtnl4hOMSUB2/SR0AQvpAHKem7RaGtyRCac3StQmlJNypKnZTLKOV+kjiz1jnI5q9SzyO4BQ2kKbU6H1ti4urY4JbHBeWcSWs+bMoYAUlmRsrTJ0ZjZM/10zRtXMgzAEWA5rQNbCdX1ZN27rGwg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628664039; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=QBYxgLxtF81b7GyG+Oo+pBv/gzakh10WM9Y4kePYhz0=; b=lc6p8HLrQw3JKUlRiwYcXXei9wmA91OKaN9TH0bTJOn9mkebrrd+ar/rQSlwvIHeDE9tknJbX6oMIHKy9LXmjAt1mppIun3HiLOICm2g770jBvQbSoKqnCnbtekNn+vqnV5wFFztTw3aANLNQ7aNwASb+25t4he1V6fIlJLsE7w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+79089+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1628664039598132.83025600377619; Tue, 10 Aug 2021 23:40:39 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id dlgDYY1788612x0x3VNTTe6e; Tue, 10 Aug 2021 23:40:39 -0700 X-Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by mx.groups.io with SMTP id smtpd.web12.4348.1628664037821878905 for ; Tue, 10 Aug 2021 23:40:38 -0700 X-Received: from dggemv704-chm.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4Gl0Ys0wb1z1CVgb for ; Wed, 11 Aug 2021 14:40:17 +0800 (CST) X-Received: from dggpemm000003.china.huawei.com (7.185.36.128) by dggemv704-chm.china.huawei.com (10.3.19.47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 11 Aug 2021 14:40:31 +0800 X-Received: from HGH1000039998.huawei.com (10.184.68.188) by dggpemm000003.china.huawei.com (7.185.36.128) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Wed, 11 Aug 2021 14:40:31 +0800 From: "wenyi,xie via groups.io" To: , , CC: , Subject: [edk2-devel] [PATCH EDK2 v3 1/1] MdeModulePkg/UefiSortLib:Add UefiSortLib unit test Date: Wed, 11 Aug 2021 14:35:39 +0800 Message-ID: <1628663739-122023-2-git-send-email-xiewenyi2@huawei.com> In-Reply-To: <1628663739-122023-1-git-send-email-xiewenyi2@huawei.com> References: <1628663739-122023-1-git-send-email-xiewenyi2@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.184.68.188] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm000003.china.huawei.com (7.185.36.128) X-CFilter-Loop: Reflected Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,xiewenyi2@huawei.com X-Gm-Message-State: wg5ZqwEqrYuVEeF1p5F94T5yx1787277AA= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1628664039; bh=S+rC0wyzqFQBfmvaS3svCUtCv4ycHE5vBsdVM18VwnQ=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=bnWhAa1vmEN8koSStu7oEVpzRs2xVSYdnU+Gfe0BIpnmcQIPmtircXh3zSgXyG92RpE GIxvefSNSIKcjH/1I/UDJrG5y2YsEipBh1tQvfKmWkSgGvbBqrXroUOA/N+TDnCDwyJ3w a8kYkYbaThSw0FrIKLtG7qw8ZMK8cdWEzy0= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1628664041433100001 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 Cc: Hao A Wu Signed-off-by: Wenyi Xie Reviewed-by: Hao A Wu --- MdeModulePkg/Test/MdeModulePkgHostTest.dsc | 6 + MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf | 32 +++ MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c | 207 ++= ++++++++++++++++++ MdeModulePkg/MdeModulePkg.ci.yaml | 1 + 4 files changed, 246 insertions(+) diff --git a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc b/MdeModulePkg/Test= /MdeModulePkgHostTest.dsc index 4da4692c8451..c9ec835df65d 100644 --- a/MdeModulePkg/Test/MdeModulePkgHostTest.dsc +++ b/MdeModulePkg/Test/MdeModulePkgHostTest.dsc @@ -41,3 +41,9 @@ [Components] gEfiMdeModulePkgTokenSpaceGuid.PcdAllowVariablePolicyEnforcementDisa= ble|TRUE } + + MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf { + + UefiSortLib|MdeModulePkg/Library/UefiSortLib/UefiSortLib.inf + DevicePathLib|MdePkg/Library/UefiDevicePathLib/UefiDevicePathLib.inf + } diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.= inf b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf new file mode 100644 index 000000000000..85d8dcd69619 --- /dev/null +++ b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.inf @@ -0,0 +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 =3D 0x00010017 + BASE_NAME =3D UefiSortLibUnitTest + FILE_GUID =3D 271337A3-0D79-BA3E-BC03-714E518E3B1B + VERSION_STRING =3D 1.0 + MODULE_TYPE =3D HOST_APPLICATION + +# +# The following information is for reference only and not required by the = build tools. +# +# VALID_ARCHITECTURES =3D IA32 X64 +# + +[Sources] + UefiSortLibUnitTest.c + +[Packages] + MdePkg/MdePkg.dec + MdeModulePkg/MdeModulePkg.dec + UnitTestFrameworkPkg/UnitTestFrameworkPkg.dec + +[LibraryClasses] + UnitTestLib + DebugLib + UefiSortLib diff --git a/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.= c b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c new file mode 100644 index 000000000000..4f44a02c5b5d --- /dev/null +++ b/MdeModulePkg/Library/UefiSortLib/UnitTest/UefiSortLibUnitTest.c @@ -0,0 +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 +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include + +#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 th= at may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e 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 =3D TEST_ARRAY_SIZE_9; + for (Index =3D 0; Index < TEST_ARRAY_SIZE_9; Index++) { + TestBuffer[Index] =3D Index + 1; + TestResult[Index] =3D TEST_ARRAY_SIZE_9 - Index; + } + + PerformQuickSort (TestBuffer, TestCount, sizeof (UINT32), (SORT_COMPARE)= TestCompareFunction); + UT_ASSERT_MEM_EQUAL (TestBuffer, TestResult, sizeof (UINT32) * TEST_ARRA= Y_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 th= at may + consume it. + + @retval UNIT_TEST_PASSED The Unit test has completed and th= e 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[] =3D { L"abcdefg" }; + + retval =3D StringCompare (TestBuffer, TestBuffer); + UT_ASSERT_TRUE (retval =3D=3D 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 =3D NULL; + + DEBUG(( DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSIO= N )); + + // + // Start setting up the test framework for running the tests. + // + Status =3D InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCa= llerBaseName, UNIT_TEST_APP_VERSION); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status =3D %r\n= ", Status)); + goto EXIT; + } + + // + // Populate the UefiSortLib Unit Test Suite. + // + Status =3D CreateUnitTestSuite (&SortTests, Framework, "UefiSortLib Sort= Tests", "UefiSortLib.SortLib", NULL, NULL); + if (EFI_ERROR (Status)) { + DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for UefiSortLib AP= I Tests\n")); + Status =3D EFI_OUT_OF_RESOURCES; + goto EXIT; + } + + // + // --------------Suite--------Description------------Name--------------F= unction----------------Pre---Post---Context----------- + // + AddTestCase (SortTests, "Sort the Array", "Sort", SortUINT3= 2ArrayShouldSucceed, NULL, NULL, NULL); + AddTestCase (SortTests, "Compare the Buffer", "Compare", CompareSa= meBufferShouldSucceed, NULL, NULL, NULL); + + // + // Execute the tests. + // + Status =3D 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/MdeModulePkg.ci.yaml b/MdeModulePkg/MdeModulePkg.= ci.yaml index 8e01caf94cbf..aa304f2ccd5c 100644 --- a/MdeModulePkg/MdeModulePkg.ci.yaml +++ b/MdeModulePkg/MdeModulePkg.ci.yaml @@ -19,6 +19,7 @@ "ExceptionList": [ "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.UID", "8005", "UNIVERSAL_PAYLOAD_PCI_ROOT_BRIDGE.HID", + "8001", "UefiSortLibUnitTestMain", ], ## Both file path and directory path are accepted. "IgnoreFiles": [ --=20 2.20.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#79089): https://edk2.groups.io/g/devel/message/79089 Mute This Topic: https://groups.io/mt/84811245/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-