From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id D21B7423D4;
	Sat, 14 Jan 2023 12:50:29 +0100 (CET)
Received: from mails.dpdk.org (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id AC0D042BB1;
	Sat, 14 Jan 2023 12:50:13 +0100 (CET)
Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189])
 by mails.dpdk.org (Postfix) with ESMTP id 203F140DDC
 for <dev@dpdk.org>; Sat, 14 Jan 2023 12:50:10 +0100 (CET)
Received: from dggpeml500024.china.huawei.com (unknown [172.30.72.57])
 by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4NvGh54FbPzJqJ1;
 Sat, 14 Jan 2023 19:45:57 +0800 (CST)
Received: from localhost.localdomain (10.69.192.56) by
 dggpeml500024.china.huawei.com (7.185.36.10) with Microsoft SMTP Server
 (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id
 15.1.2375.34; Sat, 14 Jan 2023 19:50:08 +0800
From: Chengwen Feng <fengchengwen@huawei.com>
To: <david.marchand@redhat.com>, <mb@smartsharesystems.com>,
 <anatoly.burakov@intel.com>, <dmitry.kozliuk@gmail.com>,
 <jerinjacobk@gmail.com>, <hofors@lysator.liu.se>, <liudongdong3@huawei.com>
CC: <thomas@monjalon.net>, <dev@dpdk.org>
Subject: [PATCH v12 4/6] test/memarea: support alloc and free API test
Date: Sat, 14 Jan 2023 19:49:42 +0800
Message-ID: <20230114114944.42194-5-fengchengwen@huawei.com>
X-Mailer: git-send-email 2.33.0
In-Reply-To: <20230114114944.42194-1-fengchengwen@huawei.com>
References: <20220721044648.6817-1-fengchengwen@huawei.com>
 <20230114114944.42194-1-fengchengwen@huawei.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Content-Type: text/plain
X-Originating-IP: [10.69.192.56]
X-ClientProxiedBy: dggems701-chm.china.huawei.com (10.3.19.178) To
 dggpeml500024.china.huawei.com (7.185.36.10)
X-CFilter-Loop: Reflected
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org

This patch supports rte_memarea_alloc() and rte_memarea_free() API
test.

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
Reviewed-by: Dongdong Liu <liudongdong3@huawei.com>
---
 app/test/test_memarea.c | 135 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 134 insertions(+), 1 deletion(-)

diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c
index c9de779a1d..efabc594b7 100644
--- a/app/test/test_memarea.c
+++ b/app/test/test_memarea.c
@@ -47,6 +47,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)
 {
@@ -95,8 +101,8 @@ test_memarea_create_bad_param(void)
 static int
 test_memarea_create_destroy(void)
 {
+	struct rte_memarea *ma, *src_ma;
 	struct rte_memarea_param init;
-	struct rte_memarea *ma;
 
 	/* test for create with HEAP */
 	test_memarea_init_param(&init);
@@ -113,6 +119,130 @@ test_memarea_create_destroy(void)
 	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
 	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);
+	RTE_TEST_ASSERT(src_ma != NULL, "Expected Non-NULL");
+	test_memarea_init_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_MEMAREA;
+	init.total_sz = init.total_sz >> 1;
+	init.src_memarea = src_ma;
+	ma = rte_memarea_create(&init);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+	rte_memarea_destroy(ma);
+	rte_memarea_destroy(src_ma);
+
+	return 0;
+}
+
+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);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+
+	/* test alloc fail with big size */
+	ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE, 0);
+	RTE_TEST_ASSERT(ptr[0] == NULL, "Expected NULL");
+
+	/* test alloc fail because no memory */
+	ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE - RTE_CACHE_LINE_SIZE, 0);
+	RTE_TEST_ASSERT(ptr[0] != NULL, "Expected Non-NULL");
+	ptr[1] = rte_memarea_alloc(ma, 1, 0);
+	RTE_TEST_ASSERT(ptr[1] == NULL, "Expected NULL");
+	rte_memarea_free(ma, ptr[0]);
+
+	/* test alloc fail when second fail */
+	ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr[0] != NULL, "Expected Non-NULL");
+	test_memarea_fill_region(ptr[0], MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr[1] == NULL, "Expected NULL");
+	rte_memarea_free(ma, ptr[0]);
+	ptr[1] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr[1] != NULL, "Expected Non-NULL");
+	test_memarea_fill_region(ptr[1], MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	rte_memarea_free(ma, ptr[1]);
+
+	rte_memarea_destroy(ma);
+
+	return 0;
+}
+
+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);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+
+	/* test repeat free */
+	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
+	test_memarea_fill_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	rte_memarea_free(ma, ptr);
+	rte_memarea_free(ma, ptr);
+
+	rte_memarea_destroy(ma);
+
+	return 0;
+}
+
+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);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+	memset(ptr, 0, sizeof(ptr));
+
+	/* test random alloc and free */
+	for (i = 0; i < ALLOC_MAX_NUM; i++)
+		ptr[i] = rte_memarea_alloc(ma, 1, 0);
+
+	/* 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 free NULL */
+	rte_memarea_free(ma, NULL);
+
+	rte_memarea_destroy(ma);
+
 	return 0;
 }
 
@@ -123,6 +253,9 @@ test_memarea(void)
 
 	MEMAREA_TEST_API_RUN(test_memarea_create_bad_param);
 	MEMAREA_TEST_API_RUN(test_memarea_create_destroy);
+	MEMAREA_TEST_API_RUN(test_memarea_alloc_fail);
+	MEMAREA_TEST_API_RUN(test_memarea_free_fail);
+	MEMAREA_TEST_API_RUN(test_memarea_alloc_free);
 
 	return test_memarea_retcode();
 }
-- 
2.33.0