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