summaryrefslogtreecommitdiff
path: root/test/list.c
blob: 82d232706205b149b92a7716568663215ef5c9ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
 * Copyright © 2011 Red Hat, Inc.
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a
 *  copy of this software and associated documentation files (the "Software"),
 *  to deal in the Software without restriction, including without limitation
 *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
 *  and/or sell copies of the Software, and to permit persons to whom the
 *  Software is furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice (including the next
 *  paragraph) shall be included in all copies or substantial portions of the
 *  Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 *  DEALINGS IN THE SOFTWARE.
 */

#ifdef HAVE_DIX_CONFIG_H
#include <dix-config.h>
#endif

#include <X11/Xlib.h>
#include <list.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>

struct parent {
    int a;
    struct xorg_list children;
    int b;
};

struct child {
    int foo;
    int bar;
    struct xorg_list node;
};

static void
test_xorg_list_init(void)
{
    struct parent parent, tmp;

    memset(&parent, 0, sizeof(parent));
    parent.a = 0xa5a5a5;
    parent.b = ~0xa5a5a5;

    tmp = parent;

    xorg_list_init(&parent.children);

    /* test we haven't touched anything else. */
    assert(parent.a == tmp.a);
    assert(parent.b == tmp.b);

    assert(xorg_list_is_empty(&parent.children));
}

static void
test_xorg_list_add(void)
{
    struct parent parent = { 0 };
    struct child child[3];
    struct child *c;

    xorg_list_init(&parent.children);

    xorg_list_add(&child[0].node, &parent.children);
    assert(!xorg_list_is_empty(&parent.children));

    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);

    /* note: xorg_list_add prepends */
    xorg_list_add(&child[1].node, &parent.children);
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[1], sizeof(struct child)) == 0);

    xorg_list_add(&child[2].node, &parent.children);
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[2], sizeof(struct child)) == 0);
};

static void
test_xorg_list_append(void)
{
    struct parent parent = { 0 };
    struct child child[3];
    struct child *c;
    int i;

    xorg_list_init(&parent.children);

    xorg_list_append(&child[0].node, &parent.children);
    assert(!xorg_list_is_empty(&parent.children));

    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
    c = xorg_list_last_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);

    xorg_list_append(&child[1].node, &parent.children);
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
    c = xorg_list_last_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[1], sizeof(struct child)) == 0);

    xorg_list_append(&child[2].node, &parent.children);
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);
    c = xorg_list_last_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[2], sizeof(struct child)) == 0);

    i = 0;
    xorg_list_for_each_entry(c, &parent.children, node) {
        assert(memcmp(c, &child[i++], sizeof(struct child)) == 0);
    }
};

static void
test_xorg_list_del(void)
{
    struct parent parent = { 0 };
    struct child child[3];
    struct child *c;

    xorg_list_init(&parent.children);

    xorg_list_add(&child[0].node, &parent.children);
    assert(!xorg_list_is_empty(&parent.children));

    xorg_list_del(&parent.children);
    assert(xorg_list_is_empty(&parent.children));

    xorg_list_add(&child[0].node, &parent.children);
    xorg_list_del(&child[0].node);
    assert(xorg_list_is_empty(&parent.children));

    xorg_list_add(&child[0].node, &parent.children);
    xorg_list_add(&child[1].node, &parent.children);

    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[1], sizeof(struct child)) == 0);

    /* delete first node */
    xorg_list_del(&child[1].node);
    assert(!xorg_list_is_empty(&parent.children));
    assert(xorg_list_is_empty(&child[1].node));
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[0], sizeof(struct child)) == 0);

    /* delete last node */
    xorg_list_add(&child[1].node, &parent.children);
    xorg_list_del(&child[0].node);
    c = xorg_list_first_entry(&parent.children, struct child, node);

    assert(memcmp(c, &child[1], sizeof(struct child)) == 0);

    /* delete list head */
    xorg_list_add(&child[0].node, &parent.children);
    xorg_list_del(&parent.children);
    assert(xorg_list_is_empty(&parent.children));
    assert(!xorg_list_is_empty(&child[1].node));
    assert(!xorg_list_is_empty(&child[2].node));
}

static void
test_xorg_list_for_each(void)
{
    struct parent parent = { 0 };
    struct child child[3];
    struct child *c;
    int i = 0;

    xorg_list_init(&parent.children);

    xorg_list_add(&child[2].node, &parent.children);
    xorg_list_add(&child[1].node, &parent.children);
    xorg_list_add(&child[0].node, &parent.children);

    xorg_list_for_each_entry(c, &parent.children, node) {
        assert(memcmp(c, &child[i], sizeof(struct child)) == 0);
        i++;
    }

    /* foreach on empty list */
    xorg_list_del(&parent.children);
    assert(xorg_list_is_empty(&parent.children));

    xorg_list_for_each_entry(c, &parent.children, node) {
        assert(0);              /* we must not get here */
    }
}

struct foo {
    char a;
    struct foo *next;
    char b;
};

static void
test_nt_list_init(void)
{
    struct foo foo;

    foo.a = 10;
    foo.b = 20;
    nt_list_init(&foo, next);

    assert(foo.a == 10);
    assert(foo.b == 20);
    assert(foo.next == NULL);
    assert(nt_list_next(&foo, next) == NULL);
}

static void
test_nt_list_append(void)
{
    int i;
    struct foo *foo = calloc(10, sizeof(struct foo));
    struct foo *item;

    for (item = foo, i = 1; i <= 10; i++, item++) {
        item->a = i;
        item->b = i * 2;
        nt_list_init(item, next);

        if (item != foo)
            nt_list_append(item, foo, struct foo, next);
    }

    /* Test using nt_list_next */
    for (item = foo, i = 1; i <= 10; i++, item = nt_list_next(item, next)) {
        assert(item->a = i);
        assert(item->b = i * 2);
    }

    /* Test using nt_list_for_each_entry */
    i = 1;
    nt_list_for_each_entry(item, foo, next) {
        assert(item->a = i);
        assert(item->b = i * 2);
        i++;
    }
    assert(i == 11);
}

static void
test_nt_list_insert(void)
{
    int i;
    struct foo *foo = calloc(10, sizeof(struct foo));
    struct foo *item;

    foo->a = 10;
    foo->b = 20;
    nt_list_init(foo, next);

    for (item = &foo[1], i = 9; i > 0; i--, item++) {
        item->a = i;
        item->b = i * 2;
        nt_list_init(item, next);
        nt_list_insert(item, foo, struct foo, next);
    }

    /* Test using nt_list_next */
    for (item = foo, i = 10; i > 0; i--, item = nt_list_next(item, next)) {
        assert(item->a = i);
        assert(item->b = i * 2);
    }

    /* Test using nt_list_for_each_entry */
    i = 1;
    nt_list_for_each_entry(item, foo, next) {
        assert(item->a = i);
        assert(item->b = i * 2);
        i++;
    }
    assert(i == 11);
}

static void
test_nt_list_delete(void)
{
    int i = 1;
    struct foo *list = calloc(10, sizeof(struct foo));
    struct foo *foo = list;
    struct foo *item, *tmp;
    struct foo *empty_list = foo;

    nt_list_init(empty_list, next);
    nt_list_del(empty_list, empty_list, struct foo, next);

    assert(!empty_list);

    for (item = foo, i = 1; i <= 10; i++, item++) {
        item->a = i;
        item->b = i * 2;
        nt_list_init(item, next);

        if (item != foo)
            nt_list_append(item, foo, struct foo, next);
    }

    i = 0;
    nt_list_for_each_entry(item, foo, next) {
        i++;
    }
    assert(i == 10);

    /* delete last item */
    nt_list_del(&foo[9], foo, struct foo, next);

    i = 0;
    nt_list_for_each_entry(item, foo, next) {
        assert(item->a != 10);  /* element 10 is gone now */
        i++;
    }
    assert(i == 9);             /* 9 elements left */

    /* delete second item */
    nt_list_del(foo->next, foo, struct foo, next);

    assert(foo->next->a == 3);

    i = 0;
    nt_list_for_each_entry(item, foo, next) {
        assert(item->a != 10);  /* element 10 is gone now */
        assert(item->a != 2);   /* element 2 is gone now */
        i++;
    }
    assert(i == 8);             /* 9 elements left */

    item = foo;
    /* delete first item */
    nt_list_del(foo, foo, struct foo, next);

    assert(item != foo);
    assert(item->next == NULL);
    assert(foo->a == 3);
    assert(foo->next->a == 4);

    nt_list_for_each_entry_safe(item, tmp, foo, next) {
        nt_list_del(item, foo, struct foo, next);
    }

    assert(!foo);
    assert(!item);

    free(list);
}

int
main(int argc, char **argv)
{
    test_xorg_list_init();
    test_xorg_list_add();
    test_xorg_list_append();
    test_xorg_list_del();
    test_xorg_list_for_each();

    test_nt_list_init();
    test_nt_list_append();
    test_nt_list_insert();
    test_nt_list_delete();

    return 0;
}