diff mbox

[PATCHv3,1/3] validation: buffer: add initial buffer pool tests

Message ID 1419022611-28989-2-git-send-email-taras.kondratiuk@linaro.org
State New
Headers show

Commit Message

Taras Kondratiuk Dec. 19, 2014, 8:56 p.m. UTC
Reviewed-and-tested-by: Bill Fischofer <bill.fischofer@linaro.org>
Signed-off-by: Taras Kondratiuk <taras.kondratiuk@linaro.org>
---
 test/validation/.gitignore                    |   1 +
 test/validation/Makefile.am                   |   5 +-
 test/validation/buffer/odp_buffer_pool_test.c | 212 ++++++++++++++++++++++++++
 test/validation/buffer/odp_buffer_tests.h     |  21 +++
 test/validation/odp_buffer.c                  |  14 ++
 5 files changed, 252 insertions(+), 1 deletion(-)
 create mode 100644 test/validation/buffer/odp_buffer_pool_test.c
 create mode 100644 test/validation/buffer/odp_buffer_tests.h
 create mode 100644 test/validation/odp_buffer.c
diff mbox

Patch

diff --git a/test/validation/.gitignore b/test/validation/.gitignore
index 32834ae..cc05f30 100644
--- a/test/validation/.gitignore
+++ b/test/validation/.gitignore
@@ -5,3 +5,4 @@  odp_queue
 odp_crypto
 odp_schedule
 odp_shm
+odp_buffer
diff --git a/test/validation/Makefile.am b/test/validation/Makefile.am
index a12fdfa..e7e6fec 100644
--- a/test/validation/Makefile.am
+++ b/test/validation/Makefile.am
@@ -6,8 +6,9 @@  AM_LDFLAGS += -static
 if ODP_CUNIT_ENABLED
 TESTS = ${bin_PROGRAMS}
 check_PROGRAMS = ${bin_PROGRAMS}
-bin_PROGRAMS = odp_init odp_queue odp_crypto odp_shm odp_schedule
+bin_PROGRAMS = odp_init odp_queue odp_crypto odp_shm odp_schedule odp_buffer
 odp_crypto_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/crypto
+odp_buffer_CFLAGS = $(AM_CFLAGS) -I$(srcdir)/buffer
 endif
 
 dist_odp_init_SOURCES = odp_init.c
@@ -18,3 +19,5 @@  dist_odp_crypto_SOURCES = crypto/odp_crypto_test_async_inp.c \
 			  odp_crypto.c common/odp_cunit_common.c
 dist_odp_shm_SOURCES = odp_shm.c common/odp_cunit_common.c
 dist_odp_schedule_SOURCES = odp_schedule.c common/odp_cunit_common.c
+dist_odp_buffer_SOURCES = buffer/odp_buffer_pool_test.c \
+			  odp_buffer.c common/odp_cunit_common.c
diff --git a/test/validation/buffer/odp_buffer_pool_test.c b/test/validation/buffer/odp_buffer_pool_test.c
new file mode 100644
index 0000000..6f5eaf7
--- /dev/null
+++ b/test/validation/buffer/odp_buffer_pool_test.c
@@ -0,0 +1,212 @@ 
+/* Copyright (c) 2014, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:	BSD-3-Clause
+ */
+
+#include "odp_buffer_tests.h"
+
+static int pool_name_number = 1;
+static const int default_buffer_size = 1500;
+static const int default_buffer_num = 1000;
+
+odp_buffer_pool_t pool_create(int buf_num, int buf_size, int buf_type)
+{
+	odp_buffer_pool_t pool;
+	char pool_name[ODP_BUFFER_POOL_NAME_LEN];
+	odp_buffer_pool_param_t params = {
+			.buf_size  = buf_size,
+			.buf_align = ODP_CACHE_LINE_SIZE,
+			.num_bufs  = buf_num,
+			.buf_type  = buf_type,
+	};
+
+	snprintf(pool_name, sizeof(pool_name),
+		 "test_buffer_pool-%d", pool_name_number++);
+
+	pool = odp_buffer_pool_create(pool_name, ODP_SHM_INVALID, &params);
+	CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID);
+
+	return pool;
+}
+
+static void pool_create_destroy_type(int type)
+{
+	odp_buffer_pool_t pool;
+	pool = pool_create(default_buffer_num, default_buffer_size, type);
+
+	CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
+}
+
+static void pool_create_destroy_raw(void)
+{
+	pool_create_destroy_type(ODP_BUFFER_TYPE_RAW);
+}
+
+static void pool_create_destroy_packet(void)
+{
+	pool_create_destroy_type(ODP_BUFFER_TYPE_PACKET);
+}
+
+static void pool_create_destroy_timeout(void)
+{
+	pool_create_destroy_type(ODP_BUFFER_TYPE_TIMEOUT);
+}
+
+static void pool_create_destroy_any(void)
+{
+	pool_create_destroy_type(ODP_BUFFER_TYPE_ANY);
+}
+
+static void pool_create_destroy_raw_shm(void)
+{
+	odp_buffer_pool_t pool;
+	odp_shm_t test_shm;
+	odp_buffer_pool_param_t params = {
+			.buf_size  = 1500,
+			.buf_align = ODP_CACHE_LINE_SIZE,
+			.num_bufs  = 10,
+			.buf_type  = ODP_BUFFER_TYPE_RAW,
+	};
+
+	test_shm = odp_shm_reserve("test_shm",
+				   params.buf_size * params.num_bufs * 2,
+				   ODP_CACHE_LINE_SIZE,
+				   0);
+	CU_ASSERT_FATAL(test_shm != ODP_SHM_INVALID);
+
+	pool = odp_buffer_pool_create("test_shm_pool", test_shm, &params);
+	CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID);
+
+	CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
+	CU_ASSERT(odp_shm_free(test_shm) == 0);
+}
+
+static void pool_lookup_info_print(void)
+{
+	odp_buffer_pool_t pool;
+	const char pool_name[] = "pool_for_lookup_test";
+	odp_buffer_pool_info_t info;
+	odp_buffer_pool_param_t params = {
+			.buf_size  = default_buffer_size,
+			.buf_align = ODP_CACHE_LINE_SIZE,
+			.num_bufs  = default_buffer_num,
+			.buf_type  = ODP_BUFFER_TYPE_RAW,
+	};
+
+	pool = odp_buffer_pool_create(pool_name, ODP_SHM_INVALID, &params);
+	CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID);
+
+	pool = odp_buffer_pool_lookup(pool_name);
+	CU_ASSERT_FATAL(pool != ODP_BUFFER_POOL_INVALID);
+
+	CU_ASSERT_FATAL(odp_buffer_pool_info(pool, &info) == 0);
+	CU_ASSERT(strncmp(pool_name, info.name, sizeof(pool_name)) == 0);
+	CU_ASSERT(info.shm == ODP_SHM_INVALID);
+	CU_ASSERT(params.buf_size <= info.params.buf_size);
+	CU_ASSERT(params.buf_align <= info.params.buf_align);
+	CU_ASSERT(params.num_bufs <= info.params.num_bufs);
+	CU_ASSERT(params.buf_type == info.params.buf_type);
+
+	odp_buffer_pool_print(pool);
+
+	CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
+}
+
+static void pool_alloc_buffer_type(int type)
+{
+	odp_buffer_pool_t pool;
+	const int buf_num = 3;
+	const size_t buf_size = 1500;
+	odp_buffer_t buffer[buf_num];
+	int buf_index;
+	char wrong_type = 0, wrong_size = 0;
+
+	pool = pool_create(buf_num, buf_size, type);
+	odp_buffer_pool_print(pool);
+
+	/* Try to allocate buf_num buffers from the pool */
+	for (buf_index = 0; buf_index < buf_num; buf_index++) {
+		buffer[buf_index] = odp_buffer_alloc(pool);
+		if (buffer[buf_index] == ODP_BUFFER_INVALID)
+			break;
+		if (odp_buffer_type(buffer[buf_index]) != type)
+			wrong_type = 1;
+		if (odp_buffer_size(buffer[buf_index]) < buf_size)
+			wrong_size = 1;
+		if (wrong_type || wrong_size)
+			odp_buffer_print(buffer[buf_index]);
+	}
+
+	/* Check that the pool had at least buf_num buffers */
+	CU_ASSERT(buf_index == buf_num);
+	/* buf_index points out of buffer[] or it point to an invalid buffer */
+	buf_index--;
+
+	/* Check that the pool had correct buffers */
+	CU_ASSERT(wrong_type == 0);
+	CU_ASSERT(wrong_size == 0);
+
+	for (; buf_index >= 0; buf_index--)
+		odp_buffer_free(buffer[buf_index]);
+
+	CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
+}
+
+static void pool_alloc_buffer_raw(void)
+{
+	pool_alloc_buffer_type(ODP_BUFFER_TYPE_RAW);
+}
+
+static void pool_alloc_buffer_packet(void)
+{
+	pool_alloc_buffer_type(ODP_BUFFER_TYPE_PACKET);
+}
+
+static void pool_alloc_buffer_timeout(void)
+{
+	pool_alloc_buffer_type(ODP_BUFFER_TYPE_TIMEOUT);
+}
+
+static void pool_alloc_buffer_any(void)
+{
+	pool_alloc_buffer_type(ODP_BUFFER_TYPE_ANY);
+}
+
+static void pool_free_buffer(void)
+{
+	odp_buffer_pool_t pool;
+	odp_buffer_t buffer;
+	pool = pool_create(1, 64, ODP_BUFFER_TYPE_RAW);
+
+	/* Allocate the only buffer from the pool */
+	buffer = odp_buffer_alloc(pool);
+	CU_ASSERT_FATAL(buffer != ODP_BUFFER_INVALID);
+
+	/** @todo: is it correct to assume the pool had only one buffer? */
+	CU_ASSERT_FATAL(odp_buffer_alloc(pool) == ODP_BUFFER_INVALID)
+
+	odp_buffer_free(buffer);
+
+	/* Check that the buffer was returned back to the pool */
+	buffer = odp_buffer_alloc(pool);
+	CU_ASSERT_FATAL(buffer != ODP_BUFFER_INVALID);
+
+	odp_buffer_free(buffer);
+	CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
+}
+
+CU_TestInfo buffer_pool_tests[] = {
+	_CU_TEST_INFO(pool_create_destroy_raw),
+	_CU_TEST_INFO(pool_create_destroy_packet),
+	_CU_TEST_INFO(pool_create_destroy_timeout),
+	_CU_TEST_INFO(pool_create_destroy_any),
+	_CU_TEST_INFO(pool_create_destroy_raw_shm),
+	_CU_TEST_INFO(pool_lookup_info_print),
+	_CU_TEST_INFO(pool_alloc_buffer_raw),
+	_CU_TEST_INFO(pool_alloc_buffer_packet),
+	_CU_TEST_INFO(pool_alloc_buffer_timeout),
+	_CU_TEST_INFO(pool_alloc_buffer_any),
+	_CU_TEST_INFO(pool_free_buffer),
+	CU_TEST_INFO_NULL,
+};
diff --git a/test/validation/buffer/odp_buffer_tests.h b/test/validation/buffer/odp_buffer_tests.h
new file mode 100644
index 0000000..4184fb5
--- /dev/null
+++ b/test/validation/buffer/odp_buffer_tests.h
@@ -0,0 +1,21 @@ 
+/* Copyright (c) 2014, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:	BSD-3-Clause
+ */
+
+#ifndef ODP_BUFFER_TESTS_H_
+#define ODP_BUFFER_TESTS_H_
+
+#include <odp.h>
+#include <odp_packet.h>
+#include "odp_cunit_common.h"
+
+/* Helper macro for CU_TestInfo initialization */
+#define _CU_TEST_INFO(test_func) {#test_func, test_func}
+
+extern CU_TestInfo buffer_pool_tests[];
+
+odp_buffer_pool_t pool_create(int buf_num, int buf_size, int buf_type);
+
+#endif /* ODP_BUFFER_TESTS_H_ */
diff --git a/test/validation/odp_buffer.c b/test/validation/odp_buffer.c
new file mode 100644
index 0000000..daa30ca
--- /dev/null
+++ b/test/validation/odp_buffer.c
@@ -0,0 +1,14 @@ 
+/* Copyright (c) 2014, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:	BSD-3-Clause
+ */
+
+#include "odp_buffer_tests.h"
+
+CU_SuiteInfo odp_testsuites[] = {
+	{ .pName = "buffer Pool tests",
+			.pTests = buffer_pool_tests,
+	},
+	CU_SUITE_INFO_NULL,
+};