| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "list.h" |
| 5 |
#include "tests.h" |
| 6 |
|
| 7 |
int main(int argc, const char *argv[]) { |
| 8 |
test_setup_logging(LOG_DEBUG); |
| 9 |
|
| 10 |
size_t i; |
| 11 |
typedef struct list_item { |
| 12 |
LIST_FIELDS(struct list_item, item_list); |
| 13 |
} list_item; |
| 14 |
LIST_HEAD(list_item, head); |
| 15 |
LIST_HEAD(list_item, head2); |
| 16 |
list_item items[4]; |
| 17 |
|
| 18 |
LIST_HEAD_INIT(head); |
| 19 |
LIST_HEAD_INIT(head2); |
| 20 |
ASSERT_NULL(head); |
| 21 |
ASSERT_NULL(head2); |
| 22 |
|
| 23 |
FOREACH_ELEMENT(item, items) { |
| 24 |
LIST_INIT(item_list, item); |
| 25 |
assert_se(LIST_JUST_US(item_list, item)); |
| 26 |
assert_se(LIST_PREPEND(item_list, head, item) == item); |
| 27 |
} |
| 28 |
|
| 29 |
i = 0; |
| 30 |
LIST_FOREACH_OTHERS(item_list, cursor, &items[2]) { |
| 31 |
i++; |
| 32 |
assert_se(cursor != &items[2]); |
| 33 |
} |
| 34 |
assert_se(i == ELEMENTSOF(items)-1); |
| 35 |
|
| 36 |
i = 0; |
| 37 |
LIST_FOREACH_OTHERS(item_list, cursor, &items[0]) { |
| 38 |
i++; |
| 39 |
assert_se(cursor != &items[0]); |
| 40 |
} |
| 41 |
assert_se(i == ELEMENTSOF(items)-1); |
| 42 |
|
| 43 |
i = 0; |
| 44 |
LIST_FOREACH_OTHERS(item_list, cursor, &items[3]) { |
| 45 |
i++; |
| 46 |
assert_se(cursor != &items[3]); |
| 47 |
} |
| 48 |
assert_se(i == ELEMENTSOF(items)-1); |
| 49 |
|
| 50 |
assert_se(!LIST_JUST_US(item_list, head)); |
| 51 |
|
| 52 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 56 |
|
| 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]); |
| 60 |
ASSERT_NULL(items[3].item_list_prev); |
| 61 |
|
| 62 |
list_item *cursor = LIST_FIND_HEAD(item_list, &items[0]); |
| 63 |
assert_se(cursor == &items[3]); |
| 64 |
|
| 65 |
cursor = LIST_FIND_TAIL(item_list, &items[3]); |
| 66 |
assert_se(cursor == &items[0]); |
| 67 |
|
| 68 |
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
| 69 |
assert_se(LIST_JUST_US(item_list, &items[1])); |
| 70 |
|
| 71 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 77 |
ASSERT_NULL(items[3].item_list_prev); |
| 78 |
|
| 79 |
assert_se(LIST_INSERT_AFTER(item_list, head, &items[3], &items[1]) == &items[1]); |
| 80 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 88 |
ASSERT_NULL(items[3].item_list_prev); |
| 89 |
|
| 90 |
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
| 91 |
assert_se(LIST_JUST_US(item_list, &items[1])); |
| 92 |
|
| 93 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 99 |
ASSERT_NULL(items[3].item_list_prev); |
| 100 |
|
| 101 |
assert_se(LIST_INSERT_BEFORE(item_list, head, &items[2], &items[1]) == &items[1]); |
| 102 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 110 |
ASSERT_NULL(items[3].item_list_prev); |
| 111 |
|
| 112 |
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
| 113 |
assert_se(LIST_JUST_US(item_list, &items[0])); |
| 114 |
|
| 115 |
ASSERT_NULL(items[2].item_list_next); |
| 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]); |
| 121 |
ASSERT_NULL(items[3].item_list_prev); |
| 122 |
|
| 123 |
assert_se(LIST_INSERT_BEFORE(item_list, head, &items[3], &items[0]) == &items[0]); |
| 124 |
ASSERT_NULL(items[2].item_list_next); |
| 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]); |
| 132 |
ASSERT_NULL(items[0].item_list_prev); |
| 133 |
assert_se(head == &items[0]); |
| 134 |
|
| 135 |
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
| 136 |
assert_se(LIST_JUST_US(item_list, &items[0])); |
| 137 |
|
| 138 |
ASSERT_NULL(items[2].item_list_next); |
| 139 |
assert_se(items[1].item_list_next == &items[2]); |
| 140 |
assert_se(items[3].item_list_next == &items[1]); |
| 141 |
|
| 142 |
assert_se(items[2].item_list_prev == &items[1]); |
| 143 |
assert_se(items[1].item_list_prev == &items[3]); |
| 144 |
ASSERT_NULL(items[3].item_list_prev); |
| 145 |
|
| 146 |
assert_se(LIST_INSERT_BEFORE(item_list, head, NULL, &items[0]) == &items[0]); |
| 147 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 151 |
|
| 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]); |
| 155 |
ASSERT_NULL(items[3].item_list_prev); |
| 156 |
|
| 157 |
assert_se(LIST_REMOVE(item_list, head, &items[0]) == &items[0]); |
| 158 |
assert_se(LIST_JUST_US(item_list, &items[0])); |
| 159 |
|
| 160 |
ASSERT_NULL(items[2].item_list_next); |
| 161 |
assert_se(items[1].item_list_next == &items[2]); |
| 162 |
assert_se(items[3].item_list_next == &items[1]); |
| 163 |
|
| 164 |
assert_se(items[2].item_list_prev == &items[1]); |
| 165 |
assert_se(items[1].item_list_prev == &items[3]); |
| 166 |
ASSERT_NULL(items[3].item_list_prev); |
| 167 |
|
| 168 |
assert_se(LIST_REMOVE(item_list, head, &items[1]) == &items[1]); |
| 169 |
assert_se(LIST_JUST_US(item_list, &items[1])); |
| 170 |
|
| 171 |
ASSERT_NULL(items[2].item_list_next); |
| 172 |
assert_se(items[3].item_list_next == &items[2]); |
| 173 |
|
| 174 |
assert_se(items[2].item_list_prev == &items[3]); |
| 175 |
ASSERT_NULL(items[3].item_list_prev); |
| 176 |
|
| 177 |
assert_se(LIST_REMOVE(item_list, head, &items[2]) == &items[2]); |
| 178 |
assert_se(LIST_JUST_US(item_list, &items[2])); |
| 179 |
assert_se(LIST_JUST_US(item_list, head)); |
| 180 |
|
| 181 |
assert_se(LIST_REMOVE(item_list, head, &items[3]) == &items[3]); |
| 182 |
assert_se(LIST_JUST_US(item_list, &items[3])); |
| 183 |
|
| 184 |
ASSERT_NULL(head); |
| 185 |
|
| 186 |
FOREACH_ELEMENT(item, items) { |
| 187 |
assert_se(LIST_JUST_US(item_list, item)); |
| 188 |
assert_se(LIST_APPEND(item_list, head, item) == item); |
| 189 |
} |
| 190 |
|
| 191 |
assert_se(!LIST_JUST_US(item_list, head)); |
| 192 |
|
| 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]); |
| 196 |
ASSERT_NULL(items[3].item_list_next); |
| 197 |
|
| 198 |
ASSERT_NULL(items[0].item_list_prev); |
| 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]); |
| 202 |
|
| 203 |
FOREACH_ELEMENT(item, items) |
| 204 |
assert_se(LIST_REMOVE(item_list, head, item) == item); |
| 205 |
|
| 206 |
ASSERT_NULL(head); |
| 207 |
|
| 208 |
FOREACH_ARRAY(item, items, ELEMENTSOF(items) / 2) { |
| 209 |
LIST_INIT(item_list, item); |
| 210 |
assert_se(LIST_JUST_US(item_list, item)); |
| 211 |
assert_se(LIST_PREPEND(item_list, head, item) == item); |
| 212 |
} |
| 213 |
|
| 214 |
for (i = ELEMENTSOF(items) / 2; i < ELEMENTSOF(items); i++) { |
| 215 |
LIST_INIT(item_list, &items[i]); |
| 216 |
assert_se(LIST_JUST_US(item_list, &items[i])); |
| 217 |
assert_se(LIST_PREPEND(item_list, head2, &items[i]) == &items[i]); |
| 218 |
} |
| 219 |
|
| 220 |
ASSERT_NULL(items[0].item_list_next); |
| 221 |
assert_se(items[1].item_list_next == &items[0]); |
| 222 |
ASSERT_NULL(items[2].item_list_next); |
| 223 |
assert_se(items[3].item_list_next == &items[2]); |
| 224 |
|
| 225 |
assert_se(items[0].item_list_prev == &items[1]); |
| 226 |
ASSERT_NULL(items[1].item_list_prev); |
| 227 |
assert_se(items[2].item_list_prev == &items[3]); |
| 228 |
ASSERT_NULL(items[3].item_list_prev); |
| 229 |
|
| 230 |
assert_se(LIST_JOIN(item_list, head2, head) == head2); |
| 231 |
ASSERT_NULL(head); |
| 232 |
|
| 233 |
ASSERT_NULL(items[0].item_list_next); |
| 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]); |
| 237 |
|
| 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]); |
| 241 |
ASSERT_NULL(items[3].item_list_prev); |
| 242 |
|
| 243 |
assert_se(LIST_JOIN(item_list, head, head2) == head); |
| 244 |
ASSERT_NULL(head2); |
| 245 |
assert_se(head); |
| 246 |
|
| 247 |
FOREACH_ELEMENT(item, items) |
| 248 |
assert_se(LIST_REMOVE(item_list, head, item) == item); |
| 249 |
|
| 250 |
ASSERT_NULL(head); |
| 251 |
|
| 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); |
| 255 |
|
| 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); |
| 259 |
ASSERT_NULL(LIST_POP(item_list, head)); |
| 260 |
|
| 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 |
|
| 272 |
ASSERT_NULL(head); |
| 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); |
| 283 |
ASSERT_NULL(head->item_list_next); |
| 284 |
|
| 285 |
return 0; |
| 286 |
} |