]> git.ozlabs.org Git - petitboot/blob - test/lib/list-test.c
Makefiles: remove -I$(includedir)
[petitboot] / test / lib / list-test.c
1 /*
2  *  Copyright Geoff Levand <geoff@infradead.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #define _GNU_SOURCE
23
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include <list/list.h>
28
29
30 int main(void)
31 {
32         struct item {
33                 struct list_item list;
34                 int value;
35         };
36         STATIC_LIST(tester);
37         struct item *item;
38         struct item *tmp;
39         int i;
40
41         for (i = 0; i < 5; i++) {
42                 struct item *item = malloc(sizeof(struct item));
43
44                 item->value = i;
45
46                 list_add_tail(&tester, &item->list);
47         }
48         
49         i = 0;
50         fprintf(stderr, "-- list_for_each_entry --\n");
51         list_for_each_entry(&tester, item, list) {
52                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
53         }
54         
55         i = 0;
56         fprintf(stderr, "-- list_for_each_entry_safe --\n");
57         list_for_each_entry_safe(&tester, item, tmp, list) {
58                 fprintf(stderr, "pos: %d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
59                 fprintf(stderr, "tmp:       %p -> %p\n", tmp, (tmp ? tmp->list.next : NULL));
60                 list_remove(&item->list);
61         }
62
63         /* we should see that the list is empty */
64         i = 0;
65         fprintf(stderr, "-- list_for_each_entry(empty) --\n");
66         list_for_each_entry(&tester, item, list) {
67                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
68         }
69
70         if (i) {
71                 fprintf(stderr, "Error: list should be empty\n");
72                 return EXIT_FAILURE;
73         }
74
75         i = 0;
76         fprintf(stderr, "-- list_for_each_entry_safe(empty) --\n");
77         list_for_each_entry_safe(&tester, item, tmp, list) {
78                 fprintf(stderr, "%d: %d: %p -> %p\n", i++, item->value, item, item->list.next);
79         }
80
81         if (i) {
82                 fprintf(stderr, "Error: list should be empty\n");
83                 return EXIT_FAILURE;
84         }
85         fprintf(stderr, "-- done --\n");
86         return EXIT_SUCCESS;
87 }