diff mbox

[API-NEXT,4/5] test: drv: shm: adding small allocation tests

Message ID 1482319702-5766-5-git-send-email-christophe.milard@linaro.org
State New
Headers show

Commit Message

Christophe Milard Dec. 21, 2016, 11:28 a.m. UTC
Tests for odpdrv_shm_sreserve() and odpdrv_shm_sfree are added,
testing the ishm small memory unit allocator.

Signed-off-by: Christophe Milard <christophe.milard@linaro.org>

---
 .../common_plat/validation/drv/drvshmem/drvshmem.c | 77 ++++++++++++++++++++++
 .../common_plat/validation/drv/drvshmem/drvshmem.h |  1 +
 2 files changed, 78 insertions(+)

-- 
2.7.4
diff mbox

Patch

diff --git a/test/common_plat/validation/drv/drvshmem/drvshmem.c b/test/common_plat/validation/drv/drvshmem/drvshmem.c
index 0247a03..c129693 100644
--- a/test/common_plat/validation/drv/drvshmem/drvshmem.c
+++ b/test/common_plat/validation/drv/drvshmem/drvshmem.c
@@ -21,6 +21,10 @@ 
 #define STRESS_RANDOM_SZ 5
 #define STRESS_ITERATION 5000
 
+#define TEST_SZ 1000
+#define SZ_1K   1024
+#define BUFF_PATTERN 0xA3
+
 typedef enum {
 	STRESS_FREE, /* entry is free and can be allocated */
 	STRESS_BUSY, /* entry is being processed: don't touch */
@@ -762,11 +766,84 @@  void drvshmem_test_stress(void)
 	CU_ASSERT(odpdrv_shm_print_all("After stress tests") == base);
 }
 
+void drvshmem_test_small_basic(void)
+{
+	uint8_t *buff;
+	uint8_t *addrs[TEST_SZ];
+	uint8_t length;
+	int i, j;
+
+	/* alloc a 1k buffer, filling its contents: */
+	buff = odpdrv_shm_sreserve(SZ_1K);
+	CU_ASSERT_PTR_NOT_NULL(buff);
+	for (i = 0; i < SZ_1K; i++)
+		buff[i] = BUFF_PATTERN;
+	odpdrv_shm_print_all("sreserve test: 1K reserved");
+
+	/* alloc as many buffer a possible on increseasing sz */
+	for (i = 0; i < TEST_SZ; i++) {
+		length = i * 16;
+		addrs[i] = odpdrv_shm_sreserve(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_print_all("sreserve test: after many smalloc");
+
+	/* 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 = i * 16;
+			for (j = 0; j < length; j++)
+				CU_ASSERT(addrs[i][j] ==
+					  (uint8_t)(length & 0xFF));
+		}
+		odpdrv_shm_sfree(addrs[i]);
+	}
+	odpdrv_shm_print_all("sreserve test: after 1/3 free:");
+
+	/* realloc them:*/
+	for (i = 0; i < TEST_SZ; i += 3) {
+		length = i * 16;
+		addrs[i] = odpdrv_shm_sreserve(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_print_all("sreserve test: after realloc:");
+
+	/* 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 = i * 16;
+			for (j = 0; j < length; j++)
+				CU_ASSERT(addrs[i][j] ==
+					  (uint8_t)(length & 0xFF))
+		}
+		odpdrv_shm_sfree(addrs[i]);
+	}
+	odpdrv_shm_print_all("sreserve test: after all but 1K free:");
+
+	/* check contents of our initial 1K buffer: */
+	for (i = 0; i < SZ_1K; i++)
+		CU_ASSERT((buff[i] == BUFF_PATTERN))
+	odpdrv_shm_sfree(buff);
+
+	odpdrv_shm_print_all("sreserve test: after all free");
+}
+
 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_small_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 f4c26a1..0dcbccb 100644
--- a/test/common_plat/validation/drv/drvshmem/drvshmem.h
+++ b/test/common_plat/validation/drv/drvshmem/drvshmem.h
@@ -14,6 +14,7 @@  void drvshmem_test_basic(void);
 void drvshmem_test_reserve_after_fork(void);
 void drvshmem_test_singleva_after_fork(void);
 void drvshmem_test_stress(void);
+void drvshmem_test_small_basic(void);
 
 /* test arrays: */
 extern odp_testinfo_t drvshmem_suite[];