]>
Commit | Line | Data |
---|---|---|
3b18ae68 JJ |
1 | /*** |
2 | This file is part of systemd | |
3 | ||
4 | Copyright 2013 Jan Janssen | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include "list.h" | |
21 | #include "util.h" | |
22 | ||
23 | int main(int argc, const char *argv[]) { | |
24 | size_t i; | |
25 | typedef struct list_item { | |
26 | LIST_FIELDS(struct list_item, item); | |
27 | } list_item; | |
28 | LIST_HEAD(list_item, head); | |
29 | list_item items[4]; | |
30 | list_item *cursor; | |
31 | ||
71fda00f | 32 | LIST_HEAD_INIT(head); |
3b18ae68 JJ |
33 | assert_se(head == NULL); |
34 | ||
35 | for (i = 0; i < ELEMENTSOF(items); i++) { | |
71fda00f | 36 | LIST_INIT(item, &items[i]); |
3b18ae68 | 37 | assert_se(LIST_JUST_US(item, &items[i])); |
71fda00f | 38 | LIST_PREPEND(item, head, &items[i]); |
3b18ae68 JJ |
39 | } |
40 | ||
7663df37 LP |
41 | i = 0; |
42 | LIST_FOREACH_OTHERS(item, cursor, &items[2]) { | |
43 | i++; | |
44 | assert_se(cursor != &items[2]); | |
45 | } | |
46 | assert_se(i == ELEMENTSOF(items)-1); | |
47 | ||
3ad85e37 LP |
48 | i = 0; |
49 | LIST_FOREACH_OTHERS(item, cursor, &items[0]) { | |
50 | i++; | |
51 | assert_se(cursor != &items[0]); | |
52 | } | |
53 | assert_se(i == ELEMENTSOF(items)-1); | |
54 | ||
55 | i = 0; | |
56 | LIST_FOREACH_OTHERS(item, cursor, &items[3]) { | |
57 | i++; | |
58 | assert_se(cursor != &items[3]); | |
59 | } | |
60 | assert_se(i == ELEMENTSOF(items)-1); | |
61 | ||
3b18ae68 JJ |
62 | assert_se(!LIST_JUST_US(item, head)); |
63 | ||
64 | assert_se(items[0].item_next == NULL); | |
65 | assert_se(items[1].item_next == &items[0]); | |
66 | assert_se(items[2].item_next == &items[1]); | |
67 | assert_se(items[3].item_next == &items[2]); | |
68 | ||
69 | assert_se(items[0].item_prev == &items[1]); | |
70 | assert_se(items[1].item_prev == &items[2]); | |
71 | assert_se(items[2].item_prev == &items[3]); | |
72 | assert_se(items[3].item_prev == NULL); | |
73 | ||
71fda00f | 74 | LIST_FIND_HEAD(item, &items[0], cursor); |
3b18ae68 JJ |
75 | assert_se(cursor == &items[3]); |
76 | ||
71fda00f | 77 | LIST_FIND_TAIL(item, &items[3], cursor); |
3b18ae68 JJ |
78 | assert_se(cursor == &items[0]); |
79 | ||
71fda00f | 80 | LIST_REMOVE(item, head, &items[1]); |
3b18ae68 JJ |
81 | assert_se(LIST_JUST_US(item, &items[1])); |
82 | ||
83 | assert_se(items[0].item_next == NULL); | |
84 | assert_se(items[2].item_next == &items[0]); | |
85 | assert_se(items[3].item_next == &items[2]); | |
86 | ||
87 | assert_se(items[0].item_prev == &items[2]); | |
88 | assert_se(items[2].item_prev == &items[3]); | |
89 | assert_se(items[3].item_prev == NULL); | |
90 | ||
71fda00f | 91 | LIST_INSERT_AFTER(item, head, &items[3], &items[1]); |
3b18ae68 JJ |
92 | assert_se(items[0].item_next == NULL); |
93 | assert_se(items[2].item_next == &items[0]); | |
94 | assert_se(items[1].item_next == &items[2]); | |
95 | assert_se(items[3].item_next == &items[1]); | |
96 | ||
97 | assert_se(items[0].item_prev == &items[2]); | |
98 | assert_se(items[2].item_prev == &items[1]); | |
99 | assert_se(items[1].item_prev == &items[3]); | |
100 | assert_se(items[3].item_prev == NULL); | |
101 | ||
71fda00f | 102 | LIST_REMOVE(item, head, &items[0]); |
3b18ae68 JJ |
103 | assert_se(LIST_JUST_US(item, &items[0])); |
104 | ||
105 | assert_se(items[2].item_next == NULL); | |
106 | assert_se(items[1].item_next == &items[2]); | |
107 | assert_se(items[3].item_next == &items[1]); | |
108 | ||
109 | assert_se(items[2].item_prev == &items[1]); | |
110 | assert_se(items[1].item_prev == &items[3]); | |
111 | assert_se(items[3].item_prev == NULL); | |
112 | ||
71fda00f | 113 | LIST_REMOVE(item, head, &items[1]); |
3b18ae68 JJ |
114 | assert_se(LIST_JUST_US(item, &items[1])); |
115 | ||
116 | assert_se(items[2].item_next == NULL); | |
117 | assert_se(items[3].item_next == &items[2]); | |
118 | ||
119 | assert_se(items[2].item_prev == &items[3]); | |
120 | assert_se(items[3].item_prev == NULL); | |
121 | ||
71fda00f | 122 | LIST_REMOVE(item, head, &items[2]); |
3b18ae68 JJ |
123 | assert_se(LIST_JUST_US(item, &items[2])); |
124 | assert_se(LIST_JUST_US(item, head)); | |
125 | ||
71fda00f | 126 | LIST_REMOVE(item, head, &items[3]); |
3b18ae68 JJ |
127 | assert_se(LIST_JUST_US(item, &items[3])); |
128 | ||
502f1733 ZJS |
129 | assert_se(head == NULL); |
130 | ||
131 | for (i = 0; i < ELEMENTSOF(items); i++) { | |
132 | assert_se(LIST_JUST_US(item, &items[i])); | |
133 | LIST_APPEND(item, head, &items[i]); | |
134 | } | |
135 | ||
136 | assert_se(!LIST_JUST_US(item, head)); | |
137 | ||
138 | assert_se(items[0].item_next == &items[1]); | |
139 | assert_se(items[1].item_next == &items[2]); | |
140 | assert_se(items[2].item_next == &items[3]); | |
141 | assert_se(items[3].item_next == NULL); | |
142 | ||
143 | assert_se(items[0].item_prev == NULL); | |
144 | assert_se(items[1].item_prev == &items[0]); | |
145 | assert_se(items[2].item_prev == &items[1]); | |
146 | assert_se(items[3].item_prev == &items[2]); | |
147 | ||
148 | for (i = 0; i < ELEMENTSOF(items); i++) | |
7663df37 | 149 | LIST_REMOVE(item, head, &items[i]); |
502f1733 ZJS |
150 | |
151 | assert_se(head == NULL); | |
152 | ||
3b18ae68 JJ |
153 | return 0; |
154 | } |