2019-11-19 14:31:44 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "list.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
struct item {
|
|
|
|
const char *name;
|
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define OUT(fmt, ...) do { \
|
|
|
|
fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
|
|
|
|
} while (0);
|
|
|
|
|
2019-12-19 11:49:39 +01:00
|
|
|
static void init_list(struct list_head *list)
|
2019-11-19 14:31:44 +01:00
|
|
|
{
|
|
|
|
const char *vals[] = {
|
|
|
|
"zero", "one", "two", "three", "four", "five", "six",
|
|
|
|
"seven", "eight", "nine", "ten", "eleven", "twelve"
|
|
|
|
};
|
|
|
|
|
2019-12-19 11:49:39 +01:00
|
|
|
OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
|
2019-11-19 14:31:44 +01:00
|
|
|
OUT("list_add_tail: ");
|
2019-12-19 11:49:39 +01:00
|
|
|
for (size_t i=0; i<ARRAY_SIZE(vals); i++) {
|
2019-11-19 14:31:44 +01:00
|
|
|
struct item *e = malloc(sizeof(struct item));
|
|
|
|
e->name = vals[i];
|
2019-12-19 11:49:39 +01:00
|
|
|
list_add_tail(&e->list, list);
|
2019-11-19 14:31:44 +01:00
|
|
|
fprintf(stdout, "%s ", vals[i]);
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
2019-12-19 11:49:39 +01:00
|
|
|
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);
|
2019-11-19 14:31:44 +01:00
|
|
|
|
|
|
|
first = list_first_entry(&test_list, struct item, list);
|
|
|
|
last = list_last_entry(&test_list, struct item, list);
|
|
|
|
OUT("first=%s last=%s\n", first->name, last->name);
|
|
|
|
OUT("'zero' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
|
|
|
|
OUT("'twelve' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
|
|
|
|
|
|
|
|
OUT("removing 'twelve' and 'zero'\n");
|
|
|
|
list_del(&first->list);
|
|
|
|
list_del(&last->list);
|
|
|
|
free(first);
|
|
|
|
free(last);
|
2019-12-19 11:49:39 +01:00
|
|
|
|
2019-11-19 14:31:44 +01:00
|
|
|
first = list_first_entry(&test_list, struct item, list);
|
|
|
|
last = list_last_entry(&test_list, struct item, list);
|
2019-12-19 11:49:39 +01:00
|
|
|
|
|
|
|
if (!first || !last)
|
|
|
|
return;
|
|
|
|
|
2019-11-19 14:31:44 +01:00
|
|
|
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("'eleven' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
|
|
|
|
|
|
|
|
OUT("moving 'one' to the tail\n");
|
|
|
|
list_move_tail(&first->list, &test_list);
|
|
|
|
first = list_first_entry(&test_list, struct item, list);
|
|
|
|
last = list_last_entry(&test_list, struct item, list);
|
|
|
|
OUT("first=%s last=%s\n", first->name, last->name);
|
|
|
|
OUT("'two' is first, %s\n", list_is_first(&first->list, &test_list) ? "yes" : "no");
|
|
|
|
OUT("'one' is last, %s\n", list_is_last(&last->list, &test_list) ? "yes" : "no");
|
|
|
|
|
|
|
|
OUT("list_for_each_entry: ");
|
|
|
|
list_for_each_entry(item, &test_list, list) {
|
|
|
|
fprintf(stdout, "%s ", item->name);
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
OUT("list_for_each_entry_reverse: ");
|
|
|
|
list_for_each_entry_reverse(item, &test_list, list) {
|
|
|
|
fprintf(stdout, "%s ", item->name);
|
|
|
|
}
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
|
|
|
|
OUT("delete all entries\n");
|
|
|
|
list_for_each_entry_safe(item, tmp, &test_list, list) {
|
|
|
|
list_del(&item->list);
|
|
|
|
free(item);
|
|
|
|
}
|
|
|
|
OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
|
|
|
|
}
|
|
|
|
|
2019-12-19 11:49:39 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-11-19 14:31:44 +01:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
test_basics();
|
2019-12-19 11:49:39 +01:00
|
|
|
test_while_list_empty();
|
2019-11-19 14:31:44 +01:00
|
|
|
return 0;
|
|
|
|
}
|