File: tlist.c

package info (click to toggle)
diod 1.0.24-5.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 29,200 kB
  • sloc: ansic: 33,159; sh: 6,545; makefile: 363; perl: 80
file content (74 lines) | stat: -rw-r--r-- 1,181 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* tlist.c - exercise list package (valgrind me) */

/* Internal memory allocation in list.c hangs on to some memory that's not
 * freed.  This may not really be worth the hassle, so it's disabled and
 * now this simple test runs valgrind clean.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>

#include "list.h"

#define ITERATIONS  1024

static int objcount = 0;

void oom (void)
{
    fprintf (stderr, "out of memory\n");
    exit (1);
}

void myfree (void *x)
{
    free (x);
    objcount--;
}

char *myalloc (char *s)
{
    char *cpy;

    if (!(cpy = strdup ("xyz")))
        oom ();
    objcount++;

    return cpy;
}

int
main (int argc, char *argv[])
{
    List l;
    char *p;
    int i;

    if (!(l = list_create ((ListDelF)myfree)))
        oom ();

    for (i = 0; i < ITERATIONS; i++) {
        if (!(p = myalloc ("xyz")))
            oom ();
        if (!list_append (l, p))
            oom ();
    }

    assert (objcount == i);
    list_destroy (l);
    assert (objcount == 0);

    exit (0);
}

/*
 * vi:tabstop=4 shiftwidth=4 expandtab
 */