~jamesodhunt/ubuntu/raring/upstart/1.6

« back to all changes in this revision

Viewing changes to nih/tests/test_alloc.c

  • Committer: Scott James Remnant
  • Date: 2010-02-04 23:39:59 UTC
  • mfrom: (1182.1.45 upstart)
  • mto: This revision was merged to the branch mainline in revision 1250.
  • Revision ID: scott@netsplit.com-20100204233959-7kajqjnaoh7208ob
Tags: upstream-0.6.5
ImportĀ upstreamĀ versionĀ 0.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* libnih
2
 
 *
3
 
 * test_alloc.c - test suite for nih/alloc.c
4
 
 *
5
 
 * Copyright Ā© 2009 Scott James Remnant <scott@netsplit.com>.
6
 
 * Copyright Ā© 2009 Canonical Ltd.
7
 
 *
8
 
 * This program is free software; you can redistribute it and/or modify
9
 
 * it under the terms of the GNU General Public License version 2, as
10
 
 * published by the Free Software Foundation.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License along
18
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#include <nih/test.h>
23
 
 
24
 
#include <malloc.h>
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
 
28
 
#include <nih/macros.h>
29
 
#include <nih/alloc.h>
30
 
#include <nih/list.h>
31
 
 
32
 
 
33
 
static void *
34
 
malloc_null (size_t size)
35
 
{
36
 
        return NULL;
37
 
}
38
 
 
39
 
void
40
 
test_new (void)
41
 
{
42
 
        void *ptr1;
43
 
        void *ptr2;
44
 
 
45
 
        TEST_FUNCTION ("nih_new");
46
 
 
47
 
        /* Check that nih_new works if we don't give it a parent, the block
48
 
         * should be allocated with the size of the type given.
49
 
         */
50
 
        TEST_FEATURE ("with no parent");
51
 
        ptr1 = nih_new (NULL, int);
52
 
 
53
 
        TEST_ALLOC_SIZE (ptr1, sizeof (int));
54
 
        TEST_ALLOC_ORPHAN (ptr1);
55
 
 
56
 
 
57
 
        /* Check that nih_new works if we do give a parent. */
58
 
        TEST_FEATURE ("with parent");
59
 
        ptr2 = nih_new (ptr1, char);
60
 
 
61
 
        TEST_ALLOC_SIZE (ptr2, sizeof (char));
62
 
        TEST_ALLOC_PARENT (ptr2, ptr1);
63
 
 
64
 
        nih_free (ptr1);
65
 
 
66
 
 
67
 
        /* Check that nih_new returns NULL if allocation fails. */
68
 
        TEST_FEATURE ("with failed allocation");
69
 
        __nih_malloc = malloc_null;
70
 
        ptr1 = nih_new (NULL, int);
71
 
        __nih_malloc = malloc;
72
 
 
73
 
        TEST_EQ_P (ptr1, NULL);
74
 
}
75
 
 
76
 
void
77
 
test_alloc (void)
78
 
{
79
 
        void *ptr1;
80
 
        void *ptr2;
81
 
 
82
 
        TEST_FUNCTION ("nih_alloc");
83
 
 
84
 
        /* Check allocation remembers the size, and is possible without
85
 
         * a parent.
86
 
         */
87
 
        TEST_FEATURE ("with no parent");
88
 
        ptr1 = nih_alloc (NULL, 8096);
89
 
        memset (ptr1, 'x', 8096);
90
 
 
91
 
        TEST_ALLOC_SIZE (ptr1, 8096);
92
 
        TEST_ALLOC_ORPHAN (ptr1);
93
 
 
94
 
 
95
 
        /* Check that allocation with a parent remembers the parent */
96
 
        TEST_FEATURE ("with a parent");
97
 
        ptr2 = nih_alloc (ptr1, 10);
98
 
        memset (ptr2, 'x', 10);
99
 
 
100
 
        TEST_ALLOC_SIZE (ptr2, 10);
101
 
        TEST_ALLOC_PARENT (ptr2, ptr1);
102
 
 
103
 
        nih_free (ptr1);
104
 
 
105
 
 
106
 
        /* Check that nih_alloc returns NULL if allocation fails. */
107
 
        TEST_FEATURE ("with failed allocation");
108
 
        __nih_malloc = malloc_null;
109
 
        ptr1 = nih_new (NULL, int);
110
 
        __nih_malloc = malloc;
111
 
 
112
 
        TEST_EQ_P (ptr1, NULL);
113
 
}
114
 
 
115
 
 
116
 
static void *
117
 
realloc_null (void * ptr,
118
 
              size_t size)
119
 
{
120
 
        return NULL;
121
 
}
122
 
 
123
 
void
124
 
test_realloc (void)
125
 
{
126
 
        void *ptr1;
127
 
        void *ptr2;
128
 
        void *ptr3;
129
 
 
130
 
        TEST_FUNCTION ("nih_realloc");
131
 
 
132
 
        /* Check that nih_realloc behaves like nih_alloc if the pointer is
133
 
         * NULL (it should, in fact, just call it)
134
 
         */
135
 
        TEST_FEATURE ("as nih_alloc");
136
 
        ptr1 = nih_realloc (NULL, NULL, 4096);
137
 
        memset (ptr1, 'x', 4096);
138
 
 
139
 
        TEST_ALLOC_SIZE (ptr1, 4096);
140
 
        TEST_ALLOC_ORPHAN (ptr1);
141
 
 
142
 
        nih_free (ptr1);
143
 
 
144
 
 
145
 
        /* Check that nih_realloc works if the block doesn't have a parent.
146
 
         */
147
 
        TEST_FEATURE ("with no parent");
148
 
        ptr1 = nih_alloc (NULL, 4096);
149
 
        memset (ptr1, 'x', 4096);
150
 
 
151
 
        ptr1 = nih_realloc (ptr1, NULL, 8096);
152
 
        memset (ptr1, 'x', 8096);
153
 
 
154
 
        TEST_ALLOC_SIZE (ptr1, 8096);
155
 
        TEST_ALLOC_ORPHAN (ptr1);
156
 
 
157
 
 
158
 
        /* Check that nih_realloc works if the block has a parent, the size
159
 
         * should change but the parent should remain the same.
160
 
         */
161
 
        TEST_FEATURE ("with a parent");
162
 
        ptr2 = nih_alloc (ptr1, 5);
163
 
        memset (ptr2, 'x', 5);
164
 
 
165
 
        ptr2 = nih_realloc (ptr2, ptr1, 10);
166
 
        memset (ptr2, 'x', 10);
167
 
 
168
 
        TEST_ALLOC_SIZE (ptr2, 10);
169
 
        TEST_ALLOC_PARENT (ptr2, ptr1);
170
 
 
171
 
        nih_free (ptr1);
172
 
 
173
 
 
174
 
        /* Check that nih_realloc works if the block being reallocated has
175
 
         * a child.  This is fiddly as they need their parent pointers
176
 
         * adjusted.
177
 
         */
178
 
        TEST_FEATURE ("with a child");
179
 
        ptr1 = nih_alloc (NULL, 128);
180
 
        memset (ptr1, 'x', 128);
181
 
 
182
 
        ptr2 = nih_alloc (ptr1, 512);
183
 
        memset (ptr2, 'x', 512);
184
 
 
185
 
        ptr3 = nih_realloc (ptr1, NULL, 1024);
186
 
        memset (ptr3, 'x', 1024);
187
 
 
188
 
        TEST_ALLOC_PARENT (ptr2, ptr3);
189
 
 
190
 
        nih_free (ptr3);
191
 
 
192
 
 
193
 
        /* Check that nih_realloc returns NULL and doesn't alter the block
194
 
         * if the allocator fails.
195
 
         */
196
 
        TEST_FEATURE ("with failing realloc");
197
 
        ptr1 = nih_alloc (NULL, 10);
198
 
        assert (ptr1);
199
 
        memset (ptr1, 'x', 10);
200
 
 
201
 
        __nih_realloc = realloc_null;
202
 
        ptr2 = nih_realloc (ptr1, NULL, 200);
203
 
        __nih_realloc = realloc;
204
 
 
205
 
        TEST_EQ_P (ptr2, NULL);
206
 
        TEST_ALLOC_SIZE (ptr1, 10);
207
 
 
208
 
        nih_free (ptr1);
209
 
}
210
 
 
211
 
 
212
 
static int destructor_was_called;
213
 
 
214
 
static int
215
 
destructor_called (void *ptr)
216
 
{
217
 
        destructor_was_called++;
218
 
 
219
 
        return 2;
220
 
}
221
 
 
222
 
static int child_destructor_was_called;
223
 
 
224
 
static int
225
 
child_destructor_called (void *ptr)
226
 
{
227
 
        child_destructor_was_called++;
228
 
 
229
 
        return 20;
230
 
}
231
 
 
232
 
typedef struct child {
233
 
        NihList entry;
234
 
        int     invalid;
235
 
} Child;
236
 
 
237
 
typedef struct parent {
238
 
        NihList *list;
239
 
        Child *  child;
240
 
} Parent;
241
 
 
242
 
 
243
 
static void *list_head_ptr = NULL;
244
 
static int   list_head_free = FALSE;
245
 
 
246
 
static void *
247
 
my_list_head_malloc (size_t size)
248
 
{
249
 
        list_head_ptr = malloc (size);
250
 
        list_head_free = FALSE;
251
 
        return list_head_ptr;
252
 
}
253
 
 
254
 
static void
255
 
my_list_head_free (void *ptr)
256
 
{
257
 
        if (ptr == list_head_ptr)
258
 
                list_head_free = TRUE;
259
 
        free (ptr);
260
 
}
261
 
 
262
 
static int
263
 
child_destructor_test (Child *child)
264
 
{
265
 
        TEST_FALSE (list_head_free);
266
 
 
267
 
        return 0;
268
 
}
269
 
 
270
 
void
271
 
test_free (void)
272
 
{
273
 
        void *  ptr1;
274
 
        void *  ptr2;
275
 
        Parent *parent;
276
 
        int     ret;
277
 
 
278
 
        TEST_FUNCTION ("nih_free");
279
 
 
280
 
        /* Check that nih_free works if the block has no parent.  The
281
 
         * destructor should get called and nih_free should return that
282
 
         * return value.
283
 
         */
284
 
        TEST_FEATURE ("with no parent");
285
 
        ptr1 = nih_alloc (NULL, 10);
286
 
        nih_alloc_set_destructor (ptr1, destructor_called);
287
 
        destructor_was_called = 0;
288
 
        ret = nih_free (ptr1);
289
 
 
290
 
        TEST_TRUE (destructor_was_called);
291
 
        TEST_EQ (ret, 2);
292
 
 
293
 
 
294
 
        /* Check that nih_free works if the block has a parent.  The
295
 
         * destructor should get called and nih_free should return that
296
 
         * return value.
297
 
         */
298
 
        TEST_FEATURE ("with parent");
299
 
        ptr2 = nih_alloc (NULL, 20);
300
 
 
301
 
        ptr1 = nih_alloc (ptr2, 10);
302
 
        nih_alloc_set_destructor (ptr1, destructor_called);
303
 
        destructor_was_called = 0;
304
 
        ret = nih_free (ptr1);
305
 
 
306
 
        TEST_TRUE (destructor_was_called);
307
 
        TEST_EQ (ret, 2);
308
 
 
309
 
        nih_free (ptr2);
310
 
 
311
 
 
312
 
        /* Check that the destructor on any children also gets called, which
313
 
         * is as good a indication as any that the children are being freed.
314
 
         */
315
 
        TEST_FEATURE ("with destructor on child");
316
 
        ptr1 = nih_alloc (NULL, 10);
317
 
        ptr2 = nih_alloc (ptr1, 10);
318
 
        nih_alloc_set_destructor (ptr2, child_destructor_called);
319
 
        child_destructor_was_called = 0;
320
 
        ret = nih_free (ptr1);
321
 
 
322
 
        TEST_TRUE (child_destructor_was_called);
323
 
        TEST_EQ (ret, 0);
324
 
 
325
 
 
326
 
        /* Check that both destructors on parent and children are called,
327
 
         * and that the return value from nih_free is that of the parent's.
328
 
         */
329
 
        TEST_FEATURE ("with child and destructors");
330
 
        ptr1 = nih_alloc (NULL, 10);
331
 
        ptr2 = nih_alloc (ptr1, 10);
332
 
        nih_alloc_set_destructor (ptr1, destructor_called);
333
 
        nih_alloc_set_destructor (ptr2, child_destructor_called);
334
 
        destructor_was_called = 0;
335
 
        child_destructor_was_called = 0;
336
 
        ret = nih_free (ptr1);
337
 
 
338
 
        TEST_TRUE (destructor_was_called);
339
 
        TEST_TRUE (child_destructor_was_called);
340
 
        TEST_EQ (ret, 2);
341
 
 
342
 
 
343
 
        /* Check that a child of an object may be included in a sibling
344
 
         * linked list allocated earlier.  At the point the child destructor
345
 
         * is called, the sibling must not have been freed otherwise it
346
 
         * cannot cut itself out.
347
 
         */
348
 
        TEST_FEATURE ("with child in older sibling list");
349
 
        parent = nih_new (NULL, Parent);
350
 
 
351
 
        __nih_malloc = my_list_head_malloc;
352
 
        parent->list = nih_new (parent, NihList);
353
 
        nih_list_init (parent->list);
354
 
        __nih_malloc = malloc;
355
 
 
356
 
        parent->child = nih_new (parent, Child);
357
 
        nih_list_init (&parent->child->entry);
358
 
 
359
 
        nih_list_add (parent->list, &parent->child->entry);
360
 
        nih_alloc_set_destructor (parent->child, child_destructor_test);
361
 
 
362
 
        __nih_free = my_list_head_free;
363
 
        nih_free (parent);
364
 
        __nih_free = free;
365
 
 
366
 
 
367
 
        /* Check that a child of an object may be included in a sibling
368
 
         * linked list allocated later.  At the point the child destructor
369
 
         * is called, the sibling must not have been freed otherwise it
370
 
         * cannot cut itself out.
371
 
         */
372
 
        TEST_FEATURE ("with child in younger sibling list");
373
 
        parent = nih_new (NULL, Parent);
374
 
 
375
 
        parent->child = nih_new (parent, Child);
376
 
        nih_list_init (&parent->child->entry);
377
 
 
378
 
        __nih_malloc = my_list_head_malloc;
379
 
        parent->list = nih_new (parent, NihList);
380
 
        nih_list_init (parent->list);
381
 
        __nih_malloc = malloc;
382
 
 
383
 
        nih_list_add (parent->list, &parent->child->entry);
384
 
        nih_alloc_set_destructor (parent->child, child_destructor_test);
385
 
 
386
 
        __nih_free = my_list_head_free;
387
 
        nih_free (parent);
388
 
        __nih_free = free;
389
 
}
390
 
 
391
 
 
392
 
void
393
 
test_discard (void)
394
 
{
395
 
        void *ptr1;
396
 
        void *ptr2;
397
 
        int   ret;
398
 
 
399
 
        TEST_FUNCTION ("nih_discard");
400
 
 
401
 
        /* Check that nih_discard works if the block has no parent, freeing
402
 
         * the object.  The destructor should get called and nih_discard
403
 
         * should return that return value.
404
 
         */
405
 
        TEST_FEATURE ("with no parent");
406
 
        ptr1 = nih_alloc (NULL, 10);
407
 
        nih_alloc_set_destructor (ptr1, destructor_called);
408
 
        destructor_was_called = 0;
409
 
        ret = nih_discard (ptr1);
410
 
 
411
 
        TEST_TRUE (destructor_was_called);
412
 
        TEST_EQ (ret, 2);
413
 
 
414
 
 
415
 
        /* Check that nih_discard does nothing it the block has a parent.
416
 
         */
417
 
        TEST_FEATURE ("with parent");
418
 
        ptr2 = nih_alloc (NULL, 20);
419
 
 
420
 
        ptr1 = nih_alloc (ptr2, 10);
421
 
        nih_alloc_set_destructor (ptr1, destructor_called);
422
 
        destructor_was_called = 0;
423
 
        ret = nih_discard (ptr1);
424
 
 
425
 
        TEST_FALSE (destructor_was_called);
426
 
        TEST_EQ (ret, 0);
427
 
 
428
 
        nih_free (ptr2);
429
 
 
430
 
 
431
 
        /* Check that the destructor on any children also gets called, which
432
 
         * is as good a indication as any that the children are being freed.
433
 
         */
434
 
        TEST_FEATURE ("with destructor on child");
435
 
        ptr1 = nih_alloc (NULL, 10);
436
 
        ptr2 = nih_alloc (ptr1, 10);
437
 
        nih_alloc_set_destructor (ptr2, child_destructor_called);
438
 
        child_destructor_was_called = 0;
439
 
        ret = nih_discard (ptr1);
440
 
 
441
 
        TEST_TRUE (child_destructor_was_called);
442
 
        TEST_EQ (ret, 0);
443
 
 
444
 
 
445
 
        /* Check that both destructors on parent and children are called,
446
 
         * and that the return value from nih_discard is that of the parent's.
447
 
         */
448
 
        TEST_FEATURE ("with child and destructors");
449
 
        ptr1 = nih_alloc (NULL, 10);
450
 
        ptr2 = nih_alloc (ptr1, 10);
451
 
        nih_alloc_set_destructor (ptr1, destructor_called);
452
 
        nih_alloc_set_destructor (ptr2, child_destructor_called);
453
 
        destructor_was_called = 0;
454
 
        child_destructor_was_called = 0;
455
 
        ret = nih_discard (ptr1);
456
 
 
457
 
        TEST_TRUE (destructor_was_called);
458
 
        TEST_TRUE (child_destructor_was_called);
459
 
        TEST_EQ (ret, 2);
460
 
}
461
 
 
462
 
 
463
 
void
464
 
test_ref (void)
465
 
{
466
 
        void *ptr1;
467
 
        void *ptr2;
468
 
        void *ptr3;
469
 
 
470
 
        TEST_FUNCTION ("nih_ref");
471
 
 
472
 
 
473
 
        /* Check that we can add a reference to an object that has no
474
 
         * parent.
475
 
         */
476
 
        TEST_FEATURE ("with no existing parent");
477
 
        ptr1 = nih_alloc (NULL, 100);
478
 
        memset (ptr1, 'x', 100);
479
 
 
480
 
        ptr2 = nih_alloc (NULL, 100);
481
 
        memset (ptr2, 'y', 100);
482
 
 
483
 
        nih_ref (ptr1, ptr2);
484
 
 
485
 
        TEST_ALLOC_PARENT (ptr1, ptr2);
486
 
 
487
 
        nih_free (ptr2);
488
 
 
489
 
 
490
 
        /* Check that we can add a reference to an object that already has
491
 
         * a parent, and that both shall be parents afterwards.
492
 
         */
493
 
        TEST_FEATURE ("with existing parent");
494
 
        ptr1 = nih_alloc (NULL, 100);
495
 
        memset (ptr1, 'x', 100);
496
 
 
497
 
        ptr2 = nih_alloc (ptr1, 100);
498
 
        memset (ptr2, 'y', 100);
499
 
 
500
 
        ptr3 = nih_alloc (NULL, 100);
501
 
        memset (ptr2, 'z', 100);
502
 
 
503
 
        nih_ref (ptr2, ptr3);
504
 
 
505
 
        TEST_ALLOC_PARENT (ptr2, ptr1);
506
 
        TEST_ALLOC_PARENT (ptr2, ptr3);
507
 
 
508
 
        nih_free (ptr1);
509
 
        nih_free (ptr3);
510
 
}
511
 
 
512
 
void
513
 
test_unref (void)
514
 
{
515
 
        void *ptr1;
516
 
        void *ptr2;
517
 
        void *ptr3;
518
 
 
519
 
        TEST_FUNCTION ("nih_unref");
520
 
 
521
 
 
522
 
        /* Check that we can remove a reference from an object with multiple
523
 
         * parents, which means the object will not be freed.
524
 
         */
525
 
        TEST_FEATURE ("with multiple parents");
526
 
        ptr1 = nih_alloc (NULL, 100);
527
 
        memset (ptr1, 'x', 100);
528
 
 
529
 
        ptr2 = nih_alloc (ptr1, 100);
530
 
        memset (ptr2, 'y', 100);
531
 
 
532
 
        ptr3 = nih_alloc (NULL, 100);
533
 
        memset (ptr2, 'z', 100);
534
 
 
535
 
        nih_ref (ptr2, ptr3);
536
 
 
537
 
        nih_alloc_set_destructor (ptr2, destructor_called);
538
 
        destructor_was_called = 0;
539
 
 
540
 
        nih_unref (ptr2, ptr1);
541
 
 
542
 
        TEST_FALSE (destructor_was_called);
543
 
        TEST_ALLOC_PARENT (ptr2, ptr3);
544
 
 
545
 
        nih_free (ptr1);
546
 
        nih_free (ptr3);
547
 
 
548
 
 
549
 
        /* Check that when we remove the last reference from an object,
550
 
         * the object is freed.
551
 
         */
552
 
        TEST_FEATURE ("with last parent");
553
 
        ptr1 = nih_alloc (NULL, 100);
554
 
        memset (ptr1, 'x', 100);
555
 
 
556
 
        ptr2 = nih_alloc (ptr1, 100);
557
 
        memset (ptr2, 'y', 100);
558
 
 
559
 
        nih_alloc_set_destructor (ptr2, destructor_called);
560
 
        destructor_was_called = 0;
561
 
 
562
 
        nih_unref (ptr2, ptr1);
563
 
 
564
 
        TEST_TRUE (destructor_was_called);
565
 
 
566
 
        nih_free (ptr1);
567
 
}
568
 
 
569
 
 
570
 
void
571
 
test_parent (void)
572
 
{
573
 
        void *ptr1;
574
 
        void *ptr2;
575
 
        void *ptr3;
576
 
 
577
 
        TEST_FUNCTION ("nih_alloc_parent");
578
 
 
579
 
 
580
 
        /* Check that nih_alloc_parent returns TRUE when the passed object
581
 
         * is a child of the passed parent.
582
 
         */
583
 
        TEST_FEATURE ("with child and parent");
584
 
        ptr1 = nih_alloc (NULL, 10);
585
 
        ptr2 = nih_alloc (ptr1, 10);
586
 
 
587
 
        TEST_TRUE (nih_alloc_parent (ptr2, ptr1));
588
 
 
589
 
        nih_free (ptr1);
590
 
 
591
 
 
592
 
        /* Check that nih_alloc_parent returns TRUE when the passed object
593
 
         * has a parent and NULL is passed.
594
 
         */
595
 
        TEST_FEATURE ("with child and NULL");
596
 
        ptr1 = nih_alloc (NULL, 10);
597
 
        ptr2 = nih_alloc (ptr1, 10);
598
 
 
599
 
        TEST_TRUE (nih_alloc_parent (ptr2, ptr1));
600
 
 
601
 
        nih_free (ptr1);
602
 
 
603
 
 
604
 
        /* Check that nih_alloc_parent returns FALSE when the passed object
605
 
         * is a child but not of the passed parent.
606
 
         */
607
 
        TEST_FEATURE ("with child and wrong parent");
608
 
        ptr1 = nih_alloc (NULL, 10);
609
 
        ptr2 = nih_alloc (ptr1, 10);
610
 
        ptr3 = nih_alloc (NULL, 10);
611
 
 
612
 
        TEST_FALSE (nih_alloc_parent (ptr2, ptr3));
613
 
 
614
 
        nih_free (ptr1);
615
 
        nih_free (ptr3);
616
 
 
617
 
 
618
 
        /* Check that nih_alloc_parent returns FALSE when the passed object
619
 
         * is an orphan.
620
 
         */
621
 
        TEST_FEATURE ("with orphan");
622
 
        ptr1 = nih_alloc (NULL, 10);
623
 
        ptr2 = nih_alloc (NULL, 10);
624
 
 
625
 
        TEST_FALSE (nih_alloc_parent (ptr2, ptr1));
626
 
 
627
 
        nih_free (ptr1);
628
 
        nih_free (ptr2);
629
 
 
630
 
 
631
 
        /* Check that nih_alloc_parent returns FALSE when the passed object
632
 
         * is an orphan and NULL is passed.
633
 
         */
634
 
        TEST_FEATURE ("with orphan and NULL");
635
 
        ptr1 = nih_alloc (NULL, 10);
636
 
 
637
 
        TEST_FALSE (nih_alloc_parent (ptr1, NULL));
638
 
 
639
 
        nih_free (ptr1);
640
 
}
641
 
 
642
 
 
643
 
void
644
 
test_local (void)
645
 
{
646
 
        void *parent;
647
 
        void *_ptr;
648
 
 
649
 
        TEST_GROUP ("nih_local");
650
 
 
651
 
 
652
 
        /* Make sure that when a variable goes out of scope, it's freed. */
653
 
        TEST_FEATURE ("with variable going out of scope");
654
 
        do {
655
 
                nih_local void *ptr;
656
 
 
657
 
                ptr = nih_alloc (NULL, 100);
658
 
 
659
 
                nih_alloc_set_destructor (ptr, destructor_called);
660
 
                destructor_was_called = 0;
661
 
        } while (0);
662
 
 
663
 
        TEST_TRUE (destructor_was_called);
664
 
 
665
 
 
666
 
        /* Make sure that if a variable is referenced while in scope, it
667
 
         * is not freed.
668
 
         */
669
 
        TEST_FEATURE ("with referenced variable");
670
 
        parent = nih_alloc (NULL, 100);
671
 
 
672
 
        do {
673
 
                nih_local void *ptr;
674
 
 
675
 
                ptr = nih_alloc (NULL, 100);
676
 
                nih_ref (ptr, parent);
677
 
                _ptr = ptr;
678
 
 
679
 
                nih_alloc_set_destructor (ptr, destructor_called);
680
 
                destructor_was_called = 0;
681
 
        } while (0);
682
 
 
683
 
        TEST_FALSE (destructor_was_called);
684
 
        TEST_ALLOC_PARENT (_ptr, parent);
685
 
 
686
 
        nih_free (parent);
687
 
 
688
 
 
689
 
        /* Make sure we don't need to allocate the variable. */
690
 
        TEST_FEATURE ("with NULL variable");
691
 
        do {
692
 
                nih_local void *ptr = NULL;
693
 
        } while (0);
694
 
}
695
 
 
696
 
 
697
 
int
698
 
main (int   argc,
699
 
      char *argv[])
700
 
{
701
 
        test_new ();
702
 
        test_alloc ();
703
 
        test_realloc ();
704
 
        test_free ();
705
 
        test_discard ();
706
 
        test_ref ();
707
 
        test_unref ();
708
 
        test_parent ();
709
 
        test_local ();
710
 
 
711
 
        return 0;
712
 
}