Basic tests for odpdrv_shm_pool are added here, creating a fixed size
pool and performing basic alloc/free on it

Signed-off-by: Christophe Milard <christophe.mil...@linaro.org>
---
 .../common_plat/validation/drv/drvshmem/drvshmem.c | 86 ++++++++++++++++++++++
 .../common_plat/validation/drv/drvshmem/drvshmem.h |  1 +
 2 files changed, 87 insertions(+)

diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.c 
b/test/common_plat/validation/drv/drvshmem/drvshmem.c
index 5843573..d4dedea 100644
--- a/test/common_plat/validation/drv/drvshmem/drvshmem.c
+++ b/test/common_plat/validation/drv/drvshmem/drvshmem.c
@@ -853,12 +853,98 @@ void drvshmem_test_buddy_basic(void)
        odpdrv_shm_pool_destroy(pool);
 }
 
+void drvshmem_test_slab_basic(void)
+{
+       odpdrv_shm_pool_param_t pool_params;
+       odpdrv_shm_pool_t pool, found_pool;
+       uint8_t *buff;
+       uint8_t *addrs[TEST_SZ];
+       uint16_t length;
+       int i, j;
+
+       /* create a pool and check that it can be looked up */
+       pool_params.pool_size = POOL_SZ;
+       pool_params.min_alloc = SZ_1K; /* constant size will give slab */
+       pool_params.max_alloc = SZ_1K;
+       pool = odpdrv_shm_pool_create(POOL_NAME, &pool_params);
+       found_pool = odpdrv_shm_pool_lookup(POOL_NAME);
+       CU_ASSERT(found_pool == pool);
+
+       /* alloc a 1k buffer, filling its contents: */
+       buff = odpdrv_shm_pool_alloc(pool, SZ_1K);
+       CU_ASSERT_PTR_NOT_NULL(buff);
+       for (i = 0; i < SZ_1K; i++)
+               buff[i] = BUFF_PATTERN;
+       odpdrv_shm_pool_print("buddy test: 1K reserved", pool);
+
+       /* alloc as many 1K buffer a possible */
+       for (i = 0; i < TEST_SZ; i++) {
+               length = SZ_1K;
+               addrs[i] = odpdrv_shm_pool_alloc(pool, length);
+               /* if alloc was success, fill buffer for later check */
+               if (addrs[i]) {
+                       for (j = 0; j < length; j++)
+                               addrs[i][j] = (uint8_t)(length & 0xFF);
+               }
+       }
+       odpdrv_shm_pool_print("slab test: after many mallocs", pool);
+
+       /* release every 3rth buffer, checking contents: */
+       for (i = 0; i < TEST_SZ; i += 3) {
+               /* if buffer was allocated, check the pattern in it */
+               if (addrs[i]) {
+                       length = SZ_1K;
+                       for (j = 0; j < length; j++)
+                               CU_ASSERT(addrs[i][j] ==
+                                         (uint8_t)(length & 0xFF));
+               }
+               odpdrv_shm_pool_free(pool, addrs[i]);
+       }
+       odpdrv_shm_pool_print("slab test: after 1/3 free:", pool);
+
+       /* realloc them:*/
+       for (i = 0; i < TEST_SZ; i += 3) {
+               length = SZ_1K;
+               addrs[i] = odpdrv_shm_pool_alloc(pool, length);
+               /* if alloc was success, fill buffer for later check */
+               if (addrs[i]) {
+                       for (j = 0; j < length; j++)
+                               addrs[i][j] = (uint8_t)(length & 0xFF);
+               }
+       }
+       odpdrv_shm_pool_print("slab test: after realloc:", pool);
+
+       /* free all (except buff), checking contents: */
+       for (i = 0; i < TEST_SZ; i++) {
+               /* if buffer was allocated, check the pattern in it */
+               if (addrs[i]) {
+                       length = SZ_1K;
+                       for (j = 0; j < length; j++)
+                               CU_ASSERT(addrs[i][j] ==
+                                         (uint8_t)(length & 0xFF))
+               }
+               odpdrv_shm_pool_free(pool, addrs[i]);
+       }
+       odpdrv_shm_pool_print("slab test: after all but 1K free:", pool);
+
+       /* check contents of our initial 1K buffer: */
+       for (i = 0; i < SZ_1K; i++)
+               CU_ASSERT((buff[i] == BUFF_PATTERN))
+       odpdrv_shm_pool_free(pool, buff);
+
+       odpdrv_shm_pool_print("slab test: after all free", pool);
+
+       /* destroy pool: */
+       odpdrv_shm_pool_destroy(pool);
+}
+
 odp_testinfo_t drvshmem_suite[] = {
        ODP_TEST_INFO(drvshmem_test_basic),
        ODP_TEST_INFO(drvshmem_test_reserve_after_fork),
        ODP_TEST_INFO(drvshmem_test_singleva_after_fork),
        ODP_TEST_INFO(drvshmem_test_stress),
        ODP_TEST_INFO(drvshmem_test_buddy_basic),
+       ODP_TEST_INFO(drvshmem_test_slab_basic),
        ODP_TEST_INFO_NULL,
 };
 
diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.h 
b/test/common_plat/validation/drv/drvshmem/drvshmem.h
index ab45f7c..fdc1080 100644
--- a/test/common_plat/validation/drv/drvshmem/drvshmem.h
+++ b/test/common_plat/validation/drv/drvshmem/drvshmem.h
@@ -15,6 +15,7 @@ void drvshmem_test_reserve_after_fork(void);
 void drvshmem_test_singleva_after_fork(void);
 void drvshmem_test_stress(void);
 void drvshmem_test_buddy_basic(void);
+void drvshmem_test_slab_basic(void);
 
 /* test arrays: */
 extern odp_testinfo_t drvshmem_suite[];
-- 
2.7.4

Reply via email to