From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C9BFE42EBF; Thu, 20 Jul 2023 11:31:52 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id CED1142D44; Thu, 20 Jul 2023 11:31:18 +0200 (CEST) Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by mails.dpdk.org (Postfix) with ESMTP id 4A4AD410FB for ; Thu, 20 Jul 2023 11:31:10 +0200 (CEST) Received: from dggpeml100024.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4R66qM01YrzrRqV; Thu, 20 Jul 2023 17:30:23 +0800 (CST) Received: from localhost.localdomain (10.50.163.32) by dggpeml100024.china.huawei.com (7.185.36.115) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Thu, 20 Jul 2023 17:31:08 +0800 From: Chengwen Feng To: , CC: , , , , , , Subject: [PATCH v19 4/6] test/memarea: support alloc and free API test Date: Thu, 20 Jul 2023 09:22:52 +0000 Message-ID: <20230720092254.54157-5-fengchengwen@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230720092254.54157-1-fengchengwen@huawei.com> References: <20220721044648.6817-1-fengchengwen@huawei.com> <20230720092254.54157-1-fengchengwen@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Originating-IP: [10.50.163.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpeml100024.china.huawei.com (7.185.36.115) X-CFilter-Loop: Reflected X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This patch supports rte_memarea_alloc() and rte_memarea_free() API test. Signed-off-by: Chengwen Feng Reviewed-by: Dongdong Liu Acked-by: Morten Brørup Acked-by: Anatoly Burakov --- app/test/test_memarea.c | 222 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 221 insertions(+), 1 deletion(-) diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c index 6078c93a16..805fb82d08 100644 --- a/app/test/test_memarea.c +++ b/app/test/test_memarea.c @@ -38,6 +38,12 @@ test_memarea_init_param(struct rte_memarea_param *init) init->mt_safe = 1; } +static void +test_memarea_fill_region(void *ptr, size_t size) +{ + memset(ptr, 0xff, size); +} + static int test_memarea_create_bad_param(void) { @@ -120,7 +126,7 @@ test_memarea_create_bad_param(void) static int test_memarea_create_destroy(void) { - struct rte_memarea *ma; + struct rte_memarea *ma, *src_ma; struct rte_memarea_param init; rte_errno = 0; @@ -140,6 +146,215 @@ test_memarea_create_destroy(void) TEST_ASSERT(ma != NULL, "Memarea creation failed"); rte_memarea_destroy(ma); + /* test for create with another memarea */ + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + src_ma = rte_memarea_create(&init); + TEST_ASSERT(src_ma != NULL, "Memarea creation failed"); + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_MEMAREA; + init.total_sz = init.total_sz >> 1; + init.ma.src = src_ma; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + rte_memarea_destroy(ma); + rte_memarea_destroy(src_ma); + + TEST_ASSERT(rte_errno == 0, "Expected ZERO"); + + return TEST_SUCCESS; +} + +static int +test_memarea_alloc_bad_param(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + size_t size; + void *ptr; + + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + + /* test for invalid ma */ + rte_errno = 0; + ptr = rte_memarea_alloc(NULL, 1); + TEST_ASSERT(ptr == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL"); + + /* test for invalid size (size = 0) */ + rte_errno = 0; + ptr = rte_memarea_alloc(ma, 0); + TEST_ASSERT(ptr == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL"); + + /* test for invalid size (size rewind) */ + rte_errno = 0; + memset(&size, 0xff, sizeof(size)); + ptr = rte_memarea_alloc(ma, size); + TEST_ASSERT(ptr == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL"); + + rte_memarea_destroy(ma); + + return TEST_SUCCESS; +} + +static int +test_memarea_free_bad_param(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr; + + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + ptr = rte_memarea_alloc(ma, 1); + TEST_ASSERT(ptr != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr, 1); + + /* test for invalid ma */ + rte_errno = 0; + rte_memarea_free(NULL, ptr); + TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL"); + + /* test for invalid ptr */ + rte_errno = 0; + rte_memarea_free(ma, NULL); + TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL"); + + rte_memarea_destroy(ma); + + return TEST_SUCCESS; +} + +static int +test_memarea_alloc_fail(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr[2]; + + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + + /* test alloc fail with big size */ + rte_errno = 0; + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE); + TEST_ASSERT(ptr[0] == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == ENOMEM, "Expected ENOMEM"); + + /* test alloc fail because no memory */ + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr[0] != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr[0], MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_errno = 0; + ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr[1] == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == ENOMEM, "Expected ENOMEM"); + rte_memarea_free(ma, ptr[0]); + + /* test alloc fail when second fail */ + ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr[0] != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr[0], MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_errno = 0; + ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr[1] == NULL, "Memarea allocation expect fail"); + TEST_ASSERT(rte_errno == ENOMEM, "Expected ENOMEM"); + rte_memarea_free(ma, ptr[0]); + ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr[1] != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr[1], MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_memarea_free(ma, ptr[1]); + + rte_memarea_destroy(ma); + + return TEST_SUCCESS; +} + +static int +test_memarea_free_fail(void) +{ + struct rte_memarea_param init; + struct rte_memarea *ma; + void *ptr; + + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + + /* test repeat free */ + rte_errno = 0; + ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1); + TEST_ASSERT(ptr != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1); + rte_memarea_free(ma, ptr); + TEST_ASSERT(rte_errno == 0, "Expected Zero"); + rte_memarea_free(ma, ptr); + TEST_ASSERT(rte_errno == EFAULT, "Expected EFAULT"); + + rte_memarea_destroy(ma); + + return TEST_SUCCESS; +} + +static int +test_memarea_alloc_free(void) +{ +#define ALLOC_MAX_NUM 8 + struct rte_memarea_param init; + void *ptr[ALLOC_MAX_NUM]; + struct rte_memarea *ma; + int i; + + test_memarea_init_param(&init); + init.source = RTE_MEMAREA_SOURCE_LIBC; + init.total_sz = MEMAREA_TEST_DEFAULT_SIZE; + ma = rte_memarea_create(&init); + TEST_ASSERT(ma != NULL, "Memarea creation failed"); + memset(ptr, 0, sizeof(ptr)); + + rte_errno = 0; + + /* test random alloc and free */ + for (i = 0; i < ALLOC_MAX_NUM; i++) { + ptr[i] = rte_memarea_alloc(ma, 1); + TEST_ASSERT(ptr[i] != NULL, "Memarea allocation failed"); + test_memarea_fill_region(ptr[i], 1); + } + + /* test merge left */ + rte_memarea_free(ma, ptr[0]); + rte_memarea_free(ma, ptr[1]); + + /* test merge right */ + rte_memarea_free(ma, ptr[7]); + rte_memarea_free(ma, ptr[6]); + + /* test merge left and right */ + rte_memarea_free(ma, ptr[3]); + rte_memarea_free(ma, ptr[2]); + + /* test merge remains */ + rte_memarea_free(ma, ptr[4]); + rte_memarea_free(ma, ptr[5]); + + TEST_ASSERT(rte_errno == 0, "Expected Zero"); + + rte_memarea_destroy(ma); + return TEST_SUCCESS; } @@ -150,6 +365,11 @@ static struct unit_test_suite memarea_test_suite = { .unit_test_cases = { TEST_CASE(test_memarea_create_bad_param), TEST_CASE(test_memarea_create_destroy), + TEST_CASE(test_memarea_alloc_bad_param), + TEST_CASE(test_memarea_free_bad_param), + TEST_CASE(test_memarea_alloc_fail), + TEST_CASE(test_memarea_free_fail), + TEST_CASE(test_memarea_alloc_free), TEST_CASES_END() /**< NULL terminate unit test array */ } -- 2.17.1