]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
3b18ae68 | 2 | |
d327b775 | 3 | #include "alloc-util.h" |
3b18ae68 | 4 | #include "list.h" |
965040d8 | 5 | #include "tests.h" |
3b18ae68 JJ |
6 | |
7 | int main(int argc, const char *argv[]) { | |
965040d8 YW |
8 | test_setup_logging(LOG_DEBUG); |
9 | ||
3b18ae68 JJ |
10 | size_t i; |
11 | typedef struct list_item { | |
c4f2bff1 | 12 | LIST_FIELDS(struct list_item, item_list); |
3b18ae68 JJ |
13 | } list_item; |
14 | LIST_HEAD(list_item, head); | |
5511d8c1 | 15 | LIST_HEAD(list_item, head2); |
3b18ae68 | 16 | list_item items[4]; |
3b18ae68 | 17 | |
71fda00f | 18 | LIST_HEAD_INIT(head); |
5511d8c1 | 19 | LIST_HEAD_INIT(head2); |
5152b845 IK |
20 | ASSERT_NULL(head); |
21 | ASSERT_NULL(head2); | |
3b18ae68 JJ |
22 | |
23 | for (i = 0; i < ELEMENTSOF(items); i++) { | |
c4f2bff1 ZJS |
24 | LIST_INIT(item_list, &items[i]); |
25 | assert_se(LIST_JUST_US(item_list, &items[i])); | |
cc232fa0 | 26 | assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); |
3b18ae68 JJ |
27 | } |
28 | ||
7663df37 | 29 | i = 0; |
c4f2bff1 | 30 | LIST_FOREACH_OTHERS(item_list, cursor, &items[2]) { |
7663df37 LP |
31 | i++; |
32 | assert_se(cursor != &items[2]); | |
33 | } | |
34 | assert_se(i == ELEMENTSOF(items)-1); | |
35 | ||
3ad85e37 | 36 | i = 0; |
c4f2bff1 | 37 | LIST_FOREACH_OTHERS(item_list, cursor, &items[0]) { |
3ad85e37 LP |
38 | i++; |
39 | assert_se(cursor != &items[0]); | |
40 | } | |
41 | assert_se(i == ELEMENTSOF(items)-1); | |
42 | ||
43 | i = 0; | |
c4f2bff1 | 44 | LIST_FOREACH_OTHERS(item_list, cursor, &items[3]) { |
3ad85e37 LP |
45 | i++; |
46 | assert_se(cursor != &items[3]); | |
47 | } | |
48 | assert_se(i == ELEMENTSOF(items)-1); | |
49 | ||
c4f2bff1 | 50 | assert_se(!LIST_JUST_US(item_list, head)); |
3b18ae68 | 51 | |
5152b845 | 52 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
53 | assert_se(items[1].item_list_next == &items[0]); |
54 | assert_se(items[2].item_list_next == &items[1]); | |
55 | assert_se(items[3].item_list_next == &items[2]); | |
3b18ae68 | 56 | |
c4f2bff1 ZJS |
57 | assert_se(items[0].item_list_prev == &items[1]); |
58 | assert_se(items[1].item_list_prev == &items[2]); | |
59 | assert_se(items[2].item_list_prev == &items[3]); | |
5152b845 | 60 | ASSERT_NULL(items[3].item_list_prev); |
3b18ae68 | 61 | |
cc232fa0 | 62 | list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]); |
3b18ae68 JJ |
63 | assert_se(cursor == &items[3]); |
64 | ||
cc232fa0 | 65 | cursor = LIST_FIND_TAIL(item_list, &items[3]); |
3b18ae68 JJ |
66 | assert_se(cursor == &items[0]); |
67 | ||
cc232fa0 | 68 | assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
c4f2bff1 ZJS |
69 | assert_se(LIST_JUST_US(item_list, &items[1])); |
70 | ||
5152b845 | 71 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
72 | assert_se(items[2].item_list_next == &items[0]); |
73 | assert_se(items[3].item_list_next == &items[2]); | |
74 | ||
75 | assert_se(items[0].item_list_prev == &items[2]); | |
76 | assert_se(items[2].item_list_prev == &items[3]); | |
5152b845 | 77 | ASSERT_NULL(items[3].item_list_prev); |
c4f2bff1 | 78 | |
cc232fa0 | 79 | assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &items[1]); |
5152b845 | 80 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
81 | assert_se(items[2].item_list_next == &items[0]); |
82 | assert_se(items[1].item_list_next == &items[2]); | |
83 | assert_se(items[3].item_list_next == &items[1]); | |
84 | ||
85 | assert_se(items[0].item_list_prev == &items[2]); | |
86 | assert_se(items[2].item_list_prev == &items[1]); | |
87 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 88 | ASSERT_NULL(items[3].item_list_prev); |
c4f2bff1 | 89 | |
cc232fa0 | 90 | assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
c4f2bff1 ZJS |
91 | assert_se(LIST_JUST_US(item_list, &items[1])); |
92 | ||
5152b845 | 93 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
94 | assert_se(items[2].item_list_next == &items[0]); |
95 | assert_se(items[3].item_list_next == &items[2]); | |
96 | ||
97 | assert_se(items[0].item_list_prev == &items[2]); | |
98 | assert_se(items[2].item_list_prev == &items[3]); | |
5152b845 | 99 | ASSERT_NULL(items[3].item_list_prev); |
c4f2bff1 | 100 | |
cc232fa0 | 101 | assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &items[1]); |
5152b845 | 102 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
103 | assert_se(items[2].item_list_next == &items[0]); |
104 | assert_se(items[1].item_list_next == &items[2]); | |
105 | assert_se(items[3].item_list_next == &items[1]); | |
106 | ||
107 | assert_se(items[0].item_list_prev == &items[2]); | |
108 | assert_se(items[2].item_list_prev == &items[1]); | |
109 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 110 | ASSERT_NULL(items[3].item_list_prev); |
c4f2bff1 | 111 | |
cc232fa0 | 112 | assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
c4f2bff1 ZJS |
113 | assert_se(LIST_JUST_US(item_list, &items[0])); |
114 | ||
5152b845 | 115 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 ZJS |
116 | assert_se(items[1].item_list_next == &items[2]); |
117 | assert_se(items[3].item_list_next == &items[1]); | |
118 | ||
119 | assert_se(items[2].item_list_prev == &items[1]); | |
120 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 121 | ASSERT_NULL(items[3].item_list_prev); |
c4f2bff1 | 122 | |
cc232fa0 | 123 | assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &items[0]); |
5152b845 | 124 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 ZJS |
125 | assert_se(items[1].item_list_next == &items[2]); |
126 | assert_se(items[3].item_list_next == &items[1]); | |
127 | assert_se(items[0].item_list_next == &items[3]); | |
128 | ||
129 | assert_se(items[2].item_list_prev == &items[1]); | |
130 | assert_se(items[1].item_list_prev == &items[3]); | |
131 | assert_se(items[3].item_list_prev == &items[0]); | |
5152b845 | 132 | ASSERT_NULL(items[0].item_list_prev); |
5076f421 MO |
133 | assert_se(head == &items[0]); |
134 | ||
cc232fa0 | 135 | assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
c4f2bff1 | 136 | assert_se(LIST_JUST_US(item_list, &items[0])); |
5076f421 | 137 | |
5152b845 | 138 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 ZJS |
139 | assert_se(items[1].item_list_next == &items[2]); |
140 | assert_se(items[3].item_list_next == &items[1]); | |
5076f421 | 141 | |
c4f2bff1 ZJS |
142 | assert_se(items[2].item_list_prev == &items[1]); |
143 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 144 | ASSERT_NULL(items[3].item_list_prev); |
5076f421 | 145 | |
cc232fa0 | 146 | assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &items[0]); |
5152b845 | 147 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
148 | assert_se(items[2].item_list_next == &items[0]); |
149 | assert_se(items[1].item_list_next == &items[2]); | |
150 | assert_se(items[3].item_list_next == &items[1]); | |
3b18ae68 | 151 | |
c4f2bff1 ZJS |
152 | assert_se(items[0].item_list_prev == &items[2]); |
153 | assert_se(items[2].item_list_prev == &items[1]); | |
154 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 155 | ASSERT_NULL(items[3].item_list_prev); |
3b18ae68 | 156 | |
cc232fa0 | 157 | assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
c4f2bff1 | 158 | assert_se(LIST_JUST_US(item_list, &items[0])); |
3b18ae68 | 159 | |
5152b845 | 160 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 ZJS |
161 | assert_se(items[1].item_list_next == &items[2]); |
162 | assert_se(items[3].item_list_next == &items[1]); | |
3b18ae68 | 163 | |
c4f2bff1 ZJS |
164 | assert_se(items[2].item_list_prev == &items[1]); |
165 | assert_se(items[1].item_list_prev == &items[3]); | |
5152b845 | 166 | ASSERT_NULL(items[3].item_list_prev); |
3b18ae68 | 167 | |
cc232fa0 | 168 | assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
c4f2bff1 | 169 | assert_se(LIST_JUST_US(item_list, &items[1])); |
3b18ae68 | 170 | |
5152b845 | 171 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 | 172 | assert_se(items[3].item_list_next == &items[2]); |
3b18ae68 | 173 | |
c4f2bff1 | 174 | assert_se(items[2].item_list_prev == &items[3]); |
5152b845 | 175 | ASSERT_NULL(items[3].item_list_prev); |
3b18ae68 | 176 | |
cc232fa0 | 177 | assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]); |
c4f2bff1 ZJS |
178 | assert_se(LIST_JUST_US(item_list, &items[2])); |
179 | assert_se(LIST_JUST_US(item_list, head)); | |
3b18ae68 | 180 | |
cc232fa0 | 181 | assert_se(LIST_REMOVE(item_list, head, &items[3]) == &items[3]); |
c4f2bff1 | 182 | assert_se(LIST_JUST_US(item_list, &items[3])); |
3b18ae68 | 183 | |
5152b845 | 184 | ASSERT_NULL(head); |
502f1733 ZJS |
185 | |
186 | for (i = 0; i < ELEMENTSOF(items); i++) { | |
c4f2bff1 | 187 | assert_se(LIST_JUST_US(item_list, &items[i])); |
cc232fa0 | 188 | assert_se(LIST_APPEND(item_list, head, &items[i]) == &items[i]); |
502f1733 ZJS |
189 | } |
190 | ||
c4f2bff1 | 191 | assert_se(!LIST_JUST_US(item_list, head)); |
502f1733 | 192 | |
c4f2bff1 ZJS |
193 | assert_se(items[0].item_list_next == &items[1]); |
194 | assert_se(items[1].item_list_next == &items[2]); | |
195 | assert_se(items[2].item_list_next == &items[3]); | |
5152b845 | 196 | ASSERT_NULL(items[3].item_list_next); |
502f1733 | 197 | |
5152b845 | 198 | ASSERT_NULL(items[0].item_list_prev); |
c4f2bff1 ZJS |
199 | assert_se(items[1].item_list_prev == &items[0]); |
200 | assert_se(items[2].item_list_prev == &items[1]); | |
201 | assert_se(items[3].item_list_prev == &items[2]); | |
502f1733 ZJS |
202 | |
203 | for (i = 0; i < ELEMENTSOF(items); i++) | |
cc232fa0 | 204 | assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); |
502f1733 | 205 | |
5152b845 | 206 | ASSERT_NULL(head); |
502f1733 | 207 | |
5511d8c1 | 208 | for (i = 0; i < ELEMENTSOF(items) / 2; i++) { |
c4f2bff1 ZJS |
209 | LIST_INIT(item_list, &items[i]); |
210 | assert_se(LIST_JUST_US(item_list, &items[i])); | |
cc232fa0 | 211 | assert_se(LIST_PREPEND(item_list, head, &items[i]) == &items[i]); |
5511d8c1 LB |
212 | } |
213 | ||
214 | for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) { | |
c4f2bff1 ZJS |
215 | LIST_INIT(item_list, &items[i]); |
216 | assert_se(LIST_JUST_US(item_list, &items[i])); | |
cc232fa0 | 217 | assert_se(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]); |
5511d8c1 LB |
218 | } |
219 | ||
5152b845 | 220 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 | 221 | assert_se(items[1].item_list_next == &items[0]); |
5152b845 | 222 | ASSERT_NULL(items[2].item_list_next); |
c4f2bff1 | 223 | assert_se(items[3].item_list_next == &items[2]); |
5511d8c1 | 224 | |
c4f2bff1 | 225 | assert_se(items[0].item_list_prev == &items[1]); |
5152b845 | 226 | ASSERT_NULL(items[1].item_list_prev); |
c4f2bff1 | 227 | assert_se(items[2].item_list_prev == &items[3]); |
5152b845 | 228 | ASSERT_NULL(items[3].item_list_prev); |
5511d8c1 | 229 | |
cc232fa0 | 230 | assert_se(LIST_JOIN(item_list, head2, head) == head2); |
5152b845 | 231 | ASSERT_NULL(head); |
5511d8c1 | 232 | |
5152b845 | 233 | ASSERT_NULL(items[0].item_list_next); |
c4f2bff1 ZJS |
234 | assert_se(items[1].item_list_next == &items[0]); |
235 | assert_se(items[2].item_list_next == &items[1]); | |
236 | assert_se(items[3].item_list_next == &items[2]); | |
5511d8c1 | 237 | |
c4f2bff1 ZJS |
238 | assert_se(items[0].item_list_prev == &items[1]); |
239 | assert_se(items[1].item_list_prev == &items[2]); | |
240 | assert_se(items[2].item_list_prev == &items[3]); | |
5152b845 | 241 | ASSERT_NULL(items[3].item_list_prev); |
5511d8c1 | 242 | |
cc232fa0 | 243 | assert_se(LIST_JOIN(item_list, head, head2) == head); |
5152b845 | 244 | ASSERT_NULL(head2); |
64903d18 | 245 | assert_se(head); |
5511d8c1 LB |
246 | |
247 | for (i = 0; i < ELEMENTSOF(items); i++) | |
cc232fa0 | 248 | assert_se(LIST_REMOVE(item_list, head, &items[i]) == &items[i]); |
5511d8c1 | 249 | |
5152b845 | 250 | ASSERT_NULL(head); |
5511d8c1 | 251 | |
cc232fa0 DDM |
252 | assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0); |
253 | assert_se(LIST_PREPEND(item_list, head, items + 1) == items + 1); | |
254 | assert_se(LIST_PREPEND(item_list, head, items + 2) == items + 2); | |
55cb63bf | 255 | |
c4f2bff1 ZJS |
256 | assert_se(LIST_POP(item_list, head) == items + 2); |
257 | assert_se(LIST_POP(item_list, head) == items + 1); | |
258 | assert_se(LIST_POP(item_list, head) == items + 0); | |
5152b845 | 259 | ASSERT_NULL(LIST_POP(item_list, head)); |
55cb63bf | 260 | |
d327b775 DT |
261 | /* No-op on an empty list */ |
262 | ||
263 | LIST_CLEAR(item_list, head, free); | |
264 | ||
265 | /* A non-empty list is cleared */ | |
266 | ||
267 | assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1))); | |
268 | assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1))); | |
269 | ||
270 | LIST_CLEAR(item_list, head, free); | |
271 | ||
5152b845 | 272 | ASSERT_NULL(head); |
d327b775 DT |
273 | |
274 | /* A list can be cleared partially */ | |
275 | ||
276 | assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1))); | |
277 | assert_se(LIST_PREPEND(item_list, head, new0(list_item, 1))); | |
278 | assert_se(LIST_PREPEND(item_list, head, items + 0) == items + 0); | |
279 | ||
280 | LIST_CLEAR(item_list, head->item_list_next, free); | |
281 | ||
282 | assert_se(head == items + 0); | |
5152b845 | 283 | ASSERT_NULL(head->item_list_next); |
d327b775 | 284 | |
3b18ae68 JJ |
285 | return 0; |
286 | } |