tests: list: add test case for list_empty iterator

Increasing unit testing code coverage.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
This commit is contained in:
Petr Štetiar 2019-12-19 11:49:39 +01:00
parent 7da66430de
commit 7c4ef0d9ae
2 changed files with 54 additions and 18 deletions

View file

@ -2,9 +2,9 @@ check that list is producing expected results:
$ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH" $ [ -n "$TEST_BIN_DIR" ] && export PATH="$TEST_BIN_DIR:$PATH"
$ valgrind --quiet --leak-check=full test-list $ valgrind --quiet --leak-check=full test-list
test_basics: list_empty: yes init_list: list_empty: yes
test_basics: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve init_list: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve
test_basics: list_empty: no init_list: list_empty: no
test_basics: first=zero last=twelve test_basics: first=zero last=twelve
test_basics: 'zero' is first, yes test_basics: 'zero' is first, yes
test_basics: 'twelve' is last, yes test_basics: 'twelve' is last, yes
@ -20,11 +20,16 @@ check that list is producing expected results:
test_basics: list_for_each_entry_reverse: one eleven ten nine eight seven six five four three two test_basics: list_for_each_entry_reverse: one eleven ten nine eight seven six five four three two
test_basics: delete all entries test_basics: delete all entries
test_basics: list_empty: yes test_basics: list_empty: yes
init_list: list_empty: yes
init_list: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve
init_list: list_empty: no
test_while_list_empty: delete all entries
test_while_list_empty: list_empty: yes
$ test-list-san $ test-list-san
test_basics: list_empty: yes init_list: list_empty: yes
test_basics: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve init_list: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve
test_basics: list_empty: no init_list: list_empty: no
test_basics: first=zero last=twelve test_basics: first=zero last=twelve
test_basics: 'zero' is first, yes test_basics: 'zero' is first, yes
test_basics: 'twelve' is last, yes test_basics: 'twelve' is last, yes
@ -40,3 +45,8 @@ check that list is producing expected results:
test_basics: list_for_each_entry_reverse: one eleven ten nine eight seven six five four three two test_basics: list_for_each_entry_reverse: one eleven ten nine eight seven six five four three two
test_basics: delete all entries test_basics: delete all entries
test_basics: list_empty: yes test_basics: list_empty: yes
init_list: list_empty: yes
init_list: list_add_tail: zero one two three four five six seven eight nine ten eleven twelve
init_list: list_empty: no
test_while_list_empty: delete all entries
test_while_list_empty: list_empty: yes

View file

@ -14,30 +14,34 @@ struct item {
fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \ fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
} while (0); } while (0);
static void test_basics() static void init_list(struct list_head *list)
{ {
size_t i;
struct item *tmp;
struct item *item;
struct item *last;
struct item *first;
static struct list_head test_list = LIST_HEAD_INIT(test_list);
const char *vals[] = { const char *vals[] = {
"zero", "one", "two", "three", "four", "five", "six", "zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve" "seven", "eight", "nine", "ten", "eleven", "twelve"
}; };
OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
OUT("list_add_tail: "); OUT("list_add_tail: ");
for (i=0; i<ARRAY_SIZE(vals); i++) { for (size_t i=0; i<ARRAY_SIZE(vals); i++) {
struct item *e = malloc(sizeof(struct item)); struct item *e = malloc(sizeof(struct item));
e->name = vals[i]; e->name = vals[i];
list_add_tail(&e->list, &test_list); list_add_tail(&e->list, list);
fprintf(stdout, "%s ", vals[i]); fprintf(stdout, "%s ", vals[i]);
} }
fprintf(stdout, "\n"); fprintf(stdout, "\n");
OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
}
static void test_basics()
{
struct item *tmp;
struct item *item;
struct item *last;
struct item *first;
struct list_head test_list = LIST_HEAD_INIT(test_list);
init_list(&test_list);
first = list_first_entry(&test_list, struct item, list); first = list_first_entry(&test_list, struct item, list);
last = list_last_entry(&test_list, struct item, list); last = list_last_entry(&test_list, struct item, list);
@ -50,8 +54,13 @@ static void test_basics()
list_del(&last->list); list_del(&last->list);
free(first); free(first);
free(last); free(last);
first = list_first_entry(&test_list, struct item, list); first = list_first_entry(&test_list, struct item, list);
last = list_last_entry(&test_list, struct item, list); last = list_last_entry(&test_list, struct item, list);
if (!first || !last)
return;
OUT("first=%s last=%s\n", first->name, last->name); OUT("first=%s last=%s\n", first->name, last->name);
OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no"); OUT("'one' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no"); OUT("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
@ -84,8 +93,25 @@ static void test_basics()
OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no"); OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
} }
static void test_while_list_empty()
{
struct item *first;
struct list_head test_list = LIST_HEAD_INIT(test_list);
init_list(&test_list);
OUT("delete all entries\n");
while (!list_empty(&test_list)) {
first = list_first_entry(&test_list, struct item, list);
list_del(&first->list);
free(first);
}
OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
}
int main() int main()
{ {
test_basics(); test_basics();
test_while_list_empty();
return 0; return 0;
} }