| 1 |
/// @file |
| 2 |
/// @brief basic unit tester for list.h |
| 3 |
|
| 4 |
#ifdef NDEBUG |
| 5 |
#error "this is not intended to be compiled with assertions off" |
| 6 |
#endif |
| 7 |
|
| 8 |
#define NO_CONFIG // suppress include of config.h in list.c |
| 9 |
|
| 10 |
#include <assert.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <util/list.c> |
| 14 |
#include <util/list.h> |
| 15 |
#include <util/unused.h> |
| 16 |
|
| 17 |
// test construction and destruction, with nothing in-between |
| 18 |
static void test_create_reset(void) { |
| 19 |
LIST(int) i = {0}; |
| 20 |
LIST_FREE(&i); |
| 21 |
} |
| 22 |
|
| 23 |
// a list should start in a known initial state |
| 24 |
static void test_init(void) { |
| 25 |
LIST(int) i = {0}; |
| 26 |
assert(LIST_IS_EMPTY(&i)); |
| 27 |
assert(LIST_SIZE(&i) == 0); |
| 28 |
} |
| 29 |
|
| 30 |
// reset of an initialized list should be OK and idempotent |
| 31 |
static void test_init_reset(void) { |
| 32 |
LIST(int) i = {0}; |
| 33 |
LIST_FREE(&i); |
| 34 |
LIST_FREE(&i); |
| 35 |
LIST_FREE(&i); |
| 36 |
} |
| 37 |
|
| 38 |
// repeated append |
| 39 |
static void test_append(void) { |
| 40 |
LIST(int) xs = {0}; |
| 41 |
assert(LIST_IS_EMPTY(&xs)); |
| 42 |
|
| 43 |
for (size_t i = 0; i < 10; ++i) { |
| 44 |
LIST_APPEND(&xs, (int)i); |
| 45 |
assert(LIST_SIZE(&xs) == i + 1); |
| 46 |
} |
| 47 |
|
| 48 |
LIST_FREE(&xs); |
| 49 |
} |
| 50 |
|
| 51 |
/// append should not be affected by surprising parameter expansion order |
| 52 |
/// https://gitlab.com/graphviz/graphviz/-/issues/2734 |
| 53 |
static void test_2734_append(void) { |
| 54 |
LIST(int) xs = {0}; |
| 55 |
|
| 56 |
LIST_APPEND(&xs, 42); |
| 57 |
LIST_APPEND(&xs, LIST_GET(&xs, LIST_SIZE(&xs) - 1)); |
| 58 |
assert(LIST_GET(&xs, 1) == 42); |
| 59 |
|
| 60 |
LIST_FREE(&xs); |
| 61 |
} |
| 62 |
|
| 63 |
/// prepend to an empty list |
| 64 |
static void test_prepend_0(void) { |
| 65 |
LIST(int) xs = {0}; |
| 66 |
|
| 67 |
LIST_PREPEND(&xs, 42); |
| 68 |
assert(LIST_SIZE(&xs) == 1); |
| 69 |
assert(LIST_GET(&xs, 0) == 42); |
| 70 |
|
| 71 |
LIST_FREE(&xs); |
| 72 |
} |
| 73 |
|
| 74 |
/// try-append should not be affected by surprising parameter expansion order |
| 75 |
/// https://gitlab.com/graphviz/graphviz/-/issues/2734 |
| 76 |
static void test_2734_try_append(void) { |
| 77 |
LIST(int) xs = {0}; |
| 78 |
|
| 79 |
if (!LIST_TRY_APPEND(&xs, 42)) { |
| 80 |
goto done; |
| 81 |
} |
| 82 |
|
| 83 |
if (!LIST_TRY_APPEND(&xs, LIST_GET(&xs, LIST_SIZE(&xs) - 1))) { |
| 84 |
goto done; |
| 85 |
} |
| 86 |
|
| 87 |
assert(LIST_GET(&xs, 1) == 42); |
| 88 |
|
| 89 |
done: |
| 90 |
LIST_FREE(&xs); |
| 91 |
} |
| 92 |
|
| 93 |
/// interleaved append and prepend |
| 94 |
static void test_append_prepend(void) { |
| 95 |
LIST(int) xs = {0}; |
| 96 |
|
| 97 |
for (size_t i = 0; i < 10; ++i) { |
| 98 |
if (i % 2 == 0) { |
| 99 |
LIST_APPEND(&xs, (int)i); |
| 100 |
} else { |
| 101 |
LIST_PREPEND(&xs, (int)i); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
assert(LIST_SIZE(&xs) == 10); |
| 106 |
assert(LIST_GET(&xs, 0) == 9); |
| 107 |
assert(LIST_GET(&xs, 1) == 7); |
| 108 |
assert(LIST_GET(&xs, 2) == 5); |
| 109 |
assert(LIST_GET(&xs, 3) == 3); |
| 110 |
assert(LIST_GET(&xs, 4) == 1); |
| 111 |
assert(LIST_GET(&xs, 5) == 0); |
| 112 |
assert(LIST_GET(&xs, 6) == 2); |
| 113 |
assert(LIST_GET(&xs, 7) == 4); |
| 114 |
assert(LIST_GET(&xs, 8) == 6); |
| 115 |
assert(LIST_GET(&xs, 9) == 8); |
| 116 |
|
| 117 |
LIST_FREE(&xs); |
| 118 |
} |
| 119 |
|
| 120 |
/// prepend should not be affected by surprising parameter expansion order |
| 121 |
/// https://gitlab.com/graphviz/graphviz/-/issues/2734 |
| 122 |
static void test_2734_prepend(void) { |
| 123 |
LIST(int) xs = {0}; |
| 124 |
|
| 125 |
LIST_PREPEND(&xs, 42); |
| 126 |
LIST_PREPEND(&xs, LIST_GET(&xs, 0)); |
| 127 |
assert(LIST_GET(&xs, 0) == 42); |
| 128 |
|
| 129 |
LIST_FREE(&xs); |
| 130 |
} |
| 131 |
|
| 132 |
static void test_get(void) { |
| 133 |
LIST(int) xs = {0}; |
| 134 |
for (size_t i = 0; i < 10; ++i) { |
| 135 |
LIST_APPEND(&xs, (int)i); |
| 136 |
} |
| 137 |
|
| 138 |
for (size_t i = 0; i < 10; ++i) { |
| 139 |
assert(LIST_GET(&xs, i) == (int)i); |
| 140 |
} |
| 141 |
for (size_t i = 9;; --i) { |
| 142 |
assert(LIST_GET(&xs, i) == (int)i); |
| 143 |
if (i == 0) { |
| 144 |
break; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
LIST_FREE(&xs); |
| 149 |
} |
| 150 |
|
| 151 |
static void test_set(void) { |
| 152 |
LIST(int) xs = {0}; |
| 153 |
for (size_t i = 0; i < 10; ++i) { |
| 154 |
LIST_APPEND(&xs, (int)i); |
| 155 |
} |
| 156 |
|
| 157 |
for (size_t i = 0; i < 10; ++i) { |
| 158 |
LIST_SET(&xs, i, (int)(i + 1)); |
| 159 |
assert(LIST_GET(&xs, i) == (int)i + 1); |
| 160 |
} |
| 161 |
for (size_t i = 9;; --i) { |
| 162 |
LIST_SET(&xs, i, (int)i - 1); |
| 163 |
assert(LIST_GET(&xs, i) == (int)i - 1); |
| 164 |
if (i == 0) { |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
LIST_FREE(&xs); |
| 170 |
} |
| 171 |
|
| 172 |
/// removing from an empty list should be a no-op |
| 173 |
static void test_remove_empty(void) { |
| 174 |
LIST(int) xs = {0}; |
| 175 |
LIST_REMOVE(&xs, 10); |
| 176 |
assert(LIST_SIZE(&xs) == 0); |
| 177 |
LIST_FREE(&xs); |
| 178 |
} |
| 179 |
|
| 180 |
/// some basic removal tests |
| 181 |
static void test_remove(void) { |
| 182 |
LIST(int) xs = {0}; |
| 183 |
|
| 184 |
for (size_t i = 0; i < 10; ++i) { |
| 185 |
LIST_APPEND(&xs, (int)i); |
| 186 |
} |
| 187 |
|
| 188 |
// remove something that does not exist |
| 189 |
LIST_REMOVE(&xs, 42); |
| 190 |
for (size_t i = 0; i < 10; ++i) { |
| 191 |
assert(LIST_GET(&xs, i) == (int)i); |
| 192 |
} |
| 193 |
|
| 194 |
// remove in the middle |
| 195 |
LIST_REMOVE(&xs, 4); |
| 196 |
assert(LIST_SIZE(&xs) == 9); |
| 197 |
for (size_t i = 0; i < 9; ++i) { |
| 198 |
if (i < 4) { |
| 199 |
assert(LIST_GET(&xs, i) == (int)i); |
| 200 |
} else { |
| 201 |
assert(LIST_GET(&xs, i) == (int)i + 1); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// remove the first |
| 206 |
LIST_REMOVE(&xs, 0); |
| 207 |
assert(LIST_SIZE(&xs) == 8); |
| 208 |
for (size_t i = 0; i < 8; ++i) { |
| 209 |
if (i < 3) { |
| 210 |
assert(LIST_GET(&xs, i) == (int)i + 1); |
| 211 |
} else { |
| 212 |
assert(LIST_GET(&xs, i) == (int)i + 2); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// remove the last |
| 217 |
LIST_REMOVE(&xs, 9); |
| 218 |
assert(LIST_SIZE(&xs) == 7); |
| 219 |
for (size_t i = 0; i < 7; ++i) { |
| 220 |
if (i < 3) { |
| 221 |
assert(LIST_GET(&xs, i) == (int)i + 1); |
| 222 |
} else { |
| 223 |
assert(LIST_GET(&xs, i) == (int)i + 2); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
// remove all the rest |
| 228 |
for (size_t i = 0; i < 7; ++i) { |
| 229 |
LIST_REMOVE(&xs, LIST_GET(&xs, 0)); |
| 230 |
} |
| 231 |
assert(LIST_SIZE(&xs) == 0); |
| 232 |
|
| 233 |
LIST_FREE(&xs); |
| 234 |
} |
| 235 |
|
| 236 |
static void test_at(void) { |
| 237 |
LIST(int) xs = {0}; |
| 238 |
for (size_t i = 0; i < 10; ++i) { |
| 239 |
LIST_APPEND(&xs, (int)i); |
| 240 |
} |
| 241 |
|
| 242 |
for (size_t i = 0; i < 10; ++i) { |
| 243 |
assert(LIST_GET(&xs, i) == *LIST_AT(&xs, i)); |
| 244 |
} |
| 245 |
|
| 246 |
for (size_t i = 0; i < 10; ++i) { |
| 247 |
int *j = LIST_AT(&xs, i); |
| 248 |
*j = (int)i + 1; |
| 249 |
assert(LIST_GET(&xs, i) == (int)i + 1); |
| 250 |
} |
| 251 |
|
| 252 |
LIST_FREE(&xs); |
| 253 |
} |
| 254 |
|
| 255 |
static void test_clear_empty(void) { |
| 256 |
LIST(int) xs = {0}; |
| 257 |
LIST_CLEAR(&xs); |
| 258 |
assert(LIST_IS_EMPTY(&xs)); |
| 259 |
|
| 260 |
LIST_FREE(&xs); |
| 261 |
} |
| 262 |
|
| 263 |
static void test_clear(void) { |
| 264 |
LIST(int) xs = {0}; |
| 265 |
for (size_t i = 0; i < 10; ++i) { |
| 266 |
LIST_APPEND(&xs, (int)i); |
| 267 |
} |
| 268 |
|
| 269 |
assert(!LIST_IS_EMPTY(&xs)); |
| 270 |
LIST_CLEAR(&xs); |
| 271 |
assert(LIST_IS_EMPTY(&xs)); |
| 272 |
|
| 273 |
LIST_FREE(&xs); |
| 274 |
} |
| 275 |
|
| 276 |
// basic push then pop |
| 277 |
static void test_push_one(void) { |
| 278 |
LIST(int) s = {0}; |
| 279 |
int arbitrary = 42; |
| 280 |
LIST_PUSH_BACK(&s, arbitrary); |
| 281 |
assert(LIST_SIZE(&s) == 1); |
| 282 |
int top = LIST_POP_BACK(&s); |
| 283 |
assert(top == arbitrary); |
| 284 |
assert(LIST_IS_EMPTY(&s)); |
| 285 |
LIST_FREE(&s); |
| 286 |
} |
| 287 |
|
| 288 |
static void push_then_pop(int count) { |
| 289 |
LIST(int) s = {0}; |
| 290 |
for (int i = 0; i < count; ++i) { |
| 291 |
LIST_PUSH_BACK(&s, i); |
| 292 |
assert(LIST_SIZE(&s) == (size_t)i + 1); |
| 293 |
} |
| 294 |
for (int i = count - 1;; --i) { |
| 295 |
assert(LIST_SIZE(&s) == (size_t)i + 1); |
| 296 |
int p = LIST_POP_BACK(&s); |
| 297 |
assert(p == i); |
| 298 |
if (i == 0) { |
| 299 |
break; |
| 300 |
} |
| 301 |
} |
| 302 |
LIST_FREE(&s); |
| 303 |
} |
| 304 |
|
| 305 |
// push a series of items |
| 306 |
static void test_push_then_pop_ten(void) { push_then_pop(10); } |
| 307 |
|
| 308 |
// push enough to cause an expansion |
| 309 |
static void test_push_then_pop_many(void) { push_then_pop(4096); } |
| 310 |
|
| 311 |
// interleave some push and pop operations |
| 312 |
static void test_push_pop_interleaved(void) { |
| 313 |
LIST(int) s = {0}; |
| 314 |
size_t size = 0; |
| 315 |
for (int i = 0; i < 4096; ++i) { |
| 316 |
if (i % 3 == 1) { |
| 317 |
int p = LIST_POP_BACK(&s); |
| 318 |
assert(p == i - 1); |
| 319 |
--size; |
| 320 |
} else { |
| 321 |
LIST_PUSH_BACK(&s, i); |
| 322 |
++size; |
| 323 |
} |
| 324 |
assert(LIST_SIZE(&s) == size); |
| 325 |
} |
| 326 |
LIST_FREE(&s); |
| 327 |
} |
| 328 |
|
| 329 |
/// an int comparer |
| 330 |
static int cmp_int(const void *x, const void *y) { |
| 331 |
const int *a = x; |
| 332 |
const int *b = y; |
| 333 |
if (*a < *b) { |
| 334 |
return -1; |
| 335 |
} |
| 336 |
if (*a > *b) { |
| 337 |
return 1; |
| 338 |
} |
| 339 |
return 0; |
| 340 |
} |
| 341 |
|
| 342 |
/// sort on an empty list should be a no-op |
| 343 |
static void test_sort_empty(void) { |
| 344 |
LIST(int) xs = {0}; |
| 345 |
LIST_SORT(&xs, cmp_int); |
| 346 |
assert(LIST_SIZE(&xs) == 0); |
| 347 |
LIST_FREE(&xs); |
| 348 |
} |
| 349 |
|
| 350 |
static void test_sort(void) { |
| 351 |
LIST(int) xs = {0}; |
| 352 |
|
| 353 |
// a list of ints in an arbitrary order |
| 354 |
const int ys[] = {4, 2, 10, 5, -42, 3}; |
| 355 |
|
| 356 |
// setup this list and sort it |
| 357 |
for (size_t i = 0; i < sizeof(ys) / sizeof(ys[0]); ++i) { |
| 358 |
LIST_APPEND(&xs, ys[i]); |
| 359 |
} |
| 360 |
LIST_SORT(&xs, cmp_int); |
| 361 |
|
| 362 |
// we should now have a sorted version of `ys` |
| 363 |
assert(LIST_SIZE(&xs) == sizeof(ys) / sizeof(ys[0])); |
| 364 |
assert(LIST_GET(&xs, 0) == -42); |
| 365 |
assert(LIST_GET(&xs, 1) == 2); |
| 366 |
assert(LIST_GET(&xs, 2) == 3); |
| 367 |
assert(LIST_GET(&xs, 3) == 4); |
| 368 |
assert(LIST_GET(&xs, 4) == 5); |
| 369 |
assert(LIST_GET(&xs, 5) == 10); |
| 370 |
|
| 371 |
LIST_FREE(&xs); |
| 372 |
} |
| 373 |
|
| 374 |
/// sorting an already sorted list should be a no-op |
| 375 |
static void test_sort_sorted(void) { |
| 376 |
LIST(int) xs = {0}; |
| 377 |
const int ys[] = {-42, 2, 3, 4, 5, 10}; |
| 378 |
|
| 379 |
for (size_t i = 0; i < sizeof(ys) / sizeof(ys[0]); ++i) { |
| 380 |
LIST_APPEND(&xs, ys[i]); |
| 381 |
} |
| 382 |
LIST_SORT(&xs, cmp_int); |
| 383 |
|
| 384 |
for (size_t i = 0; i < sizeof(ys) / sizeof(ys[0]); ++i) { |
| 385 |
assert(LIST_GET(&xs, i) == ys[i]); |
| 386 |
} |
| 387 |
|
| 388 |
LIST_FREE(&xs); |
| 389 |
} |
| 390 |
|
| 391 |
typedef struct { |
| 392 |
int x; |
| 393 |
int y; |
| 394 |
} pair_t; |
| 395 |
|
| 396 |
/// a pair comparer, using only the first element |
| 397 |
static int cmp_pair(const void *x, const void *y) { |
| 398 |
const pair_t *a = x; |
| 399 |
const pair_t *b = y; |
| 400 |
if (a->x < b->x) { |
| 401 |
return -1; |
| 402 |
} |
| 403 |
if (a->x > b->x) { |
| 404 |
return 1; |
| 405 |
} |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
/// sorting a complex type should move entire values of the type together |
| 410 |
static void test_sort_complex(void) { |
| 411 |
LIST(pair_t) xs = {0}; |
| 412 |
|
| 413 |
const pair_t ys[] = {{1, 2}, {-2, 3}, {-10, 4}, {0, 7}}; |
| 414 |
|
| 415 |
for (size_t i = 0; i < sizeof(ys) / sizeof(ys[0]); ++i) { |
| 416 |
LIST_APPEND(&xs, ys[i]); |
| 417 |
} |
| 418 |
LIST_SORT(&xs, cmp_pair); |
| 419 |
|
| 420 |
assert(LIST_SIZE(&xs) == sizeof(ys) / sizeof(ys[0])); |
| 421 |
assert(LIST_GET(&xs, 0).x == -10); |
| 422 |
assert(LIST_GET(&xs, 0).y == 4); |
| 423 |
assert(LIST_GET(&xs, 1).x == -2); |
| 424 |
assert(LIST_GET(&xs, 1).y == 3); |
| 425 |
assert(LIST_GET(&xs, 2).x == 0); |
| 426 |
assert(LIST_GET(&xs, 2).y == 7); |
| 427 |
assert(LIST_GET(&xs, 3).x == 1); |
| 428 |
assert(LIST_GET(&xs, 3).y == 2); |
| 429 |
|
| 430 |
LIST_FREE(&xs); |
| 431 |
} |
| 432 |
|
| 433 |
static void test_shrink(void) { |
| 434 |
LIST(int) xs = {0}; |
| 435 |
|
| 436 |
// to test this one we need to access the list internals |
| 437 |
while (LIST_SIZE(&xs) == xs.impl.capacity) { |
| 438 |
LIST_APPEND(&xs, 42); |
| 439 |
} |
| 440 |
|
| 441 |
assert(xs.impl.capacity > LIST_SIZE(&xs)); |
| 442 |
LIST_SHRINK_TO_FIT(&xs); |
| 443 |
assert(xs.impl.capacity == LIST_SIZE(&xs)); |
| 444 |
|
| 445 |
LIST_FREE(&xs); |
| 446 |
} |
| 447 |
|
| 448 |
static void test_shrink_empty(void) { |
| 449 |
LIST(int) xs = {0}; |
| 450 |
LIST_SHRINK_TO_FIT(&xs); |
| 451 |
assert(xs.impl.capacity == 0); |
| 452 |
LIST_FREE(&xs); |
| 453 |
} |
| 454 |
|
| 455 |
static void test_free(void) { |
| 456 |
LIST(int) xs = {0}; |
| 457 |
for (size_t i = 0; i < 10; ++i) { |
| 458 |
LIST_APPEND(&xs, (int)i); |
| 459 |
} |
| 460 |
|
| 461 |
LIST_FREE(&xs); |
| 462 |
assert(LIST_SIZE(&xs) == 0); |
| 463 |
assert(xs.impl.capacity == 0); |
| 464 |
} |
| 465 |
|
| 466 |
static void test_push_back(void) { |
| 467 |
LIST(int) xs = {0}; |
| 468 |
LIST(int) ys = {0}; |
| 469 |
|
| 470 |
for (size_t i = 0; i < 10; ++i) { |
| 471 |
LIST_APPEND(&xs, (int)i); |
| 472 |
LIST_PUSH_BACK(&ys, (int)i); |
| 473 |
assert(LIST_SIZE(&xs) == LIST_SIZE(&ys)); |
| 474 |
for (size_t j = 0; j <= i; ++j) { |
| 475 |
assert(LIST_GET(&xs, j) == LIST_GET(&ys, j)); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
LIST_FREE(&ys); |
| 480 |
LIST_FREE(&xs); |
| 481 |
} |
| 482 |
|
| 483 |
static void test_pop_back(void) { |
| 484 |
LIST(int) xs = {0}; |
| 485 |
|
| 486 |
for (size_t i = 0; i < 10; ++i) { |
| 487 |
LIST_PUSH_BACK(&xs, (int)i); |
| 488 |
} |
| 489 |
for (size_t i = 0; i < 10; ++i) { |
| 490 |
assert(LIST_SIZE(&xs) == 10 - i); |
| 491 |
int x = LIST_POP_BACK(&xs); |
| 492 |
assert(x == 10 - (int)i - 1); |
| 493 |
} |
| 494 |
|
| 495 |
for (size_t i = 0; i < 10; ++i) { |
| 496 |
LIST_PUSH_BACK(&xs, (int)i); |
| 497 |
(void)LIST_POP_BACK(&xs); |
| 498 |
assert(LIST_IS_EMPTY(&xs)); |
| 499 |
} |
| 500 |
|
| 501 |
LIST_FREE(&xs); |
| 502 |
} |
| 503 |
|
| 504 |
static void test_large(void) { |
| 505 |
LIST(int) xs = {0}; |
| 506 |
|
| 507 |
for (int i = 0; i < 5000; ++i) { |
| 508 |
LIST_APPEND(&xs, i); |
| 509 |
} |
| 510 |
for (size_t i = 0; i < 5000; ++i) { |
| 511 |
assert(LIST_GET(&xs, i) == (int)i); |
| 512 |
} |
| 513 |
|
| 514 |
LIST_FREE(&xs); |
| 515 |
} |
| 516 |
|
| 517 |
static void test_detach(void) { |
| 518 |
LIST(int) xs = {0}; |
| 519 |
for (size_t i = 0; i < 10; ++i) { |
| 520 |
LIST_APPEND(&xs, (int)i); |
| 521 |
} |
| 522 |
|
| 523 |
int *ys; |
| 524 |
size_t ys_size; |
| 525 |
LIST_DETACH(&xs, &ys, &ys_size); |
| 526 |
assert(ys != NULL); |
| 527 |
assert(ys_size == 10); |
| 528 |
assert(LIST_IS_EMPTY(&xs)); |
| 529 |
|
| 530 |
for (size_t i = 0; i < 10; ++i) { |
| 531 |
assert(ys[i] == (int)i); |
| 532 |
} |
| 533 |
|
| 534 |
free(ys); |
| 535 |
} |
| 536 |
|
| 537 |
static void test_dtor(void) { |
| 538 |
|
| 539 |
// setup a list with a non-trivial destructor |
| 540 |
LIST(char *) xs = {.dtor = LIST_DTOR_FREE}; |
| 541 |
|
| 542 |
for (size_t i = 0; i < 10; ++i) { |
| 543 |
char *hello = strdup("hello"); |
| 544 |
assert(hello != NULL); |
| 545 |
LIST_APPEND(&xs, hello); |
| 546 |
} |
| 547 |
|
| 548 |
for (size_t i = 0; i < 10; ++i) { |
| 549 |
assert(strcmp(LIST_GET(&xs, i), "hello") == 0); |
| 550 |
} |
| 551 |
|
| 552 |
LIST_FREE(&xs); |
| 553 |
} |
| 554 |
|
| 555 |
/// test removal does not leak memory |
| 556 |
static void test_remove_with_dtor(void) { |
| 557 |
LIST(char *) xs = {.dtor = LIST_DTOR_FREE}; |
| 558 |
|
| 559 |
char *hello = strdup("hello"); |
| 560 |
assert(hello != NULL); |
| 561 |
|
| 562 |
LIST_APPEND(&xs, hello); |
| 563 |
LIST_REMOVE(&xs, hello); |
| 564 |
assert(LIST_SIZE(&xs) == 0); |
| 565 |
|
| 566 |
LIST_FREE(&xs); |
| 567 |
} |
| 568 |
|
| 569 |
#ifndef BAD_TEST |
| 570 |
#define BAD_TEST 0 |
| 571 |
#endif |
| 572 |
|
| 573 |
#if BAD_TEST == 1 |
| 574 |
/// appending a struct to an int list should fail to compile |
| 575 |
static UNUSED void test_bad_append1(void) { |
| 576 |
LIST(int) xs = {0}; |
| 577 |
|
| 578 |
struct foo { |
| 579 |
int x; |
| 580 |
}; |
| 581 |
struct foo y = {0}; |
| 582 |
|
| 583 |
LIST_APPEND(&xs, y); |
| 584 |
} |
| 585 |
#endif |
| 586 |
|
| 587 |
#if BAD_TEST == 2 |
| 588 |
/// appending an int to a struct list should fail to compile |
| 589 |
static UNUSED void test_bad_append2(void) { |
| 590 |
struct foo { |
| 591 |
int x; |
| 592 |
}; |
| 593 |
LIST(struct foo) xs = {0}; |
| 594 |
|
| 595 |
LIST_APPEND(&xs, 1); |
| 596 |
} |
| 597 |
#endif |
| 598 |
|
| 599 |
#if BAD_TEST == 3 |
| 600 |
/// getter of an int list should return an int-typed value |
| 601 |
static UNUSED void test_bad_get1(void) { |
| 602 |
LIST(int) xs = {0}; |
| 603 |
LIST_APPEND(&xs, 1); |
| 604 |
|
| 605 |
struct foo { |
| 606 |
int x; |
| 607 |
}; |
| 608 |
struct foo y UNUSED = LIST_GET(&xs, 0); |
| 609 |
} |
| 610 |
#endif |
| 611 |
|
| 612 |
#if BAD_TEST == 4 |
| 613 |
/// getter of an struct list should return an struct-typed value |
| 614 |
static UNUSED void test_bad_get2(void) { |
| 615 |
struct foo { |
| 616 |
int x; |
| 617 |
}; |
| 618 |
LIST(struct foo) xs = {0}; |
| 619 |
|
| 620 |
struct foo y = {1}; |
| 621 |
LIST_APPEND(&xs, y); |
| 622 |
|
| 623 |
int x UNUSED = LIST_GET(&xs, 0); |
| 624 |
} |
| 625 |
#endif |
| 626 |
|
| 627 |
#if BAD_TEST == 5 |
| 628 |
/// `at` of an int list should return an int-typed pointer |
| 629 |
static UNUSED void test_bad_get1(void) { |
| 630 |
LIST(int) xs = {0}; |
| 631 |
LIST_APPEND(&xs, 1); |
| 632 |
|
| 633 |
struct foo { |
| 634 |
int x; |
| 635 |
}; |
| 636 |
// upgrade incompatible pointer assignments into a compiler error |
| 637 |
#ifdef __GNUC__ |
| 638 |
#pragma GCC diagnostic push |
| 639 |
#pragma GCC diagnostic error "-Wincompatible-pointer-types" |
| 640 |
#endif |
| 641 |
struct foo *y UNUSED = LIST_AT(&xs, 0); |
| 642 |
#ifdef __GNUC__ |
| 643 |
#pragma GCC diagnostic pop |
| 644 |
#endif |
| 645 |
} |
| 646 |
#endif |
| 647 |
|
| 648 |
#if BAD_TEST == 6 |
| 649 |
/// `at` of a struct list should return an struct-typed pointer |
| 650 |
static UNUSED void test_bad_get2(void) { |
| 651 |
struct foo { |
| 652 |
int x; |
| 653 |
}; |
| 654 |
LIST(struct foo) xs = {0}; |
| 655 |
|
| 656 |
struct foo y = {1}; |
| 657 |
LIST_APPEND(&xs, y); |
| 658 |
|
| 659 |
// upgrade incompatible pointer assignments into a compiler error |
| 660 |
#ifdef __GNUC__ |
| 661 |
#pragma GCC diagnostic push |
| 662 |
#pragma GCC diagnostic error "-Wincompatible-pointer-types" |
| 663 |
#endif |
| 664 |
int *x UNUSED = LIST_AT(&xs, 0); |
| 665 |
#ifdef __GNUC__ |
| 666 |
#pragma GCC diagnostic pop |
| 667 |
#endif |
| 668 |
} |
| 669 |
#endif |
| 670 |
|
| 671 |
/// test removal does not leak memory |
| 672 |
int main(void) { |
| 673 |
|
| 674 |
#define RUN(t) \ |
| 675 |
do { \ |
| 676 |
printf("running test_%s... ", #t); \ |
| 677 |
fflush(stdout); \ |
| 678 |
test_##t(); \ |
| 679 |
printf("OK\n"); \ |
| 680 |
} while (0) |
| 681 |
|
| 682 |
RUN(create_reset); |
| 683 |
RUN(init); |
| 684 |
RUN(init_reset); |
| 685 |
RUN(append); |
| 686 |
RUN(2734_append); |
| 687 |
RUN(prepend_0); |
| 688 |
RUN(2734_try_append); |
| 689 |
RUN(append_prepend); |
| 690 |
RUN(2734_prepend); |
| 691 |
RUN(get); |
| 692 |
RUN(set); |
| 693 |
RUN(remove_empty); |
| 694 |
RUN(remove); |
| 695 |
RUN(at); |
| 696 |
RUN(clear_empty); |
| 697 |
RUN(clear); |
| 698 |
RUN(push_one); |
| 699 |
RUN(push_then_pop_ten); |
| 700 |
RUN(push_then_pop_many); |
| 701 |
RUN(push_pop_interleaved); |
| 702 |
RUN(sort_empty); |
| 703 |
RUN(sort); |
| 704 |
RUN(sort_sorted); |
| 705 |
RUN(sort_complex); |
| 706 |
RUN(shrink); |
| 707 |
RUN(shrink_empty); |
| 708 |
RUN(free); |
| 709 |
RUN(push_back); |
| 710 |
RUN(pop_back); |
| 711 |
RUN(large); |
| 712 |
RUN(detach); |
| 713 |
RUN(dtor); |
| 714 |
RUN(remove_with_dtor); |
| 715 |
|
| 716 |
#undef RUN |
| 717 |
|
| 718 |
return EXIT_SUCCESS; |
| 719 |
} |