blob: make blob_parse_untrusted more permissive
[project/libubox.git] / tests / test-list.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "list.h"
6 #include "utils.h"
7
8 struct item {
9 const char *name;
10 struct list_head list;
11 };
12
13 #define OUT(fmt, ...) do { \
14 fprintf(stdout, "%s: " fmt, __func__, ## __VA_ARGS__); \
15 } while (0);
16
17 static void init_list(struct list_head *list)
18 {
19 const char *vals[] = {
20 "zero", "one", "two", "three", "four", "five", "six",
21 "seven", "eight", "nine", "ten", "eleven", "twelve"
22 };
23
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));
28 e->name = vals[i];
29 list_add_tail(&e->list, list);
30 fprintf(stdout, "%s ", vals[i]);
31 }
32 fprintf(stdout, "\n");
33 OUT("list_empty: %s\n", list_empty(list) ? "yes" : "no");
34 }
35
36 static void test_basics()
37 {
38 struct item *tmp;
39 struct item *item;
40 struct item *last;
41 struct item *first;
42 struct list_head test_list = LIST_HEAD_INIT(test_list);
43
44 init_list(&test_list);
45
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");
51
52 OUT("removing 'twelve' and 'zero'\n");
53 list_del(&first->list);
54 list_del(&last->list);
55 free(first);
56 free(last);
57
58 first = list_first_entry(&test_list, struct item, list);
59 last = list_last_entry(&test_list, struct item, list);
60
61 if (!first || !last)
62 return;
63
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");
67
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");
75
76 OUT("list_for_each_entry: ");
77 list_for_each_entry(item, &test_list, list) {
78 fprintf(stdout, "%s ", item->name);
79 }
80 fprintf(stdout, "\n");
81
82 OUT("list_for_each_entry_reverse: ");
83 list_for_each_entry_reverse(item, &test_list, list) {
84 fprintf(stdout, "%s ", item->name);
85 }
86 fprintf(stdout, "\n");
87
88 OUT("delete all entries\n");
89 list_for_each_entry_safe(item, tmp, &test_list, list) {
90 list_del(&item->list);
91 free(item);
92 }
93 OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
94 }
95
96 static void test_while_list_empty()
97 {
98 struct item *first;
99 struct list_head test_list = LIST_HEAD_INIT(test_list);
100
101 init_list(&test_list);
102
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);
107 free(first);
108 }
109 OUT("list_empty: %s\n", list_empty(&test_list) ? "yes" : "no");
110 }
111
112 int main()
113 {
114 test_basics();
115 test_while_list_empty();
116 return 0;
117 }