-
Notifications
You must be signed in to change notification settings - Fork 55
/
tlist.c
74 lines (57 loc) · 1.15 KB
/
tlist.c
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
/* 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
*/