~ubuntu-branches/debian/sid/upstart/sid

« back to all changes in this revision

Viewing changes to nih/tests/test_list.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2006-12-13 17:27:37 UTC
  • mto: (16.1.6 feisty)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: james.westby@ubuntu.com-20061213172737-xtzw97b0cl4q7oc5
Tags: upstream-0.3.1
ImportĀ upstreamĀ versionĀ 0.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20
20
 */
21
21
 
22
 
#ifdef HAVE_CONFIG_H
23
 
# include <config.h>
24
 
#endif /* HAVE_CONFIG_H */
25
 
 
26
 
 
27
 
#include <stdio.h>
28
 
#include <string.h>
29
 
 
 
22
#include <nih/test.h>
 
23
 
 
24
#include <nih/macros.h>
30
25
#include <nih/alloc.h>
31
26
#include <nih/list.h>
32
27
 
33
28
 
34
 
int
 
29
void
35
30
test_init (void)
36
31
{
37
32
        NihList entry;
38
 
        int     ret = 0;
39
33
 
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.
 
36
         */
 
37
        TEST_FUNCTION ("nih_list_init");
41
38
        nih_list_init (&entry);
42
39
 
43
 
        /* Previous pointer should point back to itself */
44
 
        if (entry.prev != &entry) {
45
 
                printf ("BAD: prev pointer set incorrectly.\n");
46
 
                ret = 1;
47
 
        }
48
 
 
49
 
        /* Next pointer should point back to itself */
50
 
        if (entry.next != &entry) {
51
 
                printf ("BAD: next pointer set incorrectly.\n");
52
 
                ret = 1;
53
 
        }
54
 
 
55
 
        return ret;
 
40
        TEST_EQ_P (entry.prev, &entry);
 
41
        TEST_EQ_P (entry.next, &entry);
56
42
}
57
43
 
58
 
int
 
44
void
59
45
test_new (void)
60
46
{
61
47
        NihList *list;
62
 
        int      ret = 0;
63
 
 
64
 
        printf ("Testing nih_list_new()\n");
65
 
        list = nih_list_new ();
66
 
 
67
 
        /* Previous pointer should point back to itself */
68
 
        if (list->prev != list) {
69
 
                printf ("BAD: prev pointer set incorrectly.\n");
70
 
                ret = 1;
71
 
        }
72
 
 
73
 
        /* Next pointer should point back to itself */
74
 
        if (list->next != list) {
75
 
                printf ("BAD: next pointer set incorrectly.\n");
76
 
                ret = 1;
77
 
        }
78
 
 
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");
82
 
                ret = 1;
83
 
        }
 
48
 
 
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.
 
51
         */
 
52
        TEST_FUNCTION ("nih_list_new");
 
53
        list = nih_list_new (NULL);
 
54
 
 
55
        TEST_ALLOC_SIZE (list, sizeof (NihList));
 
56
        TEST_EQ_P (list->prev, list);
 
57
        TEST_EQ_P (list->next, list);
84
58
 
85
59
        nih_free (list);
86
 
 
87
 
        return ret;
88
60
}
89
61
 
90
 
int
 
62
void
91
63
test_add (void)
92
64
{
93
65
        NihList *list, *entry1, *entry2, *ptr;
94
 
        int      ret = 0;
95
 
 
96
 
        printf ("Testing nih_list_add()\n");
97
 
 
98
 
        list = nih_list_new ();
99
 
        entry1 = nih_list_new ();
100
 
        entry2 = nih_list_new ();
101
 
 
102
 
        printf ("...with single-entry list\n");
 
66
 
 
67
        TEST_FUNCTION ("nih_list_add");
 
68
 
 
69
        list = nih_list_new (NULL);
 
70
        entry1 = nih_list_new (NULL);
 
71
        entry2 = nih_list_new (NULL);
 
72
 
 
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
 
75
         * chain up.
 
76
         */
 
77
        TEST_FEATURE ("with single-entry list");
103
78
        ptr = nih_list_add (list, entry1);
104
79
 
105
 
        /* The added entry should be returned */
106
 
        if (ptr != entry1) {
107
 
                printf ("BAD: return value wasn't what we expected.\n");
108
 
                ret = 1;
109
 
        }
110
 
 
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");
114
 
                ret = 1;
115
 
        }
116
 
 
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");
120
 
                ret = 1;
121
 
        }
122
 
 
123
 
        /* New entry's next pointer should be the head */
124
 
        if (entry1->next != list) {
125
 
                printf ("BAD: entry next pointer set incorrectly.\n");
126
 
                ret = 1;
127
 
        }
128
 
 
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");
132
 
                ret = 1;
133
 
        }
134
 
 
135
 
 
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);
 
85
 
 
86
 
 
87
        /* Check that we can now add another entry to that two entry list,
 
88
         * and the pointers are still all right.
 
89
         */
 
90
        TEST_FEATURE ("with multi-entry list");
137
91
        nih_list_add (list, entry2);
138
92
 
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");
142
 
                ret = 1;
143
 
        }
144
 
 
145
 
        /* Head entry's next pointer should be unchanged. */
146
 
        if (list->next != entry1) {
147
 
                printf ("BAD: head next pointer changed.\n");
148
 
                ret = 1;
149
 
        }
150
 
 
151
 
        /* New entry's next pointer should be the head */
152
 
        if (entry2->next != list) {
153
 
                printf ("BAD: entry next pointer set incorrectly.\n");
154
 
                ret = 1;
155
 
        }
156
 
 
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");
160
 
                ret = 1;
161
 
        }
162
 
 
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");
166
 
                ret = 1;
167
 
        }
168
 
 
169
 
        /* Previous tail's prev pointer should be unchanged. */
170
 
        if (entry1->prev != list) {
171
 
                printf ("BAD: previous tail prev pointer changed.\n");
172
 
                ret = 1;
173
 
        }
174
 
 
175
 
 
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);
 
99
 
 
100
 
 
101
        /* Check that we can use nih_list_add to swap two entries that are
 
102
         * in the same list.
 
103
         */
 
104
        TEST_FEATURE ("with two entries from same list");
177
105
        nih_list_add (entry1, entry2);
178
106
 
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");
182
 
                ret = 1;
183
 
        }
184
 
 
185
 
        /* entry2's previous pointer should be the head entry */
186
 
        if (entry2->prev != list) {
187
 
                printf ("BAD: entry2 prev pointer set incorrectly.\n");
188
 
                ret = 1;
189
 
        }
190
 
 
191
 
        /* entry2's next pointer should be entry1 */
192
 
        if (entry2->next != entry1) {
193
 
                printf ("BAD: entry2 next pointer set incorrectly.\n");
194
 
                ret = 1;
195
 
        }
196
 
 
197
 
        /* entry1's previous pointer should be entry2 */
198
 
        if (entry1->prev != entry2) {
199
 
                printf ("BAD: entry1 prev pointer set incorrectly.\n");
200
 
                ret = 1;
201
 
        }
202
 
 
203
 
        /* entry1's next pointer should be the head entry */
204
 
        if (entry1->next != list) {
205
 
                printf ("BAD: entry1 next pointer set incorrectly.\n");
206
 
                ret = 1;
207
 
        }
208
 
 
209
 
        /* head entry's previous pointer should be entry1 */
210
 
        if (list->prev != entry1) {
211
 
                printf ("BAD: head prev pointer set incorrectly.\n");
212
 
                ret = 1;
213
 
        }
214
 
 
215
 
 
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);
 
113
 
 
114
 
 
115
        /* Check that we can rip an entry out of its list and place it in
 
116
         * a new empty one.
 
117
         */
 
118
        TEST_FEATURE ("with entry from other list");
 
119
        ptr = nih_list_new (NULL);
218
120
        nih_list_add (ptr, entry2);
219
121
 
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");
224
 
                ret = 1;
225
 
        }
 
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);
226
126
 
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");
231
 
                ret = 1;
232
 
        }
 
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);
233
131
 
234
132
        nih_list_free (list);
235
133
        nih_list_free (entry1);
236
134
        nih_list_free (entry2);
237
135
        nih_list_free (ptr);
238
 
 
239
 
        return ret;
240
136
}
241
137
 
242
 
int
 
138
void
243
139
test_add_after (void)
244
140
{
245
141
        NihList *list, *entry1, *entry2, *ptr;
246
 
        int      ret = 0;
247
 
 
248
 
        printf ("Testing nih_list_add_after()\n");
249
 
 
250
 
        list = nih_list_new ();
251
 
        entry1 = nih_list_new ();
252
 
        entry2 = nih_list_new ();
253
 
 
254
 
        printf ("...with single-entry list\n");
 
142
 
 
143
        TEST_FUNCTION ("nih_list_add_after");
 
144
 
 
145
        list = nih_list_new (NULL);
 
146
        entry1 = nih_list_new (NULL);
 
147
        entry2 = nih_list_new (NULL);
 
148
 
 
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.
 
151
         */
 
152
        TEST_FEATURE ("with single-entry list");
255
153
        ptr = nih_list_add_after (list, entry1);
256
154
 
257
 
        /* The added entry should be returned */
258
 
        if (ptr != entry1) {
259
 
                printf ("BAD: return value wasn't what we expected.\n");
260
 
                ret = 1;
261
 
        }
262
 
 
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");
266
 
                ret = 1;
267
 
        }
268
 
 
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");
272
 
                ret = 1;
273
 
        }
274
 
 
275
 
        /* New entry's previous pointer should be the head */
276
 
        if (entry1->prev != list) {
277
 
                printf ("BAD: entry prev pointer set incorrectly.\n");
278
 
                ret = 1;
279
 
        }
280
 
 
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");
284
 
                ret = 1;
285
 
        }
286
 
 
287
 
 
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);
 
160
 
 
161
 
 
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)
 
165
         */
 
166
        TEST_FEATURE ("with multi-entry list");
289
167
        nih_list_add_after (list, entry2);
290
168
 
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");
294
 
                ret = 1;
295
 
        }
296
 
 
297
 
        /* Head entry's previous pointer should be unchanged. */
298
 
        if (list->prev != entry1) {
299
 
                printf ("BAD: head prev pointer changed.\n");
300
 
                ret = 1;
301
 
        }
302
 
 
303
 
        /* New entry's previous pointer should be the head */
304
 
        if (entry2->prev != list) {
305
 
                printf ("BAD: entry prev pointer set incorrectly.\n");
306
 
                ret = 1;
307
 
        }
308
 
 
309
 
        /* New entry's next pointer should be the tail */
310
 
        if (entry2->next != entry1) {
311
 
                printf ("BAD: entry next pointer set incorrectly.\n");
312
 
                ret = 1;
313
 
        }
314
 
 
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");
318
 
                ret = 1;
319
 
        }
320
 
 
321
 
        /* Tail entry's next pointer should be unchanged. */
322
 
        if (entry1->next != list) {
323
 
                printf ("BAD: tail next pointer changed.\n");
324
 
                ret = 1;
325
 
        }
326
 
 
327
 
 
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);
 
175
 
 
176
 
 
177
        /* Check that nih_list_add_after can be used to swap two entries
 
178
         * around.
 
179
         */
 
180
        TEST_FEATURE ("with two entries from same list");
329
181
        nih_list_add_after (entry1, entry2);
330
182
 
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");
334
 
                ret = 1;
335
 
        }
336
 
 
337
 
        /* entry1's previous pointer should be the head entry */
338
 
        if (entry1->prev != list) {
339
 
                printf ("BAD: entry1 prev pointer set incorrectly.\n");
340
 
                ret = 1;
341
 
        }
342
 
 
343
 
        /* entry1's next pointer should be entry2 */
344
 
        if (entry1->next != entry2) {
345
 
                printf ("BAD: entry1 next pointer set incorrectly.\n");
346
 
                ret = 1;
347
 
        }
348
 
 
349
 
        /* entry2's previous pointer should be entry1 */
350
 
        if (entry2->prev != entry1) {
351
 
                printf ("BAD: entry2 prev pointer set incorrectly.\n");
352
 
                ret = 1;
353
 
        }
354
 
 
355
 
        /* entry2's next pointer should be the head entry */
356
 
        if (entry2->next != list) {
357
 
                printf ("BAD: entry2 next pointer set incorrectly.\n");
358
 
                ret = 1;
359
 
        }
360
 
 
361
 
        /* head entry's previous pointer should be entry2 */
362
 
        if (list->prev != entry2) {
363
 
                printf ("BAD: head prev pointer set incorrectly.\n");
364
 
                ret = 1;
365
 
        }
366
 
 
367
 
 
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);
 
189
 
 
190
 
 
191
        /* Check that nih_list_add_after can rip an entry out of its
 
192
         * containing list, and add it to a new one.
 
193
         */
 
194
        TEST_FEATURE ("with entry from other list");
 
195
        ptr = nih_list_new (NULL);
370
196
        nih_list_add_after (ptr, entry1);
371
197
 
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");
376
 
                ret = 1;
377
 
        }
 
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);
378
202
 
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");
383
 
                ret = 1;
384
 
        }
 
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);
385
207
 
386
208
        nih_free (list);
387
209
        nih_free (entry1);
388
210
        nih_free (entry2);
389
211
        nih_free (ptr);
390
 
 
391
 
        return ret;
392
212
}
393
213
 
394
 
int
 
214
void
395
215
test_empty (void)
396
216
{
397
217
        NihList *list, *entry;
398
 
        int      ret = 0;
399
 
 
400
 
        printf ("Testing NIH_LIST_EMPTY()\n");
401
 
        list = nih_list_new ();
402
 
        entry = nih_list_new ();
403
 
 
404
 
        printf ("...with empty list\n");
405
 
 
406
 
        /* The list should be empty */
407
 
        if (! NIH_LIST_EMPTY (list)) {
408
 
                printf ("BAD: return value wasn't what we expected.\n");
409
 
                ret = 1;
410
 
        }
411
 
 
412
 
 
413
 
        printf ("...with non-empty list\n");
 
218
 
 
219
        TEST_FUNCTION ("NIH_LIST_EMPTY");
 
220
 
 
221
        /* Check that NIH_LIST_EMPTY is TRUE on an empty list */
 
222
        TEST_FEATURE ("with empty list");
 
223
        list = nih_list_new (NULL);
 
224
 
 
225
        TEST_LIST_EMPTY (list);
 
226
 
 
227
 
 
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);
415
232
 
416
 
        /* The list should not be empty */
417
 
        if (NIH_LIST_EMPTY (list)) {
418
 
                printf ("BAD: return value wasn't what we expected.\n");
419
 
                ret = 1;
420
 
        }
421
 
 
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");
425
 
                ret = 1;
426
 
        }
 
233
        TEST_LIST_NOT_EMPTY (list);
 
234
        TEST_LIST_NOT_EMPTY (entry);
427
235
 
428
236
        nih_free (list);
429
237
        nih_free (entry);
430
 
 
431
 
        return ret;
432
238
}
433
239
 
434
 
int
 
240
void
435
241
test_foreach (void)
436
242
{
437
243
        NihList *list, *entry[3];
438
 
        int      i, ret = 0;
439
 
 
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 ());
445
 
 
446
 
        /* Perform a test iteration */
 
244
        int      i;
 
245
 
 
246
        /* Check that NIH_LIST_FOREACH iterates the list correctly in
 
247
         * order, visiting each entry.
 
248
         */
 
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));
 
254
 
447
255
        i = 0;
448
256
        NIH_LIST_FOREACH (list, iter) {
449
 
                if (i > 2) {
450
 
                        printf ("BAD: more iterations than expected.\n");
451
 
                        ret = 1;
452
 
                        break;
453
 
                }
 
257
                if (i > 2)
 
258
                        TEST_FAILED ("wrong number of iterations, expected %d got %d",
 
259
                                     3, i + 1);
454
260
 
455
 
                if (iter != entry[i]) {
456
 
                        printf ("BAD: iteration not entry we expected.\n");
457
 
                        ret = 1;
458
 
                }
 
261
                if (iter != entry[i])
 
262
                        TEST_FAILED ("wrong list entry, expected %p got %p",
 
263
                                     entry[i], iter);
459
264
 
460
265
                i++;
461
266
        }
464
269
        nih_free (entry[0]);
465
270
        nih_free (entry[1]);
466
271
        nih_free (entry[2]);
467
 
 
468
 
        return ret;
469
272
}
470
273
 
471
 
int
 
274
void
472
275
test_foreach_safe (void)
473
276
{
474
277
        NihList *list, *entry[3];
475
 
        int      i, ret = 0;
476
 
 
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 ());
482
 
 
483
 
        /* Perform a test iteration */
 
278
        int      i;
 
279
 
 
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
 
282
         * while doing so.
 
283
         */
 
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));
 
289
 
484
290
        i = 0;
485
291
        NIH_LIST_FOREACH_SAFE (list, iter) {
486
 
                if (i > 2) {
487
 
                        printf ("BAD: more iterations than expected.\n");
488
 
                        ret = 1;
489
 
                        break;
490
 
                }
 
292
                if (i > 2)
 
293
                        TEST_FAILED ("wrong number of iterations, expected %d got %d",
 
294
                                     3, i + 1);
491
295
 
492
 
                if (iter != entry[i]) {
493
 
                        printf ("BAD: iteration not entry we expected.\n");
494
 
                        ret = 1;
495
 
                }
 
296
                if (iter != entry[i])
 
297
                        TEST_FAILED ("wrong list entry, expected %p got %p",
 
298
                                     entry[i], iter);
496
299
 
497
300
                nih_list_remove (entry[i]);
498
301
 
499
302
                i++;
500
303
        }
501
304
 
502
 
        /* List should be empty */
503
 
        if (! NIH_LIST_EMPTY (list)) {
504
 
                printf ("BAD: list not empty.\n");
505
 
                ret = 1;
506
 
        }
 
305
 
 
306
        /* Check that the list is now empty */
 
307
        TEST_LIST_EMPTY (list);
507
308
 
508
309
        nih_free (list);
509
310
        nih_free (entry[0]);
510
311
        nih_free (entry[1]);
511
312
        nih_free (entry[2]);
512
 
 
513
 
        return ret;
514
313
}
515
314
 
516
 
int
 
315
void
517
316
test_remove (void)
518
317
{
519
318
        NihList *list, *entry, *tail, *ptr;
520
 
        int      ret = 0;
521
 
 
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 ());
526
 
 
527
 
        printf ("...with two-entry list\n");
 
319
 
 
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));
 
324
 
 
325
        /* Check that nih_list_remove works, returning the entry that was
 
326
         * removed and adjusting both sets of pointers in the lists.
 
327
         */
 
328
        TEST_FEATURE ("with two-entry list");
528
329
        ptr = nih_list_remove (entry);
529
330
 
530
 
        /* The entry that was removed should be returned */
531
 
        if (ptr != entry) {
532
 
                printf ("BAD: return value is not what we expected.\n");
533
 
                ret = 1;
534
 
        }
535
 
 
536
 
        /* The previous pointer should point back to itself */
537
 
        if (entry->prev != entry) {
538
 
                printf ("BAD: prev pointer set incorrectly.\n");
539
 
                ret = 1;
540
 
        }
541
 
 
542
 
        /* The next pointer should point back to itself */
543
 
        if (entry->next != entry) {
544
 
                printf ("BAD: next pointer set incorrectly.\n");
545
 
                ret = 1;
546
 
        }
547
 
 
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");
551
 
                ret = 1;
552
 
        }
553
 
 
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");
557
 
                ret = 1;
558
 
        }
559
 
 
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");
563
 
                ret = 1;
564
 
        }
565
 
 
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");
569
 
                ret = 1;
570
 
        }
571
 
 
572
 
 
573
 
        printf ("...with one-entry list\n");
574
 
        ptr = nih_list_remove (tail);
575
 
 
576
 
        /* The entry that was removed should be returned */
577
 
        if (ptr != tail) {
578
 
                printf ("BAD: return value is not what we expected.\n");
579
 
                ret = 1;
580
 
        }
581
 
 
582
 
        /* The previous pointer should point back to itself */
583
 
        if (tail->prev != tail) {
584
 
                printf ("BAD: prev pointer set incorrectly.\n");
585
 
                ret = 1;
586
 
        }
587
 
 
588
 
        /* The next pointer should point back to itself */
589
 
        if (tail->next != tail) {
590
 
                printf ("BAD: next pointer set incorrectly.\n");
591
 
                ret = 1;
592
 
        }
593
 
 
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");
597
 
                ret = 1;
598
 
        }
599
 
 
600
 
        /* Head entry's previous pointer should point back to itself */
601
 
        if (list->prev != list) {
602
 
                printf ("BAD: head prev pointer changed.\n");
603
 
                ret = 1;
604
 
        }
605
 
 
606
 
 
607
 
        printf ("...with empty list\n");
608
 
        ptr = nih_list_remove (tail);
609
 
 
610
 
        /* The entry that was removed should be returned */
611
 
        if (ptr != tail) {
612
 
                printf ("BAD: return value is not what we expected.\n");
613
 
                ret = 1;
614
 
        }
615
 
 
616
 
        /* The previous pointer should still point back to itself */
617
 
        if (tail->prev != tail) {
618
 
                printf ("BAD: prev pointer set incorrectly.\n");
619
 
                ret = 1;
620
 
        }
621
 
 
622
 
        /* The next pointer should still point back to itself */
623
 
        if (tail->next != tail) {
624
 
                printf ("BAD: next pointer set incorrectly.\n");
625
 
                ret = 1;
626
 
        }
 
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);
 
336
 
 
337
        TEST_EQ_P (entry->next, entry);
 
338
        TEST_EQ_P (entry->prev, entry);
 
339
 
 
340
 
 
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.
 
343
         */
 
344
        TEST_FEATURE ("with one-entry list");
 
345
        ptr = nih_list_remove (tail);
 
346
 
 
347
        TEST_EQ_P (list->next, list);
 
348
        TEST_EQ_P (list->prev, list);
 
349
 
 
350
        TEST_EQ_P (tail->next, tail);
 
351
        TEST_EQ_P (tail->prev, tail);
 
352
 
 
353
 
 
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);
 
357
 
 
358
        TEST_EQ_P (tail->next, tail);
 
359
        TEST_EQ_P (tail->prev, tail);
627
360
 
628
361
        nih_free (list);
629
362
        nih_free (entry);
630
363
        nih_free (tail);
631
 
 
632
 
        return ret;
633
364
}
634
365
 
635
 
int
 
366
void
636
367
test_destructor (void)
637
368
{
638
369
        NihList *list, *entry, *tail;
639
 
        int      ret = 0, retval;
640
 
 
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);
646
 
 
647
 
        /* Zero should be returned */
648
 
        if (retval != 0) {
649
 
                printf ("BAD: return value is not what we expected.\n");
650
 
                ret = 1;
651
 
        }
652
 
 
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");
656
 
                ret = 1;
657
 
        }
658
 
 
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");
662
 
                ret = 1;
663
 
        }
664
 
 
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");
668
 
                ret = 1;
669
 
        }
670
 
 
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");
674
 
                ret = 1;
675
 
        }
 
370
        int      ret;
 
371
 
 
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.
 
375
         */
 
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);
 
381
 
 
382
        TEST_EQ (ret, 0);
 
383
 
 
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);
676
388
 
677
389
        nih_free (entry);
678
390
        nih_free (list);
679
391
        nih_free (tail);
680
 
 
681
 
        return ret;
682
392
}
683
393
 
 
394
 
684
395
static int was_called;
685
396
 
686
397
static int
691
402
        return 0;
692
403
}
693
404
 
694
 
static int
 
405
void
695
406
test_free (void)
696
407
{
697
408
        NihList *list, *entry, *tail;
698
 
        int      ret = 0;
699
 
 
700
 
        list = nih_list_new ();
701
 
        entry = nih_list_add (list, nih_list_new ());
702
 
        tail = nih_list_add (list, nih_list_new ());
703
 
 
704
 
        printf ("Testing nih_list_free()\n");
 
409
        int      ret;
 
410
 
 
411
        /* Check that destructors are called on nih_list_free and the return
 
412
         * value of that destructor is returned; the entry should be cut out
 
413
         * of the list it was in.
 
414
         */
 
415
        TEST_FUNCTION ("nih_list_free");
 
416
        list = nih_list_new (NULL);
 
417
        entry = nih_list_add (list, nih_list_new (NULL));
 
418
        tail = nih_list_add (list, nih_list_new (NULL));
705
419
        nih_alloc_set_destructor (entry, destructor_called);
706
 
        nih_list_free (entry);
707
 
 
708
 
        /* Destructor should have been called */
709
 
        if (! was_called) {
710
 
                printf ("BAD: destructor was not called.\n");
711
 
                ret = 1;
712
 
        }
713
 
 
714
 
        /* Head entry's next pointer should point to the tail entry */
715
 
        if (list->next != tail) {
716
 
                printf ("BAD: head next pointer set incorrectly.\n");
717
 
                ret = 1;
718
 
        }
719
 
 
720
 
        /* Head entry's previous pointer should still point to the tail */
721
 
        if (list->prev != tail) {
722
 
                printf ("BAD: head prev pointer changed.\n");
723
 
                ret = 1;
724
 
        }
725
 
 
726
 
        /* Tail entry's next pointer should still point to the head */
727
 
        if (tail->next != list) {
728
 
                printf ("BAD: tail next pointer changed.\n");
729
 
                ret = 1;
730
 
        }
731
 
 
732
 
        /* Tail entry's previous pointer should point to the head entry */
733
 
        if (tail->prev != list) {
734
 
                printf ("BAD: tail next pointer set incorrectly.\n");
735
 
                ret = 1;
736
 
        }
 
420
        ret = nih_list_free (entry);
 
421
 
 
422
        TEST_EQ (ret, 0);
 
423
        TEST_TRUE (was_called);
 
424
 
 
425
        TEST_EQ_P (list->next, tail);
 
426
        TEST_EQ_P (tail->next, list);
 
427
        TEST_EQ_P (list->prev, tail);
 
428
        TEST_EQ_P (tail->prev, list);
737
429
 
738
430
        nih_free (list);
739
431
        nih_free (tail);
740
 
 
741
 
        return ret;
742
432
}
743
433
 
744
434
 
746
436
main (int   argc,
747
437
      char *argv[])
748
438
{
749
 
        int ret = 0;
750
 
 
751
 
        ret |= test_init ();
752
 
        ret |= test_new ();
753
 
        ret |= test_add ();
754
 
        ret |= test_add_after ();
755
 
        ret |= test_empty ();
756
 
        ret |= test_foreach ();
757
 
        ret |= test_foreach_safe ();
758
 
        ret |= test_remove ();
759
 
        ret |= test_destructor ();
760
 
        ret |= test_free ();
761
 
 
762
 
        return ret;
 
439
        test_init ();
 
440
        test_new ();
 
441
        test_add ();
 
442
        test_add_after ();
 
443
        test_empty ();
 
444
        test_foreach ();
 
445
        test_foreach_safe ();
 
446
        test_remove ();
 
447
        test_destructor ();
 
448
        test_free ();
 
449
 
 
450
        return 0;
763
451
}