1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include <fwk_assert.h>
9 #include <fwk_list.h>
10 #include <fwk_macros.h>
11 #include <fwk_test.h>
12 
13 #include <string.h>
14 
15 static struct fwk_dlist list;
16 static struct fwk_dlist_node node[3];
17 
setup(void)18 static void setup(void)
19 {
20     fwk_list_init(&list);
21 
22     /* Remove node links before each test case */
23     memset(node, 0, sizeof(node));
24 }
25 
test_list_insert(void)26 static void test_list_insert(void)
27 {
28     fwk_list_push_tail(&list, &node[0]);
29     fwk_list_push_tail(&list, &node[2]);
30 
31     fwk_list_insert(&list, &node[1], &node[2]);
32 
33     assert(node[0].next == &node[1]);
34     assert(node[1].next == &node[2]);
35     assert(node[2].next == (struct fwk_dlist_node *)&list);
36 
37     assert(node[2].prev == &node[1]);
38     assert(node[1].prev == &node[0]);
39     assert(node[0].prev == (struct fwk_dlist_node *)&list);
40 
41     assert(list.head == &node[0]);
42     assert(list.tail == &node[2]);
43 }
44 
test_list_insert_empty(void)45 static void test_list_insert_empty(void)
46 {
47     fwk_list_insert(&list, &node[0], NULL);
48 
49     assert(list.head == &node[0]);
50     assert(list.tail == &node[0]);
51 
52     assert(node[0].next == (struct fwk_dlist_node *)&list);
53     assert(node[0].prev == (struct fwk_dlist_node *)&list);
54 }
55 
test_list_insert_tail_implicit(void)56 static void test_list_insert_tail_implicit(void)
57 {
58     fwk_list_insert(&list, &node[0], NULL);
59 
60     assert(list.head == &node[0]);
61     assert(list.tail == &node[0]);
62 
63     fwk_list_insert(&list, &node[1], NULL);
64 
65     assert(list.head == &node[0]);
66     assert(list.tail == &node[1]);
67 }
68 
69 static const struct fwk_test_case_desc tests[] = {
70     FWK_TEST_CASE(test_list_insert),
71     FWK_TEST_CASE(test_list_insert_empty),
72     FWK_TEST_CASE(test_list_insert_tail_implicit),
73 };
74 
75 struct fwk_test_suite_desc test_suite = {
76     .name = "fwk_list_insert",
77     .test_case_setup = setup,
78     .test_case_table = tests,
79     .test_case_count = FWK_ARRAY_SIZE(tests),
80 };
81