On Wed, Nov 19, 2014 at 3:39 PM, Taras Kondratiuk
<[email protected]> wrote:
> Initial version of buffer pool, buffer and packet API tests.
> Based on Bill's buffer pool API update series [1].
> odp_packet_seg_*() functions are not implemented in linux-generic, so
> these tests are guarded by TEST_PACKET_SEGMENTS define.
>
> Current linux-generic has issue in odp_buffer_pool_create(). Created
> buffer has a wrong number of buffers, so many tests are failing.
>
> [1] http://lists.linaro.org/pipermail/lng-odp/2014-November/004992.html
>
> Signed-off-by: Taras Kondratiuk <[email protected]>
> ---
>  configure.ac                              |    1 +
>  test/cunit/Makefile.am                    |    2 +
>  test/cunit/buffer/Makefile.am             |   11 +
>  test/cunit/buffer/odp_buffer_pool_test.c  |  213 ++++++++++++++++++
>  test/cunit/buffer/odp_buffer_test.c       |   52 +++++
>  test/cunit/buffer/odp_buffer_testsuites.c |   51 +++++
>  test/cunit/buffer/odp_buffer_testsuites.h |   30 +++
>  test/cunit/buffer/odp_packet_test.c       |  337 
> +++++++++++++++++++++++++++++
>  8 files changed, 697 insertions(+)
>  create mode 100644 test/cunit/buffer/Makefile.am
>  create mode 100644 test/cunit/buffer/odp_buffer_pool_test.c
>  create mode 100644 test/cunit/buffer/odp_buffer_test.c
>  create mode 100644 test/cunit/buffer/odp_buffer_testsuites.c
>  create mode 100644 test/cunit/buffer/odp_buffer_testsuites.h
>  create mode 100644 test/cunit/buffer/odp_packet_test.c
>
> diff --git a/configure.ac b/configure.ac
> index fcd7279..7a4b277 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -173,6 +173,7 @@ AC_CONFIG_FILES([Makefile
>                  test/Makefile
>                  test/api_test/Makefile
>                   test/cunit/Makefile
> +                test/cunit/buffer/Makefile
>                  pkgconfig/libodp.pc])
>
>  AC_SEARCH_LIBS([timer_create],[rt posix4])
> diff --git a/test/cunit/Makefile.am b/test/cunit/Makefile.am
> index 4014bed..04c94b2 100644
> --- a/test/cunit/Makefile.am
> +++ b/test/cunit/Makefile.am
> @@ -3,6 +3,8 @@ include $(top_srcdir)/test/Makefile.inc
>  AM_CFLAGS += -I$(CUNIT_PATH)/include
>  AM_LDFLAGS += -L$(CUNIT_PATH)/lib
>
> +SUBDIRS = buffer
> +
>  if ODP_CUNIT_ENABLED
>  TESTS = ${bin_PROGRAMS}
>  check_PROGRAMS = ${bin_PROGRAMS}
> diff --git a/test/cunit/buffer/Makefile.am b/test/cunit/buffer/Makefile.am
> new file mode 100644
> index 0000000..071a701
> --- /dev/null
> +++ b/test/cunit/buffer/Makefile.am
> @@ -0,0 +1,11 @@
> +include $(top_srcdir)/test/Makefile.inc
> +
> +if ODP_CUNIT_ENABLED
> +bin_PROGRAMS = odp_buffer_test
> +odp_buffer_test_LDFLAGS = $(AM_LDFLAGS) -static -lcunit

Do you have cunit installed in standard paths? I don't, and I had to
add this here:

odp_schedule_test_LDFLAGS = $(AM_LDFLAGS) -L$(CUNIT_PATH)/lib -static -lcunit
odp_schedule_test_CFLAGS = $(AM_CFLAGS) -I$(CUNIT_PATH)/include


> +endif
> +
> +dist_odp_buffer_test_SOURCES = odp_buffer_pool_test.c \
> +                                                               
> odp_buffer_test.c \
> +                                                               
> odp_packet_test.c \
> +                                                               
> odp_buffer_testsuites.c
> diff --git a/test/cunit/buffer/odp_buffer_pool_test.c 
> b/test/cunit/buffer/odp_buffer_pool_test.c
> new file mode 100644
> index 0000000..cd7711e
> --- /dev/null
> +++ b/test/cunit/buffer/odp_buffer_pool_test.c
> @@ -0,0 +1,213 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:    BSD-3-Clause
> + */
> +
> +#include "odp_buffer_testsuites.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_shm_t shm;
> +       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, &shm, &info) == 0);
> +       CU_ASSERT(shm == ODP_SHM_INVALID);
> +       CU_ASSERT(strncmp(pool_name, info.name, sizeof(pool_name)) == 0);
> +       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/cunit/buffer/odp_buffer_test.c 
> b/test/cunit/buffer/odp_buffer_test.c
> new file mode 100644
> index 0000000..e4a6b68
> --- /dev/null
> +++ b/test/cunit/buffer/odp_buffer_test.c
> @@ -0,0 +1,52 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:    BSD-3-Clause
> + */
> +
> +#include "odp_buffer_testsuites.h"
> +
> +static odp_buffer_pool_t raw_pool;
> +static odp_buffer_t raw_buffer = ODP_BUFFER_INVALID;
> +static const size_t raw_buffer_size = 1500;
> +
> +int buffer_tests_init(void)
> +{
> +       odp_buffer_pool_param_t params = {
> +                       .buf_size  = raw_buffer_size,
> +                       .buf_align = ODP_CACHE_LINE_SIZE,
> +                       .num_bufs  = 100,
> +                       .buf_type  = ODP_BUFFER_TYPE_RAW,
> +       };
> +
> +       raw_pool = odp_buffer_pool_create("raw_pool", ODP_SHM_INVALID, 
> &params);
> +       if (raw_pool == ODP_BUFFER_POOL_INVALID)
> +               return -1;
> +       raw_buffer = odp_buffer_alloc(raw_pool);
> +       if (raw_buffer == ODP_BUFFER_INVALID)
> +               return -1;
> +       return 0;
> +}
> +
> +int buffer_tests_finalize(void)
> +{
> +       odp_buffer_free(raw_buffer);
> +       if (odp_buffer_pool_destroy(raw_pool) != 0)
> +               return -1;
> +       return 0;
> +}
> +
> +static void buffer_management_basic(void)
> +{
> +       CU_ASSERT(odp_buffer_is_valid(raw_buffer) == 1);
> +       CU_ASSERT(odp_buffer_pool(raw_buffer) != ODP_BUFFER_POOL_INVALID);
> +       CU_ASSERT(odp_buffer_type(raw_buffer) == ODP_BUFFER_TYPE_RAW);
> +       CU_ASSERT(odp_buffer_size(raw_buffer) >= raw_buffer_size);
> +       CU_ASSERT(odp_buffer_addr(raw_buffer) != NULL);
> +       odp_buffer_print(raw_buffer);
> +}
> +
> +CU_TestInfo buffer_tests[] = {
> +       _CU_TEST_INFO(buffer_management_basic),
> +       CU_TEST_INFO_NULL,
> +};
> diff --git a/test/cunit/buffer/odp_buffer_testsuites.c 
> b/test/cunit/buffer/odp_buffer_testsuites.c
> new file mode 100644
> index 0000000..ac28c58
> --- /dev/null
> +++ b/test/cunit/buffer/odp_buffer_testsuites.c
> @@ -0,0 +1,51 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:    BSD-3-Clause
> + */
> +
> +#include "odp_buffer_testsuites.h"
> +
> +static CU_SuiteInfo suites[] = {
> +       { .pName = "buffer Pool tests",
> +                       .pTests = buffer_pool_tests,
> +       },
> +       { .pName = "buffer tests",
> +                       .pTests = buffer_tests,
> +                       .pInitFunc = buffer_tests_init,
> +                       .pCleanupFunc = buffer_tests_finalize,
> +       },
> +       { .pName = "packet tests",
> +                       .pTests = packet_tests,
> +                       .pInitFunc = packet_tests_init,
> +                       .pCleanupFunc = packet_tests_finalize,
> +       },
> +       CU_SUITE_INFO_NULL,
> +};
> +
> +int main(void)
> +{
> +       if (odp_init_global(NULL, NULL)) {
> +               ODP_ERR("ODP global init failed.\n");
> +               return -1;
> +       }
> +       odp_init_local();
> +
> +       printf("\tODP version: %s\n", odp_version_api_str());
> +
> +       /* initialize the CUnit test registry */
> +       if (CUE_SUCCESS != CU_initialize_registry())
> +               return CU_get_error();
> +
> +       /* register suites */
> +       CU_register_suites(suites);
> +       /* Run all tests using the CUnit Basic interface */
> +       CU_basic_set_mode(CU_BRM_VERBOSE);
> +       CU_basic_run_tests();
> +       CU_cleanup_registry();
> +
> +       odp_term_local();
> +       odp_term_global();
> +
> +       return CU_get_error();
> +}
> diff --git a/test/cunit/buffer/odp_buffer_testsuites.h 
> b/test/cunit/buffer/odp_buffer_testsuites.h
> new file mode 100644
> index 0000000..774c58a
> --- /dev/null
> +++ b/test/cunit/buffer/odp_buffer_testsuites.h
> @@ -0,0 +1,30 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:    BSD-3-Clause
> + */
> +
> +#ifndef ODP_BUFFER_TESTSUITES_H_
> +#define ODP_BUFFER_TESTSUITES_H_
> +
> +#include <odp.h>
> +#include <odph_packet.h>
> +#include <CUnit/CUnit.h>
> +#include <CUnit/Basic.h>
> +
> +/* Helper macro for CU_TestInfo initialization */
> +#define _CU_TEST_INFO(test_func) {#test_func, test_func}
> +
> +extern CU_TestInfo buffer_pool_tests[];
> +extern CU_TestInfo buffer_tests[];
> +extern CU_TestInfo packet_tests[];
> +
> +extern int buffer_tests_init(void);
> +extern int buffer_tests_finalize(void);
> +
> +extern int packet_tests_init(void);
> +extern int packet_tests_finalize(void);
> +
> +odp_buffer_pool_t pool_create(int buf_num, int buf_size, int buf_type);
> +
> +#endif /* ODP_BUFFER_TESTSUITES_H_ */
> diff --git a/test/cunit/buffer/odp_packet_test.c 
> b/test/cunit/buffer/odp_packet_test.c
> new file mode 100644
> index 0000000..79753a9
> --- /dev/null
> +++ b/test/cunit/buffer/odp_packet_test.c
> @@ -0,0 +1,337 @@
> +/* Copyright (c) 2014, Linaro Limited
> + * All rights reserved.
> + *
> + * SPDX-License-Identifier:    BSD-3-Clause
> + */
> +
> +#include "odp_buffer_testsuites.h"
> +/* #define TEST_PACKET_SEGMENTS */
> +
> +static odp_buffer_pool_t packet_pool;
> +static const size_t packet_size = 1500;
> +odp_packet_t test_packet;
> +
> +int packet_tests_init(void)
> +{
> +       odp_buffer_pool_param_t params = {
> +                       .buf_size  = packet_size,
> +                       .buf_align = ODP_CACHE_LINE_SIZE,
> +                       .num_bufs  = 100,
> +                       .buf_type  = ODP_BUFFER_TYPE_PACKET,
> +       };
> +
> +       packet_pool = odp_buffer_pool_create("packet_pool", ODP_SHM_INVALID,
> +                                            &params);
> +       if (packet_pool == ODP_BUFFER_POOL_INVALID)
> +               return -1;
> +
> +       test_packet = odph_packet_alloc(packet_pool);
> +       if (odph_packet_is_valid(test_packet) == 0)
> +               return -1;
> +
> +       return 0;
> +}
> +
> +int packet_tests_finalize(void)
> +{
> +       odph_packet_free(test_packet);
> +       if (odp_buffer_pool_destroy(packet_pool) != 0)
> +               return -1;
> +       return 0;
> +}
> +
> +static void packet_alloc_free(void)
> +{
> +       odp_buffer_pool_t pool;
> +       odp_packet_t packet;
> +       pool = pool_create(1, 64, ODP_BUFFER_TYPE_PACKET);
> +
> +       /* Allocate the only buffer from the pool */
> +       packet = odph_packet_alloc(pool);
> +       CU_ASSERT_FATAL(packet != ODP_PACKET_INVALID);
> +
> +       /** @todo: is it correct to assume the pool had only one buffer? */
> +       CU_ASSERT_FATAL(odph_packet_alloc(pool) == ODP_PACKET_INVALID)
> +
> +       odph_packet_free(packet);
> +
> +       /* Check that the buffer was returned back to the pool */
> +       packet = odph_packet_alloc(pool);
> +       CU_ASSERT_FATAL(packet != ODP_PACKET_INVALID);
> +
> +       odph_packet_free(packet);
> +       CU_ASSERT(odp_buffer_pool_destroy(pool) == 0);
> +}
> +
> +static void packet_management_basic(void)
> +{
> +       CU_ASSERT(odph_packet_is_valid(test_packet) == 1);
> +       /** @todo: What is the expected packet length? */
> +       CU_ASSERT(odp_packet_addr(test_packet) != NULL);
> +       CU_ASSERT(odp_packet_data(test_packet) != NULL);
> +       odp_packet_print(test_packet);
> +}
> +
> +static void packet_length(void)
> +{
> +       CU_ASSERT(odp_packet_get_len(test_packet) == packet_size);
> +       odp_packet_set_len(test_packet, packet_size / 2);
> +       CU_ASSERT(odp_packet_get_len(test_packet) == packet_size / 2);
> +       odp_packet_init(test_packet);
> +       CU_ASSERT(odp_packet_get_len(test_packet) == packet_size);
> +}
> +
> +static void packet_context(void)
> +{
> +       char context = 2;
> +       void *prev_context;
> +
> +       prev_context = odp_packet_get_ctx(test_packet);
> +       odp_packet_set_ctx(test_packet, &context);
> +       CU_ASSERT(odp_packet_get_ctx(test_packet) == &context);
> +       odp_packet_set_ctx(test_packet, prev_context);
> +       odp_packet_init(test_packet);
> +}
> +
> +static void packet_buffer_conversion(void)
> +{
> +       odp_packet_t tmp_packet;
> +       odp_buffer_t test_buffer;
> +
> +       test_buffer = odp_packet_to_buffer(test_packet);
> +       CU_ASSERT_FATAL(test_buffer != ODP_BUFFER_INVALID);
> +       CU_ASSERT(odp_buffer_type(test_buffer) == ODP_BUFFER_TYPE_PACKET);
> +       CU_ASSERT(odp_buffer_size(test_buffer) >= packet_size);
> +
> +       tmp_packet = odp_packet_from_buffer(test_buffer);
> +       CU_ASSERT_FATAL(tmp_packet != ODP_PACKET_INVALID);
> +       /** @todo: Need an API to compare packets */
> +}
> +
> +static void packet_layer_offsets(void)
> +{
> +       odp_packet_t packet = test_packet;
> +       uint8_t *l2_addr, *l3_addr, *l4_addr;
> +       const size_t l2_off = 2;
> +       const size_t l3_off = l2_off + 14;
> +       const size_t l4_off = l3_off + 14;
> +       /* Set offsets to the same value */
> +       odp_packet_set_l2_offset(packet, l2_off);
> +       odp_packet_set_l3_offset(packet, l2_off);
> +       odp_packet_set_l4_offset(packet, l2_off);
> +
> +       /* Addresses should be the same */
> +       l2_addr = odp_packet_l2(packet);
> +       l3_addr = odp_packet_l3(packet);
> +       l4_addr = odp_packet_l4(packet);
> +       CU_ASSERT(l2_addr != NULL);
> +       CU_ASSERT(l2_addr == l3_addr);
> +       CU_ASSERT(l2_addr == l4_addr);
> +
> +       /* Set offsets to the different values */
> +       odp_packet_set_l2_offset(packet, l2_off);
> +       CU_ASSERT(odp_packet_l2_offset(packet) == l2_off);
> +       odp_packet_set_l3_offset(packet, l3_off);
> +       CU_ASSERT(odp_packet_l3_offset(packet) == l3_off);
> +       odp_packet_set_l4_offset(packet, l4_off);
> +       CU_ASSERT(odp_packet_l4_offset(packet) == l4_off);
> +
> +       /* Addresses should not be the same */
> +       l2_addr = odp_packet_l2(packet);
> +       CU_ASSERT(l2_addr != NULL);
> +       l3_addr = odp_packet_l3(packet);
> +       CU_ASSERT(l3_addr != NULL);
> +       l4_addr = odp_packet_l4(packet);
> +       CU_ASSERT(l4_addr != NULL);
> +
> +       CU_ASSERT(l2_addr != l3_addr);
> +       CU_ASSERT(l2_addr != l4_addr);
> +       CU_ASSERT(l3_addr != l4_addr);
> +}
> +
> +#ifdef TEST_PACKET_SEGMENTS
> +static void _test_one_segment(odp_packet_t packet,
> +                             odp_packet_seg_t segment,
> +                             odp_packet_seg_info_t *info)
> +{
> +       size_t headroom, tailroom;
> +
> +       CU_ASSERT(odp_packet_seg_info(packet, segment, info) == 0);
> +       CU_ASSERT(info->addr != NULL);
> +       CU_ASSERT(info->data != NULL);
> +       CU_ASSERT(info->data >= info->addr);
> +       CU_ASSERT(info->size != 0);
> +
> +       CU_ASSERT(odp_packet_seg_addr(packet, segment) == info->addr);
> +       CU_ASSERT(odp_packet_seg_size(packet, segment) == info->size);
> +       CU_ASSERT(odp_packet_seg_data(packet, segment) == info->data);
> +       CU_ASSERT(odp_packet_seg_data_len(packet, segment) == info->data_len);
> +
> +       headroom = odp_packet_seg_headroom(packet, segment);
> +       tailroom = odp_packet_seg_tailroom(packet, segment);
> +       CU_ASSERT(info->size ==
> +                       info->data_len + headroom + tailroom);
> +}
> +
> +static void packet_segments(void)
> +{
> +       int segment_count, seg_index;
> +       size_t data_size, buf_size;
> +       odp_packet_seg_t segment;
> +       odp_packet_t packet = test_packet;
> +       odp_packet_seg_info_t seg_info;
> +
> +       CU_ASSERT(odph_packet_is_valid(packet) == 1);
> +
> +       segment_count = odp_packet_seg_count(packet);
> +       CU_ASSERT(segment_count != 0);
> +
> +       if (!odp_packet_is_segmented(packet)) {
> +               CU_ASSERT(segment_count == 1);
> +       }
> +
> +       segment = ODP_PACKET_SEG_INVALID;
> +       buf_size = 0;
> +       data_size = 0;
> +       for (seg_index = 0; seg_index < segment_count; seg_index++) {
> +               segment = odp_packet_seg(packet, seg_index);
> +               if (segment == ODP_PACKET_SEG_INVALID)
> +                       break;
> +               _test_one_segment(packet, segment, &seg_info);
> +
> +               buf_size += seg_info.size;
> +               data_size += seg_info.data_len;
> +               /** @todo: touch memory in a segment */
> +       }
> +
> +       CU_ASSERT(seg_index == segment_count);
> +       CU_ASSERT(buf_size == odp_buffer_size(odp_packet_to_buffer(packet)));
> +       CU_ASSERT(data_size == odp_packet_get_len(packet));
> +
> +       segment = ODP_PACKET_SEG_INVALID;
> +       buf_size = 0;
> +       data_size = 0;
> +       for (seg_index = 0; seg_index < segment_count; seg_index++) {
> +               segment = odp_packet_seg_next(packet, segment);
> +
> +               if (segment == ODP_PACKET_SEG_INVALID)
> +                       break;
> +               _test_one_segment(packet, segment, &seg_info);
> +
> +               buf_size += seg_info.size;
> +               data_size += seg_info.data_len;
> +               /** @todo: touch memory in a segment */
> +       }
> +
> +       CU_ASSERT(seg_index == segment_count);
> +       CU_ASSERT(buf_size == odp_buffer_size(odp_packet_to_buffer(packet)));
> +       CU_ASSERT(data_size == odp_packet_get_len(packet));
> +
> +       if (seg_index == segment_count) {
> +               segment = odp_packet_seg_next(packet, segment);
> +               CU_ASSERT(segment == ODP_PACKET_SEG_INVALID);
> +       }
> +}
> +
> +static void _verify_headroom_shift(odp_packet_t packet,
> +                                  odp_packet_seg_t segment,
> +                                  int shift)
> +{
> +       size_t room = odp_packet_seg_headroom(packet, segment);
> +       size_t data_len = odp_packet_seg_data_len(packet, segment);
> +       char *data = odp_packet_seg_data(packet, segment);
> +       void *addr;
> +
> +       if (shift > 0)
> +               addr = odp_packet_seg_push_head(packet, segment, shift);
> +       else
> +               addr = odp_packet_seg_pull_head(packet, segment, -shift);
> +
> +       CU_ASSERT(addr != NULL);
> +       CU_ASSERT(odp_packet_seg_headroom(packet, segment) == room - shift);
> +       CU_ASSERT(odp_packet_seg_data_len(packet, segment) == data_len + 
> shift);
> +       CU_ASSERT(odp_packet_seg_data(packet, segment) == addr);
> +       CU_ASSERT(addr == data + shift);
> +}
> +
> +static void _verify_tailroom_shift(odp_packet_t packet,
> +                                  odp_packet_seg_t segment,
> +                                  int shift)
> +{
> +       size_t room = odp_packet_seg_tailroom(packet, segment);
> +       int len;
> +       size_t data_len = odp_packet_seg_data_len(packet, segment);
> +
> +       if (shift > 0)
> +               len = odp_packet_seg_push_tail(packet, segment, shift);
> +       else
> +               len = odp_packet_seg_pull_tail(packet, segment, -shift);
> +
> +       CU_ASSERT(len != -1);
> +       CU_ASSERT(odp_packet_seg_tailroom(packet, segment) == room - shift);
> +       /** @todo: tail push/pull return int, but data_len is size_t */
> +       CU_ASSERT(len == (int)data_len + shift);
> +       CU_ASSERT((int)odp_packet_seg_data_len(packet, segment) == len);
> +}
> +
> +static void packet_segment_headroom(void)
> +{
> +       odp_packet_seg_t segment;
> +       odp_packet_t packet = test_packet;
> +       size_t room;
> +       size_t data_len;
> +       int shift = 1;
> +
> +       segment = odp_packet_seg(packet, 0);
> +       CU_ASSERT_FATAL(segment != ODP_PACKET_SEG_INVALID);
> +       room = odp_packet_seg_headroom(packet, segment);
> +       data_len = odp_packet_seg_data_len(packet, segment);
> +
> +       /** @todo: Specification doesn't specify what room size to expect */
> +       if (room == 0 && data_len > 0)
> +               shift = -shift;
> +       else if (room == 0)
> +               CU_FAIL_FATAL("No space for headroom manipulation");
> +
> +       _verify_headroom_shift(packet, segment,  shift);
> +       _verify_headroom_shift(packet, segment, -shift);
> +}
> +
> +static void packet_segment_tailroom(void)
> +{
> +       odp_packet_seg_t segment;
> +       odp_packet_t packet = test_packet;
> +       size_t room;
> +       size_t data_len;
> +       int shift = 1;
> +
> +       segment = odp_packet_seg(test_packet, 0);
> +       CU_ASSERT_FATAL(segment != ODP_PACKET_SEG_INVALID);
> +       room = odp_packet_seg_tailroom(packet, segment);
> +       data_len = odp_packet_seg_data_len(packet, segment);
> +
> +       /** @todo: Specification doesn't specify what room size to expect */
> +       if (room == 0 && data_len > 0)
> +               shift = -shift;
> +       else if (room == 0)
> +               CU_FAIL_FATAL("No space for tailroom manipulation");
> +
> +       _verify_tailroom_shift(packet, segment,  shift);
> +       _verify_tailroom_shift(packet, segment, -shift);
> +}
> +#endif /* TEST_PACKET_SEGMENTS */
> +
> +CU_TestInfo packet_tests[] = {
> +       _CU_TEST_INFO(packet_alloc_free),
> +       _CU_TEST_INFO(packet_management_basic),
> +       _CU_TEST_INFO(packet_length),
> +       _CU_TEST_INFO(packet_context),
> +       _CU_TEST_INFO(packet_buffer_conversion),
> +       _CU_TEST_INFO(packet_layer_offsets),
> +#ifdef TEST_PACKET_SEGMENTS
> +       _CU_TEST_INFO(packet_segments),
> +       _CU_TEST_INFO(packet_segment_headroom),
> +       _CU_TEST_INFO(packet_segment_tailroom),
> +#endif /* TEST_PACKET_SEGMENTS */
> +       CU_TEST_INFO_NULL,
> +};
> --
> 1.7.9.5
>
>
> _______________________________________________
> lng-odp mailing list
> [email protected]
> http://lists.linaro.org/mailman/listinfo/lng-odp

_______________________________________________
lng-odp mailing list
[email protected]
http://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to