Lumiera 0.pre.04~rc.1
»edit your freedom«
Loading...
Searching...
No Matches
test-llist.c
Go to the documentation of this file.
1/*
2 TEST-LLIST - test the linked list lib
3
4 Copyright (C)
5 2008, Christian Thaeter <ct@pipapo.org>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
20#include <nobug.h>
21
22#include "lib/llist.h"
23#include "lib/test/test.h"
24
26
27TEST (basic)
28{
29 LLIST_AUTO (node1);
30
31 llist node2;
32 llist_init (&node2);
33
34 printf ("%d\n", llist_is_empty (&node1));
35 printf ("%d\n", llist_is_empty (&node2));
36}
37
38TEST (nodeinsert)
39{
40 LLIST_AUTO (list);
41 LLIST_AUTO (node1);
42 LLIST_AUTO (node2);
43 LLIST_AUTO (node3);
44
45 llist_insert_next (&list, &node1);
46 printf ("%d\n", llist_is_empty (&list));
47 printf ("%d\n", llist_is_empty (&node1));
48 printf ("%d\n", llist_is_single (&node1));
49 llist_insert_next (&node1, &node2);
50 printf ("%d\n", llist_is_single (&node1));
51 llist_insert_prev (&node1, &node3);
52 printf ("%d\n", llist_next (&list) == &node3);
53 printf ("%d\n", llist_next (&node3) == &node1);
54 printf ("%d\n", llist_next (&node1) == &node2);
55 printf ("%d\n", llist_prev (&list) == &node2);
56 printf ("%d\n", llist_count (&list));
57}
58
59TEST (predicates)
60{
61 LLIST_AUTO (list);
62 LLIST_AUTO (node1);
63 LLIST_AUTO (node2);
64 LLIST_AUTO (node3);
65 LLIST_AUTO (node4);
66 LLIST_AUTO (nil);
67
68 llist_insert_tail (&list, &node2);
69 llist_insert_tail (&list, &node3);
70 llist_insert_tail (&list, &node4);
71 llist_insert_head (&list, &node1);
72
73 printf ("%d\n", llist_is_head (&list, &node1));
74 printf ("%d\n", llist_is_tail (&list, &node4));
75 printf ("%d\n", llist_is_head (&list, &node4));
76 printf ("%d\n", llist_is_tail (&list, &node1));
77 printf ("%d\n", llist_is_end (&list, &list));
78 printf ("%d\n", llist_is_member (&list, &node3));
79 printf ("%d\n", llist_is_member (&list, &nil));
80
81 printf ("%d\n", llist_is_before_after (&list, &node1, &node3));
82 printf ("%d\n", llist_is_before_after (&list, &node3, &node1));
83 printf ("%d\n", llist_is_before_after (&list, &node1, &nil));
84}
85
86TEST (unlink)
87{
88 LLIST_AUTO (list);
89 LLIST_AUTO (node1);
90 LLIST_AUTO (node2);
91 LLIST_AUTO (node3);
92 LLIST_AUTO (node4);
93 LLIST_AUTO (nil);
94
95 llist_insert_tail (&list, &node2);
96 llist_insert_tail (&list, &node3);
97 llist_insert_tail (&list, &node4);
98 llist_insert_head (&list, &node1);
99
100 LLIST_FOREACH_REV (&list, itr)
101 {
102 if(itr == &node1) printf ("node1 ");
103 else if(itr == &node2) printf ("node2 ");
104 else if(itr == &node3) printf ("node3 ");
105 else if(itr == &node4) printf ("node4 ");
106 else printf ("unknown ");
107 }
108 printf (".\n");
109
110 llist_unlink (&nil);
111 llist_unlink (&node2);
112 llist_unlink (&node3);
113
114 LLIST_FOREACH (&list, itr)
115 {
116 if(itr == &node1) printf ("node1 ");
117 else if(itr == &node2) printf ("node2 ");
118 else if(itr == &node3) printf ("node3 ");
119 else if(itr == &node4) printf ("node4 ");
120 else printf ("unknown ");
121 }
122 printf (".\n");
123 printf ("%d\n", llist_is_empty (&node2));
124 printf ("%d\n", llist_is_empty (&node3));
125 printf ("%d\n", llist_is_empty (&nil));
126}
127
128TEST (whiles)
129{
130 LLIST_AUTO (list);
131 LLIST_AUTO (node1);
132 LLIST_AUTO (node2);
133 LLIST_AUTO (node3);
134 LLIST_AUTO (node4);
135 LLIST_AUTO (nil);
136
137 llist_insert_tail (&list, &node2);
138 llist_insert_tail (&list, &node3);
139 llist_insert_tail (&list, &node4);
140 llist_insert_head (&list, &node1);
141
142 LLIST_FOREACH_REV (&list, itr)
143 {
144 if(itr == &node1) printf ("node1 ");
145 else if(itr == &node2) printf ("node2 ");
146 else if(itr == &node3) printf ("node3 ");
147 else if(itr == &node4) printf ("node4 ");
148 else printf ("unknown ");
149 }
150 printf (".\n");
151
152 LLIST_WHILE_HEAD (&list, head)
153 llist_unlink (head);
154
155 LLIST_FOREACH (&list, itr)
156 {
157 if(itr == &node1) printf ("node1 ");
158 else if(itr == &node2) printf ("node2 ");
159 else if(itr == &node3) printf ("node3 ");
160 else if(itr == &node4) printf ("node4 ");
161 else printf ("unknown ");
162 }
163 printf (".\n");
164
165 llist_insert_tail (&list, &node2);
166 llist_insert_tail (&list, &node3);
167 llist_insert_tail (&list, &node4);
168 llist_insert_head (&list, &node1);
169
170 LLIST_WHILE_TAIL (&list, tail)
171 llist_unlink (tail);
172
173 LLIST_FOREACH (&list, itr)
174 {
175 if(itr == &node1) printf ("node1 ");
176 else if(itr == &node2) printf ("node2 ");
177 else if(itr == &node3) printf ("node3 ");
178 else if(itr == &node4) printf ("node4 ");
179 else printf ("unknown ");
180 }
181 printf (".\n");
182}
183
184
185TEST (relocate)
186{
187 llist source;
188 llist_init (&source);
189
190 llist something;
191 llist_init (&something);
192
193 llist_insert_head (&source, &something);
194
195 llist target = {NULL,NULL};
196
197 target = source;
198
199 llist_relocate (&target);
200 CHECK (llist_is_head (&target, &something));
201}
202
203
Intrusive cyclic double linked list There is only one node type which contains a forward and a backwa...
#define LLIST_AUTO(name)
Macro to instantiate a local llist.
Definition llist.h:89
#define llist_insert_tail(list, element)
Definition llist.h:96
return NULL
Definition llist.h:586
#define LLIST_WHILE_HEAD(list, head)
Consume a list from head.
Definition llist.h:164
#define LLIST_FOREACH_REV(list, node)
Iterate backward over a list.
Definition llist.h:129
#define LLIST_WHILE_TAIL(list, tail)
Consume a list from tail.
Definition llist.h:175
#define LLIST_FOREACH(list, node)
Iterate forward over a list.
Definition llist.h:119
#define llist_insert_head(list, element)
Definition llist.h:95
Helpers and support macros for defining test executables in C.
#define TEST(name)
Definition test.h:45
#define TESTS_END
Definition test.h:55
#define TESTS_BEGIN
Definition test.h:34