1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2018-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 <stddef.h>
14 #include <string.h>
15 
16 static struct fwk_slist slist;
17 static struct fwk_dlist dlist;
18 
19 static struct fwk_slist_node snodes[3];
20 static struct fwk_dlist_node dnodes[3];
21 
test_case_setup(void)22 static void test_case_setup(void)
23 {
24     fwk_list_init(&slist);
25     fwk_list_init(&dlist);
26 
27     /* Remove node links before each test case */
28     memset(dnodes, 0, sizeof(dnodes));
29     memset(snodes, 0, sizeof(snodes));
30 }
31 
test_slist_remove_on_one(void)32 static void test_slist_remove_on_one(void)
33 {
34     fwk_list_push_tail(&slist, &snodes[0]);
35 
36     fwk_list_remove(&slist, &snodes[0]);
37     assert(slist.head == (struct fwk_slist_node *)&slist);
38     assert(slist.tail == (struct fwk_slist_node *)&slist);
39 }
40 
test_slist_remove_first_of_two(void)41 static void test_slist_remove_first_of_two(void)
42 {
43     fwk_list_push_tail(&slist, &snodes[0]);
44     fwk_list_push_tail(&slist, &snodes[1]);
45 
46     fwk_list_remove(&slist, &snodes[0]);
47     assert(slist.head == &snodes[1]);
48     assert(slist.tail == &snodes[1]);
49     assert(snodes[1].next == (struct fwk_slist_node *)&slist);
50 }
51 
test_slist_remove_second_of_two(void)52 static void test_slist_remove_second_of_two(void)
53 {
54     fwk_list_push_tail(&slist, &snodes[0]);
55     fwk_list_push_tail(&slist, &snodes[1]);
56 
57     fwk_list_remove(&slist, &snodes[1]);
58     assert(slist.head == &snodes[0]);
59     assert(slist.tail == &snodes[0]);
60     assert(snodes[0].next == (struct fwk_slist_node *)&slist);
61 }
62 
test_slist_remove_second_of_three(void)63 static void test_slist_remove_second_of_three(void)
64 {
65     fwk_list_push_tail(&slist, &snodes[0]);
66     fwk_list_push_tail(&slist, &snodes[1]);
67     fwk_list_push_tail(&slist, &snodes[2]);
68 
69     fwk_list_remove(&slist, &snodes[1]);
70     assert(slist.head == &snodes[0]);
71     assert(slist.tail == &snodes[2]);
72     assert(snodes[0].next == &snodes[2]);
73     assert(snodes[2].next == (struct fwk_slist_node *)&slist);
74 }
test_dlist_remove_on_one(void)75 static void test_dlist_remove_on_one(void)
76 {
77     fwk_list_push_tail(&dlist, &dnodes[0]);
78 
79     fwk_list_remove(&dlist, &dnodes[0]);
80     assert(dlist.head == (struct fwk_dlist_node *)&dlist);
81     assert(dlist.tail == (struct fwk_dlist_node *)&dlist);
82 }
83 
test_dlist_remove_first_of_two(void)84 static void test_dlist_remove_first_of_two(void)
85 {
86     fwk_list_push_tail(&dlist, &dnodes[0]);
87     fwk_list_push_tail(&dlist, &dnodes[1]);
88 
89     fwk_list_remove(&dlist, &dnodes[0]);
90     assert(dlist.head == &dnodes[1]);
91     assert(dlist.tail == &dnodes[1]);
92     assert(dnodes[1].next == (struct fwk_dlist_node *)&dlist);
93     assert(dnodes[1].prev == (struct fwk_dlist_node *)&dlist);
94 }
95 
test_dlist_remove_second_of_two(void)96 static void test_dlist_remove_second_of_two(void)
97 {
98     fwk_list_push_tail(&dlist, &dnodes[0]);
99     fwk_list_push_tail(&dlist, &dnodes[1]);
100 
101     fwk_list_remove(&dlist, &dnodes[1]);
102     assert(dlist.head == &dnodes[0]);
103     assert(dlist.tail == &dnodes[0]);
104     assert(dnodes[0].next == (struct fwk_dlist_node *)&dlist);
105     assert(dnodes[0].prev == (struct fwk_dlist_node *)&dlist);
106 }
107 
test_dlist_remove_second_of_three(void)108 static void test_dlist_remove_second_of_three(void)
109 {
110     fwk_list_push_tail(&dlist, &dnodes[0]);
111     fwk_list_push_tail(&dlist, &dnodes[1]);
112     fwk_list_push_tail(&dlist, &dnodes[2]);
113 
114     fwk_list_remove(&dlist, &dnodes[1]);
115     assert(dlist.head == &dnodes[0]);
116     assert(dlist.tail == &dnodes[2]);
117     assert(dnodes[0].next == &dnodes[2]);
118     assert(dnodes[0].prev == (struct fwk_dlist_node *)&dlist);
119     assert(dnodes[2].next == (struct fwk_dlist_node *)&dlist);
120     assert(dnodes[2].prev == &dnodes[0]);
121 }
122 
123 static const struct fwk_test_case_desc test_case_table[] = {
124     FWK_TEST_CASE(test_slist_remove_on_one),
125     FWK_TEST_CASE(test_slist_remove_first_of_two),
126     FWK_TEST_CASE(test_slist_remove_second_of_two),
127     FWK_TEST_CASE(test_slist_remove_second_of_three),
128     FWK_TEST_CASE(test_dlist_remove_on_one),
129     FWK_TEST_CASE(test_dlist_remove_first_of_two),
130     FWK_TEST_CASE(test_dlist_remove_second_of_two),
131     FWK_TEST_CASE(test_dlist_remove_second_of_three),
132 };
133 
134 struct fwk_test_suite_desc test_suite = {
135     .name = "fwk_list_remove",
136     .test_case_setup = test_case_setup,
137     .test_case_count = FWK_ARRAY_SIZE(test_case_table),
138     .test_case_table = test_case_table,
139 };
140