10 struct list_head list
;
13 #define OUT(fmt, ...) do { \
14 fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
17 static void test_basics()
24 static struct list_head test_list
= LIST_HEAD_INIT(test_list
);
26 const char *vals
[] = {
27 "zero", "one", "two", "three", "four", "five", "six",
28 "seven", "eight", "nine", "ten", "eleven", "twelve"
31 OUT("list_empty: %s\n", list_empty(&test_list
) ? "yes" : "no");
32 OUT("list_add_tail: ");
33 for (i
=0; i
<ARRAY_SIZE(vals
); i
++) {
34 struct item
*e
= malloc(sizeof(struct item
));
36 list_add_tail(&e
->list
, &test_list
);
37 fprintf(stdout
, "%s ", vals
[i
]);
39 fprintf(stdout
, "\n");
40 OUT("list_empty: %s\n", list_empty(&test_list
) ? "yes" : "no");
42 first
= list_first_entry(&test_list
, struct item
, list
);
43 last
= list_last_entry(&test_list
, struct item
, list
);
44 OUT("first=%s last=%s\n", first
->name
, last
->name
);
45 OUT("'zero' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
46 OUT("'twelve' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
48 OUT("removing 'twelve' and 'zero'\n");
49 list_del(&first
->list
);
50 list_del(&last
->list
);
53 first
= list_first_entry(&test_list
, struct item
, list
);
54 last
= list_last_entry(&test_list
, struct item
, list
);
55 OUT("first=%s last=%s\n", first
->name
, last
->name
);
56 OUT("'one' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
57 OUT("'eleven' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
59 OUT("moving 'one' to the tail\n");
60 list_move_tail(&first
->list
, &test_list
);
61 first
= list_first_entry(&test_list
, struct item
, list
);
62 last
= list_last_entry(&test_list
, struct item
, list
);
63 OUT("first=%s last=%s\n", first
->name
, last
->name
);
64 OUT("'two' is first, %s\n", list_is_first(&first
->list
, &test_list
) ? "yes" : "no");
65 OUT("'one' is last, %s\n", list_is_last(&last
->list
, &test_list
) ? "yes" : "no");
67 OUT("list_for_each_entry: ");
68 list_for_each_entry(item
, &test_list
, list
) {
69 fprintf(stdout
, "%s ", item
->name
);
71 fprintf(stdout
, "\n");
73 OUT("list_for_each_entry_reverse: ");
74 list_for_each_entry_reverse(item
, &test_list
, list
) {
75 fprintf(stdout
, "%s ", item
->name
);
77 fprintf(stdout
, "\n");
79 OUT("delete all entries\n");
80 list_for_each_entry_safe(item
, tmp
, &test_list
, list
) {
81 list_del(&item
->list
);
84 OUT("list_empty: %s\n", list_empty(&test_list
) ? "yes" : "no");