diff mbox series

[v5,04/10] test/memarea: support alloc/free/update-refcnt test

Message ID tencent_0FCB315E0181FAFA2765AF6E3F360FCE5309@qq.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers show
Series [v5,01/10] memarea: introduce memarea library | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

datshan Oct. 5, 2022, 4:09 a.m. UTC
From: Chengwen Feng <fengchengwen@huawei.com>

This patch supports rte_memarea_alloc()/rte_memarea_free()/
rte_memarea_update_refcnt() test.

Signed-off-by: Chengwen Feng <fengchengwen@huawei.com>
---
 app/test/test_memarea.c | 150 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 149 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c
index 7c3d78652d..0a54ede4c1 100644
--- a/app/test/test_memarea.c
+++ b/app/test/test_memarea.c
@@ -41,6 +41,12 @@  test_memarea_init_def_param(struct rte_memarea_param *init)
 	init->mt_safe = 1;
 }
 
+static void
+test_memarea_fill_allocated_region(void *ptr, size_t size)
+{
+	memset(ptr, 0xff, size);
+}
+
 static int
 test_memarea_create_bad_param(void)
 {
@@ -106,8 +112,8 @@  static int
 test_memarea_create_destroy(void)
 {
 	uint8_t user_buffer[MEMAREA_TEST_DEFAULT_SIZE + RTE_CACHE_LINE_SIZE];
+	struct rte_memarea *ma, *user_ma;
 	struct rte_memarea_param init;
-	struct rte_memarea *ma;
 
 	/* test for create with RTE memory */
 	test_memarea_init_def_param(&init);
@@ -133,6 +139,145 @@  test_memarea_create_destroy(void)
 	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
 	rte_memarea_destroy(ma);
 
+	/* test for create with user-memarea */
+	test_memarea_init_def_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_SYSTEM_API;
+	user_ma = rte_memarea_create(&init);
+	RTE_TEST_ASSERT(user_ma != NULL, "Expected Non-NULL");
+	test_memarea_init_def_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_USER_MEMAREA;
+	init.total_sz = init.total_sz >> 1;
+	init.user_memarea = user_ma;
+	ma = rte_memarea_create(&init);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+	rte_memarea_destroy(ma);
+	rte_memarea_destroy(user_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_def_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_SYSTEM_API;
+	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_allocated_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_allocated_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;
+
+	/* prepare env */
+	test_memarea_init_def_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_SYSTEM_API;
+	init.total_sz = MEMAREA_TEST_DEFAULT_SIZE;
+	ma = rte_memarea_create(&init);
+	RTE_TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+
+	/* test invalid parameters with update-refcnt */
+	rte_memarea_update_refcnt(NULL, (void *)(uintptr_t)1, 0);
+	rte_memarea_update_refcnt(ma, NULL, 0);
+	rte_memarea_update_refcnt(NULL, NULL, 0);
+
+	/* test free with refcnt fail */
+	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
+	test_memarea_fill_allocated_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	rte_memarea_free(ma, ptr);
+	rte_memarea_free(ma, ptr);
+
+	/* test update refcnt with fail */
+	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
+	test_memarea_fill_allocated_region(ptr, MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	rte_memarea_update_refcnt(ma, ptr, -2);
+	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1, 0);
+	RTE_TEST_ASSERT(ptr == NULL, "Expected NULL");
+
+	rte_memarea_destroy(ma);
+
+	return 0;
+}
+
+static int
+test_memarea_alloc_free(void)
+{
+#define ALLOC_MAX_NUM	8
+	struct rte_memarea_param init;
+	struct rte_memarea *ma;
+	void *ptr[ALLOC_MAX_NUM];
+	int i;
+
+	/* prepare env */
+	test_memarea_init_def_param(&init);
+	init.source = RTE_MEMAREA_SOURCE_SYSTEM_API;
+	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, ptr[6]);
+
+	rte_memarea_destroy(ma);
+
 	return 0;
 }
 
@@ -141,6 +286,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 0;
 }