10 struct list_head list
;
13 #define OUT(fmt, ...) do { \
14 fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
17 static void init_list(struct list_head
*list
)
19 const char *vals
[] = {
20 "zero", "one", "two", "three", "four", "five", "six",
21 "seven", "eight", "nine", "ten", "eleven", "twelve"
24 OUT("list_empty: %s\n", list_empty(list
) ? "yes" : "no");
25 OUT("list_add_tail: ");
26 for (size_t i
=0; i
<ARRAY_SIZE(vals
); i
++) {
27 struct item
*e
= malloc(sizeof(struct item
));
29 list_add_tail(&e
->list
, list
);
30 fprintf(stdout
, "%s ", vals
[i
]);
32 fprintf(stdout
, "\n");
33 OUT("list_empty: %s\n", list_empty(list
) ? "yes" : "no");
36 static void test_basics()
42 struct list_head test_list
= LIST_HEAD_INIT(test_list
);
44 init_list(&test_list
);
46 first
= list_first_entry(&test_list
, struct item
, list
);
47 last
= list_last_entry(&test_list
, struct item
, list
);
48 OUT("first=%s last=%s\n", first
->name
, last
->name
);
49 OUT("'zero' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
50 OUT("'twelve' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
52 OUT("removing 'twelve' and 'zero'\n");
53 list_del(&first
->list
);
54 list_del(&last
->list
);
58 first
= list_first_entry(&test_list
, struct item
, list
);
59 last
= list_last_entry(&test_list
, struct item
, list
);
64 OUT("first=%s last=%s\n", first
->name
, last
->name
);
65 OUT("'one' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
66 OUT("'eleven' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
68 OUT("moving 'one' to the tail\n");
69 list_move_tail(&first
->list
, &test_list
);
70 first
= list_first_entry(&test_list
, struct item
, list
);
71 last
= list_last_entry(&test_list
, struct item
, list
);
72 OUT("first=%s last=%s\n", first
->name
, last
->name
);
73 OUT("'two' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
74 OUT("'one' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
76 OUT("list_for_each_entry: ");
77 list_for_each_entry(item
, &test_list
, list
) {
78 fprintf(stdout
, "%s ", item
->name
);
80 fprintf(stdout
, "\n");
82 OUT("list_for_each_entry_reverse: ");
83 list_for_each_entry_reverse(item
, &test_list
, list
) {
84 fprintf(stdout
, "%s ", item
->name
);
86 fprintf(stdout
, "\n");
88 OUT("delete all entries\n");
89 list_for_each_entry_safe(item
, tmp
, &test_list
, list
) {
90 list_del(&item
->list
);
93 OUT("list_empty: %s\n", list_empty(&test_list
) ? "yes" : "no");
96 static void test_while_list_empty()
99 struct list_head test_list
= LIST_HEAD_INIT(test_list
);
101 init_list(&test_list
);
103 OUT("delete all entries\n");
104 while (!list_empty(&test_list
)) {
105 first
= list_first_entry(&test_list
, struct item
, list
);
106 list_del(&first
->list
);
109 OUT("list_empty: %s\n", list_empty(&test_list
) ? "yes" : "no");
115 test_while_list_empty();