~pmdj/ubuntu/trusty/qemu/2.9+applesmc+fadtv3

« back to all changes in this revision

Viewing changes to roms/skiboot/ccan/list/test/run.c

  • Committer: Phil Dennis-Jordan
  • Date: 2017-07-21 08:03:43 UTC
  • mfrom: (1.1.1)
  • Revision ID: phil@philjordan.eu-20170721080343-2yr2vdj7713czahv
New upstream release 2.9.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <ccan/list/test/helper.c>
 
2
#include <ccan/list/list.h>
 
3
#include <ccan/tap/tap.h>
 
4
#include <ccan/list/list.c>
 
5
#include "helper.h"
 
6
 
 
7
struct parent {
 
8
        const char *name;
 
9
        struct list_head children;
 
10
        unsigned int num_children;
 
11
};
 
12
 
 
13
struct child {
 
14
        const char *name;
 
15
        struct list_node list;
 
16
};
 
17
 
 
18
static LIST_HEAD(static_list);
 
19
 
 
20
int main(int argc, char *argv[])
 
21
{
 
22
        struct parent parent;
 
23
        struct child c1, c2, c3, *c, *n;
 
24
        unsigned int i;
 
25
        struct list_head list = LIST_HEAD_INIT(list);
 
26
        opaque_t *q, *nq;
 
27
        struct list_head opaque_list = LIST_HEAD_INIT(opaque_list);
 
28
 
 
29
        (void)argc;
 
30
        (void)argv;
 
31
 
 
32
        plan_tests(65);
 
33
        /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
 
34
        ok1(list_empty(&static_list));
 
35
        ok1(list_check(&static_list, NULL));
 
36
        ok1(list_empty(&list));
 
37
        ok1(list_check(&list, NULL));
 
38
 
 
39
        parent.num_children = 0;
 
40
        list_head_init(&parent.children);
 
41
        /* Test list_head_init */
 
42
        ok1(list_empty(&parent.children));
 
43
        ok1(list_check(&parent.children, NULL));
 
44
 
 
45
        c2.name = "c2";
 
46
        list_add(&parent.children, &c2.list);
 
47
        /* Test list_add and !list_empty. */
 
48
        ok1(!list_empty(&parent.children));
 
49
        ok1(c2.list.next == &parent.children.n);
 
50
        ok1(c2.list.prev == &parent.children.n);
 
51
        ok1(parent.children.n.next == &c2.list);
 
52
        ok1(parent.children.n.prev == &c2.list);
 
53
        /* Test list_check */
 
54
        ok1(list_check(&parent.children, NULL));
 
55
 
 
56
        c1.name = "c1";
 
57
        list_add(&parent.children, &c1.list);
 
58
        /* Test list_add and !list_empty. */
 
59
        ok1(!list_empty(&parent.children));
 
60
        ok1(c2.list.next == &parent.children.n);
 
61
        ok1(c2.list.prev == &c1.list);
 
62
        ok1(parent.children.n.next == &c1.list);
 
63
        ok1(parent.children.n.prev == &c2.list);
 
64
        ok1(c1.list.next == &c2.list);
 
65
        ok1(c1.list.prev == &parent.children.n);
 
66
        /* Test list_check */
 
67
        ok1(list_check(&parent.children, NULL));
 
68
 
 
69
        c3.name = "c3";
 
70
        list_add_tail(&parent.children, &c3.list);
 
71
        /* Test list_add_tail and !list_empty. */
 
72
        ok1(!list_empty(&parent.children));
 
73
        ok1(parent.children.n.next == &c1.list);
 
74
        ok1(parent.children.n.prev == &c3.list);
 
75
        ok1(c1.list.next == &c2.list);
 
76
        ok1(c1.list.prev == &parent.children.n);
 
77
        ok1(c2.list.next == &c3.list);
 
78
        ok1(c2.list.prev == &c1.list);
 
79
        ok1(c3.list.next == &parent.children.n);
 
80
        ok1(c3.list.prev == &c2.list);
 
81
        /* Test list_check */
 
82
        ok1(list_check(&parent.children, NULL));
 
83
 
 
84
        /* Test list_check_node */
 
85
        ok1(list_check_node(&c1.list, NULL));
 
86
        ok1(list_check_node(&c2.list, NULL));
 
87
        ok1(list_check_node(&c3.list, NULL));
 
88
 
 
89
        /* Test list_top */
 
90
        ok1(list_top(&parent.children, struct child, list) == &c1);
 
91
 
 
92
        /* Test list_tail */
 
93
        ok1(list_tail(&parent.children, struct child, list) == &c3);
 
94
 
 
95
        /* Test list_for_each. */
 
96
        i = 0;
 
97
        list_for_each(&parent.children, c, list) {
 
98
                switch (i++) {
 
99
                case 0:
 
100
                        ok1(c == &c1);
 
101
                        break;
 
102
                case 1:
 
103
                        ok1(c == &c2);
 
104
                        break;
 
105
                case 2:
 
106
                        ok1(c == &c3);
 
107
                        break;
 
108
                }
 
109
                if (i > 2)
 
110
                        break;
 
111
        }
 
112
        ok1(i == 3);
 
113
 
 
114
        /* Test list_for_each_rev. */
 
115
        i = 0;
 
116
        list_for_each_rev(&parent.children, c, list) {
 
117
                switch (i++) {
 
118
                case 0:
 
119
                        ok1(c == &c3);
 
120
                        break;
 
121
                case 1:
 
122
                        ok1(c == &c2);
 
123
                        break;
 
124
                case 2:
 
125
                        ok1(c == &c1);
 
126
                        break;
 
127
                }
 
128
                if (i > 2)
 
129
                        break;
 
130
        }
 
131
        ok1(i == 3);
 
132
 
 
133
        /* Test list_for_each_safe, list_del and list_del_from. */
 
134
        i = 0;
 
135
        list_for_each_safe(&parent.children, c, n, list) {
 
136
                switch (i++) {
 
137
                case 0:
 
138
                        ok1(c == &c1);  
 
139
                        list_del(&c->list);
 
140
                        break;
 
141
                case 1:
 
142
                        ok1(c == &c2);
 
143
                        list_del_from(&parent.children, &c->list);
 
144
                        break;
 
145
                case 2:
 
146
                        ok1(c == &c3);
 
147
                        list_del_from(&parent.children, &c->list);
 
148
                        break;
 
149
                }
 
150
                ok1(list_check(&parent.children, NULL));
 
151
                if (i > 2)
 
152
                        break;
 
153
        }
 
154
        ok1(i == 3);
 
155
        ok1(list_empty(&parent.children));
 
156
 
 
157
        /* Test list_for_each_off. */
 
158
        list_add_tail(&opaque_list,
 
159
                      (struct list_node *)create_opaque_blob());
 
160
        list_add_tail(&opaque_list,
 
161
                      (struct list_node *)create_opaque_blob());
 
162
        list_add_tail(&opaque_list,
 
163
                      (struct list_node *)create_opaque_blob());
 
164
 
 
165
        i = 0;
 
166
 
 
167
        list_for_each_off(&opaque_list, q, 0) {
 
168
          i++;
 
169
          ok1(if_blobs_know_the_secret(q));
 
170
        }
 
171
        ok1(i == 3);
 
172
 
 
173
        /* Test list_for_each_safe_off, list_del_off and list_del_from_off. */
 
174
        i = 0;
 
175
        list_for_each_safe_off(&opaque_list, q, nq, 0) {
 
176
                switch (i++) {
 
177
                case 0:
 
178
                        ok1(if_blobs_know_the_secret(q));
 
179
                        list_del_off(q, 0);
 
180
                        destroy_opaque_blob(q);
 
181
                        break;
 
182
                case 1:
 
183
                        ok1(if_blobs_know_the_secret(q));
 
184
                        list_del_from_off(&opaque_list, q, 0);
 
185
                        destroy_opaque_blob(q);
 
186
                        break;
 
187
                case 2:
 
188
                        ok1(c == &c3);
 
189
                        list_del_from_off(&opaque_list, q, 0);
 
190
                        destroy_opaque_blob(q);
 
191
                        break;
 
192
                }
 
193
                ok1(list_check(&opaque_list, NULL));
 
194
                if (i > 2)
 
195
                        break;
 
196
        }
 
197
        ok1(i == 3);
 
198
        ok1(list_empty(&opaque_list));
 
199
 
 
200
        /* Test list_top/list_tail on empty list. */
 
201
        ok1(list_top(&parent.children, struct child, list) == NULL);
 
202
        ok1(list_tail(&parent.children, struct child, list) == NULL);
 
203
        return exit_status();
 
204
}