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

« back to all changes in this revision

Viewing changes to roms/skiboot/ccan/list/test/run-single-eval.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
/* Make sure macros only evaluate their args once. */
 
2
#include <ccan/list/list.h>
 
3
#include <ccan/tap/tap.h>
 
4
#include <ccan/list/list.c>
 
5
 
 
6
struct parent {
 
7
        const char *name;
 
8
        struct list_head children;
 
9
        unsigned int num_children;
 
10
        int eval_count;
 
11
};
 
12
 
 
13
struct child {
 
14
        const char *name;
 
15
        struct list_node list;
 
16
};
 
17
 
 
18
static LIST_HEAD(static_list);
 
19
 
 
20
#define ref(obj, counter) ((counter)++, (obj))
 
21
 
 
22
int main(int argc, char *argv[])
 
23
{
 
24
        struct parent parent;
 
25
        struct child c1, c2, c3, *c, *n;
 
26
        unsigned int i;
 
27
        unsigned int static_count = 0, parent_count = 0, list_count = 0,
 
28
                node_count = 0;
 
29
        struct list_head list = LIST_HEAD_INIT(list);
 
30
 
 
31
        (void)argc;
 
32
        (void)argv;
 
33
 
 
34
        plan_tests(74);
 
35
        /* Test LIST_HEAD, LIST_HEAD_INIT, list_empty and check_list */
 
36
        ok1(list_empty(ref(&static_list, static_count)));
 
37
        ok1(static_count == 1);
 
38
        ok1(list_check(ref(&static_list, static_count), NULL));
 
39
        ok1(static_count == 2);
 
40
        ok1(list_empty(ref(&list, list_count)));
 
41
        ok1(list_count == 1);
 
42
        ok1(list_check(ref(&list, list_count), NULL));
 
43
        ok1(list_count == 2);
 
44
 
 
45
        parent.num_children = 0;
 
46
        list_head_init(ref(&parent.children, parent_count));
 
47
        ok1(parent_count == 1);
 
48
        /* Test list_head_init */
 
49
        ok1(list_empty(ref(&parent.children, parent_count)));
 
50
        ok1(parent_count == 2);
 
51
        ok1(list_check(ref(&parent.children, parent_count), NULL));
 
52
        ok1(parent_count == 3);
 
53
 
 
54
        c2.name = "c2";
 
55
        list_add(ref(&parent.children, parent_count), &c2.list);
 
56
        ok1(parent_count == 4);
 
57
        /* Test list_add and !list_empty. */
 
58
        ok1(!list_empty(ref(&parent.children, parent_count)));
 
59
        ok1(parent_count == 5);
 
60
        ok1(c2.list.next == &parent.children.n);
 
61
        ok1(c2.list.prev == &parent.children.n);
 
62
        ok1(parent.children.n.next == &c2.list);
 
63
        ok1(parent.children.n.prev == &c2.list);
 
64
        /* Test list_check */
 
65
        ok1(list_check(ref(&parent.children, parent_count), NULL));
 
66
        ok1(parent_count == 6);
 
67
 
 
68
        c1.name = "c1";
 
69
        list_add(ref(&parent.children, parent_count), &c1.list);
 
70
        ok1(parent_count == 7);
 
71
        /* Test list_add and !list_empty. */
 
72
        ok1(!list_empty(ref(&parent.children, parent_count)));
 
73
        ok1(parent_count == 8);
 
74
        ok1(c2.list.next == &parent.children.n);
 
75
        ok1(c2.list.prev == &c1.list);
 
76
        ok1(parent.children.n.next == &c1.list);
 
77
        ok1(parent.children.n.prev == &c2.list);
 
78
        ok1(c1.list.next == &c2.list);
 
79
        ok1(c1.list.prev == &parent.children.n);
 
80
        /* Test list_check */
 
81
        ok1(list_check(ref(&parent.children, parent_count), NULL));
 
82
        ok1(parent_count == 9);
 
83
 
 
84
        c3.name = "c3";
 
85
        list_add_tail(ref(&parent.children, parent_count), &c3.list);
 
86
        ok1(parent_count == 10);
 
87
        /* Test list_add_tail and !list_empty. */
 
88
        ok1(!list_empty(ref(&parent.children, parent_count)));
 
89
        ok1(parent_count == 11);
 
90
        ok1(parent.children.n.next == &c1.list);
 
91
        ok1(parent.children.n.prev == &c3.list);
 
92
        ok1(c1.list.next == &c2.list);
 
93
        ok1(c1.list.prev == &parent.children.n);
 
94
        ok1(c2.list.next == &c3.list);
 
95
        ok1(c2.list.prev == &c1.list);
 
96
        ok1(c3.list.next == &parent.children.n);
 
97
        ok1(c3.list.prev == &c2.list);
 
98
        /* Test list_check */
 
99
        ok1(list_check(ref(&parent.children, parent_count), NULL));
 
100
        ok1(parent_count == 12);
 
101
 
 
102
        /* Test list_check_node */
 
103
        ok1(list_check_node(&c1.list, NULL));
 
104
        ok1(list_check_node(&c2.list, NULL));
 
105
        ok1(list_check_node(&c3.list, NULL));
 
106
 
 
107
        /* Test list_top */
 
108
        ok1(list_top(ref(&parent.children, parent_count), struct child, list) == &c1);
 
109
        ok1(parent_count == 13);
 
110
 
 
111
        /* Test list_tail */
 
112
        ok1(list_tail(ref(&parent.children, parent_count), struct child, list) == &c3);
 
113
        ok1(parent_count == 14);
 
114
 
 
115
        /* Test list_for_each. */
 
116
        i = 0;
 
117
        list_for_each(&parent.children, c, list) {
 
118
                switch (i++) {
 
119
                case 0:
 
120
                        ok1(c == &c1);
 
121
                        break;
 
122
                case 1:
 
123
                        ok1(c == &c2);
 
124
                        break;
 
125
                case 2:
 
126
                        ok1(c == &c3);
 
127
                        break;
 
128
                }
 
129
                if (i > 2)
 
130
                        break;
 
131
        }
 
132
        ok1(i == 3);
 
133
 
 
134
        /* Test list_for_each_safe, list_del and list_del_from. */
 
135
        i = 0;
 
136
        list_for_each_safe(&parent.children, c, n, list) {
 
137
                switch (i++) {
 
138
                case 0:
 
139
                        ok1(c == &c1);
 
140
                        list_del(ref(&c->list, node_count));
 
141
                        ok1(node_count == 1);
 
142
                        break;
 
143
                case 1:
 
144
                        ok1(c == &c2);
 
145
                        list_del_from(ref(&parent.children, parent_count),
 
146
                                      ref(&c->list, node_count));
 
147
                        ok1(node_count == 2);
 
148
                        break;
 
149
                case 2:
 
150
                        ok1(c == &c3);
 
151
                        list_del_from(ref(&parent.children, parent_count),
 
152
                                      ref(&c->list, node_count));
 
153
                        ok1(node_count == 3);
 
154
                        break;
 
155
                }
 
156
                ok1(list_check(ref(&parent.children, parent_count), NULL));
 
157
                if (i > 2)
 
158
                        break;
 
159
        }
 
160
        ok1(i == 3);
 
161
        ok1(parent_count == 19);
 
162
        ok1(list_empty(ref(&parent.children, parent_count)));
 
163
        ok1(parent_count == 20);
 
164
 
 
165
        /* Test list_top/list_tail on empty list. */
 
166
        ok1(list_top(ref(&parent.children, parent_count), struct child, list) == NULL);
 
167
        ok1(parent_count == 21);
 
168
        ok1(list_tail(ref(&parent.children, parent_count), struct child, list) == NULL);
 
169
        ok1(parent_count == 22);
 
170
        return exit_status();
 
171
}