Skip to content

Commit 04539d9

Browse files
committed
test list.h
1 parent 71e9416 commit 04539d9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

test/test.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "endian.h"
1313
#include "idalloc.h"
1414
#include "leb128.h"
15+
#include "list.h"
1516
#include "timeutil.h"
1617
#include "type.h"
1718

@@ -601,6 +602,66 @@ test_timeutil_int64(void **state)
601602
assert_int_equal(a.tv_nsec, UINT64_MAX % 1000000000);
602603
}
603604

605+
void
606+
test_list(void **state)
607+
{
608+
struct item {
609+
int dummy1;
610+
LIST_ENTRY(struct item) entry;
611+
int dummy2;
612+
};
613+
LIST_HEAD(struct item) h;
614+
615+
LIST_HEAD_INIT(&h);
616+
assert_true(LIST_EMPTY(&h));
617+
assert_null(LIST_FIRST(&h));
618+
619+
struct item item;
620+
LIST_INSERT_TAIL(&h, &item, entry);
621+
assert_false(LIST_EMPTY(&h));
622+
assert_ptr_equal(LIST_FIRST(&h), &item);
623+
LIST_REMOVE(&h, &item, entry);
624+
assert_true(LIST_EMPTY(&h));
625+
assert_null(LIST_FIRST(&h));
626+
627+
struct item items[10];
628+
int i;
629+
for (i = 0; i < 10; i++) {
630+
LIST_INSERT_TAIL(&h, &items[i], entry);
631+
assert_false(LIST_EMPTY(&h));
632+
}
633+
assert_ptr_equal(LIST_FIRST(&h), &items[0]);
634+
635+
struct item *it;
636+
i = 0;
637+
LIST_FOREACH(it, &h, entry) {
638+
assert_int_equal(it - items, i);
639+
i++;
640+
}
641+
642+
LIST_REMOVE(&h, &items[0], entry);
643+
LIST_REMOVE(&h, &items[2], entry);
644+
LIST_REMOVE(&h, &items[4], entry);
645+
LIST_REMOVE(&h, &items[8], entry);
646+
LIST_REMOVE(&h, &items[6], entry);
647+
648+
i = 0;
649+
LIST_FOREACH(it, &h, entry) {
650+
assert_int_equal(it - items, i * 2 + 1);
651+
i++;
652+
}
653+
assert_int_equal(i, 5);
654+
655+
i = 0;
656+
while ((it = LIST_FIRST(&h)) != NULL) {
657+
assert_int_equal(it - items, i * 2 + 1);
658+
i++;
659+
LIST_REMOVE(&h, it, entry);
660+
}
661+
assert_int_equal(i, 5);
662+
assert_true(LIST_EMPTY(&h));
663+
}
664+
604665
int
605666
main(int argc, char **argv)
606667
{
@@ -611,6 +672,7 @@ main(int argc, char **argv)
611672
cmocka_unit_test(test_idalloc),
612673
cmocka_unit_test(test_timeutil),
613674
cmocka_unit_test(test_timeutil_int64),
675+
cmocka_unit_test(test_list),
614676
};
615677
return cmocka_run_group_tests(tests, NULL, NULL);
616678
}

0 commit comments

Comments
 (0)