From 7eb8cf2515e114f8ff04fb051288acdfe1b8e65a Mon Sep 17 00:00:00 2001 From: Ningx Zhao Date: Wed, 1 Jul 2020 18:10:07 +0800 Subject: [PATCH] tests: updata the double list testcase Add some testcases to: Verify an user defined structure contains dlist node works, Verify dlist "for each" style APIs work. Signed-off-by: Ningx Zhao --- .../dlist_perf/CMakeLists.txt | 8 + .../data_structure_perf/dlist_perf/prj.conf | 1 + .../dlist_perf/src/dlist_perf.c | 184 ++++++++++++++++++ .../dlist_perf/testcase.yaml | 3 + 4 files changed, 196 insertions(+) create mode 100644 tests/benchmarks/data_structure_perf/dlist_perf/CMakeLists.txt create mode 100644 tests/benchmarks/data_structure_perf/dlist_perf/prj.conf create mode 100644 tests/benchmarks/data_structure_perf/dlist_perf/src/dlist_perf.c create mode 100644 tests/benchmarks/data_structure_perf/dlist_perf/testcase.yaml diff --git a/tests/benchmarks/data_structure_perf/dlist_perf/CMakeLists.txt b/tests/benchmarks/data_structure_perf/dlist_perf/CMakeLists.txt new file mode 100644 index 00000000000..1ebbf4df517 --- /dev/null +++ b/tests/benchmarks/data_structure_perf/dlist_perf/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(dlist) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/tests/benchmarks/data_structure_perf/dlist_perf/prj.conf b/tests/benchmarks/data_structure_perf/dlist_perf/prj.conf new file mode 100644 index 00000000000..9467c292689 --- /dev/null +++ b/tests/benchmarks/data_structure_perf/dlist_perf/prj.conf @@ -0,0 +1 @@ +CONFIG_ZTEST=y diff --git a/tests/benchmarks/data_structure_perf/dlist_perf/src/dlist_perf.c b/tests/benchmarks/data_structure_perf/dlist_perf/src/dlist_perf.c new file mode 100644 index 00000000000..bdc555aa04b --- /dev/null +++ b/tests/benchmarks/data_structure_perf/dlist_perf/src/dlist_perf.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2018 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +/** + * @brief doubly-linked list tests + * + * @defgroup lib_dlist_tests Dlist + */ + +#include +#include + +#define NODE_SIZE 5 + +static sys_dlist_t test_list; + +struct container_node { + sys_dnode_t node; + int value; +}; + +/** + * @brief Test whether dlist node struct is embeddedable + * in any user structure + * + * @details Initialize an user defined structure contains + * dlist node.Appending nodes into the doubly-linked list + * and enumerate the doubly-linked list. + * + * Verify that the value enumerated is equal to the + * value initialized.If the verification pass,the user + * defined structure works. + * + * @ingroup lib_dlist_tests + * + * @see sys_dlist_append() + */ + +void test_dlist_container(void) +{ + int i, count; + struct container_node *cnode, *s_cnode; + + /* Initialize an user-defiend structure of contains dlist node */ + struct container_node data_node[NODE_SIZE] = { + { .value = 0 }, + { .value = 1 }, + { .value = 2 }, + { .value = 3 }, + { .value = 4 } + }; + + sys_dlist_init(&test_list); + zassert_true(sys_dlist_is_empty(&test_list), + "sys_dlist_init failed"); + + /* Add into a doubly-linked list */ + for (i = 0; i < NODE_SIZE; i++) { + sys_dlist_append(&test_list, &data_node[i].node); + } + + /* Enumerate the doubly-linked list */ + count = 0; + SYS_DLIST_FOR_EACH_CONTAINER(&test_list, cnode, node) { + zassert_true(cnode->value == count, + "SYS_DLIST_FOR_EACH_CONTAINER failed"); + count++; + } + zassert_true(count == NODE_SIZE, + "SYS_DLIST_FOR_EACH_CONTAINER failed " + "expected %d get %d", NODE_SIZE, count); + + /* Enumerate the doubly-linked list */ + count = 0; + SYS_DLIST_FOR_EACH_CONTAINER_SAFE(&test_list, cnode, s_cnode, node) { + zassert_true(cnode->value == count, + "SYS_DLIST_FOR_EACH_CONTAINER_SAFE failed"); + count++; + } + zassert_true(count == NODE_SIZE, + "SYS_DLIST_FOR_EACH_CONTAINER_SAFE failed"); +} + +/** + * @brief Test dlist for each function + * + * @details Initialize a double-linked list. + * Appending nodes into the doubly-linked list + * and enumerate the doubly-linked list. + * + * Verify that the value enumerated is equal to the + * value initialized.If the verification pass,the "for each" + * style API work. + * + * @ingroup lib_dlist_tests + * + * @see SYS_DLIST_FOR_EACH_NODE(),SYS_DLIST_FOR_EACH_NODE_SAFE() + * SYS_DLIST_ITERATE_FROM_NODE() + */ + +void test_dlist_for_each(void) +{ + int i, count, val; + sys_dnode_t *node, *s_node; + + /* Initialize a doubly-linked list */ + struct container_node data_node[NODE_SIZE] = { + { .value = 0 }, + { .value = 1 }, + { .value = 2 }, + { .value = 3 }, + { .value = 4 } + }; + + sys_dlist_init(&test_list); + zassert_true(sys_dlist_is_empty(&test_list), + "sys_dlist_init failed"); + + for (i = 0; i < NODE_SIZE; i++) { + sys_dlist_append(&test_list, &data_node[i].node); + } + + /* Enumerate the doubly-linked list */ + count = 0; + SYS_DLIST_FOR_EACH_NODE(&test_list, node) { + val = CONTAINER_OF(node, struct container_node, node)->value; + zassert_true(val == count, "SYS_DLIST_FOR_EACH_NODE failed" + " expected %d get %d", count, val); + count++; + } + zassert_true(count == NODE_SIZE, + "SYS_DLIST_FOR_EACH_NODE failed"); + + /* Enumerate the doubly-linked list */ + count = 0; + SYS_DLIST_FOR_EACH_NODE_SAFE(&test_list, node, s_node) { + val = CONTAINER_OF(node, struct container_node, node)->value; + zassert_true(val == count, "SYS_DLIST_FOR_EACH_NODE_SAFE failed" + " expected %d get %d", count, val); + count++; + } + zassert_true(count == NODE_SIZE, + "SYS_DLIST_FOR_EACH_NODE_SAFE failed"); + + /* Enumerate the doubly-linked list */ + count = 0; + SYS_DLIST_ITERATE_FROM_NODE(&test_list, node) { + count++; + val = ((struct container_node *)node)->value; + if (val == 1) { + break; + } + } + zassert_true(count == 2, "SYS_DLIST_ITERATE_FROM_NODE failed"); + + count = 0; + SYS_DLIST_ITERATE_FROM_NODE(&test_list, node) { + count++; + val = ((struct container_node *)node)->value; + if (val == 2) { + break; + } + } + zassert_true(count == 1, "SYS_DLIST_ITERATE_FROM_NODE failed"); + + count = 0; + SYS_DLIST_ITERATE_FROM_NODE(&test_list, node) { + count++; + } + zassert_true(count == 2, "SYS_DLIST_ITERATE_FROM_NODE failed"); +} + +/* ztest main entry */ +void test_main(void) +{ + ztest_test_suite(test_dlist, + ztest_unit_test(test_dlist_container), + ztest_unit_test(test_dlist_for_each) + ); + ztest_run_test_suite(test_dlist); +} diff --git a/tests/benchmarks/data_structure_perf/dlist_perf/testcase.yaml b/tests/benchmarks/data_structure_perf/dlist_perf/testcase.yaml new file mode 100644 index 00000000000..91309ffe690 --- /dev/null +++ b/tests/benchmarks/data_structure_perf/dlist_perf/testcase.yaml @@ -0,0 +1,3 @@ +tests: + benchmark.data_structures: + tags: dlist req-test