19
19
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
#endif /* HAVE_CONFIG_H */
24
#include <nih/macros.h>
30
25
#include <nih/alloc.h>
31
26
#include <nih/list.h>
40
printf ("Testing nih_list_init()\n");
34
/* Check that nih_list_init correctly initialises an empty list,
35
* with both pointers pointing back to the entry itself.
37
TEST_FUNCTION ("nih_list_init");
41
38
nih_list_init (&entry);
43
/* Previous pointer should point back to itself */
44
if (entry.prev != &entry) {
45
printf ("BAD: prev pointer set incorrectly.\n");
49
/* Next pointer should point back to itself */
50
if (entry.next != &entry) {
51
printf ("BAD: next pointer set incorrectly.\n");
40
TEST_EQ_P (entry.prev, &entry);
41
TEST_EQ_P (entry.next, &entry);
64
printf ("Testing nih_list_new()\n");
65
list = nih_list_new ();
67
/* Previous pointer should point back to itself */
68
if (list->prev != list) {
69
printf ("BAD: prev pointer set incorrectly.\n");
73
/* Next pointer should point back to itself */
74
if (list->next != list) {
75
printf ("BAD: next pointer set incorrectly.\n");
79
/* Should have been allocated using nih_alloc */
80
if (nih_alloc_size (list) != sizeof (NihList)) {
81
printf ("BAD: nih_alloc was not used.\n");
49
/* Check that nih_list_new allocates a new empty list with nih_alloc
50
* and that it is initialised with pointers pointing to itself.
52
TEST_FUNCTION ("nih_list_new");
53
list = nih_list_new (NULL);
55
TEST_ALLOC_SIZE (list, sizeof (NihList));
56
TEST_EQ_P (list->prev, list);
57
TEST_EQ_P (list->next, list);
93
65
NihList *list, *entry1, *entry2, *ptr;
96
printf ("Testing nih_list_add()\n");
98
list = nih_list_new ();
99
entry1 = nih_list_new ();
100
entry2 = nih_list_new ();
102
printf ("...with single-entry list\n");
67
TEST_FUNCTION ("nih_list_add");
69
list = nih_list_new (NULL);
70
entry1 = nih_list_new (NULL);
71
entry2 = nih_list_new (NULL);
73
/* Check that nih_list_add can add a single entry to an empty list;
74
* the added entry should be returned and the pointers should all
77
TEST_FEATURE ("with single-entry list");
103
78
ptr = nih_list_add (list, entry1);
105
/* The added entry should be returned */
107
printf ("BAD: return value wasn't what we expected.\n");
111
/* Head entry's previous pointer should be the new entry */
112
if (list->prev != entry1) {
113
printf ("BAD: head prev pointer set incorrectly.\n");
117
/* Head entry's next pointer should be the new entry too */
118
if (list->next != entry1) {
119
printf ("BAD: head next pointer set incorrectly.\n");
123
/* New entry's next pointer should be the head */
124
if (entry1->next != list) {
125
printf ("BAD: entry next pointer set incorrectly.\n");
129
/* New entry's previous pointer should be the head too */
130
if (entry1->prev != list) {
131
printf ("BAD: entry prev pointer set incorrectly.\n");
136
printf ("...with multi-entry list\n");
80
TEST_EQ_P (ptr, entry1);
81
TEST_EQ_P (list->next, entry1);
82
TEST_EQ_P (entry1->next, list);
83
TEST_EQ_P (list->prev, entry1);
84
TEST_EQ_P (entry1->prev, list);
87
/* Check that we can now add another entry to that two entry list,
88
* and the pointers are still all right.
90
TEST_FEATURE ("with multi-entry list");
137
91
nih_list_add (list, entry2);
139
/* Head entry's previous pointer should be the new entry */
140
if (list->prev != entry2) {
141
printf ("BAD: head prev pointer set incorrectly.\n");
145
/* Head entry's next pointer should be unchanged. */
146
if (list->next != entry1) {
147
printf ("BAD: head next pointer changed.\n");
151
/* New entry's next pointer should be the head */
152
if (entry2->next != list) {
153
printf ("BAD: entry next pointer set incorrectly.\n");
157
/* New entry's previous pointer should be the previous tail */
158
if (entry2->prev != entry1) {
159
printf ("BAD: entry prev pointer set incorrectly.\n");
163
/* Previous tail's next pointer should be the new entry */
164
if (entry1->next != entry2) {
165
printf ("BAD: previous tail next pointer set incorrectly.\n");
169
/* Previous tail's prev pointer should be unchanged. */
170
if (entry1->prev != list) {
171
printf ("BAD: previous tail prev pointer changed.\n");
176
printf ("...with two entries from same list\n");
93
TEST_EQ_P (list->next, entry1);
94
TEST_EQ_P (entry1->next, entry2);
95
TEST_EQ_P (entry2->next, list);
96
TEST_EQ_P (list->prev, entry2);
97
TEST_EQ_P (entry2->prev, entry1);
98
TEST_EQ_P (entry1->prev, list);
101
/* Check that we can use nih_list_add to swap two entries that are
104
TEST_FEATURE ("with two entries from same list");
177
105
nih_list_add (entry1, entry2);
179
/* Head entry's next pointer should now be entry2 */
180
if (list->next != entry2) {
181
printf ("BAD: list head next pointer set incorrectly.\n");
185
/* entry2's previous pointer should be the head entry */
186
if (entry2->prev != list) {
187
printf ("BAD: entry2 prev pointer set incorrectly.\n");
191
/* entry2's next pointer should be entry1 */
192
if (entry2->next != entry1) {
193
printf ("BAD: entry2 next pointer set incorrectly.\n");
197
/* entry1's previous pointer should be entry2 */
198
if (entry1->prev != entry2) {
199
printf ("BAD: entry1 prev pointer set incorrectly.\n");
203
/* entry1's next pointer should be the head entry */
204
if (entry1->next != list) {
205
printf ("BAD: entry1 next pointer set incorrectly.\n");
209
/* head entry's previous pointer should be entry1 */
210
if (list->prev != entry1) {
211
printf ("BAD: head prev pointer set incorrectly.\n");
216
printf ("...with entry from other list\n");
217
ptr = nih_list_new ();
107
TEST_EQ_P (list->next, entry2);
108
TEST_EQ_P (entry2->next, entry1);
109
TEST_EQ_P (entry1->next, list);
110
TEST_EQ_P (list->prev, entry1);
111
TEST_EQ_P (entry1->prev, entry2);
112
TEST_EQ_P (entry2->prev, list);
115
/* Check that we can rip an entry out of its list and place it in
118
TEST_FEATURE ("with entry from other list");
119
ptr = nih_list_new (NULL);
218
120
nih_list_add (ptr, entry2);
220
/* The entry should be removed from the old list, so the
221
* old list head entry's next pointer should point to the tail. */
222
if (list->next != entry1) {
223
printf ("BAD: old list head next pointer set incorrectly.\n");
122
TEST_EQ_P (list->next, entry1);
123
TEST_EQ_P (entry1->next, list);
124
TEST_EQ_P (list->prev, entry1);
125
TEST_EQ_P (entry1->prev, list);
227
/* The entry should be removed from the old list, so the
228
* old list tail entry's prev pointer should point to the head. */
229
if (entry1->prev != list) {
230
printf ("BAD: old list tail prev pointer set incorrectly.\n");
127
TEST_EQ_P (ptr->next, entry2);
128
TEST_EQ_P (entry2->next, ptr);
129
TEST_EQ_P (ptr->prev, entry2);
130
TEST_EQ_P (entry2->prev, ptr);
234
132
nih_list_free (list);
235
133
nih_list_free (entry1);
236
134
nih_list_free (entry2);
237
135
nih_list_free (ptr);
243
139
test_add_after (void)
245
141
NihList *list, *entry1, *entry2, *ptr;
248
printf ("Testing nih_list_add_after()\n");
250
list = nih_list_new ();
251
entry1 = nih_list_new ();
252
entry2 = nih_list_new ();
254
printf ("...with single-entry list\n");
143
TEST_FUNCTION ("nih_list_add_after");
145
list = nih_list_new (NULL);
146
entry1 = nih_list_new (NULL);
147
entry2 = nih_list_new (NULL);
149
/* Check that nih_list_add_after can add a single entry to an empty
150
* list, the result should be the same as nih_list_add.
152
TEST_FEATURE ("with single-entry list");
255
153
ptr = nih_list_add_after (list, entry1);
257
/* The added entry should be returned */
259
printf ("BAD: return value wasn't what we expected.\n");
263
/* Head entry's next pointer should be the new entry */
264
if (list->next != entry1) {
265
printf ("BAD: head next pointer set incorrectly.\n");
269
/* Head entry's previous pointer should be the new entry too */
270
if (list->prev != entry1) {
271
printf ("BAD: head prev pointer set incorrectly.\n");
275
/* New entry's previous pointer should be the head */
276
if (entry1->prev != list) {
277
printf ("BAD: entry prev pointer set incorrectly.\n");
281
/* New entry's next pointer should be the head too */
282
if (entry1->next != list) {
283
printf ("BAD: entry next pointer set incorrectly.\n");
288
printf ("...with multi-entry list\n");
155
TEST_EQ_P (ptr, entry1);
156
TEST_EQ_P (list->next, entry1);
157
TEST_EQ_P (entry1->next, list);
158
TEST_EQ_P (list->prev, entry1);
159
TEST_EQ_P (entry1->prev, list);
162
/* Check that when adding an entry to a list with multiple entries,
163
* nih_list_add_after adds the entry immediately after the entry
164
* given, not before (as nih_list_add does)
166
TEST_FEATURE ("with multi-entry list");
289
167
nih_list_add_after (list, entry2);
291
/* Head entry's next pointer should be the new entry */
292
if (list->next != entry2) {
293
printf ("BAD: head next pointer set incorrectly.\n");
297
/* Head entry's previous pointer should be unchanged. */
298
if (list->prev != entry1) {
299
printf ("BAD: head prev pointer changed.\n");
303
/* New entry's previous pointer should be the head */
304
if (entry2->prev != list) {
305
printf ("BAD: entry prev pointer set incorrectly.\n");
309
/* New entry's next pointer should be the tail */
310
if (entry2->next != entry1) {
311
printf ("BAD: entry next pointer set incorrectly.\n");
315
/* Tail entry's prev pointer should be the new entry */
316
if (entry1->prev != entry2) {
317
printf ("BAD: tail prev pointer set incorrectly.\n");
321
/* Tail entry's next pointer should be unchanged. */
322
if (entry1->next != list) {
323
printf ("BAD: tail next pointer changed.\n");
328
printf ("...with two entries from same list\n");
169
TEST_EQ_P (list->next, entry2);
170
TEST_EQ_P (entry2->next, entry1);
171
TEST_EQ_P (entry1->next, list);
172
TEST_EQ_P (list->prev, entry1);
173
TEST_EQ_P (entry1->prev, entry2);
174
TEST_EQ_P (entry2->prev, list);
177
/* Check that nih_list_add_after can be used to swap two entries
180
TEST_FEATURE ("with two entries from same list");
329
181
nih_list_add_after (entry1, entry2);
331
/* Head entry's next pointer should now be entry1 */
332
if (list->next != entry1) {
333
printf ("BAD: list head next pointer set incorrectly.\n");
337
/* entry1's previous pointer should be the head entry */
338
if (entry1->prev != list) {
339
printf ("BAD: entry1 prev pointer set incorrectly.\n");
343
/* entry1's next pointer should be entry2 */
344
if (entry1->next != entry2) {
345
printf ("BAD: entry1 next pointer set incorrectly.\n");
349
/* entry2's previous pointer should be entry1 */
350
if (entry2->prev != entry1) {
351
printf ("BAD: entry2 prev pointer set incorrectly.\n");
355
/* entry2's next pointer should be the head entry */
356
if (entry2->next != list) {
357
printf ("BAD: entry2 next pointer set incorrectly.\n");
361
/* head entry's previous pointer should be entry2 */
362
if (list->prev != entry2) {
363
printf ("BAD: head prev pointer set incorrectly.\n");
368
printf ("...with entry from other list\n");
369
ptr = nih_list_new ();
183
TEST_EQ_P (list->next, entry1);
184
TEST_EQ_P (entry1->next, entry2);
185
TEST_EQ_P (entry2->next, list);
186
TEST_EQ_P (list->prev, entry2);
187
TEST_EQ_P (entry2->prev, entry1);
188
TEST_EQ_P (entry1->prev, list);
191
/* Check that nih_list_add_after can rip an entry out of its
192
* containing list, and add it to a new one.
194
TEST_FEATURE ("with entry from other list");
195
ptr = nih_list_new (NULL);
370
196
nih_list_add_after (ptr, entry1);
372
/* The entry should be removed from the old list, so the
373
* old list head entry's next pointer should point to the tail. */
374
if (list->next != entry2) {
375
printf ("BAD: old list head next pointer set incorrectly.\n");
198
TEST_EQ_P (list->next, entry2);
199
TEST_EQ_P (entry2->next, list);
200
TEST_EQ_P (list->prev, entry2);
201
TEST_EQ_P (entry2->prev, list);
379
/* The entry should be removed from the old list, so the
380
* old list tail entry's prev pointer should point to the head. */
381
if (entry2->prev != list) {
382
printf ("BAD: old list tail prev pointer set incorrectly.\n");
203
TEST_EQ_P (ptr->next, entry1);
204
TEST_EQ_P (entry1->next, ptr);
205
TEST_EQ_P (ptr->prev, entry1);
206
TEST_EQ_P (entry1->prev, ptr);
387
209
nih_free (entry1);
388
210
nih_free (entry2);
395
215
test_empty (void)
397
217
NihList *list, *entry;
400
printf ("Testing NIH_LIST_EMPTY()\n");
401
list = nih_list_new ();
402
entry = nih_list_new ();
404
printf ("...with empty list\n");
406
/* The list should be empty */
407
if (! NIH_LIST_EMPTY (list)) {
408
printf ("BAD: return value wasn't what we expected.\n");
413
printf ("...with non-empty list\n");
219
TEST_FUNCTION ("NIH_LIST_EMPTY");
221
/* Check that NIH_LIST_EMPTY is TRUE on an empty list */
222
TEST_FEATURE ("with empty list");
223
list = nih_list_new (NULL);
225
TEST_LIST_EMPTY (list);
228
/* Check that NIH_LIST_EMPTY is FALSE on a non-empty list */
229
TEST_FEATURE ("with non-empty list");
230
entry = nih_list_new (NULL);
414
231
nih_list_add (list, entry);
416
/* The list should not be empty */
417
if (NIH_LIST_EMPTY (list)) {
418
printf ("BAD: return value wasn't what we expected.\n");
422
/* Check that's true for the entry too */
423
if (NIH_LIST_EMPTY (entry)) {
424
printf ("BAD: return value wasn't what we expected.\n");
233
TEST_LIST_NOT_EMPTY (list);
234
TEST_LIST_NOT_EMPTY (entry);
429
237
nih_free (entry);
435
241
test_foreach (void)
437
243
NihList *list, *entry[3];
440
printf ("Testing NIH_LIST_FOREACH()\n");
441
list = nih_list_new ();
442
entry[0] = nih_list_add (list, nih_list_new ());
443
entry[1] = nih_list_add (list, nih_list_new ());
444
entry[2] = nih_list_add (list, nih_list_new ());
446
/* Perform a test iteration */
246
/* Check that NIH_LIST_FOREACH iterates the list correctly in
247
* order, visiting each entry.
249
TEST_FUNCTION ("NIH_LIST_FOREACH");
250
list = nih_list_new (NULL);
251
entry[0] = nih_list_add (list, nih_list_new (NULL));
252
entry[1] = nih_list_add (list, nih_list_new (NULL));
253
entry[2] = nih_list_add (list, nih_list_new (NULL));
448
256
NIH_LIST_FOREACH (list, iter) {
450
printf ("BAD: more iterations than expected.\n");
258
TEST_FAILED ("wrong number of iterations, expected %d got %d",
455
if (iter != entry[i]) {
456
printf ("BAD: iteration not entry we expected.\n");
261
if (iter != entry[i])
262
TEST_FAILED ("wrong list entry, expected %p got %p",
464
269
nih_free (entry[0]);
465
270
nih_free (entry[1]);
466
271
nih_free (entry[2]);
472
275
test_foreach_safe (void)
474
277
NihList *list, *entry[3];
477
printf ("Testing NIH_LIST_FOREACH_SAFE()\n");
478
list = nih_list_new ();
479
entry[0] = nih_list_add (list, nih_list_new ());
480
entry[1] = nih_list_add (list, nih_list_new ());
481
entry[2] = nih_list_add (list, nih_list_new ());
483
/* Perform a test iteration */
280
/* Check that NIH_LIST_FOREACH_SAFE iterates the list correctly in
281
* order, visiting each entry; and that it's safe to remove entries
284
TEST_FUNCTION ("NIH_LIST_FOREACH_SAFE");
285
list = nih_list_new (NULL);
286
entry[0] = nih_list_add (list, nih_list_new (NULL));
287
entry[1] = nih_list_add (list, nih_list_new (NULL));
288
entry[2] = nih_list_add (list, nih_list_new (NULL));
485
291
NIH_LIST_FOREACH_SAFE (list, iter) {
487
printf ("BAD: more iterations than expected.\n");
293
TEST_FAILED ("wrong number of iterations, expected %d got %d",
492
if (iter != entry[i]) {
493
printf ("BAD: iteration not entry we expected.\n");
296
if (iter != entry[i])
297
TEST_FAILED ("wrong list entry, expected %p got %p",
497
300
nih_list_remove (entry[i]);
502
/* List should be empty */
503
if (! NIH_LIST_EMPTY (list)) {
504
printf ("BAD: list not empty.\n");
306
/* Check that the list is now empty */
307
TEST_LIST_EMPTY (list);
509
310
nih_free (entry[0]);
510
311
nih_free (entry[1]);
511
312
nih_free (entry[2]);
517
316
test_remove (void)
519
318
NihList *list, *entry, *tail, *ptr;
522
printf ("Testing nih_list_remove()\n");
523
list = nih_list_new ();
524
entry = nih_list_add (list, nih_list_new ());
525
tail = nih_list_add (list, nih_list_new ());
527
printf ("...with two-entry list\n");
320
TEST_FUNCTION ("nih_list_remove");
321
list = nih_list_new (NULL);
322
entry = nih_list_add (list, nih_list_new (NULL));
323
tail = nih_list_add (list, nih_list_new (NULL));
325
/* Check that nih_list_remove works, returning the entry that was
326
* removed and adjusting both sets of pointers in the lists.
328
TEST_FEATURE ("with two-entry list");
528
329
ptr = nih_list_remove (entry);
530
/* The entry that was removed should be returned */
532
printf ("BAD: return value is not what we expected.\n");
536
/* The previous pointer should point back to itself */
537
if (entry->prev != entry) {
538
printf ("BAD: prev pointer set incorrectly.\n");
542
/* The next pointer should point back to itself */
543
if (entry->next != entry) {
544
printf ("BAD: next pointer set incorrectly.\n");
548
/* Head entry's next pointer should point to the tail entry */
549
if (list->next != tail) {
550
printf ("BAD: head next pointer set incorrectly.\n");
554
/* Head entry's previous pointer should still point to the tail */
555
if (list->prev != tail) {
556
printf ("BAD: head prev pointer changed.\n");
560
/* Tail entry's next pointer should still point to the head */
561
if (tail->next != list) {
562
printf ("BAD: tail next pointer changed.\n");
566
/* Tail entry's previous pointer should point to the head entry */
567
if (tail->prev != list) {
568
printf ("BAD: tail next pointer set incorrectly.\n");
573
printf ("...with one-entry list\n");
574
ptr = nih_list_remove (tail);
576
/* The entry that was removed should be returned */
578
printf ("BAD: return value is not what we expected.\n");
582
/* The previous pointer should point back to itself */
583
if (tail->prev != tail) {
584
printf ("BAD: prev pointer set incorrectly.\n");
588
/* The next pointer should point back to itself */
589
if (tail->next != tail) {
590
printf ("BAD: next pointer set incorrectly.\n");
594
/* Head entry's next pointer should point back to itself */
595
if (list->next != list) {
596
printf ("BAD: head next pointer set incorrectly.\n");
600
/* Head entry's previous pointer should point back to itself */
601
if (list->prev != list) {
602
printf ("BAD: head prev pointer changed.\n");
607
printf ("...with empty list\n");
608
ptr = nih_list_remove (tail);
610
/* The entry that was removed should be returned */
612
printf ("BAD: return value is not what we expected.\n");
616
/* The previous pointer should still point back to itself */
617
if (tail->prev != tail) {
618
printf ("BAD: prev pointer set incorrectly.\n");
622
/* The next pointer should still point back to itself */
623
if (tail->next != tail) {
624
printf ("BAD: next pointer set incorrectly.\n");
331
TEST_EQ_P (ptr, entry);
332
TEST_EQ_P (list->next, tail);
333
TEST_EQ_P (tail->next, list);
334
TEST_EQ_P (list->prev, tail);
335
TEST_EQ_P (tail->prev, list);
337
TEST_EQ_P (entry->next, entry);
338
TEST_EQ_P (entry->prev, entry);
341
/* Check that nih_list_remove works if there is only one entry in the
342
* list that's not the head, the pointers should both curl in.
344
TEST_FEATURE ("with one-entry list");
345
ptr = nih_list_remove (tail);
347
TEST_EQ_P (list->next, list);
348
TEST_EQ_P (list->prev, list);
350
TEST_EQ_P (tail->next, tail);
351
TEST_EQ_P (tail->prev, tail);
354
/* Check that it works on an empty list, this should do nothing. */
355
TEST_FEATURE ("with empty list");
356
ptr = nih_list_remove (tail);
358
TEST_EQ_P (tail->next, tail);
359
TEST_EQ_P (tail->prev, tail);
629
362
nih_free (entry);
636
367
test_destructor (void)
638
369
NihList *list, *entry, *tail;
641
printf ("Testing nih_list_destructor()\n");
642
list = nih_list_new ();
643
entry = nih_list_add (list, nih_list_new ());
644
tail = nih_list_add (list, nih_list_new ());
645
retval = nih_list_destructor (entry);
647
/* Zero should be returned */
649
printf ("BAD: return value is not what we expected.\n");
653
/* Head entry's next pointer should point to the tail entry */
654
if (list->next != tail) {
655
printf ("BAD: head next pointer set incorrectly.\n");
659
/* Head entry's previous pointer should still point to the tail */
660
if (list->prev != tail) {
661
printf ("BAD: head prev pointer changed.\n");
665
/* Tail entry's next pointer should still point to the head */
666
if (tail->next != list) {
667
printf ("BAD: tail next pointer changed.\n");
671
/* Tail entry's previous pointer should point to the head entry */
672
if (tail->prev != list) {
673
printf ("BAD: tail next pointer set incorrectly.\n");
372
/* Check that the function removes the entry from its containing
373
* list, it needn't bother updating the entry itself seeing as it's
374
* being freed anyway.
376
TEST_FUNCTION ("nih_list_destructor");
377
list = nih_list_new (NULL);
378
entry = nih_list_add (list, nih_list_new (NULL));
379
tail = nih_list_add (list, nih_list_new (NULL));
380
ret = nih_list_destructor (entry);
384
TEST_EQ_P (list->next, tail);
385
TEST_EQ_P (tail->next, list);
386
TEST_EQ_P (list->prev, tail);
387
TEST_EQ_P (tail->prev, list);
677
389
nih_free (entry);
684
395
static int was_called;