2 * Copyright © 2011 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 #ifdef HAVE_DIX_CONFIG_H
25 #include <dix-config.h>
36 struct xorg_list children
;
43 struct xorg_list node
;
47 test_xorg_list_init(void)
49 struct parent parent
, tmp
;
51 memset(&parent
, 0, sizeof(parent
));
57 xorg_list_init(&parent
.children
);
59 /* test we haven't touched anything else. */
60 assert(parent
.a
== tmp
.a
);
61 assert(parent
.b
== tmp
.b
);
63 assert(xorg_list_is_empty(&parent
.children
));
67 test_xorg_list_add(void)
69 struct parent parent
= { 0 };
70 struct child child
[3];
73 xorg_list_init(&parent
.children
);
75 xorg_list_add(&child
[0].node
, &parent
.children
);
76 assert(!xorg_list_is_empty(&parent
.children
));
78 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
80 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
82 /* note: xorg_list_add prepends */
83 xorg_list_add(&child
[1].node
, &parent
.children
);
84 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
86 assert(memcmp(c
, &child
[1], sizeof(struct child
)) == 0);
88 xorg_list_add(&child
[2].node
, &parent
.children
);
89 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
91 assert(memcmp(c
, &child
[2], sizeof(struct child
)) == 0);
95 test_xorg_list_append(void)
97 struct parent parent
= { 0 };
98 struct child child
[3];
102 xorg_list_init(&parent
.children
);
104 xorg_list_append(&child
[0].node
, &parent
.children
);
105 assert(!xorg_list_is_empty(&parent
.children
));
107 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
109 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
110 c
= xorg_list_last_entry(&parent
.children
, struct child
, node
);
112 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
114 xorg_list_append(&child
[1].node
, &parent
.children
);
115 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
117 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
118 c
= xorg_list_last_entry(&parent
.children
, struct child
, node
);
120 assert(memcmp(c
, &child
[1], sizeof(struct child
)) == 0);
122 xorg_list_append(&child
[2].node
, &parent
.children
);
123 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
125 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
126 c
= xorg_list_last_entry(&parent
.children
, struct child
, node
);
128 assert(memcmp(c
, &child
[2], sizeof(struct child
)) == 0);
131 xorg_list_for_each_entry(c
, &parent
.children
, node
) {
132 assert(memcmp(c
, &child
[i
++], sizeof(struct child
)) == 0);
137 test_xorg_list_del(void)
139 struct parent parent
= { 0 };
140 struct child child
[2];
143 xorg_list_init(&parent
.children
);
145 xorg_list_add(&child
[0].node
, &parent
.children
);
146 assert(!xorg_list_is_empty(&parent
.children
));
148 xorg_list_del(&parent
.children
);
149 assert(xorg_list_is_empty(&parent
.children
));
151 xorg_list_add(&child
[0].node
, &parent
.children
);
152 xorg_list_del(&child
[0].node
);
153 assert(xorg_list_is_empty(&parent
.children
));
155 xorg_list_add(&child
[0].node
, &parent
.children
);
156 xorg_list_add(&child
[1].node
, &parent
.children
);
158 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
160 assert(memcmp(c
, &child
[1], sizeof(struct child
)) == 0);
162 /* delete first node */
163 xorg_list_del(&child
[1].node
);
164 assert(!xorg_list_is_empty(&parent
.children
));
165 assert(xorg_list_is_empty(&child
[1].node
));
166 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
168 assert(memcmp(c
, &child
[0], sizeof(struct child
)) == 0);
170 /* delete last node */
171 xorg_list_add(&child
[1].node
, &parent
.children
);
172 xorg_list_del(&child
[0].node
);
173 c
= xorg_list_first_entry(&parent
.children
, struct child
, node
);
175 assert(memcmp(c
, &child
[1], sizeof(struct child
)) == 0);
177 /* delete list head */
178 xorg_list_add(&child
[0].node
, &parent
.children
);
179 xorg_list_del(&parent
.children
);
180 assert(xorg_list_is_empty(&parent
.children
));
181 assert(!xorg_list_is_empty(&child
[0].node
));
182 assert(!xorg_list_is_empty(&child
[1].node
));
186 test_xorg_list_for_each(void)
188 struct parent parent
= { 0 };
189 struct child child
[3];
193 xorg_list_init(&parent
.children
);
195 xorg_list_add(&child
[2].node
, &parent
.children
);
196 xorg_list_add(&child
[1].node
, &parent
.children
);
197 xorg_list_add(&child
[0].node
, &parent
.children
);
199 xorg_list_for_each_entry(c
, &parent
.children
, node
) {
200 assert(memcmp(c
, &child
[i
], sizeof(struct child
)) == 0);
204 /* foreach on empty list */
205 xorg_list_del(&parent
.children
);
206 assert(xorg_list_is_empty(&parent
.children
));
208 xorg_list_for_each_entry(c
, &parent
.children
, node
) {
209 assert(0); /* we must not get here */
220 test_nt_list_init(void)
226 nt_list_init(&foo
, next
);
230 assert(foo
.next
== NULL
);
231 assert(nt_list_next(&foo
, next
) == NULL
);
235 test_nt_list_append(void)
238 struct foo
*foo
= calloc(10, sizeof(struct foo
));
241 for (item
= foo
, i
= 1; i
<= 10; i
++, item
++) {
244 nt_list_init(item
, next
);
247 nt_list_append(item
, foo
, struct foo
, next
);
250 /* Test using nt_list_next */
251 for (item
= foo
, i
= 1; i
<= 10; i
++, item
= nt_list_next(item
, next
)) {
253 assert(item
->b
= i
* 2);
256 /* Test using nt_list_for_each_entry */
258 nt_list_for_each_entry(item
, foo
, next
) {
260 assert(item
->b
= i
* 2);
267 test_nt_list_insert(void)
270 struct foo
*foo
= calloc(10, sizeof(struct foo
));
275 nt_list_init(foo
, next
);
277 for (item
= &foo
[1], i
= 9; i
> 0; i
--, item
++) {
280 nt_list_init(item
, next
);
281 nt_list_insert(item
, foo
, struct foo
, next
);
284 /* Test using nt_list_next */
285 for (item
= foo
, i
= 10; i
> 0; i
--, item
= nt_list_next(item
, next
)) {
287 assert(item
->b
= i
* 2);
290 /* Test using nt_list_for_each_entry */
292 nt_list_for_each_entry(item
, foo
, next
) {
294 assert(item
->b
= i
* 2);
301 test_nt_list_delete(void)
304 struct foo
*list
= calloc(10, sizeof(struct foo
));
305 struct foo
*foo
= list
;
306 struct foo
*item
, *tmp
;
307 struct foo
*empty_list
= foo
;
309 nt_list_init(empty_list
, next
);
310 nt_list_del(empty_list
, empty_list
, struct foo
, next
);
314 for (item
= foo
, i
= 1; i
<= 10; i
++, item
++) {
317 nt_list_init(item
, next
);
320 nt_list_append(item
, foo
, struct foo
, next
);
324 nt_list_for_each_entry(item
, foo
, next
) {
329 /* delete last item */
330 nt_list_del(&foo
[9], foo
, struct foo
, next
);
333 nt_list_for_each_entry(item
, foo
, next
) {
334 assert(item
->a
!= 10); /* element 10 is gone now */
337 assert(i
== 9); /* 9 elements left */
339 /* delete second item */
340 nt_list_del(foo
->next
, foo
, struct foo
, next
);
342 assert(foo
->next
->a
== 3);
345 nt_list_for_each_entry(item
, foo
, next
) {
346 assert(item
->a
!= 10); /* element 10 is gone now */
347 assert(item
->a
!= 2); /* element 2 is gone now */
350 assert(i
== 8); /* 9 elements left */
353 /* delete first item */
354 nt_list_del(foo
, foo
, struct foo
, next
);
357 assert(item
->next
== NULL
);
359 assert(foo
->next
->a
== 4);
361 nt_list_for_each_entry_safe(item
, tmp
, foo
, next
) {
362 nt_list_del(item
, foo
, struct foo
, next
);
372 main(int argc
, char **argv
)
374 test_xorg_list_init();
375 test_xorg_list_add();
376 test_xorg_list_append();
377 test_xorg_list_del();
378 test_xorg_list_for_each();
381 test_nt_list_append();
382 test_nt_list_insert();
383 test_nt_list_delete();