[v16,4/6] test/memarea: support alloc and free API test

Message ID 20230710064923.19849-5-fengchengwen@huawei.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series introduce memarea library |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

fengchengwen July 10, 2023, 6:49 a.m. UTC
  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>
Acked-by: Morten Brørup <mb@smartsharesystems.com>
---
 app/test/test_memarea.c | 214 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 213 insertions(+), 1 deletion(-)
  

Comments

Anatoly Burakov July 17, 2023, 1:57 p.m. UTC | #1
On 7/10/2023 7:49 AM, Chengwen Feng wrote:
> 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>
> Acked-by: Morten Brørup <mb@smartsharesystems.com>
> ---

> +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, "Expected Non-NULL");
> +
> +	/* test repeat free */
> +	rte_errno = 0;
> +	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1);
> +	TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
> +	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 0;

Same as in other, I think TEST_SUCCESS would be more correct (even 
though DPDK codebase doesn't always follow that convention).

Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
  
fengchengwen July 18, 2023, 2:49 a.m. UTC | #2
Hi Anatoly,

  Thanks for your review, both fix in v17.

Thanks.

On 2023/7/17 21:57, Burakov, Anatoly wrote:
> On 7/10/2023 7:49 AM, Chengwen Feng wrote:
>> 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>
>> Acked-by: Morten Brørup <mb@smartsharesystems.com>
>> ---
> 
>> +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, "Expected Non-NULL");
>> +
>> +    /* test repeat free */
>> +    rte_errno = 0;
>> +    ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1);
>> +    TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
>> +    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 0;
> 
> Same as in other, I think TEST_SUCCESS would be more correct (even though DPDK codebase doesn't always follow that convention).
> 
> Acked-by: Anatoly Burakov <anatoly.burakov@intel.com>
  

Patch

diff --git a/app/test/test_memarea.c b/app/test/test_memarea.c
index d8d5fc35d4..4053cdcac9 100644
--- a/app/test/test_memarea.c
+++ b/app/test/test_memarea.c
@@ -23,6 +23,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,7 +101,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;
@@ -115,6 +121,207 @@  test_memarea_create_destroy(void)
 	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);
+	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.ma.src = src_ma;
+	ma = rte_memarea_create(&init);
+	TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+	rte_memarea_destroy(ma);
+	rte_memarea_destroy(src_ma);
+
+	TEST_ASSERT(rte_errno == 0, "Expected ZERO");
+
+	return 0;
+}
+
+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, "Expected Non-NULL");
+
+	/* test for invalid ma */
+	ptr = rte_memarea_alloc(NULL, 1);
+	TEST_ASSERT(ptr == NULL, "Expected NULL");
+	TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL");
+
+	/* test for invalid size (size = 0) */
+	ptr = rte_memarea_alloc(ma, 0);
+	TEST_ASSERT(ptr == NULL, "Expected NULL");
+	TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL");
+
+	/* test for invalid size (size rewind) */
+	memset(&size, 0xff, sizeof(size));
+	ptr = rte_memarea_alloc(ma, size);
+	TEST_ASSERT(ptr == NULL, "Expected NULL");
+	TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL");
+
+	rte_memarea_destroy(ma);
+
+	return 0;
+}
+
+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, "Expected Non-NULL");
+	ptr = rte_memarea_alloc(ma, 1);
+	TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
+	test_memarea_fill_region(ptr, 1);
+
+	/* test for invalid ma */
+	rte_memarea_free(NULL, ptr);
+	TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL");
+
+	/* test for invalid ptr */
+	rte_memarea_free(ma, NULL);
+	TEST_ASSERT(rte_errno == EINVAL, "Expected EINVAL");
+
+	rte_memarea_destroy(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);
+	TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+
+	/* test alloc fail with big size */
+	ptr[0] = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE);
+	TEST_ASSERT(ptr[0] == NULL, "Expected NULL");
+	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, "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);
+	TEST_ASSERT(ptr[1] == NULL, "Expected NULL");
+	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, "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);
+	TEST_ASSERT(ptr[1] == NULL, "Expected NULL");
+	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, "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);
+	TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+
+	/* test repeat free */
+	rte_errno = 0;
+	ptr = rte_memarea_alloc(ma, MEMAREA_TEST_DEFAULT_SIZE >> 1);
+	TEST_ASSERT(ptr != NULL, "Expected Non-NULL");
+	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 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);
+	TEST_ASSERT(ma != NULL, "Expected Non-NULL");
+	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, "Expected Non-NULL");
+		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 0;
 }
 
@@ -125,6 +332,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 */
 	}